From f631ef769ad718f9ce52955d67623fa67a942cd5 Mon Sep 17 00:00:00 2001 From: Jorim Jaggi Date: Fri, 24 Feb 2017 13:49:47 +0100 Subject: [PATCH] Fix sharing bugreports from lockscreen When launching ChooserActivity from lockscreen, we will start it in stopped state because lockscreen is still showing, meaning that the activity goes through start -> resume -> pause -> stop immediately after it was launched, and will be later resumed once Keyguard actually goes away. However, ResolverActivity finished itself in onStop. We add a private extra to change this behavior for sharing bugreports. Test: Take bugreport, double tap on it on lockscreen Test: com.android.shell.BugreportReceiverTest$1 Bug: 33009364 Change-Id: I973b2c71587950499b7c88b16af9cf1387795e17 --- .../com/android/internal/app/ChooserActivity.java | 9 +++++++++ .../com/android/internal/app/ResolverActivity.java | 14 +++++++++++++- .../android/shell/BugreportProgressService.java | 10 ++++++++-- 3 files changed, 30 insertions(+), 3 deletions(-) diff --git a/core/java/com/android/internal/app/ChooserActivity.java b/core/java/com/android/internal/app/ChooserActivity.java index 84c8f7a22962c..df65659dface3 100644 --- a/core/java/com/android/internal/app/ChooserActivity.java +++ b/core/java/com/android/internal/app/ChooserActivity.java @@ -84,6 +84,14 @@ import java.util.List; public class ChooserActivity extends ResolverActivity { private static final String TAG = "ChooserActivity"; + /** + * Boolean extra to change the following behavior: Normally, ChooserActivity finishes itself + * in onStop when launched in a new task. If this extra is set to true, we do not finish + * ourselves when onStop gets called. + */ + public static final String EXTRA_PRIVATE_RETAIN_IN_ON_STOP + = "com.android.internal.app.ChooserActivity.EXTRA_PRIVATE_RETAIN_IN_ON_STOP"; + private static final boolean DEBUG = false; private static final int QUERY_TARGET_SERVICE_LIMIT = 5; @@ -260,6 +268,7 @@ public class ChooserActivity extends ResolverActivity { } mPinnedSharedPrefs = getPinnedSharedPrefs(this); + setRetainInOnStop(intent.getBooleanExtra(EXTRA_PRIVATE_RETAIN_IN_ON_STOP, false)); super.onCreate(savedInstanceState, target, title, defaultTitleRes, initialIntents, null, false); diff --git a/core/java/com/android/internal/app/ResolverActivity.java b/core/java/com/android/internal/app/ResolverActivity.java index 0b1f0aadf55a1..9fb9cb6cb664e 100644 --- a/core/java/com/android/internal/app/ResolverActivity.java +++ b/core/java/com/android/internal/app/ResolverActivity.java @@ -116,6 +116,10 @@ public class ResolverActivity extends Activity { private Runnable mPostListReadyRunnable; private boolean mRegistered; + + /** See {@link #setRetainInOnStop}. */ + private boolean mRetainInOnStop; + private final PackageMonitor mPackageMonitor = new PackageMonitor() { @Override public void onSomePackagesChanged() { mAdapter.handlePackagesChanged(); @@ -502,7 +506,7 @@ public class ResolverActivity extends Activity { } final Intent intent = getIntent(); if ((intent.getFlags() & FLAG_ACTIVITY_NEW_TASK) != 0 && !isVoiceInteraction() - && !mResolvingHome) { + && !mResolvingHome && !mRetainInOnStop) { // This resolver is in the unusual situation where it has been // launched at the top of a new task. We don't let it be added // to the recent tasks shown to the user, and we need to make sure @@ -1028,6 +1032,14 @@ public class ResolverActivity extends Activity { return mSupportsAlwaysUseOption && mAdapter.hasFilteredItem(); } + /** + * If {@code retainInOnStop} is set to true, we will not finish ourselves when onStop gets + * called and we are launched in a new task. + */ + protected void setRetainInOnStop(boolean retainInOnStop) { + mRetainInOnStop = retainInOnStop; + } + /** * Check a simple match for the component of two ResolveInfos. */ diff --git a/packages/Shell/src/com/android/shell/BugreportProgressService.java b/packages/Shell/src/com/android/shell/BugreportProgressService.java index 12d0c0306e610..1df626ff9a2f4 100644 --- a/packages/Shell/src/com/android/shell/BugreportProgressService.java +++ b/packages/Shell/src/com/android/shell/BugreportProgressService.java @@ -44,6 +44,7 @@ import java.util.zip.ZipOutputStream; import libcore.io.Streams; import com.android.internal.annotations.VisibleForTesting; +import com.android.internal.app.ChooserActivity; import com.android.internal.logging.MetricsLogger; import com.android.internal.logging.nano.MetricsProto.MetricsEvent; import com.android.internal.util.FastPrintWriter; @@ -943,8 +944,13 @@ public class BugreportProgressService extends Service { } static void sendShareIntent(Context context, Intent intent) { - context.startActivity(Intent.createChooser(intent, - context.getResources().getText(R.string.bugreport_intent_chooser_title))); + final Intent chooserIntent = Intent.createChooser(intent, + context.getResources().getText(R.string.bugreport_intent_chooser_title)); + + // Since we may be launched behind lockscreen, make sure that ChooserActivity doesn't finish + // itself in onStop. + chooserIntent.putExtra(ChooserActivity.EXTRA_PRIVATE_RETAIN_IN_ON_STOP, true); + context.startActivity(chooserIntent); } /**