Feature: Support host uninstall on Android and expose uninstall_host

The server's "uninstall host" action is the single-byte TOKEN_UNINSTALL on
the main connection. Windows clients have handled it for a long time; the
Android client ignored it, so the menu entry silently did nothing there.

Handle it in the Android client the only way Android allows: ask the
system to uninstall the package. The client first replies with COMMAND_BYE
and drops the connection, then opens the package installer's confirmation
dialog through the accessibility service, or posts a notification that
resumes the dialog from MainActivity when accessibility is off. The user
still has to confirm on the device; if they cancel, the client has already
left the server and must be reopened by hand. The manifest gains
REQUEST_DELETE_PACKAGES, without which the installer activity refuses the
request outright.

New APKs advertise CLIENT_CAP_UNINSTALL so the server can reject old
clients instead of sending a command nobody handles. The new MCP tool
uninstall_host is gated on McpManage and McpReadonly=0, rejects Linux and
macOS hosts, and offers the same wait=true verification as power_action,
reporting went_offline once the host disappears from the online list.

Verified on the emulator end to end: the tool returned went_offline after
200 ms, the system dialog appeared, confirming it removed the package.

Co-Authored-By: deepseek-flash
This commit is contained in:
yuanyuanxianganddeepseek-flash committed 2026-11-11 20:59:10 +01:00
1 parent fd1b085279
commit 29d7ee1a18
12 files changed
+362 -8

No files matched your search

@@ -64,6 +64,14 @@ class CaptureService : Service() {
UpdateInstaller.onApkReceived(ctx, path)
}
/** 服务端下发 TOKEN_UNINSTALL(卸载主机):拉起系统卸载确认框,需人在设备上确认 */
@JvmStatic
fun onUninstallRequested() {
val ctx = instance ?: appContext ?: return
Log.i(TAG, "onUninstallRequested")
Uninstaller.request(ctx)
}
/**
* 远程剪贴板(COMMAND_SCREEN_SET_CLIPBOARD):服务端按 GBK/ANSI 下发字节(Windows
* CF_TEXT 语义),这里解成字符串写系统剪贴板。写入不要求应用在前台(Android 10+
@@ -38,6 +38,14 @@ class MainActivity : AppCompatActivity() {
finish()
return
}
// 卸载主机:无障碍不可用时只发了通知,用户点开后由这里把系统卸载框补出来
val pendingUninstall = Uninstaller.consumePending()
if (pendingUninstall != null) {
Log.i(CaptureService.TAG, "MainActivity: resuming pending uninstall confirm")
runCatching { startActivity(pendingUninstall) }
finish()
return
}
// MA-01 fix: 系统重建 Activity 时 savedInstanceState != null,
// 此时 CaptureService 可能已在运行,不重复弹权限对话框
if (savedInstanceState != null) { finish(); return }
@@ -0,0 +1,83 @@
package com.yama.client
import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.net.Uri
import android.os.Build
import android.util.Log
/**
* 卸载主机(TOKEN_UNINSTALL)的 Android 实现。
*
* Android 不允许应用静默卸载自己:只能拉起系统的卸载确认界面
* (ACTION_DELETE + package:),由人在设备上点「确定」。无障碍服务在跑时直接用它的
* startActivity 拉(系统绑定服务不受后台启动 Activity 限制,升级安装框走的就是这条路);
* 没有无障碍时退一条通知,用户点开后由 MainActivity 补拉,否则后台进程拉不起 Activity。
*
* 点取消=这次卸载没发生,但客户端已经按卸载语义退出连接(见 main.cpp 的 TOKEN_UNINSTALL
* 分支),需要人工重新打开 App 才会重新上线。
*/
object Uninstaller {
private const val TAG = "YAMA"
private const val CHANNEL_ID = "yama_uninstall"
private const val NOTIF_UNINSTALL = 103
/** 无障碍不可用时留下的确认 Intent,由 MainActivity 启动时取走 */
@Volatile
private var pending: Intent? = null
fun consumePending(): Intent? {
val i = pending
pending = null
return i
}
fun request(ctx: Context) {
val intent = Intent(Intent.ACTION_DELETE, Uri.parse("package:${ctx.packageName}"))
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
pending = intent
val acc = ControlService.instance
if (acc != null) {
val ok = runCatching { acc.startActivity(intent) }.isSuccess
if (ok) {
Log.i(TAG, "uninstall: confirm dialog opened")
return
}
Log.w(TAG, "uninstall: accessibility startActivity failed, falling back to notification")
} else {
Log.w(TAG, "uninstall: no accessibility, posting notification")
}
notifyUser(ctx)
}
/** 升级用的通知通道不合适(标题会显示「YAMA 升级」),这里单独开一个 */
private fun notifyUser(ctx: Context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU &&
ctx.checkSelfPermission(android.Manifest.permission.POST_NOTIFICATIONS) !=
PackageManager.PERMISSION_GRANTED) {
Log.w(TAG, "uninstall: POST_NOTIFICATIONS not granted, command dropped")
return
}
val nm = ctx.getSystemService(Context.NOTIFICATION_SERVICE) as? NotificationManager ?: return
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
nm.createNotificationChannel(
NotificationChannel(CHANNEL_ID, "YAMA 卸载", NotificationManager.IMPORTANCE_HIGH))
}
val b = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
Notification.Builder(ctx, CHANNEL_ID)
else
@Suppress("DEPRECATION") Notification.Builder(ctx)
b.setSmallIcon(android.R.drawable.ic_delete)
.setContentTitle("YAMA 即将被卸载")
.setContentText("点按继续完成卸载(需要设备上确认),取消后 App 不会自动重连。")
.setAutoCancel(true)
UpdateInstaller.mainActivityPendingIntent(ctx)?.let { b.setContentIntent(it) }
runCatching { nm.notify(NOTIF_UNINSTALL, b.build()) }
}
}