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 | f6d31a0 | 2017-05-24 13:29:19 -0400 | [diff] [blame] | 19 | import static android.media.MediaRouter.ROUTE_TYPE_REMOTE_DISPLAY; |
| 20 | |
Jason Monk | ee1f6e2 | 2017-04-21 12:51:18 -0400 | [diff] [blame] | 21 | import android.app.Dialog; |
John Spurlock | 1e6eb17 | 2014-07-13 11:59:50 -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; |
Narayan Kamath | f60a627 | 2019-02-27 14:25:59 +0000 | [diff] [blame^] | 24 | import android.media.projection.MediaProjectionInfo; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 25 | import android.provider.Settings; |
Jason Monk | 3250885 | 2017-01-18 09:17:13 -0500 | [diff] [blame] | 26 | import android.service.quicksettings.Tile; |
John Spurlock | 1e6eb17 | 2014-07-13 11:59:50 -0400 | [diff] [blame] | 27 | import android.util.Log; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 28 | import android.view.View; |
John Spurlock | 1e6eb17 | 2014-07-13 11:59:50 -0400 | [diff] [blame] | 29 | import android.view.View.OnAttachStateChangeListener; |
| 30 | import android.view.ViewGroup; |
Jason Monk | 9ca1848 | 2017-08-01 10:38:02 -0400 | [diff] [blame] | 31 | import android.view.WindowManager.LayoutParams; |
Julia Reynolds | 20aef8a | 2016-05-04 16:44:08 -0400 | [diff] [blame] | 32 | import android.widget.Button; |
Chris Wren | f6e9228b | 2016-01-26 18:04:35 -0500 | [diff] [blame] | 33 | |
Jason Monk | f6d31a0 | 2017-05-24 13:29:19 -0400 | [diff] [blame] | 34 | import com.android.internal.app.MediaRouteDialogPresenter; |
Chris Wren | 457a21c | 2015-05-06 17:50:34 -0400 | [diff] [blame] | 35 | import com.android.internal.logging.MetricsLogger; |
Tamas Berghammer | 383db5eb | 2016-06-22 15:21:38 +0100 | [diff] [blame] | 36 | import com.android.internal.logging.nano.MetricsProto.MetricsEvent; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 37 | import com.android.systemui.R; |
Jason Monk | ec34da8 | 2017-02-24 15:57:05 -0500 | [diff] [blame] | 38 | import com.android.systemui.plugins.ActivityStarter; |
Jason Monk | e5b770e | 2017-03-03 21:49:29 -0500 | [diff] [blame] | 39 | import com.android.systemui.plugins.qs.DetailAdapter; |
Jason Monk | 1cbf5fb | 2017-03-30 15:23:43 -0400 | [diff] [blame] | 40 | import com.android.systemui.plugins.qs.QSTile.BooleanState; |
John Spurlock | 1e6eb17 | 2014-07-13 11:59:50 -0400 | [diff] [blame] | 41 | import com.android.systemui.qs.QSDetailItems; |
| 42 | import com.android.systemui.qs.QSDetailItems.Item; |
Jason Monk | 702e2eb | 2017-03-03 16:53:44 -0500 | [diff] [blame] | 43 | import com.android.systemui.qs.QSHost; |
Jason Monk | 702e2eb | 2017-03-03 16:53:44 -0500 | [diff] [blame] | 44 | import com.android.systemui.qs.tileimpl.QSTileImpl; |
Jason Monk | 9ca1848 | 2017-08-01 10:38:02 -0400 | [diff] [blame] | 45 | import com.android.systemui.statusbar.phone.SystemUIDialog; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 46 | import com.android.systemui.statusbar.policy.CastController; |
John Spurlock | 1e6eb17 | 2014-07-13 11:59:50 -0400 | [diff] [blame] | 47 | import com.android.systemui.statusbar.policy.CastController.CastDevice; |
John Spurlock | 657c62c | 2014-07-22 12:18:09 -0400 | [diff] [blame] | 48 | import com.android.systemui.statusbar.policy.KeyguardMonitor; |
Fabian Kozynski | 4235b2a | 2018-10-04 17:46:59 -0400 | [diff] [blame] | 49 | import com.android.systemui.statusbar.policy.NetworkController; |
John Spurlock | 1e6eb17 | 2014-07-13 11:59:50 -0400 | [diff] [blame] | 50 | |
| 51 | import java.util.LinkedHashMap; |
| 52 | import java.util.Set; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 53 | |
Jason Monk | 5d57720 | 2018-12-26 15:43:06 -0500 | [diff] [blame] | 54 | import javax.inject.Inject; |
| 55 | |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 56 | /** Quick settings tile: Cast **/ |
Jason Monk | 702e2eb | 2017-03-03 16:53:44 -0500 | [diff] [blame] | 57 | public class CastTile extends QSTileImpl<BooleanState> { |
Jeff Brown | 1849262 | 2014-08-19 17:51:47 -0700 | [diff] [blame] | 58 | private static final Intent CAST_SETTINGS = |
| 59 | new Intent(Settings.ACTION_CAST_SETTINGS); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 60 | |
| 61 | private final CastController mController; |
John Spurlock | 1e6eb17 | 2014-07-13 11:59:50 -0400 | [diff] [blame] | 62 | private final CastDetailAdapter mDetailAdapter; |
John Spurlock | 657c62c | 2014-07-22 12:18:09 -0400 | [diff] [blame] | 63 | private final KeyguardMonitor mKeyguard; |
Fabian Kozynski | 4235b2a | 2018-10-04 17:46:59 -0400 | [diff] [blame] | 64 | private final NetworkController mNetworkController; |
John Spurlock | 657c62c | 2014-07-22 12:18:09 -0400 | [diff] [blame] | 65 | private final Callback mCallback = new Callback(); |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 66 | private final ActivityStarter mActivityStarter; |
Jason Monk | ee1f6e2 | 2017-04-21 12:51:18 -0400 | [diff] [blame] | 67 | private Dialog mDialog; |
Fabian Kozynski | 4235b2a | 2018-10-04 17:46:59 -0400 | [diff] [blame] | 68 | private boolean mWifiConnected; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 69 | |
Jason Monk | 5d57720 | 2018-12-26 15:43:06 -0500 | [diff] [blame] | 70 | @Inject |
Jason Monk | c0e0e2b | 2018-12-26 16:47:41 -0500 | [diff] [blame] | 71 | public CastTile(QSHost host, CastController castController, KeyguardMonitor keyguardMonitor, |
| 72 | NetworkController networkController, ActivityStarter activityStarter) { |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 73 | super(host); |
Jason Monk | c0e0e2b | 2018-12-26 16:47:41 -0500 | [diff] [blame] | 74 | mController = castController; |
John Spurlock | 1e6eb17 | 2014-07-13 11:59:50 -0400 | [diff] [blame] | 75 | mDetailAdapter = new CastDetailAdapter(); |
Jason Monk | c0e0e2b | 2018-12-26 16:47:41 -0500 | [diff] [blame] | 76 | mKeyguard = keyguardMonitor; |
| 77 | mNetworkController = networkController; |
| 78 | mActivityStarter = activityStarter; |
Jason Monk | fa452ef | 2018-12-26 17:26:10 -0500 | [diff] [blame] | 79 | mController.observe(this, mCallback); |
| 80 | mKeyguard.observe(this, mCallback); |
| 81 | mNetworkController.observe(this, mSignalCallback); |
John Spurlock | 1e6eb17 | 2014-07-13 11:59:50 -0400 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | @Override |
| 85 | public DetailAdapter getDetailAdapter() { |
| 86 | return mDetailAdapter; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | @Override |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 90 | public BooleanState newTileState() { |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 91 | return new BooleanState(); |
| 92 | } |
| 93 | |
| 94 | @Override |
Jason Monk | 1c6116c | 2017-09-06 17:33:01 -0400 | [diff] [blame] | 95 | public void handleSetListening(boolean listening) { |
Jason Monk | 1c6116c | 2017-09-06 17:33:01 -0400 | [diff] [blame] | 96 | if (DEBUG) Log.d(TAG, "handleSetListening " + listening); |
Jason Monk | fa452ef | 2018-12-26 17:26:10 -0500 | [diff] [blame] | 97 | if (!listening) { |
John Spurlock | 1e6eb17 | 2014-07-13 11:59:50 -0400 | [diff] [blame] | 98 | mController.setDiscovering(false); |
John Spurlock | ccb6b9a | 2014-05-17 15:54:40 -0400 | [diff] [blame] | 99 | } |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | @Override |
| 103 | protected void handleUserSwitch(int newUserId) { |
| 104 | super.handleUserSwitch(newUserId); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 105 | mController.setCurrentUserId(newUserId); |
| 106 | } |
| 107 | |
| 108 | @Override |
Jason Monk | 76c67aa | 2016-02-19 14:49:42 -0500 | [diff] [blame] | 109 | public Intent getLongClickIntent() { |
| 110 | return new Intent(Settings.ACTION_CAST_SETTINGS); |
| 111 | } |
| 112 | |
| 113 | @Override |
Jason Monk | 3250885 | 2017-01-18 09:17:13 -0500 | [diff] [blame] | 114 | protected void handleSecondaryClick() { |
| 115 | handleClick(); |
| 116 | } |
| 117 | |
| 118 | @Override |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 119 | protected void handleClick() { |
Fabian Kozynski | 4235b2a | 2018-10-04 17:46:59 -0400 | [diff] [blame] | 120 | if (getState().state == Tile.STATE_UNAVAILABLE) { |
| 121 | return; |
| 122 | } |
Jason Monk | ba2318e | 2015-12-08 09:04:23 -0500 | [diff] [blame] | 123 | if (mKeyguard.isSecure() && !mKeyguard.canSkipBouncer()) { |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 124 | mActivityStarter.postQSRunnableDismissingKeyguard(() -> { |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 125 | showDetail(true); |
Jason Monk | ba2318e | 2015-12-08 09:04:23 -0500 | [diff] [blame] | 126 | }); |
| 127 | return; |
| 128 | } |
Narayan Kamath | f60a627 | 2019-02-27 14:25:59 +0000 | [diff] [blame^] | 129 | |
| 130 | CastDevice activeProjection = getActiveDeviceMediaProjection(); |
| 131 | if (activeProjection == null) { |
| 132 | showDetail(true); |
| 133 | } else { |
| 134 | mController.stopCasting(activeProjection); |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | private CastDevice getActiveDeviceMediaProjection() { |
| 139 | CastDevice activeDevice = null; |
| 140 | for (CastDevice device : mController.getCastDevices()) { |
| 141 | if (device.state == CastDevice.STATE_CONNECTED |
| 142 | || device.state == CastDevice.STATE_CONNECTING) { |
| 143 | activeDevice = device; |
| 144 | break; |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | if (activeDevice != null && activeDevice.tag instanceof MediaProjectionInfo) { |
| 149 | return activeDevice; |
| 150 | } |
| 151 | |
| 152 | return null; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | @Override |
Jason Monk | 1cbf5fb | 2017-03-30 15:23:43 -0400 | [diff] [blame] | 156 | public void showDetail(boolean show) { |
| 157 | mUiHandler.post(() -> { |
Jason Monk | f6d31a0 | 2017-05-24 13:29:19 -0400 | [diff] [blame] | 158 | mDialog = MediaRouteDialogPresenter.createDialog(mContext, ROUTE_TYPE_REMOTE_DISPLAY, |
| 159 | v -> { |
| 160 | mDialog.dismiss(); |
Jason Monk | c0e0e2b | 2018-12-26 16:47:41 -0500 | [diff] [blame] | 161 | mActivityStarter |
Jason Monk | f6d31a0 | 2017-05-24 13:29:19 -0400 | [diff] [blame] | 162 | .postStartActivityDismissingKeyguard(getLongClickIntent(), 0); |
| 163 | }); |
Jason Monk | 9ca1848 | 2017-08-01 10:38:02 -0400 | [diff] [blame] | 164 | mDialog.getWindow().setType(LayoutParams.TYPE_KEYGUARD_DIALOG); |
| 165 | SystemUIDialog.setShowForAllUsers(mDialog, true); |
| 166 | SystemUIDialog.registerDismissListener(mDialog); |
| 167 | SystemUIDialog.setWindowOnTop(mDialog); |
Jason Monk | fa3478a | 2017-05-08 10:32:03 -0400 | [diff] [blame] | 168 | mUiHandler.post(() -> mDialog.show()); |
Jason Monk | 1cbf5fb | 2017-03-30 15:23:43 -0400 | [diff] [blame] | 169 | mHost.collapsePanels(); |
| 170 | }); |
| 171 | } |
| 172 | |
| 173 | @Override |
Jason Monk | 39c98e6 | 2016-03-16 09:18:35 -0400 | [diff] [blame] | 174 | public CharSequence getTileLabel() { |
| 175 | return mContext.getString(R.string.quick_settings_cast_title); |
| 176 | } |
| 177 | |
| 178 | @Override |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 179 | protected void handleUpdateState(BooleanState state, Object arg) { |
John Spurlock | 1e6eb17 | 2014-07-13 11:59:50 -0400 | [diff] [blame] | 180 | state.label = mContext.getString(R.string.quick_settings_cast_title); |
Julia Reynolds | 20aef8a | 2016-05-04 16:44:08 -0400 | [diff] [blame] | 181 | state.contentDescription = state.label; |
John Spurlock | 1e6eb17 | 2014-07-13 11:59:50 -0400 | [diff] [blame] | 182 | state.value = false; |
| 183 | final Set<CastDevice> devices = mController.getCastDevices(); |
| 184 | boolean connecting = false; |
| 185 | for (CastDevice device : devices) { |
| 186 | if (device.state == CastDevice.STATE_CONNECTED) { |
| 187 | state.value = true; |
| 188 | state.label = getDeviceName(device); |
Julia Reynolds | 20aef8a | 2016-05-04 16:44:08 -0400 | [diff] [blame] | 189 | state.contentDescription = state.contentDescription + "," + |
| 190 | mContext.getString(R.string.accessibility_cast_name, state.label); |
John Spurlock | 1e6eb17 | 2014-07-13 11:59:50 -0400 | [diff] [blame] | 191 | } else if (device.state == CastDevice.STATE_CONNECTING) { |
| 192 | connecting = true; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 193 | } |
| 194 | } |
John Spurlock | 1e6eb17 | 2014-07-13 11:59:50 -0400 | [diff] [blame] | 195 | if (!state.value && connecting) { |
| 196 | state.label = mContext.getString(R.string.quick_settings_connecting); |
| 197 | } |
John Spurlock | 2d69581 | 2014-10-30 13:25:21 -0400 | [diff] [blame] | 198 | state.icon = ResourceIcon.get(state.value ? R.drawable.ic_qs_cast_on |
| 199 | : R.drawable.ic_qs_cast_off); |
Fabian Kozynski | 4235b2a | 2018-10-04 17:46:59 -0400 | [diff] [blame] | 200 | if (mWifiConnected) { |
| 201 | state.state = state.value ? Tile.STATE_ACTIVE : Tile.STATE_INACTIVE; |
| 202 | state.secondaryLabel = ""; |
| 203 | state.contentDescription = state.contentDescription + "," |
| 204 | + mContext.getString(R.string.accessibility_quick_settings_open_details); |
| 205 | state.expandedAccessibilityClassName = Button.class.getName(); |
| 206 | } else { |
| 207 | state.state = Tile.STATE_UNAVAILABLE; |
| 208 | String noWifi = mContext.getString(R.string.quick_settings_cast_no_wifi); |
| 209 | state.secondaryLabel = noWifi; |
| 210 | state.contentDescription = state.contentDescription + ", " + mContext.getString( |
| 211 | R.string.accessibility_quick_settings_not_available, noWifi); |
| 212 | } |
John Spurlock | 1e6eb17 | 2014-07-13 11:59:50 -0400 | [diff] [blame] | 213 | mDetailAdapter.updateItems(devices); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 214 | } |
| 215 | |
Selim Cinek | 4fda7b2 | 2014-08-18 22:07:25 +0200 | [diff] [blame] | 216 | @Override |
Chris Wren | 457a21c | 2015-05-06 17:50:34 -0400 | [diff] [blame] | 217 | public int getMetricsCategory() { |
Chris Wren | f6e9228b | 2016-01-26 18:04:35 -0500 | [diff] [blame] | 218 | return MetricsEvent.QS_CAST; |
Chris Wren | 457a21c | 2015-05-06 17:50:34 -0400 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | @Override |
Selim Cinek | 4fda7b2 | 2014-08-18 22:07:25 +0200 | [diff] [blame] | 222 | protected String composeChangeAnnouncement() { |
| 223 | if (!mState.value) { |
| 224 | // We only announce when it's turned off to avoid vocal overflow. |
| 225 | return mContext.getString(R.string.accessibility_casting_turned_off); |
| 226 | } |
| 227 | return null; |
| 228 | } |
| 229 | |
John Spurlock | 1e6eb17 | 2014-07-13 11:59:50 -0400 | [diff] [blame] | 230 | private String getDeviceName(CastDevice device) { |
| 231 | return device.name != null ? device.name |
| 232 | : mContext.getString(R.string.quick_settings_cast_device_default_name); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 233 | } |
| 234 | |
Fabian Kozynski | 4235b2a | 2018-10-04 17:46:59 -0400 | [diff] [blame] | 235 | private final NetworkController.SignalCallback mSignalCallback = |
| 236 | new NetworkController.SignalCallback() { |
| 237 | @Override |
| 238 | public void setWifiIndicators(boolean enabled, |
| 239 | NetworkController.IconState statusIcon, |
| 240 | NetworkController.IconState qsIcon, boolean activityIn, boolean activityOut, |
| 241 | String description, boolean isTransient, String statusLabel) { |
| 242 | // statusIcon.visible has the connected status information |
| 243 | boolean enabledAndConnected = enabled && qsIcon.visible; |
| 244 | if (enabledAndConnected != mWifiConnected) { |
| 245 | mWifiConnected = enabledAndConnected; |
| 246 | refreshState(); |
| 247 | } |
| 248 | } |
| 249 | }; |
| 250 | |
John Spurlock | 657c62c | 2014-07-22 12:18:09 -0400 | [diff] [blame] | 251 | private final class Callback implements CastController.Callback, KeyguardMonitor.Callback { |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 252 | @Override |
John Spurlock | 1e6eb17 | 2014-07-13 11:59:50 -0400 | [diff] [blame] | 253 | public void onCastDevicesChanged() { |
| 254 | refreshState(); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 255 | } |
John Spurlock | 657c62c | 2014-07-22 12:18:09 -0400 | [diff] [blame] | 256 | |
| 257 | @Override |
Jason Monk | aa573e9 | 2017-01-27 17:00:29 -0500 | [diff] [blame] | 258 | public void onKeyguardShowingChanged() { |
John Spurlock | 657c62c | 2014-07-22 12:18:09 -0400 | [diff] [blame] | 259 | refreshState(); |
| 260 | } |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 261 | }; |
John Spurlock | 76c43b9 | 2014-05-13 21:10:51 -0400 | [diff] [blame] | 262 | |
John Spurlock | 1e6eb17 | 2014-07-13 11:59:50 -0400 | [diff] [blame] | 263 | private final class CastDetailAdapter implements DetailAdapter, QSDetailItems.Callback { |
| 264 | private final LinkedHashMap<String, CastDevice> mVisibleOrder = new LinkedHashMap<>(); |
| 265 | |
| 266 | private QSDetailItems mItems; |
| 267 | |
John Spurlock | 76c43b9 | 2014-05-13 21:10:51 -0400 | [diff] [blame] | 268 | @Override |
Jason Monk | c06fbb1 | 2016-01-08 14:12:18 -0500 | [diff] [blame] | 269 | public CharSequence getTitle() { |
| 270 | return mContext.getString(R.string.quick_settings_cast_title); |
John Spurlock | 76c43b9 | 2014-05-13 21:10:51 -0400 | [diff] [blame] | 271 | } |
John Spurlock | 1e6eb17 | 2014-07-13 11:59:50 -0400 | [diff] [blame] | 272 | |
| 273 | @Override |
| 274 | public Boolean getToggleState() { |
| 275 | return null; |
| 276 | } |
| 277 | |
| 278 | @Override |
| 279 | public Intent getSettingsIntent() { |
Jeff Brown | 1849262 | 2014-08-19 17:51:47 -0700 | [diff] [blame] | 280 | return CAST_SETTINGS; |
John Spurlock | 1e6eb17 | 2014-07-13 11:59:50 -0400 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | @Override |
| 284 | public void setToggleState(boolean state) { |
| 285 | // noop |
| 286 | } |
| 287 | |
| 288 | @Override |
Chris Wren | 457a21c | 2015-05-06 17:50:34 -0400 | [diff] [blame] | 289 | public int getMetricsCategory() { |
Chris Wren | f6e9228b | 2016-01-26 18:04:35 -0500 | [diff] [blame] | 290 | return MetricsEvent.QS_CAST_DETAILS; |
Chris Wren | 457a21c | 2015-05-06 17:50:34 -0400 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | @Override |
John Spurlock | 1e6eb17 | 2014-07-13 11:59:50 -0400 | [diff] [blame] | 294 | public View createDetailView(Context context, View convertView, ViewGroup parent) { |
| 295 | mItems = QSDetailItems.convertOrInflate(context, convertView, parent); |
| 296 | mItems.setTagSuffix("Cast"); |
| 297 | if (convertView == null) { |
| 298 | if (DEBUG) Log.d(TAG, "addOnAttachStateChangeListener"); |
| 299 | mItems.addOnAttachStateChangeListener(new OnAttachStateChangeListener() { |
| 300 | @Override |
| 301 | public void onViewAttachedToWindow(View v) { |
| 302 | if (DEBUG) Log.d(TAG, "onViewAttachedToWindow"); |
| 303 | } |
| 304 | |
| 305 | @Override |
| 306 | public void onViewDetachedFromWindow(View v) { |
| 307 | if (DEBUG) Log.d(TAG, "onViewDetachedFromWindow"); |
| 308 | mVisibleOrder.clear(); |
| 309 | } |
| 310 | }); |
| 311 | } |
| 312 | mItems.setEmptyState(R.drawable.ic_qs_cast_detail_empty, |
| 313 | R.string.quick_settings_cast_detail_empty_text); |
| 314 | mItems.setCallback(this); |
| 315 | updateItems(mController.getCastDevices()); |
| 316 | mController.setDiscovering(true); |
| 317 | return mItems; |
| 318 | } |
| 319 | |
| 320 | private void updateItems(Set<CastDevice> devices) { |
| 321 | if (mItems == null) return; |
| 322 | Item[] items = null; |
| 323 | if (devices != null && !devices.isEmpty()) { |
| 324 | // if we are connected, simply show that device |
| 325 | for (CastDevice device : devices) { |
| 326 | if (device.state == CastDevice.STATE_CONNECTED) { |
| 327 | final Item item = new Item(); |
jackqdyulei | 786a490 | 2017-10-24 13:27:53 -0700 | [diff] [blame] | 328 | item.iconResId = R.drawable.ic_qs_cast_on; |
John Spurlock | 1e6eb17 | 2014-07-13 11:59:50 -0400 | [diff] [blame] | 329 | item.line1 = getDeviceName(device); |
| 330 | item.line2 = mContext.getString(R.string.quick_settings_connected); |
| 331 | item.tag = device; |
| 332 | item.canDisconnect = true; |
| 333 | items = new Item[] { item }; |
| 334 | break; |
| 335 | } |
| 336 | } |
| 337 | // otherwise list all available devices, and don't move them around |
| 338 | if (items == null) { |
| 339 | for (CastDevice device : devices) { |
| 340 | mVisibleOrder.put(device.id, device); |
| 341 | } |
| 342 | items = new Item[devices.size()]; |
| 343 | int i = 0; |
| 344 | for (String id : mVisibleOrder.keySet()) { |
| 345 | final CastDevice device = mVisibleOrder.get(id); |
| 346 | if (!devices.contains(device)) continue; |
| 347 | final Item item = new Item(); |
jackqdyulei | 786a490 | 2017-10-24 13:27:53 -0700 | [diff] [blame] | 348 | item.iconResId = R.drawable.ic_qs_cast_off; |
John Spurlock | 1e6eb17 | 2014-07-13 11:59:50 -0400 | [diff] [blame] | 349 | item.line1 = getDeviceName(device); |
| 350 | if (device.state == CastDevice.STATE_CONNECTING) { |
| 351 | item.line2 = mContext.getString(R.string.quick_settings_connecting); |
| 352 | } |
| 353 | item.tag = device; |
| 354 | items[i++] = item; |
| 355 | } |
| 356 | } |
| 357 | } |
| 358 | mItems.setItems(items); |
| 359 | } |
| 360 | |
| 361 | @Override |
| 362 | public void onDetailItemClick(Item item) { |
| 363 | if (item == null || item.tag == null) return; |
Chris Wren | f6e9228b | 2016-01-26 18:04:35 -0500 | [diff] [blame] | 364 | MetricsLogger.action(mContext, MetricsEvent.QS_CAST_SELECT); |
John Spurlock | 1e6eb17 | 2014-07-13 11:59:50 -0400 | [diff] [blame] | 365 | final CastDevice device = (CastDevice) item.tag; |
| 366 | mController.startCasting(device); |
| 367 | } |
| 368 | |
| 369 | @Override |
| 370 | public void onDetailItemDisconnect(Item item) { |
| 371 | if (item == null || item.tag == null) return; |
Chris Wren | f6e9228b | 2016-01-26 18:04:35 -0500 | [diff] [blame] | 372 | MetricsLogger.action(mContext, MetricsEvent.QS_CAST_DISCONNECT); |
John Spurlock | 78b8c8f | 2014-08-25 17:52:06 -0400 | [diff] [blame] | 373 | final CastDevice device = (CastDevice) item.tag; |
| 374 | mController.stopCasting(device); |
John Spurlock | 1e6eb17 | 2014-07-13 11:59:50 -0400 | [diff] [blame] | 375 | } |
| 376 | } |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 377 | } |