Skip to content

Commit 82491b5

Browse files
Gong-Mihamjin
authored andcommitted
fix: Android 12+ foreground service & SplashScreen adaptation
- ProfileReceiver: Fix ForegroundServiceStartNotAllowedException on Android 12+ when AlarmManager triggers background profile updates. Falls back to direct background update via coroutine. - SplashScreen: Add values-v31/themes.xml to adapt Android 12 mandatory SplashScreen API. - Logs: Add READ_LOGS permission for crash log collection on rooted/debug devices. Squashed from PR MetaCubeX#726 (Gong-Mi/ClashMetaForAndroid).
1 parent 9df2ede commit 82491b5

9 files changed

Lines changed: 74 additions & 2 deletions

File tree

app/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ dependencies {
1818

1919
implementation(libs.kotlin.coroutine)
2020
implementation(libs.androidx.core)
21+
implementation(libs.androidx.splashscreen)
2122
implementation(libs.androidx.activity)
2223
implementation(libs.androidx.fragment)
2324
implementation(libs.androidx.appcompat)

app/src/main/AndroidManifest.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
tools:ignore="QueryAllPackagesPermission" />
1717
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_SPECIAL_USE" />
1818
<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>
19+
<uses-permission android:name="android.permission.READ_LOGS" />
1920

2021
<application
2122
android:name=".MainApplication"
@@ -43,7 +44,8 @@
4344
android:configChanges="uiMode"
4445
android:exported="true"
4546
android:label="@string/launch_name"
46-
android:launchMode="singleTask">
47+
android:launchMode="singleTask"
48+
android:theme="@style/SplashScreenTheme">
4749
<intent-filter>
4850
<action android:name="android.intent.action.APPLICATION_PREFERENCES" />
4951
<action android:name="android.intent.action.MAIN" />

app/src/main/java/com/github/kr328/clash/MainActivity.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import androidx.activity.result.contract.ActivityResultContracts
99
import androidx.activity.result.contract.ActivityResultContracts.RequestPermission
1010
import androidx.core.app.ActivityCompat
1111
import androidx.core.content.ContextCompat
12+
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
1213
import androidx.core.content.pm.ShortcutInfoCompat
1314
import androidx.core.content.pm.ShortcutManagerCompat
1415
import androidx.core.graphics.drawable.IconCompat
@@ -149,6 +150,8 @@ class MainActivity : BaseActivity<MainDesign>() {
149150
}
150151

151152
override fun onCreate(savedInstanceState: Bundle?) {
153+
installSplashScreen()
154+
152155
super.onCreate(savedInstanceState)
153156
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
154157
val requestPermissionLauncher =
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<vector xmlns:android="http://schemas.android.com/apk/res/android"
2+
xmlns:tools="http://schemas.android.com/tools"
3+
android:width="205dp"
4+
android:height="205dp"
5+
android:viewportWidth="205"
6+
android:viewportHeight="200"
7+
android:tint="@color/color_clash">
8+
<path
9+
android:pathData="M 31.55 16.82 C 32.47 16.76 33.46 16.72 34.27 17.21 C 35.55 17.98 36.58 19.09 37.68 20.07 C 54.09 35.08 70.55 50.02 86.96 65.03 C 88.06 66.02 89.1 67.14 90.46 67.79 C 91.53 68.3 92.75 68.05 93.88 67.89 C 96.89 67.38 99.95 67.35 102.99 67.35 C 105.92 67.4 108.87 67.49 111.76 67.97 C 112.92 68.11 114.31 68.48 115.19 67.47 C 133.27 50.95 151.41 34.47 169.52 17.94 C 171.05 16.35 173.4 16.7 175.4 16.94 C 180.44 17.69 185.53 18.04 190.6 18.61 C 191.44 18.77 192.63 18.83 192.92 19.84 C 193.32 21.57 193.1 23.36 193.14 25.12 C 193.12 60.85 193.12 96.58 193.1 132.31 C 193.08 134.12 193.2 135.94 193.02 137.73 C 192.94 138.38 192.86 139.13 192.33 139.58 C 191.21 140.08 189.96 140.25 188.78 140.55 C 183.71 141.73 178.57 142.58 173.52 143.87 C 170.99 144.53 168.37 144.28 165.79 144.32 C 164.47 144.3 163.11 144.41 161.79 144.2 C 161.02 144.14 160.47 143.45 160.41 142.7 C 160.13 140.92 160.29 139.13 160.27 137.34 C 160.25 119.75 160.23 102.16 160.25 84.57 C 160.23 82.83 160.35 81.08 160.11 79.34 C 159.79 77.35 157.71 75.95 155.75 76.19 C 154.67 76.21 153.74 76.88 152.95 77.57 C 144.22 85.61 135.39 93.52 126.67 101.55 C 125.66 102.42 124.52 103.42 123.1 103.28 C 121.23 103.12 119.47 102.45 117.66 102.04 C 110.4 100.22 102.83 100.01 95.42 100.64 C 91.81 101.13 88.16 101.55 84.69 102.69 C 83.57 103 82.44 103.36 81.28 103.3 C 80.1 103.22 79.17 102.43 78.32 101.7 C 69.66 93.74 60.91 85.91 52.25 77.94 C 51.41 77.21 50.5 76.41 49.35 76.23 C 47.92 76.05 46.44 76.64 45.45 77.69 C 44.6 78.65 44.56 79.99 44.52 81.22 C 44.56 101.19 44.5 121.15 44.54 141.12 C 44.5 141.89 44.5 142.68 44.21 143.41 C 43.75 144.24 42.69 144.28 41.86 144.32 C 39.61 144.35 37.38 144.3 35.14 144.32 C 32.61 144.35 30.17 143.62 27.72 143.11 C 23.25 142.13 18.77 141.12 14.28 140.18 C 13.33 139.94 12.01 139.66 11.83 138.5 C 11.55 136.45 11.75 134.38 11.71 132.31 C 11.67 96.72 11.69 61.12 11.69 25.53 C 11.69 23.85 11.59 22.18 11.75 20.52 C 11.81 19.91 11.99 19.2 12.58 18.89 C 13.21 18.61 13.92 18.57 14.59 18.47 C 20.27 18.08 25.91 17.39 31.55 16.82 Z M 98.96 148.02 C 101.51 147.81 104.07 147.89 106.6 148.02 C 108.57 148.01 109.79 150.39 108.88 152.07 C 107.78 154.27 106.63 156.44 105.33 158.55 C 104.51 160.07 102.1 160.37 101.02 158.97 C 99.97 157.69 99.36 156.15 98.53 154.73 C 97.9 153.54 97.09 152.42 96.79 151.08 C 96.5 149.68 97.56 148.22 98.96 148.02 Z M 13.54 152.62 C 14.04 152.56 14.53 152.52 15.04 152.52 C 29.14 152.56 43.24 152.54 57.36 152.54 C 58.62 152.52 60.06 152.83 60.81 153.98 C 61.58 155.26 61.42 157.25 60.08 158.1 C 59.17 158.73 58.01 158.73 56.94 158.77 C 42.98 158.73 29 158.75 15.04 158.77 C 14.26 158.73 13.47 158.73 12.74 158.48 C 11.53 158.02 10.73 156.7 10.9 155.42 C 11 154.04 12.11 152.74 13.54 152.62 Z M 146.68 152.62 C 148.57 152.42 150.49 152.58 152.4 152.54 C 164.96 152.52 177.54 152.58 190.1 152.52 C 190.91 152.56 191.76 152.54 192.51 152.87 C 193.63 153.37 194.26 154.59 194.26 155.79 C 194.28 157.19 193.12 158.51 191.72 158.65 C 189.92 158.87 188.11 158.73 186.31 158.75 L 151.55 158.75 C 149.78 158.73 147.98 158.89 146.23 158.59 C 144.91 158.36 143.82 157.15 143.92 155.77 C 143.74 154.15 145.12 152.73 146.68 152.62 Z M 56.69 167.7 C 57.62 167.45 58.66 167.31 59.55 167.76 C 61.4 168.61 61.84 171.57 60.18 172.85 C 59.19 173.54 58.01 173.81 56.91 174.21 C 52.73 175.51 48.66 177.15 44.48 178.45 C 39.67 179.97 34.96 181.76 30.17 183.38 C 26.68 184.5 23.21 185.68 19.74 186.89 C 18.3 187.38 16.9 188.01 15.42 188.33 C 14.51 188.52 13.5 188.35 12.79 187.74 C 11.61 186.81 11.39 185.02 12.07 183.73 C 12.62 182.81 13.72 182.47 14.67 182.1 C 17.67 181.05 20.62 179.97 23.64 179.02 C 31.19 176.57 38.62 173.74 46.2 171.35 C 49.73 170.21 53.16 168.81 56.69 167.7 Z M 146.25 167.55 C 147.33 167.29 148.4 167.7 149.42 168 C 152.72 169.12 155.95 170.38 159.26 171.45 C 167.05 173.91 174.68 176.85 182.49 179.32 C 185.05 180.14 187.59 181.09 190.16 181.98 C 191.13 182.35 192.19 182.67 192.9 183.48 C 194.02 185.02 193.35 187.52 191.56 188.21 C 190.4 188.66 189.17 188.21 188.05 187.86 C 183.41 186.14 178.72 184.6 174.03 183.06 C 169.46 181.49 164.92 179.81 160.33 178.35 C 156.15 177.03 152.1 175.39 147.92 174.11 C 146.82 173.72 145.64 173.44 144.75 172.65 C 143.15 171.09 144.02 167.94 146.25 167.55 Z"
10+
android:fillColor="#FFFFFFFF" />
11+
</vector>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<resources>
3+
<style name="SplashScreenTheme" parent="Theme.SplashScreen">
4+
<item name="windowSplashScreenBackground">@color/color_dark_background</item>
5+
<item name="windowSplashScreenAnimatedIcon">@drawable/ic_logo</item>
6+
<item name="postSplashScreenTheme">@style/BootstrapTheme</item>
7+
</style>
8+
</resources>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<resources>
3+
<style name="AppThemeLight" parent="AppThemeLightBase">
4+
<item name="android:statusBarColor">@color/color_system_ui_overlay</item>
5+
<item name="android:navigationBarColor">@color/color_system_ui_overlay</item>
6+
<item name="android:windowSplashScreenBackground">@color/color_light_background</item>
7+
<item name="android:windowSplashScreenAnimatedIcon">@drawable/ic_logo</item>
8+
</style>
9+
<style name="AppThemeDark" parent="AppThemeDarkBase">
10+
<item name="android:statusBarColor">@android:color/transparent</item>
11+
<item name="android:navigationBarColor">@android:color/transparent</item>
12+
<item name="android:windowSplashScreenBackground">@color/color_dark_background</item>
13+
<item name="android:windowSplashScreenAnimatedIcon">@drawable/ic_logo</item>
14+
</style>
15+
</resources>

design/src/main/res/values/themes.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,4 +166,10 @@
166166
<item name="android:statusBarColor">@android:color/transparent</item>
167167
<item name="android:navigationBarColor">@android:color/transparent</item>
168168
</style>
169+
170+
<style name="SplashScreenTheme" parent="Theme.SplashScreen">
171+
<item name="windowSplashScreenBackground">@color/color_light_background</item>
172+
<item name="windowSplashScreenAnimatedIcon">@drawable/ic_logo</item>
173+
<item name="postSplashScreenTheme">@style/BootstrapTheme</item>
174+
</style>
169175
</resources>

gradle/libs.versions.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ room = "2.4.2"
1818
multiprocess = "1.0.0"
1919
quickie = "1.11.0"
2020
androidx-activity-ktx = "1.9.0"
21+
androidx-splashscreen = "1.0.1"
2122

2223
[libraries]
2324
build-android = { module = "com.android.tools.build:gradle", version.ref = "agp" }
@@ -28,6 +29,7 @@ build-golang = { module = "com.github.kr328.golang:gradle-plugin", version.ref =
2829
kotlin-coroutine = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-android", version.ref = "coroutine" }
2930
kotlin-serialization-json = { module = "org.jetbrains.kotlinx:kotlinx-serialization-json", version.ref = "serialization" }
3031
androidx-core = { module = "androidx.core:core-ktx", version.ref = "coreKtx" }
32+
androidx-splashscreen = { module = "androidx.core:core-splashscreen", version.ref = "androidx-splashscreen" }
3133
androidx-activity = { module = "androidx.activity:activity", version.ref = "activity" }
3234
androidx-fragment = { module = "androidx.fragment:fragment", version.ref = "fragment" }
3335
androidx-appcompat = { module = "androidx.appcompat:appcompat", version.ref = "appcompat" }

service/src/main/java/com/github/kr328/clash/service/ProfileReceiver.kt

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import android.app.PendingIntent
55
import android.content.BroadcastReceiver
66
import android.content.Context
77
import android.content.Intent
8+
import android.os.Build
89
import androidx.core.content.getSystemService
910
import com.github.kr328.clash.common.Global
1011
import com.github.kr328.clash.common.compat.pendingIntentFlags
@@ -13,6 +14,7 @@ import com.github.kr328.clash.common.constants.Intents
1314
import com.github.kr328.clash.common.log.Log
1415
import com.github.kr328.clash.common.util.componentName
1516
import com.github.kr328.clash.common.util.setUUID
17+
import com.github.kr328.clash.common.util.uuid
1618
import com.github.kr328.clash.service.data.Imported
1719
import com.github.kr328.clash.service.data.ImportedDao
1820
import com.github.kr328.clash.service.model.Profile
@@ -39,7 +41,29 @@ class ProfileReceiver : BroadcastReceiver() {
3941
Intents.ACTION_PROFILE_REQUEST_UPDATE -> {
4042
val redirect = intent.setComponent(ProfileWorker::class.componentName)
4143

42-
context.startForegroundServiceCompat(redirect)
44+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
45+
try {
46+
context.startForegroundServiceCompat(redirect)
47+
} catch (e: IllegalStateException) {
48+
// Android 12+ forbids starting foreground service from background via AlarmManager
49+
Global.launch {
50+
intent.uuid?.also { uuid ->
51+
try {
52+
ProfileProcessor.update(context, uuid, null)
53+
val imported = ImportedDao().queryByUUID(uuid)
54+
if (imported != null) {
55+
scheduleNext(context, imported)
56+
}
57+
Log.i("ProfileReceiver: background update completed for $uuid")
58+
} catch (e: Exception) {
59+
Log.e("ProfileReceiver: fallback background update failed", e)
60+
}
61+
}
62+
}
63+
}
64+
} else {
65+
context.startForegroundServiceCompat(redirect)
66+
}
4367
}
4468
}
4569
}

0 commit comments

Comments
 (0)