John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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.systemui.qs.tiles; |
| 18 | |
Jason Monk | 3fd0b14 | 2017-08-30 18:11:21 -0400 | [diff] [blame] | 19 | import static com.android.settingslib.graph.BluetoothDeviceLayerDrawable.createLayerDrawable; |
| 20 | |
Rohan Shah | d5df175 | 2018-01-19 10:45:09 -0800 | [diff] [blame] | 21 | import android.annotation.Nullable; |
Jason Monk | fac2538 | 2016-07-19 14:13:37 -0400 | [diff] [blame] | 22 | import android.bluetooth.BluetoothAdapter; |
Rohan Shah | d5df175 | 2018-01-19 10:45:09 -0800 | [diff] [blame] | 23 | import android.bluetooth.BluetoothClass; |
Jason Monk | be3c5db | 2015-02-04 13:00:55 -0500 | [diff] [blame] | 24 | import android.bluetooth.BluetoothDevice; |
| 25 | import android.bluetooth.BluetoothProfile; |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 26 | import android.content.Context; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 27 | import android.content.Intent; |
Evan Laird | 4673c38 | 2017-09-27 13:00:50 -0400 | [diff] [blame] | 28 | import android.graphics.drawable.Drawable; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 29 | import android.provider.Settings; |
Jason Monk | 3250885 | 2017-01-18 09:17:13 -0500 | [diff] [blame] | 30 | import android.service.quicksettings.Tile; |
Amin Shaikh | 3468e5a | 2018-04-18 11:39:07 -0400 | [diff] [blame] | 31 | import android.text.TextUtils; |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 32 | import android.view.View; |
| 33 | import android.view.ViewGroup; |
Julia Reynolds | 20aef8a | 2016-05-04 16:44:08 -0400 | [diff] [blame] | 34 | import android.widget.Switch; |
Chris Wren | f6e9228b | 2016-01-26 18:04:35 -0500 | [diff] [blame] | 35 | |
Chris Wren | 457a21c | 2015-05-06 17:50:34 -0400 | [diff] [blame] | 36 | import com.android.internal.logging.MetricsLogger; |
Tamas Berghammer | 383db5eb | 2016-06-22 15:21:38 +0100 | [diff] [blame] | 37 | import com.android.internal.logging.nano.MetricsProto.MetricsEvent; |
Jack He | 6258aae | 2017-06-29 17:01:23 -0700 | [diff] [blame] | 38 | import com.android.settingslib.Utils; |
Jason Monk | be3c5db | 2015-02-04 13:00:55 -0500 | [diff] [blame] | 39 | import com.android.settingslib.bluetooth.CachedBluetoothDevice; |
Rohan Shah | d5df175 | 2018-01-19 10:45:09 -0800 | [diff] [blame] | 40 | import com.android.settingslib.graph.BluetoothDeviceLayerDrawable; |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 41 | import com.android.systemui.Dependency; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 42 | import com.android.systemui.R; |
Jason Monk | ec34da8 | 2017-02-24 15:57:05 -0500 | [diff] [blame] | 43 | import com.android.systemui.plugins.ActivityStarter; |
Jason Monk | e5b770e | 2017-03-03 21:49:29 -0500 | [diff] [blame] | 44 | import com.android.systemui.plugins.qs.DetailAdapter; |
Jason Monk | be3235a | 2017-04-05 09:29:53 -0400 | [diff] [blame] | 45 | import com.android.systemui.plugins.qs.QSTile.BooleanState; |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 46 | import com.android.systemui.qs.QSDetailItems; |
| 47 | import com.android.systemui.qs.QSDetailItems.Item; |
Jason Monk | 702e2eb | 2017-03-03 16:53:44 -0500 | [diff] [blame] | 48 | import com.android.systemui.qs.QSHost; |
Jason Monk | 702e2eb | 2017-03-03 16:53:44 -0500 | [diff] [blame] | 49 | import com.android.systemui.qs.tileimpl.QSTileImpl; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 50 | import com.android.systemui.statusbar.policy.BluetoothController; |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 51 | |
Jason Monk | c17d329 | 2016-01-30 12:49:41 -0500 | [diff] [blame] | 52 | import java.util.ArrayList; |
Jason Monk | be3c5db | 2015-02-04 13:00:55 -0500 | [diff] [blame] | 53 | import java.util.Collection; |
Amin Shaikh | 45fb7a7 | 2018-04-18 14:14:38 -0400 | [diff] [blame] | 54 | import java.util.List; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 55 | |
| 56 | /** Quick settings tile: Bluetooth **/ |
Jason Monk | 702e2eb | 2017-03-03 16:53:44 -0500 | [diff] [blame] | 57 | public class BluetoothTile extends QSTileImpl<BooleanState> { |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 58 | private static final Intent BLUETOOTH_SETTINGS = new Intent(Settings.ACTION_BLUETOOTH_SETTINGS); |
| 59 | |
| 60 | private final BluetoothController mController; |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 61 | private final BluetoothDetailAdapter mDetailAdapter; |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 62 | private final ActivityStarter mActivityStarter; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 63 | |
Jason Monk | 702e2eb | 2017-03-03 16:53:44 -0500 | [diff] [blame] | 64 | public BluetoothTile(QSHost host) { |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 65 | super(host); |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 66 | mController = Dependency.get(BluetoothController.class); |
| 67 | mActivityStarter = Dependency.get(ActivityStarter.class); |
Muyuan Li | 0e9f538 | 2016-04-27 15:51:15 -0700 | [diff] [blame] | 68 | mDetailAdapter = (BluetoothDetailAdapter) createDetailAdapter(); |
John Spurlock | ccb6b9a | 2014-05-17 15:54:40 -0400 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | @Override |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 72 | public DetailAdapter getDetailAdapter() { |
| 73 | return mDetailAdapter; |
| 74 | } |
| 75 | |
| 76 | @Override |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 77 | public BooleanState newTileState() { |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 78 | return new BooleanState(); |
| 79 | } |
| 80 | |
| 81 | @Override |
Jason Monk | 1c6116c | 2017-09-06 17:33:01 -0400 | [diff] [blame] | 82 | public void handleSetListening(boolean listening) { |
John Spurlock | ccb6b9a | 2014-05-17 15:54:40 -0400 | [diff] [blame] | 83 | if (listening) { |
Jason Monk | 8852905 | 2016-11-04 13:29:58 -0400 | [diff] [blame] | 84 | mController.addCallback(mCallback); |
John Spurlock | ccb6b9a | 2014-05-17 15:54:40 -0400 | [diff] [blame] | 85 | } else { |
Jason Monk | 8852905 | 2016-11-04 13:29:58 -0400 | [diff] [blame] | 86 | mController.removeCallback(mCallback); |
John Spurlock | ccb6b9a | 2014-05-17 15:54:40 -0400 | [diff] [blame] | 87 | } |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | @Override |
Jason Monk | 3250885 | 2017-01-18 09:17:13 -0500 | [diff] [blame] | 91 | protected void handleClick() { |
Jason Monk | dc35dcb | 2015-12-04 16:36:15 -0500 | [diff] [blame] | 92 | // Secondary clicks are header clicks, just toggle. |
Jason Monk | 3c3b650 | 2017-07-27 15:41:26 -0400 | [diff] [blame] | 93 | final boolean isEnabled = mState.value; |
Amin Shaikh | d03a743 | 2018-03-01 15:46:55 -0500 | [diff] [blame] | 94 | // Immediately enter transient enabling state when turning bluetooth on. |
| 95 | refreshState(isEnabled ? null : ARG_SHOW_TRANSIENT_ENABLING); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 96 | mController.setBluetoothEnabled(!isEnabled); |
| 97 | } |
| 98 | |
| 99 | @Override |
Jason Monk | 76c67aa | 2016-02-19 14:49:42 -0500 | [diff] [blame] | 100 | public Intent getLongClickIntent() { |
| 101 | return new Intent(Settings.ACTION_BLUETOOTH_SETTINGS); |
| 102 | } |
| 103 | |
| 104 | @Override |
Jason Monk | 3250885 | 2017-01-18 09:17:13 -0500 | [diff] [blame] | 105 | protected void handleSecondaryClick() { |
Sudheer Shanka | b6fc931 | 2016-01-27 19:59:03 +0000 | [diff] [blame] | 106 | if (!mController.canConfigBluetooth()) { |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 107 | mActivityStarter.postStartActivityDismissingKeyguard( |
| 108 | new Intent(Settings.ACTION_BLUETOOTH_SETTINGS), 0); |
Sudheer Shanka | b6fc931 | 2016-01-27 19:59:03 +0000 | [diff] [blame] | 109 | return; |
| 110 | } |
Jason Monk | 48e093e | 2016-07-15 10:37:07 -0400 | [diff] [blame] | 111 | showDetail(true); |
Jason Monk | 3c3b650 | 2017-07-27 15:41:26 -0400 | [diff] [blame] | 112 | if (!mState.value) { |
| 113 | mController.setBluetoothEnabled(true); |
| 114 | } |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | @Override |
Jason Monk | 39c98e6 | 2016-03-16 09:18:35 -0400 | [diff] [blame] | 118 | public CharSequence getTileLabel() { |
| 119 | return mContext.getString(R.string.quick_settings_bluetooth_label); |
| 120 | } |
| 121 | |
| 122 | @Override |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 123 | protected void handleUpdateState(BooleanState state, Object arg) { |
Amin Shaikh | d03a743 | 2018-03-01 15:46:55 -0500 | [diff] [blame] | 124 | final boolean transientEnabling = arg == ARG_SHOW_TRANSIENT_ENABLING; |
| 125 | final boolean enabled = transientEnabling || mController.isBluetoothEnabled(); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 126 | final boolean connected = mController.isBluetoothConnected(); |
Amin Shaikh | c548340 | 2018-04-26 13:51:54 -0400 | [diff] [blame] | 127 | final boolean connecting = mController.isBluetoothConnecting(); |
| 128 | state.isTransient = transientEnabling || connecting || |
| 129 | mController.getBluetoothState() == BluetoothAdapter.STATE_TURNING_ON; |
Jason Monk | 2db4619 | 2017-02-01 17:18:22 -0500 | [diff] [blame] | 130 | state.dualTarget = true; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 131 | state.value = enabled; |
Jason Monk | 395617f | 2017-05-05 14:07:58 -0400 | [diff] [blame] | 132 | if (state.slash == null) { |
| 133 | state.slash = new SlashState(); |
| 134 | } |
| 135 | state.slash.isSlashed = !enabled; |
Jason Monk | c17b7d9 | 2018-01-15 15:59:41 -0500 | [diff] [blame] | 136 | state.label = mContext.getString(R.string.quick_settings_bluetooth_label); |
Amin Shaikh | 3468e5a | 2018-04-18 11:39:07 -0400 | [diff] [blame] | 137 | state.secondaryLabel = TextUtils.emptyIfNull( |
Amin Shaikh | c548340 | 2018-04-26 13:51:54 -0400 | [diff] [blame] | 138 | getSecondaryLabel(enabled, connecting, connected, state.isTransient)); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 139 | if (enabled) { |
| 140 | if (connected) { |
Rohan Shah | 4321648 | 2018-02-09 11:07:29 -0800 | [diff] [blame] | 141 | state.icon = new BluetoothConnectedTileIcon(); |
Amin Shaikh | 45fb7a7 | 2018-04-18 14:14:38 -0400 | [diff] [blame] | 142 | if (!TextUtils.isEmpty(mController.getConnectedDeviceName())) { |
| 143 | state.label = mController.getConnectedDeviceName(); |
| 144 | } |
Amin Shaikh | 3468e5a | 2018-04-18 11:39:07 -0400 | [diff] [blame] | 145 | state.contentDescription = |
| 146 | mContext.getString(R.string.accessibility_bluetooth_name, state.label) |
| 147 | + ", " + state.secondaryLabel; |
Jason Monk | 110e5f6 | 2017-03-31 13:45:12 -0400 | [diff] [blame] | 148 | } else if (state.isTransient) { |
| 149 | state.icon = ResourceIcon.get(R.drawable.ic_bluetooth_transient_animation); |
Amin Shaikh | 3468e5a | 2018-04-18 11:39:07 -0400 | [diff] [blame] | 150 | state.contentDescription = state.secondaryLabel; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 151 | } else { |
John Spurlock | 2d69581 | 2014-10-30 13:25:21 -0400 | [diff] [blame] | 152 | state.icon = ResourceIcon.get(R.drawable.ic_qs_bluetooth_on); |
Selim Cinek | 4fda7b2 | 2014-08-18 22:07:25 +0200 | [diff] [blame] | 153 | state.contentDescription = mContext.getString( |
Amin Shaikh | 3468e5a | 2018-04-18 11:39:07 -0400 | [diff] [blame] | 154 | R.string.accessibility_quick_settings_bluetooth) + "," |
Julia Reynolds | 20aef8a | 2016-05-04 16:44:08 -0400 | [diff] [blame] | 155 | + mContext.getString(R.string.accessibility_not_connected); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 156 | } |
Jason Monk | 3250885 | 2017-01-18 09:17:13 -0500 | [diff] [blame] | 157 | state.state = Tile.STATE_ACTIVE; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 158 | } else { |
Jason Monk | 395617f | 2017-05-05 14:07:58 -0400 | [diff] [blame] | 159 | state.icon = ResourceIcon.get(R.drawable.ic_qs_bluetooth_on); |
Selim Cinek | 4fda7b2 | 2014-08-18 22:07:25 +0200 | [diff] [blame] | 160 | state.contentDescription = mContext.getString( |
Amin Shaikh | 3468e5a | 2018-04-18 11:39:07 -0400 | [diff] [blame] | 161 | R.string.accessibility_quick_settings_bluetooth); |
Jason Monk | 3250885 | 2017-01-18 09:17:13 -0500 | [diff] [blame] | 162 | state.state = Tile.STATE_INACTIVE; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 163 | } |
Selim Cinek | 4fda7b2 | 2014-08-18 22:07:25 +0200 | [diff] [blame] | 164 | |
Jason Monk | 28df4ed | 2017-03-23 10:25:37 -0400 | [diff] [blame] | 165 | state.dualLabelContentDescription = mContext.getResources().getString( |
Julia Reynolds | 20aef8a | 2016-05-04 16:44:08 -0400 | [diff] [blame] | 166 | R.string.accessibility_quick_settings_open_settings, getTileLabel()); |
Jason Monk | 702e2eb | 2017-03-03 16:53:44 -0500 | [diff] [blame] | 167 | state.expandedAccessibilityClassName = Switch.class.getName(); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 168 | } |
| 169 | |
Rohan Shah | d5df175 | 2018-01-19 10:45:09 -0800 | [diff] [blame] | 170 | /** |
| 171 | * Returns the secondary label to use for the given bluetooth connection in the form of the |
| 172 | * battery level or bluetooth profile name. If the bluetooth is disabled, there's no connected |
| 173 | * devices, or we can't map the bluetooth class to a profile, this instead returns {@code null}. |
Rohan Shah | d5df175 | 2018-01-19 10:45:09 -0800 | [diff] [blame] | 174 | * @param enabled whether bluetooth is enabled |
Amin Shaikh | c548340 | 2018-04-26 13:51:54 -0400 | [diff] [blame] | 175 | * @param connecting whether bluetooth is connecting to a device |
Rohan Shah | d5df175 | 2018-01-19 10:45:09 -0800 | [diff] [blame] | 176 | * @param connected whether there's a device connected via bluetooth |
Amin Shaikh | d03a743 | 2018-03-01 15:46:55 -0500 | [diff] [blame] | 177 | * @param isTransient whether bluetooth is currently in a transient state turning on |
Rohan Shah | d5df175 | 2018-01-19 10:45:09 -0800 | [diff] [blame] | 178 | */ |
| 179 | @Nullable |
Amin Shaikh | c548340 | 2018-04-26 13:51:54 -0400 | [diff] [blame] | 180 | private String getSecondaryLabel(boolean enabled, boolean connecting, boolean connected, |
| 181 | boolean isTransient) { |
| 182 | if (connecting) { |
| 183 | return mContext.getString(R.string.quick_settings_connecting); |
| 184 | } |
Amin Shaikh | d03a743 | 2018-03-01 15:46:55 -0500 | [diff] [blame] | 185 | if (isTransient) { |
| 186 | return mContext.getString(R.string.quick_settings_bluetooth_secondary_label_transient); |
| 187 | } |
Rohan Shah | d5df175 | 2018-01-19 10:45:09 -0800 | [diff] [blame] | 188 | |
Amin Shaikh | 45fb7a7 | 2018-04-18 14:14:38 -0400 | [diff] [blame] | 189 | List<CachedBluetoothDevice> connectedDevices = mController.getConnectedDevices(); |
| 190 | if (enabled && connected && !connectedDevices.isEmpty()) { |
| 191 | if (connectedDevices.size() > 1) { |
| 192 | // TODO(b/76102598): add a new string for "X connected devices" after P |
| 193 | return mContext.getResources().getQuantityString( |
| 194 | R.plurals.quick_settings_hotspot_secondary_label_num_devices, |
| 195 | connectedDevices.size(), |
| 196 | connectedDevices.size()); |
| 197 | } |
| 198 | |
| 199 | CachedBluetoothDevice lastDevice = connectedDevices.get(0); |
Rohan Shah | d5df175 | 2018-01-19 10:45:09 -0800 | [diff] [blame] | 200 | final int batteryLevel = lastDevice.getBatteryLevel(); |
| 201 | |
| 202 | if (batteryLevel != BluetoothDevice.BATTERY_LEVEL_UNKNOWN) { |
| 203 | return mContext.getString( |
| 204 | R.string.quick_settings_bluetooth_secondary_label_battery_level, |
| 205 | Utils.formatPercentage(batteryLevel)); |
| 206 | |
| 207 | } else { |
| 208 | final BluetoothClass bluetoothClass = lastDevice.getBtClass(); |
| 209 | if (bluetoothClass != null) { |
Stanley Tng | 9085541 | 2018-11-29 13:47:45 -0800 | [diff] [blame^] | 210 | if (lastDevice.isHearingAidDevice()) { |
| 211 | return mContext.getString( |
| 212 | R.string.quick_settings_bluetooth_secondary_label_hearing_aids); |
| 213 | } else if (bluetoothClass.doesClassMatch(BluetoothClass.PROFILE_A2DP)) { |
Rohan Shah | d5df175 | 2018-01-19 10:45:09 -0800 | [diff] [blame] | 214 | return mContext.getString( |
| 215 | R.string.quick_settings_bluetooth_secondary_label_audio); |
| 216 | } else if (bluetoothClass.doesClassMatch(BluetoothClass.PROFILE_HEADSET)) { |
| 217 | return mContext.getString( |
| 218 | R.string.quick_settings_bluetooth_secondary_label_headset); |
| 219 | } else if (bluetoothClass.doesClassMatch(BluetoothClass.PROFILE_HID)) { |
| 220 | return mContext.getString( |
| 221 | R.string.quick_settings_bluetooth_secondary_label_input); |
| 222 | } |
| 223 | } |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | return null; |
| 228 | } |
| 229 | |
Selim Cinek | 4fda7b2 | 2014-08-18 22:07:25 +0200 | [diff] [blame] | 230 | @Override |
Chris Wren | 457a21c | 2015-05-06 17:50:34 -0400 | [diff] [blame] | 231 | public int getMetricsCategory() { |
Chris Wren | f6e9228b | 2016-01-26 18:04:35 -0500 | [diff] [blame] | 232 | return MetricsEvent.QS_BLUETOOTH; |
Chris Wren | 457a21c | 2015-05-06 17:50:34 -0400 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | @Override |
Selim Cinek | 4fda7b2 | 2014-08-18 22:07:25 +0200 | [diff] [blame] | 236 | protected String composeChangeAnnouncement() { |
| 237 | if (mState.value) { |
| 238 | return mContext.getString(R.string.accessibility_quick_settings_bluetooth_changed_on); |
| 239 | } else { |
| 240 | return mContext.getString(R.string.accessibility_quick_settings_bluetooth_changed_off); |
| 241 | } |
| 242 | } |
| 243 | |
Jason Monk | c3f42c1 | 2016-02-05 12:33:13 -0500 | [diff] [blame] | 244 | @Override |
| 245 | public boolean isAvailable() { |
| 246 | return mController.isBluetoothSupported(); |
Jason Monk | ba2318e | 2015-12-08 09:04:23 -0500 | [diff] [blame] | 247 | } |
| 248 | |
John Spurlock | d1c86e2 | 2014-06-01 00:04:53 -0400 | [diff] [blame] | 249 | private final BluetoothController.Callback mCallback = new BluetoothController.Callback() { |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 250 | @Override |
Jason Monk | a7d92b6 | 2015-05-27 10:20:37 -0400 | [diff] [blame] | 251 | public void onBluetoothStateChange(boolean enabled) { |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 252 | refreshState(); |
Jason Monk | be3235a | 2017-04-05 09:29:53 -0400 | [diff] [blame] | 253 | if (isShowingDetail()) { |
| 254 | mDetailAdapter.updateItems(); |
Jason Monk | 3c3b650 | 2017-07-27 15:41:26 -0400 | [diff] [blame] | 255 | fireToggleStateChanged(mDetailAdapter.getToggleState()); |
Jason Monk | be3235a | 2017-04-05 09:29:53 -0400 | [diff] [blame] | 256 | } |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 257 | } |
Jason Monk | a7d92b6 | 2015-05-27 10:20:37 -0400 | [diff] [blame] | 258 | |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 259 | @Override |
Jason Monk | be3c5db | 2015-02-04 13:00:55 -0500 | [diff] [blame] | 260 | public void onBluetoothDevicesChanged() { |
PauloftheWest | 6cdeaa0 | 2014-10-06 09:27:08 -0700 | [diff] [blame] | 261 | refreshState(); |
Jason Monk | be3235a | 2017-04-05 09:29:53 -0400 | [diff] [blame] | 262 | if (isShowingDetail()) { |
| 263 | mDetailAdapter.updateItems(); |
| 264 | } |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 265 | } |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 266 | }; |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 267 | |
Muyuan Li | 0e9f538 | 2016-04-27 15:51:15 -0700 | [diff] [blame] | 268 | @Override |
| 269 | protected DetailAdapter createDetailAdapter() { |
| 270 | return new BluetoothDetailAdapter(); |
| 271 | } |
| 272 | |
Rohan Shah | d5df175 | 2018-01-19 10:45:09 -0800 | [diff] [blame] | 273 | /** |
| 274 | * Bluetooth icon wrapper for Quick Settings with a battery indicator that reflects the |
| 275 | * connected device's battery level. This is used instead of |
| 276 | * {@link com.android.systemui.qs.tileimpl.QSTileImpl.DrawableIcon} in order to use a context |
| 277 | * that reflects dark/light theme attributes. |
| 278 | */ |
jackqdyulei | 786a490 | 2017-10-24 13:27:53 -0700 | [diff] [blame] | 279 | private class BluetoothBatteryTileIcon extends Icon { |
Rohan Shah | d5df175 | 2018-01-19 10:45:09 -0800 | [diff] [blame] | 280 | private int mBatteryLevel; |
jackqdyulei | 4b7f43b | 2017-10-19 11:05:09 -0700 | [diff] [blame] | 281 | private float mIconScale; |
Evan Laird | 4673c38 | 2017-09-27 13:00:50 -0400 | [diff] [blame] | 282 | |
Rohan Shah | d5df175 | 2018-01-19 10:45:09 -0800 | [diff] [blame] | 283 | BluetoothBatteryTileIcon(int batteryLevel, float iconScale) { |
| 284 | mBatteryLevel = batteryLevel; |
jackqdyulei | 4b7f43b | 2017-10-19 11:05:09 -0700 | [diff] [blame] | 285 | mIconScale = iconScale; |
Evan Laird | 4673c38 | 2017-09-27 13:00:50 -0400 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | @Override |
| 289 | public Drawable getDrawable(Context context) { |
jackqdyulei | c9d6a17 | 2017-09-18 18:55:41 -0700 | [diff] [blame] | 290 | // This method returns Pair<Drawable, String> while first value is the drawable |
Rohan Shah | d5df175 | 2018-01-19 10:45:09 -0800 | [diff] [blame] | 291 | return BluetoothDeviceLayerDrawable.createLayerDrawable( |
| 292 | context, |
| 293 | R.drawable.ic_qs_bluetooth_connected, |
| 294 | mBatteryLevel, |
| 295 | mIconScale); |
Evan Laird | 4673c38 | 2017-09-27 13:00:50 -0400 | [diff] [blame] | 296 | } |
| 297 | } |
| 298 | |
Rohan Shah | 4321648 | 2018-02-09 11:07:29 -0800 | [diff] [blame] | 299 | |
| 300 | /** |
| 301 | * Bluetooth icon wrapper (when connected with no battery indicator) for Quick Settings. This is |
| 302 | * used instead of {@link com.android.systemui.qs.tileimpl.QSTileImpl.DrawableIcon} in order to |
| 303 | * use a context that reflects dark/light theme attributes. |
| 304 | */ |
| 305 | private class BluetoothConnectedTileIcon extends Icon { |
| 306 | |
| 307 | BluetoothConnectedTileIcon() { |
| 308 | // Do nothing. Default constructor to limit visibility. |
| 309 | } |
| 310 | |
| 311 | @Override |
| 312 | public Drawable getDrawable(Context context) { |
| 313 | // This method returns Pair<Drawable, String> - the first value is the drawable. |
| 314 | return context.getDrawable(R.drawable.ic_qs_bluetooth_connected); |
| 315 | } |
| 316 | } |
| 317 | |
Muyuan Li | 0e9f538 | 2016-04-27 15:51:15 -0700 | [diff] [blame] | 318 | protected class BluetoothDetailAdapter implements DetailAdapter, QSDetailItems.Callback { |
Jason Monk | 9ef03b4 | 2017-06-13 12:49:55 -0400 | [diff] [blame] | 319 | // We probably won't ever have space in the UI for more than 20 devices, so don't |
| 320 | // get info for them. |
| 321 | private static final int MAX_DEVICES = 20; |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 322 | private QSDetailItems mItems; |
| 323 | |
| 324 | @Override |
Jason Monk | c06fbb1 | 2016-01-08 14:12:18 -0500 | [diff] [blame] | 325 | public CharSequence getTitle() { |
| 326 | return mContext.getString(R.string.quick_settings_bluetooth_label); |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | @Override |
| 330 | public Boolean getToggleState() { |
| 331 | return mState.value; |
| 332 | } |
| 333 | |
| 334 | @Override |
Jason Monk | fac2538 | 2016-07-19 14:13:37 -0400 | [diff] [blame] | 335 | public boolean getToggleEnabled() { |
| 336 | return mController.getBluetoothState() == BluetoothAdapter.STATE_OFF |
| 337 | || mController.getBluetoothState() == BluetoothAdapter.STATE_ON; |
| 338 | } |
| 339 | |
| 340 | @Override |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 341 | public Intent getSettingsIntent() { |
| 342 | return BLUETOOTH_SETTINGS; |
| 343 | } |
| 344 | |
| 345 | @Override |
| 346 | public void setToggleState(boolean state) { |
Chris Wren | f6e9228b | 2016-01-26 18:04:35 -0500 | [diff] [blame] | 347 | MetricsLogger.action(mContext, MetricsEvent.QS_BLUETOOTH_TOGGLE, state); |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 348 | mController.setBluetoothEnabled(state); |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | @Override |
Chris Wren | 457a21c | 2015-05-06 17:50:34 -0400 | [diff] [blame] | 352 | public int getMetricsCategory() { |
Chris Wren | f6e9228b | 2016-01-26 18:04:35 -0500 | [diff] [blame] | 353 | return MetricsEvent.QS_BLUETOOTH_DETAILS; |
Chris Wren | 457a21c | 2015-05-06 17:50:34 -0400 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | @Override |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 357 | public View createDetailView(Context context, View convertView, ViewGroup parent) { |
| 358 | mItems = QSDetailItems.convertOrInflate(context, convertView, parent); |
| 359 | mItems.setTagSuffix("Bluetooth"); |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 360 | mItems.setCallback(this); |
| 361 | updateItems(); |
| 362 | setItemsVisible(mState.value); |
| 363 | return mItems; |
| 364 | } |
| 365 | |
| 366 | public void setItemsVisible(boolean visible) { |
| 367 | if (mItems == null) return; |
| 368 | mItems.setItemsVisible(visible); |
| 369 | } |
| 370 | |
| 371 | private void updateItems() { |
| 372 | if (mItems == null) return; |
Jason Monk | be3235a | 2017-04-05 09:29:53 -0400 | [diff] [blame] | 373 | if (mController.isBluetoothEnabled()) { |
| 374 | mItems.setEmptyState(R.drawable.ic_qs_bluetooth_detail_empty, |
| 375 | R.string.quick_settings_bluetooth_detail_empty_text); |
| 376 | } else { |
| 377 | mItems.setEmptyState(R.drawable.ic_qs_bluetooth_detail_empty, |
| 378 | R.string.bt_is_off); |
| 379 | } |
Jason Monk | c17d329 | 2016-01-30 12:49:41 -0500 | [diff] [blame] | 380 | ArrayList<Item> items = new ArrayList<Item>(); |
Jason Monk | be3c5db | 2015-02-04 13:00:55 -0500 | [diff] [blame] | 381 | final Collection<CachedBluetoothDevice> devices = mController.getDevices(); |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 382 | if (devices != null) { |
tturney | 6f991c2 | 2017-04-18 13:59:40 -0700 | [diff] [blame] | 383 | int connectedDevices = 0; |
Jason Monk | 9ef03b4 | 2017-06-13 12:49:55 -0400 | [diff] [blame] | 384 | int count = 0; |
Jason Monk | be3c5db | 2015-02-04 13:00:55 -0500 | [diff] [blame] | 385 | for (CachedBluetoothDevice device : devices) { |
Jason Monk | 9ef03b4 | 2017-06-13 12:49:55 -0400 | [diff] [blame] | 386 | if (mController.getBondState(device) == BluetoothDevice.BOND_NONE) continue; |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 387 | final Item item = new Item(); |
jackqdyulei | 786a490 | 2017-10-24 13:27:53 -0700 | [diff] [blame] | 388 | item.iconResId = R.drawable.ic_qs_bluetooth_on; |
Jason Monk | be3c5db | 2015-02-04 13:00:55 -0500 | [diff] [blame] | 389 | item.line1 = device.getName(); |
tturney | 6f991c2 | 2017-04-18 13:59:40 -0700 | [diff] [blame] | 390 | item.tag = device; |
Jack He | a46af4e | 2017-07-26 23:51:23 -0700 | [diff] [blame] | 391 | int state = device.getMaxConnectionState(); |
Jason Monk | be3c5db | 2015-02-04 13:00:55 -0500 | [diff] [blame] | 392 | if (state == BluetoothProfile.STATE_CONNECTED) { |
jackqdyulei | 786a490 | 2017-10-24 13:27:53 -0700 | [diff] [blame] | 393 | item.iconResId = R.drawable.ic_qs_bluetooth_connected; |
Jack He | 6258aae | 2017-06-29 17:01:23 -0700 | [diff] [blame] | 394 | int batteryLevel = device.getBatteryLevel(); |
| 395 | if (batteryLevel != BluetoothDevice.BATTERY_LEVEL_UNKNOWN) { |
Rohan Shah | d5df175 | 2018-01-19 10:45:09 -0800 | [diff] [blame] | 396 | item.icon = new BluetoothBatteryTileIcon(batteryLevel,1 /* iconScale */); |
Jack He | 6258aae | 2017-06-29 17:01:23 -0700 | [diff] [blame] | 397 | item.line2 = mContext.getString( |
| 398 | R.string.quick_settings_connected_battery_level, |
| 399 | Utils.formatPercentage(batteryLevel)); |
| 400 | } else { |
| 401 | item.line2 = mContext.getString(R.string.quick_settings_connected); |
| 402 | } |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 403 | item.canDisconnect = true; |
tturney | 6f991c2 | 2017-04-18 13:59:40 -0700 | [diff] [blame] | 404 | items.add(connectedDevices, item); |
| 405 | connectedDevices++; |
Jason Monk | be3c5db | 2015-02-04 13:00:55 -0500 | [diff] [blame] | 406 | } else if (state == BluetoothProfile.STATE_CONNECTING) { |
jackqdyulei | 786a490 | 2017-10-24 13:27:53 -0700 | [diff] [blame] | 407 | item.iconResId = R.drawable.ic_qs_bluetooth_connecting; |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 408 | item.line2 = mContext.getString(R.string.quick_settings_connecting); |
tturney | 6f991c2 | 2017-04-18 13:59:40 -0700 | [diff] [blame] | 409 | items.add(connectedDevices, item); |
| 410 | } else { |
| 411 | items.add(item); |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 412 | } |
Jason Monk | 9ef03b4 | 2017-06-13 12:49:55 -0400 | [diff] [blame] | 413 | if (++count == MAX_DEVICES) { |
| 414 | break; |
| 415 | } |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 416 | } |
| 417 | } |
Jason Monk | c17d329 | 2016-01-30 12:49:41 -0500 | [diff] [blame] | 418 | mItems.setItems(items.toArray(new Item[items.size()])); |
Jason Monk | be3c5db | 2015-02-04 13:00:55 -0500 | [diff] [blame] | 419 | } |
| 420 | |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 421 | @Override |
| 422 | public void onDetailItemClick(Item item) { |
| 423 | if (item == null || item.tag == null) return; |
Jason Monk | be3c5db | 2015-02-04 13:00:55 -0500 | [diff] [blame] | 424 | final CachedBluetoothDevice device = (CachedBluetoothDevice) item.tag; |
| 425 | if (device != null && device.getMaxConnectionState() |
| 426 | == BluetoothProfile.STATE_DISCONNECTED) { |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 427 | mController.connect(device); |
| 428 | } |
| 429 | } |
| 430 | |
| 431 | @Override |
| 432 | public void onDetailItemDisconnect(Item item) { |
| 433 | if (item == null || item.tag == null) return; |
Jason Monk | be3c5db | 2015-02-04 13:00:55 -0500 | [diff] [blame] | 434 | final CachedBluetoothDevice device = (CachedBluetoothDevice) item.tag; |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 435 | if (device != null) { |
| 436 | mController.disconnect(device); |
| 437 | } |
| 438 | } |
| 439 | } |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 440 | } |