forked from fmeum/WearAuthn
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathComplicationConfigActivity.kt
77 lines (66 loc) · 2.45 KB
/
ComplicationConfigActivity.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package me.henneke.wearauthn.complication
import android.app.Activity
import android.os.Bundle
import android.preference.Preference
import android.preference.PreferenceFragment
import android.preference.PreferenceScreen
import android.support.wearable.complications.ComplicationProviderService
import android.support.wearable.preference.WearablePreferenceActivity
import me.henneke.wearauthn.R
import me.henneke.wearauthn.bthid.canUseAuthenticator
import me.henneke.wearauthn.bthid.defaultAdapter
import me.henneke.wearauthn.ui.BluetoothDevicePreference
class ComplicationConfigActivity : WearablePreferenceActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val shortcutPicker = ShortcutPicker().apply {
arguments = intent.extras
check(arguments.containsKey(ComplicationProviderService.EXTRA_CONFIG_COMPLICATION_ID)) { "No complication ID provided." }
}
startPreferenceFragment(shortcutPicker, false)
}
}
class ShortcutPicker : PreferenceFragment() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
preferenceScreen = preferenceManager.createPreferenceScreen(context)
preferenceScreen.setTitle(R.string.preference_screen_title_shortcut_picker)
}
override fun onPreferenceTreeClick(
preferenceScreen: PreferenceScreen?,
preference: Preference?
): Boolean {
return if (preference != null) {
ShortcutComplicationProviderService.setDeviceShortcut(
context,
arguments.getInt(ComplicationProviderService.EXTRA_CONFIG_COMPLICATION_ID),
preference.key
)
activity.apply {
setResult(Activity.RESULT_OK)
finish()
}
true
} else {
super.onPreferenceTreeClick(preferenceScreen, preference)
}
}
override fun onResume() {
super.onResume()
createPreferences()
}
override fun onPause() {
super.onPause()
clearPreferences()
}
private fun createPreferences() {
for (device in defaultAdapter.bondedDevices) {
if (!device.canUseAuthenticator)
continue
preferenceScreen.addPreference(BluetoothDevicePreference(context, device))
}
}
private fun clearPreferences() {
preferenceScreen.removeAll()
}
}