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