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 android.content.Context;
20 import android.os.UserHandle;
21 import android.util.Log;
22 
23 import androidx.annotation.NonNull;
24 import androidx.lifecycle.Lifecycle;
25 import androidx.preference.Preference;
26 import androidx.preference.PreferenceScreen;
27 
28 import com.android.settings.R;
29 import com.android.settings.Utils;
30 import com.android.settings.biometrics.combination.BiometricFingerprintStatusPreferenceController;
31 import com.android.settings.privatespace.PrivateSpaceMaintainer;
32 
33 public class PrivateSpaceFingerprintPreferenceController
34         extends BiometricFingerprintStatusPreferenceController {
35     private static final String TAG = "PrivateSpaceFingerCtrl";
36 
PrivateSpaceFingerprintPreferenceController(Context context, String key)37     public PrivateSpaceFingerprintPreferenceController(Context context, String key) {
38         super(context, key);
39     }
40 
PrivateSpaceFingerprintPreferenceController( Context context, String key, Lifecycle lifecycle)41     public PrivateSpaceFingerprintPreferenceController(
42             Context context, String key, Lifecycle lifecycle) {
43         super(context, key, lifecycle);
44     }
45 
46     @Override
isUserSupported()47     protected boolean isUserSupported() {
48         return android.os.Flags.allowPrivateProfile()
49                 && android.multiuser.Flags.enableBiometricsToUnlockPrivateSpace()
50                 && android.multiuser.Flags.enablePrivateSpaceFeatures()
51                 && getUserId() != UserHandle.USER_NULL;
52     }
53 
54     @Override
getUserId()55     protected int getUserId() {
56         UserHandle privateProfileHandle =
57                 PrivateSpaceMaintainer.getInstance(mContext).getPrivateProfileHandle();
58         if (privateProfileHandle != null) {
59             return privateProfileHandle.getIdentifier();
60         } else {
61             Log.e(TAG, "Private profile user handle is not expected to be null.");
62         }
63         return UserHandle.USER_NULL;
64     }
65 
66     @Override
getAvailabilityStatus()67     public int getAvailabilityStatus() {
68         return android.os.Flags.allowPrivateProfile()
69                         && android.multiuser.Flags.enableBiometricsToUnlockPrivateSpace()
70                         && android.multiuser.Flags.enablePrivateSpaceFeatures()
71                 ? AVAILABLE
72                 : UNSUPPORTED_ON_DEVICE;
73     }
74 
75     @Override
updateState(@onNull Preference preference)76     public void updateState(@NonNull Preference preference) {
77         if (mLockPatternUtils.isSeparateProfileChallengeEnabled(getUserId())) {
78             super.updateState(preference);
79             preference.setEnabled(true);
80         } else {
81             Utils.removeEnrolledFingerprintForUser(mContext, getUserId());
82             preference.setSummary(
83                     mContext.getString(R.string.lock_settings_profile_unified_summary));
84             preference.setEnabled(false);
85         }
86     }
87 
88     @Override
displayPreference(@onNull PreferenceScreen screen)89     public void displayPreference(@NonNull PreferenceScreen screen) {
90         super.displayPreference(screen);
91         Preference preference = screen.findPreference(getPreferenceKey());
92         if (!Utils.isMultipleBiometricsSupported(mContext)
93                 || !Utils.isFaceNotConvenienceBiometric(mContext)) {
94             preference.setTitle(R.string.private_space_fingerprint_title);
95         }
96     }
97 }
98