Skip to content

Commit aa1d8b9

Browse files
committed
fix: WebAPK recents recovery using singleTask and transparent overlay
1 parent 9857c50 commit aa1d8b9

4 files changed

Lines changed: 55 additions & 19 deletions

File tree

app/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ android {
99
applicationId "com.android.chrome"
1010
minSdk 21
1111
targetSdk 34
12-
versionCode 1
13-
versionName "1.0"
12+
versionCode 2
13+
versionName "1.1"
1414
}
1515

1616
buildTypes {

app/src/main/AndroidManifest.xml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@
2121

2222
<activity
2323
android:name=".RedirectActivity"
24-
android:exported="true">
24+
android:exported="true"
25+
android:theme="@style/Theme.Transparent"
26+
android:launchMode="singleTask">
2527
<intent-filter>
2628
<action android:name="android.intent.action.VIEW" />
2729
<category android:name="android.intent.category.DEFAULT" />
@@ -49,7 +51,7 @@
4951
android:name="com.google.android.apps.chrome.webapps.WebappActivity"
5052
android:targetActivity=".RedirectActivity"
5153
android:exported="true" />
52-
54+
5355
<activity-alias
5456
android:name="org.chromium.chrome.browser.webapps.WebappLauncherActivity"
5557
android:targetActivity=".RedirectActivity"

app/src/main/java/com/android/chrome/RedirectActivity.kt

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,32 @@ import android.os.Bundle
88
import android.util.Log
99

1010
class RedirectActivity : Activity() {
11+
12+
private var hasRedirected = false
13+
1114
override fun onCreate(savedInstanceState: Bundle?) {
1215
super.onCreate(savedInstanceState)
16+
hasRedirected = false
17+
handleIntent(intent, isReEntry = false)
18+
}
1319

14-
val incomingIntent = intent
20+
override fun onNewIntent(intent: Intent) {
21+
super.onNewIntent(intent)
22+
setIntent(intent)
23+
24+
// If we already redirected and the browser is on top of us,
25+
// this is a re-entry from the launcher icon or recents.
26+
// Just do nothing — the browser activity is still on top of our back stack.
27+
if (hasRedirected) {
28+
Log.d("Redirector", "Re-entry detected, skipping redirect (browser is already on top)")
29+
return
30+
}
31+
32+
handleIntent(intent, isReEntry = true)
33+
}
34+
35+
private fun handleIntent(incomingIntent: Intent, isReEntry: Boolean) {
36+
val action = incomingIntent.action
1537
var data = incomingIntent.data
1638

1739
// WebAPK specific extras might contain the URL
@@ -25,44 +47,46 @@ class RedirectActivity : Activity() {
2547
if (data != null) {
2648
Log.d("Redirector", "Redirecting URL: $data")
2749

28-
// Get target package from preferences
2950
val prefs = getSharedPreferences("RedirectorPrefs", Context.MODE_PRIVATE)
3051
val targetPackage = prefs.getString("target_package", null)
3152

3253
if (targetPackage == null) {
33-
// If not set, go to selection screen
3454
val selectIntent = Intent(this, SettingsActivity::class.java)
35-
selectIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
3655
startActivity(selectIntent)
37-
finish()
3856
return
3957
}
4058

41-
// Create a new intent to the target browser
4259
val targetIntent = Intent(Intent.ACTION_VIEW, data)
4360
targetIntent.setPackage(targetPackage)
44-
targetIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
61+
// NO FLAG_ACTIVITY_NEW_TASK — launch browser within our task
4562

46-
// Copy all extras from the original intent
4763
val extras = incomingIntent.extras
4864
if (extras != null) {
4965
targetIntent.putExtras(extras)
5066
}
5167

5268
try {
5369
startActivity(targetIntent)
70+
hasRedirected = true
71+
// NO finish() — keep our activity in the back stack
72+
// so the task stays alive in Recents
5473
} catch (e: Exception) {
5574
Log.e("Redirector", "Failed to start target activity", e)
56-
val selectIntent = Intent(this, SettingsActivity::class.java)
57-
startActivity(selectIntent)
75+
// Browser might require NEW_TASK (singleTask browsers), try with it
76+
targetIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
77+
try {
78+
startActivity(targetIntent)
79+
hasRedirected = true
80+
} catch (e2: Exception) {
81+
Log.e("Redirector", "Fallback also failed", e2)
82+
val selectIntent = Intent(this, SettingsActivity::class.java)
83+
startActivity(selectIntent)
84+
}
5885
}
59-
} else {
60-
Log.w("Redirector", "No data or webapp_url found in intent")
61-
// If opened directly without data, show selection screen
86+
} else if (action == Intent.ACTION_MAIN) {
6287
val selectIntent = Intent(this, SettingsActivity::class.java)
6388
startActivity(selectIntent)
6489
}
65-
66-
finish()
90+
// NO finish() anywhere
6791
}
6892
}

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<resources>
2+
<style name="Theme.Transparent" parent="android:Theme.Translucent.NoTitleBar">
3+
<item name="android:windowIsTranslucent">true</item>
4+
<item name="android:windowBackground">@android:color/transparent</item>
5+
<item name="android:windowContentOverlay">@null</item>
6+
<item name="android:windowNoTitle">true</item>
7+
<item name="android:windowIsFloating">true</item>
8+
<item name="android:backgroundDimEnabled">false</item>
9+
</style>
10+
</resources>

0 commit comments

Comments
 (0)