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