leejunyoung5819wayland-android-remote

공개

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

79줄3.4 KB
1package com.androidremote.companion
2
3import android.app.Notification
4import android.app.NotificationChannel
5import android.app.NotificationManager
6import android.app.PendingIntent
7import android.app.RemoteInput
8import android.content.BroadcastReceiver
9import android.content.Context
10import android.content.Intent
11
12/**
13 * 무선 디버깅 페어링 코드 입력 알림 (기획서 5.4 "페어링 코드 입력").
14 * 사용자는 설정의 페어링 코드 창을 연 채로 알림의 답장 칸에 6자리 코드를 입력한다.
15 */
16object PairingNotifier {
17 private const val CHANNEL = "pairing"
18 const val ID = 1001
19 const val KEY_CODE = "code"
20
21 fun show(context: Context, text: String = "무선 디버깅 > '페어링 코드로 기기 페어링'의 6자리 코드를 입력하세요") {
22 val nm = context.getSystemService(NotificationManager::class.java)
23 nm.createNotificationChannel(
24 NotificationChannel(CHANNEL, "무선 디버깅 페어링", NotificationManager.IMPORTANCE_HIGH),
25 )
26 val input = RemoteInput.Builder(KEY_CODE).setLabel("페어링 코드 6자리").build()
27 val reply = PendingIntent.getBroadcast(
28 context, 0, Intent(context, PairingReceiver::class.java),
29 PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_MUTABLE,
30 )
31 val action = Notification.Action.Builder(null, "코드 입력", reply).addRemoteInput(input).build()
32 val n = Notification.Builder(context, CHANNEL)
33 .setSmallIcon(android.R.drawable.stat_sys_data_bluetooth)
34 .setContentTitle("Android Remote 페어링")
35 .setContentText(text)
36 .setOngoing(true)
37 .addAction(action)
38 .build()
39 nm.notify(ID, n)
40 }
41
42 fun result(context: Context, text: String) {
43 val n = Notification.Builder(context, CHANNEL)
44 .setSmallIcon(android.R.drawable.stat_sys_data_bluetooth)
45 .setContentTitle("Android Remote 페어링")
46 .setContentText(text)
47 .setAutoCancel(true)
48 .build()
49 context.getSystemService(NotificationManager::class.java).notify(ID, n)
50 }
51}
52
53/** 알림 답장으로 받은 코드로 페어링하고, 성공하면 바로 데몬을 띄운다. */
54class PairingReceiver : BroadcastReceiver() {
55 override fun onReceive(context: Context, intent: Intent) {
56 val code = RemoteInput.getResultsFromIntent(intent)?.getCharSequence(PairingNotifier.KEY_CODE)?.toString()
57 if (code.isNullOrBlank()) return
58 PairingNotifier.result(context, "페어링 중…")
59 val pending = goAsync()
60 val app = context.applicationContext
61 Thread {
62 try {
63 val paired = Bootstrap.pair(app, code)
64 if (paired.isFailure) {
65 PairingNotifier.show(app, "페어링 실패: ${paired.exceptionOrNull()?.message}. 다시 입력하세요")
66 return@Thread
67 }
68 val started = Bootstrap.launchDaemon(app)
69 PairingNotifier.result(
70 app,
71 if (started.isSuccess) "페어링 완료, 서비스를 시작했습니다"
72 else "페어링 완료. 서비스 시작 실패: ${started.exceptionOrNull()?.message}",
73 )
74 } finally {
75 pending.finish()
76 }
77 }.start()
78 }
79}