From b4c3817a6529377999092cb8cdf84e4ab54de711 Mon Sep 17 00:00:00 2001 From: Chaohui Wang Date: Mon, 7 Mar 2022 06:57:17 +0000 Subject: [PATCH] Fix App Compatibility Settings' lifecycle When "Don't keep activities" enabled, it used to be unusable. Bug: 222226636 Test: On Pixel device Change-Id: I11718bea90cecfb7ee455fba9059bb09ffb72016 --- .../compat/PlatformCompatDashboard.java | 86 +++++++++++++------ 1 file changed, 59 insertions(+), 27 deletions(-) diff --git a/src/com/android/settings/development/compat/PlatformCompatDashboard.java b/src/com/android/settings/development/compat/PlatformCompatDashboard.java index c617e179496..f8cbf21b36b 100644 --- a/src/com/android/settings/development/compat/PlatformCompatDashboard.java +++ b/src/com/android/settings/development/compat/PlatformCompatDashboard.java @@ -31,6 +31,7 @@ import android.graphics.drawable.Drawable; import android.os.Bundle; import android.os.RemoteException; import android.os.ServiceManager; +import android.text.TextUtils; import android.util.ArraySet; import androidx.annotation.VisibleForTesting; @@ -66,6 +67,8 @@ public class PlatformCompatDashboard extends DashboardFragment { private AndroidBuildClassifier mAndroidBuildClassifier = new AndroidBuildClassifier(); + private boolean mShouldStartAppPickerOnResume = true; + @VisibleForTesting String mSelectedApp; @@ -98,13 +101,67 @@ public class PlatformCompatDashboard extends DashboardFragment { } @Override - public void onActivityCreated(Bundle savedInstanceState) { - super.onActivityCreated(savedInstanceState); + public void onCreate(Bundle icicle) { + super.onCreate(icicle); try { mChanges = getPlatformCompat().listUIChanges(); } catch (RemoteException e) { throw new RuntimeException("Could not list changes!", e); } + if (icicle != null) { + mShouldStartAppPickerOnResume = false; + mSelectedApp = icicle.getString(COMPAT_APP); + } + } + + @Override + public void onActivityResult(int requestCode, int resultCode, Intent data) { + if (requestCode == REQUEST_COMPAT_CHANGE_APP) { + mShouldStartAppPickerOnResume = false; + switch (resultCode) { + case Activity.RESULT_OK: + mSelectedApp = data.getAction(); + break; + case Activity.RESULT_CANCELED: + if (TextUtils.isEmpty(mSelectedApp)) { + finish(); + } + break; + case AppPicker.RESULT_NO_MATCHING_APPS: + mSelectedApp = null; + break; + } + return; + } + super.onActivityResult(requestCode, resultCode, data); + } + + @Override + public void onResume() { + super.onResume(); + if (isFinishingOrDestroyed()) { + return; + } + if (!mShouldStartAppPickerOnResume) { + if (TextUtils.isEmpty(mSelectedApp)) { + new AlertDialog.Builder(getContext()) + .setTitle(R.string.platform_compat_dialog_title_no_apps) + .setMessage(R.string.platform_compat_dialog_text_no_apps) + .setPositiveButton(R.string.okay, (dialog, which) -> finish()) + .setOnDismissListener(dialog -> finish()) + .setCancelable(false) + .show(); + return; + } + try { + final ApplicationInfo applicationInfo = getApplicationInfo(); + addPreferences(applicationInfo); + return; + } catch (PackageManager.NameNotFoundException e) { + mShouldStartAppPickerOnResume = true; + mSelectedApp = null; + } + } startAppPicker(); } @@ -114,31 +171,6 @@ public class PlatformCompatDashboard extends DashboardFragment { outState.putString(COMPAT_APP, mSelectedApp); } - @Override - public void onActivityResult(int requestCode, int resultCode, Intent data) { - if (requestCode == REQUEST_COMPAT_CHANGE_APP) { - if (resultCode == Activity.RESULT_OK) { - mSelectedApp = data.getAction(); - try { - final ApplicationInfo applicationInfo = getApplicationInfo(); - addPreferences(applicationInfo); - } catch (PackageManager.NameNotFoundException e) { - startAppPicker(); - } - } else if (resultCode == AppPicker.RESULT_NO_MATCHING_APPS) { - new AlertDialog.Builder(getContext()) - .setTitle(R.string.platform_compat_dialog_title_no_apps) - .setMessage(R.string.platform_compat_dialog_text_no_apps) - .setPositiveButton(R.string.okay, (dialog, which) -> finish()) - .setOnDismissListener(dialog -> finish()) - .setCancelable(false) - .show(); - } - return; - } - super.onActivityResult(requestCode, resultCode, data); - } - private void addPreferences(ApplicationInfo applicationInfo) { getPreferenceScreen().removeAll(); getPreferenceScreen().addPreference(createAppPreference(applicationInfo));