1 /* 2 * Copyright (C) 2024 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 package com.android.settings.bluetooth; 17 18 import android.app.Dialog; 19 import android.app.settings.SettingsEnums; 20 import android.bluetooth.BluetoothDevice; 21 import android.content.DialogInterface; 22 import android.content.DialogInterface.OnClickListener; 23 import android.os.Bundle; 24 import android.util.Log; 25 import android.view.View; 26 import android.widget.TextView; 27 28 import androidx.annotation.NonNull; 29 import androidx.annotation.Nullable; 30 import androidx.appcompat.app.AlertDialog; 31 32 import com.android.settings.R; 33 import com.android.settings.core.SubSettingLauncher; 34 import com.android.settings.core.instrumentation.InstrumentedDialogFragment; 35 import com.android.settingslib.bluetooth.LocalBluetoothManager; 36 37 /** 38 * A dialogFragment used by {@link BluetoothKeyMissingDialog} to create a dialog for the bluetooth 39 * device. 40 */ 41 public class BluetoothKeyMissingDialogFragment extends InstrumentedDialogFragment 42 implements OnClickListener { 43 44 private static final String TAG = "BTKeyMissingDialogFrg"; 45 private static final String KEY_CACHED_DEVICE_ADDRESS = "cached_device"; 46 47 private BluetoothDevice mBluetoothDevice; 48 49 /** Creates a new instant of the fragment. */ newInstance(BluetoothDevice device)50 public static BluetoothKeyMissingDialogFragment newInstance(BluetoothDevice device) { 51 Bundle args = new Bundle(1); 52 args.putString(KEY_CACHED_DEVICE_ADDRESS, device.getAddress()); 53 BluetoothKeyMissingDialogFragment fragment = new BluetoothKeyMissingDialogFragment(); 54 fragment.setArguments(args); 55 return fragment; 56 } 57 58 @NonNull 59 @Override onCreateDialog(@ullable Bundle savedInstanceState)60 public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) { 61 String deviceAddress = getArguments().getString(KEY_CACHED_DEVICE_ADDRESS); 62 LocalBluetoothManager manager = Utils.getLocalBtManager(getContext()); 63 mBluetoothDevice = manager.getBluetoothAdapter().getRemoteDevice(deviceAddress); 64 65 AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 66 View view = getActivity().getLayoutInflater().inflate(R.layout.bluetooth_key_missing, null); 67 TextView keyMissingTitle = view.findViewById(R.id.bluetooth_key_missing_title); 68 keyMissingTitle.setText( 69 getString(R.string.bluetooth_key_missing_title, mBluetoothDevice.getAlias())); 70 builder.setView(view); 71 builder.setPositiveButton(getString(R.string.bluetooth_key_missing_device_settings), this); 72 builder.setNegativeButton(getString(R.string.bluetooth_key_missing_close), this); 73 AlertDialog dialog = builder.create(); 74 dialog.setCanceledOnTouchOutside(false); 75 return dialog; 76 } 77 78 @Override onDestroy()79 public void onDestroy() { 80 super.onDestroy(); 81 if (!getActivity().isChangingConfigurations() && !getActivity().isFinishing()) { 82 getActivity().finish(); 83 } 84 } 85 86 @Override onClick(DialogInterface dialog, int which)87 public void onClick(DialogInterface dialog, int which) { 88 if (which == DialogInterface.BUTTON_POSITIVE) { 89 Log.i(TAG, "Positive button clicked for " + mBluetoothDevice.getAnonymizedAddress()); 90 Bundle args = new Bundle(); 91 args.putString( 92 BluetoothDeviceDetailsFragment.KEY_DEVICE_ADDRESS, 93 mBluetoothDevice.getAddress()); 94 95 new SubSettingLauncher(requireContext()) 96 .setDestination(BluetoothDeviceDetailsFragment.class.getName()) 97 .setArguments(args) 98 .setTitleRes(R.string.device_details_title) 99 .setSourceMetricsCategory(getMetricsCategory()) 100 .launch(); 101 } else if (which == DialogInterface.BUTTON_NEGATIVE) { 102 Log.i(TAG, "Negative button clicked for " + mBluetoothDevice.getAnonymizedAddress()); 103 } 104 if (!getActivity().isFinishing()) { 105 getActivity().finish(); 106 } 107 } 108 109 @Override getMetricsCategory()110 public int getMetricsCategory() { 111 return SettingsEnums.BLUETOOTH_KEY_MISSING_DIALOG_FRAGMENT; 112 } 113 } 114