1 /*
2  * Copyright (C) 2018 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;
18 
19 import static com.android.settings.biometrics.BiometricEnrollBase.RESULT_FINISHED;
20 import static com.android.settings.biometrics.BiometricEnrollBase.RESULT_TIMEOUT;
21 
22 import android.app.Activity;
23 import android.app.Dialog;
24 import android.content.DialogInterface;
25 import android.hardware.biometrics.BiometricConstants;
26 import android.os.Bundle;
27 
28 import androidx.appcompat.app.AlertDialog;
29 
30 import com.android.settings.core.instrumentation.InstrumentedDialogFragment;
31 
32 /**
33  * Abstract dialog, shown when an error occurs during biometric enrollment.
34  */
35 public abstract class BiometricErrorDialog extends InstrumentedDialogFragment {
36 
37     public static final String KEY_ERROR_MSG = "error_msg";
38     public static final String KEY_ERROR_ID = "error_id";
39 
getTitleResId()40     public abstract int getTitleResId();
getOkButtonTextResId()41     public abstract int getOkButtonTextResId();
42 
43     @Override
onCreateDialog(Bundle savedInstanceState)44     public Dialog onCreateDialog(Bundle savedInstanceState) {
45         AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
46         CharSequence errorString = getArguments().getCharSequence(KEY_ERROR_MSG);
47         final int errMsgId = getArguments().getInt(KEY_ERROR_ID);
48 
49         builder.setTitle(getTitleResId())
50                 .setMessage(errorString)
51                 .setCancelable(false)
52                 .setPositiveButton(getOkButtonTextResId(),
53                         new DialogInterface.OnClickListener() {
54                             @Override
55                             public void onClick(DialogInterface dialog, int which) {
56                                 dialog.dismiss();
57                                 boolean wasTimeout =
58                                         errMsgId == BiometricConstants.BIOMETRIC_ERROR_TIMEOUT;
59                                 Activity activity = getActivity();
60                                 activity.setResult(wasTimeout ?
61                                         RESULT_TIMEOUT : RESULT_FINISHED);
62                                 activity.finish();
63                             }
64                         });
65         AlertDialog dialog = builder.create();
66         dialog.setCanceledOnTouchOutside(false);
67         return dialog;
68     }
69 }
70