用户三条需求:
① "notes 用户要能查看"
② "加个读取手机通知的工具"
③ "打开手机 app 和调整手机音量的工具"
## ① notes 用户能查看了
查下去发现不只是"没界面",还有两个真问题:
**问题一:笔记写到了用户看不到的地方。**
notes.py 自己算路径 `工具文件/../data/notes` —— 在安卓上落到
**App 的外部私有目录**(`/storage/emulated/0/Android/data/<包名>/files/data/notes`)。
那地方用户用文件管理器根本翻不到(Android 11+ 直接隐藏),
而且**卸载 App 就一起删了**。
**问题二:笔记只有 AI 能读写。**
AI 说"我记下来了",用户没有任何办法确认它记了什么、记错没有、删掉记错的。
这类"AI 替你做事但你无法查看"的功能,用户不会信任。
修法:
· 路径改用内核的 `<数据目录>/notes`(和 conversations / screens 放一起)
· **自动迁移老位置的笔记**(只搬一次,搬完留 `.migrated` 标记)——
不搬的话用户以前记的笔记就"不见了",这类"改路径把用户数据弄丢"最不该发生
· 迁移放在 `store._notes_dir()` 里,**任何一次读笔记都会先保证迁移过**。
第一版只在 notes 工具被调用时才迁移,于是用户装完新版**先打开笔记页**
会看到空列表 —— 以为笔记全丢了(实测踩到)
· 新增 `/api/notes` 列表 / `GET /api/notes/<file>` 读 / `DELETE` 删
· 界面:抽屉 →「笔记」→ 列表(标题 + 预览 + 时间)→ 点开看全文(markdown 渲染)、可复制、可删
## ② 读手机通知
★ Android **没有**"读取通知"的运行时权限 —— 唯一合法的路是让用户手动去
「设置 → 通知 → 通知使用权」勾上。所以加了个 `NotificationListenerService`。
几个刻意的决定:
· **只在内存里留最近 200 条,不落盘**。通知里有验证码、银行短信、私信 ——
是整个 App 最隐私的一类数据。写磁盘意味着卸载前一直躺着,也可能被别的工具读到。
· **已划掉的也留着**(标 `gone:true`)。用户问"刚才那条验证码呢"时它早被划走了,
删了就直接查不到。
· **权限没开时返回错误,不是空列表**。返回空列表模型会说"你没有通知"——
那是**错的**(明明有几条,只是我们读不到),用户会被误导。错误里带上
"怎么开"的完整步骤,模型照着念就行。
· `action=open_settings` 直接跳那个设置页(安卓不给申请弹窗,只能跳)。
## ③ 打开 App + 调音量
**打开 App**:两段式(`find` 找 / `open` 开),`open` 也支持只传名字。
· Manifest 里加了 `<queries>` LAUNCHER intent —— Android 11+ 不声明这条,
`queryIntentActivities` 只返回空列表,表现是"装了 8 个 App 一个都找不到"
· 匹配顺序:包名完全相同 → 名字完全相同 → 前缀 → 包含 → 包名包含
· 找不到时错误里**带上相近的名字**,模型能自己改一个再试
**调音量**:六种流分开(music/ring/alarm/call/system/notification)。
· `mode=set` 的 value 是**百分比**,工具侧先读 max 再换算成档位 ——
安卓原始值按流的 max 算(媒体通常 15 档、铃声 7 档),
直接把 50 当原始值会撞上限
· 改完**必须回读确认**:勿扰模式开着时系统会**拒绝**改铃声/通知音量而且**不报错**
(静默失败)。不回读的话模型会告诉用户"调好了"而实际没变 —— 比说"没调成"糟得多
## 实测(Android 14 x86_64)
音量:读出 6 个流;set 40% → 实际 40%(换算对了);up 6→7;mute→0;unmute→7 ✓
App:列出 8 个;找到「设置」;不存在的名字 → 如实报错 + 指路 ✓
通知:`cmd notification post` 发一条 → 内核读到
`{app:"Shell", title:"测试标题", text:"…12345", ago:"刚刚", gone:false}` ✓
笔记:老位置的文件被搬到新位置,界面显示 1 条、内容正确 ✓
## 顺带修的
· `apps` 工具的"找不到"提示里写的是 `action=app_list` —— **那个 action 不存在**
(实际叫 `list`),模型照着调会失败。改成 `action=list`。
· 写测试脚本时踩到:工具抛错时响应是**顶层** `{ok:false, error}`,不是
`{result:{error}}`。第一版脚本只看了 result,于是三条断言全红,
看起来像"错误信息没给",其实给得挺全。测试脚本必须按真实响应形状取值。
140 lines
6.0 KiB
Python
140 lines
6.0 KiB
Python
"""
|
||
运行期路径与平台判断。
|
||
|
||
为什么单独一个模块:
|
||
* Node 版把路径写在 config.mjs 的模块级常量里(读进程环境变量),
|
||
Python 版要能在安卓(Chaquopy)里被反复以不同参数启动,所以做成可配置单例。
|
||
* 所有落盘位置都从「数据目录」派生,安卓上把数据目录指到
|
||
/data/data/<包名>/files 或外部存储即可,源码目录保持只读。
|
||
|
||
只用标准库;安卓相关判断也只用标准库(sys.platform / os.environ)。
|
||
"""
|
||
|
||
import os
|
||
import sys
|
||
|
||
# 让 `python android/kernel/main.py` 和 `python -m android.kernel` 两种启动方式
|
||
# 都能用扁平导入(import store / from app import ...)。tools/*.py 也是这个套路。
|
||
_HERE = os.path.dirname(os.path.abspath(__file__))
|
||
if _HERE not in sys.path:
|
||
sys.path.insert(0, _HERE)
|
||
|
||
# 仓库根目录:android/kernel/ -> android/ -> <repo>
|
||
REPO_ROOT = os.path.dirname(os.path.dirname(_HERE))
|
||
|
||
|
||
def _detect_android() -> bool:
|
||
"""
|
||
判断是否跑在安卓的 CPython(Chaquopy)里。
|
||
Chaquopy 会设 sys.platform == 'android'(较新版本)或提供 sys.getandroidapilevel(),
|
||
老版本两个都没有,只能退回看 ANDROID_ROOT / ANDROID_DATA 环境变量。
|
||
"""
|
||
if sys.platform.startswith('android') or sys.platform in ('linux-android', 'android'):
|
||
return True
|
||
if hasattr(sys, 'getandroidapilevel'):
|
||
return True
|
||
return bool(os.environ.get('ANDROID_ROOT') or os.environ.get('ANDROID_DATA'))
|
||
|
||
|
||
IS_ANDROID = _detect_android()
|
||
IS_WINDOWS = sys.platform.startswith('win')
|
||
|
||
|
||
class Paths:
|
||
"""一次内核进程用到的全部目录/文件位置。"""
|
||
|
||
def __init__(
|
||
self,
|
||
data_dir: str | None = None,
|
||
tools_dir: str | None = None,
|
||
web_dir: str | None = None,
|
||
dist_dir: str | None = None,
|
||
web_core_dir: str | None = None,
|
||
):
|
||
# 数据目录:安卓上由 Kotlin 侧(或 --data-dir)指定,默认放在内核目录下的 data/
|
||
self.data_dir = os.path.abspath(
|
||
data_dir or os.environ.get('DOUBAO_DATA') or os.path.join(_HERE, 'data')
|
||
)
|
||
# 内置工具目录(仓库里的 tools/;settings.toolsDir 可以在运行时覆盖它)
|
||
self.tools_dir = os.path.abspath(
|
||
tools_dir or os.environ.get('DOUBAO_TOOLS') or os.path.join(REPO_ROOT, 'tools')
|
||
)
|
||
# 静态托管:宿主页 + Lynx 产物 + web-core 运行时(安卓上一般用不上,
|
||
# 因为页面在 assets 里,但保留以便桌面/局域网调试时同一端口全包)
|
||
self.web_dir = os.path.abspath(web_dir or os.environ.get('DOUBAO_WEB') or os.path.join(REPO_ROOT, 'web'))
|
||
self.dist_dir = os.path.abspath(
|
||
dist_dir or os.environ.get('DOUBAO_DIST') or os.path.join(REPO_ROOT, 'app', 'dist')
|
||
)
|
||
self.web_core_dir = self._resolve_web_core(web_core_dir)
|
||
|
||
# 数据目录下的固定文件(文件名与 Node 版保持一致,方便两边互相读)
|
||
self.uploads_dir = os.path.join(self.data_dir, 'uploads')
|
||
self.tmp_dir = os.path.join(self.data_dir, 'tmp')
|
||
# ★ AI 弹出来的那些 HTML 界面存这儿 —— 用户关掉之后还要能再打开
|
||
# (用户明确要求:"AI 写的界面可以再点开")
|
||
self.screens_dir = os.path.join(self.data_dir, 'screens')
|
||
# ★ notes 工具的笔记存这儿("notes 用户要能查看")。
|
||
#
|
||
# 以前 notes.py 自己算路径:`工具文件/../data/notes` ——
|
||
# 在安卓上会落到 **App 的外部私有目录**
|
||
# (`/storage/emulated/0/Android/data/<包名>/files/data/notes`)。
|
||
# 那个地方用户用文件管理器根本翻不到(Android 11+ 直接隐藏),
|
||
# 而且**卸载 App 就一起删了**。
|
||
# 放到 data_dir 下和别的数据一起,持久、位置统一、也能通过接口读出来给界面看。
|
||
self.notes_dir = os.path.join(self.data_dir, 'notes')
|
||
self.settings_file = os.path.join(self.data_dir, 'settings.json')
|
||
self.conversations_file = os.path.join(self.data_dir, 'conversations.json')
|
||
self.agents_file = os.path.join(self.data_dir, 'agents.json')
|
||
self.mcp_file = os.path.join(self.data_dir, 'mcp.servers.json')
|
||
|
||
@staticmethod
|
||
def _resolve_web_core(explicit: str | None) -> str:
|
||
candidates = [
|
||
explicit,
|
||
os.environ.get('DOUBAO_WEB_CORE'),
|
||
os.path.join(REPO_ROOT, 'web-core'),
|
||
os.path.join(REPO_ROOT, 'app', 'node_modules', '@lynx-js', 'web-core', 'dist', 'client_prod', 'static'),
|
||
]
|
||
for item in candidates:
|
||
if item and os.path.isdir(item):
|
||
return os.path.abspath(item)
|
||
# 都不存在时也返回最后一个候选,交给 404 分支处理(和 Node 版行为一致)
|
||
return os.path.abspath(candidates[-1])
|
||
|
||
def ensure_dirs(self) -> None:
|
||
for folder in (self.data_dir, self.uploads_dir, self.tmp_dir, self.screens_dir,
|
||
self.notes_dir):
|
||
try:
|
||
os.makedirs(folder, exist_ok=True)
|
||
except OSError as exc: # 安卓上外部存储没授权时会失败,不能让它把进程打死
|
||
print(f'[paths] 无法创建目录 {folder}:{exc}', file=sys.stderr)
|
||
|
||
def as_dict(self) -> dict:
|
||
return {
|
||
'dataDir': self.data_dir,
|
||
'toolsDir': self.tools_dir,
|
||
'webDir': self.web_dir,
|
||
'distDir': self.dist_dir,
|
||
'webCoreDir': self.web_core_dir,
|
||
'settingsFile': self.settings_file,
|
||
}
|
||
|
||
|
||
_current: Paths | None = None
|
||
|
||
|
||
def configure(**kwargs) -> Paths:
|
||
"""(重新)配置全局路径;main.py 启动时调一次,测试里可以反复调。"""
|
||
global _current
|
||
_current = Paths(**kwargs)
|
||
_current.ensure_dirs()
|
||
return _current
|
||
|
||
|
||
def get() -> Paths:
|
||
"""取当前路径配置,没配置过就用默认值(方便被安卓侧直接 import 调用)。"""
|
||
global _current
|
||
if _current is None:
|
||
_current = configure()
|
||
return _current
|