leejunyoung5819 wayland-android-remote
공개Ubuntu(Wayland/X11)에서 Android 기기 화면 미러링 및 원격 제어 (Rust)
| 1 | package com.androidremote.companion |
| 2 | |
| 3 | import android.app.Notification |
| 4 | import android.app.NotificationChannel |
| 5 | import android.app.NotificationManager |
| 6 | import android.app.Service |
| 7 | import android.content.BroadcastReceiver |
| 8 | import android.content.Context |
| 9 | import android.content.Intent |
| 10 | import android.content.pm.ServiceInfo |
| 11 | import android.os.IBinder |
| 12 | import android.util.Log |
| 13 | |
| 14 | /** 재부팅·앱 업데이트 뒤 데몬을 자동으로 다시 띄운다 (기획서 5.4 "재부팅 후 자동 재기동"). */ |
| 15 | class 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 | */ |
| 26 | class 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 | } |