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 | |
John Spurlock | 1e6eb17 | 2014-07-13 11:59:50 -0400 | [diff] [blame] | 19 | import android.content.Context; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 20 | import android.content.Intent; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 21 | import android.provider.Settings; |
Jason Monk | 3250885 | 2017-01-18 09:17:13 -0500 | [diff] [blame^] | 22 | import android.service.quicksettings.Tile; |
John Spurlock | 1e6eb17 | 2014-07-13 11:59:50 -0400 | [diff] [blame] | 23 | import android.util.Log; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 24 | import android.view.View; |
John Spurlock | 1e6eb17 | 2014-07-13 11:59:50 -0400 | [diff] [blame] | 25 | import android.view.View.OnAttachStateChangeListener; |
| 26 | import android.view.ViewGroup; |
Julia Reynolds | 20aef8a | 2016-05-04 16:44:08 -0400 | [diff] [blame] | 27 | import android.widget.Button; |
Chris Wren | f6e9228b | 2016-01-26 18:04:35 -0500 | [diff] [blame] | 28 | |
Chris Wren | 457a21c | 2015-05-06 17:50:34 -0400 | [diff] [blame] | 29 | import com.android.internal.logging.MetricsLogger; |
Tamas Berghammer | 383db5eb | 2016-06-22 15:21:38 +0100 | [diff] [blame] | 30 | import com.android.internal.logging.nano.MetricsProto.MetricsEvent; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 31 | import com.android.systemui.R; |
Jason Monk | 0ceef21 | 2016-11-02 14:05:23 -0400 | [diff] [blame] | 32 | import com.android.systemui.plugins.qs.QS.DetailAdapter; |
John Spurlock | 1e6eb17 | 2014-07-13 11:59:50 -0400 | [diff] [blame] | 33 | import com.android.systemui.qs.QSDetailItems; |
| 34 | import com.android.systemui.qs.QSDetailItems.Item; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 35 | import com.android.systemui.qs.QSTile; |
| 36 | import com.android.systemui.statusbar.policy.CastController; |
John Spurlock | 1e6eb17 | 2014-07-13 11:59:50 -0400 | [diff] [blame] | 37 | import com.android.systemui.statusbar.policy.CastController.CastDevice; |
John Spurlock | 657c62c | 2014-07-22 12:18:09 -0400 | [diff] [blame] | 38 | import com.android.systemui.statusbar.policy.KeyguardMonitor; |
John Spurlock | 1e6eb17 | 2014-07-13 11:59:50 -0400 | [diff] [blame] | 39 | |
| 40 | import java.util.LinkedHashMap; |
| 41 | import java.util.Set; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 42 | |
| 43 | /** Quick settings tile: Cast **/ |
| 44 | public class CastTile extends QSTile<QSTile.BooleanState> { |
Jeff Brown | 1849262 | 2014-08-19 17:51:47 -0700 | [diff] [blame] | 45 | private static final Intent CAST_SETTINGS = |
| 46 | new Intent(Settings.ACTION_CAST_SETTINGS); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 47 | |
| 48 | private final CastController mController; |
John Spurlock | 1e6eb17 | 2014-07-13 11:59:50 -0400 | [diff] [blame] | 49 | private final CastDetailAdapter mDetailAdapter; |
John Spurlock | 657c62c | 2014-07-22 12:18:09 -0400 | [diff] [blame] | 50 | private final KeyguardMonitor mKeyguard; |
| 51 | private final Callback mCallback = new Callback(); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 52 | |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 53 | public CastTile(Host host) { |
| 54 | super(host); |
| 55 | mController = host.getCastController(); |
John Spurlock | 1e6eb17 | 2014-07-13 11:59:50 -0400 | [diff] [blame] | 56 | mDetailAdapter = new CastDetailAdapter(); |
John Spurlock | 657c62c | 2014-07-22 12:18:09 -0400 | [diff] [blame] | 57 | mKeyguard = host.getKeyguardMonitor(); |
John Spurlock | 1e6eb17 | 2014-07-13 11:59:50 -0400 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | @Override |
| 61 | public DetailAdapter getDetailAdapter() { |
| 62 | return mDetailAdapter; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | @Override |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 66 | public BooleanState newTileState() { |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 67 | return new BooleanState(); |
| 68 | } |
| 69 | |
| 70 | @Override |
John Spurlock | ccb6b9a | 2014-05-17 15:54:40 -0400 | [diff] [blame] | 71 | public void setListening(boolean listening) { |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 72 | if (mController == null) return; |
John Spurlock | 1e6eb17 | 2014-07-13 11:59:50 -0400 | [diff] [blame] | 73 | if (DEBUG) Log.d(TAG, "setListening " + listening); |
John Spurlock | ccb6b9a | 2014-05-17 15:54:40 -0400 | [diff] [blame] | 74 | if (listening) { |
| 75 | mController.addCallback(mCallback); |
John Spurlock | 657c62c | 2014-07-22 12:18:09 -0400 | [diff] [blame] | 76 | mKeyguard.addCallback(mCallback); |
John Spurlock | ccb6b9a | 2014-05-17 15:54:40 -0400 | [diff] [blame] | 77 | } else { |
John Spurlock | 1e6eb17 | 2014-07-13 11:59:50 -0400 | [diff] [blame] | 78 | mController.setDiscovering(false); |
John Spurlock | ccb6b9a | 2014-05-17 15:54:40 -0400 | [diff] [blame] | 79 | mController.removeCallback(mCallback); |
John Spurlock | 657c62c | 2014-07-22 12:18:09 -0400 | [diff] [blame] | 80 | mKeyguard.removeCallback(mCallback); |
John Spurlock | ccb6b9a | 2014-05-17 15:54:40 -0400 | [diff] [blame] | 81 | } |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | @Override |
| 85 | protected void handleUserSwitch(int newUserId) { |
| 86 | super.handleUserSwitch(newUserId); |
| 87 | if (mController == null) return; |
| 88 | mController.setCurrentUserId(newUserId); |
| 89 | } |
| 90 | |
| 91 | @Override |
Jason Monk | 76c67aa | 2016-02-19 14:49:42 -0500 | [diff] [blame] | 92 | public Intent getLongClickIntent() { |
| 93 | return new Intent(Settings.ACTION_CAST_SETTINGS); |
| 94 | } |
| 95 | |
| 96 | @Override |
Jason Monk | 3250885 | 2017-01-18 09:17:13 -0500 | [diff] [blame^] | 97 | protected void handleSecondaryClick() { |
| 98 | handleClick(); |
| 99 | } |
| 100 | |
| 101 | @Override |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 102 | protected void handleClick() { |
Jason Monk | ba2318e | 2015-12-08 09:04:23 -0500 | [diff] [blame] | 103 | if (mKeyguard.isSecure() && !mKeyguard.canSkipBouncer()) { |
| 104 | mHost.startRunnableDismissingKeyguard(new Runnable() { |
| 105 | @Override |
| 106 | public void run() { |
| 107 | MetricsLogger.action(mContext, getMetricsCategory()); |
| 108 | showDetail(true); |
| 109 | mHost.openPanels(); |
| 110 | } |
| 111 | }); |
| 112 | return; |
| 113 | } |
Chris Wren | 9e7283f | 2015-05-08 17:23:47 -0400 | [diff] [blame] | 114 | MetricsLogger.action(mContext, getMetricsCategory()); |
John Spurlock | 1e6eb17 | 2014-07-13 11:59:50 -0400 | [diff] [blame] | 115 | showDetail(true); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | @Override |
Jason Monk | 39c98e6 | 2016-03-16 09:18:35 -0400 | [diff] [blame] | 119 | public CharSequence getTileLabel() { |
| 120 | return mContext.getString(R.string.quick_settings_cast_title); |
| 121 | } |
| 122 | |
| 123 | @Override |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 124 | protected void handleUpdateState(BooleanState state, Object arg) { |
John Spurlock | 1e6eb17 | 2014-07-13 11:59:50 -0400 | [diff] [blame] | 125 | state.label = mContext.getString(R.string.quick_settings_cast_title); |
Julia Reynolds | 20aef8a | 2016-05-04 16:44:08 -0400 | [diff] [blame] | 126 | state.contentDescription = state.label; |
John Spurlock | 1e6eb17 | 2014-07-13 11:59:50 -0400 | [diff] [blame] | 127 | state.value = false; |
Selim Cinek | 06d3bca | 2014-08-26 17:29:20 +0200 | [diff] [blame] | 128 | state.autoMirrorDrawable = false; |
John Spurlock | 1e6eb17 | 2014-07-13 11:59:50 -0400 | [diff] [blame] | 129 | final Set<CastDevice> devices = mController.getCastDevices(); |
| 130 | boolean connecting = false; |
| 131 | for (CastDevice device : devices) { |
| 132 | if (device.state == CastDevice.STATE_CONNECTED) { |
| 133 | state.value = true; |
| 134 | state.label = getDeviceName(device); |
Julia Reynolds | 20aef8a | 2016-05-04 16:44:08 -0400 | [diff] [blame] | 135 | state.contentDescription = state.contentDescription + "," + |
| 136 | mContext.getString(R.string.accessibility_cast_name, state.label); |
John Spurlock | 1e6eb17 | 2014-07-13 11:59:50 -0400 | [diff] [blame] | 137 | } else if (device.state == CastDevice.STATE_CONNECTING) { |
| 138 | connecting = true; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 139 | } |
| 140 | } |
John Spurlock | 1e6eb17 | 2014-07-13 11:59:50 -0400 | [diff] [blame] | 141 | if (!state.value && connecting) { |
| 142 | state.label = mContext.getString(R.string.quick_settings_connecting); |
| 143 | } |
Jason Monk | 3250885 | 2017-01-18 09:17:13 -0500 | [diff] [blame^] | 144 | state.state = state.value ? Tile.STATE_ACTIVE : Tile.STATE_INACTIVE; |
John Spurlock | 2d69581 | 2014-10-30 13:25:21 -0400 | [diff] [blame] | 145 | state.icon = ResourceIcon.get(state.value ? R.drawable.ic_qs_cast_on |
| 146 | : R.drawable.ic_qs_cast_off); |
John Spurlock | 1e6eb17 | 2014-07-13 11:59:50 -0400 | [diff] [blame] | 147 | mDetailAdapter.updateItems(devices); |
Julia Reynolds | 20aef8a | 2016-05-04 16:44:08 -0400 | [diff] [blame] | 148 | state.minimalAccessibilityClassName = state.expandedAccessibilityClassName = |
| 149 | Button.class.getName(); |
| 150 | state.contentDescription = state.contentDescription + "," |
| 151 | + mContext.getString(R.string.accessibility_quick_settings_open_details); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 152 | } |
| 153 | |
Selim Cinek | 4fda7b2 | 2014-08-18 22:07:25 +0200 | [diff] [blame] | 154 | @Override |
Chris Wren | 457a21c | 2015-05-06 17:50:34 -0400 | [diff] [blame] | 155 | public int getMetricsCategory() { |
Chris Wren | f6e9228b | 2016-01-26 18:04:35 -0500 | [diff] [blame] | 156 | return MetricsEvent.QS_CAST; |
Chris Wren | 457a21c | 2015-05-06 17:50:34 -0400 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | @Override |
Selim Cinek | 4fda7b2 | 2014-08-18 22:07:25 +0200 | [diff] [blame] | 160 | protected String composeChangeAnnouncement() { |
| 161 | if (!mState.value) { |
| 162 | // We only announce when it's turned off to avoid vocal overflow. |
| 163 | return mContext.getString(R.string.accessibility_casting_turned_off); |
| 164 | } |
| 165 | return null; |
| 166 | } |
| 167 | |
John Spurlock | 1e6eb17 | 2014-07-13 11:59:50 -0400 | [diff] [blame] | 168 | private String getDeviceName(CastDevice device) { |
| 169 | return device.name != null ? device.name |
| 170 | : mContext.getString(R.string.quick_settings_cast_device_default_name); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 171 | } |
| 172 | |
John Spurlock | 657c62c | 2014-07-22 12:18:09 -0400 | [diff] [blame] | 173 | private final class Callback implements CastController.Callback, KeyguardMonitor.Callback { |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 174 | @Override |
John Spurlock | 1e6eb17 | 2014-07-13 11:59:50 -0400 | [diff] [blame] | 175 | public void onCastDevicesChanged() { |
| 176 | refreshState(); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 177 | } |
John Spurlock | 657c62c | 2014-07-22 12:18:09 -0400 | [diff] [blame] | 178 | |
| 179 | @Override |
| 180 | public void onKeyguardChanged() { |
| 181 | refreshState(); |
| 182 | } |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 183 | }; |
John Spurlock | 76c43b9 | 2014-05-13 21:10:51 -0400 | [diff] [blame] | 184 | |
John Spurlock | 1e6eb17 | 2014-07-13 11:59:50 -0400 | [diff] [blame] | 185 | private final class CastDetailAdapter implements DetailAdapter, QSDetailItems.Callback { |
| 186 | private final LinkedHashMap<String, CastDevice> mVisibleOrder = new LinkedHashMap<>(); |
| 187 | |
| 188 | private QSDetailItems mItems; |
| 189 | |
John Spurlock | 76c43b9 | 2014-05-13 21:10:51 -0400 | [diff] [blame] | 190 | @Override |
Jason Monk | c06fbb1 | 2016-01-08 14:12:18 -0500 | [diff] [blame] | 191 | public CharSequence getTitle() { |
| 192 | return mContext.getString(R.string.quick_settings_cast_title); |
John Spurlock | 76c43b9 | 2014-05-13 21:10:51 -0400 | [diff] [blame] | 193 | } |
John Spurlock | 1e6eb17 | 2014-07-13 11:59:50 -0400 | [diff] [blame] | 194 | |
| 195 | @Override |
| 196 | public Boolean getToggleState() { |
| 197 | return null; |
| 198 | } |
| 199 | |
| 200 | @Override |
| 201 | public Intent getSettingsIntent() { |
Jeff Brown | 1849262 | 2014-08-19 17:51:47 -0700 | [diff] [blame] | 202 | return CAST_SETTINGS; |
John Spurlock | 1e6eb17 | 2014-07-13 11:59:50 -0400 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | @Override |
| 206 | public void setToggleState(boolean state) { |
| 207 | // noop |
| 208 | } |
| 209 | |
| 210 | @Override |
Chris Wren | 457a21c | 2015-05-06 17:50:34 -0400 | [diff] [blame] | 211 | public int getMetricsCategory() { |
Chris Wren | f6e9228b | 2016-01-26 18:04:35 -0500 | [diff] [blame] | 212 | return MetricsEvent.QS_CAST_DETAILS; |
Chris Wren | 457a21c | 2015-05-06 17:50:34 -0400 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | @Override |
John Spurlock | 1e6eb17 | 2014-07-13 11:59:50 -0400 | [diff] [blame] | 216 | public View createDetailView(Context context, View convertView, ViewGroup parent) { |
| 217 | mItems = QSDetailItems.convertOrInflate(context, convertView, parent); |
| 218 | mItems.setTagSuffix("Cast"); |
| 219 | if (convertView == null) { |
| 220 | if (DEBUG) Log.d(TAG, "addOnAttachStateChangeListener"); |
| 221 | mItems.addOnAttachStateChangeListener(new OnAttachStateChangeListener() { |
| 222 | @Override |
| 223 | public void onViewAttachedToWindow(View v) { |
| 224 | if (DEBUG) Log.d(TAG, "onViewAttachedToWindow"); |
| 225 | } |
| 226 | |
| 227 | @Override |
| 228 | public void onViewDetachedFromWindow(View v) { |
| 229 | if (DEBUG) Log.d(TAG, "onViewDetachedFromWindow"); |
| 230 | mVisibleOrder.clear(); |
| 231 | } |
| 232 | }); |
| 233 | } |
| 234 | mItems.setEmptyState(R.drawable.ic_qs_cast_detail_empty, |
| 235 | R.string.quick_settings_cast_detail_empty_text); |
| 236 | mItems.setCallback(this); |
| 237 | updateItems(mController.getCastDevices()); |
| 238 | mController.setDiscovering(true); |
| 239 | return mItems; |
| 240 | } |
| 241 | |
| 242 | private void updateItems(Set<CastDevice> devices) { |
| 243 | if (mItems == null) return; |
| 244 | Item[] items = null; |
| 245 | if (devices != null && !devices.isEmpty()) { |
| 246 | // if we are connected, simply show that device |
| 247 | for (CastDevice device : devices) { |
| 248 | if (device.state == CastDevice.STATE_CONNECTED) { |
| 249 | final Item item = new Item(); |
| 250 | item.icon = R.drawable.ic_qs_cast_on; |
| 251 | item.line1 = getDeviceName(device); |
| 252 | item.line2 = mContext.getString(R.string.quick_settings_connected); |
| 253 | item.tag = device; |
| 254 | item.canDisconnect = true; |
| 255 | items = new Item[] { item }; |
| 256 | break; |
| 257 | } |
| 258 | } |
| 259 | // otherwise list all available devices, and don't move them around |
| 260 | if (items == null) { |
| 261 | for (CastDevice device : devices) { |
| 262 | mVisibleOrder.put(device.id, device); |
| 263 | } |
| 264 | items = new Item[devices.size()]; |
| 265 | int i = 0; |
| 266 | for (String id : mVisibleOrder.keySet()) { |
| 267 | final CastDevice device = mVisibleOrder.get(id); |
| 268 | if (!devices.contains(device)) continue; |
| 269 | final Item item = new Item(); |
| 270 | item.icon = R.drawable.ic_qs_cast_off; |
| 271 | item.line1 = getDeviceName(device); |
| 272 | if (device.state == CastDevice.STATE_CONNECTING) { |
| 273 | item.line2 = mContext.getString(R.string.quick_settings_connecting); |
| 274 | } |
| 275 | item.tag = device; |
| 276 | items[i++] = item; |
| 277 | } |
| 278 | } |
| 279 | } |
| 280 | mItems.setItems(items); |
| 281 | } |
| 282 | |
| 283 | @Override |
| 284 | public void onDetailItemClick(Item item) { |
| 285 | if (item == null || item.tag == null) return; |
Chris Wren | f6e9228b | 2016-01-26 18:04:35 -0500 | [diff] [blame] | 286 | MetricsLogger.action(mContext, MetricsEvent.QS_CAST_SELECT); |
John Spurlock | 1e6eb17 | 2014-07-13 11:59:50 -0400 | [diff] [blame] | 287 | final CastDevice device = (CastDevice) item.tag; |
| 288 | mController.startCasting(device); |
| 289 | } |
| 290 | |
| 291 | @Override |
| 292 | public void onDetailItemDisconnect(Item item) { |
| 293 | if (item == null || item.tag == null) return; |
Chris Wren | f6e9228b | 2016-01-26 18:04:35 -0500 | [diff] [blame] | 294 | MetricsLogger.action(mContext, MetricsEvent.QS_CAST_DISCONNECT); |
John Spurlock | 78b8c8f | 2014-08-25 17:52:06 -0400 | [diff] [blame] | 295 | final CastDevice device = (CastDevice) item.tag; |
| 296 | mController.stopCasting(device); |
John Spurlock | 1e6eb17 | 2014-07-13 11:59:50 -0400 | [diff] [blame] | 297 | } |
| 298 | } |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 299 | } |