1 /*
2  * Copyright (C) 2023 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.settings.privatespace.onelock;
18 
19 import static com.android.internal.widget.LockPatternUtils.CREDENTIAL_TYPE_PASSWORD;
20 import static com.android.internal.widget.LockPatternUtils.CREDENTIAL_TYPE_PATTERN;
21 import static com.android.internal.widget.LockPatternUtils.CREDENTIAL_TYPE_PIN;
22 import static com.android.settings.password.ChooseLockGeneric.ChooseLockGenericFragment.HIDE_INSECURE_OPTIONS;
23 import static com.android.settings.password.ChooseLockSettingsHelper.EXTRA_KEY_CHOOSE_LOCK_SCREEN_TITLE;
24 
25 import android.content.Context;
26 import android.content.Intent;
27 import android.os.Bundle;
28 import android.os.UserHandle;
29 import android.os.UserManager;
30 import android.text.TextUtils;
31 import android.util.Log;
32 
33 import androidx.preference.Preference;
34 
35 import com.android.internal.widget.LockPatternUtils;
36 import com.android.settings.R;
37 import com.android.settings.SettingsPreferenceFragment;
38 import com.android.settings.Utils;
39 import com.android.settings.core.SubSettingLauncher;
40 import com.android.settings.overlay.FeatureFactory;
41 import com.android.settings.password.ChooseLockGeneric;
42 import com.android.settings.privatespace.PrivateSpaceMaintainer;
43 import com.android.settingslib.core.AbstractPreferenceController;
44 import com.android.settingslib.transition.SettingsTransitionHelper;
45 
46 
47 /** Represents the preference controller for changing private space lock. */
48 public class PrivateSpaceLockController extends AbstractPreferenceController {
49     private static final String TAG = "PrivateSpaceLockContr";
50     private static final String KEY_CHANGE_PROFILE_LOCK =
51             "change_private_space_lock";
52 
53     private final SettingsPreferenceFragment mHost;
54     private final UserManager mUserManager;
55     private final LockPatternUtils mLockPatternUtils;
56     private final int mProfileUserId;
57 
PrivateSpaceLockController(Context context, SettingsPreferenceFragment host)58     public PrivateSpaceLockController(Context context, SettingsPreferenceFragment host) {
59         super(context);
60         mUserManager = context.getSystemService(UserManager.class);
61         mLockPatternUtils = FeatureFactory.getFeatureFactory()
62                 .getSecurityFeatureProvider()
63                 .getLockPatternUtils(context);
64         mHost = host;
65         UserHandle privateProfileHandle = PrivateSpaceMaintainer.getInstance(context)
66                 .getPrivateProfileHandle();
67         if (privateProfileHandle != null) {
68             mProfileUserId = privateProfileHandle.getIdentifier();
69         } else {
70             mProfileUserId = -1;
71             Log.e(TAG, "Private profile user handle is not expected to be null.");
72         }
73     }
74 
75     @Override
isAvailable()76     public boolean isAvailable() {
77         return android.os.Flags.allowPrivateProfile()
78                 && android.multiuser.Flags.enablePrivateSpaceFeatures();
79     }
80 
81     @Override
getPreferenceKey()82     public String getPreferenceKey() {
83         return KEY_CHANGE_PROFILE_LOCK;
84     }
85 
86     @Override
handlePreferenceTreeClick(Preference preference)87     public boolean handlePreferenceTreeClick(Preference preference) {
88         if (!TextUtils.equals(preference.getKey(), getPreferenceKey())) {
89             return false;
90         }
91         //Checks if the profile is in quiet mode and show a dialog to unpause the profile.
92         if (Utils.startQuietModeDialogIfNecessary(mContext, mUserManager,
93                 mProfileUserId)) {
94             return false;
95         }
96         final Bundle extras = new Bundle();
97         extras.putInt(Intent.EXTRA_USER_ID, mProfileUserId);
98         extras.putBoolean(HIDE_INSECURE_OPTIONS, true);
99         extras.putInt(EXTRA_KEY_CHOOSE_LOCK_SCREEN_TITLE, R.string.private_space_lock_setup_title);
100         new SubSettingLauncher(mContext)
101                 .setDestination(ChooseLockGeneric.ChooseLockGenericFragment.class.getName())
102                 .setSourceMetricsCategory(mHost.getMetricsCategory())
103                 .setArguments(extras)
104                 .setExtras(extras)
105                 .setTransitionType(SettingsTransitionHelper.TransitionType.TRANSITION_SLIDE)
106                 .launch();
107         return true;
108     }
109 
110     @Override
updateState(Preference preference)111     public void updateState(Preference preference) {
112         if (mLockPatternUtils.isSeparateProfileChallengeEnabled(mProfileUserId)) {
113             preference.setSummary(
114                     mContext.getString(getCredentialTypeResId(mProfileUserId)));
115             preference.setEnabled(true);
116         } else {
117             preference.setSummary(mContext.getString(
118                     R.string.lock_settings_profile_unified_summary));
119             preference.setEnabled(false);
120         }
121     }
122 
getCredentialTypeResId(int userId)123     private int getCredentialTypeResId(int userId) {
124         int credentialType = mLockPatternUtils.getCredentialTypeForUser(userId);
125         switch (credentialType) {
126             case CREDENTIAL_TYPE_PATTERN :
127                 return R.string.unlock_set_unlock_mode_pattern;
128             case CREDENTIAL_TYPE_PIN:
129                 return R.string.unlock_set_unlock_mode_pin;
130             case CREDENTIAL_TYPE_PASSWORD:
131                 return R.string.unlock_set_unlock_mode_password;
132             default:
133                 // This is returned for CREDENTIAL_TYPE_NONE
134                 return R.string.unlock_set_unlock_mode_off;
135         }
136     }
137 }
138