1 /*
2  * Copyright (C) 2017 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.bluetooth;
18 
19 import android.app.Activity;
20 import android.app.Dialog;
21 import android.app.settings.SettingsEnums;
22 import android.bluetooth.BluetoothDevice;
23 import android.content.Context;
24 import android.content.DialogInterface;
25 import android.os.Bundle;
26 import android.util.Log;
27 
28 import androidx.annotation.VisibleForTesting;
29 import androidx.appcompat.app.AlertDialog;
30 
31 import com.android.settings.R;
32 import com.android.settings.core.instrumentation.InstrumentedDialogFragment;
33 import com.android.settingslib.bluetooth.CachedBluetoothDevice;
34 import com.android.settingslib.bluetooth.LocalBluetoothManager;
35 
36 /** Implements an AlertDialog for confirming that a user wishes to unpair or "forget" a paired
37  *  device*/
38 public class ForgetDeviceDialogFragment extends InstrumentedDialogFragment {
39     public static final String TAG = "ForgetBluetoothDevice";
40     private static final String KEY_DEVICE_ADDRESS = "device_address";
41 
42     private CachedBluetoothDevice mDevice;
43 
newInstance(String deviceAddress)44     public static ForgetDeviceDialogFragment newInstance(String deviceAddress) {
45         Bundle args = new Bundle(1);
46         args.putString(KEY_DEVICE_ADDRESS, deviceAddress);
47         ForgetDeviceDialogFragment dialog = new ForgetDeviceDialogFragment();
48         dialog.setArguments(args);
49         return dialog;
50     }
51 
52     @VisibleForTesting
getDevice(Context context)53     CachedBluetoothDevice getDevice(Context context) {
54         String deviceAddress = getArguments().getString(KEY_DEVICE_ADDRESS);
55         LocalBluetoothManager manager = Utils.getLocalBtManager(context);
56         BluetoothDevice device = manager.getBluetoothAdapter().getRemoteDevice(deviceAddress);
57         return manager.getCachedDeviceManager().findDevice(device);
58     }
59 
60     @Override
getMetricsCategory()61     public int getMetricsCategory() {
62         return SettingsEnums.DIALOG_BLUETOOTH_PAIRED_DEVICE_FORGET;
63     }
64 
65     @Override
onCreateDialog(Bundle inState)66     public Dialog onCreateDialog(Bundle inState) {
67         Context context = getContext();
68         mDevice = getDevice(context);
69         if (mDevice == null) {
70             Log.e(TAG, "onCreateDialog: Device is null.");
71             return null;
72         }
73 
74         DialogInterface.OnClickListener onConfirm = (dialog, which) -> {
75             mDevice.unpair();
76             Activity activity = getActivity();
77             if (activity != null) {
78                 activity.finish();
79             }
80         };
81         AlertDialog dialog = new AlertDialog.Builder(context)
82                 .setPositiveButton(R.string.bluetooth_unpair_dialog_forget_confirm_button,
83                         onConfirm)
84                 .setNegativeButton(android.R.string.cancel, null)
85                 .create();
86         dialog.setTitle(R.string.bluetooth_unpair_dialog_title);
87         dialog.setMessage(context.getString(R.string.bluetooth_unpair_dialog_body,
88                 mDevice.getName()));
89         return dialog;
90     }
91 }
92