• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 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.Dialog;
20 import android.app.settings.SettingsEnums;
21 import android.bluetooth.BluetoothAdapter;
22 import android.bluetooth.BluetoothDevice;
23 import android.content.Context;
24 import android.os.Bundle;
25 
26 import androidx.annotation.NonNull;
27 import androidx.annotation.Nullable;
28 import androidx.appcompat.app.AlertDialog;
29 
30 import com.android.settings.R;
31 import com.android.settings.core.SubSettingLauncher;
32 import com.android.settings.core.instrumentation.InstrumentedDialogFragment;
33 import com.android.settingslib.bluetooth.CachedBluetoothDevice;
34 import com.android.settingslib.bluetooth.HearingAidInfo;
35 import com.android.settingslib.bluetooth.LocalBluetoothManager;
36 
37 /**
38  * Provides a dialog to pair another side of hearing aid device.
39  */
40 public class HearingAidPairingDialogFragment extends InstrumentedDialogFragment implements
41         CachedBluetoothDevice.Callback {
42     public static final String TAG = "HearingAidPairingDialogFragment";
43     private static final String KEY_DEVICE_ADDRESS = "device_address";
44     private LocalBluetoothManager mLocalBluetoothManager;
45     private CachedBluetoothDevice mDevice;
46 
47     /**
48      * Creates a new {@link HearingAidPairingDialogFragment} and shows pair another side of hearing
49      * aid device according to {@code deviceAddress}.
50      *
51      * @param deviceAddress The remote Bluetooth device address, that needs to be a hearing aid
52      *                      device.
53      * @return a DialogFragment
54      */
newInstance(String deviceAddress)55     public static HearingAidPairingDialogFragment newInstance(String deviceAddress) {
56         Bundle args = new Bundle(1);
57         args.putString(KEY_DEVICE_ADDRESS, deviceAddress);
58         final HearingAidPairingDialogFragment fragment = new HearingAidPairingDialogFragment();
59         fragment.setArguments(args);
60         return fragment;
61     }
62 
63     @Override
onAttach(Context context)64     public void onAttach(Context context) {
65         super.onAttach(context);
66         mLocalBluetoothManager = Utils.getLocalBtManager(context);
67         mDevice = getDevice();
68         if (mDevice != null) {
69             mDevice.registerCallback(this);
70         }
71     }
72 
73     @Override
onDetach()74     public void onDetach() {
75         super.onDetach();
76         if (mDevice != null) {
77             mDevice.unregisterCallback(this);
78         }
79     }
80 
getDevice()81     private CachedBluetoothDevice getDevice() {
82         final String deviceAddress = getArguments().getString(KEY_DEVICE_ADDRESS);
83         final BluetoothDevice device = BluetoothAdapter.getDefaultAdapter().getRemoteDevice(
84                 deviceAddress);
85         return mLocalBluetoothManager.getCachedDeviceManager().findDevice(device);
86     }
87 
88     @Override
getMetricsCategory()89     public int getMetricsCategory() {
90         return SettingsEnums.DIALOG_ACCESSIBILITY_HEARING_AID_PAIR_ANOTHER;
91     }
92 
93     @NonNull
94     @Override
onCreateDialog(@ullable Bundle savedInstanceState)95     public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
96         final int deviceSide = mDevice.getDeviceSide();
97         final int titleId = R.string.bluetooth_pair_other_ear_dialog_title;
98         final int messageId = (deviceSide == HearingAidInfo.DeviceSide.SIDE_LEFT)
99                         ? R.string.bluetooth_pair_other_ear_dialog_left_ear_message
100                         : R.string.bluetooth_pair_other_ear_dialog_right_ear_message;
101         final int pairBtnId = (deviceSide == HearingAidInfo.DeviceSide.SIDE_LEFT)
102                         ? R.string.bluetooth_pair_other_ear_dialog_right_ear_positive_button
103                         : R.string.bluetooth_pair_other_ear_dialog_left_ear_positive_button;
104 
105         return new AlertDialog.Builder(getActivity())
106                 .setTitle(titleId)
107                 .setMessage(messageId)
108                 .setNegativeButton(android.R.string.cancel, /* listener= */ null)
109                 .setPositiveButton(pairBtnId, (dialog, which) -> positiveButtonListener())
110                 .create();
111     }
112 
positiveButtonListener()113     private void positiveButtonListener() {
114         new SubSettingLauncher(getActivity())
115                 .setDestination(BluetoothPairingDetail.class.getName())
116                 .setSourceMetricsCategory(getMetricsCategory())
117                 .launch();
118     }
119 
120     @Override
onDeviceAttributesChanged()121     public void onDeviceAttributesChanged() {
122         final CachedBluetoothDevice subDevice = mDevice.getSubDevice();
123         if (subDevice != null && subDevice.isConnectedAshaHearingAidDevice()) {
124             this.dismiss();
125         }
126     }
127 }
128