• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 removing a trusted device.
29  */
30 public class ConfirmRemoveDeviceDialog extends DialogFragment {
31 
32     /** Identifier for the dialog which confirms removing a trusted device. */
33     public static final String TAG = "confirm_remove_device_dialog";
34     private static final String DEVICE_NAME_KEY = "deviceName";
35     private static final String HANDLE_KEY = "handle";
36     private ConfirmRemoveDeviceListener mConfirmRemoveDeviceListener;
37 
38     /**
39      * Factory method for creating a {@link ConfirmRemoveDeviceDialog}
40      *
41      * @param deviceName the name of current clicked device
42      * @param handle the handle of current clicked device and is used to identify the device
43      */
newInstance(String deviceName, long handle)44     public static ConfirmRemoveDeviceDialog newInstance(String deviceName, long handle) {
45         Bundle args = new Bundle();
46         args.putString(DEVICE_NAME_KEY, deviceName);
47         args.putLong(HANDLE_KEY, handle);
48 
49         ConfirmRemoveDeviceDialog dialog = new ConfirmRemoveDeviceDialog();
50         dialog.setArguments(args);
51         return dialog;
52     }
53 
54     /** Sets a listener to act when a user confirms removing the trusted device. */
setConfirmRemoveDeviceListener(ConfirmRemoveDeviceListener listener)55     public void setConfirmRemoveDeviceListener(ConfirmRemoveDeviceListener listener) {
56         mConfirmRemoveDeviceListener = listener;
57     }
58 
59     @Override
onCreateDialog(Bundle savedInstanceState)60     public Dialog onCreateDialog(Bundle savedInstanceState) {
61         Bundle args = getArguments();
62         String name = args.getString(DEVICE_NAME_KEY);
63         long handle = args.getLong(HANDLE_KEY);
64         return new AlertDialog.Builder(getContext())
65                 .setTitle(name)
66                 .setMessage(getContext().getString(R.string.remove_device_message, name, name))
67                 .setPositiveButton(R.string.trusted_device_remove_button, (dialog, which) -> {
68                     if (mConfirmRemoveDeviceListener != null) {
69                         mConfirmRemoveDeviceListener.onConfirmRemoveDevice(handle);
70                     }
71                 })
72                 .setNegativeButton(R.string.trusted_device_done_button, /* listener= */ null)
73                 .create();
74     }
75 
76     /** A listener for when user confirms removing a trusted device. */
77     public interface ConfirmRemoveDeviceListener {
78         /** Defines the actions to take when a user confirms removing the trusted device. */
79         void onConfirmRemoveDevice(long handle);
80     }
81 
82 }
83