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.BluetoothUtils; 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 DialogInterface.OnClickListener onConfirm = (dialog, which) -> { 68 mDevice.unpair(); 69 Activity activity = getActivity(); 70 if (activity != null) { 71 activity.finish(); 72 } 73 }; 74 Context context = getContext(); 75 mDevice = getDevice(context); 76 final boolean untetheredHeadset = BluetoothUtils.getBooleanMetaData( 77 mDevice.getDevice(), BluetoothDevice.METADATA_IS_UNTETHERED_HEADSET); 78 79 AlertDialog dialog = new AlertDialog.Builder(context) 80 .setPositiveButton(R.string.bluetooth_unpair_dialog_forget_confirm_button, 81 onConfirm) 82 .setNegativeButton(android.R.string.cancel, null) 83 .create(); 84 dialog.setTitle(R.string.bluetooth_unpair_dialog_title); 85 dialog.setMessage(context.getString(untetheredHeadset 86 ? R.string.bluetooth_untethered_unpair_dialog_body 87 : R.string.bluetooth_unpair_dialog_body, 88 mDevice.getName())); 89 return dialog; 90 } 91 } 92