Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit b4c3817a authored by Chaohui Wang's avatar Chaohui Wang
Browse files

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
parent ebebc5e1
Loading
Loading
Loading
Loading
+51 −19
Original line number Diff line number Diff line
@@ -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,34 +101,49 @@ 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);
        }
        startAppPicker();
        if (icicle != null) {
            mShouldStartAppPickerOnResume = false;
            mSelectedApp = icicle.getString(COMPAT_APP);
        }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        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) {
            mShouldStartAppPickerOnResume = false;
            switch (resultCode) {
                case Activity.RESULT_OK:
                    mSelectedApp = data.getAction();
                try {
                    final ApplicationInfo applicationInfo = getApplicationInfo();
                    addPreferences(applicationInfo);
                } catch (PackageManager.NameNotFoundException e) {
                    startAppPicker();
                    break;
                case Activity.RESULT_CANCELED:
                    if (TextUtils.isEmpty(mSelectedApp)) {
                        finish();
                    }
            } else if (resultCode == AppPicker.RESULT_NO_MATCHING_APPS) {
                    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)
@@ -133,10 +151,24 @@ public class PlatformCompatDashboard extends DashboardFragment {
                        .setOnDismissListener(dialog -> finish())
                        .setCancelable(false)
                        .show();
                return;
            }
            try {
                final ApplicationInfo applicationInfo = getApplicationInfo();
                addPreferences(applicationInfo);
                return;
            } catch (PackageManager.NameNotFoundException e) {
                mShouldStartAppPickerOnResume = true;
                mSelectedApp = null;
            }
        super.onActivityResult(requestCode, resultCode, data);
        }
        startAppPicker();
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putString(COMPAT_APP, mSelectedApp);
    }

    private void addPreferences(ApplicationInfo applicationInfo) {