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.homepage.contextualcards;
18 
19 
20 import android.content.Context;
21 import android.content.DialogInterface;
22 import android.content.Intent;
23 import android.content.pm.PackageManager;
24 import android.hardware.face.Face;
25 import android.hardware.face.FaceManager;
26 import android.os.Bundle;
27 import android.util.Log;
28 
29 import com.android.internal.app.AlertActivity;
30 import com.android.internal.app.AlertController;
31 import com.android.settings.R;
32 import com.android.settings.Utils;
33 import com.android.settings.homepage.contextualcards.slices.FaceSetupSlice;
34 
35 /**
36  * This class is used to show a popup dialog for {@link FaceSetupSlice}.
37  */
38 public class FaceReEnrollDialog extends AlertActivity implements
39         DialogInterface.OnClickListener {
40 
41     private static final String TAG = "FaceReEnrollDialog";
42 
43     private static final String BIOMETRIC_ENROLL_ACTION = "android.settings.BIOMETRIC_ENROLL";
44 
45     private FaceManager mFaceManager;
46     /**
47      * The type of re-enrollment that has been requested,
48      * see {@link Settings.Secure#FACE_UNLOCK_RE_ENROLL} for more details.
49      */
50     private int mReEnrollType;
51 
52     @Override
onCreate(Bundle savedInstanceState)53     protected void onCreate(Bundle savedInstanceState) {
54         super.onCreate(savedInstanceState);
55 
56         final PackageManager pm = getApplicationContext().getPackageManager();
57         final int dialogMessageRes = pm.hasSystemFeature(PackageManager.FEATURE_FINGERPRINT)
58                 ? R.string.security_settings_face_enroll_improve_face_alert_body_fingerprint
59                 : R.string.security_settings_face_enroll_improve_face_alert_body;
60 
61         final AlertController.AlertParams alertParams = mAlertParams;
62         alertParams.mTitle = getText(
63                 R.string.security_settings_face_enroll_improve_face_alert_title);
64         alertParams.mMessage = getText(dialogMessageRes);
65         alertParams.mPositiveButtonText = getText(R.string.storage_menu_set_up);
66         alertParams.mNegativeButtonText = getText(R.string.cancel);
67         alertParams.mPositiveButtonListener = this;
68 
69         mFaceManager = Utils.getFaceManagerOrNull(getApplicationContext());
70 
71         final Context context = getApplicationContext();
72         mReEnrollType = FaceSetupSlice.getReEnrollSetting(context, getUserId());
73 
74         Log.d(TAG, "ReEnroll Type : " + mReEnrollType);
75         if (mReEnrollType == FaceSetupSlice.FACE_RE_ENROLL_SUGGESTED) {
76             // setupAlert will actually display the popup dialog.
77             setupAlert();
78         } else if (mReEnrollType == FaceSetupSlice.FACE_RE_ENROLL_REQUIRED) {
79             // in this case we are skipping the popup dialog and directly going to the
80             // re enrollment flow. A grey overlay will appear to indicate that we are
81             // transitioning.
82             removeFaceAndReEnroll();
83         } else {
84             Log.d(TAG, "Error unsupported flow for : " + mReEnrollType);
85             dismiss();
86         }
87     }
88 
89     @Override
onClick(DialogInterface dialog, int which)90     public void onClick(DialogInterface dialog, int which) {
91         removeFaceAndReEnroll();
92     }
93 
removeFaceAndReEnroll()94     public void removeFaceAndReEnroll() {
95         final int userId = getUserId();
96         if (mFaceManager == null || !mFaceManager.hasEnrolledTemplates(userId)) {
97             finish();
98         }
99         mFaceManager.remove(new Face("", 0, 0), userId, new FaceManager.RemovalCallback() {
100             @Override
101             public void onRemovalError(Face face, int errMsgId, CharSequence errString) {
102                 super.onRemovalError(face, errMsgId, errString);
103                 finish();
104             }
105 
106             @Override
107             public void onRemovalSucceeded(Face face, int remaining) {
108                 super.onRemovalSucceeded(face, remaining);
109                 if (remaining != 0) {
110                     return;
111                 }
112                 // Send user to the enroll flow.
113                 final Intent reEnroll = new Intent(BIOMETRIC_ENROLL_ACTION);
114                 final Context context = getApplicationContext();
115 
116                 try {
117                     startActivity(reEnroll);
118                 } catch (Exception e) {
119                     Log.e(TAG, "Failed to startActivity");
120                 }
121 
122                 finish();
123             }
124         });
125     }
126 }
127