1 /*
2  * Copyright (C) 2019 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.biometrics.face;
18 
19 import static com.android.settings.Utils.SETTINGS_PACKAGE_NAME;
20 
21 import android.app.admin.DevicePolicyManager;
22 import android.content.Context;
23 import android.content.Intent;
24 import android.view.View;
25 import android.widget.Button;
26 
27 import androidx.preference.Preference;
28 
29 import com.android.settings.R;
30 import com.android.settings.core.BasePreferenceController;
31 import com.android.settings.password.ChooseLockSettingsHelper;
32 import com.android.settingslib.RestrictedLockUtilsInternal;
33 import com.android.settingslib.widget.LayoutPreference;
34 
35 import com.google.android.setupdesign.util.ButtonStyler;
36 import com.google.android.setupdesign.util.PartnerStyleHelper;
37 
38 /**
39  * Preference controller that allows a user to enroll their face.
40  */
41 public class FaceSettingsEnrollButtonPreferenceController extends BasePreferenceController
42         implements View.OnClickListener {
43 
44     private static final String TAG = "FaceSettings/Remove";
45     static final String KEY = "security_settings_face_enroll_faces_container";
46 
47     private final Context mContext;
48 
49     private int mUserId;
50     private byte[] mToken;
51     private Button mButton;
52     private boolean mIsClicked;
53     private Listener mListener;
54 
FaceSettingsEnrollButtonPreferenceController(Context context)55     public FaceSettingsEnrollButtonPreferenceController(Context context) {
56         this(context, KEY);
57     }
58 
FaceSettingsEnrollButtonPreferenceController(Context context, String preferenceKey)59     public FaceSettingsEnrollButtonPreferenceController(Context context, String preferenceKey) {
60         super(context, preferenceKey);
61         mContext = context;
62     }
63 
64     @Override
updateState(Preference preference)65     public void updateState(Preference preference) {
66         super.updateState(preference);
67 
68         mButton = ((LayoutPreference) preference).findViewById(
69                 R.id.security_settings_face_settings_enroll_button);
70 
71         if (PartnerStyleHelper.shouldApplyPartnerResource(mButton)) {
72             ButtonStyler.applyPartnerCustomizationPrimaryButtonStyle(mContext, mButton);
73         }
74 
75         mButton.setOnClickListener(this);
76         final boolean isDeviceOwnerBlockingAuth =
77                 RestrictedLockUtilsInternal.checkIfKeyguardFeaturesDisabled(
78                         mContext, DevicePolicyManager.KEYGUARD_DISABLE_FACE, mUserId) != null;
79         mButton.setEnabled(!isDeviceOwnerBlockingAuth);
80     }
81 
82     @Override
onClick(View v)83     public void onClick(View v) {
84         mIsClicked = true;
85         final Intent intent = new Intent();
86         intent.setClassName(SETTINGS_PACKAGE_NAME, FaceEnrollIntroduction.class.getName());
87         intent.putExtra(Intent.EXTRA_USER_ID, mUserId);
88         intent.putExtra(ChooseLockSettingsHelper.EXTRA_KEY_CHALLENGE_TOKEN, mToken);
89         if (mListener != null) {
90             mListener.onStartEnrolling(intent);
91         } else {
92             mContext.startActivity(intent);
93         }
94     }
95 
96     @Override
getAvailabilityStatus()97     public int getAvailabilityStatus() {
98         return AVAILABLE;
99     }
100 
setUserId(int userId)101     public void setUserId(int userId) {
102         mUserId = userId;
103     }
104 
setToken(byte[] token)105     public void setToken(byte[] token) {
106         mToken = token;
107     }
108 
109     // Return the click state, then clear its state.
isClicked()110     public boolean isClicked() {
111         final boolean wasClicked = mIsClicked;
112         mIsClicked = false;
113         return wasClicked;
114     }
115 
setListener(Listener listener)116     public void setListener(Listener listener) {
117         mListener = listener;
118     }
119 
120     /**
121      * Interface for registering callbacks related to the face enroll preference button.
122      */
123     public interface Listener {
124         /**
125          * Called when the user has indicated an intent to begin enrolling a new face.
126          * @param intent The Intent that should be used to launch face enrollment.
127          */
onStartEnrolling(Intent intent)128         void onStartEnrolling(Intent intent);
129     }
130 }
131