Skip to content

Commit 05f5f52

Browse files
authored
[fix] drop FLAG_ACTIVITY_NEW_TASK when launching BugReportActivity from an Activity context (#252)
It seems Xiaomi/MIUI could silently block startActivity calls that pair FLAG_ACTIVITY_NEW_TASK with a transparent destination via its pop-up window gate, causing the "Create Report" button in DebugPanelDialog to do nothing when a draft already exists (#251). The flag was added for the FAB's overlay (non-Activity) context and was applied indiscriminately to the in-Activity caller; now it is only added when the caller context does not unwrap to an Activity. Not sure if this fixes the issue 100% as I don't own Xiaomi devices, but trying as this should be harmless for other devices.
1 parent a2b58b7 commit 05f5f52

4 files changed

Lines changed: 14 additions & 12 deletions

File tree

debugoverlay-core/src/main/kotlin/com/ms/square/debugoverlay/internal/OverlayViewManager.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ import com.ms.square.debugoverlay.internal.data.source.OverlayPreferences
3737
import com.ms.square.debugoverlay.internal.data.source.SharedPreferencesOverlayPreferences
3838
import com.ms.square.debugoverlay.internal.ui.DebugPanelActivity
3939
import com.ms.square.debugoverlay.internal.ui.DraggableOverlayPanel
40-
import com.ms.square.debugoverlay.internal.util.findActivity
40+
import com.ms.square.debugoverlay.internal.util.findActivityOrNull
4141
import com.ms.square.debugoverlay.internal.util.isDarkTheme
4242
import curtains.Curtains
4343
import curtains.OnRootViewsChangedListener
@@ -182,7 +182,7 @@ internal class OverlayViewManager(
182182
}
183183

184184
// Exclude windows belonging to DebugOverlay's own activities
185-
if (view.findActivity()?.isDebugOverlayActivity() == true) return@lastOrNull false
185+
if (view.findActivityOrNull()?.isDebugOverlayActivity() == true) return@lastOrNull false
186186

187187
view.windowToken != null && view.isShown
188188
}
@@ -388,7 +388,8 @@ internal class OverlayViewManager(
388388
DebugOverlay.overlayDataRepository.stopJankStatsTracking(activity)
389389
}
390390
}
391-
private fun isCurrentTarget(activity: Activity): Boolean = currentTargetWindowView?.findActivity() === activity
391+
private fun isCurrentTarget(activity: Activity): Boolean =
392+
currentTargetWindowView?.findActivityOrNull() === activity
392393
}
393394
}
394395

debugoverlay-core/src/main/kotlin/com/ms/square/debugoverlay/internal/bugreport/ui/BugReportActivity.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import com.ms.square.debugoverlay.internal.Logger
3333
import com.ms.square.debugoverlay.internal.bugreport.BugReportGenerator
3434
import com.ms.square.debugoverlay.internal.bugreport.model.BugReportResult
3535
import com.ms.square.debugoverlay.internal.bugreport.model.UserInput
36+
import com.ms.square.debugoverlay.internal.util.findActivityOrNull
3637
import com.ms.square.debugoverlay.internal.util.isDarkTheme
3738
import com.ms.square.debugoverlay.internal.util.runCatchingNonCancellation
3839
import com.ms.square.debugoverlay.model.ExportResult
@@ -331,15 +332,15 @@ internal class BugReportActivity : ComponentActivity() {
331332
companion object {
332333
fun launchWithDraftPicker(context: Context) {
333334
val intent = Intent(context, BugReportActivity::class.java).apply {
334-
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
335+
if (context.findActivityOrNull() == null) addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
335336
putExtra(INTENT_EXTRA_SHOW_DRAFT_PICKER, true)
336337
}
337338
context.startActivity(intent)
338339
}
339340

340341
fun launchWithMetadataDialog(context: Context, bugCapturedFolderPath: String) {
341342
val intent = Intent(context, BugReportActivity::class.java).apply {
342-
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
343+
if (context.findActivityOrNull() == null) addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
343344
putExtra(INTENT_EXTRA_CAPTURE_FOLDER, bugCapturedFolderPath)
344345
}
345346
context.startActivity(intent)

debugoverlay-core/src/main/kotlin/com/ms/square/debugoverlay/internal/util/Contexts.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ internal fun Context.defaultDisplay(): Display? {
1111
return dm?.getDisplay(Display.DEFAULT_DISPLAY)
1212
}
1313

14-
internal tailrec fun Context.findActivity(): Activity = when (this) {
14+
internal tailrec fun Context.findActivityOrNull(): Activity? = when (this) {
1515
is Activity -> this
16-
is ContextWrapper -> this.baseContext.findActivity()
17-
else -> throw IllegalArgumentException("Could not find activity!")
16+
is ContextWrapper -> this.baseContext.findActivityOrNull()
17+
else -> null
1818
}

debugoverlay-core/src/main/kotlin/com/ms/square/debugoverlay/internal/util/Views.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import android.app.Activity
44
import android.view.View
55
import curtains.phoneWindow
66

7-
internal fun View.findActivity(): Activity? {
7+
internal fun View.findActivityOrNull(): Activity? {
88
val callback = phoneWindow?.callback
9-
// check for the callback first as context.findActivity() won't work for certain contexts such as DecorContext.
10-
// for dialogs, callback could be non-Activity (ex..DialogWrapper), so fallback to the findActivity for those cases.
11-
return callback as? Activity ?: runCatching { context.findActivity() }.getOrNull()
9+
// check for the callback first as context unwrapping won't work for certain contexts such as DecorContext.
10+
// for dialogs, callback could be non-Activity (ex..DialogWrapper), so fallback to unwrapping the context.
11+
return callback as? Activity ?: context.findActivityOrNull()
1212
}

0 commit comments

Comments
 (0)