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

+3
View File
@@ -7,6 +7,9 @@
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_MEDIA_PROJECTION" />
<!-- 在线升级:走 PackageInstaller 会话安装服务端下发的 APK,仍需系统安装器人工确认 -->
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
<!-- 卸载主机(TOKEN_UNINSTALL):ACTION_DELETE 拉系统卸载确认框,缺这个权限
UninstallerActivity 会直接报 "does not have REQUEST_DELETE_PACKAGES" 并退出 -->
<uses-permission android:name="android.permission.REQUEST_DELETE_PACKAGES" />
<!-- 升级完成/需要人工确认时的提示通知(Android 13+ 是运行时权限) -->
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
+19
View File
@@ -245,6 +245,7 @@ static jmethodID g_statusMid = nullptr; // CaptureService.onNativeSt
static jmethodID g_nativeExitMid = nullptr; // CaptureService.onNativeExit() — 服务端主动断开时停止服务
static jmethodID g_updateReceivedMid = nullptr; // CaptureService.onUpdateReceived(String) — 升级 APK 已落盘
static jmethodID g_clipboardMid = nullptr; // CaptureService.onClipboardText([B)V — 远程剪贴板
static jmethodID g_uninstallMid = nullptr; // CaptureService.onUninstallRequested()V — TOKEN_UNINSTALL
// ScreenShot(无障碍 takeScreenshot 抓屏,MCP get_screenshot 用)
static jclass g_shotClass = nullptr;
static jmethodID g_shotCanMid = nullptr; // ScreenShot.canCapture()Z
@@ -1001,6 +1002,21 @@ int DataProcess(void* /*user*/, PBYTE szBuffer, ULONG ulLength)
g_bExit = S_CLIENT_EXIT;
break;
case TOKEN_UNINSTALL: {
// 卸载主机(主控在线列表的「卸载主机」= 单字节命令,与 Windows 客户端同义)。
// Android 不允许应用静默卸载自己:先回 COMMAND_BYE 并退出连接(服务端随即把
// 本机移出在线列表),再拉起系统的卸载确认框;设备上点确认后包才真正删除。
// 用户点取消的话客户端已经退出,需要人工重新打开 App。
PostStatus("UNINSTALL from server");
if (auto mc = GetMainClient()) {
BYTE bye = COMMAND_BYE;
mc->Send2Server((char*)&bye, 1);
}
g_bExit = S_CLIENT_EXIT;
CallCaptureStaticVoid(g_uninstallMid);
break;
}
case CMD_HEARTBEAT_ACK:
if (ulLength >= 1 + (ULONG)sizeof(HeartbeatACK)) {
HeartbeatACK ack;
@@ -1473,6 +1489,9 @@ Java_com_yama_client_YamaBridge_nativeInit(
g_clipboardMid = env->GetStaticMethodID(g_captureClass, "onClipboardText", "([B)V");
if (env->ExceptionCheck()) { env->ExceptionClear(); g_clipboardMid = nullptr; }
if (!g_clipboardMid) LOGE("CaptureService.onClipboardText not found");
g_uninstallMid = env->GetStaticMethodID(g_captureClass, "onUninstallRequested", "()V");
if (env->ExceptionCheck()) { env->ExceptionClear(); g_uninstallMid = nullptr; }
if (!g_uninstallMid) LOGE("CaptureService.onUninstallRequested not found");
UseAndroidLog(PostStatus);
} else {
LOGE("CaptureService class not found");
@@ -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()) }
}
}