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