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