The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 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 | |
Fan Zhang | c7162cd | 2018-06-18 15:21:41 -0700 | [diff] [blame] | 19 | import static android.os.UserManager.DISALLOW_CONFIG_BLUETOOTH; |
| 20 | |
Fan Zhang | 31b2100 | 2019-01-16 13:49:47 -0800 | [diff] [blame] | 21 | import android.app.settings.SettingsEnums; |
Jake Hamby | 436b29e | 2011-02-07 18:21:25 -0800 | [diff] [blame] | 22 | import android.bluetooth.BluetoothDevice; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 23 | import android.content.Context; |
Jake Hamby | 436b29e | 2011-02-07 18:21:25 -0800 | [diff] [blame] | 24 | import android.content.DialogInterface; |
pkanwar | 8fa3d44 | 2016-04-29 17:43:24 -0700 | [diff] [blame] | 25 | import android.content.res.Resources; |
jackqdyulei | 7661137 | 2017-08-09 09:57:27 -0700 | [diff] [blame] | 26 | import android.graphics.drawable.Drawable; |
Maggie Benthall | 11b69bf | 2013-04-08 16:05:38 -0400 | [diff] [blame] | 27 | import android.os.UserManager; |
Jake Hamby | ca9812a | 2011-07-12 11:05:46 -0700 | [diff] [blame] | 28 | import android.text.Html; |
Jake Hamby | 436b29e | 2011-02-07 18:21:25 -0800 | [diff] [blame] | 29 | import android.text.TextUtils; |
pkanwar | 8fa3d44 | 2016-04-29 17:43:24 -0700 | [diff] [blame] | 30 | import android.util.Pair; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 31 | import android.util.TypedValue; |
timpeng | 0dde3a3 | 2019-04-12 16:53:56 +0800 | [diff] [blame] | 32 | import android.view.View; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 33 | import android.widget.ImageView; |
Jake Hamby | c090feb | 2010-12-07 16:27:21 -0800 | [diff] [blame] | 34 | |
hughchen | 14e0fe2 | 2019-07-23 16:18:15 +0800 | [diff] [blame] | 35 | import androidx.annotation.IntDef; |
jackqdyulei | 36948ce | 2019-03-29 15:00:07 -0700 | [diff] [blame] | 36 | import androidx.annotation.VisibleForTesting; |
Fan Zhang | 23f8d59 | 2018-08-28 15:11:40 -0700 | [diff] [blame] | 37 | import androidx.appcompat.app.AlertDialog; |
| 38 | import androidx.preference.Preference; |
| 39 | import androidx.preference.PreferenceViewHolder; |
| 40 | |
Jake Hamby | 436b29e | 2011-02-07 18:21:25 -0800 | [diff] [blame] | 41 | import com.android.settings.R; |
Doris Ling | 133b096 | 2017-03-09 17:35:22 -0800 | [diff] [blame] | 42 | import com.android.settings.overlay.FeatureFactory; |
Fan Zhang | 00d8ff5 | 2017-04-10 16:26:42 -0700 | [diff] [blame] | 43 | import com.android.settings.widget.GearPreference; |
hughchen | 9d4b634 | 2019-03-25 18:06:21 +0800 | [diff] [blame] | 44 | import com.android.settingslib.bluetooth.BluetoothUtils; |
Fan Zhang | c7162cd | 2018-06-18 15:21:41 -0700 | [diff] [blame] | 45 | import com.android.settingslib.bluetooth.CachedBluetoothDevice; |
Leif Hendrik Wilden | 28dee1f | 2018-01-11 10:15:36 -0800 | [diff] [blame] | 46 | import com.android.settingslib.core.instrumentation.MetricsFeatureProvider; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 47 | |
hughchen | 14e0fe2 | 2019-07-23 16:18:15 +0800 | [diff] [blame] | 48 | import java.lang.annotation.Retention; |
| 49 | import java.lang.annotation.RetentionPolicy; |
| 50 | |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 51 | /** |
| 52 | * BluetoothDevicePreference is the preference type used to display each remote |
| 53 | * Bluetooth device in the Bluetooth Settings screen. |
| 54 | */ |
Fan Zhang | 00d8ff5 | 2017-04-10 16:26:42 -0700 | [diff] [blame] | 55 | public final class BluetoothDevicePreference extends GearPreference implements |
| 56 | CachedBluetoothDevice.Callback { |
| 57 | private static final String TAG = "BluetoothDevicePref"; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 58 | |
| 59 | private static int sDimAlpha = Integer.MIN_VALUE; |
Michael Chan | e6531e2 | 2009-09-30 14:26:10 -0700 | [diff] [blame] | 60 | |
hughchen | 14e0fe2 | 2019-07-23 16:18:15 +0800 | [diff] [blame] | 61 | @Retention(RetentionPolicy.SOURCE) |
| 62 | @IntDef({SortType.TYPE_DEFAULT, |
hughchen | 043e43f | 2020-02-03 13:59:06 +0800 | [diff] [blame] | 63 | SortType.TYPE_FIFO, |
| 64 | SortType.TYPE_NO_SORT}) |
hughchen | 14e0fe2 | 2019-07-23 16:18:15 +0800 | [diff] [blame] | 65 | public @interface SortType { |
| 66 | int TYPE_DEFAULT = 1; |
| 67 | int TYPE_FIFO = 2; |
hughchen | 043e43f | 2020-02-03 13:59:06 +0800 | [diff] [blame] | 68 | int TYPE_NO_SORT = 3; |
hughchen | 14e0fe2 | 2019-07-23 16:18:15 +0800 | [diff] [blame] | 69 | } |
| 70 | |
Jake Hamby | 39ef225 | 2010-12-16 18:33:28 -0800 | [diff] [blame] | 71 | private final CachedBluetoothDevice mCachedDevice; |
Fan Zhang | 00d8ff5 | 2017-04-10 16:26:42 -0700 | [diff] [blame] | 72 | private final UserManager mUserManager; |
jackqdyulei | cf1ce05 | 2017-11-16 16:06:14 -0800 | [diff] [blame] | 73 | private final boolean mShowDevicesWithoutNames; |
hughchen | 14e0fe2 | 2019-07-23 16:18:15 +0800 | [diff] [blame] | 74 | private final long mCurrentTime; |
| 75 | private final int mType; |
Amith Yamasani | 48e9000 | 2010-10-26 13:44:33 -0700 | [diff] [blame] | 76 | |
Jake Hamby | 436b29e | 2011-02-07 18:21:25 -0800 | [diff] [blame] | 77 | private AlertDialog mDisconnectDialog; |
pkanwar | 8fa3d44 | 2016-04-29 17:43:24 -0700 | [diff] [blame] | 78 | private String contentDescription = null; |
hughchen | 7ef4fd8 | 2018-04-24 15:46:30 +0800 | [diff] [blame] | 79 | private boolean mHideSecondTarget = false; |
jackqdyulei | 36948ce | 2019-03-29 15:00:07 -0700 | [diff] [blame] | 80 | @VisibleForTesting |
| 81 | boolean mNeedNotifyHierarchyChanged = false; |
pkanwar | 8fa3d44 | 2016-04-29 17:43:24 -0700 | [diff] [blame] | 82 | /* Talk-back descriptions for various BT icons */ |
Antony Sargent | 04a3b21 | 2017-05-04 15:06:32 -0700 | [diff] [blame] | 83 | Resources mResources; |
pkanwar | 8fa3d44 | 2016-04-29 17:43:24 -0700 | [diff] [blame] | 84 | |
Jack He | 19ba320 | 2017-05-31 18:37:28 -0700 | [diff] [blame] | 85 | public BluetoothDevicePreference(Context context, CachedBluetoothDevice cachedDevice, |
hughchen | 14e0fe2 | 2019-07-23 16:18:15 +0800 | [diff] [blame] | 86 | boolean showDeviceWithoutNames, @SortType int type) { |
Fan Zhang | 00d8ff5 | 2017-04-10 16:26:42 -0700 | [diff] [blame] | 87 | super(context, null); |
Antony Sargent | 04a3b21 | 2017-05-04 15:06:32 -0700 | [diff] [blame] | 88 | mResources = getContext().getResources(); |
Fan Zhang | 00d8ff5 | 2017-04-10 16:26:42 -0700 | [diff] [blame] | 89 | mUserManager = (UserManager) context.getSystemService(Context.USER_SERVICE); |
jackqdyulei | cf1ce05 | 2017-11-16 16:06:14 -0800 | [diff] [blame] | 90 | mShowDevicesWithoutNames = showDeviceWithoutNames; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 91 | |
| 92 | if (sDimAlpha == Integer.MIN_VALUE) { |
| 93 | TypedValue outValue = new TypedValue(); |
| 94 | context.getTheme().resolveAttribute(android.R.attr.disabledAlpha, outValue, true); |
Jake Hamby | 436b29e | 2011-02-07 18:21:25 -0800 | [diff] [blame] | 95 | sDimAlpha = (int) (outValue.getFloat() * 255); |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 96 | } |
Michael Chan | e6531e2 | 2009-09-30 14:26:10 -0700 | [diff] [blame] | 97 | |
Nick Pelly | d63c011 | 2009-08-14 18:43:18 -0700 | [diff] [blame] | 98 | mCachedDevice = cachedDevice; |
hughchen | 14e0fe2 | 2019-07-23 16:18:15 +0800 | [diff] [blame] | 99 | mCurrentTime = System.currentTimeMillis(); |
| 100 | mType = type; |
Michael Chan | e6531e2 | 2009-09-30 14:26:10 -0700 | [diff] [blame] | 101 | |
Jake Hamby | c090feb | 2010-12-07 16:27:21 -0800 | [diff] [blame] | 102 | onDeviceAttributesChanged(); |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 103 | } |
Michael Chan | e6531e2 | 2009-09-30 14:26:10 -0700 | [diff] [blame] | 104 | |
jackqdyulei | 36948ce | 2019-03-29 15:00:07 -0700 | [diff] [blame] | 105 | public void setNeedNotifyHierarchyChanged(boolean needNotifyHierarchyChanged) { |
| 106 | mNeedNotifyHierarchyChanged = needNotifyHierarchyChanged; |
Jason Monk | 2071eda | 2016-02-25 13:55:48 -0500 | [diff] [blame] | 107 | } |
| 108 | |
Fan Zhang | 00d8ff5 | 2017-04-10 16:26:42 -0700 | [diff] [blame] | 109 | @Override |
| 110 | protected boolean shouldHideSecondTarget() { |
| 111 | return mCachedDevice == null |
| 112 | || mCachedDevice.getBondState() != BluetoothDevice.BOND_BONDED |
hughchen | 7ef4fd8 | 2018-04-24 15:46:30 +0800 | [diff] [blame] | 113 | || mUserManager.hasUserRestriction(DISALLOW_CONFIG_BLUETOOTH) |
| 114 | || mHideSecondTarget; |
Amith Yamasani | 48e9000 | 2010-10-26 13:44:33 -0700 | [diff] [blame] | 115 | } |
| 116 | |
Fan Zhang | 00d8ff5 | 2017-04-10 16:26:42 -0700 | [diff] [blame] | 117 | @Override |
| 118 | protected int getSecondTargetResId() { |
| 119 | return R.layout.preference_widget_gear; |
| 120 | } |
| 121 | |
| 122 | CachedBluetoothDevice getCachedDevice() { |
| 123 | return mCachedDevice; |
Doris Ling | 457c3cb | 2017-03-28 19:53:31 +0000 | [diff] [blame] | 124 | } |
| 125 | |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 126 | @Override |
| 127 | protected void onPrepareForRemoval() { |
| 128 | super.onPrepareForRemoval(); |
Jake Hamby | 436b29e | 2011-02-07 18:21:25 -0800 | [diff] [blame] | 129 | if (mDisconnectDialog != null) { |
| 130 | mDisconnectDialog.dismiss(); |
| 131 | mDisconnectDialog = null; |
| 132 | } |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 133 | } |
| 134 | |
Hugh Chen | 194dd88 | 2020-09-02 17:00:13 +0800 | [diff] [blame^] | 135 | @Override |
| 136 | public void onAttached() { |
| 137 | super.onAttached(); |
| 138 | mCachedDevice.registerCallback(this); |
| 139 | } |
| 140 | |
| 141 | @Override |
| 142 | public void onDetached() { |
| 143 | super.onDetached(); |
| 144 | mCachedDevice.unregisterCallback(this); |
| 145 | } |
| 146 | |
Fan Zhang | 00d8ff5 | 2017-04-10 16:26:42 -0700 | [diff] [blame] | 147 | public CachedBluetoothDevice getBluetoothDevice() { |
| 148 | return mCachedDevice; |
| 149 | } |
| 150 | |
hughchen | 7ef4fd8 | 2018-04-24 15:46:30 +0800 | [diff] [blame] | 151 | public void hideSecondTarget(boolean hideSecondTarget) { |
| 152 | mHideSecondTarget = hideSecondTarget; |
| 153 | } |
| 154 | |
Jake Hamby | c090feb | 2010-12-07 16:27:21 -0800 | [diff] [blame] | 155 | public void onDeviceAttributesChanged() { |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 156 | /* |
| 157 | * The preference framework takes care of making sure the value has |
Jake Hamby | 436b29e | 2011-02-07 18:21:25 -0800 | [diff] [blame] | 158 | * changed before proceeding. It will also call notifyChanged() if |
| 159 | * any preference info has changed from the previous value. |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 160 | */ |
Nick Pelly | d63c011 | 2009-08-14 18:43:18 -0700 | [diff] [blame] | 161 | setTitle(mCachedDevice.getName()); |
Jack He | 04f5967 | 2017-06-28 12:08:55 -0700 | [diff] [blame] | 162 | // Null check is done at the framework |
| 163 | setSummary(mCachedDevice.getConnectionSummary()); |
pkanwar | 8fa3d44 | 2016-04-29 17:43:24 -0700 | [diff] [blame] | 164 | |
hughchen | 9d4b634 | 2019-03-25 18:06:21 +0800 | [diff] [blame] | 165 | final Pair<Drawable, String> pair = |
| 166 | BluetoothUtils.getBtRainbowDrawableWithDescription(getContext(), mCachedDevice); |
jackqdyulei | 7661137 | 2017-08-09 09:57:27 -0700 | [diff] [blame] | 167 | if (pair.first != null) { |
pkanwar | 8fa3d44 | 2016-04-29 17:43:24 -0700 | [diff] [blame] | 168 | setIcon(pair.first); |
| 169 | contentDescription = pair.second; |
Jake Hamby | 79be0b3 | 2011-08-01 16:36:35 -0700 | [diff] [blame] | 170 | } |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 171 | |
| 172 | // Used to gray out the item |
Jake Hamby | 436b29e | 2011-02-07 18:21:25 -0800 | [diff] [blame] | 173 | setEnabled(!mCachedDevice.isBusy()); |
Michael Chan | e6531e2 | 2009-09-30 14:26:10 -0700 | [diff] [blame] | 174 | |
Jack He | 19ba320 | 2017-05-31 18:37:28 -0700 | [diff] [blame] | 175 | // Device is only visible in the UI if it has a valid name besides MAC address or when user |
| 176 | // allows showing devices without user-friendly name in developer settings |
jackqdyulei | cf1ce05 | 2017-11-16 16:06:14 -0800 | [diff] [blame] | 177 | setVisible(mShowDevicesWithoutNames || mCachedDevice.hasHumanReadableName()); |
Jack He | 19ba320 | 2017-05-31 18:37:28 -0700 | [diff] [blame] | 178 | |
Jake Hamby | 436b29e | 2011-02-07 18:21:25 -0800 | [diff] [blame] | 179 | // This could affect ordering, so notify that |
jackqdyulei | 36948ce | 2019-03-29 15:00:07 -0700 | [diff] [blame] | 180 | if (mNeedNotifyHierarchyChanged) { |
| 181 | notifyHierarchyChanged(); |
| 182 | } |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | @Override |
Jason Monk | 39b4674 | 2015-09-10 15:52:51 -0400 | [diff] [blame] | 186 | public void onBindViewHolder(PreferenceViewHolder view) { |
Michael Chan | 132b214 | 2009-07-01 14:06:43 -0700 | [diff] [blame] | 187 | // Disable this view if the bluetooth enable/disable preference view is off |
Jake Hamby | c090feb | 2010-12-07 16:27:21 -0800 | [diff] [blame] | 188 | if (null != findPreferenceInHierarchy("bt_checkbox")) { |
Yue Lixin | a41e2f9 | 2009-07-09 16:48:52 +0800 | [diff] [blame] | 189 | setDependency("bt_checkbox"); |
| 190 | } |
Michael Chan | 132b214 | 2009-07-01 14:06:43 -0700 | [diff] [blame] | 191 | |
Jake Hamby | 79be0b3 | 2011-08-01 16:36:35 -0700 | [diff] [blame] | 192 | if (mCachedDevice.getBondState() == BluetoothDevice.BOND_BONDED) { |
Fan Zhang | 00d8ff5 | 2017-04-10 16:26:42 -0700 | [diff] [blame] | 193 | ImageView deviceDetails = (ImageView) view.findViewById(R.id.settings_button); |
PauloftheWest | da43135 | 2014-06-18 12:00:31 -0700 | [diff] [blame] | 194 | |
Jake Hamby | 79be0b3 | 2011-08-01 16:36:35 -0700 | [diff] [blame] | 195 | if (deviceDetails != null) { |
| 196 | deviceDetails.setOnClickListener(this); |
Jake Hamby | 79be0b3 | 2011-08-01 16:36:35 -0700 | [diff] [blame] | 197 | } |
| 198 | } |
pkanwar | 8fa3d44 | 2016-04-29 17:43:24 -0700 | [diff] [blame] | 199 | final ImageView imageView = (ImageView) view.findViewById(android.R.id.icon); |
| 200 | if (imageView != null) { |
| 201 | imageView.setContentDescription(contentDescription); |
timpeng | 0dde3a3 | 2019-04-12 16:53:56 +0800 | [diff] [blame] | 202 | // Set property to prevent Talkback from reading out. |
| 203 | imageView.setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_NO); |
jackqdyulei | 36ce63d | 2019-03-19 10:18:19 -0700 | [diff] [blame] | 204 | imageView.setElevation( |
| 205 | getContext().getResources().getDimension(R.dimen.bt_icon_elevation)); |
pkanwar | 8fa3d44 | 2016-04-29 17:43:24 -0700 | [diff] [blame] | 206 | } |
Jason Monk | 39b4674 | 2015-09-10 15:52:51 -0400 | [diff] [blame] | 207 | super.onBindViewHolder(view); |
Amith Yamasani | 48e9000 | 2010-10-26 13:44:33 -0700 | [diff] [blame] | 208 | } |
| 209 | |
Gilles Debunne | 4346cda | 2011-06-28 14:01:50 -0700 | [diff] [blame] | 210 | @Override |
Jake Hamby | 436b29e | 2011-02-07 18:21:25 -0800 | [diff] [blame] | 211 | public boolean equals(Object o) { |
| 212 | if ((o == null) || !(o instanceof BluetoothDevicePreference)) { |
| 213 | return false; |
| 214 | } |
| 215 | return mCachedDevice.equals( |
| 216 | ((BluetoothDevicePreference) o).mCachedDevice); |
| 217 | } |
| 218 | |
Gilles Debunne | 4346cda | 2011-06-28 14:01:50 -0700 | [diff] [blame] | 219 | @Override |
Jake Hamby | 436b29e | 2011-02-07 18:21:25 -0800 | [diff] [blame] | 220 | public int hashCode() { |
| 221 | return mCachedDevice.hashCode(); |
| 222 | } |
| 223 | |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 224 | @Override |
| 225 | public int compareTo(Preference another) { |
| 226 | if (!(another instanceof BluetoothDevicePreference)) { |
Gilles Debunne | 4346cda | 2011-06-28 14:01:50 -0700 | [diff] [blame] | 227 | // Rely on default sort |
| 228 | return super.compareTo(another); |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 229 | } |
Michael Chan | e6531e2 | 2009-09-30 14:26:10 -0700 | [diff] [blame] | 230 | |
hughchen | 14e0fe2 | 2019-07-23 16:18:15 +0800 | [diff] [blame] | 231 | switch (mType) { |
| 232 | case SortType.TYPE_DEFAULT: |
| 233 | return mCachedDevice |
| 234 | .compareTo(((BluetoothDevicePreference) another).mCachedDevice); |
| 235 | case SortType.TYPE_FIFO: |
hughchen | 41eab2c | 2019-08-14 16:27:12 +0800 | [diff] [blame] | 236 | return mCurrentTime > ((BluetoothDevicePreference) another).mCurrentTime ? 1 : -1; |
hughchen | 14e0fe2 | 2019-07-23 16:18:15 +0800 | [diff] [blame] | 237 | default: |
| 238 | return super.compareTo(another); |
| 239 | } |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 240 | } |
Michael Chan | e6531e2 | 2009-09-30 14:26:10 -0700 | [diff] [blame] | 241 | |
Jake Hamby | 436b29e | 2011-02-07 18:21:25 -0800 | [diff] [blame] | 242 | void onClicked() { |
Jack He | 5b017f7 | 2017-08-30 19:18:40 -0700 | [diff] [blame] | 243 | Context context = getContext(); |
Jake Hamby | 436b29e | 2011-02-07 18:21:25 -0800 | [diff] [blame] | 244 | int bondState = mCachedDevice.getBondState(); |
| 245 | |
Doris Ling | 133b096 | 2017-03-09 17:35:22 -0800 | [diff] [blame] | 246 | final MetricsFeatureProvider metricsFeatureProvider = |
Jack He | 5b017f7 | 2017-08-30 19:18:40 -0700 | [diff] [blame] | 247 | FeatureFactory.getFactory(context).getMetricsFeatureProvider(); |
Doris Ling | 133b096 | 2017-03-09 17:35:22 -0800 | [diff] [blame] | 248 | |
Jake Hamby | 436b29e | 2011-02-07 18:21:25 -0800 | [diff] [blame] | 249 | if (mCachedDevice.isConnected()) { |
Jack He | 5b017f7 | 2017-08-30 19:18:40 -0700 | [diff] [blame] | 250 | metricsFeatureProvider.action(context, |
Fan Zhang | 31b2100 | 2019-01-16 13:49:47 -0800 | [diff] [blame] | 251 | SettingsEnums.ACTION_SETTINGS_BLUETOOTH_DISCONNECT); |
Paul West | c0963dd | 2014-08-27 20:31:38 +0000 | [diff] [blame] | 252 | askDisconnect(); |
Jake Hamby | 436b29e | 2011-02-07 18:21:25 -0800 | [diff] [blame] | 253 | } else if (bondState == BluetoothDevice.BOND_BONDED) { |
Jack He | 5b017f7 | 2017-08-30 19:18:40 -0700 | [diff] [blame] | 254 | metricsFeatureProvider.action(context, |
Fan Zhang | 31b2100 | 2019-01-16 13:49:47 -0800 | [diff] [blame] | 255 | SettingsEnums.ACTION_SETTINGS_BLUETOOTH_CONNECT); |
hughchen | 0ab3283 | 2020-01-14 16:07:59 +0800 | [diff] [blame] | 256 | mCachedDevice.connect(); |
Jake Hamby | 436b29e | 2011-02-07 18:21:25 -0800 | [diff] [blame] | 257 | } else if (bondState == BluetoothDevice.BOND_NONE) { |
Jack He | 5b017f7 | 2017-08-30 19:18:40 -0700 | [diff] [blame] | 258 | metricsFeatureProvider.action(context, |
Fan Zhang | 31b2100 | 2019-01-16 13:49:47 -0800 | [diff] [blame] | 259 | SettingsEnums.ACTION_SETTINGS_BLUETOOTH_PAIR); |
Jack He | 5b017f7 | 2017-08-30 19:18:40 -0700 | [diff] [blame] | 260 | if (!mCachedDevice.hasHumanReadableName()) { |
| 261 | metricsFeatureProvider.action(context, |
Fan Zhang | 31b2100 | 2019-01-16 13:49:47 -0800 | [diff] [blame] | 262 | SettingsEnums.ACTION_SETTINGS_BLUETOOTH_PAIR_DEVICES_WITHOUT_NAMES); |
Jack He | 5b017f7 | 2017-08-30 19:18:40 -0700 | [diff] [blame] | 263 | } |
Jake Hamby | 436b29e | 2011-02-07 18:21:25 -0800 | [diff] [blame] | 264 | pair(); |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | // Show disconnect confirmation dialog for a device. |
| 269 | private void askDisconnect() { |
| 270 | Context context = getContext(); |
| 271 | String name = mCachedDevice.getName(); |
| 272 | if (TextUtils.isEmpty(name)) { |
| 273 | name = context.getString(R.string.bluetooth_device); |
| 274 | } |
Jake Hamby | ca9812a | 2011-07-12 11:05:46 -0700 | [diff] [blame] | 275 | String message = context.getString(R.string.bluetooth_disconnect_all_profiles, name); |
| 276 | String title = context.getString(R.string.bluetooth_disconnect_title); |
Jake Hamby | 436b29e | 2011-02-07 18:21:25 -0800 | [diff] [blame] | 277 | |
| 278 | DialogInterface.OnClickListener disconnectListener = new DialogInterface.OnClickListener() { |
| 279 | public void onClick(DialogInterface dialog, int which) { |
| 280 | mCachedDevice.disconnect(); |
| 281 | } |
| 282 | }; |
| 283 | |
| 284 | mDisconnectDialog = Utils.showDisconnectDialog(context, |
Jake Hamby | ca9812a | 2011-07-12 11:05:46 -0700 | [diff] [blame] | 285 | mDisconnectDialog, disconnectListener, title, Html.fromHtml(message)); |
Jake Hamby | 436b29e | 2011-02-07 18:21:25 -0800 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | private void pair() { |
| 289 | if (!mCachedDevice.startPairing()) { |
| 290 | Utils.showError(getContext(), mCachedDevice.getName(), |
| 291 | R.string.bluetooth_pairing_error_message); |
| 292 | } |
| 293 | } |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 294 | } |