Skip to content

armgit / andee

Public Imported from GitHub
0

Andee lives on your phone or Mac and uses any app like you do—no integrations, no root, any model; memory and passwords never leave your device | Andee 住进你的手机或 Mac,像你一样操作任何 App——无需集成、无需 Root、支持任意模型,记忆和密码绝不离开设备。

refactor(core): move the four voice engines into the shared module

booboosui3e671414ec1b
diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml
index 898eabc..4399ff1 100644
--- a/app/src/main/AndroidManifest.xml
+++ b/app/src/main/AndroidManifest.xml
@@ -107,6 +107,7 @@
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />
<application
+ android:name=".AndeeApp"
android:allowBackup="false"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
diff --git a/app/src/main/java/net/kuafuai/andee/AndeeApp.kt b/app/src/main/java/net/kuafuai/andee/AndeeApp.kt
new file mode 100644
index 0000000..bf19b8a
--- /dev/null
+++ b/app/src/main/java/net/kuafuai/andee/AndeeApp.kt
@@ -0,0 +1,27 @@
+package net.kuafuai.andee
+
+import android.app.Application
+import android.util.Log
+
+/**
+ * Runs before any service, receiver or activity of this app. It exists to
+ * point the shared module's logging at logcat before the first line is
+ * written; nothing else belongs here.
+ */
+class AndeeApp : Application() {
+
+ override fun onCreate() {
+ super.onCreate()
+ net.kuafuai.andee.util.Log.sink = object : net.kuafuai.andee.util.Log.Sink {
+ override fun log(level: Char, tag: String, msg: String, t: Throwable?) {
+ when (level) {
+ 'V' -> Log.v(tag, msg)
+ 'D' -> Log.d(tag, msg)
+ 'I' -> Log.i(tag, msg)
+ 'W' -> if (t == null) Log.w(tag, msg) else Log.w(tag, msg, t)
+ else -> if (t == null) Log.e(tag, msg) else Log.e(tag, msg, t)
+ }
+ }
+ }
+ }
+}
diff --git a/app/src/main/java/net/kuafuai/andee/config/VoiceConfig.kt b/app/src/main/java/net/kuafuai/andee/config/VoiceConfig.kt
index 073dc20..2b37c11 100644
--- a/app/src/main/java/net/kuafuai/andee/config/VoiceConfig.kt
+++ b/app/src/main/java/net/kuafuai/andee/config/VoiceConfig.kt
@@ -4,52 +4,12 @@ import android.content.Context
import net.kuafuai.andee.BuildConfig
import java.util.UUID
-
-data class VoiceConfig(
- val apiKey: String,
- val asrEndpoint: String,
- val asrResourceId: String,
- val asrUid: String,
- val ttsEndpoint: String,
- val ttsResourceId: String,
- val ttsSpeaker: String,
- val ttsSampleRate: Int,
- /**
- * [VOICE_HUOSHAN] or [VOICE_GOOGLE] — which service hears and speaks.
- *
- * Read here rather than at the call site so that the two engines, the
- * meeting recorder and the start-up log cannot disagree about it, the same
- * reason [ttsSpeaker] lives in this object.
- */
- val voiceProvider: String,
- /** Vertex Express key. Empty means the Google path cannot run. */
- val googleApiKey: String,
- val googleBaseUrl: String,
- /**
- * GCP project **number**, and the region the Live API session is opened
- * in. Both are only used by the listening half.
- *
- * The model has to be named by its full resource path —
- * `projects/{n}/locations/{r}/publishers/google/models/{m}` — because the
- * Live API reads a bare name as a project id and answers
- * `1008 Permission denied on resource project …`. The reference gateway
- * found that out the hard way; these two rows are what that costs.
- */
- val googleProject: String,
- val googleLocation: String,
- val googleAsrModel: String,
- val googleTtsModel: String,
- /**
- * A hint for both directions, or blank for "let Google decide".
- *
- * Blank rather than a fixed `zh-CN`, following the reference gateway: a
- * Chinese user says "hello 帮我查一下" and an English one says
- * "帮我查杭州天气", so naming one language does not restrict the other, it
- * only makes it less accurate.
- */
- val googleLanguage: String,
-) {
- companion object {
+/**
+ * Reads and writes the phone's settings (`voice_prefs`). Everything here used
+ * to be the companion of the settings data class, which now lives in the shared
+ * module as [VoiceSettings].
+ */
+object VoiceConfig {
/**
* The 火山 (Volcengine Speech) account the voice pipeline runs on when
* the user is on the **本机** backend. On [BRAIN_CODEFLYING], the key
@@ -106,8 +66,8 @@ data class VoiceConfig(
* the picker, the self-check rows and the engines all branch on the
* string, so a third provider is a value rather than a second flag.
*/
- const val VOICE_HUOSHAN = "huoshan"
- const val VOICE_GOOGLE = "google"
+ const val VOICE_HUOSHAN = VoiceSettings.VOICE_HUOSHAN
+ const val VOICE_GOOGLE = VoiceSettings.VOICE_GOOGLE
/**
* The Vertex endpoint, its Express-keyed surface.
@@ -592,7 +552,7 @@ data class VoiceConfig(
context.getSharedPreferences(PREFS, Context.MODE_PRIVATE)
.getString("api_key", "").orEmpty().isNotBlank()
- fun load(context: Context): VoiceConfig {
+ fun load(context: Context): VoiceSettings {
val p = context.getSharedPreferences(PREFS, Context.MODE_PRIVATE)
var uid = p.getString("uid", null)
if (uid.isNullOrEmpty()) {
@@ -604,7 +564,7 @@ data class VoiceConfig(
// (user's own 火山 account); 云端 shares the same prefs because
// ASR/TTS still run on this device whichever brain answers.
val codeFlying = brainMode(context) == BRAIN_CODEFLYING
- return VoiceConfig(
+ return VoiceSettings(
apiKey = if (codeFlying) codeFlyingKey(context)
else p.getString("api_key", "").orEmpty().trim().ifEmpty { API_KEY },
asrEndpoint = if (codeFlying) codeFlyingAsrEndpoint()
@@ -963,5 +923,4 @@ data class VoiceConfig(
"sweep", "sweep_quiet_minutes", "sweep_daily_cap",
"max_steps",
)
- }
}
diff --git a/core/build.gradle b/core/build.gradle
index 22adba3..69e29a2 100644
--- a/core/build.gradle
+++ b/core/build.gradle
@@ -31,6 +31,8 @@ dependencies {
// The brain's HTTP client. Same version the app already ships.
implementation 'com.squareup.okhttp3:okhttp:4.12.0'
+ // 火山's binary WebSocket protocol (HuoshanAsr / HuoshanTts).
+ implementation 'org.java-websocket:Java-WebSocket:1.5.7'
testImplementation libs.junit
testImplementation 'org.json:json:20240303'
diff --git a/app/src/main/java/net/kuafuai/andee/asr/GoogleAsr.kt b/core/src/main/kotlin/net/kuafuai/andee/asr/GoogleAsr.kt
similarity index 98%
rename from app/src/main/java/net/kuafuai/andee/asr/GoogleAsr.kt
rename to core/src/main/kotlin/net/kuafuai/andee/asr/GoogleAsr.kt
index 2e8dbb5..e632b44 100644
--- a/app/src/main/java/net/kuafuai/andee/asr/GoogleAsr.kt
+++ b/core/src/main/kotlin/net/kuafuai/andee/asr/GoogleAsr.kt
@@ -1,8 +1,7 @@
package net.kuafuai.andee.asr
-import android.util.Base64
-import android.util.Log
-import net.kuafuai.andee.config.VoiceConfig
+import net.kuafuai.andee.util.Log
+import net.kuafuai.andee.config.VoiceSettings
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.Response
@@ -34,8 +33,8 @@ import java.util.concurrent.atomic.AtomicBoolean
*
* * **The model must be named by its full resource path.** A bare name is
* read as a project id → `1008 Permission denied on resource project …`;
- * `models/x` is a 1007. Hence [VoiceConfig.googleProject] and
- * [VoiceConfig.googleLocation].
+ * `models/x` is a 1007. Hence [VoiceSettings.googleProject] and
+ * [VoiceSettings.googleLocation].
* * **`responseModalities` must be `TEXT`.** A transcription model wants the
* opposite of a talking one, and getting it backwards drops the connection.
* * **`languageCodes` is sent as an explicit empty array**, not omitted, when
@@ -55,7 +54,7 @@ import java.util.concurrent.atomic.AtomicBoolean
* all I/O is on [io]; the socket's callbacks arrive on OkHttp's thread.
*/
class GoogleAsr(
- private val config: VoiceConfig,
+ private val config: VoiceSettings,
private val listener: AsrEngine.Listener,
) : AsrEngine {
@@ -597,7 +596,7 @@ class GoogleAsr(
JSONObject().put(
"audio",
JSONObject()
- .put("data", Base64.encodeToString(pcm, Base64.NO_WRAP))
+ .put("data", java.util.Base64.getEncoder().encodeToString(pcm))
.put("mimeType", "audio/pcm;rate=$SAMPLE_RATE"),
),
)
diff --git a/app/src/main/java/net/kuafuai/andee/asr/HuoshanAsr.kt b/core/src/main/kotlin/net/kuafuai/andee/asr/HuoshanAsr.kt
similarity index 99%
rename from app/src/main/java/net/kuafuai/andee/asr/HuoshanAsr.kt
rename to core/src/main/kotlin/net/kuafuai/andee/asr/HuoshanAsr.kt
index 6205003..fead943 100644
--- a/app/src/main/java/net/kuafuai/andee/asr/HuoshanAsr.kt
+++ b/core/src/main/kotlin/net/kuafuai/andee/asr/HuoshanAsr.kt
@@ -1,7 +1,7 @@
package net.kuafuai.andee.asr
-import android.util.Log
-import net.kuafuai.andee.config.VoiceConfig
+import net.kuafuai.andee.util.Log
+import net.kuafuai.andee.config.VoiceSettings
import org.java_websocket.client.WebSocketClient
import org.java_websocket.handshake.ServerHandshake
import org.json.JSONObject
@@ -29,7 +29,7 @@ import javax.net.ssl.SSLContext
* [3] reserved (0)
*/
class HuoshanAsr(
- private val config: VoiceConfig,
+ private val config: VoiceSettings,
private val listener: AsrEngine.Listener,
) : AsrEngine {
diff --git a/core/src/main/kotlin/net/kuafuai/andee/config/VoiceSettings.kt b/core/src/main/kotlin/net/kuafuai/andee/config/VoiceSettings.kt
new file mode 100644
index 0000000..412a166
--- /dev/null
+++ b/core/src/main/kotlin/net/kuafuai/andee/config/VoiceSettings.kt
@@ -0,0 +1,56 @@
+package net.kuafuai.andee.config
+
+/**
+ * Everything the voice engines need to reach their service: endpoints, keys,
+ * the speaker and which provider is in use. Built by each platform from its own
+ * stored settings; the engines only read it.
+ */
+data class VoiceSettings(
+ val apiKey: String,
+ val asrEndpoint: String,
+ val asrResourceId: String,
+ val asrUid: String,
+ val ttsEndpoint: String,
+ val ttsResourceId: String,
+ val ttsSpeaker: String,
+ val ttsSampleRate: Int,
+ /**
+ * [VOICE_HUOSHAN] or [VOICE_GOOGLE] — which service hears and speaks.
+ *
+ * Read here rather than at the call site so that the two engines, the
+ * meeting recorder and the start-up log cannot disagree about it, the same
+ * reason [ttsSpeaker] lives in this object.
+ */
+ val voiceProvider: String,
+ /** Vertex Express key. Empty means the Google path cannot run. */
+ val googleApiKey: String,
+ val googleBaseUrl: String,
+ /**
+ * GCP project **number**, and the region the Live API session is opened
+ * in. Both are only used by the listening half.
+ *
+ * The model has to be named by its full resource path —
+ * `projects/{n}/locations/{r}/publishers/google/models/{m}` — because the
+ * Live API reads a bare name as a project id and answers
+ * `1008 Permission denied on resource project …`. The reference gateway
+ * found that out the hard way; these two rows are what that costs.
+ */
+ val googleProject: String,
+ val googleLocation: String,
+ val googleAsrModel: String,
+ val googleTtsModel: String,
+ /**
+ * A hint for both directions, or blank for "let Google decide".
+ *
+ * Blank rather than a fixed `zh-CN`, following the reference gateway: a
+ * Chinese user says "hello 帮我查一下" and an English one says
+ * "帮我查杭州天气", so naming one language does not restrict the other, it
+ * only makes it less accurate.
+ */
+ val googleLanguage: String,
+) {
+ companion object {
+ const val VOICE_HUOSHAN = "huoshan"
+ const val VOICE_GOOGLE = "google"
+ }
+}
diff --git a/app/src/main/java/net/kuafuai/andee/tts/GoogleTts.kt b/core/src/main/kotlin/net/kuafuai/andee/tts/GoogleTts.kt
similarity index 98%
rename from app/src/main/java/net/kuafuai/andee/tts/GoogleTts.kt
rename to core/src/main/kotlin/net/kuafuai/andee/tts/GoogleTts.kt
index 987394b..fcac399 100644
--- a/app/src/main/java/net/kuafuai/andee/tts/GoogleTts.kt
+++ b/core/src/main/kotlin/net/kuafuai/andee/tts/GoogleTts.kt
@@ -1,8 +1,7 @@
package net.kuafuai.andee.tts
-import android.util.Base64
-import android.util.Log
-import net.kuafuai.andee.config.VoiceConfig
+import net.kuafuai.andee.util.Log
+import net.kuafuai.andee.config.VoiceSettings
import okhttp3.Call
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.OkHttpClient
@@ -46,7 +45,7 @@ import java.util.concurrent.TimeUnit
* the WebSocket the vendor's own live models want.
*/
class GoogleTts(
- private val config: VoiceConfig,
+ private val config: VoiceSettings,
private val listener: TtsEngine.Listener,
) : TtsEngine {
@@ -242,7 +241,7 @@ class GoogleTts(
val data = inline.optString("data")
if (data.isBlank()) continue
checkRate(inline.optString("mimeType"))
- val pcm = runCatching { Base64.decode(data, Base64.DEFAULT) }.getOrNull() ?: continue
+ val pcm = runCatching { java.util.Base64.getMimeDecoder().decode(data) }.getOrNull() ?: continue
if (pcm.isNotEmpty()) out.add(pcm)
}
return out
diff --git a/app/src/main/java/net/kuafuai/andee/tts/HuoshanTts.kt b/core/src/main/kotlin/net/kuafuai/andee/tts/HuoshanTts.kt
similarity index 99%
rename from app/src/main/java/net/kuafuai/andee/tts/HuoshanTts.kt
rename to core/src/main/kotlin/net/kuafuai/andee/tts/HuoshanTts.kt
index c9f2438..4fada5c 100644
--- a/app/src/main/java/net/kuafuai/andee/tts/HuoshanTts.kt
+++ b/core/src/main/kotlin/net/kuafuai/andee/tts/HuoshanTts.kt
@@ -1,7 +1,7 @@
package net.kuafuai.andee.tts
-import android.util.Log
-import net.kuafuai.andee.config.VoiceConfig
+import net.kuafuai.andee.util.Log
+import net.kuafuai.andee.config.VoiceSettings
import org.java_websocket.client.WebSocketClient
import org.java_websocket.handshake.ServerHandshake
import org.json.JSONObject
@@ -33,7 +33,7 @@ import javax.net.ssl.SSLContext
* (session_id is omitted for connection-level events: 1, 2, 50, 51, 52)
*/
class HuoshanTts(
- private val config: VoiceConfig,
+ private val config: VoiceSettings,
private val listener: TtsEngine.Listener,
) : TtsEngine {
diff --git a/core/src/main/kotlin/net/kuafuai/andee/util/Log.kt b/core/src/main/kotlin/net/kuafuai/andee/util/Log.kt
new file mode 100644
index 0000000..897ff74
--- /dev/null
+++ b/core/src/main/kotlin/net/kuafuai/andee/util/Log.kt
@@ -0,0 +1,32 @@
+package net.kuafuai.andee.util
+
+/**
+ * Logging for shared code, with Android's call shape so code moved out of the
+ * app reads unchanged (`Log.i(TAG, "…")`).
+ *
+ * Each platform installs its sink once at startup — the phone forwards to
+ * `android.util.Log`, the desktop writes a file. Until then lines go to stderr,
+ * which is what unit tests see.
+ */
+object Log {
+
+ interface Sink {
+ fun log(level: Char, tag: String, msg: String, t: Throwable?)
+ }
+
+ @Volatile
+ var sink: Sink = object : Sink {
+ override fun log(level: Char, tag: String, msg: String, t: Throwable?) {
+ System.err.println("$level/$tag: $msg")
+ t?.printStackTrace()
+ }
+ }
+
+ fun v(tag: String, msg: String) = sink.log('V', tag, msg, null)
+ fun d(tag: String, msg: String) = sink.log('D', tag, msg, null)
+ fun i(tag: String, msg: String) = sink.log('I', tag, msg, null)
+ fun w(tag: String, msg: String) = sink.log('W', tag, msg, null)
+ fun w(tag: String, msg: String, t: Throwable?) = sink.log('W', tag, msg, t)
+ fun e(tag: String, msg: String) = sink.log('E', tag, msg, null)
+ fun e(tag: String, msg: String, t: Throwable?) = sink.log('E', tag, msg, t)
+}