diff --git a/old/STAR.nvgt b/old/STAR.nvgt index f3ef0f5..7b84734 100644 --- a/old/STAR.nvgt +++ b/old/STAR.nvgt @@ -1,11 +1,14 @@ -// This is the original STAR client written in NVGT. The initial reason that sparked a rewrite to python was that low vision rather than just blind users became interested in this program and wanted a proper GUI, also it just seemed more prudent to make this a purely python project and to let NVGT excelle at what it was intended for which was audio game design rather than utility programs. - #include "form.nvgt" #include "ini.nvgt" #pragma asset audio #pragma asset "STAR.ini" -atomic_bool program_exit = false, server_connected = true; // Initial loop will set server_connected to false when needed. +// Global state variables +atomic_bool program_exit = false, server_connected = true; +int g_next_request_id = 1; +dictionary request_map; + +// Thread-safe message queues class json_message_queue { json_object@[] message_queue; fast_mutex message_queue_mutex; @@ -21,6 +24,7 @@ class json_message_queue { return r; } } + class string_message_queue { string[] message_queue; fast_mutex message_queue_mutex; @@ -36,33 +40,146 @@ class string_message_queue { return r; } } + json_message_queue recv_queue; json_message_queue send_queue; string_message_queue audio_queue; - web_socket@ ws; + +// --- Thread Logic --- void socket_thread() { json_object parse_fail; parse_fail["error"] = "received invalid message from server"; json_object lost_server; lost_server["error"] = "lost server connection..."; + + speak("Socket thread started"); + while (program_exit == false) { try { - http_client cl(config.get_string("", "host", "127.0.0.1"), config.get_double("", "port", 7774)); - http_request req(HTTP_GET, "/"); + speak("Step 1: Parsing Config"); + // Get raw URL + string full_url = config.get_string("", "host", "127.0.0.1"); + if (full_url.is_empty()) { + speak("Error: Host is empty"); + wait(1000); + continue; + } + + // URI Parsing Logic + string scheme = "ws"; + string host = "127.0.0.1"; + int port = 7774; + string username = ""; + string password = ""; + string path = "/"; + + int scheme_idx = full_url.find("://"); + if (scheme_idx > -1) { + scheme = full_url.substr(0, scheme_idx); + full_url = full_url.substr(scheme_idx + 3); + } + + int path_idx = full_url.find("/"); + if (path_idx > -1) { + path = full_url.substr(path_idx); + full_url = full_url.substr(0, path_idx); + } + + int at_idx = full_url.find_last("@"); + if (at_idx > -1) { + string user_info = full_url.substr(0, at_idx); + host = full_url.substr(at_idx + 1); + + int colon = user_info.find(":"); + if (colon > -1) { + username = user_info.substr(0, colon); + password = user_info.substr(colon + 1); + } else { + username = user_info; + } + + username = url_decode(username); + password = url_decode(password); + } else { + host = full_url; + } + + if (host.starts_with("[")) { + int close_bracket = host.find("]"); + if (close_bracket > -1) { + int port_colon = host.find(":", close_bracket); + if (port_colon > -1) { + port = parse_int(host.substr(port_colon + 1)); + host = host.substr(0, close_bracket + 1); + } + } + } else { + int port_idx = host.find_last(":"); + if (port_idx > -1) { + port = parse_int(host.substr(port_idx + 1)); + host = host.substr(0, port_idx); + } else { + port = (scheme == "wss" ? 443 : 80); + } + } + + speak("Step 2: Connecting TCP"); + http_client@ cl; + if (scheme == "wss") { + @cl = https_client(host, port); + } else { + @cl = http_client(host, port); + } + + if (@cl == null) { + speak("Error: Failed to create client"); + wait(1000); + continue; + } + + http_request req(HTTP_GET, path); + + if (username != "") { + string auth = username + ":" + password; + string encoded = string_base64_encode(auth); + req.set("Authorization", "Basic " + encoded); + } + http_response resp; + + speak("Step 3: Websocket Handshake"); @ws = web_socket(cl, req, resp); + + if (@ws == null) { + speak("Error: Websocket creation returned null"); + wait(2000); + continue; + } + + speak("Step 4: Sending Handshake"); int flags; - ws.send_frame("""{"user": 2}"""); + ws.send_frame("""{"user": 4}"""); + server_connected = true; + speak("Connected!"); + recv_queue.enqueue_message(json_object()); - while(program_exit == false) { + + // --- Main Socket Loop (FIXED) --- + while (program_exit == false) { + // IMPORTANT: Wait here to yield CPU to the main thread so the window can draw + wait(5); + json_object@ msg_to_send = null; while ((@msg_to_send = send_queue.dequeue_message()) != null) { - ws.send_frame(msg_to_send.stringify()); + if (@ws != null) ws.send_frame(msg_to_send.stringify()); } - if (ws.poll(timespan(5000), SOCKET_SELECT_READ)) { + + // Poll with a 1000 microsecond (1ms) timeout to be responsive but not blocking + if (@ws != null && ws.poll(timespan(1000), SOCKET_SELECT_READ)) { string frame = ws.receive_frame(flags); + if ((flags & WS_FRAME_OP_BITMASK) == WS_FRAME_OP_CLOSE or flags == 0) { ws.shutdown(); break; @@ -73,80 +190,85 @@ void socket_thread() { audio_queue.enqueue_message(frame); continue; } + json_object@ o; try { @o = parse_json(frame); - } catch { recv_queue.enqueue_message(parse_fail); } - recv_queue.enqueue_message(o); + } catch { + recv_queue.enqueue_message(parse_fail); + continue; + } + if (@o != null) recv_queue.enqueue_message(o); } } } catch { - lost_server["error"] = get_exception_info(); + string err = get_exception_info(); + speak("Crash: " + err); + lost_server["error"] = err; if (server_connected == true) recv_queue.enqueue_message(lost_server); server_connected = false; + wait(2000); } } } +// --- Audio Handling --- void handle_audio(const string&in audio) { - if (audio.length() < 44) return; + if (audio.length() < 4) return; datastream ds(audio.substr(0, 2)); - string id = audio.substr(2, ds.read_uint16()); - string[]@ id_parts = id.split("_"); - bool rendering = parse_int(id_parts[1]) == render_ID; - if (rendering) { - render_count++; - if (render_count < item_count) play("progress", pitch = (float(render_count) / item_count * 100.0) + 60); - else { - play("complete"); - form.set_caption(f_render, "&Render to wav"); - item_count = render_count = 0; + uint16 meta_len = ds.read_uint16(); + string meta_str = audio.substr(2, meta_len); + string actual_audio_data = audio.substr(2 + meta_len); + string req_id = ""; + if (meta_str.starts_with("{")) { + json_object@ meta_json = parse_json(meta_str); + if (@meta_json != null) { + if (meta_json.exists("id")) req_id = string(meta_json["id"]); } - if (!directory_exists("output")) directory_create("output"); - file_put_contents("output/" + id_parts[2] + ".wav", audio.substr(id.length() + 2)); } else { - string data = audio.substr(id.length() + 2); - demo_cache.set(id_parts[1], data); - demo.close(); - demo.push_memory(data, true); - demo.play(); + req_id = meta_str; } + string original_text; + if (request_map.exists(req_id)) { + request_map.get(req_id, original_text); + demo_cache.set(string_hash_sha256(original_text), actual_audio_data); + request_map.delete(req_id); + } + demo.close(); + demo.load_memory(actual_audio_data); + demo.play(); } void send_request(const string&in text, bool rendering) { json_object result; - result["id"] = rendering? render_ID : string_hash_sha256(text, false); - result["user"] = 2; - string[]@ raw_lines = text.split("\r\n", false); - if (rendering) { - form.set_caption(f_render, "Cancel"); - play("begin"); - } - json_array lines; - for (uint i = 0; i < raw_lines.length(); i++) { - string l = raw_lines[i].trim_whitespace(); - if (l.empty() or l.starts_with(";")) continue; - lines.add(l); - } - if (rendering) item_count = lines.length(); - result["request"] = lines; + string this_id = "" + g_next_request_id; + g_next_request_id++; + request_map.set(this_id, text); + result["id"] = this_id; + result["user"] = 4; + result["command"] = "speak"; + result["request"] = text; send_queue.enqueue_message(result); } + void starspeak(const string&in text) { if (text.empty()) { demo.close(); return; } - string data = string_hash_sha256(text); - last_demoed_line = data; - if (demo_cache.exists(data)) { - data = string(demo_cache[data]); + string data_hash = string_hash_sha256(text); + last_demoed_line = data_hash; + if (demo_cache.exists(data_hash)) { + string data = string(demo_cache[data_hash]); demo.close(); - demo.push_memory(data, true); + demo.load_memory(data); demo.play(); - } else send_request(text, false); + } else { + send_request(text, false); + } } +// --- UI and Globals --- audio_form form; sound demo; dictionary demo_cache; @@ -156,6 +278,7 @@ int old_soundcard=0; int render_count = 0, item_count = 0; int f_voices, f_quickspeak, f_script, f_soundcard, f_render, f_stop, f_exit; int render_ID = 0x7fff; + void setup_interface() { show_window("STAR client"); wait(100); @@ -164,14 +287,36 @@ void setup_interface() { f_quickspeak = form.create_input_box("&quickspeak", multiline:true, multiline_enter:false); f_script = form.create_input_box("enter &script", multiline:true); f_soundcard = form.create_list("available &output devices"); - for (uint i=0; i 0? s.play_looped() : s.play(); + for (uint i = 0; i < g_sounds.length(); i++) { + if (g_sounds[i].playing or g_sounds[i].paused) continue; + g_sounds.remove_at(i); + i--; + } + g_sounds.insert_last(s); + return s; +} + +// --- Main --- void main() { async(socket_thread); speak("connecting..."); @@ -186,9 +331,12 @@ void main() { } else break; } else if (@result != null and result.size() == 0) break; } + setup_interface(); + while(!form.is_pressed(f_exit)) { wait(5); + if (form.get_current_focus() == f_script and (keyboard_modifiers & KEYMOD_CTRL) != 0) { int dir = -100; if (key_pressed(KEY_UP)) dir = -1; @@ -208,6 +356,7 @@ void main() { if (!line_text.starts_with(";")) starspeak(line_text); } } + if (form.get_current_focus() == f_voices) { if (keyboard_modifiers & KEYMOD_CTRL > 0 && key_pressed(KEY_C)) { clipboard_set_text(form.get_list_item(f_voices, form.get_list_position(f_voices))); @@ -215,10 +364,12 @@ void main() { } if (key_pressed(KEY_SPACE)) starspeak(form.get_list_item(f_voices, form.get_list_position(f_voices)) + ": Hello there, my name is " + form.get_list_item(f_voices, form.get_list_position(f_voices))); } + if (form.get_current_focus() == f_soundcard and old_soundcard!=form.get_list_position(f_soundcard)) { old_soundcard=form.get_list_position(f_soundcard); sound_output_device=old_soundcard; } + if (form.get_current_focus() == f_quickspeak and keyboard_modifiers & KEYMOD_CTRL == 0 and keyboard_modifiers & KEYMOD_SHIFT == 0) { if (key_pressed(KEY_RETURN)) { string text = form.get_text(f_quickspeak); @@ -227,22 +378,24 @@ void main() { else speak("Select a voice, type text and press enter to speak it"); } } + form.monitor(); + if (form.is_pressed(f_stop)) { if (demo.playing) demo.stop(); } + if (form.is_pressed(f_render)) { - if (item_count > 0) { - speak("Rendering in progress, will add canceling ability soon"); - continue; - } string text = form.get_text(f_script).trim_whitespace_this(); - if (!text.empty()) send_request(text, true); + if (!text.empty()) speak("Rendering not supported in this version."); } + string audio = audio_queue.dequeue_message(); if (!audio.empty()) handle_audio(audio); + json_object@ msg = recv_queue.dequeue_message(); if (@msg == null) continue; + if (msg.exists("voices")) { play("ready"); json_array@ v = msg["voices"]; @@ -257,16 +410,18 @@ void main() { } int old_index=form.get_list_position(f_voices); form.clear_list(f_voices); - for (uint i = 0; i < v.length(); i++) form.add_list_item(f_voices, v[i]); + for (uint i = 0; i < v.length(); i++) { + string v_name = ""; + if (v[i].get_is_string()) v_name = string(v[i]); + else if (v.is_object(i)) { + json_object@ v_obj = v[i]; + if (v_obj.exists("name")) v_name = string(v_obj["name"]); + } + if (!v_name.empty()) form.add_list_item(f_voices, v_name); + } if (old_index 0) render_count++; - if (render_count == item_count and item_count > 0) { - play("complete"); - form.set_caption(f_render, "&Render to wav"); - item_count = render_count = 0; - } if (msg.exists("warning")) speak(msg["warning"], false); else if (msg.exists("status")) speak(msg["status"], false); } else if (msg.exists("error")) { @@ -276,20 +431,3 @@ void main() { } program_exit = true; } - -sound@[] g_sounds; -enum sound_flags { SOUND_LOOPING, SOUND_PAUSED } -sound@ play(const string&in filename, int64 flags = 0, double pitch = 100) { - sound s; - if (!s.load("audio/" + filename + ".ogg")) return null; - s.pitch = pitch; - if (flags & SOUND_PAUSED == 0) - flags & SOUND_LOOPING > 0? s.play_looped() : s.play(); - for (uint i = 0; i < g_sounds.length(); i++) { - if (g_sounds[i].playing or g_sounds[i].paused) continue; - g_sounds.remove_at(i); - i--; - } - g_sounds.insert_last(s); - return s; -} diff --git a/provider/android/.gitignore b/provider/android/.gitignore new file mode 100644 index 0000000..aa724b7 --- /dev/null +++ b/provider/android/.gitignore @@ -0,0 +1,15 @@ +*.iml +.gradle +/local.properties +/.idea/caches +/.idea/libraries +/.idea/modules.xml +/.idea/workspace.xml +/.idea/navEditor.xml +/.idea/assetWizardSettings.xml +.DS_Store +/build +/captures +.externalNativeBuild +.cxx +local.properties diff --git a/provider/android/app/.gitignore b/provider/android/app/.gitignore new file mode 100644 index 0000000..42afabf --- /dev/null +++ b/provider/android/app/.gitignore @@ -0,0 +1 @@ +/build \ No newline at end of file diff --git a/provider/android/app/build.gradle.kts b/provider/android/app/build.gradle.kts new file mode 100644 index 0000000..f204e90 --- /dev/null +++ b/provider/android/app/build.gradle.kts @@ -0,0 +1,97 @@ +// Module-level build file +// build.gradle.kts (Module: app) + +plugins { + // Apply the plugins declared in the root build.gradle.kts + // or directly by their ID if not using the root declaration pattern + id("com.android.application") + id("org.jetbrains.kotlin.android") + // If you had defined an alias for compose, e.g., in libs.versions.toml as + // kotlin-compose = { id = "org.jetbrains.kotlin.plugin.compose", version.ref = "kotlin" } + // You might also add: id("org.jetbrains.kotlin.plugin.compose") + // However, with modern AGP and composeOptions, it's often implicitly handled. +} + +android { + namespace = "com.star.provider" + compileSdk = 35 + + defaultConfig { + applicationId = "com.star.provider" + minSdk = 32 // Ensure this is appropriate for the APIs used + targetSdk = 35 + versionCode = 1 + versionName = "1.0" + + testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" + vectorDrawables { + useSupportLibrary = true + } + } + + buildTypes { + release { + isMinifyEnabled = false // Set to true for actual releases + proguardFiles( + getDefaultProguardFile("proguard-android-optimize.txt"), + "proguard-rules.pro" + ) + } + } + + compileOptions { + sourceCompatibility = JavaVersion.VERSION_1_8 + targetCompatibility = JavaVersion.VERSION_1_8 + } + + kotlinOptions { + jvmTarget = "1.8" + } + + buildFeatures { + compose = true + } + + composeOptions { + // Ensure this version is compatible with your Kotlin and Compose BOM versions + // Refer to: https://developer.android.com/jetpack/androidx/releases/compose-kotlin + kotlinCompilerExtensionVersion = "1.5.3" // Example, update to your required version + } + + packaging { + resources { + excludes += "/META-INF/{AL2.0,LGPL2.1}" + } + } +} + +dependencies { + // Core Android & Jetpack Compose Dependencies (from version catalog) + implementation(libs.androidx.core.ktx) + implementation(libs.androidx.appcompat) + implementation(libs.androidx.recyclerview) + implementation(libs.material) + implementation(libs.androidx.lifecycle.runtime.ktx) + implementation(libs.androidx.activity.compose) + implementation(platform(libs.androidx.compose.bom)) // Ensure this BOM version is up-to-date + implementation(libs.androidx.ui) + implementation(libs.androidx.ui.graphics) + implementation(libs.androidx.ui.tooling.preview) + implementation(libs.androidx.material3) + implementation("androidx.compose.material3:material3:1.2.1") // Or your current M3 version + implementation("androidx.compose.material:material-icons-core:1.6.7") // Or latest + implementation("androidx.compose.material:material-icons-extended:1.6.7") // Or latest - for all icons + // Added Dependencies for STAR Provider Service (explicitly versioned for clarity, or add to TOML) + implementation("com.squareup.okhttp3:okhttp:4.12.0") + implementation("org.json:json:20231013") + implementation("com.google.code.gson:gson:2.10.1") + + // Testing Dependencies (from version catalog) + testImplementation(libs.junit) + androidTestImplementation(libs.androidx.junit) + androidTestImplementation(libs.androidx.espresso.core) + androidTestImplementation(platform(libs.androidx.compose.bom)) // For Compose testing + androidTestImplementation(libs.androidx.ui.test.junit4) + debugImplementation(libs.androidx.ui.tooling) + debugImplementation(libs.androidx.ui.test.manifest) +} \ No newline at end of file diff --git a/provider/android/app/proguard-rules.pro b/provider/android/app/proguard-rules.pro new file mode 100644 index 0000000..481bb43 --- /dev/null +++ b/provider/android/app/proguard-rules.pro @@ -0,0 +1,21 @@ +# Add project specific ProGuard rules here. +# You can control the set of applied configuration files using the +# proguardFiles setting in build.gradle. +# +# For more details, see +# http://developer.android.com/guide/developing/tools/proguard.html + +# If your project uses WebView with JS, uncomment the following +# and specify the fully qualified class name to the JavaScript interface +# class: +#-keepclassmembers class fqcn.of.javascript.interface.for.webview { +# public *; +#} + +# Uncomment this to preserve the line number information for +# debugging stack traces. +#-keepattributes SourceFile,LineNumberTable + +# If you keep the line number information, uncomment this to +# hide the original source file name. +#-renamesourcefileattribute SourceFile \ No newline at end of file diff --git a/provider/android/app/src/androidTest/java/com/star/provider/ExampleInstrumentedTest.kt b/provider/android/app/src/androidTest/java/com/star/provider/ExampleInstrumentedTest.kt new file mode 100644 index 0000000..47e44d6 --- /dev/null +++ b/provider/android/app/src/androidTest/java/com/star/provider/ExampleInstrumentedTest.kt @@ -0,0 +1,24 @@ +package com.star.provider + +import androidx.test.platform.app.InstrumentationRegistry +import androidx.test.ext.junit.runners.AndroidJUnit4 + +import org.junit.Test +import org.junit.runner.RunWith + +import org.junit.Assert.* + +/** + * Instrumented test, which will execute on an Android device. + * + * See [testing documentation](http://d.android.com/tools/testing). + */ +@RunWith(AndroidJUnit4::class) +class ExampleInstrumentedTest { + @Test + fun useAppContext() { + // Context of the app under test. + val appContext = InstrumentationRegistry.getInstrumentation().targetContext + assertEquals("com.star.provider", appContext.packageName) + } +} \ No newline at end of file diff --git a/provider/android/app/src/main/AndroidManifest.xml b/provider/android/app/src/main/AndroidManifest.xml new file mode 100644 index 0000000..93d7db2 --- /dev/null +++ b/provider/android/app/src/main/AndroidManifest.xml @@ -0,0 +1,46 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/provider/android/app/src/main/java/com/star/provider/MainActivity.kt b/provider/android/app/src/main/java/com/star/provider/MainActivity.kt new file mode 100644 index 0000000..9187a75 --- /dev/null +++ b/provider/android/app/src/main/java/com/star/provider/MainActivity.kt @@ -0,0 +1,649 @@ +package com.star.provider + +import android.content.ComponentName +import android.content.Context +import android.content.Intent +import android.content.ServiceConnection +import android.content.SharedPreferences +import android.os.Build +import android.os.Bundle +import android.os.IBinder +import android.util.Log +import androidx.activity.ComponentActivity +import androidx.activity.compose.setContent +import androidx.activity.result.contract.ActivityResultContracts +import androidx.compose.foundation.background +import androidx.compose.foundation.clickable +import androidx.compose.foundation.layout.* +import androidx.compose.foundation.lazy.LazyColumn +import androidx.compose.foundation.lazy.items +import androidx.compose.foundation.lazy.itemsIndexed +import androidx.compose.foundation.lazy.rememberLazyListState +import androidx.compose.foundation.rememberScrollState +import androidx.compose.foundation.text.KeyboardActions +import androidx.compose.foundation.text.KeyboardOptions +import androidx.compose.foundation.verticalScroll +import androidx.compose.material.icons.Icons +import androidx.compose.material.icons.filled.Add +import androidx.compose.material.icons.filled.Build +import androidx.compose.material.icons.filled.Delete +import androidx.compose.material.icons.filled.Settings +import androidx.compose.material3.* +import androidx.compose.runtime.* +import androidx.compose.ui.Alignment +import androidx.compose.ui.Modifier +import androidx.compose.ui.platform.LocalFocusManager +import androidx.compose.ui.semantics.CollectionInfo +import androidx.compose.ui.semantics.CollectionItemInfo +import androidx.compose.ui.semantics.CustomAccessibilityAction +import androidx.compose.ui.semantics.ScrollAxisRange +import androidx.compose.ui.semantics.clearAndSetSemantics +import androidx.compose.ui.semantics.collectionInfo +import androidx.compose.ui.semantics.collectionItemInfo +import androidx.compose.ui.semantics.contentDescription +import androidx.compose.ui.semantics.customActions +import androidx.compose.ui.semantics.horizontalScrollAxisRange +import androidx.compose.ui.semantics.onClick +import androidx.compose.ui.semantics.semantics +import androidx.compose.ui.semantics.verticalScrollAxisRange +import androidx.compose.ui.text.TextStyle +import androidx.compose.ui.text.font.FontFamily +import androidx.compose.ui.text.font.FontWeight +import androidx.compose.ui.text.input.ImeAction +import androidx.compose.ui.text.input.KeyboardType +import androidx.compose.ui.tooling.preview.Preview +import androidx.compose.ui.unit.dp +import androidx.compose.ui.unit.sp +import androidx.compose.ui.window.Dialog +import androidx.core.content.ContextCompat +import com.star.provider.ui.theme.StarProviderTheme +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.delay +import kotlinx.coroutines.launch +import kotlinx.coroutines.withContext +import java.util.Locale +import com.star.provider.ServiceStateListener +import com.star.provider.ServiceLogListener +import com.star.provider.PersistedVoiceConfig +import com.star.provider.DialogVoiceInfo +import com.star.provider.DialogEngineInfo + +const val PREFS_NAME = "StarProviderPrefs" +const val KEY_SERVER_URLS = "server_urls" +const val KEY_PROVIDER_NAME = "provider_name" + +class MainActivity : ComponentActivity(), ServiceStateListener, ServiceLogListener { + + private var starProviderService: StarProviderService? = null + private var serviceBinder: StarProviderService.LocalBinder? = null + private var isBound = false + private lateinit var sharedPreferences: SharedPreferences + + private val _serverUrls = mutableStateListOf() + private val _providerName = mutableStateOf("MyAndroidTTS") + private val _currentStatus = mutableStateOf("Status: Disconnected") + private val _logMessages = mutableStateOf("Logs will appear here...") + private val _isServiceRunningState = mutableStateOf(false) + private val _showVoiceConfigDialog = mutableStateOf(false) + private val _showEngineConfigDialog = mutableStateOf(false) + + private val connection = object : ServiceConnection { + override fun onServiceConnected(className: ComponentName, service: IBinder) { + serviceBinder = service as StarProviderService.LocalBinder + starProviderService = serviceBinder?.getService() + isBound = true + Log.d("MainActivity", "Service connected") + serviceBinder?.registerStateListener(this@MainActivity) + serviceBinder?.registerLogListener(this@MainActivity) + starProviderService?.requestCurrentStatus() + } + + override fun onServiceDisconnected(arg0: ComponentName) { + if (isBound) { + serviceBinder?.unregisterStateListener(this@MainActivity) + serviceBinder?.unregisterLogListener(this@MainActivity) + isBound = false + serviceBinder = null + starProviderService = null + Log.d("MainActivity", "Service disconnected") + } + _currentStatus.value = "Status: Service Unbound" + _isServiceRunningState.value = false + } + } + + private val requestPermissionLauncher = registerForActivityResult(ActivityResultContracts.RequestPermission()) { isGranted: Boolean -> + if (isGranted) { Log.i("MainActivity", "Notification permission granted.") } + else { Log.w("MainActivity", "Notification permission denied.") } + } + + override fun onStatusUpdate(status: String, isRunning: Boolean) { CoroutineScope(Dispatchers.Main).launch { _currentStatus.value = status; _isServiceRunningState.value = isRunning } } + override fun onLogMessage(message: String) { CoroutineScope(Dispatchers.Main).launch { _logMessages.value = (_logMessages.value + "\n" + message).takeLast(2000) } } + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + sharedPreferences = getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE) + + val savedUrls = sharedPreferences.getStringSet(KEY_SERVER_URLS, null) + if (savedUrls.isNullOrEmpty()) { + _serverUrls.add("ws://localhost:8765") + } else { + _serverUrls.addAll(savedUrls) + } + + _providerName.value = sharedPreferences.getString(KEY_PROVIDER_NAME, "MyAndroidTTS") ?: "MyAndroidTTS" + + setContent { + StarProviderTheme { + StarProviderScreen( + serverUrls = _serverUrls, + onAddServerUrl = { url -> + val trimmedUrl = url.trim() + if (trimmedUrl.isNotBlank() && !_serverUrls.contains(trimmedUrl)) { + _serverUrls.add(trimmedUrl) + saveUrls() + } + }, + onRemoveServerUrl = { url -> _serverUrls.remove(url); saveUrls() }, + providerName = _providerName.value, + onProviderNameChange = { _providerName.value = it; sharedPreferences.edit().putString(KEY_PROVIDER_NAME, it).apply() }, + currentStatus = _currentStatus.value, + logMessages = _logMessages.value, + isServiceRunning = _isServiceRunningState.value, + onStartStopClick = { toggleService() }, + onConfigureVoicesClick = { _showVoiceConfigDialog.value = true }, + onConfigureEnginesClick = { _showEngineConfigDialog.value = true } + ) + if (_showVoiceConfigDialog.value) { + VoiceConfigurationDialog(onDismiss = { _showVoiceConfigDialog.value = false }, starProviderService = starProviderService) + } + if (_showEngineConfigDialog.value) { + EngineConfigurationDialog(onDismiss = { _showEngineConfigDialog.value = false }, starProviderService = starProviderService) + } + } + } + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { requestNotificationPermission() } + } + + private fun saveUrls() { + sharedPreferences.edit().putStringSet(KEY_SERVER_URLS, _serverUrls.toSet()).apply() + } + + override fun onStart() { + super.onStart() + Intent(this, StarProviderService::class.java).also { intent -> + try { + bindService(intent, connection, Context.BIND_AUTO_CREATE) + } catch (e: SecurityException) { + Log.e("MainActivity", "Failed to bind to service", e) + _currentStatus.value = "Error: Cannot bind to service." + } + } + } + + override fun onStop() { + super.onStop() + if (isBound) { + serviceBinder?.unregisterStateListener(this) + serviceBinder?.unregisterLogListener(this) + unbindService(connection) + isBound = false + } + } + + private fun requestNotificationPermission() { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { + requestPermissionLauncher.launch(android.Manifest.permission.POST_NOTIFICATIONS) + } + } + + private fun startProviderService() { + saveUrls() + sharedPreferences.edit().putString(KEY_PROVIDER_NAME, _providerName.value).apply() + + val serviceIntent = Intent(this, StarProviderService::class.java).apply { + putStringArrayListExtra(StarProviderService.EXTRA_SERVER_URLS, ArrayList(_serverUrls)) + putExtra("PROVIDER_NAME", _providerName.value) + } + try { + ContextCompat.startForegroundService(this, serviceIntent) + if (!isBound) { + bindService(serviceIntent, connection, Context.BIND_AUTO_CREATE) + } + } catch (e: Exception) { + Log.e("MainActivity", "Error starting service", e) + _currentStatus.value = "Error: Could not start service. ${e.message}" + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S && e is SecurityException) { + _currentStatus.value = "Error: FG Service start restricted by OS." + } + } + } + + private fun stopProviderService() { + starProviderService?.stopServiceInternal() ?: run { + val serviceIntent = Intent(this, StarProviderService::class.java) + try { + stopService(serviceIntent) + Log.i("MainActivity", "stopService() called directly.") + } catch (e: Exception) { + Log.e("MainActivity", "Error calling stopService()", e) + } + } + if (!isBound) { + _currentStatus.value = "Status: Disconnected" + _isServiceRunningState.value = false + } + } + + private fun toggleService() { + if (_isServiceRunningState.value) { + stopProviderService() + } else { + startProviderService() + } + } +} + +@OptIn(ExperimentalMaterial3Api::class) +@Composable +fun StarProviderScreen( + serverUrls: List, onAddServerUrl: (String) -> Unit, onRemoveServerUrl: (String) -> Unit, + providerName: String, onProviderNameChange: (String) -> Unit, + currentStatus: String, logMessages: String, isServiceRunning: Boolean, + onStartStopClick: () -> Unit, onConfigureVoicesClick: () -> Unit, onConfigureEnginesClick: () -> Unit +) { + val logScrollState = rememberScrollState() + + Surface(modifier = Modifier.fillMaxSize(), color = MaterialTheme.colorScheme.background) { + LazyColumn( + modifier = Modifier + .fillMaxSize() + .padding(16.dp), + horizontalAlignment = Alignment.CenterHorizontally + ) { + item { Text("STAR Android TTS Provider", style = MaterialTheme.typography.headlineSmall, modifier = Modifier.padding(bottom = 16.dp)) } + + item { Text("Coagulator Hosts", style = MaterialTheme.typography.titleMedium, modifier = Modifier.fillMaxWidth()) + Spacer(Modifier.height(8.dp)) + } + + items(serverUrls, key = { it }) { url -> + var showEditDialog by remember { mutableStateOf(false) } + + if (showEditDialog) { + EditHostDialog( + currentUrl = url, + onDismiss = { showEditDialog = false }, + onConfirm = { newUrl -> + if (newUrl.isNotBlank() && newUrl != url) { + onRemoveServerUrl(url) + onAddServerUrl(newUrl) + } + showEditDialog = false + } + ) + } + + Column( + modifier = Modifier + .fillMaxWidth() + .background(MaterialTheme.colorScheme.surfaceVariant.copy(alpha = 0.2f)) + .clickable( + onClickLabel = "edit info", + onClick = { showEditDialog = true } + ) + .semantics { + contentDescription = "Host $url" + customActions = listOf( + CustomAccessibilityAction("Remove") { + onRemoveServerUrl(url) + true + } + ) + } + ) { + Row( + verticalAlignment = Alignment.CenterVertically, + modifier = Modifier + .padding(horizontal = 8.dp, vertical = 4.dp) + .fillMaxWidth() + .clearAndSetSemantics { } + ) { + Text( + text = url, + modifier = Modifier + .weight(1f) + .padding(vertical = 12.dp) + ) + IconButton( + onClick = { onRemoveServerUrl(url) } + ) { + Icon(Icons.Default.Delete, contentDescription = null) + } + } + HorizontalDivider() + } + } + + item { + var newHostUrl by remember { mutableStateOf("ws://") } + val focusManager = LocalFocusManager.current + OutlinedTextField( + value = newHostUrl, onValueChange = { newHostUrl = it }, + label = { Text("Add new host URL") }, singleLine = true, + keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Uri, imeAction = ImeAction.Done), + keyboardActions = KeyboardActions(onDone = { onAddServerUrl(newHostUrl); newHostUrl="ws://"; focusManager.clearFocus() }), + modifier = Modifier.fillMaxWidth().padding(top=4.dp), + trailingIcon = { IconButton(onClick = { onAddServerUrl(newHostUrl); newHostUrl="ws://"; focusManager.clearFocus() }) { Icon(Icons.Default.Add, "Add Host") } } + ) + Spacer(modifier = Modifier.height(16.dp)) + } + + item { + OutlinedTextField(value = providerName, onValueChange = onProviderNameChange, label = { Text("Provider Name") }, singleLine = true, modifier = Modifier.fillMaxWidth()) + Spacer(modifier = Modifier.height(16.dp)) + } + + item { + Row(Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.SpaceBetween, verticalAlignment = Alignment.CenterVertically) { + Button(onClick = onStartStopClick, modifier = Modifier.weight(1f)) { Text(if (isServiceRunning) "Stop Provider" else "Start Provider") } + Spacer(Modifier.width(8.dp)) + IconButton(onClick = onConfigureEnginesClick) { Icon(Icons.Filled.Build, contentDescription = "Configure Engines") } + IconButton(onClick = onConfigureVoicesClick) { Icon(Icons.Filled.Settings, contentDescription = "Configure Voices") } + } + Spacer(modifier = Modifier.height(16.dp)) + } + + item { + Text(currentStatus, style = MaterialTheme.typography.bodyLarge, modifier = Modifier.fillMaxWidth()) + Spacer(modifier = Modifier.height(16.dp)) + Text("Logs:", style = MaterialTheme.typography.titleSmall, modifier = Modifier.fillMaxWidth()) + } + + item { + Box(modifier = Modifier.fillMaxWidth().height(200.dp).background(MaterialTheme.colorScheme.surfaceVariant.copy(alpha = 0.3f)).padding(8.dp)) { Text(text = logMessages, modifier = Modifier.fillMaxSize().verticalScroll(logScrollState), style = TextStyle(fontFamily = FontFamily.Monospace, fontSize = 12.sp)) + } + } + } + } +} + +@Composable +fun EditHostDialog(currentUrl: String, onDismiss: () -> Unit, onConfirm: (String) -> Unit) { + var editedUrl by remember { mutableStateOf(currentUrl) } + AlertDialog( + onDismissRequest = onDismiss, + title = { Text("Edit Host URL") }, + text = { + OutlinedTextField( + value = editedUrl, + onValueChange = { editedUrl = it }, + label = { Text("Host URL") }, + singleLine = true, + modifier = Modifier.fillMaxWidth(), + keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Uri, imeAction = ImeAction.Done), + keyboardActions = KeyboardActions(onDone = { onConfirm(editedUrl) }) + ) + }, + confirmButton = { Button(onClick = { onConfirm(editedUrl) }) { Text("Save") } }, + dismissButton = { TextButton(onClick = onDismiss) { Text("Cancel") } } + ) +} + +@Composable +fun EngineConfigurationDialog(onDismiss: () -> Unit, starProviderService: StarProviderService?) { + var engineConfigItems by remember { mutableStateOf>(emptyList()) } + var isLoading by remember { mutableStateOf(true) } + + LaunchedEffect(starProviderService) { + isLoading = true + if (starProviderService != null) { + engineConfigItems = withContext(Dispatchers.IO) { + starProviderService.getSystemEnginesForConfiguration() + } + } + isLoading = false + } + + Dialog(onDismissRequest = onDismiss) { + Surface(modifier = Modifier.fillMaxWidth(0.95f).fillMaxHeight(0.85f), shape = MaterialTheme.shapes.medium, tonalElevation = AlertDialogDefaults.TonalElevation) { + Column(modifier = Modifier.padding(16.dp)) { + Text("Configure TTS Engines", style = MaterialTheme.typography.headlineSmall, modifier = Modifier.padding(bottom = 16.dp)) + + if (isLoading) { + Box(modifier = Modifier.fillMaxWidth().weight(1f), contentAlignment = Alignment.Center) { CircularProgressIndicator() } + } else if (engineConfigItems.isEmpty()) { + Box(modifier = Modifier.fillMaxWidth().weight(1f), contentAlignment = Alignment.Center) { + Text("No TTS engines found or service not ready.", modifier = Modifier.padding(16.dp)) + } + } else { + val listState = rememberLazyListState() + LazyColumn( + modifier = Modifier + .fillMaxWidth() + .weight(1f) + .semantics { + collectionInfo = CollectionInfo(rowCount = engineConfigItems.size, columnCount = 1) + verticalScrollAxisRange = ScrollAxisRange( + value = { listState.firstVisibleItemIndex.toFloat() }, + maxValue = { engineConfigItems.size.toFloat() } + ) + horizontalScrollAxisRange = ScrollAxisRange(value = { 0f }, maxValue = { 0f }) + }, + state = listState + ) { + itemsIndexed(engineConfigItems) { index, engine -> + EngineItem( + engine = engine, + onEnabledChange = { pkg, isEnabled -> + engineConfigItems = engineConfigItems.map { + if (it.packageName == pkg) it.copy(isEnabled = isEnabled) else it + } + }, + index = index + ) + } + } + } + + Spacer(modifier = Modifier.height(16.dp)) + Row(modifier = Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.End) { + TextButton(onClick = onDismiss) { Text("Cancel") } + Spacer(modifier = Modifier.width(8.dp)) + Button(onClick = { + val configsToSave = engineConfigItems.associate { it.packageName to it.isEnabled } + starProviderService?.saveAndReloadEngineConfigs(configsToSave) + onDismiss() + }, enabled = !isLoading && engineConfigItems.isNotEmpty()) { Text("Save & Apply") } + } + } + } + } +} + +@Composable +fun EngineItem(engine: DialogEngineInfo, onEnabledChange: (String, Boolean) -> Unit, index: Int) { + Column( + modifier = Modifier + .fillMaxWidth() + .semantics { + collectionItemInfo = CollectionItemInfo(rowIndex = index, rowSpan = 1, columnIndex = 0, columnSpan = 1) + } + ) { + Row( + verticalAlignment = Alignment.CenterVertically, + modifier = Modifier + .padding(vertical = 8.dp, horizontal = 16.dp) + .fillMaxWidth() + ) { + Column(modifier = Modifier.weight(1f)) { + Text(text = engine.label, fontSize = 18.sp, color = MaterialTheme.colorScheme.onSurface) + Text(text = engine.packageName, fontSize = 12.sp, color = MaterialTheme.colorScheme.onSurfaceVariant) + } + Switch( + checked = engine.isEnabled, + onCheckedChange = { onEnabledChange(engine.packageName, it) }, + modifier = Modifier.padding(start = 8.dp) + ) + } + HorizontalDivider() + } +} + + +data class VoiceConfigItem( + val id: String, + val displayName: String, + var alias: String, + var isEnabled: Boolean, + val locale: String, + val isNetwork: Boolean +) + +@OptIn(ExperimentalMaterial3Api::class) +@Composable +fun VoiceConfigurationDialog( + onDismiss: () -> Unit, + starProviderService: StarProviderService? +) { + + var voiceConfigItems by remember { mutableStateOf>(emptyList()) } + var isLoading by remember { mutableStateOf(true) } + + LaunchedEffect(starProviderService) { + isLoading = true + if (starProviderService != null) { + val systemVoicesData = withContext(Dispatchers.IO) { + starProviderService.getSystemVoicesForConfiguration() + } + voiceConfigItems = systemVoicesData.map { dialogInfo -> + val engineShortName = dialogInfo.engineName.split('.').lastOrNull() ?: dialogInfo.engineName + VoiceConfigItem( + id = "${dialogInfo.engineName}:${dialogInfo.originalName}", + displayName = "${dialogInfo.originalName} (${dialogInfo.locale.toLanguageTag()}, Engine: $engineShortName)", + alias = dialogInfo.currentAlias, + isEnabled = dialogInfo.currentIsEnabled, + locale = dialogInfo.locale.toLanguageTag(), + isNetwork = dialogInfo.isNetwork + ) + } + } + isLoading = false + } + + Dialog(onDismissRequest = onDismiss) { + Surface(modifier = Modifier.fillMaxWidth(0.95f).fillMaxHeight(0.85f), shape = MaterialTheme.shapes.medium, tonalElevation = AlertDialogDefaults.TonalElevation) { + Column(modifier = Modifier.padding(16.dp)) { + Text("Configure Voices", style = MaterialTheme.typography.headlineSmall, modifier = Modifier.padding(bottom = 16.dp)) + + if (isLoading) { + Box(modifier = Modifier.fillMaxWidth().weight(1f), contentAlignment = Alignment.Center) { CircularProgressIndicator() } + } else if (voiceConfigItems.isEmpty()) { + Box(modifier = Modifier.fillMaxWidth().weight(1f), contentAlignment = Alignment.Center) { + Text("No voices available. Check TTS engine configuration or logs.", modifier = Modifier.padding(16.dp)) + } + } else { + val listState = rememberLazyListState() + LazyColumn( + modifier = Modifier + .fillMaxWidth() + .weight(1f) + .semantics { + collectionInfo = CollectionInfo(rowCount = voiceConfigItems.size, columnCount = 1) + verticalScrollAxisRange = ScrollAxisRange( + value = { listState.firstVisibleItemIndex.toFloat() }, + maxValue = { voiceConfigItems.size.toFloat() } + ) + horizontalScrollAxisRange = ScrollAxisRange(value = { 0f }, maxValue = { 0f }) + }, + state = listState + ) { + itemsIndexed( + items = voiceConfigItems, + key = { _, item -> item.id } + ) { index, voiceItem -> + VoiceItem( + item = voiceItem, + onAliasChange = { id, newAlias -> + voiceConfigItems = voiceConfigItems.map { if (it.id == id) it.copy(alias = newAlias) else it } + }, + onEnabledChange = { id, isEnabled -> + voiceConfigItems = voiceConfigItems.map { if (it.id == id) it.copy(isEnabled = isEnabled) else it } + }, + index = index + ) + } + } + } + + Spacer(modifier = Modifier.height(16.dp)) + Row(modifier = Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.End) { + TextButton(onClick = onDismiss) { Text("Cancel") } + Spacer(modifier = Modifier.width(8.dp)) + Button( + onClick = { + val configsToSave = voiceConfigItems.mapNotNull { item -> + val idParts = item.id.split(":", limit = 2) + if (idParts.size == 2) { + PersistedVoiceConfig(originalName = idParts[1], engineName = idParts[0], starLabel = item.alias, isEnabled = item.isEnabled) + } else { + Log.w("MainActivity", "Skipping invalid voice config item with ID: ${item.id}") + null + } + } + starProviderService?.savePersistedVoiceConfigs(configsToSave) + onDismiss() + }, + enabled = !isLoading && voiceConfigItems.isNotEmpty() + ) { Text("Save & Apply") } + } + } + } + } +} + +@Composable +fun VoiceItem( + item: VoiceConfigItem, + onAliasChange: (String, String) -> Unit, + onEnabledChange: (String, Boolean) -> Unit, + index: Int +) { + Column( + modifier = Modifier + .fillMaxWidth() + .semantics { + collectionItemInfo = CollectionItemInfo(rowIndex = index, rowSpan = 1, columnIndex = 0, columnSpan = 1) + } + ) { + Row( + verticalAlignment = Alignment.CenterVertically, + modifier = Modifier + .padding(vertical = 4.dp, horizontal = 16.dp) + .fillMaxWidth() + ) { + Column(modifier = Modifier.weight(1f)) { + Text(text = item.displayName, fontSize = 16.sp, color = MaterialTheme.colorScheme.onSurface) + OutlinedTextField( + value = item.alias, + onValueChange = { onAliasChange(item.id, it) }, + label = { Text("Alias") }, + singleLine = true, + textStyle = TextStyle(fontSize = 14.sp), + modifier = Modifier + .fillMaxWidth() + .padding(top = 2.dp) + ) + } + Switch( + checked = item.isEnabled, + onCheckedChange = { onEnabledChange(item.id, it) }, + modifier = Modifier + .padding(start = 8.dp) + .semantics { contentDescription = "Enable voice ${item.alias.ifBlank { item.displayName }}" } + ) + } + HorizontalDivider() + } +} diff --git a/provider/android/app/src/main/java/com/star/provider/starProviderService.kt b/provider/android/app/src/main/java/com/star/provider/starProviderService.kt new file mode 100644 index 0000000..b4241c6 --- /dev/null +++ b/provider/android/app/src/main/java/com/star/provider/starProviderService.kt @@ -0,0 +1,777 @@ +package com.star.provider + +import android.app.Notification +import android.app.NotificationChannel +import android.app.NotificationManager +import android.app.PendingIntent +import android.app.Service +import android.content.Context +import android.content.Intent +import android.content.SharedPreferences +import android.os.Binder +import android.os.Build +import android.os.Bundle +import android.os.Handler +import android.os.IBinder +import android.os.Looper +import android.speech.tts.TextToSpeech +import android.speech.tts.UtteranceProgressListener +import android.speech.tts.Voice +import android.util.Log +import androidx.core.app.NotificationCompat +import okhttp3.* +import okhttp3.Credentials +import okhttp3.HttpUrl.Companion.toHttpUrlOrNull +import okio.ByteString +import okio.ByteString.Companion.toByteString +import org.json.JSONArray +import org.json.JSONException +import org.json.JSONObject +import java.io.File +import java.io.FileOutputStream +import java.io.IOException +import java.nio.ByteBuffer +import java.nio.ByteOrder +import java.text.SimpleDateFormat +import java.util.Date +import java.util.Locale +import java.util.UUID +import java.util.concurrent.ConcurrentHashMap +import java.util.concurrent.CopyOnWriteArrayList +import java.util.concurrent.TimeUnit +import java.util.concurrent.atomic.AtomicInteger +import kotlin.math.pow +import com.star.provider.R + + +interface ServiceStateListener { + fun onStatusUpdate(status: String, isRunning: Boolean) +} + +interface ServiceLogListener { + fun onLogMessage(message: String) +} + +private data class CoagulatorConnection( + val hostUrl: String, + var webSocket: WebSocket? = null, + var status: String = "Idle", + var reconnectAttempts: Int = 0, + var isManuallyStopped: Boolean = false, + val handler: Handler = Handler(Looper.getMainLooper()), + val client: OkHttpClient +) + +data class PersistedVoiceConfig( + val originalName: String, + val engineName: String, + var starLabel: String, + var isEnabled: Boolean +) + +data class StarVoiceConfig( + val originalName: String, + val engineName: String, + val androidVoice: Voice, + var starLabel: String, + var isEnabled: Boolean = true, + val systemLocaleTag: String = androidVoice.locale.toLanguageTag(), + val systemIsNetwork: Boolean = androidVoice.isNetworkConnectionRequired +) + +data class DialogVoiceInfo( + val originalName: String, + val engineName: String, + val locale: Locale, + val isNetwork: Boolean, + val currentAlias: String, + val currentIsEnabled: Boolean +) + +data class DialogEngineInfo( + val packageName: String, + val label: String, + val isEnabled: Boolean +) + +class StarProviderService : Service() { + + private val binder = LocalBinder() + private val ttsEngines = mutableMapOf() + private val ttsInitializedEngines = mutableSetOf() + private var ttsDiscoveryInstance: TextToSpeech? = null + private var allEnginesDiscovered = false + private val engineLabels = mutableMapOf() + private var allSystemVoices: MutableList = mutableListOf() + private var activeStarVoices: Map = mapOf() + private val connections = ConcurrentHashMap() + private val synthesisQueue = ConcurrentHashMap() + private val utteranceContexts = ConcurrentHashMap() + private val NOTIFICATION_ID = 101 + private val CHANNEL_ID = "StarProviderServiceChannel" + private var providerNameInternal: String = "AndroidProvider" + private val providerRevision = 4 + private var currentStatus: String = "Service Idle" + private var isServiceCurrentlyRunning = false + private var isStartedByCommand = false // Track if Start was clicked + private val canceledRequests = ConcurrentHashMap.newKeySet() + private var isManuallyStopped = false + private val stateListeners = CopyOnWriteArrayList() + private val logListeners = CopyOnWriteArrayList() + private lateinit var sharedPreferences: SharedPreferences + private val VOICE_CONFIG_PREF_KEY = "voice_configurations" + private val ENGINE_CONFIG_PREF_KEY = "engine_configurations" + private val LOG_FILE_NAME = "star_provider_service.log" + private val BACKUP_LOG_FILE_NAME = "star_provider_service.log.1" + private val MAX_LOG_SIZE_BYTES = 1 * 1024 * 1024 + private var logFile: File? = null + + private data class SynthesisRequest( + val starRequestId: String, + val text: String, + val voiceStarLabel: String, + val rate: Float, + val pitch: Float + ) + + inner class LocalBinder : Binder() { + fun getService(): StarProviderService = this@StarProviderService + fun registerStateListener(listener: ServiceStateListener) { + stateListeners.add(listener) + listener.onStatusUpdate(currentStatus, isServiceCurrentlyRunning) + } + fun unregisterStateListener(listener: ServiceStateListener) { stateListeners.remove(listener) } + fun registerLogListener(listener: ServiceLogListener) { logListeners.add(listener) } + fun unregisterLogListener(listener: ServiceLogListener) { logListeners.remove(listener) } + } + + override fun onBind(intent: Intent): IBinder = binder + + override fun onCreate() { + super.onCreate() + Log.d("StarProviderService", "onCreate") + sharedPreferences = getSharedPreferences("StarProviderPrefs", Context.MODE_PRIVATE) + initializeLogFile() + logToFile("Service onCreate") + ttsDiscoveryInstance = TextToSpeech(this) { status -> + if (status == TextToSpeech.SUCCESS) { + discoverAllSystemEnginesAndInitialize() + } else { + val errorMsg = "TTS Discovery Engine failed to initialize. Status: $status" + logToFile("CRITICAL: $errorMsg") + updateStatus(errorMsg, false); stopSelf() + } + } + createNotificationChannel() + } + + override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { + Log.d("StarProviderService", "onStartCommand received") + isManuallyStopped = false + isStartedByCommand = true + val hostUrls = intent?.getStringArrayListExtra(EXTRA_SERVER_URLS) + providerNameInternal = intent?.getStringExtra("PROVIDER_NAME") ?: "MyAndroidTTS" + + if (hostUrls.isNullOrEmpty()) { + val errorMsg = "Error: Server URLs are missing." + logToFile(errorMsg); updateStatus(errorMsg, false); stopSelf() + return START_NOT_STICKY + } + logToFile("Service starting with ${hostUrls.size} hosts: ${hostUrls.joinToString()}") + startForegroundService() + isServiceCurrentlyRunning = true + updateStatus("Service Starting, Initializing...", true) + + connections.clear() + hostUrls.forEach { url -> + val parseableUrl = url.replaceFirst("ws://", "http://", true).replaceFirst("wss://", "https://", true) + val httpUrl = parseableUrl.toHttpUrlOrNull() + if (httpUrl == null) { + logToActivity("Invalid host URL format: $url. Skipping.") + logToFile("ERROR: Invalid host URL format '$url'. Could not parse.") + return@forEach + } + val user = httpUrl.username + val pass = httpUrl.password + val clientBuilder = OkHttpClient.Builder().readTimeout(0, TimeUnit.MILLISECONDS).pingInterval(30, TimeUnit.SECONDS) + if (user.isNotBlank()) { + val credential = Credentials.basic(user, pass) + clientBuilder.authenticator { _, response -> response.request.newBuilder().header("Authorization", credential).build() } + } + connections[url] = CoagulatorConnection(hostUrl = url, client = clientBuilder.build()) + logToFile("Configured connection for: $url") + } + + if (connections.isEmpty()) { + val errorMsg = "No valid host URLs provided." + logToFile(errorMsg); updateStatus(errorMsg, false); stopSelf() + return START_NOT_STICKY + } + + if (isTtsReady()) { + connections.keys.forEach { connectWebSocket(it) } + } else { + logToFile("TTS not ready yet. Connections will trigger automatically when initialization finishes.") + } + return START_STICKY + } + + private fun startForegroundService() { + val notificationIntent = Intent(this, MainActivity::class.java) + val pendingIntentFlags = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT else PendingIntent.FLAG_UPDATE_CURRENT + val pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, pendingIntentFlags) + val notification = NotificationCompat.Builder(this, CHANNEL_ID) + .setContentTitle("STAR Provider Active").setContentText("Initializing...") + .setSmallIcon(R.drawable.ic_stat_name).setContentIntent(pendingIntent) + .setOngoing(true).build() + startForeground(NOTIFICATION_ID, notification) + } + + private fun discoverAllSystemEnginesAndInitialize() { + logToFile("Starting initial discovery of all system TTS engines...") + val allSystemEngineInfos = ttsDiscoveryInstance?.engines ?: run { + updateStatus("Error: Could not query TTS engines.", false); stopSelf(); return + } + if (allSystemEngineInfos.isEmpty()) { + updateStatus("No TTS engines found on this device.", false); stopSelf(); return + } + engineLabels.clear() + allSystemEngineInfos.forEach { engineInfo -> engineLabels[engineInfo.name] = engineInfo.label } + allEnginesDiscovered = true + logToFile("Discovered ${engineLabels.size} total engines: ${engineLabels.values.joinToString()}") + initializeEnabledEngines() + } + + private fun initializeEnabledEngines(onComplete: (() -> Unit)? = null) { + logToFile("Initializing enabled engines...") + val enabledEngineConfigs = loadEngineConfigurations() + val enginesToInitialize = engineLabels.filter { (packageName, _) -> enabledEngineConfigs.getOrDefault(packageName, true) } + logToFile("Engines to be initialized: ${enginesToInitialize.values.joinToString()}") + + if (enginesToInitialize.isEmpty()) { + logToActivity("No engines are enabled.") + populateAvailableVoices() + onComplete?.invoke() + return + } + + val initializationCounter = AtomicInteger(enginesToInitialize.size) + enginesToInitialize.forEach { (enginePackageName, engineLabel) -> + try { + val ttsInstance = TextToSpeech(this, { status -> + if (status == TextToSpeech.SUCCESS) { + ttsInitializedEngines.add(enginePackageName) + ttsEngines[enginePackageName]?.setOnUtteranceProgressListener(StarUtteranceListener()) + logToFile("TTS Engine initialized successfully: $engineLabel") + } else { + logToFile("TTS Engine failed to initialize: $engineLabel, Status: $status") + } + if (initializationCounter.decrementAndGet() == 0) { + logToFile("All requested engines have finished initialization process.") + populateAvailableVoices() + onComplete?.invoke() + } + }, enginePackageName) + ttsEngines[enginePackageName] = ttsInstance + } catch (e: Exception) { + logToFile("Failed to instantiate TTS engine $engineLabel: ${e.message}") + if (initializationCounter.decrementAndGet() == 0) { + logToFile("All requested engines have finished initialization process (with errors).") + populateAvailableVoices() + onComplete?.invoke() + } + } + } + } + + fun saveAndReloadEngineConfigs(newConfigs: Map) { + sharedPreferences.edit().putString(ENGINE_CONFIG_PREF_KEY, JSONObject(newConfigs as Map<*, *>).toString()).apply() + logToFile("Saved updated engine configurations. Performing full state refresh.") + logToActivity("Applying new engine configuration...") + + val onRefreshComplete = { + logToActivity("Engine refresh complete. Repopulating voice list from active engines.") + populateAvailableVoices() + + logToActivity("Forcing reconnection to servers to apply new engine configuration.") + val hostsToReconnect = connections.keys.toList() + hostsToReconnect.forEach { hostUrl -> + connections[hostUrl]?.let { conn -> + conn.webSocket?.close(1000, "Re-registering new engine configuration") + conn.webSocket = null + conn.handler.removeCallbacksAndMessages(null) + conn.reconnectAttempts = 0 + logToFile("Force-reconnecting to $hostUrl to apply engine changes.") + connectWebSocket(hostUrl) + } + } + } + + logToFile("Shutting down all current TTS engines before re-initialization.") + ttsEngines.values.toList().forEach { engine -> + try { + engine.shutdown() + } catch (e: Exception) { + logToFile("Error during engine shutdown: ${e.message}") + } + } + ttsEngines.clear() + ttsInitializedEngines.clear() + logToFile("All running engines shut down and active state cleared.") + + logToFile("Starting re-initialization of enabled engines...") + initializeEnabledEngines(onComplete = onRefreshComplete) + } + + fun getSystemEnginesForConfiguration(): List { + if (!allEnginesDiscovered) return emptyList() + val persistedConfigs = loadEngineConfigurations() + return engineLabels.map { (packageName, label) -> + DialogEngineInfo(packageName, label, persistedConfigs.getOrDefault(packageName, true)) + }.sortedBy { it.label } + } + + private fun loadEngineConfigurations(): Map { + val jsonString = sharedPreferences.getString(ENGINE_CONFIG_PREF_KEY, null) + return if (jsonString != null) { + try { + val json = JSONObject(jsonString) + json.keys().asSequence().associateWith { key -> json.getBoolean(key) } + } catch (e: Exception) { + Log.e("StarProviderService", "Error parsing engine configurations", e); emptyMap() + } + } else { emptyMap() } + } + + fun getSystemVoicesForConfiguration(): List { + if (!isTtsReady() && ttsInitializedEngines.isEmpty()) return emptyList() + val persistedConfigs = loadVoiceConfigurations().associateBy { "${it.engineName}:${it.originalName}" } + return ttsInitializedEngines.flatMap { engineName -> + val tts = ttsEngines[engineName] ?: return@flatMap emptyList() + try { + tts.voices?.mapNotNull { voice -> + val configKey = "$engineName:${voice.name}" + val persisted = persistedConfigs[configKey] + val shortEngineName = getShortEngineName(engineName) + val defaultAlias = "${shortEngineName}_${voice.name}".replace(Regex("[^a-zA-Z0-9_-]" ), "_") + DialogVoiceInfo(voice.name, engineName, voice.locale, voice.isNetworkConnectionRequired, + persisted?.starLabel ?: defaultAlias, persisted?.isEnabled ?: true) + } ?: emptyList() + } catch (e: Exception) { + Log.e("StarProviderService", "Error getting voices for engine $engineName: ${e.message}", e); emptyList() + } + } + } + + fun savePersistedVoiceConfigs(configs: List) { + sharedPreferences.edit().putString(VOICE_CONFIG_PREF_KEY, convertPersistedVoiceConfigListToJson(configs)).apply() + logToFile("Saved updated voice configurations from MainActivity.") + logToActivity("Applying new voice settings...") + + populateAvailableVoices() + + logToActivity("Forcing reconnection to servers to apply new voice list.") + val hostsToReconnect = connections.keys.toList() + hostsToReconnect.forEach { hostUrl -> + connections[hostUrl]?.let { conn -> + conn.webSocket?.close(1000, "Re-registering new voice configuration") + conn.webSocket = null + + conn.handler.removeCallbacksAndMessages(null) + + conn.reconnectAttempts = 0 + + logToFile("Force-reconnecting to $hostUrl to apply voice changes.") + connectWebSocket(hostUrl) + } + } + } + + private fun loadVoiceConfigurations(): List { + val jsonString = sharedPreferences.getString(VOICE_CONFIG_PREF_KEY, null) + return if (jsonString != null) { + try { parsePersistedVoiceConfigListFromJson(jsonString) + } catch (e: Exception) { Log.e("StarProviderService", "Error parsing voice configurations from JSON", e); emptyList() } + } else { emptyList() } + } + + private fun convertPersistedVoiceConfigListToJson(configs: List): String { + val jsonArray = JSONArray() + configs.forEach { config -> + val jsonObj = JSONObject() + jsonObj.put("originalName", config.originalName); jsonObj.put("engineName", config.engineName) + jsonObj.put("starLabel", config.starLabel); jsonObj.put("isEnabled", config.isEnabled) + jsonArray.put(jsonObj) + } + return jsonArray.toString() + } + + private fun parsePersistedVoiceConfigListFromJson(jsonString: String): List { + val configs = mutableListOf() + try { + val jsonArray = JSONArray(jsonString) + for (i in 0 until jsonArray.length()) { + val jsonObj = jsonArray.getJSONObject(i) + configs.add(PersistedVoiceConfig( + originalName = jsonObj.getString("originalName"), engineName = jsonObj.getString("engineName"), + starLabel = jsonObj.getString("starLabel"), isEnabled = jsonObj.getBoolean("isEnabled") + )) + } + } catch (e: JSONException) { Log.e("StarProviderService", "Manual JSON parsing error for voice configs: ${e.message}", e) } + return configs + } + + private fun getShortEngineName(engineName: String): String { + return when { + engineName.contains("google") -> "google" + engineName.contains("rhvoice") -> "rhvoice" + engineName.contains("espeak") -> "espeak" + engineName.contains("dectalk") -> "dectalk" + engineName.contains("samsung") -> "samsung" + else -> engineName.split(".").lastOrNull() ?: "eng" + } + } + + private fun populateAvailableVoices() { + allSystemVoices.clear() + val persistedVoiceConfigs = loadVoiceConfigurations().associateBy { "${it.engineName}:${it.originalName}" } + var voiceDetailsLog = "Populating available voices based on active engines...\n" + val activeEngines = ttsInitializedEngines.toList() + voiceDetailsLog += "Active initialized engines: ${activeEngines.joinToString { engineLabels[it] ?: it }}\n" + activeEngines.forEach { engineName -> + val tts = ttsEngines[engineName] ?: return@forEach + val engineLabel = engineLabels[engineName] ?: "Unknown" + try { + tts.voices?.forEach { voice -> + val configKey = "$engineName:${voice.name}" + val persistedConfig = persistedVoiceConfigs[configKey] + val shortEngineName = getShortEngineName(engineName) + val defaultStarLabel = "${shortEngineName}_${voice.name}".replace(Regex("[^a-zA-Z0-9_-]" ), "_") + val starLabel = persistedConfig?.starLabel ?: defaultStarLabel + val isEnabled = persistedConfig?.isEnabled ?: true + val voiceConfig = StarVoiceConfig(originalName = voice.name, engineName = engineName, androidVoice = voice, starLabel = starLabel, isEnabled = isEnabled) + allSystemVoices.add(voiceConfig) + voiceDetailsLog += " Found voice: ${voice.name} (Engine: $engineLabel), STAR Label: ${voiceConfig.starLabel}, Enabled: ${voiceConfig.isEnabled}\n" + } + } catch (e: Exception) { + voiceDetailsLog += " Error populating voices for $engineName: ${e.message}\n" + } + } + val finalActiveVoices = mutableMapOf() + val usedLabels = mutableSetOf() + allSystemVoices.filter { it.isEnabled }.forEach { config -> + var currentLabel = config.starLabel; var counter = 1 + while(usedLabels.contains(currentLabel)) { currentLabel = "${config.starLabel}_${counter++}" } + usedLabels.add(currentLabel) + finalActiveVoices[currentLabel] = config.copy(starLabel = currentLabel) + } + activeStarVoices = finalActiveVoices + logToFile(voiceDetailsLog.trim()) + + if (isStartedByCommand) { + connections.keys.forEach { hostUrl -> + val conn = connections[hostUrl] + if (conn?.webSocket == null) { + logToFile("TTS is now ready. Starting pending connection for $hostUrl") + connectWebSocket(hostUrl) + } else { + logToFile("Voices updated. Re-registering with $hostUrl") + registerWithCoagulator(conn.webSocket!!) + } + } + } + + if (activeStarVoices.isEmpty()) { + logToActivity("Warning: No active TTS voices found or configured.") + } + updateCombinedStatus() + } + + private fun connectWebSocket(hostUrl: String) { + val connection = connections[hostUrl] + if (connection == null) { logToFile("Error: Attempted to connect to an unconfigured host: $hostUrl"); return } + if (connection.webSocket != null) { logToActivity("WebSocket for $hostUrl already connected or connecting."); return } + val connectMsg = "Attempting to connect to: $hostUrl (Attempt: ${connection.reconnectAttempts + 1})" + logToFile(connectMsg) + val request = Request.Builder().url(hostUrl).build() + connection.client.newWebSocket(request, StarWebSocketListener(hostUrl)) + connection.status = "Connecting... (Attempt ${connection.reconnectAttempts + 1})" + updateCombinedStatus() + } + + private fun scheduleReconnect(hostUrl: String) { + val connection = connections[hostUrl] ?: return + val maxReconnectAttempts = 10 + val initialReconnectDelayMs = 1000L + if (connection.isManuallyStopped || connection.reconnectAttempts >= maxReconnectAttempts) { + logToFile("Max reconnects for $hostUrl or manually stopped. Giving up.") + connection.status = "Disconnected (Max retries)"; updateCombinedStatus(); return + } + val delay = (initialReconnectDelayMs * 2.0.pow(connection.reconnectAttempts.toDouble())).toLong() + connection.reconnectAttempts++ + logToFile("Scheduling reconnect for $hostUrl in ${delay}ms.") + logToActivity("Connection to $hostUrl lost. Reconnecting in ${delay/1000}s...") + connection.status = "Reconnecting (Attempt ${connection.reconnectAttempts})..." + updateCombinedStatus() + connection.handler.postDelayed({ + if (!connection.isManuallyStopped) { + logToFile("Executing reconnect for $hostUrl.") + connectWebSocket(hostUrl) + } else { + logToFile("Reconnect for $hostUrl cancelled.") + } + }, delay) + } + + private fun registerWithCoagulator(webSocket: WebSocket) { + if (!isTtsReady() && activeStarVoices.isEmpty()) { + logToActivity("TTS not fully ready, but sending available voices (${activeStarVoices.size})") + } + val registrationPayload = JSONObject().apply { + put("provider", providerRevision) + put("provider_name", providerNameInternal) + put("voices", JSONArray(activeStarVoices.keys.toList())) + }.toString() + logToFile("TX Registration to ${webSocket.request().url}: $registrationPayload") + val sent = webSocket.send(registrationPayload) + if (sent) { logToActivity("Registration sent to ${webSocket.request().url.host}.") } + else { logToActivity("Failed to send registration to ${webSocket.request().url.host}.") } + } + + private inner class StarWebSocketListener(private val hostUrl: String) : WebSocketListener() { + override fun onOpen(webSocket: WebSocket, response: Response) { + val connection = connections[hostUrl] ?: return + connection.webSocket = webSocket; connection.reconnectAttempts = 0; connection.status = "Connected" + val msg = "WebSocket Connected to $hostUrl" + Log.i("StarProviderService", msg); logToActivity(msg); updateCombinedStatus(); registerWithCoagulator(webSocket) + } + override fun onMessage(webSocket: WebSocket, text: String) { + Log.d("StarProviderService", "RX Text from ${webSocket.request().url.host}: $text"); logToActivity("RX: $text") + try { + val json = JSONObject(text) + if (json.has("abort")) { + val requestIdToCancel = json.optString("abort"); if (requestIdToCancel.isNotEmpty()) { + canceledRequests.add(requestIdToCancel); logToActivity("Abort request for ID: $requestIdToCancel.") + }; return + } + if (!json.has("voice") || !json.has("text") || !json.has("id")) { + logToActivity("Invalid request (missing fields): $text"); return + } + handleSynthesisRequest(json.getString("voice"), json.getString("text"), json.getString("id"), + json.optDouble("rate", 1.0).toFloat(), json.optDouble("pitch", 1.0).toFloat(), webSocket) + } catch (e: JSONException) { logToActivity("Error parsing JSON from server: ${e.message}") } + } + override fun onClosing(webSocket: WebSocket, code: Int, reason: String) { + val msg = "WebSocket Closing for $hostUrl: $code / $reason" + Log.i("StarProviderService", msg); logToActivity(msg); cleanupWebSocket(hostUrl) + if (code != 1000 && !(connections[hostUrl]?.isManuallyStopped ?: false)) { scheduleReconnect(hostUrl) + } else { connections[hostUrl]?.status = "Disconnected"; updateCombinedStatus() } + } + override fun onFailure(webSocket: WebSocket, t: Throwable, response: Response?) { + val msg = "WebSocket Failure for $hostUrl: ${t.message}" + Log.e("StarProviderService", msg, t); logToActivity(msg); cleanupWebSocket(hostUrl) + if (!(connections[hostUrl]?.isManuallyStopped ?: false)) { scheduleReconnect(hostUrl) + } else { connections[hostUrl]?.status = "Disconnected (Failure)"; updateCombinedStatus() } + } + override fun onMessage(webSocket: WebSocket, bytes: ByteString) { val msg = "RX Binary from ${webSocket.request().url.host}: ${bytes.hex()}"; Log.d("StarProviderService", msg); logToActivity(msg) } + } + + private fun cleanupWebSocket(hostUrl: String) { + logToFile("Cleaning up WebSocket for $hostUrl.") + val connection = connections[hostUrl]; connection?.webSocket?.close(1000, "Client closing"); connection?.webSocket = null; updateCombinedStatus() + } + + private fun handleSynthesisRequest(voiceStarLabel: String, textToSpeak: String, starRequestId: String, rate: Float, pitch: Float, sourceSocket: WebSocket) { + logToFile("Handling synthesis ID: $starRequestId from ${sourceSocket.request().url.host}, Voice: $voiceStarLabel, Text: \"${textToSpeak.take(50)}...\"") + if (canceledRequests.contains(starRequestId)) { + canceledRequests.remove(starRequestId); logToActivity("Synthesis for ID $starRequestId canceled by client before starting.") + sendError(starRequestId, "Request canceled by client.", sourceSocket); return + } + if (!isTtsReady()) { sendError(starRequestId, "TTS not ready on Android provider for request $starRequestId.", sourceSocket); return } + val voiceConfig = activeStarVoices[voiceStarLabel] + if (voiceConfig == null) { + val errorMsg = "Voice '$voiceStarLabel' not found or not active. Active: ${activeStarVoices.keys.joinToString()}" + sendError(starRequestId, errorMsg, sourceSocket); logToActivity(errorMsg); return + } + val tts = ttsEngines[voiceConfig.engineName] + if (tts == null) { + val errorMsg = "TTS engine '${voiceConfig.engineName}' for voice '$voiceStarLabel' is not available or failed to initialize." + sendError(starRequestId, errorMsg, sourceSocket); logToActivity(errorMsg); return + } + val utteranceId = UUID.randomUUID().toString() + synthesisQueue[utteranceId] = SynthesisRequest(starRequestId, textToSpeak, voiceStarLabel, rate, pitch) + utteranceContexts[utteranceId] = sourceSocket + tts.voice = voiceConfig.androidVoice; tts.setSpeechRate(rate.coerceIn(0.1f, 4.0f)); tts.setPitch(pitch.coerceIn(0.1f, 4.0f)) + val tempAudioFile = File(cacheDir, "$utteranceId.wav") + logToActivity("Synthesizing for $starRequestId (utt: $utteranceId) using Engine: ${engineLabels[voiceConfig.engineName] ?: "Unknown"}, Voice: ${voiceConfig.androidVoice.name}") + val result = tts.synthesizeToFile(textToSpeak, Bundle(), tempAudioFile, utteranceId) + if (result != TextToSpeech.SUCCESS) { + val errorMsg = "TTS synthesizeToFile failed for $utteranceId (STAR ID: $starRequestId). Code: $result" + Log.e("StarProviderService", errorMsg); logToFile(errorMsg) + synthesisQueue.remove(utteranceId); utteranceContexts.remove(utteranceId) + sendError(starRequestId, "Android TTS synthesis command failed (code: $result).", sourceSocket) + tempAudioFile.delete() + } + } + + private inner class StarUtteranceListener : UtteranceProgressListener() { + override fun onStart(utteranceId: String?) { + Log.d("TTSListener", "Utterance started: $utteranceId") + synthesisQueue[utteranceId]?.let { logToActivity("TTS synthesis started for STAR ID: ${it.starRequestId} (Utt: $utteranceId)") } + } + override fun onDone(utteranceId: String?) { + Log.d("TTSListener", "Utterance done: $utteranceId") + val requestDetails = synthesisQueue.remove(utteranceId) + val sourceSocket = utteranceContexts.remove(utteranceId) + if (utteranceId != null && requestDetails != null && sourceSocket != null) { + if (canceledRequests.contains(requestDetails.starRequestId)) { + canceledRequests.remove(requestDetails.starRequestId) + logToActivity("Synthesis for ID ${requestDetails.starRequestId} completed but was canceled. Audio not sent.") + File(cacheDir, "$utteranceId.wav").delete(); return + } + val audioFile = File(cacheDir, "$utteranceId.wav") + if (audioFile.exists() && audioFile.length() > 0) { + logToActivity("TTS synthesis done for ${requestDetails.starRequestId}, sending audio (${audioFile.length()} bytes).") + sendAudioData(requestDetails.starRequestId, audioFile, sourceSocket) + } else { + sendError(requestDetails.starRequestId, "Synthesized audio file not found or was empty.", sourceSocket) + } + audioFile.delete() + } + } + override fun onError(utteranceId: String?, errorCode: Int) { + val errorDetail = ttsErrorToString(errorCode) + Log.e("TTSListener", "TTSListener: onError for $utteranceId, code: $errorCode ($errorDetail)") + handleTtsError(utteranceId, errorDetail) + } + private fun handleTtsError(utteranceId: String?, errorMessage: String) { + val requestDetails = synthesisQueue.remove(utteranceId) + val sourceSocket = utteranceContexts.remove(utteranceId) + if (utteranceId != null && requestDetails != null && sourceSocket != null) { + if (!canceledRequests.remove(requestDetails.starRequestId)) { + val detailedError = "Android TTS Error for ${requestDetails.starRequestId}: $errorMessage" + sendError(requestDetails.starRequestId, detailedError, sourceSocket) + } + } + File(cacheDir, "$utteranceId.wav").delete() + } + @Deprecated("deprecated", replaceWith = ReplaceWith("onError(utteranceId, errorCode)")) + override fun onError(utteranceId: String?) { onError(utteranceId, -1) } + private fun ttsErrorToString(errorCode: Int): String = when (errorCode) { + TextToSpeech.ERROR_SYNTHESIS -> "Synthesis error"; TextToSpeech.ERROR_SERVICE -> "Service error (TTS engine)" + TextToSpeech.ERROR_OUTPUT -> "Output error"; TextToSpeech.ERROR_NETWORK -> "Network error" + TextToSpeech.ERROR_NETWORK_TIMEOUT -> "Network timeout"; TextToSpeech.ERROR_INVALID_REQUEST -> "Invalid request" + TextToSpeech.ERROR_NOT_INSTALLED_YET -> "TTS data not installed yet"; else -> "Unknown TTS error ($errorCode)" + } + } + + private fun sendAudioData(starRequestId: String, audioFile: File, destinationSocket: WebSocket) { + val audioBytes = audioFile.readBytes() + val metadataJsonString = JSONObject().apply { put("id", starRequestId); put("extension", "wav") }.toString() + val metadataBytes = metadataJsonString.toByteArray(Charsets.UTF_8) + val metadataLength = metadataBytes.size.toShort() + val packetBuffer = ByteBuffer.allocate(2 + metadataBytes.size + audioBytes.size) + packetBuffer.order(ByteOrder.LITTLE_ENDIAN); packetBuffer.putShort(metadataLength); packetBuffer.put(metadataBytes); packetBuffer.put(audioBytes) + val combinedPacket = packetBuffer.array().toByteString(0, packetBuffer.position()) + val sent = destinationSocket.send(combinedPacket) + if (sent) logToActivity("Sent audio for $starRequestId to ${destinationSocket.request().url.host}") + else logToActivity("Failed to send audio packet for $starRequestId to ${destinationSocket.request().url.host}") + } + + private fun sendError(starRequestId: String, errorMessage: String, destinationSocket: WebSocket) { + val errorPayload = JSONObject().apply { + put("provider", providerRevision); put("id", starRequestId) + put("status", errorMessage.take(200)); put("abort", true) + }.toString() + logToFile("TX Error for $starRequestId to ${destinationSocket.request().url.host}: $errorMessage") + destinationSocket.send(errorPayload) + } + + private fun updateCombinedStatus() { + val connectedCount = connections.values.count { it.webSocket != null && it.status == "Connected" } + val totalCount = connections.size + val isAnyConnecting = connections.values.any { it.status.contains("Connecting") || it.status.contains("Reconnecting") } + val summary = if (totalCount > 0) "Connected to $connectedCount of $totalCount hosts. Voices: ${activeStarVoices.size}" + else "No hosts configured. Voices: ${activeStarVoices.size}" + val isRunning = connectedCount > 0 || isAnyConnecting + updateStatus(summary, isRunning) + } + + private fun updateStatus(newStatus: String, isRunning: Boolean) { + currentStatus = newStatus; isServiceCurrentlyRunning = isRunning + Log.i("StarProviderService", "Status Update: $newStatus, IsRunning: $isRunning") + updateNotification(newStatus); stateListeners.forEach { it.onStatusUpdate(newStatus, isRunning) } + } + + internal fun stopServiceInternal() { + isManuallyStopped = true + isStartedByCommand = false + logToActivity("Service stop requested by MainActivity.") + connections.values.forEach { it.isManuallyStopped = true; it.handler.removeCallbacksAndMessages(null); cleanupWebSocket(it.hostUrl) } + connections.clear(); ttsEngines.values.forEach { try { it.stop() } catch (e: Exception) { /* ignore */ } } + stopForeground(STOP_FOREGROUND_REMOVE); stopSelf() + } + + override fun onDestroy() { + super.onDestroy() + isManuallyStopped = true; updateStatus("Service Stopped", false) + logToFile("Service onDestroy. Cleaning up all resources.") + ttsDiscoveryInstance?.shutdown() + ttsEngines.values.forEach { try { it.shutdown() } catch(e: Exception) { /* ignore */ } } + ttsEngines.clear(); ttsInitializedEngines.clear() + connections.values.forEach { it.handler.removeCallbacksAndMessages(null) } + connections.clear() + logToFile("--- Log session ended: ${getCurrentTimestamp()} ---", false) + stateListeners.clear(); logListeners.clear() + } + + companion object { const val EXTRA_SERVER_URLS = "com.star.provider.EXTRA_SERVER_URLS" } + private fun isTtsReady(): Boolean = ttsInitializedEngines.isNotEmpty() || activeStarVoices.isNotEmpty() + fun isRunning(): Boolean = isServiceCurrentlyRunning + fun requestCurrentStatus() { stateListeners.forEach { it.onStatusUpdate(currentStatus, isServiceCurrentlyRunning) } } + + private fun createNotificationChannel() { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + val serviceChannel = NotificationChannel(CHANNEL_ID, "STAR Provider Service", NotificationManager.IMPORTANCE_LOW) + getSystemService(NotificationManager::class.java)?.createNotificationChannel(serviceChannel); + } + } + + private fun updateNotification(message: String) { + val notificationIntent = Intent(this, MainActivity::class.java) + val pendingIntentFlags = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT else PendingIntent.FLAG_UPDATE_CURRENT + val pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, pendingIntentFlags) + val notification = NotificationCompat.Builder(this, CHANNEL_ID) + .setContentTitle("STAR Provider").setContentText(message) + .setSmallIcon(R.drawable.ic_stat_name).setContentIntent(pendingIntent) + .setOngoing(true).setOnlyAlertOnce(true).build() + (getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager).notify(NOTIFICATION_ID, notification) + } + + private fun logToActivity(message: String) { Log.d("StarProviderServiceLog", message); logToFile(message); logListeners.forEach { it.onLogMessage(message) } } + + private fun initializeLogFile() { + try { + val logsDir = File(getExternalFilesDir(null), "logs"); if (!logsDir.exists()) logsDir.mkdirs() + logFile = File(logsDir, LOG_FILE_NAME); if (!logFile!!.exists()) { logFile!!.createNewFile() } + logToFile("--- Log session started: ${getCurrentTimestamp()} ---", false) + } catch (e: Exception) { Log.e("StarProviderService", "Error initializing log file", e); logFile = null } + } + + private fun getCurrentTimestamp(): String = SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS", Locale.getDefault()).format(Date()) + + private fun logToFile(message: String, includeTimestamp: Boolean = true) { + if (logFile == null) initializeLogFile() + logFile?.let { file -> + try { + if (file.length() > MAX_LOG_SIZE_BYTES) { + val backupFile = File(file.parentFile, BACKUP_LOG_FILE_NAME); if (backupFile.exists()) backupFile.delete() + file.renameTo(backupFile); if (file.createNewFile()) { + FileOutputStream(file, true).bufferedWriter().use { it.append("${getCurrentTimestamp()} - Log file rotated.\n") } + } + } + FileOutputStream(file, true).bufferedWriter().use { it.append(if(includeTimestamp) "${getCurrentTimestamp()} - $message\n" else "$message\n") } + } catch (e: IOException) { Log.e("StarProviderService", "Error writing to log file", e) } + } + } +} diff --git a/provider/android/app/src/main/java/com/star/provider/ui/theme/Color.kt b/provider/android/app/src/main/java/com/star/provider/ui/theme/Color.kt new file mode 100644 index 0000000..975c557 --- /dev/null +++ b/provider/android/app/src/main/java/com/star/provider/ui/theme/Color.kt @@ -0,0 +1,11 @@ +package com.star.provider.ui.theme + +import androidx.compose.ui.graphics.Color + +val Purple80 = Color(0xFFD0BCFF) +val PurpleGrey80 = Color(0xFFCCC2DC) +val Pink80 = Color(0xFFEFB8C8) + +val Purple40 = Color(0xFF6650a4) +val PurpleGrey40 = Color(0xFF625b71) +val Pink40 = Color(0xFF7D5260) \ No newline at end of file diff --git a/provider/android/app/src/main/java/com/star/provider/ui/theme/Theme.kt b/provider/android/app/src/main/java/com/star/provider/ui/theme/Theme.kt new file mode 100644 index 0000000..900d590 --- /dev/null +++ b/provider/android/app/src/main/java/com/star/provider/ui/theme/Theme.kt @@ -0,0 +1,58 @@ +package com.star.provider.ui.theme + +import android.app.Activity +import android.os.Build +import androidx.compose.foundation.isSystemInDarkTheme +import androidx.compose.material3.MaterialTheme +import androidx.compose.material3.darkColorScheme +import androidx.compose.material3.dynamicDarkColorScheme +import androidx.compose.material3.dynamicLightColorScheme +import androidx.compose.material3.lightColorScheme +import androidx.compose.runtime.Composable +import androidx.compose.ui.platform.LocalContext + +private val DarkColorScheme = darkColorScheme( + primary = Purple80, + secondary = PurpleGrey80, + tertiary = Pink80 +) + +private val LightColorScheme = lightColorScheme( + primary = Purple40, + secondary = PurpleGrey40, + tertiary = Pink40 + + /* Other default colors to override + background = Color(0xFFFFFBFE), + surface = Color(0xFFFFFBFE), + onPrimary = Color.White, + onSecondary = Color.White, + onTertiary = Color.White, + onBackground = Color(0xFF1C1B1F), + onSurface = Color(0xFF1C1B1F), + */ +) + +@Composable +fun StarProviderTheme( + darkTheme: Boolean = isSystemInDarkTheme(), + // Dynamic color is available on Android 12+ + dynamicColor: Boolean = true, + content: @Composable () -> Unit +) { + val colorScheme = when { + dynamicColor && Build.VERSION.SDK_INT >= Build.VERSION_CODES.S -> { + val context = LocalContext.current + if (darkTheme) dynamicDarkColorScheme(context) else dynamicLightColorScheme(context) + } + + darkTheme -> DarkColorScheme + else -> LightColorScheme + } + + MaterialTheme( + colorScheme = colorScheme, + typography = Typography, + content = content + ) +} \ No newline at end of file diff --git a/provider/android/app/src/main/java/com/star/provider/ui/theme/Type.kt b/provider/android/app/src/main/java/com/star/provider/ui/theme/Type.kt new file mode 100644 index 0000000..3119680 --- /dev/null +++ b/provider/android/app/src/main/java/com/star/provider/ui/theme/Type.kt @@ -0,0 +1,34 @@ +package com.star.provider.ui.theme + +import androidx.compose.material3.Typography +import androidx.compose.ui.text.TextStyle +import androidx.compose.ui.text.font.FontFamily +import androidx.compose.ui.text.font.FontWeight +import androidx.compose.ui.unit.sp + +// Set of Material typography styles to start with +val Typography = Typography( + bodyLarge = TextStyle( + fontFamily = FontFamily.Default, + fontWeight = FontWeight.Normal, + fontSize = 16.sp, + lineHeight = 24.sp, + letterSpacing = 0.5.sp + ) + /* Other default text styles to override + titleLarge = TextStyle( + fontFamily = FontFamily.Default, + fontWeight = FontWeight.Normal, + fontSize = 22.sp, + lineHeight = 28.sp, + letterSpacing = 0.sp + ), + labelSmall = TextStyle( + fontFamily = FontFamily.Default, + fontWeight = FontWeight.Medium, + fontSize = 11.sp, + lineHeight = 16.sp, + letterSpacing = 0.5.sp + ) + */ +) \ No newline at end of file diff --git a/provider/android/app/src/main/res/drawable/ic_launcher_background.xml b/provider/android/app/src/main/res/drawable/ic_launcher_background.xml new file mode 100644 index 0000000..07d5da9 --- /dev/null +++ b/provider/android/app/src/main/res/drawable/ic_launcher_background.xml @@ -0,0 +1,170 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/provider/android/app/src/main/res/drawable/ic_launcher_foreground.xml b/provider/android/app/src/main/res/drawable/ic_launcher_foreground.xml new file mode 100644 index 0000000..2b068d1 --- /dev/null +++ b/provider/android/app/src/main/res/drawable/ic_launcher_foreground.xml @@ -0,0 +1,30 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/provider/android/app/src/main/res/drawable/ic_stat_name.xml b/provider/android/app/src/main/res/drawable/ic_stat_name.xml new file mode 100644 index 0000000..1670c07 --- /dev/null +++ b/provider/android/app/src/main/res/drawable/ic_stat_name.xml @@ -0,0 +1,12 @@ + + + + + \ No newline at end of file diff --git a/provider/android/app/src/main/res/drawable/ic_voice_config.xml b/provider/android/app/src/main/res/drawable/ic_voice_config.xml new file mode 100644 index 0000000..1670c07 --- /dev/null +++ b/provider/android/app/src/main/res/drawable/ic_voice_config.xml @@ -0,0 +1,12 @@ + + + + + \ No newline at end of file diff --git a/provider/android/app/src/main/res/mipmap-anydpi/ic_launcher.xml b/provider/android/app/src/main/res/mipmap-anydpi/ic_launcher.xml new file mode 100644 index 0000000..6f3b755 --- /dev/null +++ b/provider/android/app/src/main/res/mipmap-anydpi/ic_launcher.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/provider/android/app/src/main/res/mipmap-anydpi/ic_launcher_round.xml b/provider/android/app/src/main/res/mipmap-anydpi/ic_launcher_round.xml new file mode 100644 index 0000000..6f3b755 --- /dev/null +++ b/provider/android/app/src/main/res/mipmap-anydpi/ic_launcher_round.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/provider/android/app/src/main/res/mipmap-hdpi/ic_launcher.webp b/provider/android/app/src/main/res/mipmap-hdpi/ic_launcher.webp new file mode 100644 index 0000000..c209e78 Binary files /dev/null and b/provider/android/app/src/main/res/mipmap-hdpi/ic_launcher.webp differ diff --git a/provider/android/app/src/main/res/mipmap-hdpi/ic_launcher_round.webp b/provider/android/app/src/main/res/mipmap-hdpi/ic_launcher_round.webp new file mode 100644 index 0000000..b2dfe3d Binary files /dev/null and b/provider/android/app/src/main/res/mipmap-hdpi/ic_launcher_round.webp differ diff --git a/provider/android/app/src/main/res/mipmap-mdpi/ic_launcher.webp b/provider/android/app/src/main/res/mipmap-mdpi/ic_launcher.webp new file mode 100644 index 0000000..4f0f1d6 Binary files /dev/null and b/provider/android/app/src/main/res/mipmap-mdpi/ic_launcher.webp differ diff --git a/provider/android/app/src/main/res/mipmap-mdpi/ic_launcher_round.webp b/provider/android/app/src/main/res/mipmap-mdpi/ic_launcher_round.webp new file mode 100644 index 0000000..62b611d Binary files /dev/null and b/provider/android/app/src/main/res/mipmap-mdpi/ic_launcher_round.webp differ diff --git a/provider/android/app/src/main/res/mipmap-xhdpi/ic_launcher.webp b/provider/android/app/src/main/res/mipmap-xhdpi/ic_launcher.webp new file mode 100644 index 0000000..948a307 Binary files /dev/null and b/provider/android/app/src/main/res/mipmap-xhdpi/ic_launcher.webp differ diff --git a/provider/android/app/src/main/res/mipmap-xhdpi/ic_launcher_round.webp b/provider/android/app/src/main/res/mipmap-xhdpi/ic_launcher_round.webp new file mode 100644 index 0000000..1b9a695 Binary files /dev/null and b/provider/android/app/src/main/res/mipmap-xhdpi/ic_launcher_round.webp differ diff --git a/provider/android/app/src/main/res/mipmap-xxhdpi/ic_launcher.webp b/provider/android/app/src/main/res/mipmap-xxhdpi/ic_launcher.webp new file mode 100644 index 0000000..28d4b77 Binary files /dev/null and b/provider/android/app/src/main/res/mipmap-xxhdpi/ic_launcher.webp differ diff --git a/provider/android/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.webp b/provider/android/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.webp new file mode 100644 index 0000000..9287f50 Binary files /dev/null and b/provider/android/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.webp differ diff --git a/provider/android/app/src/main/res/mipmap-xxxhdpi/ic_launcher.webp b/provider/android/app/src/main/res/mipmap-xxxhdpi/ic_launcher.webp new file mode 100644 index 0000000..aa7d642 Binary files /dev/null and b/provider/android/app/src/main/res/mipmap-xxxhdpi/ic_launcher.webp differ diff --git a/provider/android/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.webp b/provider/android/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.webp new file mode 100644 index 0000000..9126ae3 Binary files /dev/null and b/provider/android/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.webp differ diff --git a/provider/android/app/src/main/res/values/colors.xml b/provider/android/app/src/main/res/values/colors.xml new file mode 100644 index 0000000..f8c6127 --- /dev/null +++ b/provider/android/app/src/main/res/values/colors.xml @@ -0,0 +1,10 @@ + + + #FFBB86FC + #FF6200EE + #FF3700B3 + #FF03DAC5 + #FF018786 + #FF000000 + #FFFFFFFF + \ No newline at end of file diff --git a/provider/android/app/src/main/res/values/strings.xml b/provider/android/app/src/main/res/values/strings.xml new file mode 100644 index 0000000..867e083 --- /dev/null +++ b/provider/android/app/src/main/res/values/strings.xml @@ -0,0 +1,3 @@ + + star provider + \ No newline at end of file diff --git a/provider/android/app/src/main/res/values/themes.xml b/provider/android/app/src/main/res/values/themes.xml new file mode 100644 index 0000000..e65b4e1 --- /dev/null +++ b/provider/android/app/src/main/res/values/themes.xml @@ -0,0 +1,5 @@ + + + +