diff --git a/android/app/build.gradle.kts b/android/app/build.gradle.kts index 6f00b4b..bb19584 100644 --- a/android/app/build.gradle.kts +++ b/android/app/build.gradle.kts @@ -30,8 +30,8 @@ android { * versionCode 每次发版 +1(Android 靠它判断"能不能覆盖安装", * 改小了会装不上)。 */ - versionCode = 241 - versionName = "2.0.41" + versionCode = 242 + versionName = "2.0.42" ndk { // Chaquopy 只支持这两个;Termux bootstrap 也只准备了这两份 diff --git a/android/app/src/main/AndroidManifest.xml b/android/app/src/main/AndroidManifest.xml index cea125c..89fd2f1 100644 --- a/android/app/src/main/AndroidManifest.xml +++ b/android/app/src/main/AndroidManifest.xml @@ -17,6 +17,16 @@ + + + + + diff --git a/android/app/src/main/java/com/doubao/lynxclone/DoubaoNotificationListener.kt b/android/app/src/main/java/com/doubao/lynxclone/DoubaoNotificationListener.kt index bdeb8ea..6846324 100644 --- a/android/app/src/main/java/com/doubao/lynxclone/DoubaoNotificationListener.kt +++ b/android/app/src/main/java/com/doubao/lynxclone/DoubaoNotificationListener.kt @@ -32,15 +32,25 @@ class DoubaoNotificationListener : NotificationListenerService() { override fun onListenerConnected() { super.onListenerConnected() connected = true - Log.i(TAG, "通知使用权已连接") + instance = this + // ★ 连上的瞬间就把当前挂着的通知同步进来 —— 不然用户装完 App 立刻问 + // "我最近收到什么通知了",答案是空的(那些通知在他装 App 之前就收到了) + syncActive() + Log.i(TAG, "通知使用权已连接,当前挂着 ${items.size} 条") } override fun onListenerDisconnected() { super.onListenerDisconnected() connected = false + if (instance === this) instance = null Log.i(TAG, "通知使用权已断开") } + override fun onDestroy() { + super.onDestroy() + if (instance === this) instance = null + } + override fun onNotificationPosted(sbn: StatusBarNotification?) { val n = sbn ?: return try { @@ -63,7 +73,14 @@ class DoubaoNotificationListener : NotificationListenerService() { } } - private fun record(n: StatusBarNotification, removed: Boolean) { + /** + * 记一条通知。 + * + * ★ 可见性要 **internal**(不能 private):`companion object` 里的 + * `syncActive()` 要调它,而 Kotlin 的 private 成员**只对同一个类体可见**, + * 伴生对象不算 —— 写成 private 会报"cannot access"。 + */ + internal fun record(n: StatusBarNotification, removed: Boolean) { val extras: Bundle = n.notification?.extras ?: Bundle() val title = extras.getCharSequence(Notification.EXTRA_TITLE)?.toString().orEmpty() val text = extras.getCharSequence(Notification.EXTRA_TEXT)?.toString().orEmpty() @@ -81,6 +98,14 @@ class DoubaoNotificationListener : NotificationListenerService() { removed = removed, atRemoved = 0L, ) + // ★ 标题和正文都空的就**不进列表**。 + // + // 这类通知真实存在:媒体控制、正在进行的下载、某些 App 的纯图标通知 —— + // 它们的文字要么在 RemoteViews 里(`extras` 取不到),要么压根没有。 + // 留着的话用户/模型会看到一条"什么都没有"的条目,纯噪音。 + // (实测:`cmd notification post` 发的测试通知就有一条是这样的。) + if (item.title.isBlank() && item.text.isBlank()) return + items[item.key] = item // 超上限就丢最旧的。用 postTime 排(key 是字符串,排不了时间序) @@ -115,10 +140,49 @@ class DoubaoNotificationListener : NotificationListenerService() { /** 最近的通知(内存里,进程死了就没了 —— 见类注释里的隐私考虑) */ private val items = ConcurrentHashMap() + /** + * 当前活着的 service 实例。 + * + * ★ 必须有它:`getActiveNotifications()` 是 **service 自己**的方法, + * 不是静态的。没有实例就调不到,也就拿不到"系统里现在挂着哪些通知"。 + */ + @Volatile + private var instance: DoubaoNotificationListener? = null + /** 通知使用权连上了没有 */ @Volatile var connected: Boolean = false + /** + * ★★★ 把系统里**当前还挂着的**通知同步进来。 + * + * ## 为什么必须有这个(用户报"还是没有读到"的根因) + * + * 第一版只在 `onNotificationPosted` 里记 —— 也就是说**只有本进程活着的 + * 那段时间收到的通知才算数**。而进程一重启(用户升级 App、或者系统杀后台) + * 内存就清空了,那之前收到的通知**全都看不到**。 + * + * 用户的期望是"读我手机上现在的通知" —— 那本来就该包含 + * **在他打开 App 之前就收到的那些**。 + * + * `NotificationListenerService.getActiveNotifications()` 正好干这个: + * 系统把当前通知栏上挂着的全给它。所以每次读之前先同步一遍。 + * + * ★ 拿不到实例(service 没起来)时不报错,就用手上已有的 —— + * 至少比什么都不给强。 + */ + fun syncActive() { + val svc = instance ?: return + try { + for (sbn in svc.activeNotifications ?: emptyArray()) { + if (sbn.packageName == svc.packageName) continue + svc.record(sbn, removed = false) + } + } catch (t: Throwable) { + Log.w(TAG, "同步当前通知失败:${t.message}") + } + } + /** * 取最近的通知。 * @@ -127,6 +191,9 @@ class DoubaoNotificationListener : NotificationListenerService() { * @param query 关键词过滤(标题/正文/App 名里含它) */ fun recent(limit: Int = 20, onlyAlive: Boolean = false, query: String = ""): JSONArray { + // ★ 先跟系统对一次账,把"我们没见过的、但此刻挂着的"补进来 + syncActive() + var list = items.values.toList() if (onlyAlive) list = list.filter { !it.removed } if (query.isNotBlank()) { diff --git a/android/app/src/main/java/com/doubao/lynxclone/MainActivity.kt b/android/app/src/main/java/com/doubao/lynxclone/MainActivity.kt index 649d6ce..7d5893e 100644 --- a/android/app/src/main/java/com/doubao/lynxclone/MainActivity.kt +++ b/android/app/src/main/java/com/doubao/lynxclone/MainActivity.kt @@ -35,10 +35,32 @@ class MainActivity : ComponentActivity() { private val client = KernelClient() + companion object { + /** 发通知权限的请求码(只要不和别的撞就行) */ + private const val REQ_NOTIF = 0x4D32 + } + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) enableEdgeToEdge() + // ★★ 申请「发通知」权限(用户要求"还可以发送通知")。 + // + // Android 13(API 33) 起这是**运行时权限**,新装的 App **默认拒绝** —— + // 不申请的话 `NotificationManager.notify()` 不报错、但通知**根本不显示**。 + // 实测踩到:渠道建出来了(importance=3),App 级却是 `importance=NONE`, + // 通知栏里什么都没有,而工具还返回"发送成功"。 + // + // ★ 只在 33+ 申请:低版本没这个权限,申请了系统会直接忽略(但会打日志)。 + // ★ 用户拒绝也不影响别的功能 —— 只是发通知会失败,而且工具会如实报错。 + if (android.os.Build.VERSION.SDK_INT >= 33) { + val need = checkSelfPermission(android.Manifest.permission.POST_NOTIFICATIONS) != + android.content.pm.PackageManager.PERMISSION_GRANTED + if (need) { + requestPermissions(arrayOf(android.Manifest.permission.POST_NOTIFICATIONS), REQ_NOTIF) + } + } + var kernelUrl by mutableStateOf("") var status by mutableStateOf("正在启动本地内核…") var termuxReady by mutableStateOf(false) diff --git a/android/app/src/main/java/com/doubao/lynxclone/NativeBridge.kt b/android/app/src/main/java/com/doubao/lynxclone/NativeBridge.kt index d12520b..d958540 100644 --- a/android/app/src/main/java/com/doubao/lynxclone/NativeBridge.kt +++ b/android/app/src/main/java/com/doubao/lynxclone/NativeBridge.kt @@ -109,6 +109,18 @@ class NativeBridge(private val activity: Activity) { DoubaoNotificationListener.clear() JSONObject().put("ok", true) } + // ★ AI 主动发一条通知(用户要求"还可以发送通知") + "notification_post" -> { + val title = req.optString("title") + val text = req.optString("text") + if (title.isBlank() && text.isBlank()) { + JSONObject().put("error", "title 和 text 至少要给一个") + } else { + PhoneOps.postNotification( + activity, title, text, req.optBoolean("autoCancel", true), + ) + } + } "apps" -> appsList(req) "app_launch" -> appLaunch(req) "volume" -> JSONObject().put("state", PhoneOps.volumeState(activity)) diff --git a/android/app/src/main/java/com/doubao/lynxclone/PhoneOps.kt b/android/app/src/main/java/com/doubao/lynxclone/PhoneOps.kt index c91db73..f949f76 100644 --- a/android/app/src/main/java/com/doubao/lynxclone/PhoneOps.kt +++ b/android/app/src/main/java/com/doubao/lynxclone/PhoneOps.kt @@ -52,6 +52,90 @@ object PhoneOps { Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS") .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) + /* ==================================================================== */ + /* 四、发通知 */ + /* ==================================================================== */ + + /** + * 通知渠道 id。 + * + * ★ Android 8+ 必须挂在渠道上,否则通知**直接被丢掉**(连报错都没有)。 + * 渠道的重要性用 DEFAULT:太低会被折叠成静音,用户根本注意不到; + * 太高(HIGH)会弹横幅 —— 对"AI 提醒你一件事"这种场景 + * 弹横幅太打扰了,DEFAULT 正合适(有声音 + 进通知栏,不弹横幅)。 + */ + private const val CHANNEL_ID = "doubao_ai" + private const val CHANNEL_NAME = "AI 提醒" + + private fun ensureChannel(ctx: Context) { + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) return + val nm = ctx.getSystemService(Context.NOTIFICATION_SERVICE) as android.app.NotificationManager + if (nm.getNotificationChannel(CHANNEL_ID) != null) return + val ch = android.app.NotificationChannel( + CHANNEL_ID, CHANNEL_NAME, android.app.NotificationManager.IMPORTANCE_DEFAULT, + ).apply { + description = "豆包主动发出来的提醒" + enableVibration(true) + } + nm.createNotificationChannel(ch) + } + + /** + * ★★ 发一条通知 —— 用户要求"还可以发送通知"。 + * + * 用途:AI 说到点提醒你什么、长任务跑完了通知一声。 + * + * ★ 点通知要能回到 App:用 `PendingIntent` 打开 MainActivity。 + * 不给 PendingIntent 的话点了没反应 —— 用户会觉得"这通知是死的"。 + * + * ★ 用 `System.currentTimeMillis()` 当通知 id:每条独立存在,不会互相覆盖。 + * 固定 id 的话后一条会把前一条顶掉("我刚收到的提醒怎么没了")。 + */ + fun postNotification(ctx: Context, title: String, text: String, autoCancel: Boolean): JSONObject { + val nm = ctx.getSystemService(Context.NOTIFICATION_SERVICE) as android.app.NotificationManager + ensureChannel(ctx) + + val t = title.trim().ifBlank { "豆包" } + val body = text.trim() + + // 点通知打开 App + val open = Intent(ctx, MainActivity::class.java) + .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP) + var flags = android.app.PendingIntent.FLAG_UPDATE_CURRENT + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { + flags = flags or android.app.PendingIntent.FLAG_IMMUTABLE + } + val pi = android.app.PendingIntent.getActivity(ctx, 0, open, flags) + + val builder = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + android.app.Notification.Builder(ctx, CHANNEL_ID) + } else { + @Suppress("DEPRECATION") + android.app.Notification.Builder(ctx) + } + builder + .setSmallIcon(ctx.applicationInfo.icon) // ★ 不给 smallIcon 通知**不会显示** + .setContentTitle(t) + .setContentText(body) + .setAutoCancel(autoCancel) + .setContentIntent(pi) + // 正文长的话展开显示(否则通知栏只显示一行,被截断) + if (body.length > 40) { + builder.setStyle(android.app.Notification.BigTextStyle().bigText(body)) + } + + val id = (System.currentTimeMillis() % Int.MAX_VALUE).toInt() + return try { + nm.notify(id, builder.build()) + JSONObject().put("ok", true).put("id", id) + .put("title", t) + .put("chars", body.length) + } catch (t2: Throwable) { + Log.w(TAG, "发通知失败:${t2.message}") + JSONObject().put("error", "发通知失败:${t2.javaClass.simpleName}: ${t2.message}") + } + } + /* ==================================================================== */ /* 二、打开 App */ /* ==================================================================== */ diff --git a/android/app/src/main/java/com/doubao/lynxclone/ui/AppRoot.kt b/android/app/src/main/java/com/doubao/lynxclone/ui/AppRoot.kt index bb5d6d7..a62d38f 100644 --- a/android/app/src/main/java/com/doubao/lynxclone/ui/AppRoot.kt +++ b/android/app/src/main/java/com/doubao/lynxclone/ui/AppRoot.kt @@ -141,6 +141,37 @@ fun AppRoot( * `ask_user` 是阻塞的,用户必须先回答它;而「打开网页」是用户自己点的, * 他最想看的是网页,所以网页在最上面。 */ + // 「更多功能」—— 抽屉里放不下的入口收在这儿 + AnimatedVisibility( + visible = vm.moreSheetOpen, + enter = slideInVertically(tween(240)) { it / 6 } + fadeIn(tween(180)), + exit = slideOutVertically(tween(200)) { it / 8 } + fadeOut(tween(150)), + ) { + MoreSheetScreen( + onOpenAiScreens = { + vm.moreSheetOpen = false + vm.openAiScreenList() + }, + onOpenSkills = { + vm.moreSheetOpen = false + vm.screen = Screen.Skills + }, + onOpenMeetings = { + vm.moreSheetOpen = false + vm.screen = Screen.Meetings + }, + onOpenTerminal = { + vm.moreSheetOpen = false + vm.screen = Screen.Terminal + }, + onOpenSettings = { + vm.moreSheetOpen = false + vm.screen = Screen.Settings + }, + onClose = { vm.moreSheetOpen = false }, + ) + } + // 「笔记」—— 用户要能查看 AI 替他记了什么 AnimatedVisibility( visible = vm.notesOpen, @@ -244,38 +275,28 @@ private fun DrawerOverlay(vm: AppViewModel) { ) } + /** + * ★★ 抽屉分两层(改过一次,用户反馈"太挤了对话都没了")。 + * + * 原来 9 个入口平铺,把底下的**对话列表**挤得只剩一条 —— + * 而对话列表恰恰是抽屉最主要的功能(用户 90% 打开抽屉是为了切对话)。 + * + * 现在: + * · 第一层只放**天天用的**三个:新话题 / 智能体 / 笔记 + * · 其余的收进「更多功能」一个入口,点开是独立全屏页(按项目约定, + * 列表这种要滚动的东西不用弹窗或折叠面板) + */ + // 分区标题:抽屉里两组东西(功能 / 对话)不标一下容易混在一起看 + SectionLabel("功能", Modifier.padding(horizontal = 20.dp, vertical = 6.dp)) DrawerItem(Icons.Filled.AddComment, "开启新话题") { vm.newTopic() } DrawerItem(Icons.Filled.AutoAwesome, "智能体") { vm.screen = Screen.Agents; vm.drawerOpen = false } - /** - * ★「AI 界面」—— 用户要求"AI 写的界面可以再点开"。 - * - * 放在抽屉里而不是只靠消息操作栏:操作栏那个按钮只有**那一条回复** - * 弹过界面时才出现,用户想"翻翻前几次弹过什么"就没路走。 - * 这里是个总入口,能看全部、也能删。 - */ - DrawerItem(Icons.Filled.Dashboard, "AI 界面") { - vm.drawerOpen = false - vm.openAiScreenList() - } // 「笔记」—— 用户要能查看 AI 替他记了什么 DrawerItem(Icons.Filled.EditNote, "笔记") { vm.drawerOpen = false vm.openNotes() } - DrawerItem(Icons.Filled.Layers, "技能·连接器") { - vm.screen = Screen.Skills; vm.drawerOpen = false - } - DrawerItem(Icons.Filled.AccountTree, "会议模式") { - vm.screen = Screen.Meetings; vm.drawerOpen = false - } - DrawerItem(Icons.Filled.Terminal, "终端") { - vm.screen = Screen.Terminal; vm.drawerOpen = false - } - DrawerItem(Icons.Filled.Settings, "设置") { - vm.screen = Screen.Settings; vm.drawerOpen = false - } // 「模型」那一栏:和豆包一样把当前模型 + 思考强度摆右边 DrawerItem( Icons.Filled.Memory, "模型", @@ -284,9 +305,13 @@ private fun DrawerOverlay(vm: AppViewModel) { vm.modelPickerOpen = true vm.drawerOpen = false } + DrawerItem(Icons.Filled.Apps, "更多功能") { + vm.drawerOpen = false + vm.moreSheetOpen = true + } Spacer(Modifier.height(10.dp)) - SectionLabel("置顶", Modifier.padding(horizontal = 20.dp, vertical = 8.dp)) + SectionLabel("对话", Modifier.padding(horizontal = 20.dp, vertical = 8.dp)) LazyColumn(Modifier.weight(1f)) { items(vm.state.conversations.size) { i -> diff --git a/android/app/src/main/java/com/doubao/lynxclone/ui/AppViewModel.kt b/android/app/src/main/java/com/doubao/lynxclone/ui/AppViewModel.kt index 2829e4f..b38c893 100644 --- a/android/app/src/main/java/com/doubao/lynxclone/ui/AppViewModel.kt +++ b/android/app/src/main/java/com/doubao/lynxclone/ui/AppViewModel.kt @@ -91,6 +91,14 @@ class AppViewModel(val client: KernelClient) : ViewModel() { var aiScreenListOpen by mutableStateOf(false) private set + /** + * 「更多功能」页开着没有。 + * + * ★ 抽屉原来平铺 9 个入口,把对话列表挤没了(用户反馈"太挤了对话都没了")。 + * 现在只留天天用的,其余收进这一页。 + */ + var moreSheetOpen by mutableStateOf(false) + /* -------------------- 笔记(用户要能查看)-------------------- */ /** diff --git a/android/app/src/main/java/com/doubao/lynxclone/ui/MoreSheetScreen.kt b/android/app/src/main/java/com/doubao/lynxclone/ui/MoreSheetScreen.kt new file mode 100644 index 0000000..c7b6916 --- /dev/null +++ b/android/app/src/main/java/com/doubao/lynxclone/ui/MoreSheetScreen.kt @@ -0,0 +1,147 @@ +package com.doubao.lynxclone.ui + +import androidx.activity.compose.BackHandler +import androidx.compose.foundation.background +import androidx.compose.foundation.clickable +import androidx.compose.foundation.layout.Arrangement +import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.layout.PaddingValues +import androidx.compose.foundation.layout.Row +import androidx.compose.foundation.layout.Spacer +import androidx.compose.foundation.layout.fillMaxSize +import androidx.compose.foundation.layout.fillMaxWidth +import androidx.compose.foundation.layout.height +import androidx.compose.foundation.layout.padding +import androidx.compose.foundation.layout.size +import androidx.compose.foundation.layout.width +import androidx.compose.foundation.lazy.LazyColumn +import androidx.compose.foundation.shape.CircleShape +import androidx.compose.foundation.shape.RoundedCornerShape +import androidx.compose.material.icons.Icons +import androidx.compose.material.icons.automirrored.filled.KeyboardArrowRight +import androidx.compose.material.icons.filled.AccountTree +import androidx.compose.material.icons.filled.Dashboard +import androidx.compose.material.icons.filled.Layers +import androidx.compose.material.icons.filled.Settings +import androidx.compose.material.icons.filled.Terminal +import androidx.compose.material3.Icon +import androidx.compose.material3.Text +import androidx.compose.runtime.Composable +import androidx.compose.ui.Alignment +import androidx.compose.ui.Modifier +import androidx.compose.ui.draw.clip +import androidx.compose.ui.graphics.vector.ImageVector +import androidx.compose.ui.text.font.FontWeight +import androidx.compose.ui.unit.dp +import androidx.compose.ui.unit.sp + +/** + * ★★ 「更多功能」—— 抽屉里放不下的那些入口收在这儿。 + * + * ## 为什么要这么改(用户反馈:"太挤了对话都没了") + * + * 抽屉原来平铺了 9 个入口,把底下的**对话列表**挤得只剩一条 —— + * 而对话列表恰恰是抽屉最主要的功能:用户 90% 打开抽屉是为了切对话。 + * + * 现在抽屉第一层只留天天用的(新话题 / 智能体 / 笔记 / 模型), + * 其余收进这里。 + * + * ## 按项目约定:独立全屏界面 + * + * `← 返回 | 标题`,和别的全屏界面一套顶栏。 + * 不用折叠面板:折叠起来每次都要展开一次,反而更麻烦。 + */ +@Composable +fun MoreSheetScreen( + onOpenAiScreens: () -> Unit, + onOpenSkills: () -> Unit, + onOpenMeetings: () -> Unit, + onOpenTerminal: () -> Unit, + onOpenSettings: () -> Unit, + onClose: () -> Unit, +) { + BackHandler(enabled = true) { onClose() } + + Column( + Modifier + .fillMaxSize() + .background(doubaoBg()), + ) { + ScreenHeader("更多功能", onBack = onClose) + + LazyColumn( + Modifier.fillMaxSize(), + contentPadding = PaddingValues(16.dp), + verticalArrangement = Arrangement.spacedBy(10.dp), + ) { + item { + MoreRow( + icon = Icons.Filled.Dashboard, + title = "AI 界面", + subtitle = "AI 弹过的界面都存着,点一下就能再打开", + ) { onOpenAiScreens() } + } + item { + MoreRow( + icon = Icons.Filled.Layers, + title = "技能 · 连接器", + subtitle = "Python 工具、MCP 服务、界面能力开关", + ) { onOpenSkills() } + } + item { + MoreRow( + icon = Icons.Filled.AccountTree, + title = "会议模式", + subtitle = "多个智能体一起讨论,可以搭流程图", + ) { onOpenMeetings() } + } + item { + MoreRow( + icon = Icons.Filled.Terminal, + title = "终端", + subtitle = "内置 Termux,真 Linux 环境", + ) { onOpenTerminal() } + } + item { + MoreRow( + icon = Icons.Filled.Settings, + title = "设置", + subtitle = "模型、语音、外观、数据", + ) { onOpenSettings() } + } + } + } +} + +@Composable +private fun MoreRow( + icon: ImageVector, + title: String, + subtitle: String, + onClick: () -> Unit, +) { + Row( + Modifier + .fillMaxWidth() + .clip(RoundedCornerShape(14.dp)) + .background(doubaoSurfaceAlt()) + .clickable(onClick = onClick) + .padding(horizontal = 14.dp, vertical = 14.dp), + verticalAlignment = Alignment.CenterVertically, + ) { + Icon(icon, null, tint = doubaoText(), modifier = Modifier.size(22.dp)) + Spacer(Modifier.width(14.dp)) + Column(Modifier.weight(1f)) { + Text(title, fontSize = 15.sp, fontWeight = FontWeight.Medium, color = doubaoText()) + Spacer(Modifier.height(2.dp)) + Text(subtitle, fontSize = 12.sp, lineHeight = 17.sp, color = doubaoTextDim()) + } + Spacer(Modifier.width(8.dp)) + Icon( + Icons.AutoMirrored.Filled.KeyboardArrowRight, + null, + tint = doubaoTextDim(), + modifier = Modifier.size(20.dp), + ) + } +} diff --git a/tools/.shipped-hashes.json b/tools/.shipped-hashes.json index a124d70..e580f98 100644 --- a/tools/.shipped-hashes.json +++ b/tools/.shipped-hashes.json @@ -53,7 +53,8 @@ "7215ae7bc60f6f83" ], "notifications.py": [ - "3e8ba8ae55b80019" + "3e8ba8ae55b80019", + "5c006bc7be02d5f3" ], "volume.py": [ "255939a9c89f51a2" diff --git a/tools/notifications.py b/tools/notifications.py index 43b2b96..4c54b74 100644 --- a/tools/notifications.py +++ b/tools/notifications.py @@ -32,28 +32,36 @@ from _ui_bridge import call_device # noqa: E402 TOOL = { "name": "notifications", "description": ( - "读手机上的通知(最近收到的、以及已经被划掉的)。" - "\n\n用法:" + "读手机上的通知,或者**主动发一条通知**给用户。" + "\n\n读:" "\n· action=list(默认)—— 列出最近的通知。" "query 可以按关键词过滤(标题/正文/App 名),limit 默认 20、最多 200。" "\n· action=alive —— 只看**还挂在通知栏上**的(已经被用户划掉的不算)" "\n· action=access —— 查有没有「通知使用权」" "\n· action=open_settings —— 跳到系统的通知使用权设置页,让用户去开" "\n· action=clear —— 把已经读到的记录清掉(内存里那份)" - "\n\n★ 第一次用之前用户必须先手动开「通知使用权」(安卓不给运行时申请)。" + "\n\n发:" + "\n· action=notify —— 发一条通知给用户。传 title(标题)和 text(正文)。" + "\n ★ 这是**会打扰用户**的动作(他手机会响/震)——只在真的值得提醒时用:" + "\n 定时提醒、长任务跑完了、用户让你过一会儿告诉他。" + "\n 别用它来重复你已经在对话里说过的话。" + "\n 点了通知会回到本 App。" + "\n\n★ 读通知之前用户必须先手动开「通知使用权」(安卓不给运行时申请)。" "\n 没开的时候本工具会返回错误 + 怎么开的提示 —— 请照着念给用户," - "\n 别自己编步骤。开了之后才能读到东西。" + "\n 别自己编步骤。**发通知不需要这个权限。**" ), "parameters": { "type": "object", "properties": { "action": { "type": "string", - "enum": ["list", "alive", "access", "open_settings", "clear"], + "enum": ["list", "alive", "access", "open_settings", "clear", "notify"], "description": "要做的事,默认 list", }, "query": {"type": "string", "description": "按关键词过滤(可选)"}, "limit": {"type": "number", "description": "最多几条,默认 20,最多 200"}, + "title": {"type": "string", "description": "action=notify 时的通知标题"}, + "text": {"type": "string", "description": "action=notify 时的通知正文"}, }, }, } @@ -62,6 +70,34 @@ TOOL = { def run(args: dict) -> dict: action = str(args.get("action") or "list").strip().lower() + # ★ 发通知放在最前面:它**不需要**通知使用权(那权限只管读), + # 所以不能被下面读路径的权限检查拦住。 + if action == "notify": + title = str(args.get("title") or "").strip() + text = str(args.get("text") or "").strip() + if not title and not text: + raise ValueError("notify 至少要给 title 或 text") + res = call_device({ + "action": "notification_post", + "title": title, + "text": text, + "autoCancel": True, + }) + if res.get("unavailable"): + raise RuntimeError(res.get("error") or "当前环境没有通知能力") + if res.get("error"): + raise RuntimeError(str(res["error"])) + return { + "sent": True, + "title": res.get("title") or title, + "chars": res.get("chars", len(text)), + "note": ( + "通知已经发出去了,用户手机会响/震一下。" + "点它会回到本 App。" + "**别为了同一件事再发一条** —— 用户会烦。" + ), + } + if action == "open_settings": res = call_device({"action": "notification_settings"}) if res.get("unavailable"):