Modern Apps git
git clone https://git.maproj.org/Modern-Apps.git
dependencies ▾

Modern-Apps.git / communicate-dev / src / main / java / com / vayunmathur / communicate / data / rcs / RcsProvisioningWatcher.kt

RcsProvisioningWatcher.kt108 lines · 4.1 KiBRaw
1package com.vayunmathur.communicate.data.rcs
2
3import android.content.Context
4import android.telephony.SubscriptionManager
5import com.vayunmathur.library.log.Log
6import java.util.concurrent.Executors
7import kotlinx.coroutines.CoroutineScope
8import kotlinx.coroutines.Dispatchers
9import kotlinx.coroutines.SupervisorJob
10import kotlinx.coroutines.launch
11
12/**
13 * Live provisioning watcher: registers the hidden
14 * `ProvisioningManager` RCS callback (see [RcsHiddenApi]) so carrier config
15 * changes reach us without re-probing.
16 *
17 * On `onConfigurationChanged` the raw RCS config XML is parsed for the
18 * FT-over-HTTP content-server URI (`ftHTTPCSURI` →
19 * [RcsContentServer.uri], which is what activates the
20 * FT-over-HTTP upload path) and provisioning is re-probed to refresh the
21 * transport state. On reset/removal the content-server URI is cleared and
22 * the state re-probed.
23 *
24 * Owned by the transport lifecycle: [watch] at `ensureRegistered` time,
25 * [unwatch] on teardown. Best-effort throughout — the bridge returns false
26 * without carrier privilege and everything keeps working on the last-known
27 * values. Never throws.
28 */
29object RcsProvisioningWatcher {
30 private const val TAG = "RcsProvWatch"
31
32 private val scope = CoroutineScope(SupervisorJob() + Dispatchers.IO)
33 private val executor = Executors.newSingleThreadExecutor()
34
35 @Volatile
36 private var manager: Any? = null
37
38 @Volatile
39 private var watchingSubId: Int = SubscriptionManager.INVALID_SUBSCRIPTION_ID
40
41 /**
42 * Register for provisioning callbacks on [subId]. Idempotent per subId —
43 * re-calls for the same subId are no-ops. Call [unwatch] first when
44 * switching subscriptions.
45 */
46 fun watch(context: Context, subId: Int) {
47 if (!RcsFeature.enabled) return
48 if (!SubscriptionManager.isValidSubscriptionId(subId)) return
49 if (subId == watchingSubId && manager != null) return
50 unwatch()
51 val pm = RcsHiddenApi.provisioningManager(subId) ?: run {
52 Log.status(TAG, "No provisioning manager for subId=$subId (no privilege or unsupported)")
53 return
54 }
55 val app = context.applicationContext
56 val ok = RcsHiddenApi.registerProvisioningCallback(
57 provisioningManager = pm,
58 executor = executor,
59 rcsVersion = "6.0",
60 rcsProfile = "UP_2.0",
61 callback = object : RcsHiddenApi.ProvisioningCallback {
62 override fun onConfigurationChanged(configXml: ByteArray) {
63 scope.launch { applyConfig(app, subId, configXml) }
64 }
65
66 override fun onConfigurationReset() {
67 scope.launch {
68 Log.status(TAG, "Provisioning config reset for subId=$subId")
69 RcsContentServer.uri = null
70 RcsProvisioning.probe(app, subId)
71 }
72 }
73
74 override fun onRemoved() {
75 scope.launch {
76 Log.status(TAG, "Provisioning removed for subId=$subId")
77 RcsContentServer.uri = null
78 RcsProvisioning.probe(app, subId)
79 }
80 }
81 },
82 )
83 if (ok) {
84 manager = pm
85 watchingSubId = subId
86 Log.status(TAG, "Watching provisioning for subId=$subId")
87 }
88 }
89
90 /** Unregister (best-effort) and drop watcher state. */
91 fun unwatch() {
92 manager?.let { runCatching { RcsHiddenApi.unregisterProvisioningCallback(it) } }
93 manager = null
94 watchingSubId = SubscriptionManager.INVALID_SUBSCRIPTION_ID
95 }
96
97 private suspend fun applyConfig(context: Context, subId: Int, configXml: ByteArray) {
98 if (!RcsFeature.enabled || configXml.isEmpty()) return
99 RcsContentServer.parse(configXml)?.let { uri ->
100 if (RcsContentServer.uri != uri) {
101 Log.status(TAG, "FT content server configured for subId=$subId")
102 RcsContentServer.uri = uri
103 }
104 }
105 // Config changed under us — refresh the transport state.
106 RcsProvisioning.probe(context, subId)
107 }
108}