165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane/*
265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * Copyright (C) 2014 The Android Open Source Project
365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane *
465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * Licensed under the Apache License, Version 2.0 (the "License");
565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * you may not use this file except in compliance with the License.
665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * You may obtain a copy of the License at
765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane *
865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane *      http://www.apache.org/licenses/LICENSE-2.0
965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane *
1065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * Unless required by applicable law or agreed to in writing, software
1165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * distributed under the License is distributed on an "AS IS" BASIS,
1265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * See the License for the specific language governing permissions and
1465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * limitations under the License.
1565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane */
1665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
1765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lanepackage com.android.tv.settings.users;
1865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
1965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.content.BroadcastReceiver;
2065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.content.Context;
2165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.content.Intent;
2265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.content.IntentFilter;
2365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.content.pm.ApplicationInfo;
2465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.content.pm.IPackageManager;
2565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.content.pm.PackageManager;
2665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.net.Uri;
2765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.os.Bundle;
2865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.os.RemoteException;
2965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.os.ServiceManager;
3065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.os.UserHandle;
3165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.os.UserManager;
3265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.util.Log;
3365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
34944e793b0520642950bd72c636d51fd72dee6517Tony Mantlerimport com.android.tv.settings.R;
35944e793b0520642950bd72c636d51fd72dee6517Tony Mantlerimport com.android.tv.settings.dialog.DialogFragment;
36944e793b0520642950bd72c636d51fd72dee6517Tony Mantlerimport com.android.tv.settings.dialog.DialogFragment.Action;
37944e793b0520642950bd72c636d51fd72dee6517Tony Mantler
3865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport java.util.ArrayList;
3965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport java.util.HashMap;
4065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport java.util.Map;
4165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
4265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane/**
4365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * DialogFragment that configures the app restrictions for a given user.
4465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane */
4565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lanepublic class UserAppRestrictionsDialogFragment extends DialogFragment implements Action.Listener,
4665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        AppLoadingTask.Listener {
4765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
4865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private static final boolean DEBUG = false;
4965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private static final String TAG = "RestrictedProfile";
5065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
5165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    /** Key for extra passed in from calling fragment for the userId of the user being edited */
5265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    public static final String EXTRA_USER_ID = "user_id";
5365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
5465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    /** Key for extra passed in from calling fragment to indicate if this is a newly created user */
5565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    public static final String EXTRA_NEW_USER = "new_user";
5665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
5765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private static final String EXTRA_CONTENT_TITLE = "title";
5865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private static final String EXTRA_CONTENT_BREADCRUMB = "breadcrumb";
5965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private static final String EXTRA_CONTENT_DESCRIPTION = "description";
6065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private static final String EXTRA_CONTENT_ICON_RESOURCE_ID = "iconResourceId";
6165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private static final String EXTRA_CONTENT_ICON_URI = "iconUri";
6265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private static final String EXTRA_CONTENT_ICON_BITMAP = "iconBitmap";
6365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private static final String EXTRA_CONTENT_ICON_BACKGROUND = "iconBackground";
6465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
6565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private static final String EXTRA_PACKAGE_NAME = "packageName";
6665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private static final String EXTRA_CAN_CONFIGURE_RESTRICTIONS = "canConfigureRestrictions";
676e995161147d9110d77ae1fe38b697e52891d3f2Tony Mantler    private static final String EXTRA_CAN_SEE_RESTRICTED_ACCOUNTS = "canSeeRestrictedAccounts";
6865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private static final String EXTRA_IS_ALLOWED = "isAllowed";
6965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private static final String EXTRA_CAN_BE_ENABLED_DISABLED = "canBeEnabledDisabled";
7065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private static final String EXTRA_CONTROLLING_APP = "controllingApp";
7165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
7265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private static final String ACTION_ALLOW = "allow";
7365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private static final String ACTION_DISALLOW = "disallow";
7465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private static final String ACTION_CONFIGURE = "configure";
7565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private static final String ACTION_CUSTOMIZE_RESTRICTIONS = "customizeRestriction";
7665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
7765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private static final int CHECK_SET_ID = 1;
7865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
7965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    public static UserAppRestrictionsDialogFragment newInstance(Context context, int userId,
8065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            boolean newUser) {
8165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        UserAppRestrictionsDialogFragment fragment = new UserAppRestrictionsDialogFragment();
8265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        Bundle args = new Bundle();
8365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        args.putString(EXTRA_CONTENT_TITLE,
8465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                context.getString(R.string.restricted_profile_configure_apps_title));
8565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        args.putInt(EXTRA_CONTENT_ICON_RESOURCE_ID, R.drawable.ic_settings_launcher_icon);
8665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        args.putInt(EXTRA_CONTENT_ICON_BACKGROUND,
8765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                context.getResources().getColor(R.color.icon_background));
8865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        args.putInt(EXTRA_USER_ID, userId);
8965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        args.putBoolean(EXTRA_NEW_USER, newUser);
9065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        fragment.setArguments(args);
9165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        return fragment;
9265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
9365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
9465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private UserManager mUserManager;
9565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private IPackageManager mIPm;
9665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private AppLoadingTask mAppLoadingTask;
976e995161147d9110d77ae1fe38b697e52891d3f2Tony Mantler    private final HashMap<String, Boolean> mSelectedPackages = new HashMap<>();
9865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private UserHandle mUser;
9965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private boolean mNewUser;
10065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private boolean mAppListChanged;
10165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private AppRestrictionsManager mAppRestrictionsManager;
10265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
1036e995161147d9110d77ae1fe38b697e52891d3f2Tony Mantler    private final BroadcastReceiver mUserBackgrounding = new BroadcastReceiver() {
10465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        @Override
10565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        public void onReceive(Context context, Intent intent) {
10665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            // Update the user's app selection right away without waiting for a pause
10765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            // onPause() might come in too late, causing apps to disappear after broadcasts
10865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            // have been scheduled during user startup.
10965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            if (mAppListChanged) {
11065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                if (DEBUG) {
11165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    Log.d(TAG, "User backgrounding, update app list");
11265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                }
11365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                applyUserAppsStates(mSelectedPackages, getActions(), mIPm, mUser.getIdentifier());
11465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                if (DEBUG) {
11565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    Log.d(TAG, "User backgrounding, done updating app list");
11665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                }
11765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            }
11865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        }
11965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    };
1206e995161147d9110d77ae1fe38b697e52891d3f2Tony Mantler    private final BroadcastReceiver mPackageObserver = new BroadcastReceiver() {
12165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        @Override
12265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        public void onReceive(Context context, Intent intent) {
12365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            onPackageChanged(intent);
12465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        }
12565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
12665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        private void onPackageChanged(Intent intent) {
12765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            String action = intent.getAction();
12865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            String packageName = intent.getData().getSchemeSpecificPart();
12965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            // Package added, check if the preference needs to be enabled
13065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            ArrayList<Action> matchingActions = findActionsWithPackageName(getActions(),
13165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    packageName);
13265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            for (Action matchingAction : matchingActions) {
13365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                if ((Intent.ACTION_PACKAGE_ADDED.equals(action) && matchingAction.isChecked()) || (
13465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                        Intent.ACTION_PACKAGE_REMOVED.equals(action)
13565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                        && !matchingAction.isChecked())) {
13665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    matchingAction.setEnabled(true);
13765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                }
13865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            }
13965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        }
14065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    };
14165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
14265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    @Override
14365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    public void onCreate(Bundle icicle) {
14465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        super.onCreate(icicle);
14565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        mUserManager = (UserManager) getActivity().getSystemService(Context.USER_SERVICE);
14665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        setActions(new ArrayList<Action>());
14765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        setListener(this);
14865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        if (icicle != null) {
14965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            mUser = new UserHandle(icicle.getInt(EXTRA_USER_ID));
15065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        } else {
15165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            Bundle args = getArguments();
15265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            if (args != null) {
15365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                if (args.containsKey(EXTRA_USER_ID)) {
15465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    mUser = new UserHandle(args.getInt(EXTRA_USER_ID));
15565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                }
15665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                mNewUser = args.getBoolean(EXTRA_NEW_USER, false);
15765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            }
15865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        }
15965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
16065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        if (mUser == null) {
16165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            mUser = android.os.Process.myUserHandle();
16265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        }
16365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
16465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        mIPm = IPackageManager.Stub.asInterface(ServiceManager.getService("package"));
16565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
16665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
16765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    @Override
16865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    public void onResume() {
16965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        super.onResume();
17065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
17165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        getActivity().registerReceiver(mUserBackgrounding,
17265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                new IntentFilter(Intent.ACTION_USER_BACKGROUND));
17365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        IntentFilter packageFilter = new IntentFilter();
17465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        packageFilter.addAction(Intent.ACTION_PACKAGE_ADDED);
17565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        packageFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);
17665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        packageFilter.addDataScheme("package");
17765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        getActivity().registerReceiver(mPackageObserver, packageFilter);
17865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
17965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        if (mAppLoadingTask == null) {
18065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            mAppListChanged = false;
18165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            mAppLoadingTask = new AppLoadingTask(getActivity(), mUser.getIdentifier(), mNewUser,
18265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    mIPm, this);
18365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            mAppLoadingTask.execute((Void[]) null);
18465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        }
18565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
18665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
18765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    @Override
18865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    public void onPause() {
18965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        super.onPause();
19065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        mNewUser = false;
19165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        getActivity().unregisterReceiver(mUserBackgrounding);
19265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        getActivity().unregisterReceiver(mPackageObserver);
19365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        if (mAppListChanged) {
19465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            new Thread() {
19565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                public void run() {
19665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    applyUserAppsStates(mSelectedPackages, getActions(), mIPm,
19765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                            mUser.getIdentifier());
19865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                }
19965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            }.start();
20065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        }
20165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
20265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
20365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    @Override
20465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    public void onActionClicked(Action action) {
20565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        String packageName = action.getIntent().getStringExtra(EXTRA_PACKAGE_NAME);
20665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        if (ACTION_CONFIGURE.equals(action.getKey())) {
20765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            boolean isAllowed = action.getIntent().getBooleanExtra(EXTRA_IS_ALLOWED, false);
20865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            boolean canBeEnabledDisabled = action.getIntent().getBooleanExtra(
20965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    EXTRA_CAN_BE_ENABLED_DISABLED, false);
21065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            String controllingActivity = action.getIntent().getStringExtra(EXTRA_CONTROLLING_APP);
2116e995161147d9110d77ae1fe38b697e52891d3f2Tony Mantler            final ArrayList<Action> initialAllowDisallowActions = new ArrayList<>();
21265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            if (controllingActivity != null) {
21365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                initialAllowDisallowActions.add(new Action.Builder()
21465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                        .title(getString(isAllowed ? R.string.restricted_profile_allowed
21565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                                : R.string.restricted_profile_not_allowed))
21665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                        .description(getString(R.string.user_restrictions_controlled_by,
21765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                                controllingActivity))
21865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                        .checked(isAllowed)
21965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                        .infoOnly(true)
22065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                        .build());
22165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            } else if (!canBeEnabledDisabled) {
22265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                initialAllowDisallowActions.add(new Action.Builder()
22365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                        .title(getString(isAllowed ? R.string.restricted_profile_allowed
22465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                                : R.string.restricted_profile_not_allowed))
22565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                        .checked(isAllowed)
22665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                        .infoOnly(true)
22765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                        .build());
22865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            } else {
22965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                boolean canSeeRestrictedAccounts = action.getIntent().getBooleanExtra(
23065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                        EXTRA_CAN_SEE_RESTRICTED_ACCOUNTS, true);
23165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                initialAllowDisallowActions.add(new Action.Builder().key(ACTION_DISALLOW)
23265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                        .title(getString(R.string.restricted_profile_not_allowed))
23365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                        .intent(action.getIntent())
23465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                        .checked(!isAllowed)
23565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                        .checkSetId(CHECK_SET_ID).build());
23665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                initialAllowDisallowActions.add(new Action.Builder().key(ACTION_ALLOW)
23765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                        .title(getString(R.string.restricted_profile_allowed))
23865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                        .description(canSeeRestrictedAccounts ? getString(
23965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                                R.string.app_sees_restricted_accounts)
24065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                                : null)
24165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                        .intent(action.getIntent())
24265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                        .checkSetId(CHECK_SET_ID)
24365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                        .checked(isAllowed)
24465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                        .build());
24565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            }
24665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
24765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            final DialogFragment dialogFragment = new DialogFragment.Builder()
24865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    .title(action.getTitle()).iconUri(action.getIconUri())
24965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    .actions(initialAllowDisallowActions).build();
25065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
25165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            boolean canConfigureRestrictions = action.getIntent().getBooleanExtra(
25265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    EXTRA_CAN_CONFIGURE_RESTRICTIONS, false);
25365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            if (canConfigureRestrictions) {
25465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                mAppRestrictionsManager = new AppRestrictionsManager(this, mUser,
25565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                        mUserManager, packageName, new AppRestrictionsManager.Listener() {
25665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                            @Override
25765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                            public void onRestrictionActionsLoaded(String packageName,
25865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                                    ArrayList<Action> restrictionActions) {
25965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                                ArrayList<Action> oldActions = dialogFragment.getActions();
2606e995161147d9110d77ae1fe38b697e52891d3f2Tony Mantler                                ArrayList<Action> newActions = new ArrayList<>();
26165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                                if (oldActions != null && oldActions.size()
26265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                                        >= initialAllowDisallowActions.size()) {
26365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                                    for (int i = 0, size = initialAllowDisallowActions.size();
26465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                                            i < size; i++) {
26565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                                        newActions.add(oldActions.get(i));
26665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                                    }
26765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                                } else {
26865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                                    newActions.addAll(initialAllowDisallowActions);
26965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                                }
27065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                                newActions.addAll(restrictionActions);
27165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                                dialogFragment.setActions(newActions);
27265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                            }
27365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                        });
27465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                mAppRestrictionsManager.loadRestrictionActions();
27565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            }
27665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
27765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            dialogFragment.setListener(this);
27865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            DialogFragment.add(getFragmentManager(), dialogFragment);
27965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        } else if (ACTION_ALLOW.equals(action.getKey())) {
28065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            setEnabled(packageName, true);
2814b7b881c8bbf2b75ad75604fd70314b144bb9833Andrew Wilson            getFragmentManager().popBackStack();
28265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        } else if (ACTION_DISALLOW.equals(action.getKey())) {
28365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            setEnabled(packageName, false);
2844b7b881c8bbf2b75ad75604fd70314b144bb9833Andrew Wilson            getFragmentManager().popBackStack();
28565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        } else if (mAppRestrictionsManager != null) {
28665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            mAppRestrictionsManager.onActionClicked(action);
28765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        }
28865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
28965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
29065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    @Override
29165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    public void onActivityResult(int requestCode, int resultCode, Intent data) {
29265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        if (mAppRestrictionsManager != null) {
29365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            mAppRestrictionsManager.onActivityResult(requestCode, resultCode, data);
29465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        }
29565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
29665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
29765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private void setEnabled(String packageName, boolean enabled) {
29865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        onPackageEnableChanged(packageName, enabled);
29965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        for (Action action : getActions()) {
30065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            String actionPackageName = action.getIntent().getStringExtra(EXTRA_PACKAGE_NAME);
30165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            if (actionPackageName.equals(packageName)) {
30265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                action.setChecked(enabled);
30365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                action.setDescription(getString(enabled ? R.string.restricted_profile_allowed
30465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                        : R.string.restricted_profile_not_allowed));
30565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                action.getIntent().putExtra(EXTRA_IS_ALLOWED, enabled);
30665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            }
30765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        }
30865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        onActionsLoaded(getActions());
30965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
31065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
31165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    @Override
31265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    public void onPackageEnableChanged(String packageName, boolean enabled) {
31365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        mSelectedPackages.put(packageName, enabled);
31465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        mAppListChanged = true;
31565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        if (getActivity() instanceof AppLoadingTask.Listener) {
31665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            ((AppLoadingTask.Listener) getActivity()).onPackageEnableChanged(packageName, enabled);
31765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        }
31865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
31965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
32065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    @Override
32165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    public void onActionsLoaded(ArrayList<Action> actions) {
32265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        setActions(actions);
32365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        if (getActivity() instanceof AppLoadingTask.Listener) {
32465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            ((AppLoadingTask.Listener) getActivity()).onActionsLoaded(actions);
32565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        }
32665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
32765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
32865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    static Action createAction(Context context, String packageName, String title, Uri iconUri,
32965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            boolean canBeEnabledDisabled, boolean isAllowed, boolean hasCustomizableRestrictions,
33065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            boolean canSeeRestrictedAccounts, boolean availableForRestrictedProfile,
33165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            String controllingActivity) {
33265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        String description = context.getString(
33365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                availableForRestrictedProfile ? isAllowed ? R.string.restricted_profile_allowed
33465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                        : R.string.restricted_profile_not_allowed
33565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                        : R.string.app_not_supported_in_limited);
33665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
33765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        Intent intent = new Intent().putExtra(EXTRA_IS_ALLOWED, isAllowed)
33865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                .putExtra(EXTRA_CAN_CONFIGURE_RESTRICTIONS,
33965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                        (hasCustomizableRestrictions && (controllingActivity == null)))
34065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                .putExtra(EXTRA_CAN_BE_ENABLED_DISABLED, canBeEnabledDisabled)
34165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                .putExtra(EXTRA_PACKAGE_NAME, packageName)
34265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                .putExtra(EXTRA_CONTROLLING_APP, controllingActivity)
34365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                .putExtra(EXTRA_CAN_SEE_RESTRICTED_ACCOUNTS, canSeeRestrictedAccounts);
34465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
34565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        if (DEBUG) {
34665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            Log.d(TAG, "Icon uri: " + (iconUri != null ? iconUri.toString() : "null"));
34765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        }
34865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        return new Action.Builder()
34965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                .key(ACTION_CONFIGURE)
35065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                .title(title)
35165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                .description(description)
35265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                .enabled(availableForRestrictedProfile)
35365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                .iconUri(iconUri)
35465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                .checked(isAllowed)
35565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                .intent(intent)
35665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                .build();
35765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
35865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
35965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    static void applyUserAppsStates(HashMap<String, Boolean> selectedPackages,
36065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            ArrayList<Action> actions, IPackageManager ipm, int userId) {
36165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        for (Map.Entry<String, Boolean> entry : selectedPackages.entrySet()) {
36265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            String packageName = entry.getKey();
36365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            boolean enabled = entry.getValue();
36465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            if (applyUserAppState(ipm, userId, packageName, enabled)) {
36565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                disableActionForPackage(actions, packageName);
36665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            }
36765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        }
36865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
36965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
37065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    static boolean applyUserAppState(IPackageManager ipm, int userId, String packageName,
37165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            boolean enabled) {
37265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        boolean disableActionForPackage = false;
37365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        if (enabled) {
37465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            // Enable selected apps
37565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            try {
37665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                ApplicationInfo info = ipm.getApplicationInfo(packageName,
37765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                        PackageManager.GET_UNINSTALLED_PACKAGES, userId);
3786e995161147d9110d77ae1fe38b697e52891d3f2Tony Mantler                if (info == null || !info.enabled
37965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                        || (info.flags & ApplicationInfo.FLAG_INSTALLED) == 0) {
38065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    ipm.installExistingPackageAsUser(packageName, userId);
38165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    if (DEBUG) {
38265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                        Log.d(TAG, "Installing " + packageName);
38365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    }
38465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                }
385c554922d6f7786efd6514d2332754d0e963f0f4dAlex Klyubin                if (info != null && (info.privateFlags & ApplicationInfo.PRIVATE_FLAG_HIDDEN) != 0
38665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                        && (info.flags & ApplicationInfo.FLAG_INSTALLED) != 0) {
38765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    disableActionForPackage = true;
38865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    ipm.setApplicationHiddenSettingAsUser(packageName, false, userId);
38965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    if (DEBUG) {
39065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                        Log.d(TAG, "Unhiding " + packageName);
39165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    }
39265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                }
39365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            } catch (RemoteException re) {
39465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                Log.e(TAG, "Caught exception while installing " + packageName + "!", re);
39565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            }
39665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        } else {
39765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            // Blacklist all other apps, system or downloaded
39865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            try {
39965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                ApplicationInfo info = ipm.getApplicationInfo(packageName, 0, userId);
40065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                if (info != null) {
40165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    ipm.deletePackageAsUser(packageName, null, userId,
40265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                            PackageManager.DELETE_SYSTEM_APP);
40365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    if (DEBUG) {
40465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                        Log.d(TAG, "Uninstalling " + packageName);
40565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    }
40665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                }
40765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            } catch (RemoteException re) {
40865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                Log.e(TAG, "Caught exception while uninstalling " + packageName + "!", re);
40965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            }
41065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        }
41165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        return disableActionForPackage;
41265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
41365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
41465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private static void disableActionForPackage(ArrayList<Action> actions, String packageName) {
41565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        ArrayList<Action> matchingActions = findActionsWithPackageName(actions, packageName);
41665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        for(Action matchingAction : matchingActions) {
41765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            matchingAction.setEnabled(false);
41865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        }
41965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
42065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
42165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private static ArrayList<Action> findActionsWithPackageName(ArrayList<Action> actions,
42265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            String packageName) {
4236e995161147d9110d77ae1fe38b697e52891d3f2Tony Mantler        ArrayList<Action> matchingActions = new ArrayList<>();
42465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        if (packageName != null) {
42565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            for (Action action : actions) {
42665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                if (packageName.equals(action.getIntent().getStringExtra(EXTRA_PACKAGE_NAME))) {
42765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    matchingActions.add(action);
42865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                }
42965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            }
43065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        }
43165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        return matchingActions;
43265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
43365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane}
434