BluetoothTile.java revision c3f42c102422f70f5bbe67105e16515ce9c306a3
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 17package com.android.systemui.qs.tiles; 18 19import android.bluetooth.BluetoothDevice; 20import android.bluetooth.BluetoothProfile; 21import android.content.Context; 22import android.content.Intent; 23import android.provider.Settings; 24import android.text.TextUtils; 25import android.view.View; 26import android.view.ViewGroup; 27 28import com.android.internal.logging.MetricsLogger; 29import com.android.internal.logging.MetricsProto.MetricsEvent; 30import com.android.settingslib.bluetooth.CachedBluetoothDevice; 31import com.android.systemui.R; 32import com.android.systemui.qs.QSDetailItems; 33import com.android.systemui.qs.QSDetailItems.Item; 34import com.android.systemui.qs.QSTile; 35import com.android.systemui.statusbar.policy.BluetoothController; 36 37import java.util.ArrayList; 38import java.util.Collection; 39 40/** Quick settings tile: Bluetooth **/ 41public class BluetoothTile extends QSTile<QSTile.BooleanState> { 42 private static final Intent BLUETOOTH_SETTINGS = new Intent(Settings.ACTION_BLUETOOTH_SETTINGS); 43 44 private final BluetoothController mController; 45 private final BluetoothDetailAdapter mDetailAdapter; 46 47 public BluetoothTile(Host host) { 48 super(host); 49 mController = host.getBluetoothController(); 50 mDetailAdapter = new BluetoothDetailAdapter(); 51 } 52 53 @Override 54 public DetailAdapter getDetailAdapter() { 55 return mDetailAdapter; 56 } 57 58 @Override 59 public BooleanState newTileState() { 60 return new BooleanState(); 61 } 62 63 @Override 64 public void setListening(boolean listening) { 65 if (listening) { 66 mController.addStateChangedCallback(mCallback); 67 } else { 68 mController.removeStateChangedCallback(mCallback); 69 } 70 } 71 72 @Override 73 protected void handleSecondaryClick() { 74 // Secondary clicks are header clicks, just toggle. 75 final boolean isEnabled = (Boolean)mState.value; 76 MetricsLogger.action(mContext, getMetricsCategory(), !isEnabled); 77 mController.setBluetoothEnabled(!isEnabled); 78 } 79 80 @Override 81 protected void handleClick() { 82 if (!mController.canConfigBluetooth()) { 83 mHost.startActivityDismissingKeyguard(new Intent(Settings.ACTION_BLUETOOTH_SETTINGS)); 84 return; 85 } 86 if (!mState.value) { 87 mState.value = true; 88 mController.setBluetoothEnabled(true); 89 } 90 showDetail(true); 91 } 92 93 @Override 94 protected void handleUpdateState(BooleanState state, Object arg) { 95 final boolean enabled = mController.isBluetoothEnabled(); 96 final boolean connected = mController.isBluetoothConnected(); 97 final boolean connecting = mController.isBluetoothConnecting(); 98 state.value = enabled; 99 state.autoMirrorDrawable = false; 100 if (enabled) { 101 state.label = null; 102 if (connected) { 103 state.icon = ResourceIcon.get(R.drawable.ic_qs_bluetooth_connected); 104 state.contentDescription = mContext.getString( 105 R.string.accessibility_quick_settings_bluetooth_connected); 106 state.label = mController.getLastDeviceName(); 107 } else if (connecting) { 108 state.icon = ResourceIcon.get(R.drawable.ic_qs_bluetooth_connecting); 109 state.contentDescription = mContext.getString( 110 R.string.accessibility_quick_settings_bluetooth_connecting); 111 state.label = mContext.getString(R.string.quick_settings_bluetooth_label); 112 } else { 113 state.icon = ResourceIcon.get(R.drawable.ic_qs_bluetooth_on); 114 state.contentDescription = mContext.getString( 115 R.string.accessibility_quick_settings_bluetooth_on); 116 } 117 if (TextUtils.isEmpty(state.label)) { 118 state.label = mContext.getString(R.string.quick_settings_bluetooth_label); 119 } 120 } else { 121 state.icon = ResourceIcon.get(R.drawable.ic_qs_bluetooth_off); 122 state.label = mContext.getString(R.string.quick_settings_bluetooth_label); 123 state.contentDescription = mContext.getString( 124 R.string.accessibility_quick_settings_bluetooth_off); 125 } 126 127 CharSequence bluetoothName = state.label; 128 if (connected) { 129 bluetoothName = state.dualLabelContentDescription = mContext.getString( 130 R.string.accessibility_bluetooth_name, state.label); 131 } 132 state.dualLabelContentDescription = bluetoothName; 133 } 134 135 @Override 136 public int getMetricsCategory() { 137 return MetricsEvent.QS_BLUETOOTH; 138 } 139 140 @Override 141 protected String composeChangeAnnouncement() { 142 if (mState.value) { 143 return mContext.getString(R.string.accessibility_quick_settings_bluetooth_changed_on); 144 } else { 145 return mContext.getString(R.string.accessibility_quick_settings_bluetooth_changed_off); 146 } 147 } 148 149 @Override 150 public boolean isAvailable() { 151 return mController.isBluetoothSupported(); 152 } 153 154 private final BluetoothController.Callback mCallback = new BluetoothController.Callback() { 155 @Override 156 public void onBluetoothStateChange(boolean enabled) { 157 refreshState(); 158 } 159 160 @Override 161 public void onBluetoothDevicesChanged() { 162 mUiHandler.post(new Runnable() { 163 @Override 164 public void run() { 165 mDetailAdapter.updateItems(); 166 } 167 }); 168 refreshState(); 169 } 170 }; 171 172 private final class BluetoothDetailAdapter implements DetailAdapter, QSDetailItems.Callback { 173 private QSDetailItems mItems; 174 175 @Override 176 public CharSequence getTitle() { 177 return mContext.getString(R.string.quick_settings_bluetooth_label); 178 } 179 180 @Override 181 public Boolean getToggleState() { 182 return mState.value; 183 } 184 185 @Override 186 public Intent getSettingsIntent() { 187 return BLUETOOTH_SETTINGS; 188 } 189 190 @Override 191 public void setToggleState(boolean state) { 192 MetricsLogger.action(mContext, MetricsEvent.QS_BLUETOOTH_TOGGLE, state); 193 mController.setBluetoothEnabled(state); 194 showDetail(false); 195 } 196 197 @Override 198 public int getMetricsCategory() { 199 return MetricsEvent.QS_BLUETOOTH_DETAILS; 200 } 201 202 @Override 203 public View createDetailView(Context context, View convertView, ViewGroup parent) { 204 mItems = QSDetailItems.convertOrInflate(context, convertView, parent); 205 mItems.setTagSuffix("Bluetooth"); 206 mItems.setEmptyState(R.drawable.ic_qs_bluetooth_detail_empty, 207 R.string.quick_settings_bluetooth_detail_empty_text); 208 mItems.setCallback(this); 209 mItems.setMinHeightInItems(0); 210 updateItems(); 211 setItemsVisible(mState.value); 212 return mItems; 213 } 214 215 public void setItemsVisible(boolean visible) { 216 if (mItems == null) return; 217 mItems.setItemsVisible(visible); 218 } 219 220 private void updateItems() { 221 if (mItems == null) return; 222 ArrayList<Item> items = new ArrayList<Item>(); 223 final Collection<CachedBluetoothDevice> devices = mController.getDevices(); 224 if (devices != null) { 225 for (CachedBluetoothDevice device : devices) { 226 if (device.getBondState() == BluetoothDevice.BOND_NONE) continue; 227 final Item item = new Item(); 228 item.icon = R.drawable.ic_qs_bluetooth_on; 229 item.line1 = device.getName(); 230 int state = device.getMaxConnectionState(); 231 if (state == BluetoothProfile.STATE_CONNECTED) { 232 item.icon = R.drawable.ic_qs_bluetooth_connected; 233 item.line2 = mContext.getString(R.string.quick_settings_connected); 234 item.canDisconnect = true; 235 } else if (state == BluetoothProfile.STATE_CONNECTING) { 236 item.icon = R.drawable.ic_qs_bluetooth_connecting; 237 item.line2 = mContext.getString(R.string.quick_settings_connecting); 238 } 239 item.tag = device; 240 items.add(item); 241 } 242 } 243 mItems.setItems(items.toArray(new Item[items.size()])); 244 } 245 246 @Override 247 public void onDetailItemClick(Item item) { 248 if (item == null || item.tag == null) return; 249 final CachedBluetoothDevice device = (CachedBluetoothDevice) item.tag; 250 if (device != null && device.getMaxConnectionState() 251 == BluetoothProfile.STATE_DISCONNECTED) { 252 mController.connect(device); 253 } 254 } 255 256 @Override 257 public void onDetailItemDisconnect(Item item) { 258 if (item == null || item.tag == null) return; 259 final CachedBluetoothDevice device = (CachedBluetoothDevice) item.tag; 260 if (device != null) { 261 mController.disconnect(device); 262 } 263 } 264 } 265} 266