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