Skip to content

Commit 84cffdc

Browse files
authored
Merge pull request #132 from holochain/fix/open-settings
Fix open settings issue for system_settings build.
2 parents 427ba4e + f2a7ccf commit 84cffdc

File tree

2 files changed

+59
-10
lines changed

2 files changed

+59
-10
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Unreleased
22

3+
- The 'Open Settings' button will first attempt to open the android-service-runtime app via the system settings (i.e. for 'system' builds). If that fails it will fallback to opening the app via the launcher (i.e. for 'user' builds).
4+
35
# 0.2.1
46
- bump to holochain 0.5.4
57
- fix infrastructure urls
Lines changed: 57 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package org.holochain.androidserviceruntime.plugin.client
22

33
import android.app.Activity
4+
import android.content.ComponentName
5+
import android.content.Intent
6+
import android.content.pm.PackageManager
7+
import android.os.Build
48
import android.util.Log
59
import android.widget.Button
610
import androidx.cardview.widget.CardView
@@ -13,24 +17,67 @@ class DisconnectedNotice(
1317
R.layout.disconnected_notice,
1418
"DisconnectedNotice",
1519
) {
20+
companion object {
21+
private const val TAG = "DisconnectedNotice"
22+
private const val ASR_SETTINGS_ACTION = "com.android.settings.action.IA_SETTINGS"
23+
}
24+
1625
override fun setupNoticeCardView(noticeView: CardView) {
1726
// Start the app that contains the HolochainService
1827
noticeView.findViewById<Button>(R.id.openSettingsAction).setOnClickListener {
19-
try {
20-
val launchIntent = this.activity.packageManager.getLaunchIntentForPackage(servicePackage)
21-
if (launchIntent != null) {
22-
this.activity.startActivity(launchIntent)
23-
} else {
24-
Log.e(logTag, "Could not find launch intent for package " + servicePackage)
25-
}
26-
} catch (e: Exception) {
27-
Log.e(logTag, "Failed to launch package " + servicePackage, e)
28-
}
28+
openAsr()
2929
}
3030

3131
// Restart this app
3232
noticeView.findViewById<Button>(R.id.restartAction).setOnClickListener {
3333
super.restartApp()
3434
}
3535
}
36+
37+
private fun openAsr() {
38+
if (!trySettings() && !tryLauncher()) {
39+
Log.e(TAG, "Failed to open ASR via both settings and launcher")
40+
}
41+
}
42+
43+
private fun trySettings(): Boolean =
44+
try {
45+
val pm = activity.packageManager
46+
// Resolve the settings activity dynamically
47+
val probe = Intent(ASR_SETTINGS_ACTION).setPackage(servicePackage)
48+
val resolve =
49+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
50+
pm
51+
.queryIntentActivities(
52+
probe,
53+
PackageManager.ResolveInfoFlags.of(0),
54+
).firstOrNull()
55+
} else {
56+
@Suppress("DEPRECATION")
57+
pm.queryIntentActivities(probe, 0).firstOrNull()
58+
} ?: return false
59+
val target =
60+
ComponentName(resolve.activityInfo.packageName, resolve.activityInfo.name)
61+
val intent =
62+
Intent(ASR_SETTINGS_ACTION).apply {
63+
component = target
64+
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
65+
}
66+
Log.i(TAG, "targeting ASR settings: $target")
67+
activity.startActivity(intent)
68+
true
69+
} catch (t: Throwable) {
70+
Log.w(TAG, "Settings intent failed", t)
71+
false
72+
}
73+
74+
private fun tryLauncher(): Boolean =
75+
try {
76+
val launch = this.activity.packageManager.getLaunchIntentForPackage(servicePackage) ?: return false
77+
activity.startActivity(launch)
78+
true
79+
} catch (t: Throwable) {
80+
Log.w(TAG, "Launcher intent failed", t)
81+
false
82+
}
3683
}

0 commit comments

Comments
 (0)