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 
23 import android.content.Context;
24 import android.os.UserHandle;
25 import android.util.Log;
26 
27 import com.android.internal.widget.LockPatternUtils;
28 import com.android.settings.R;
29 import com.android.settings.core.BasePreferenceController;
30 import com.android.settings.overlay.FeatureFactory;
31 import com.android.settings.privatespace.PrivateSpaceMaintainer;
32 
33 /** Represents the preference controller for using the same lock as the screen lock */
34 public class UseOneLockController extends BasePreferenceController {
35     private static final String TAG = "UseOneLockController";
36     private final LockPatternUtils mLockPatternUtils;
37     private final PrivateSpaceMaintainer mPrivateSpaceMaintainer;
38 
UseOneLockController(Context context, String preferenceKey)39     public UseOneLockController(Context context, String preferenceKey) {
40         super(context, preferenceKey);
41         mPrivateSpaceMaintainer = PrivateSpaceMaintainer.getInstance(mContext);
42         mLockPatternUtils = FeatureFactory.getFeatureFactory()
43                 .getSecurityFeatureProvider()
44                 .getLockPatternUtils(context);
45     }
46     @Override
getAvailabilityStatus()47     public int getAvailabilityStatus() {
48         return android.os.Flags.allowPrivateProfile()
49                 && android.multiuser.Flags.enablePrivateSpaceFeatures()
50                 ? AVAILABLE
51                 : UNSUPPORTED_ON_DEVICE;
52     }
53 
54     @Override
getSliceHighlightMenuRes()55     public int getSliceHighlightMenuRes() {
56         return 0;
57     }
58 
59     @Override
getSummary()60     public CharSequence getSummary() {
61         UserHandle privateProfileHandle = mPrivateSpaceMaintainer.getPrivateProfileHandle();
62         if (privateProfileHandle != null) {
63             int privateUserId = privateProfileHandle.getIdentifier();
64             if (mLockPatternUtils.isSeparateProfileChallengeEnabled(privateUserId)) {
65                 return mContext.getString(getCredentialTypeResId(privateUserId));
66             }
67         } else {
68             Log.w(TAG, "Did not find Private Space.");
69         }
70         return mContext.getString(R.string.private_space_screen_lock_summary);
71     }
72 
getCredentialTypeResId(int userId)73     private int getCredentialTypeResId(int userId) {
74         int credentialType = mLockPatternUtils.getCredentialTypeForUser(userId);
75         switch (credentialType) {
76             case CREDENTIAL_TYPE_PATTERN:
77                 return R.string.unlock_set_unlock_mode_pattern;
78             case CREDENTIAL_TYPE_PIN:
79                 return R.string.unlock_set_unlock_mode_pin;
80             case CREDENTIAL_TYPE_PASSWORD:
81                 return R.string.unlock_set_unlock_mode_password;
82             default:
83                 // This is returned for CREDENTIAL_TYPE_NONE
84                 return R.string.unlock_set_unlock_mode_off;
85         }
86     }
87 }
88