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