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 com.android.tv.settings.R;
2065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport com.android.tv.settings.dialog.DialogFragment;
2165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport com.android.tv.settings.dialog.DialogFragment.Action;
2265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
2365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.accounts.Account;
2465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.accounts.AccountManager;
2565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.app.Activity;
2665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.app.ActivityManagerNative;
2765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.app.Fragment;
2865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.app.admin.DevicePolicyManager;
2965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.content.Context;
3065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.content.Intent;
3165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.content.IntentFilter;
3265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.content.pm.IPackageManager;
3365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.content.pm.UserInfo;
3465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.graphics.Bitmap;
3565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.graphics.Canvas;
3665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.graphics.drawable.Drawable;
3765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.os.AsyncTask;
3865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.os.Bundle;
3965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.os.Handler;
4065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.os.Message;
4165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.os.RemoteException;
4265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.os.ServiceManager;
4365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.os.UserHandle;
4465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.os.UserManager;
4565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.preference.PreferenceManager;
4665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.provider.Settings.Secure;
4765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.util.Log;
4865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.view.inputmethod.InputMethodInfo;
4965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.view.inputmethod.InputMethodManager;
5065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
5165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport com.android.internal.widget.ILockSettings;
5265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport com.android.internal.widget.LockPatternUtils;
5365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport com.android.internal.widget.LockPatternUtilsCache;
5465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport com.android.tv.dialog.PinDialogFragment;
5565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
5665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport java.util.ArrayList;
5765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport java.util.Collections;
5865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport java.util.HashMap;
5965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport java.util.HashSet;
6065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport java.util.List;
6165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport java.util.Set;
6265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
6365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane/**
6465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * Activity that allows the configuration of a user's restricted profile.
6565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane */
6665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lanepublic class RestrictedProfileActivity extends Activity implements Action.Listener,
6765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        AppLoadingTask.Listener {
6865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
6965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    public static class RestrictedProfilePinDialogFragment extends PinDialogFragment {
7065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
7165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        private static final String PREF_DISABLE_PIN_UNTIL =
7265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                "RestrictedProfileActivity$RestrictedProfilePinDialogFragment.disable_pin_until";
7365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
7465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        /**
7565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane         * Returns the time until we should disable the PIN dialog (because the user input wrong
7665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane         * PINs repeatedly).
7765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane         */
7865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        public static final long getDisablePinUntil(Context context) {
7965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            return PreferenceManager.getDefaultSharedPreferences(context).getLong(
8065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    PREF_DISABLE_PIN_UNTIL, 0);
8165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        }
8265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
8365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        /**
8465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane         * Saves the time until we should disable the PIN dialog (because the user input wrong PINs
8565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane         * repeatedly).
8665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane         */
8765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        public static final void setDisablePinUntil(Context context, long timeMillis) {
8865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            PreferenceManager.getDefaultSharedPreferences(context).edit().putLong(
8965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    PREF_DISABLE_PIN_UNTIL, timeMillis).apply();
9065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        }
9165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
9265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        private final LockPatternUtils mLpu;
9365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        private final ILockSettings mILockSettings;
9465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
9565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        public RestrictedProfilePinDialogFragment(int type, ResultListener listener,
9665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                LockPatternUtils lpu, ILockSettings iLockSettings) {
9765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            super(type, listener);
9865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            mLpu = lpu;
9965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            mILockSettings = iLockSettings;
10065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        }
10165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
10265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        @Override
10365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        public long getPinDisabledUntil() {
10465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            return getDisablePinUntil(getActivity());
10565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        }
10665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
10765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        @Override
10865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        public void setPinDisabledUntil(long retryDisableTimeout) {
10965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            setDisablePinUntil(getActivity(), retryDisableTimeout);
11065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        }
11165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
11265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        @Override
11365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        public void setPin(String pin) {
11465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            mLpu.saveLockPassword(pin, DevicePolicyManager.PASSWORD_QUALITY_SOMETHING);
11565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        }
11665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
11765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        @Override
11865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        public boolean isPinCorrect(String pin) {
11965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            try {
12065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                if (mILockSettings.checkPassword(pin, UserHandle.USER_OWNER)) {
12165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    return true;
12265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                }
12365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            } catch (RemoteException re) {
12465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                // Do nothing
12565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            }
12665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            return false;
12765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        }
12865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
12965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        @Override
13065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        public boolean isPinSet() {
13165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            return UserHandle.myUserId() != UserHandle.USER_OWNER || hasLockscreenSecurity(mLpu);
13265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        }
13365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
13465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
13565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private static final boolean DEBUG = false;
13665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private static final String TAG = "RestrictedProfile";
13765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
13865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private static final String
13965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            ACTION_RESTRICTED_PROFILE_SETUP_LOCKSCREEN = "restricted_setup_locakscreen";
14065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private static final String ACTION_RESTRICTED_PROFILE_CREATE = "restricted_profile_create";
14165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private static final String
14265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            ACTION_RESTRICTED_PROFILE_SWITCH_TO = "restricted_profile_switch_to";
14365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private static final String
14465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            ACTION_RESTRICTED_PROFILE_SWITCH_OUT = "restricted_profile_switch_out";
14565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private static final String ACTION_RESTRICTED_PROFILE_CONFIG = "restricted_profile_config";
14665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private static final String ACTION_RESTRICTED_PROFILE_CONFIG_APPS = "restricted_profile_config_apps";
14765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private static final String ACTION_RESTRICTED_PROFILE_CHANGE_PASSWORD = "restricted_profile_change_password";
14865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private static final String ACTION_RESTRICTED_PROFILE_DELETE = "restricted_profile_delete";
14965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private static final String
15065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            ACTION_RESTRICTED_PROFILE_DELETE_CONFIRM = "restricted_profile_delete_confirm";
15165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private static final String
15265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            ACTION_RESTRICTED_PROFILE_DELETE_CANCEL = "restricted_profile_delete_cancel";
15365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
15465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    /**
15565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane     * The description string that should be used for an action that launches the restricted profile
15665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane     * activity.
15765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane     *
15865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane     * @param context used to get the appropriate string.
15965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane     * @return the description string that should be used for an action that launches the restricted
16065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane     *         profile activity.
16165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane     */
16265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    public static String getActionDescription(Context context) {
1630898ab0f3d589be95488997e7ec1a8cc2a06b399Tim Kilbourn        return context.getString(isRestrictedProfileInEffect(context) ? R.string.on : R.string.off);
1640898ab0f3d589be95488997e7ec1a8cc2a06b399Tim Kilbourn    }
1650898ab0f3d589be95488997e7ec1a8cc2a06b399Tim Kilbourn
1660898ab0f3d589be95488997e7ec1a8cc2a06b399Tim Kilbourn    public static boolean isRestrictedProfileInEffect(Context context) {
16765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        UserManager userManager = (UserManager) context.getSystemService(Context.USER_SERVICE);
16865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        UserInfo restrictedUserInfo = findRestrictedUser(userManager);
16965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        boolean isOwner = UserHandle.myUserId() == UserHandle.USER_OWNER;
17065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        boolean isRestrictedProfileOn = restrictedUserInfo != null && !isOwner;
1710898ab0f3d589be95488997e7ec1a8cc2a06b399Tim Kilbourn        return isRestrictedProfileOn;
17265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
17365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
17465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    static void switchUserNow(int userId) {
17565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        try {
17665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            ActivityManagerNative.getDefault().switchUser(userId);
17765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        } catch (RemoteException re) {
17865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            Log.e(TAG, "Caught exception while switching user! " + re);
17965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        }
18065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
18165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
18265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    static int getIconResource() {
18365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        return R.drawable.ic_settings_restricted_profile;
18465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
18565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
18665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    static UserInfo findRestrictedUser(UserManager userManager) {
18765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        for (UserInfo userInfo : userManager.getUsers()) {
18865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            if (userInfo.isRestricted()) {
18965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                return userInfo;
19065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            }
19165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        }
19265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        return null;
19365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
19465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
19565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private final HashMap<String, Boolean> mSelectedPackages = new HashMap<String, Boolean>();
19665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private final boolean mIsOwner = UserHandle.myUserId() == UserHandle.USER_OWNER;
19765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private final AsyncTask<Void, Void, UserInfo>
19865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            mAddUserAsyncTask = new AsyncTask<Void, Void, UserInfo>() {
19965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        @Override
20065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        protected UserInfo doInBackground(Void... params) {
20165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            UserInfo restrictedUserInfo = mUserManager.createUser(
20265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    RestrictedProfileActivity.this.getString(R.string.user_new_profile_name),
20365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    UserInfo.FLAG_RESTRICTED);
20465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            if (restrictedUserInfo == null) {
20565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                Log.wtf(TAG, "Got back a null user handle!");
20665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                return null;
20765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            }
20865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            int userId = restrictedUserInfo.id;
20965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            UserHandle user = new UserHandle(userId);
21065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            mUserManager.setUserRestriction(UserManager.DISALLOW_MODIFY_ACCOUNTS, true, user);
21165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            Secure.putIntForUser(getContentResolver(), Secure.LOCATION_MODE,
21265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    Secure.LOCATION_MODE_OFF, userId);
21365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            mUserManager.setUserRestriction(UserManager.DISALLOW_SHARE_LOCATION, true, user);
21465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            Bitmap bitmap = createBitmapFromDrawable(R.drawable.ic_avatar_default);
21565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            mUserManager.setUserIcon(userId, bitmap);
21665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            // Add shared accounts
21765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            AccountManager am = AccountManager.get(RestrictedProfileActivity.this);
21865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            Account[] accounts = am.getAccounts();
21965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            if (accounts != null) {
22065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                for (Account account : accounts) {
22165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    am.addSharedAccount(account, user);
22265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                }
22365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            }
22465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            return restrictedUserInfo;
22565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        }
22665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
22765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        @Override
22865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        protected void onPostExecute(UserInfo result) {
22965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            if (result == null) {
23065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                return;
23165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            }
23265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            mRestrictedUserInfo = result;
23365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            UserSwitchListenerService.updateLaunchPoint(RestrictedProfileActivity.this, true);
23465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            int userId = result.id;
23565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            if (result.isRestricted() && mIsOwner) {
23665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                DialogFragment dialogFragment = UserAppRestrictionsDialogFragment.newInstance(
23765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                        RestrictedProfileActivity.this, userId, true);
23865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                DialogFragment.add(getFragmentManager(), dialogFragment);
23965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                mMainMenuDialogFragment.setActions(getMainMenuActions());
24065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            }
24165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        }
24265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    };
24365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
24465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private UserManager mUserManager;
24565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private UserInfo mRestrictedUserInfo;
24665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private DialogFragment mMainMenuDialogFragment;
24765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private ILockSettings mLockSettingsService;
24865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private Handler mHandler;
24965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private IPackageManager mIPm;
25065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private AppLoadingTask mAppLoadingTask;
25165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private Action mConfigAppsAction;
25265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private DialogFragment mConfigDialogFragment;
25365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
25465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    @Override
25565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    protected void onCreate(Bundle savedInstanceState) {
25665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        super.onCreate(savedInstanceState);
25765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        mHandler = new Handler();
25865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        mIPm = IPackageManager.Stub.asInterface(ServiceManager.getService("package"));
25965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        mUserManager = (UserManager) getSystemService(Context.USER_SERVICE);
26065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        mRestrictedUserInfo = findRestrictedUser(mUserManager);
26165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        mConfigAppsAction = createConfigAppsAction(-1);
26265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        mMainMenuDialogFragment = new DialogFragment.Builder()
26365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                .title(getString(R.string.launcher_restricted_profile_app_name))
26465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                .description(getString(R.string.user_add_profile_item_summary))
26565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                .iconResourceId(getIconResource())
26665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                .iconBackgroundColor(getResources().getColor(R.color.icon_background))
26765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                .actions(getMainMenuActions()).build();
26865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        DialogFragment.add(getFragmentManager(), mMainMenuDialogFragment);
26965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
27065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
27165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    @Override
27265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    protected void onResume() {
27365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        super.onResume();
27465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        if (mRestrictedUserInfo != null && (mAppLoadingTask == null
27565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                || mAppLoadingTask.getStatus() == AsyncTask.Status.FINISHED)) {
27665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            mAppLoadingTask = new AppLoadingTask(this, mRestrictedUserInfo.id, false, mIPm, this);
27765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            mAppLoadingTask.execute((Void[]) null);
27865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        }
27965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
28065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
28165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    @Override
28265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    public void onPackageEnableChanged(String packageName, boolean enabled) {
28365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
28465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
28565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    @Override
28665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    public void onActionsLoaded(ArrayList<Action> actions) {
28765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        int allowedApps = 0;
28865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        for(Action action : actions) {
28965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            if(action.isChecked()) {
29065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                allowedApps++;
29165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            }
29265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        }
29365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        mConfigAppsAction = createConfigAppsAction(allowedApps);
29465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        if (mConfigDialogFragment != null) {
29565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            mConfigDialogFragment.setActions(getConfigActions());
29665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        }
29765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
29865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
29965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    @Override
30065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    public void onActionClicked(Action action) {
30165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        if (ACTION_RESTRICTED_PROFILE_SWITCH_TO.equals(action.getKey())) {
30265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            switchUserNow(mRestrictedUserInfo.id);
30365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            finish();
30465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        } else if (ACTION_RESTRICTED_PROFILE_SWITCH_OUT.equals(action.getKey())) {
30565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            if (getFragmentManager().findFragmentByTag(PinDialogFragment.DIALOG_TAG) != null) {
30665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                return;
30765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            }
30865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            new RestrictedProfilePinDialogFragment(PinDialogFragment.PIN_DIALOG_TYPE_ENTER_PIN,
30965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    new PinDialogFragment.ResultListener() {
31065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                        @Override
31165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                        public void done(boolean success) {
31265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                            if (success) {
31365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                                switchUserNow(UserHandle.USER_OWNER);
31465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                                finish();
31565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                            }
31665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                        }
31765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    }, new LockPatternUtils(this), getLockSettings()).show(getFragmentManager(),
31865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    PinDialogFragment.DIALOG_TAG);
31965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        } else if (ACTION_RESTRICTED_PROFILE_CHANGE_PASSWORD.equals(action.getKey())) {
32065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            if (getFragmentManager().findFragmentByTag(PinDialogFragment.DIALOG_TAG) != null) {
32165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                return;
32265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            }
32365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            new RestrictedProfilePinDialogFragment(PinDialogFragment.PIN_DIALOG_TYPE_NEW_PIN,
32465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    new PinDialogFragment.ResultListener() {
32565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                        @Override
32665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                        public void done(boolean success) {
32765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                            // do nothing
32865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                        }
32965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    }, new LockPatternUtils(this), getLockSettings()).show(getFragmentManager(),
33065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    PinDialogFragment.DIALOG_TAG);
33165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        } else if (ACTION_RESTRICTED_PROFILE_CONFIG.equals(action.getKey())) {
33265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            mConfigDialogFragment = new DialogFragment.Builder()
33365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    .title(getString(R.string.restricted_profile_configure_title))
33465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    .iconResourceId(getIconResource())
33565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    .iconBackgroundColor(getResources().getColor(R.color.icon_background))
33665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    .actions(getConfigActions()).build();
33765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            DialogFragment.add(getFragmentManager(), mConfigDialogFragment);
33865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        } else if (ACTION_RESTRICTED_PROFILE_CONFIG_APPS.equals(action.getKey())) {
33965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            DialogFragment dialogFragment = UserAppRestrictionsDialogFragment.newInstance(
34065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    RestrictedProfileActivity.this, mRestrictedUserInfo.id, false);
34165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            DialogFragment.add(getFragmentManager(), dialogFragment);
34265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        } else if (ACTION_RESTRICTED_PROFILE_DELETE.equals(action.getKey())) {
343657ddb0514600ca834faf69228dcc3aac4061b63RoboErik            if (getFragmentManager().findFragmentByTag(PinDialogFragment.DIALOG_TAG) != null) {
344657ddb0514600ca834faf69228dcc3aac4061b63RoboErik                return;
345657ddb0514600ca834faf69228dcc3aac4061b63RoboErik            }
346657ddb0514600ca834faf69228dcc3aac4061b63RoboErik            new RestrictedProfilePinDialogFragment(PinDialogFragment.PIN_DIALOG_TYPE_ENTER_PIN,
347657ddb0514600ca834faf69228dcc3aac4061b63RoboErik                    new PinDialogFragment.ResultListener() {
348657ddb0514600ca834faf69228dcc3aac4061b63RoboErik                        @Override
349657ddb0514600ca834faf69228dcc3aac4061b63RoboErik                        public void done(boolean success) {
350657ddb0514600ca834faf69228dcc3aac4061b63RoboErik                            if (success) {
351657ddb0514600ca834faf69228dcc3aac4061b63RoboErik                                removeRestrictedUser();
352657ddb0514600ca834faf69228dcc3aac4061b63RoboErik                                LockPatternUtils lpu = new LockPatternUtils(
353657ddb0514600ca834faf69228dcc3aac4061b63RoboErik                                        RestrictedProfileActivity.this);
354657ddb0514600ca834faf69228dcc3aac4061b63RoboErik                                lpu.clearLock(false);
355657ddb0514600ca834faf69228dcc3aac4061b63RoboErik                            }
356657ddb0514600ca834faf69228dcc3aac4061b63RoboErik                        }
357657ddb0514600ca834faf69228dcc3aac4061b63RoboErik                    }, new LockPatternUtils(this), getLockSettings()).show(getFragmentManager(),
358657ddb0514600ca834faf69228dcc3aac4061b63RoboErik                    PinDialogFragment.DIALOG_TAG);
35965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        } else if (ACTION_RESTRICTED_PROFILE_DELETE_CONFIRM.equals(action.getKey())) {
360657ddb0514600ca834faf69228dcc3aac4061b63RoboErik            // TODO remove once we confirm it's not needed
36165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            removeRestrictedUser();
36265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            LockPatternUtils lpu = new LockPatternUtils(this);
36365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            lpu.clearLock(false);
36465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        } else if (ACTION_RESTRICTED_PROFILE_DELETE_CANCEL.equals(action.getKey())) {
365657ddb0514600ca834faf69228dcc3aac4061b63RoboErik            // TODO remove once we confirm it's not needed
36665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            onBackPressed();
36765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        } else if (ACTION_RESTRICTED_PROFILE_CREATE.equals(action.getKey())) {
36865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            if (hasLockscreenSecurity(new LockPatternUtils(this))) {
36965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                addRestrictedUser();
37065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            } else {
37165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                launchChooseLockscreen();
37265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            }
37365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        }
37465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
37565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
37665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private ILockSettings getLockSettings() {
37765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        if (mLockSettingsService == null) {
37865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            mLockSettingsService = LockPatternUtilsCache.getInstance(
37965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    ILockSettings.Stub.asInterface(ServiceManager.getService("lock_settings")));
38065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        }
38165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        return mLockSettingsService;
38265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
38365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
38465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private ArrayList<Action> getMainMenuActions() {
38565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        ArrayList<Action> actions = new ArrayList<Action>();
38665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        if (mRestrictedUserInfo != null) {
38765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            if (mIsOwner) {
38865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                actions.add(new Action.Builder()
38965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                        .key(ACTION_RESTRICTED_PROFILE_SWITCH_TO)
39065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                        .title(getString(R.string.restricted_profile_switch_to))
39165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                        .build());
39265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                actions.add(new Action.Builder()
39365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                        .key(ACTION_RESTRICTED_PROFILE_CONFIG)
39465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                        .title(getString(R.string.restricted_profile_configure_title))
39565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                        .build());
396657ddb0514600ca834faf69228dcc3aac4061b63RoboErik                actions.add(new Action.Builder()
397657ddb0514600ca834faf69228dcc3aac4061b63RoboErik                        .key(ACTION_RESTRICTED_PROFILE_DELETE)
398657ddb0514600ca834faf69228dcc3aac4061b63RoboErik                        .title(getString(R.string.restricted_profile_delete_title))
399657ddb0514600ca834faf69228dcc3aac4061b63RoboErik                        .build());
40065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            } else {
40165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                actions.add(new Action.Builder()
40265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                        .key(ACTION_RESTRICTED_PROFILE_SWITCH_OUT)
40365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                        .title(getString(R.string.restricted_profile_switch_out))
40465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                        .build());
40565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            }
40665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        } else {
40765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            actions.add(new Action.Builder()
40865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    .key(ACTION_RESTRICTED_PROFILE_CREATE)
40965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                        .title(getString(R.string.restricted_profile_configure_title))
41065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    .build());
41165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        }
41265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        return actions;
41365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
41465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
41565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private ArrayList<Action> getConfigActions() {
41665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        ArrayList<Action> actions = new ArrayList<Action>();
41765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        actions.add(new Action.Builder()
41865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                .key(ACTION_RESTRICTED_PROFILE_CHANGE_PASSWORD)
41965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                .title(getString(R.string.restricted_profile_change_password_title))
42065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                .build());
42165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        actions.add(mConfigAppsAction);
42265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        return actions;
42365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
42465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
42565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private Action createConfigAppsAction(int allowedApps) {
42665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        String description = allowedApps >= 0 ? getResources().getQuantityString(
42765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                R.plurals.restricted_profile_configure_apps_description, allowedApps, allowedApps)
42865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                : getString(R.string.restricted_profile_configure_apps_description_loading);
42965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        return new Action.Builder()
43065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                .key(ACTION_RESTRICTED_PROFILE_CONFIG_APPS)
43165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                .title(getString(R.string.restricted_profile_configure_apps_title))
43265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                .description(description)
43365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                .build();
43465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
43565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
43665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private static boolean hasLockscreenSecurity(LockPatternUtils lpu) {
43765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        return lpu.isLockPasswordEnabled() || lpu.isLockPatternEnabled();
43865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
43965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
44065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private void launchChooseLockscreen() {
44165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        if (getFragmentManager().findFragmentByTag(PinDialogFragment.DIALOG_TAG) != null) {
44265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            return;
44365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        }
44465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        new RestrictedProfilePinDialogFragment(PinDialogFragment.PIN_DIALOG_TYPE_NEW_PIN,
44565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                new PinDialogFragment.ResultListener() {
44665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    @Override
44765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    public void done(boolean success) {
44865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                        if (success) {
44965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                            addRestrictedUser();
45065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                        }
45165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    }
45265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                }, new LockPatternUtils(this), getLockSettings()).show(getFragmentManager(),
45365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                PinDialogFragment.DIALOG_TAG);
45465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
45565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
45665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private void removeRestrictedUser() {
45765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        mHandler.post(new Runnable() {
45865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            @Override
45965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            public void run() {
46065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                mUserManager.removeUser(mRestrictedUserInfo.id);
46165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                // pop confirm dialog
46265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                mRestrictedUserInfo = null;
46365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                UserSwitchListenerService.updateLaunchPoint(RestrictedProfileActivity.this, false);
46465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                mMainMenuDialogFragment.setActions(getMainMenuActions());
46565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                getFragmentManager().popBackStack();
46665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            }
46765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        });
46865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
46965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
47065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private Bitmap createBitmapFromDrawable(int resId) {
47165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        Drawable icon = getResources().getDrawable(resId);
47265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        icon.setBounds(0, 0, icon.getIntrinsicWidth(), icon.getIntrinsicHeight());
47365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        Bitmap bitmap = Bitmap.createBitmap(icon.getIntrinsicWidth(), icon.getIntrinsicHeight(),
47465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                Bitmap.Config.ARGB_8888);
47565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        icon.draw(new Canvas(bitmap));
47665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        return bitmap;
47765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
47865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
47965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private void addRestrictedUser() {
48065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        if (AsyncTask.Status.PENDING == mAddUserAsyncTask.getStatus()) {
48165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            mAddUserAsyncTask.execute((Void[]) null);
48265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        }
48365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
48465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane}
485