Julia Reynolds | fba474a8 | 2017-12-13 15:35:54 -0500 | [diff] [blame] | 1 | /* |
Julia Reynolds | 5daa472 | 2017-12-20 09:53:59 -0500 | [diff] [blame] | 2 | * Copyright (C) 2017 The Android Open Source Project |
Julia Reynolds | fba474a8 | 2017-12-13 15:35:54 -0500 | [diff] [blame] | 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.volume; |
| 18 | |
Julia Reynolds | dd2b96b | 2017-12-15 10:35:57 -0500 | [diff] [blame] | 19 | import static android.support.v7.media.MediaRouter.RouteInfo.CONNECTION_STATE_CONNECTED; |
| 20 | import static android.support.v7.media.MediaRouter.RouteInfo.CONNECTION_STATE_CONNECTING; |
| 21 | import static android.support.v7.media.MediaRouter.UNSELECT_REASON_DISCONNECTED; |
| 22 | |
Julia Reynolds | fba474a8 | 2017-12-13 15:35:54 -0500 | [diff] [blame] | 23 | import static com.android.settingslib.bluetooth.Utils.getBtClassDrawableWithDescription; |
| 24 | |
Julia Reynolds | f5e4182 | 2018-01-23 13:55:18 -0500 | [diff] [blame] | 25 | import android.app.Dialog; |
Julia Reynolds | fba474a8 | 2017-12-13 15:35:54 -0500 | [diff] [blame] | 26 | import android.bluetooth.BluetoothClass; |
| 27 | import android.bluetooth.BluetoothDevice; |
| 28 | import android.bluetooth.BluetoothProfile; |
| 29 | import android.content.BroadcastReceiver; |
| 30 | import android.content.Context; |
| 31 | import android.content.DialogInterface; |
| 32 | import android.content.Intent; |
| 33 | import android.content.IntentFilter; |
Julia Reynolds | f5e4182 | 2018-01-23 13:55:18 -0500 | [diff] [blame] | 34 | import android.graphics.Color; |
| 35 | import android.graphics.drawable.ColorDrawable; |
Julia Reynolds | fba474a8 | 2017-12-13 15:35:54 -0500 | [diff] [blame] | 36 | import android.graphics.drawable.Drawable; |
Julia Reynolds | dd2b96b | 2017-12-15 10:35:57 -0500 | [diff] [blame] | 37 | import android.net.Uri; |
| 38 | import android.net.wifi.WifiManager; |
Julia Reynolds | fba474a8 | 2017-12-13 15:35:54 -0500 | [diff] [blame] | 39 | import android.os.Bundle; |
Julia Reynolds | dd2b96b | 2017-12-15 10:35:57 -0500 | [diff] [blame] | 40 | import android.os.Handler; |
| 41 | import android.os.Message; |
| 42 | import android.os.SystemClock; |
| 43 | import android.support.v7.media.MediaControlIntent; |
| 44 | import android.support.v7.media.MediaRouteSelector; |
| 45 | import android.support.v7.media.MediaRouter; |
Julia Reynolds | 5daa472 | 2017-12-20 09:53:59 -0500 | [diff] [blame] | 46 | import android.telecom.TelecomManager; |
Julia Reynolds | fba474a8 | 2017-12-13 15:35:54 -0500 | [diff] [blame] | 47 | import android.util.Log; |
| 48 | import android.util.Pair; |
Julia Reynolds | f5e4182 | 2018-01-23 13:55:18 -0500 | [diff] [blame] | 49 | import android.view.Window; |
| 50 | import android.view.WindowManager; |
Julia Reynolds | fba474a8 | 2017-12-13 15:35:54 -0500 | [diff] [blame] | 51 | |
| 52 | import com.android.settingslib.Utils; |
| 53 | import com.android.settingslib.bluetooth.CachedBluetoothDevice; |
| 54 | import com.android.systemui.Dependency; |
Julia Reynolds | f5e4182 | 2018-01-23 13:55:18 -0500 | [diff] [blame] | 55 | import com.android.systemui.HardwareUiLayout; |
| 56 | import com.android.systemui.Interpolators; |
Julia Reynolds | fba474a8 | 2017-12-13 15:35:54 -0500 | [diff] [blame] | 57 | import com.android.systemui.R; |
Julia Reynolds | f5e4182 | 2018-01-23 13:55:18 -0500 | [diff] [blame] | 58 | import com.android.systemui.plugins.VolumeDialogController; |
Julia Reynolds | fba474a8 | 2017-12-13 15:35:54 -0500 | [diff] [blame] | 59 | import com.android.systemui.statusbar.policy.BluetoothController; |
| 60 | |
Julia Reynolds | dd2b96b | 2017-12-15 10:35:57 -0500 | [diff] [blame] | 61 | import java.io.IOException; |
| 62 | import java.io.InputStream; |
Julia Reynolds | fba474a8 | 2017-12-13 15:35:54 -0500 | [diff] [blame] | 63 | import java.util.ArrayList; |
| 64 | import java.util.Collection; |
Julia Reynolds | dd2b96b | 2017-12-15 10:35:57 -0500 | [diff] [blame] | 65 | import java.util.Comparator; |
| 66 | import java.util.List; |
Julia Reynolds | fba474a8 | 2017-12-13 15:35:54 -0500 | [diff] [blame] | 67 | |
Julia Reynolds | f5e4182 | 2018-01-23 13:55:18 -0500 | [diff] [blame] | 68 | public class OutputChooserDialog extends Dialog |
Julia Reynolds | fba474a8 | 2017-12-13 15:35:54 -0500 | [diff] [blame] | 69 | implements DialogInterface.OnDismissListener, OutputChooserLayout.Callback { |
| 70 | |
| 71 | private static final String TAG = Util.logTag(OutputChooserDialog.class); |
| 72 | private static final int MAX_DEVICES = 10; |
| 73 | |
Julia Reynolds | dd2b96b | 2017-12-15 10:35:57 -0500 | [diff] [blame] | 74 | private static final long UPDATE_DELAY_MS = 300L; |
Julia Reynolds | 5daa472 | 2017-12-20 09:53:59 -0500 | [diff] [blame] | 75 | private static final int MSG_UPDATE_ITEMS = 1; |
Julia Reynolds | dd2b96b | 2017-12-15 10:35:57 -0500 | [diff] [blame] | 76 | |
Julia Reynolds | fba474a8 | 2017-12-13 15:35:54 -0500 | [diff] [blame] | 77 | private final Context mContext; |
Julia Reynolds | 5daa472 | 2017-12-20 09:53:59 -0500 | [diff] [blame] | 78 | private final BluetoothController mBluetoothController; |
| 79 | private WifiManager mWifiManager; |
Julia Reynolds | fba474a8 | 2017-12-13 15:35:54 -0500 | [diff] [blame] | 80 | private OutputChooserLayout mView; |
Julia Reynolds | 5daa472 | 2017-12-20 09:53:59 -0500 | [diff] [blame] | 81 | private final MediaRouterWrapper mRouter; |
Julia Reynolds | dd2b96b | 2017-12-15 10:35:57 -0500 | [diff] [blame] | 82 | private final MediaRouterCallback mRouterCallback; |
| 83 | private long mLastUpdateTime; |
Julia Reynolds | 5daa472 | 2017-12-20 09:53:59 -0500 | [diff] [blame] | 84 | private boolean mIsInCall; |
| 85 | protected boolean isAttached; |
Julia Reynolds | fba474a8 | 2017-12-13 15:35:54 -0500 | [diff] [blame] | 86 | |
Julia Reynolds | dd2b96b | 2017-12-15 10:35:57 -0500 | [diff] [blame] | 87 | private final MediaRouteSelector mRouteSelector; |
| 88 | private Drawable mDefaultIcon; |
| 89 | private Drawable mTvIcon; |
| 90 | private Drawable mSpeakerIcon; |
| 91 | private Drawable mSpeakerGroupIcon; |
Julia Reynolds | f5e4182 | 2018-01-23 13:55:18 -0500 | [diff] [blame] | 92 | private HardwareUiLayout mHardwareLayout; |
| 93 | private final VolumeDialogController mController; |
Julia Reynolds | fba474a8 | 2017-12-13 15:35:54 -0500 | [diff] [blame] | 94 | |
Julia Reynolds | 5daa472 | 2017-12-20 09:53:59 -0500 | [diff] [blame] | 95 | public OutputChooserDialog(Context context, MediaRouterWrapper router) { |
Julia Reynolds | f5e4182 | 2018-01-23 13:55:18 -0500 | [diff] [blame] | 96 | super(context, com.android.systemui.R.style.qs_theme); |
Julia Reynolds | fba474a8 | 2017-12-13 15:35:54 -0500 | [diff] [blame] | 97 | mContext = context; |
Julia Reynolds | 5daa472 | 2017-12-20 09:53:59 -0500 | [diff] [blame] | 98 | mBluetoothController = Dependency.get(BluetoothController.class); |
Julia Reynolds | dd2b96b | 2017-12-15 10:35:57 -0500 | [diff] [blame] | 99 | mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); |
Julia Reynolds | 5daa472 | 2017-12-20 09:53:59 -0500 | [diff] [blame] | 100 | TelecomManager tm = (TelecomManager) context.getSystemService(Context.TELECOM_SERVICE); |
| 101 | mIsInCall = tm.isInCall(); |
| 102 | mRouter = router; |
Julia Reynolds | dd2b96b | 2017-12-15 10:35:57 -0500 | [diff] [blame] | 103 | mRouterCallback = new MediaRouterCallback(); |
| 104 | mRouteSelector = new MediaRouteSelector.Builder() |
| 105 | .addControlCategory(MediaControlIntent.CATEGORY_REMOTE_PLAYBACK) |
| 106 | .build(); |
Julia Reynolds | fba474a8 | 2017-12-13 15:35:54 -0500 | [diff] [blame] | 107 | |
| 108 | final IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS); |
| 109 | context.registerReceiver(mReceiver, filter); |
Julia Reynolds | f5e4182 | 2018-01-23 13:55:18 -0500 | [diff] [blame] | 110 | |
| 111 | mController = Dependency.get(VolumeDialogController.class); |
| 112 | |
| 113 | // Window initialization |
| 114 | Window window = getWindow(); |
| 115 | window.requestFeature(Window.FEATURE_NO_TITLE); |
| 116 | window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); |
| 117 | window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND |
| 118 | | WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR); |
| 119 | window.addFlags( |
| 120 | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN |
| 121 | | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
| 122 | | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
| 123 | | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH |
| 124 | | WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED); |
| 125 | window.setType(WindowManager.LayoutParams.TYPE_VOLUME_OVERLAY); |
Julia Reynolds | fba474a8 | 2017-12-13 15:35:54 -0500 | [diff] [blame] | 126 | } |
| 127 | |
Julia Reynolds | 5daa472 | 2017-12-20 09:53:59 -0500 | [diff] [blame] | 128 | protected void setIsInCall(boolean inCall) { |
| 129 | mIsInCall = inCall; |
| 130 | } |
| 131 | |
Julia Reynolds | fba474a8 | 2017-12-13 15:35:54 -0500 | [diff] [blame] | 132 | @Override |
| 133 | protected void onCreate(Bundle savedInstanceState) { |
| 134 | super.onCreate(savedInstanceState); |
| 135 | setContentView(R.layout.output_chooser); |
| 136 | setCanceledOnTouchOutside(true); |
| 137 | setOnDismissListener(this::onDismiss); |
Julia Reynolds | dd2b96b | 2017-12-15 10:35:57 -0500 | [diff] [blame] | 138 | |
Julia Reynolds | fba474a8 | 2017-12-13 15:35:54 -0500 | [diff] [blame] | 139 | mView = findViewById(R.id.output_chooser); |
Julia Reynolds | f5e4182 | 2018-01-23 13:55:18 -0500 | [diff] [blame] | 140 | mHardwareLayout = HardwareUiLayout.get(mView); |
| 141 | mHardwareLayout.setOutsideTouchListener(view -> dismiss()); |
| 142 | mHardwareLayout.setSwapOrientation(false); |
Julia Reynolds | fba474a8 | 2017-12-13 15:35:54 -0500 | [diff] [blame] | 143 | mView.setCallback(this); |
Julia Reynolds | dd2b96b | 2017-12-15 10:35:57 -0500 | [diff] [blame] | 144 | |
Julia Reynolds | 5daa472 | 2017-12-20 09:53:59 -0500 | [diff] [blame] | 145 | if (mIsInCall) { |
| 146 | mView.setTitle(R.string.output_calls_title); |
| 147 | } else { |
| 148 | mView.setTitle(R.string.output_title); |
| 149 | } |
| 150 | |
Julia Reynolds | dd2b96b | 2017-12-15 10:35:57 -0500 | [diff] [blame] | 151 | mDefaultIcon = mContext.getDrawable(R.drawable.ic_cast); |
| 152 | mTvIcon = mContext.getDrawable(R.drawable.ic_tv); |
| 153 | mSpeakerIcon = mContext.getDrawable(R.drawable.ic_speaker); |
| 154 | mSpeakerGroupIcon = mContext.getDrawable(R.drawable.ic_speaker_group); |
| 155 | |
| 156 | final boolean wifiOff = !mWifiManager.isWifiEnabled(); |
Julia Reynolds | 5daa472 | 2017-12-20 09:53:59 -0500 | [diff] [blame] | 157 | final boolean btOff = !mBluetoothController.isBluetoothEnabled(); |
| 158 | if (wifiOff && btOff) { |
Julia Reynolds | dd2b96b | 2017-12-15 10:35:57 -0500 | [diff] [blame] | 159 | mView.setEmptyState(getDisabledServicesMessage(wifiOff, btOff)); |
| 160 | } |
Julia Reynolds | 5daa472 | 2017-12-20 09:53:59 -0500 | [diff] [blame] | 161 | // time out after 5 seconds |
| 162 | mView.postDelayed(() -> updateItems(true), 5000); |
Julia Reynolds | fba474a8 | 2017-12-13 15:35:54 -0500 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | protected void cleanUp() {} |
| 166 | |
| 167 | |
| 168 | @Override |
| 169 | protected void onStart() { |
| 170 | super.onStart(); |
| 171 | } |
| 172 | |
| 173 | @Override |
Julia Reynolds | dd2b96b | 2017-12-15 10:35:57 -0500 | [diff] [blame] | 174 | public void onAttachedToWindow() { |
| 175 | super.onAttachedToWindow(); |
| 176 | |
Julia Reynolds | 5daa472 | 2017-12-20 09:53:59 -0500 | [diff] [blame] | 177 | if (!mIsInCall) { |
| 178 | mRouter.addCallback(mRouteSelector, mRouterCallback, |
| 179 | MediaRouter.CALLBACK_FLAG_PERFORM_ACTIVE_SCAN); |
| 180 | } |
| 181 | mBluetoothController.addCallback(mCallback); |
Julia Reynolds | f5e4182 | 2018-01-23 13:55:18 -0500 | [diff] [blame] | 182 | mController.addCallback(mControllerCallbackH, mHandler); |
Julia Reynolds | 5daa472 | 2017-12-20 09:53:59 -0500 | [diff] [blame] | 183 | isAttached = true; |
Julia Reynolds | dd2b96b | 2017-12-15 10:35:57 -0500 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | @Override |
| 187 | public void onDetachedFromWindow() { |
Julia Reynolds | 5daa472 | 2017-12-20 09:53:59 -0500 | [diff] [blame] | 188 | isAttached = false; |
Julia Reynolds | dd2b96b | 2017-12-15 10:35:57 -0500 | [diff] [blame] | 189 | mRouter.removeCallback(mRouterCallback); |
Julia Reynolds | f5e4182 | 2018-01-23 13:55:18 -0500 | [diff] [blame] | 190 | mController.removeCallback(mControllerCallbackH); |
Julia Reynolds | 5daa472 | 2017-12-20 09:53:59 -0500 | [diff] [blame] | 191 | mBluetoothController.removeCallback(mCallback); |
Julia Reynolds | dd2b96b | 2017-12-15 10:35:57 -0500 | [diff] [blame] | 192 | super.onDetachedFromWindow(); |
| 193 | } |
| 194 | |
| 195 | @Override |
Julia Reynolds | fba474a8 | 2017-12-13 15:35:54 -0500 | [diff] [blame] | 196 | public void onDismiss(DialogInterface unused) { |
| 197 | mContext.unregisterReceiver(mReceiver); |
Julia Reynolds | fba474a8 | 2017-12-13 15:35:54 -0500 | [diff] [blame] | 198 | cleanUp(); |
| 199 | } |
| 200 | |
| 201 | @Override |
Julia Reynolds | f5e4182 | 2018-01-23 13:55:18 -0500 | [diff] [blame] | 202 | public void show() { |
| 203 | super.show(); |
| 204 | mHardwareLayout.setTranslationX(getAnimTranslation()); |
| 205 | mHardwareLayout.setAlpha(0); |
| 206 | mHardwareLayout.animate() |
| 207 | .alpha(1) |
| 208 | .translationX(0) |
| 209 | .setDuration(300) |
| 210 | .setInterpolator(Interpolators.FAST_OUT_SLOW_IN) |
| 211 | .withEndAction(() -> getWindow().getDecorView().requestAccessibilityFocus()) |
| 212 | .start(); |
| 213 | } |
| 214 | |
| 215 | @Override |
| 216 | public void dismiss() { |
| 217 | mHardwareLayout.setTranslationX(0); |
| 218 | mHardwareLayout.setAlpha(1); |
| 219 | mHardwareLayout.animate() |
| 220 | .alpha(0) |
| 221 | .translationX(getAnimTranslation()) |
| 222 | .setDuration(300) |
| 223 | .withEndAction(() -> super.dismiss()) |
| 224 | .setInterpolator(new SystemUIInterpolators.LogAccelerateInterpolator()) |
| 225 | .start(); |
| 226 | } |
| 227 | |
| 228 | private float getAnimTranslation() { |
| 229 | return getContext().getResources().getDimension( |
| 230 | com.android.systemui.R.dimen.output_chooser_panel_width) / 2; |
| 231 | } |
| 232 | |
| 233 | @Override |
Julia Reynolds | fba474a8 | 2017-12-13 15:35:54 -0500 | [diff] [blame] | 234 | public void onDetailItemClick(OutputChooserLayout.Item item) { |
| 235 | if (item == null || item.tag == null) return; |
Julia Reynolds | dd2b96b | 2017-12-15 10:35:57 -0500 | [diff] [blame] | 236 | if (item.deviceType == OutputChooserLayout.Item.DEVICE_TYPE_BT) { |
| 237 | final CachedBluetoothDevice device = (CachedBluetoothDevice) item.tag; |
Julia Reynolds | 5daa472 | 2017-12-20 09:53:59 -0500 | [diff] [blame] | 238 | if (device.getMaxConnectionState() == BluetoothProfile.STATE_DISCONNECTED) { |
| 239 | mBluetoothController.connect(device); |
Julia Reynolds | dd2b96b | 2017-12-15 10:35:57 -0500 | [diff] [blame] | 240 | } |
| 241 | } else if (item.deviceType == OutputChooserLayout.Item.DEVICE_TYPE_MEDIA_ROUTER) { |
| 242 | final MediaRouter.RouteInfo route = (MediaRouter.RouteInfo) item.tag; |
| 243 | if (route.isEnabled()) { |
| 244 | route.select(); |
| 245 | } |
Julia Reynolds | fba474a8 | 2017-12-13 15:35:54 -0500 | [diff] [blame] | 246 | } |
| 247 | } |
| 248 | |
| 249 | @Override |
| 250 | public void onDetailItemDisconnect(OutputChooserLayout.Item item) { |
| 251 | if (item == null || item.tag == null) return; |
Julia Reynolds | dd2b96b | 2017-12-15 10:35:57 -0500 | [diff] [blame] | 252 | if (item.deviceType == OutputChooserLayout.Item.DEVICE_TYPE_BT) { |
| 253 | final CachedBluetoothDevice device = (CachedBluetoothDevice) item.tag; |
Julia Reynolds | 5daa472 | 2017-12-20 09:53:59 -0500 | [diff] [blame] | 254 | mBluetoothController.disconnect(device); |
Julia Reynolds | dd2b96b | 2017-12-15 10:35:57 -0500 | [diff] [blame] | 255 | } else if (item.deviceType == OutputChooserLayout.Item.DEVICE_TYPE_MEDIA_ROUTER) { |
| 256 | mRouter.unselect(UNSELECT_REASON_DISCONNECTED); |
Julia Reynolds | fba474a8 | 2017-12-13 15:35:54 -0500 | [diff] [blame] | 257 | } |
| 258 | } |
| 259 | |
Julia Reynolds | 5daa472 | 2017-12-20 09:53:59 -0500 | [diff] [blame] | 260 | private void updateItems(boolean timeout) { |
Julia Reynolds | dd2b96b | 2017-12-15 10:35:57 -0500 | [diff] [blame] | 261 | if (SystemClock.uptimeMillis() - mLastUpdateTime < UPDATE_DELAY_MS) { |
| 262 | mHandler.removeMessages(MSG_UPDATE_ITEMS); |
Julia Reynolds | 5daa472 | 2017-12-20 09:53:59 -0500 | [diff] [blame] | 263 | mHandler.sendMessageAtTime(mHandler.obtainMessage(MSG_UPDATE_ITEMS, timeout), |
Julia Reynolds | dd2b96b | 2017-12-15 10:35:57 -0500 | [diff] [blame] | 264 | mLastUpdateTime + UPDATE_DELAY_MS); |
| 265 | return; |
Julia Reynolds | fba474a8 | 2017-12-13 15:35:54 -0500 | [diff] [blame] | 266 | } |
Julia Reynolds | dd2b96b | 2017-12-15 10:35:57 -0500 | [diff] [blame] | 267 | mLastUpdateTime = SystemClock.uptimeMillis(); |
| 268 | if (mView == null) return; |
Julia Reynolds | fba474a8 | 2017-12-13 15:35:54 -0500 | [diff] [blame] | 269 | ArrayList<OutputChooserLayout.Item> items = new ArrayList<>(); |
Julia Reynolds | dd2b96b | 2017-12-15 10:35:57 -0500 | [diff] [blame] | 270 | |
| 271 | // Add bluetooth devices |
| 272 | addBluetoothDevices(items); |
| 273 | |
| 274 | // Add remote displays |
Julia Reynolds | 5daa472 | 2017-12-20 09:53:59 -0500 | [diff] [blame] | 275 | if (!mIsInCall) { |
| 276 | addRemoteDisplayRoutes(items); |
| 277 | } |
Julia Reynolds | dd2b96b | 2017-12-15 10:35:57 -0500 | [diff] [blame] | 278 | |
Julia Reynolds | 5daa472 | 2017-12-20 09:53:59 -0500 | [diff] [blame] | 279 | items.sort(ItemComparator.sInstance); |
Julia Reynolds | dd2b96b | 2017-12-15 10:35:57 -0500 | [diff] [blame] | 280 | |
Julia Reynolds | 5daa472 | 2017-12-20 09:53:59 -0500 | [diff] [blame] | 281 | if (items.size() == 0 && timeout) { |
Julia Reynolds | dd2b96b | 2017-12-15 10:35:57 -0500 | [diff] [blame] | 282 | String emptyMessage = mContext.getString(R.string.output_none_found); |
| 283 | final boolean wifiOff = !mWifiManager.isWifiEnabled(); |
Julia Reynolds | 5daa472 | 2017-12-20 09:53:59 -0500 | [diff] [blame] | 284 | final boolean btOff = !mBluetoothController.isBluetoothEnabled(); |
Julia Reynolds | dd2b96b | 2017-12-15 10:35:57 -0500 | [diff] [blame] | 285 | if (wifiOff || btOff) { |
| 286 | emptyMessage = getDisabledServicesMessage(wifiOff, btOff); |
| 287 | } |
| 288 | mView.setEmptyState(emptyMessage); |
| 289 | } |
| 290 | |
| 291 | mView.setItems(items.toArray(new OutputChooserLayout.Item[items.size()])); |
| 292 | } |
| 293 | |
| 294 | private String getDisabledServicesMessage(boolean wifiOff, boolean btOff) { |
| 295 | return mContext.getString(R.string.output_none_found_service_off, |
| 296 | wifiOff && btOff ? mContext.getString(R.string.output_service_bt_wifi) |
| 297 | : wifiOff ? mContext.getString(R.string.output_service_wifi) |
| 298 | : mContext.getString(R.string.output_service_bt)); |
| 299 | } |
| 300 | |
| 301 | private void addBluetoothDevices(List<OutputChooserLayout.Item> items) { |
Julia Reynolds | 5daa472 | 2017-12-20 09:53:59 -0500 | [diff] [blame] | 302 | final Collection<CachedBluetoothDevice> devices = mBluetoothController.getDevices(); |
Julia Reynolds | fba474a8 | 2017-12-13 15:35:54 -0500 | [diff] [blame] | 303 | if (devices != null) { |
| 304 | int connectedDevices = 0; |
| 305 | int count = 0; |
| 306 | for (CachedBluetoothDevice device : devices) { |
Julia Reynolds | 5daa472 | 2017-12-20 09:53:59 -0500 | [diff] [blame] | 307 | if (mBluetoothController.getBondState(device) == BluetoothDevice.BOND_NONE) continue; |
Julia Reynolds | fba474a8 | 2017-12-13 15:35:54 -0500 | [diff] [blame] | 308 | final int majorClass = device.getBtClass().getMajorDeviceClass(); |
| 309 | if (majorClass != BluetoothClass.Device.Major.AUDIO_VIDEO |
| 310 | && majorClass != BluetoothClass.Device.Major.UNCATEGORIZED) { |
| 311 | continue; |
| 312 | } |
| 313 | final OutputChooserLayout.Item item = new OutputChooserLayout.Item(); |
| 314 | item.iconResId = R.drawable.ic_qs_bluetooth_on; |
| 315 | item.line1 = device.getName(); |
| 316 | item.tag = device; |
Julia Reynolds | dd2b96b | 2017-12-15 10:35:57 -0500 | [diff] [blame] | 317 | item.deviceType = OutputChooserLayout.Item.DEVICE_TYPE_BT; |
Julia Reynolds | fba474a8 | 2017-12-13 15:35:54 -0500 | [diff] [blame] | 318 | int state = device.getMaxConnectionState(); |
| 319 | if (state == BluetoothProfile.STATE_CONNECTED) { |
| 320 | item.iconResId = R.drawable.ic_qs_bluetooth_connected; |
| 321 | int batteryLevel = device.getBatteryLevel(); |
| 322 | if (batteryLevel != BluetoothDevice.BATTERY_LEVEL_UNKNOWN) { |
| 323 | Pair<Drawable, String> pair = |
| 324 | getBtClassDrawableWithDescription(getContext(), device); |
| 325 | item.icon = pair.first; |
| 326 | item.line2 = mContext.getString( |
| 327 | R.string.quick_settings_connected_battery_level, |
| 328 | Utils.formatPercentage(batteryLevel)); |
| 329 | } else { |
| 330 | item.line2 = mContext.getString(R.string.quick_settings_connected); |
| 331 | } |
| 332 | item.canDisconnect = true; |
| 333 | items.add(connectedDevices, item); |
| 334 | connectedDevices++; |
| 335 | } else if (state == BluetoothProfile.STATE_CONNECTING) { |
| 336 | item.iconResId = R.drawable.ic_qs_bluetooth_connecting; |
| 337 | item.line2 = mContext.getString(R.string.quick_settings_connecting); |
| 338 | items.add(connectedDevices, item); |
| 339 | } else { |
| 340 | items.add(item); |
| 341 | } |
| 342 | if (++count == MAX_DEVICES) { |
| 343 | break; |
| 344 | } |
| 345 | } |
| 346 | } |
Julia Reynolds | dd2b96b | 2017-12-15 10:35:57 -0500 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | private void addRemoteDisplayRoutes(List<OutputChooserLayout.Item> items) { |
| 350 | List<MediaRouter.RouteInfo> routes = mRouter.getRoutes(); |
| 351 | for(MediaRouter.RouteInfo route : routes) { |
| 352 | if (route.isDefaultOrBluetooth() || !route.isEnabled() |
| 353 | || !route.matchesSelector(mRouteSelector)) { |
| 354 | continue; |
| 355 | } |
| 356 | final OutputChooserLayout.Item item = new OutputChooserLayout.Item(); |
| 357 | item.icon = getIconDrawable(route); |
| 358 | item.line1 = route.getName(); |
| 359 | item.tag = route; |
| 360 | item.deviceType = OutputChooserLayout.Item.DEVICE_TYPE_MEDIA_ROUTER; |
| 361 | if (route.getConnectionState() == CONNECTION_STATE_CONNECTING) { |
| 362 | mContext.getString(R.string.quick_settings_connecting); |
| 363 | } else { |
| 364 | item.line2 = route.getDescription(); |
| 365 | } |
| 366 | |
| 367 | if (route.getConnectionState() == CONNECTION_STATE_CONNECTED) { |
| 368 | item.canDisconnect = true; |
| 369 | } |
| 370 | items.add(item); |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | private Drawable getIconDrawable(MediaRouter.RouteInfo route) { |
| 375 | Uri iconUri = route.getIconUri(); |
| 376 | if (iconUri != null) { |
| 377 | try { |
| 378 | InputStream is = getContext().getContentResolver().openInputStream(iconUri); |
| 379 | Drawable drawable = Drawable.createFromStream(is, null); |
| 380 | if (drawable != null) { |
| 381 | return drawable; |
| 382 | } |
| 383 | } catch (IOException e) { |
| 384 | Log.w(TAG, "Failed to load " + iconUri, e); |
| 385 | // Falls back. |
| 386 | } |
| 387 | } |
| 388 | return getDefaultIconDrawable(route); |
| 389 | } |
| 390 | |
| 391 | private Drawable getDefaultIconDrawable(MediaRouter.RouteInfo route) { |
| 392 | // If the type of the receiver device is specified, use it. |
| 393 | switch (route.getDeviceType()) { |
| 394 | case MediaRouter.RouteInfo.DEVICE_TYPE_TV: |
| 395 | return mTvIcon; |
| 396 | case MediaRouter.RouteInfo.DEVICE_TYPE_SPEAKER: |
| 397 | return mSpeakerIcon; |
| 398 | } |
| 399 | |
| 400 | // Otherwise, make the best guess based on other route information. |
| 401 | if (route instanceof MediaRouter.RouteGroup) { |
| 402 | // Only speakers can be grouped for now. |
| 403 | return mSpeakerGroupIcon; |
| 404 | } |
| 405 | return mDefaultIcon; |
| 406 | } |
| 407 | |
| 408 | private final class MediaRouterCallback extends MediaRouter.Callback { |
| 409 | @Override |
| 410 | public void onRouteAdded(MediaRouter router, MediaRouter.RouteInfo info) { |
Julia Reynolds | 5daa472 | 2017-12-20 09:53:59 -0500 | [diff] [blame] | 411 | updateItems(false); |
Julia Reynolds | dd2b96b | 2017-12-15 10:35:57 -0500 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | @Override |
| 415 | public void onRouteRemoved(MediaRouter router, MediaRouter.RouteInfo info) { |
Julia Reynolds | 5daa472 | 2017-12-20 09:53:59 -0500 | [diff] [blame] | 416 | updateItems(false); |
Julia Reynolds | dd2b96b | 2017-12-15 10:35:57 -0500 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | @Override |
| 420 | public void onRouteChanged(MediaRouter router, MediaRouter.RouteInfo info) { |
Julia Reynolds | 5daa472 | 2017-12-20 09:53:59 -0500 | [diff] [blame] | 421 | updateItems(false); |
Julia Reynolds | dd2b96b | 2017-12-15 10:35:57 -0500 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | @Override |
| 425 | public void onRouteSelected(MediaRouter router, MediaRouter.RouteInfo route) { |
Julia Reynolds | 5daa472 | 2017-12-20 09:53:59 -0500 | [diff] [blame] | 426 | updateItems(false); |
Julia Reynolds | dd2b96b | 2017-12-15 10:35:57 -0500 | [diff] [blame] | 427 | } |
Julia Reynolds | fba474a8 | 2017-12-13 15:35:54 -0500 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | private final BroadcastReceiver mReceiver = new BroadcastReceiver() { |
| 431 | @Override |
| 432 | public void onReceive(Context context, Intent intent) { |
| 433 | if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(intent.getAction())) { |
| 434 | if (D.BUG) Log.d(TAG, "Received ACTION_CLOSE_SYSTEM_DIALOGS"); |
| 435 | cancel(); |
| 436 | cleanUp(); |
| 437 | } |
| 438 | } |
| 439 | }; |
| 440 | |
| 441 | private final BluetoothController.Callback mCallback = new BluetoothController.Callback() { |
| 442 | @Override |
| 443 | public void onBluetoothStateChange(boolean enabled) { |
Julia Reynolds | 5daa472 | 2017-12-20 09:53:59 -0500 | [diff] [blame] | 444 | updateItems(false); |
Julia Reynolds | fba474a8 | 2017-12-13 15:35:54 -0500 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | @Override |
| 448 | public void onBluetoothDevicesChanged() { |
Julia Reynolds | 5daa472 | 2017-12-20 09:53:59 -0500 | [diff] [blame] | 449 | updateItems(false); |
Julia Reynolds | fba474a8 | 2017-12-13 15:35:54 -0500 | [diff] [blame] | 450 | } |
| 451 | }; |
Julia Reynolds | dd2b96b | 2017-12-15 10:35:57 -0500 | [diff] [blame] | 452 | |
| 453 | static final class ItemComparator implements Comparator<OutputChooserLayout.Item> { |
| 454 | public static final ItemComparator sInstance = new ItemComparator(); |
| 455 | |
| 456 | @Override |
| 457 | public int compare(OutputChooserLayout.Item lhs, OutputChooserLayout.Item rhs) { |
| 458 | // Connected item(s) first |
| 459 | if (lhs.canDisconnect != rhs.canDisconnect) { |
| 460 | return Boolean.compare(rhs.canDisconnect, lhs.canDisconnect); |
| 461 | } |
| 462 | // Bluetooth items before media routes |
| 463 | if (lhs.deviceType != rhs.deviceType) { |
| 464 | return Integer.compare(lhs.deviceType, rhs.deviceType); |
| 465 | } |
| 466 | // then by name |
| 467 | return lhs.line1.toString().compareToIgnoreCase(rhs.line1.toString()); |
| 468 | } |
| 469 | } |
| 470 | |
| 471 | private final Handler mHandler = new Handler() { |
| 472 | @Override |
| 473 | public void handleMessage(Message message) { |
| 474 | switch (message.what) { |
| 475 | case MSG_UPDATE_ITEMS: |
Julia Reynolds | 5daa472 | 2017-12-20 09:53:59 -0500 | [diff] [blame] | 476 | updateItems((Boolean) message.obj); |
Julia Reynolds | dd2b96b | 2017-12-15 10:35:57 -0500 | [diff] [blame] | 477 | break; |
| 478 | } |
| 479 | } |
| 480 | }; |
Julia Reynolds | f5e4182 | 2018-01-23 13:55:18 -0500 | [diff] [blame] | 481 | |
| 482 | private final VolumeDialogController.Callbacks mControllerCallbackH |
| 483 | = new VolumeDialogController.Callbacks() { |
| 484 | @Override |
| 485 | public void onShowRequested(int reason) { |
| 486 | dismiss(); |
| 487 | } |
| 488 | |
| 489 | @Override |
| 490 | public void onDismissRequested(int reason) {} |
| 491 | |
| 492 | @Override |
| 493 | public void onScreenOff() { |
| 494 | dismiss(); |
| 495 | } |
| 496 | |
| 497 | @Override |
| 498 | public void onStateChanged(VolumeDialogController.State state) {} |
| 499 | |
| 500 | @Override |
| 501 | public void onLayoutDirectionChanged(int layoutDirection) {} |
| 502 | |
| 503 | @Override |
| 504 | public void onConfigurationChanged() {} |
| 505 | |
| 506 | @Override |
| 507 | public void onShowVibrateHint() {} |
| 508 | |
| 509 | @Override |
| 510 | public void onShowSilentHint() {} |
| 511 | |
| 512 | @Override |
| 513 | public void onShowSafetyWarning(int flags) {} |
| 514 | |
| 515 | @Override |
| 516 | public void onAccessibilityModeChanged(Boolean showA11yStream) {} |
| 517 | }; |
Julia Reynolds | fba474a8 | 2017-12-13 15:35:54 -0500 | [diff] [blame] | 518 | } |