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 | fac2538 | 2016-07-19 14:13:37 -0400 | [diff] [blame] | 19 | import android.bluetooth.BluetoothAdapter; |
Jason Monk | be3c5db | 2015-02-04 13:00:55 -0500 | [diff] [blame] | 20 | import android.bluetooth.BluetoothDevice; |
| 21 | import android.bluetooth.BluetoothProfile; |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 22 | import android.content.Context; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 23 | import android.content.Intent; |
| 24 | import android.provider.Settings; |
Jason Monk | 3250885 | 2017-01-18 09:17:13 -0500 | [diff] [blame] | 25 | import android.service.quicksettings.Tile; |
John Spurlock | d1c86e2 | 2014-06-01 00:04:53 -0400 | [diff] [blame] | 26 | import android.text.TextUtils; |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 27 | import android.view.View; |
| 28 | import android.view.ViewGroup; |
Julia Reynolds | 20aef8a | 2016-05-04 16:44:08 -0400 | [diff] [blame] | 29 | import android.widget.Switch; |
Chris Wren | f6e9228b | 2016-01-26 18:04:35 -0500 | [diff] [blame] | 30 | |
Chris Wren | 457a21c | 2015-05-06 17:50:34 -0400 | [diff] [blame] | 31 | import com.android.internal.logging.MetricsLogger; |
Tamas Berghammer | 383db5eb | 2016-06-22 15:21:38 +0100 | [diff] [blame] | 32 | import com.android.internal.logging.nano.MetricsProto.MetricsEvent; |
Jason Monk | be3c5db | 2015-02-04 13:00:55 -0500 | [diff] [blame] | 33 | import com.android.settingslib.bluetooth.CachedBluetoothDevice; |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 34 | import com.android.systemui.Dependency; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 35 | import com.android.systemui.R; |
Jason Monk | ec34da8 | 2017-02-24 15:57:05 -0500 | [diff] [blame] | 36 | import com.android.systemui.plugins.ActivityStarter; |
Jason Monk | e5b770e | 2017-03-03 21:49:29 -0500 | [diff] [blame] | 37 | import com.android.systemui.plugins.qs.DetailAdapter; |
Jason Monk | be3235a | 2017-04-05 09:29:53 -0400 | [diff] [blame] | 38 | import com.android.systemui.plugins.qs.QSTile.BooleanState; |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 39 | import com.android.systemui.qs.QSDetailItems; |
| 40 | import com.android.systemui.qs.QSDetailItems.Item; |
Jason Monk | 702e2eb | 2017-03-03 16:53:44 -0500 | [diff] [blame] | 41 | import com.android.systemui.qs.QSHost; |
Jason Monk | 702e2eb | 2017-03-03 16:53:44 -0500 | [diff] [blame] | 42 | import com.android.systemui.qs.tileimpl.QSTileImpl; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 43 | import com.android.systemui.statusbar.policy.BluetoothController; |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 44 | |
Jason Monk | c17d329 | 2016-01-30 12:49:41 -0500 | [diff] [blame] | 45 | import java.util.ArrayList; |
Jason Monk | be3c5db | 2015-02-04 13:00:55 -0500 | [diff] [blame] | 46 | import java.util.Collection; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 47 | |
| 48 | /** Quick settings tile: Bluetooth **/ |
Jason Monk | 702e2eb | 2017-03-03 16:53:44 -0500 | [diff] [blame] | 49 | public class BluetoothTile extends QSTileImpl<BooleanState> { |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 50 | private static final Intent BLUETOOTH_SETTINGS = new Intent(Settings.ACTION_BLUETOOTH_SETTINGS); |
| 51 | |
| 52 | private final BluetoothController mController; |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 53 | private final BluetoothDetailAdapter mDetailAdapter; |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 54 | private final ActivityStarter mActivityStarter; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 55 | |
Jason Monk | 702e2eb | 2017-03-03 16:53:44 -0500 | [diff] [blame] | 56 | public BluetoothTile(QSHost host) { |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 57 | super(host); |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 58 | mController = Dependency.get(BluetoothController.class); |
| 59 | mActivityStarter = Dependency.get(ActivityStarter.class); |
Muyuan Li | 0e9f538 | 2016-04-27 15:51:15 -0700 | [diff] [blame] | 60 | mDetailAdapter = (BluetoothDetailAdapter) createDetailAdapter(); |
John Spurlock | ccb6b9a | 2014-05-17 15:54:40 -0400 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | @Override |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 64 | public DetailAdapter getDetailAdapter() { |
| 65 | return mDetailAdapter; |
| 66 | } |
| 67 | |
| 68 | @Override |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 69 | public BooleanState newTileState() { |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 70 | return new BooleanState(); |
| 71 | } |
| 72 | |
| 73 | @Override |
John Spurlock | ccb6b9a | 2014-05-17 15:54:40 -0400 | [diff] [blame] | 74 | public void setListening(boolean listening) { |
| 75 | if (listening) { |
Jason Monk | 8852905 | 2016-11-04 13:29:58 -0400 | [diff] [blame] | 76 | mController.addCallback(mCallback); |
John Spurlock | ccb6b9a | 2014-05-17 15:54:40 -0400 | [diff] [blame] | 77 | } else { |
Jason Monk | 8852905 | 2016-11-04 13:29:58 -0400 | [diff] [blame] | 78 | mController.removeCallback(mCallback); |
John Spurlock | ccb6b9a | 2014-05-17 15:54:40 -0400 | [diff] [blame] | 79 | } |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | @Override |
Jason Monk | 3250885 | 2017-01-18 09:17:13 -0500 | [diff] [blame] | 83 | protected void handleClick() { |
Jason Monk | dc35dcb | 2015-12-04 16:36:15 -0500 | [diff] [blame] | 84 | // Secondary clicks are header clicks, just toggle. |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 85 | final boolean isEnabled = (Boolean)mState.value; |
| 86 | mController.setBluetoothEnabled(!isEnabled); |
| 87 | } |
| 88 | |
| 89 | @Override |
Jason Monk | 76c67aa | 2016-02-19 14:49:42 -0500 | [diff] [blame] | 90 | public Intent getLongClickIntent() { |
| 91 | return new Intent(Settings.ACTION_BLUETOOTH_SETTINGS); |
| 92 | } |
| 93 | |
| 94 | @Override |
Jason Monk | 3250885 | 2017-01-18 09:17:13 -0500 | [diff] [blame] | 95 | protected void handleSecondaryClick() { |
Sudheer Shanka | b6fc931 | 2016-01-27 19:59:03 +0000 | [diff] [blame] | 96 | if (!mController.canConfigBluetooth()) { |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 97 | mActivityStarter.postStartActivityDismissingKeyguard( |
| 98 | new Intent(Settings.ACTION_BLUETOOTH_SETTINGS), 0); |
Sudheer Shanka | b6fc931 | 2016-01-27 19:59:03 +0000 | [diff] [blame] | 99 | return; |
| 100 | } |
Jason Monk | 48e093e | 2016-07-15 10:37:07 -0400 | [diff] [blame] | 101 | showDetail(true); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | @Override |
Jason Monk | 39c98e6 | 2016-03-16 09:18:35 -0400 | [diff] [blame] | 105 | public CharSequence getTileLabel() { |
| 106 | return mContext.getString(R.string.quick_settings_bluetooth_label); |
| 107 | } |
| 108 | |
| 109 | @Override |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 110 | protected void handleUpdateState(BooleanState state, Object arg) { |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 111 | final boolean enabled = mController.isBluetoothEnabled(); |
| 112 | final boolean connected = mController.isBluetoothConnected(); |
Jason Monk | 110e5f6 | 2017-03-31 13:45:12 -0400 | [diff] [blame] | 113 | state.isTransient = mController.isBluetoothConnecting() |
| 114 | || mController.getBluetoothState() == BluetoothAdapter.STATE_TURNING_ON; |
Jason Monk | 2db4619 | 2017-02-01 17:18:22 -0500 | [diff] [blame] | 115 | state.dualTarget = true; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 116 | state.value = enabled; |
Jason Monk | 395617f | 2017-05-05 14:07:58 -0400 | [diff] [blame] | 117 | if (state.slash == null) { |
| 118 | state.slash = new SlashState(); |
| 119 | } |
| 120 | state.slash.isSlashed = !enabled; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 121 | if (enabled) { |
John Spurlock | d1c86e2 | 2014-06-01 00:04:53 -0400 | [diff] [blame] | 122 | state.label = null; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 123 | if (connected) { |
John Spurlock | 2d69581 | 2014-10-30 13:25:21 -0400 | [diff] [blame] | 124 | state.icon = ResourceIcon.get(R.drawable.ic_qs_bluetooth_connected); |
John Spurlock | d1c86e2 | 2014-06-01 00:04:53 -0400 | [diff] [blame] | 125 | state.label = mController.getLastDeviceName(); |
Julia Reynolds | 20aef8a | 2016-05-04 16:44:08 -0400 | [diff] [blame] | 126 | state.contentDescription = mContext.getString( |
| 127 | R.string.accessibility_bluetooth_name, state.label); |
Jason Monk | 110e5f6 | 2017-03-31 13:45:12 -0400 | [diff] [blame] | 128 | } else if (state.isTransient) { |
| 129 | state.icon = ResourceIcon.get(R.drawable.ic_bluetooth_transient_animation); |
Selim Cinek | 4fda7b2 | 2014-08-18 22:07:25 +0200 | [diff] [blame] | 130 | state.contentDescription = mContext.getString( |
| 131 | R.string.accessibility_quick_settings_bluetooth_connecting); |
John Spurlock | a625b74 | 2014-06-05 09:55:37 -0400 | [diff] [blame] | 132 | state.label = mContext.getString(R.string.quick_settings_bluetooth_label); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 133 | } else { |
John Spurlock | 2d69581 | 2014-10-30 13:25:21 -0400 | [diff] [blame] | 134 | state.icon = ResourceIcon.get(R.drawable.ic_qs_bluetooth_on); |
Selim Cinek | 4fda7b2 | 2014-08-18 22:07:25 +0200 | [diff] [blame] | 135 | state.contentDescription = mContext.getString( |
Julia Reynolds | 20aef8a | 2016-05-04 16:44:08 -0400 | [diff] [blame] | 136 | R.string.accessibility_quick_settings_bluetooth_on) + "," |
| 137 | + mContext.getString(R.string.accessibility_not_connected); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 138 | } |
John Spurlock | d1c86e2 | 2014-06-01 00:04:53 -0400 | [diff] [blame] | 139 | if (TextUtils.isEmpty(state.label)) { |
| 140 | state.label = mContext.getString(R.string.quick_settings_bluetooth_label); |
| 141 | } |
Jason Monk | 3250885 | 2017-01-18 09:17:13 -0500 | [diff] [blame] | 142 | state.state = Tile.STATE_ACTIVE; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 143 | } else { |
Jason Monk | 395617f | 2017-05-05 14:07:58 -0400 | [diff] [blame] | 144 | state.icon = ResourceIcon.get(R.drawable.ic_qs_bluetooth_on); |
John Spurlock | 012d4a2 | 2014-06-04 00:40:18 -0400 | [diff] [blame] | 145 | state.label = mContext.getString(R.string.quick_settings_bluetooth_label); |
Selim Cinek | 4fda7b2 | 2014-08-18 22:07:25 +0200 | [diff] [blame] | 146 | state.contentDescription = mContext.getString( |
| 147 | R.string.accessibility_quick_settings_bluetooth_off); |
Jason Monk | 3250885 | 2017-01-18 09:17:13 -0500 | [diff] [blame] | 148 | state.state = Tile.STATE_INACTIVE; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 149 | } |
Selim Cinek | 4fda7b2 | 2014-08-18 22:07:25 +0200 | [diff] [blame] | 150 | |
Jason Monk | 28df4ed | 2017-03-23 10:25:37 -0400 | [diff] [blame] | 151 | state.dualLabelContentDescription = mContext.getResources().getString( |
Julia Reynolds | 20aef8a | 2016-05-04 16:44:08 -0400 | [diff] [blame] | 152 | R.string.accessibility_quick_settings_open_settings, getTileLabel()); |
Jason Monk | 702e2eb | 2017-03-03 16:53:44 -0500 | [diff] [blame] | 153 | state.expandedAccessibilityClassName = Switch.class.getName(); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 154 | } |
| 155 | |
Selim Cinek | 4fda7b2 | 2014-08-18 22:07:25 +0200 | [diff] [blame] | 156 | @Override |
Chris Wren | 457a21c | 2015-05-06 17:50:34 -0400 | [diff] [blame] | 157 | public int getMetricsCategory() { |
Chris Wren | f6e9228b | 2016-01-26 18:04:35 -0500 | [diff] [blame] | 158 | return MetricsEvent.QS_BLUETOOTH; |
Chris Wren | 457a21c | 2015-05-06 17:50:34 -0400 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | @Override |
Selim Cinek | 4fda7b2 | 2014-08-18 22:07:25 +0200 | [diff] [blame] | 162 | protected String composeChangeAnnouncement() { |
| 163 | if (mState.value) { |
| 164 | return mContext.getString(R.string.accessibility_quick_settings_bluetooth_changed_on); |
| 165 | } else { |
| 166 | return mContext.getString(R.string.accessibility_quick_settings_bluetooth_changed_off); |
| 167 | } |
| 168 | } |
| 169 | |
Jason Monk | c3f42c1 | 2016-02-05 12:33:13 -0500 | [diff] [blame] | 170 | @Override |
| 171 | public boolean isAvailable() { |
| 172 | return mController.isBluetoothSupported(); |
Jason Monk | ba2318e | 2015-12-08 09:04:23 -0500 | [diff] [blame] | 173 | } |
| 174 | |
John Spurlock | d1c86e2 | 2014-06-01 00:04:53 -0400 | [diff] [blame] | 175 | private final BluetoothController.Callback mCallback = new BluetoothController.Callback() { |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 176 | @Override |
Jason Monk | a7d92b6 | 2015-05-27 10:20:37 -0400 | [diff] [blame] | 177 | public void onBluetoothStateChange(boolean enabled) { |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 178 | refreshState(); |
Jason Monk | be3235a | 2017-04-05 09:29:53 -0400 | [diff] [blame] | 179 | if (isShowingDetail()) { |
| 180 | mDetailAdapter.updateItems(); |
| 181 | } |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 182 | } |
Jason Monk | a7d92b6 | 2015-05-27 10:20:37 -0400 | [diff] [blame] | 183 | |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 184 | @Override |
Jason Monk | be3c5db | 2015-02-04 13:00:55 -0500 | [diff] [blame] | 185 | public void onBluetoothDevicesChanged() { |
PauloftheWest | 6cdeaa0 | 2014-10-06 09:27:08 -0700 | [diff] [blame] | 186 | refreshState(); |
Jason Monk | be3235a | 2017-04-05 09:29:53 -0400 | [diff] [blame] | 187 | if (isShowingDetail()) { |
| 188 | mDetailAdapter.updateItems(); |
| 189 | } |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 190 | } |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 191 | }; |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 192 | |
Muyuan Li | 0e9f538 | 2016-04-27 15:51:15 -0700 | [diff] [blame] | 193 | @Override |
| 194 | protected DetailAdapter createDetailAdapter() { |
| 195 | return new BluetoothDetailAdapter(); |
| 196 | } |
| 197 | |
| 198 | protected class BluetoothDetailAdapter implements DetailAdapter, QSDetailItems.Callback { |
Jason Monk | 9ef03b4 | 2017-06-13 12:49:55 -0400 | [diff] [blame] | 199 | // We probably won't ever have space in the UI for more than 20 devices, so don't |
| 200 | // get info for them. |
| 201 | private static final int MAX_DEVICES = 20; |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 202 | private QSDetailItems mItems; |
| 203 | |
| 204 | @Override |
Jason Monk | c06fbb1 | 2016-01-08 14:12:18 -0500 | [diff] [blame] | 205 | public CharSequence getTitle() { |
| 206 | return mContext.getString(R.string.quick_settings_bluetooth_label); |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | @Override |
| 210 | public Boolean getToggleState() { |
| 211 | return mState.value; |
| 212 | } |
| 213 | |
| 214 | @Override |
Jason Monk | fac2538 | 2016-07-19 14:13:37 -0400 | [diff] [blame] | 215 | public boolean getToggleEnabled() { |
| 216 | return mController.getBluetoothState() == BluetoothAdapter.STATE_OFF |
| 217 | || mController.getBluetoothState() == BluetoothAdapter.STATE_ON; |
| 218 | } |
| 219 | |
| 220 | @Override |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 221 | public Intent getSettingsIntent() { |
| 222 | return BLUETOOTH_SETTINGS; |
| 223 | } |
| 224 | |
| 225 | @Override |
| 226 | public void setToggleState(boolean state) { |
Chris Wren | f6e9228b | 2016-01-26 18:04:35 -0500 | [diff] [blame] | 227 | MetricsLogger.action(mContext, MetricsEvent.QS_BLUETOOTH_TOGGLE, state); |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 228 | mController.setBluetoothEnabled(state); |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | @Override |
Chris Wren | 457a21c | 2015-05-06 17:50:34 -0400 | [diff] [blame] | 232 | public int getMetricsCategory() { |
Chris Wren | f6e9228b | 2016-01-26 18:04:35 -0500 | [diff] [blame] | 233 | return MetricsEvent.QS_BLUETOOTH_DETAILS; |
Chris Wren | 457a21c | 2015-05-06 17:50:34 -0400 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | @Override |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 237 | public View createDetailView(Context context, View convertView, ViewGroup parent) { |
| 238 | mItems = QSDetailItems.convertOrInflate(context, convertView, parent); |
| 239 | mItems.setTagSuffix("Bluetooth"); |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 240 | mItems.setCallback(this); |
| 241 | updateItems(); |
| 242 | setItemsVisible(mState.value); |
| 243 | return mItems; |
| 244 | } |
| 245 | |
| 246 | public void setItemsVisible(boolean visible) { |
| 247 | if (mItems == null) return; |
| 248 | mItems.setItemsVisible(visible); |
| 249 | } |
| 250 | |
| 251 | private void updateItems() { |
| 252 | if (mItems == null) return; |
Jason Monk | be3235a | 2017-04-05 09:29:53 -0400 | [diff] [blame] | 253 | if (mController.isBluetoothEnabled()) { |
| 254 | mItems.setEmptyState(R.drawable.ic_qs_bluetooth_detail_empty, |
| 255 | R.string.quick_settings_bluetooth_detail_empty_text); |
| 256 | } else { |
| 257 | mItems.setEmptyState(R.drawable.ic_qs_bluetooth_detail_empty, |
| 258 | R.string.bt_is_off); |
| 259 | } |
Jason Monk | c17d329 | 2016-01-30 12:49:41 -0500 | [diff] [blame] | 260 | ArrayList<Item> items = new ArrayList<Item>(); |
Jason Monk | be3c5db | 2015-02-04 13:00:55 -0500 | [diff] [blame] | 261 | final Collection<CachedBluetoothDevice> devices = mController.getDevices(); |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 262 | if (devices != null) { |
tturney | 6f991c2 | 2017-04-18 13:59:40 -0700 | [diff] [blame] | 263 | int connectedDevices = 0; |
Jason Monk | 9ef03b4 | 2017-06-13 12:49:55 -0400 | [diff] [blame] | 264 | int count = 0; |
Jason Monk | be3c5db | 2015-02-04 13:00:55 -0500 | [diff] [blame] | 265 | for (CachedBluetoothDevice device : devices) { |
Jason Monk | 9ef03b4 | 2017-06-13 12:49:55 -0400 | [diff] [blame] | 266 | if (mController.getBondState(device) == BluetoothDevice.BOND_NONE) continue; |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 267 | final Item item = new Item(); |
| 268 | item.icon = R.drawable.ic_qs_bluetooth_on; |
Jason Monk | be3c5db | 2015-02-04 13:00:55 -0500 | [diff] [blame] | 269 | item.line1 = device.getName(); |
tturney | 6f991c2 | 2017-04-18 13:59:40 -0700 | [diff] [blame] | 270 | item.tag = device; |
Jason Monk | 9ef03b4 | 2017-06-13 12:49:55 -0400 | [diff] [blame] | 271 | int state = mController.getMaxConnectionState(device); |
Jason Monk | be3c5db | 2015-02-04 13:00:55 -0500 | [diff] [blame] | 272 | if (state == BluetoothProfile.STATE_CONNECTED) { |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 273 | item.icon = R.drawable.ic_qs_bluetooth_connected; |
| 274 | item.line2 = mContext.getString(R.string.quick_settings_connected); |
| 275 | item.canDisconnect = true; |
tturney | 6f991c2 | 2017-04-18 13:59:40 -0700 | [diff] [blame] | 276 | items.add(connectedDevices, item); |
| 277 | connectedDevices++; |
Jason Monk | be3c5db | 2015-02-04 13:00:55 -0500 | [diff] [blame] | 278 | } else if (state == BluetoothProfile.STATE_CONNECTING) { |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 279 | item.icon = R.drawable.ic_qs_bluetooth_connecting; |
| 280 | item.line2 = mContext.getString(R.string.quick_settings_connecting); |
tturney | 6f991c2 | 2017-04-18 13:59:40 -0700 | [diff] [blame] | 281 | items.add(connectedDevices, item); |
| 282 | } else { |
| 283 | items.add(item); |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 284 | } |
Jason Monk | 9ef03b4 | 2017-06-13 12:49:55 -0400 | [diff] [blame] | 285 | if (++count == MAX_DEVICES) { |
| 286 | break; |
| 287 | } |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 288 | } |
| 289 | } |
Jason Monk | c17d329 | 2016-01-30 12:49:41 -0500 | [diff] [blame] | 290 | mItems.setItems(items.toArray(new Item[items.size()])); |
Jason Monk | be3c5db | 2015-02-04 13:00:55 -0500 | [diff] [blame] | 291 | } |
| 292 | |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 293 | @Override |
| 294 | public void onDetailItemClick(Item item) { |
| 295 | if (item == null || item.tag == null) return; |
Jason Monk | be3c5db | 2015-02-04 13:00:55 -0500 | [diff] [blame] | 296 | final CachedBluetoothDevice device = (CachedBluetoothDevice) item.tag; |
| 297 | if (device != null && device.getMaxConnectionState() |
| 298 | == BluetoothProfile.STATE_DISCONNECTED) { |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 299 | mController.connect(device); |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | @Override |
| 304 | public void onDetailItemDisconnect(Item item) { |
| 305 | if (item == null || item.tag == null) return; |
Jason Monk | be3c5db | 2015-02-04 13:00:55 -0500 | [diff] [blame] | 306 | final CachedBluetoothDevice device = (CachedBluetoothDevice) item.tag; |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 307 | if (device != null) { |
| 308 | mController.disconnect(device); |
| 309 | } |
| 310 | } |
| 311 | } |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 312 | } |