leejunyoung5819wayland-android-remote

공개

Ubuntu(Wayland/X11)에서 Android 기기 화면 미러링 및 원격 제어 (Rust)

54줄2.1 KB
1package com.androidremote.companion
2
3import android.app.Notification
4import android.app.NotificationChannel
5import android.app.NotificationManager
6import android.app.Service
7import android.content.BroadcastReceiver
8import android.content.Context
9import android.content.Intent
10import android.content.pm.ServiceInfo
11import android.os.IBinder
12import android.util.Log
13
14/** 재부팅·앱 업데이트 뒤 데몬을 자동으로 다시 띄운다 (기획서 5.4 "재부팅 후 자동 재기동"). */
15class BootReceiver : BroadcastReceiver() {
16 override fun onReceive(context: Context, intent: Intent) {
17 if (!Bootstrap.isPaired(context) || !Bootstrap.canToggleWirelessDebugging(context)) return
18 context.startForegroundService(Intent(context, StartService::class.java))
19 }
20}
21
22/**
23 * 데몬 기동을 맡는 짧은 포그라운드 서비스. 무선 디버깅은 Wi-Fi가 있어야 켜지므로
24 * 실패하면 10초 간격으로 몇 번 다시 시도한 뒤 끝낸다.
25 */
26class StartService : Service() {
27 override fun onBind(intent: Intent?): IBinder? = null
28
29 override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
30 val nm = getSystemService(NotificationManager::class.java)
31 nm.createNotificationChannel(NotificationChannel(CHANNEL, "서비스 시작", NotificationManager.IMPORTANCE_LOW))
32 val n = Notification.Builder(this, CHANNEL)
33 .setSmallIcon(android.R.drawable.stat_sys_data_bluetooth)
34 .setContentTitle("Android Remote 서비스 시작 중")
35 .build()
36 startForeground(ID, n, ServiceInfo.FOREGROUND_SERVICE_TYPE_SPECIAL_USE)
37 Thread {
38 for (attempt in 1..RETRIES) {
39 val r = Bootstrap.launchDaemon(applicationContext)
40 if (r.isSuccess) break
41 Log.w("AndroidRemote", "자동 기동 실패($attempt/$RETRIES): ${r.exceptionOrNull()?.message}")
42 Thread.sleep(10_000)
43 }
44 stopSelf()
45 }.start()
46 return START_NOT_STICKY
47 }
48
49 companion object {
50 private const val CHANNEL = "start"
51 private const val ID = 1002
52 private const val RETRIES = 6
53 }
54}