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.car.settings.security;
18 
19 import android.app.AlertDialog;
20 import android.app.Dialog;
21 import android.os.Bundle;
22 
23 import androidx.fragment.app.DialogFragment;
24 
25 import com.android.car.settings.R;
26 
27 /**
28  * Dialog to confirm pairing code.
29  */
30 public class ConfirmPairingCodeDialog extends DialogFragment {
31     /** Identifier for the dialog which confirms the pairing code. */
32     public static final String TAG = "confirm_pairing_code_dialog";
33     private static final String PAIRING_CODE_KEY = "pairingCode";
34     private ConfirmPairingCodeListener mConfirmPairingCodeListener;
35 
36     /**
37      * Factory method for creating a ConfirmPairingCodeFragment
38      *
39      * @param pairingCode the pairing code sent by the connected device
40      */
newInstance(String pairingCode)41     public static ConfirmPairingCodeDialog newInstance(String pairingCode) {
42         Bundle args = new Bundle();
43         args.putString(PAIRING_CODE_KEY, pairingCode);
44 
45         ConfirmPairingCodeDialog dialog = new ConfirmPairingCodeDialog();
46         dialog.setArguments(args);
47         return dialog;
48     }
49 
50     /** Sets a listener to act when a user confirms pairing code. */
setConfirmPairingCodeListener(ConfirmPairingCodeListener listener)51     public void setConfirmPairingCodeListener(ConfirmPairingCodeListener listener) {
52         mConfirmPairingCodeListener = listener;
53     }
54 
55     @Override
onCreateDialog(Bundle savedInstanceState)56     public Dialog onCreateDialog(Bundle savedInstanceState) {
57         Bundle args = getArguments();
58         String pairingCode = args.getString(PAIRING_CODE_KEY);
59         return new AlertDialog.Builder(getContext())
60                 .setTitle(getContext().getString(R.string.trusted_device_pairing_code_dialog_title))
61                 .setMessage(pairingCode)
62                 .setPositiveButton(R.string.trusted_device_confirm_button, (dialog, which) -> {
63                     if (mConfirmPairingCodeListener != null) {
64                         mConfirmPairingCodeListener.onConfirmPairingCode();
65                     }
66                 })
67                 .setNegativeButton(android.R.string.cancel, (dialog, which) -> {
68                     if (mConfirmPairingCodeListener != null) {
69                         mConfirmPairingCodeListener.onDialogCancelled();
70                     }
71                 })
72                 .create();
73     }
74 
75     /** A listener for when user interacts with this dialog. */
76     public interface ConfirmPairingCodeListener {
77         /** Defines the actions to take when a user confirms the pairing code. */
78         void onConfirmPairingCode();
79 
80         /** Defines the actions to take when a user cancel confirming the pairing code. */
81         void onDialogCancelled();
82     }
83 
84 }
85