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

Commit ee0931f5 authored by Yanting Yang's avatar Yanting Yang
Browse files

Fix reset app preferences for work profile apps

In the past, Settings didn't support resetting the disable status of
work profile apps, it doesn’t follow the behavior that the reset dialog
mentioned.

To fix it, we are going to check apps by enabling user profiles and user
id. If the work profile is enabled, we will also reset them when users
select to reset app preferences.

Bug: 187387211
Test: manual
Change-Id: I25fd0129335539a23ed2ee0af57fdd9714eddf74
parent 286682ec
Loading
Loading
Loading
Loading
+31 −18
Original line number Diff line number Diff line
@@ -32,6 +32,8 @@ import android.os.Bundle;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.os.UserHandle;
import android.os.UserManager;
import android.util.Log;

import androidx.appcompat.app.AlertDialog;

@@ -44,6 +46,7 @@ public class ResetAppsHelper implements DialogInterface.OnClickListener,
        DialogInterface.OnDismissListener {

    private static final String EXTRA_RESET_DIALOG = "resetDialog";
    private static final String TAG = "ResetAppsHelper";

    private final PackageManager mPm;
    private final IPackageManager mIPm;
@@ -51,6 +54,7 @@ public class ResetAppsHelper implements DialogInterface.OnClickListener,
    private final NetworkPolicyManager mNpm;
    private final AppOpsManager mAom;
    private final Context mContext;
    private final UserManager mUm;

    private AlertDialog mResetDialog;

@@ -62,6 +66,7 @@ public class ResetAppsHelper implements DialogInterface.OnClickListener,
                ServiceManager.getService(Context.NOTIFICATION_SERVICE));
        mNpm = NetworkPolicyManager.from(context);
        mAom = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
        mUm = (UserManager) context.getSystemService(Context.USER_SERVICE);
    }

    public void onRestoreInstanceState(Bundle savedInstanceState) {
@@ -110,12 +115,13 @@ public class ResetAppsHelper implements DialogInterface.OnClickListener,
        AsyncTask.execute(new Runnable() {
            @Override
            public void run() {
                final List<ApplicationInfo> apps = mPm.getInstalledApplications(
                        PackageManager.GET_DISABLED_COMPONENTS);
                final List<String> allowList = Arrays.asList(
                        mContext.getResources().getStringArray(
                                R.array.config_skip_reset_apps_package_name));

                for (UserHandle userHandle : mUm.getEnabledProfiles()) {
                    final int userId = userHandle.getIdentifier();
                    final List<ApplicationInfo> apps = mPm.getInstalledApplicationsAsUser(
                            PackageManager.GET_DISABLED_COMPONENTS, userId);
                    for (int i = 0; i < apps.size(); i++) {
                        ApplicationInfo app = apps.get(i);
                        if (allowList.contains(app.packageName)) {
@@ -126,11 +132,18 @@ public class ResetAppsHelper implements DialogInterface.OnClickListener,
                        } catch (android.os.RemoteException ex) {
                        }
                        if (!app.enabled) {
                        if (mPm.getApplicationEnabledSetting(app.packageName)
                            try {
                                if (mIPm.getApplicationEnabledSetting(app.packageName, userId)
                                        == PackageManager.COMPONENT_ENABLED_STATE_DISABLED_USER) {
                            mPm.setApplicationEnabledSetting(app.packageName,
                                    mIPm.setApplicationEnabledSetting(app.packageName,
                                            PackageManager.COMPONENT_ENABLED_STATE_DEFAULT,
                                    PackageManager.DONT_KILL_APP);
                                            PackageManager.DONT_KILL_APP,
                                            userId,
                                            mContext.getPackageName());
                                }
                            } catch (RemoteException e) {
                                Log.e(TAG, "Error during reset disabled apps.", e);
                            }
                        }
                    }
                }