Jeff Brown | cbad976 | 2012-09-04 21:57:59 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 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.server.display; |
| 18 | |
Jeff Brown | a7f9c96 | 2012-10-17 15:15:12 -0700 | [diff] [blame] | 19 | import com.android.internal.R; |
Jeff Brown | cbad976 | 2012-09-04 21:57:59 -0700 | [diff] [blame] | 20 | import com.android.internal.util.DumpUtils; |
| 21 | import com.android.internal.util.IndentingPrintWriter; |
| 22 | |
Jeff Brown | a7f9c96 | 2012-10-17 15:15:12 -0700 | [diff] [blame] | 23 | import android.app.Notification; |
| 24 | import android.app.NotificationManager; |
| 25 | import android.app.PendingIntent; |
| 26 | import android.content.BroadcastReceiver; |
Jeff Brown | cbad976 | 2012-09-04 21:57:59 -0700 | [diff] [blame] | 27 | import android.content.Context; |
Jeff Brown | e08ae38 | 2012-09-07 20:36:36 -0700 | [diff] [blame] | 28 | import android.content.Intent; |
Jeff Brown | a7f9c96 | 2012-10-17 15:15:12 -0700 | [diff] [blame] | 29 | import android.content.IntentFilter; |
| 30 | import android.content.res.Resources; |
Jeff Brown | e08ae38 | 2012-09-07 20:36:36 -0700 | [diff] [blame] | 31 | import android.hardware.display.DisplayManager; |
| 32 | import android.hardware.display.WifiDisplay; |
Chong Zhang | 1f3ecaa | 2013-05-03 15:55:36 -0700 | [diff] [blame] | 33 | import android.hardware.display.WifiDisplaySessionInfo; |
Jeff Brown | e08ae38 | 2012-09-07 20:36:36 -0700 | [diff] [blame] | 34 | import android.hardware.display.WifiDisplayStatus; |
Jeff Brown | cbad976 | 2012-09-04 21:57:59 -0700 | [diff] [blame] | 35 | import android.media.RemoteDisplay; |
| 36 | import android.os.Handler; |
| 37 | import android.os.IBinder; |
Jeff Brown | a7f9c96 | 2012-10-17 15:15:12 -0700 | [diff] [blame] | 38 | import android.os.Looper; |
| 39 | import android.os.Message; |
| 40 | import android.os.UserHandle; |
| 41 | import android.provider.Settings; |
Jeff Brown | bc33545 | 2012-09-26 18:34:47 -0700 | [diff] [blame] | 42 | import android.util.Slog; |
Jeff Brown | 92130f6 | 2012-10-24 21:28:33 -0700 | [diff] [blame] | 43 | import android.view.Display; |
Jeff Brown | cbad976 | 2012-09-04 21:57:59 -0700 | [diff] [blame] | 44 | import android.view.Surface; |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 45 | import android.view.SurfaceControl; |
Jeff Brown | cbad976 | 2012-09-04 21:57:59 -0700 | [diff] [blame] | 46 | |
| 47 | import java.io.PrintWriter; |
Jeff Brown | e08ae38 | 2012-09-07 20:36:36 -0700 | [diff] [blame] | 48 | import java.util.Arrays; |
Chong Zhang | ab87a63 | 2013-06-11 10:25:49 -0700 | [diff] [blame] | 49 | import java.util.List; |
| 50 | import java.util.ArrayList; |
Jeff Brown | cbad976 | 2012-09-04 21:57:59 -0700 | [diff] [blame] | 51 | |
Jeff Brown | 74da109 | 2012-11-07 16:02:13 -0800 | [diff] [blame] | 52 | import libcore.util.Objects; |
| 53 | |
Jeff Brown | cbad976 | 2012-09-04 21:57:59 -0700 | [diff] [blame] | 54 | /** |
| 55 | * Connects to Wifi displays that implement the Miracast protocol. |
| 56 | * <p> |
| 57 | * The Wifi display protocol relies on Wifi direct for discovering and pairing |
| 58 | * with the display. Once connected, the Media Server opens an RTSP socket and accepts |
| 59 | * a connection from the display. After session negotiation, the Media Server |
| 60 | * streams encoded buffers to the display. |
| 61 | * </p><p> |
| 62 | * This class is responsible for connecting to Wifi displays and mediating |
| 63 | * the interactions between Media Server, Surface Flinger and the Display Manager Service. |
| 64 | * </p><p> |
| 65 | * Display adapters are guarded by the {@link DisplayManagerService.SyncRoot} lock. |
| 66 | * </p> |
| 67 | */ |
| 68 | final class WifiDisplayAdapter extends DisplayAdapter { |
| 69 | private static final String TAG = "WifiDisplayAdapter"; |
| 70 | |
Jeff Brown | 2444ae7 | 2012-10-11 14:30:21 -0700 | [diff] [blame] | 71 | private static final boolean DEBUG = false; |
| 72 | |
Jeff Brown | a7f9c96 | 2012-10-17 15:15:12 -0700 | [diff] [blame] | 73 | private static final int MSG_SEND_STATUS_CHANGE_BROADCAST = 1; |
| 74 | private static final int MSG_UPDATE_NOTIFICATION = 2; |
| 75 | |
| 76 | private static final String ACTION_DISCONNECT = "android.server.display.wfd.DISCONNECT"; |
| 77 | |
| 78 | private final WifiDisplayHandler mHandler; |
Jeff Brown | 77aebfd | 2012-10-01 21:07:03 -0700 | [diff] [blame] | 79 | private final PersistentDataStore mPersistentDataStore; |
| 80 | private final boolean mSupportsProtectedBuffers; |
Jeff Brown | a7f9c96 | 2012-10-17 15:15:12 -0700 | [diff] [blame] | 81 | private final NotificationManager mNotificationManager; |
| 82 | |
Jeff Brown | 6669250 | 2012-10-18 16:13:44 -0700 | [diff] [blame] | 83 | private PendingIntent mSettingsPendingIntent; |
| 84 | private PendingIntent mDisconnectPendingIntent; |
Jeff Brown | 89d5546 | 2012-09-19 11:33:42 -0700 | [diff] [blame] | 85 | |
Jeff Brown | cbad976 | 2012-09-04 21:57:59 -0700 | [diff] [blame] | 86 | private WifiDisplayController mDisplayController; |
Jeff Brown | f8f0edd | 2012-09-11 17:05:11 -0700 | [diff] [blame] | 87 | private WifiDisplayDevice mDisplayDevice; |
Jeff Brown | cbad976 | 2012-09-04 21:57:59 -0700 | [diff] [blame] | 88 | |
Jeff Brown | e08ae38 | 2012-09-07 20:36:36 -0700 | [diff] [blame] | 89 | private WifiDisplayStatus mCurrentStatus; |
Jeff Brown | 89d5546 | 2012-09-19 11:33:42 -0700 | [diff] [blame] | 90 | private int mFeatureState; |
Jeff Brown | 180bbc7 | 2012-09-08 23:15:00 -0700 | [diff] [blame] | 91 | private int mScanState; |
| 92 | private int mActiveDisplayState; |
| 93 | private WifiDisplay mActiveDisplay; |
Chong Zhang | ab87a63 | 2013-06-11 10:25:49 -0700 | [diff] [blame] | 94 | private WifiDisplay[] mDisplays = WifiDisplay.EMPTY_ARRAY; |
Jeff Brown | 89d5546 | 2012-09-19 11:33:42 -0700 | [diff] [blame] | 95 | private WifiDisplay[] mAvailableDisplays = WifiDisplay.EMPTY_ARRAY; |
| 96 | private WifiDisplay[] mRememberedDisplays = WifiDisplay.EMPTY_ARRAY; |
Chong Zhang | 1f3ecaa | 2013-05-03 15:55:36 -0700 | [diff] [blame] | 97 | private WifiDisplaySessionInfo mSessionInfo; |
Jeff Brown | e08ae38 | 2012-09-07 20:36:36 -0700 | [diff] [blame] | 98 | |
| 99 | private boolean mPendingStatusChangeBroadcast; |
Jeff Brown | a7f9c96 | 2012-10-17 15:15:12 -0700 | [diff] [blame] | 100 | private boolean mPendingNotificationUpdate; |
Jeff Brown | e08ae38 | 2012-09-07 20:36:36 -0700 | [diff] [blame] | 101 | |
Jeff Brown | 6669250 | 2012-10-18 16:13:44 -0700 | [diff] [blame] | 102 | // Called with SyncRoot lock held. |
Jeff Brown | cbad976 | 2012-09-04 21:57:59 -0700 | [diff] [blame] | 103 | public WifiDisplayAdapter(DisplayManagerService.SyncRoot syncRoot, |
Jeff Brown | 89d5546 | 2012-09-19 11:33:42 -0700 | [diff] [blame] | 104 | Context context, Handler handler, Listener listener, |
| 105 | PersistentDataStore persistentDataStore) { |
Jeff Brown | cbad976 | 2012-09-04 21:57:59 -0700 | [diff] [blame] | 106 | super(syncRoot, context, handler, listener, TAG); |
Jeff Brown | a7f9c96 | 2012-10-17 15:15:12 -0700 | [diff] [blame] | 107 | mHandler = new WifiDisplayHandler(handler.getLooper()); |
Jeff Brown | 89d5546 | 2012-09-19 11:33:42 -0700 | [diff] [blame] | 108 | mPersistentDataStore = persistentDataStore; |
Jeff Brown | 77aebfd | 2012-10-01 21:07:03 -0700 | [diff] [blame] | 109 | mSupportsProtectedBuffers = context.getResources().getBoolean( |
| 110 | com.android.internal.R.bool.config_wifiDisplaySupportsProtectedBuffers); |
Jeff Brown | a7f9c96 | 2012-10-17 15:15:12 -0700 | [diff] [blame] | 111 | mNotificationManager = (NotificationManager)context.getSystemService( |
| 112 | Context.NOTIFICATION_SERVICE); |
Jeff Brown | cbad976 | 2012-09-04 21:57:59 -0700 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | @Override |
| 116 | public void dumpLocked(PrintWriter pw) { |
| 117 | super.dumpLocked(pw); |
| 118 | |
Jeff Brown | e08ae38 | 2012-09-07 20:36:36 -0700 | [diff] [blame] | 119 | pw.println("mCurrentStatus=" + getWifiDisplayStatusLocked()); |
Jeff Brown | 89d5546 | 2012-09-19 11:33:42 -0700 | [diff] [blame] | 120 | pw.println("mFeatureState=" + mFeatureState); |
Jeff Brown | 180bbc7 | 2012-09-08 23:15:00 -0700 | [diff] [blame] | 121 | pw.println("mScanState=" + mScanState); |
| 122 | pw.println("mActiveDisplayState=" + mActiveDisplayState); |
| 123 | pw.println("mActiveDisplay=" + mActiveDisplay); |
Chong Zhang | ab87a63 | 2013-06-11 10:25:49 -0700 | [diff] [blame] | 124 | pw.println("mDisplays=" + Arrays.toString(mDisplays)); |
Jeff Brown | 89d5546 | 2012-09-19 11:33:42 -0700 | [diff] [blame] | 125 | pw.println("mAvailableDisplays=" + Arrays.toString(mAvailableDisplays)); |
| 126 | pw.println("mRememberedDisplays=" + Arrays.toString(mRememberedDisplays)); |
Jeff Brown | e08ae38 | 2012-09-07 20:36:36 -0700 | [diff] [blame] | 127 | pw.println("mPendingStatusChangeBroadcast=" + mPendingStatusChangeBroadcast); |
Jeff Brown | a7f9c96 | 2012-10-17 15:15:12 -0700 | [diff] [blame] | 128 | pw.println("mPendingNotificationUpdate=" + mPendingNotificationUpdate); |
Jeff Brown | 77aebfd | 2012-10-01 21:07:03 -0700 | [diff] [blame] | 129 | pw.println("mSupportsProtectedBuffers=" + mSupportsProtectedBuffers); |
Andy McFadden | e8b1aeb | 2014-06-13 14:05:40 -0700 | [diff] [blame] | 130 | |
Jeff Brown | cbad976 | 2012-09-04 21:57:59 -0700 | [diff] [blame] | 131 | // Try to dump the controller state. |
| 132 | if (mDisplayController == null) { |
| 133 | pw.println("mDisplayController=null"); |
| 134 | } else { |
| 135 | pw.println("mDisplayController:"); |
| 136 | final IndentingPrintWriter ipw = new IndentingPrintWriter(pw, " "); |
| 137 | ipw.increaseIndent(); |
| 138 | DumpUtils.dumpAsync(getHandler(), mDisplayController, ipw, 200); |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | @Override |
| 143 | public void registerLocked() { |
| 144 | super.registerLocked(); |
| 145 | |
Jeff Brown | 89d5546 | 2012-09-19 11:33:42 -0700 | [diff] [blame] | 146 | updateRememberedDisplaysLocked(); |
| 147 | |
Jeff Brown | cbad976 | 2012-09-04 21:57:59 -0700 | [diff] [blame] | 148 | getHandler().post(new Runnable() { |
| 149 | @Override |
| 150 | public void run() { |
| 151 | mDisplayController = new WifiDisplayController( |
| 152 | getContext(), getHandler(), mWifiDisplayListener); |
Jeff Brown | 6669250 | 2012-10-18 16:13:44 -0700 | [diff] [blame] | 153 | |
| 154 | getContext().registerReceiverAsUser(mBroadcastReceiver, UserHandle.ALL, |
| 155 | new IntentFilter(ACTION_DISCONNECT), null, mHandler); |
Jeff Brown | cbad976 | 2012-09-04 21:57:59 -0700 | [diff] [blame] | 156 | } |
| 157 | }); |
| 158 | } |
| 159 | |
Jeff Brown | ce468a3 | 2013-11-21 16:42:03 -0800 | [diff] [blame] | 160 | public void requestStartScanLocked() { |
Jeff Brown | 2444ae7 | 2012-10-11 14:30:21 -0700 | [diff] [blame] | 161 | if (DEBUG) { |
Jeff Brown | ce468a3 | 2013-11-21 16:42:03 -0800 | [diff] [blame] | 162 | Slog.d(TAG, "requestStartScanLocked"); |
Jeff Brown | 2444ae7 | 2012-10-11 14:30:21 -0700 | [diff] [blame] | 163 | } |
| 164 | |
Jeff Brown | e08ae38 | 2012-09-07 20:36:36 -0700 | [diff] [blame] | 165 | getHandler().post(new Runnable() { |
| 166 | @Override |
| 167 | public void run() { |
| 168 | if (mDisplayController != null) { |
Jeff Brown | ce468a3 | 2013-11-21 16:42:03 -0800 | [diff] [blame] | 169 | mDisplayController.requestStartScan(); |
| 170 | } |
| 171 | } |
| 172 | }); |
| 173 | } |
| 174 | |
| 175 | public void requestStopScanLocked() { |
| 176 | if (DEBUG) { |
| 177 | Slog.d(TAG, "requestStopScanLocked"); |
| 178 | } |
| 179 | |
| 180 | getHandler().post(new Runnable() { |
| 181 | @Override |
| 182 | public void run() { |
| 183 | if (mDisplayController != null) { |
| 184 | mDisplayController.requestStopScan(); |
Jeff Brown | e08ae38 | 2012-09-07 20:36:36 -0700 | [diff] [blame] | 185 | } |
| 186 | } |
| 187 | }); |
Jeff Brown | cbad976 | 2012-09-04 21:57:59 -0700 | [diff] [blame] | 188 | } |
| 189 | |
Jeff Brown | af57418 | 2013-11-14 18:16:08 -0800 | [diff] [blame] | 190 | public void requestConnectLocked(final String address) { |
Jeff Brown | 2444ae7 | 2012-10-11 14:30:21 -0700 | [diff] [blame] | 191 | if (DEBUG) { |
Jeff Brown | af57418 | 2013-11-14 18:16:08 -0800 | [diff] [blame] | 192 | Slog.d(TAG, "requestConnectLocked: address=" + address); |
Jeff Brown | bc33545 | 2012-09-26 18:34:47 -0700 | [diff] [blame] | 193 | } |
| 194 | |
Jeff Brown | e08ae38 | 2012-09-07 20:36:36 -0700 | [diff] [blame] | 195 | getHandler().post(new Runnable() { |
| 196 | @Override |
| 197 | public void run() { |
| 198 | if (mDisplayController != null) { |
| 199 | mDisplayController.requestConnect(address); |
| 200 | } |
| 201 | } |
| 202 | }); |
| 203 | } |
| 204 | |
Chong Zhang | 1f3ecaa | 2013-05-03 15:55:36 -0700 | [diff] [blame] | 205 | public void requestPauseLocked() { |
| 206 | if (DEBUG) { |
| 207 | Slog.d(TAG, "requestPauseLocked"); |
| 208 | } |
| 209 | |
| 210 | getHandler().post(new Runnable() { |
| 211 | @Override |
| 212 | public void run() { |
| 213 | if (mDisplayController != null) { |
| 214 | mDisplayController.requestPause(); |
| 215 | } |
| 216 | } |
| 217 | }); |
| 218 | } |
| 219 | |
| 220 | public void requestResumeLocked() { |
| 221 | if (DEBUG) { |
| 222 | Slog.d(TAG, "requestResumeLocked"); |
| 223 | } |
| 224 | |
| 225 | getHandler().post(new Runnable() { |
| 226 | @Override |
| 227 | public void run() { |
| 228 | if (mDisplayController != null) { |
| 229 | mDisplayController.requestResume(); |
| 230 | } |
| 231 | } |
| 232 | }); |
| 233 | } |
| 234 | |
Jeff Brown | e08ae38 | 2012-09-07 20:36:36 -0700 | [diff] [blame] | 235 | public void requestDisconnectLocked() { |
Jeff Brown | 2444ae7 | 2012-10-11 14:30:21 -0700 | [diff] [blame] | 236 | if (DEBUG) { |
| 237 | Slog.d(TAG, "requestDisconnectedLocked"); |
| 238 | } |
| 239 | |
Jeff Brown | e08ae38 | 2012-09-07 20:36:36 -0700 | [diff] [blame] | 240 | getHandler().post(new Runnable() { |
| 241 | @Override |
| 242 | public void run() { |
| 243 | if (mDisplayController != null) { |
| 244 | mDisplayController.requestDisconnect(); |
| 245 | } |
| 246 | } |
| 247 | }); |
| 248 | } |
| 249 | |
Jeff Brown | 89d5546 | 2012-09-19 11:33:42 -0700 | [diff] [blame] | 250 | public void requestRenameLocked(String address, String alias) { |
Jeff Brown | 2444ae7 | 2012-10-11 14:30:21 -0700 | [diff] [blame] | 251 | if (DEBUG) { |
| 252 | Slog.d(TAG, "requestRenameLocked: address=" + address + ", alias=" + alias); |
| 253 | } |
| 254 | |
Jeff Brown | 89d5546 | 2012-09-19 11:33:42 -0700 | [diff] [blame] | 255 | if (alias != null) { |
| 256 | alias = alias.trim(); |
Jeff Brown | 2444ae7 | 2012-10-11 14:30:21 -0700 | [diff] [blame] | 257 | if (alias.isEmpty() || alias.equals(address)) { |
Jeff Brown | 89d5546 | 2012-09-19 11:33:42 -0700 | [diff] [blame] | 258 | alias = null; |
| 259 | } |
| 260 | } |
| 261 | |
Jeff Brown | 74da109 | 2012-11-07 16:02:13 -0800 | [diff] [blame] | 262 | WifiDisplay display = mPersistentDataStore.getRememberedWifiDisplay(address); |
| 263 | if (display != null && !Objects.equal(display.getDeviceAlias(), alias)) { |
Chong Zhang | ab87a63 | 2013-06-11 10:25:49 -0700 | [diff] [blame] | 264 | display = new WifiDisplay(address, display.getDeviceName(), alias, |
| 265 | false, false, false); |
Jeff Brown | 74da109 | 2012-11-07 16:02:13 -0800 | [diff] [blame] | 266 | if (mPersistentDataStore.rememberWifiDisplay(display)) { |
| 267 | mPersistentDataStore.saveIfNeeded(); |
| 268 | updateRememberedDisplaysLocked(); |
| 269 | scheduleStatusChangedBroadcastLocked(); |
| 270 | } |
Jeff Brown | 89d5546 | 2012-09-19 11:33:42 -0700 | [diff] [blame] | 271 | } |
Jeff Brown | ee4f029 | 2012-10-15 15:31:59 -0700 | [diff] [blame] | 272 | |
Jeff Brown | 74da109 | 2012-11-07 16:02:13 -0800 | [diff] [blame] | 273 | if (mActiveDisplay != null && mActiveDisplay.getDeviceAddress().equals(address)) { |
| 274 | renameDisplayDeviceLocked(mActiveDisplay.getFriendlyDisplayName()); |
Jeff Brown | ee4f029 | 2012-10-15 15:31:59 -0700 | [diff] [blame] | 275 | } |
Jeff Brown | 89d5546 | 2012-09-19 11:33:42 -0700 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | public void requestForgetLocked(String address) { |
Jeff Brown | 2444ae7 | 2012-10-11 14:30:21 -0700 | [diff] [blame] | 279 | if (DEBUG) { |
| 280 | Slog.d(TAG, "requestForgetLocked: address=" + address); |
| 281 | } |
| 282 | |
Jeff Brown | 89d5546 | 2012-09-19 11:33:42 -0700 | [diff] [blame] | 283 | if (mPersistentDataStore.forgetWifiDisplay(address)) { |
| 284 | mPersistentDataStore.saveIfNeeded(); |
| 285 | updateRememberedDisplaysLocked(); |
| 286 | scheduleStatusChangedBroadcastLocked(); |
| 287 | } |
| 288 | |
| 289 | if (mActiveDisplay != null && mActiveDisplay.getDeviceAddress().equals(address)) { |
| 290 | requestDisconnectLocked(); |
| 291 | } |
| 292 | } |
| 293 | |
Jeff Brown | e08ae38 | 2012-09-07 20:36:36 -0700 | [diff] [blame] | 294 | public WifiDisplayStatus getWifiDisplayStatusLocked() { |
| 295 | if (mCurrentStatus == null) { |
Jeff Brown | 89d5546 | 2012-09-19 11:33:42 -0700 | [diff] [blame] | 296 | mCurrentStatus = new WifiDisplayStatus( |
| 297 | mFeatureState, mScanState, mActiveDisplayState, |
Chong Zhang | 1f3ecaa | 2013-05-03 15:55:36 -0700 | [diff] [blame] | 298 | mActiveDisplay, mDisplays, mSessionInfo); |
Jeff Brown | e08ae38 | 2012-09-07 20:36:36 -0700 | [diff] [blame] | 299 | } |
Jeff Brown | 2444ae7 | 2012-10-11 14:30:21 -0700 | [diff] [blame] | 300 | |
| 301 | if (DEBUG) { |
| 302 | Slog.d(TAG, "getWifiDisplayStatusLocked: result=" + mCurrentStatus); |
| 303 | } |
Jeff Brown | e08ae38 | 2012-09-07 20:36:36 -0700 | [diff] [blame] | 304 | return mCurrentStatus; |
| 305 | } |
| 306 | |
Chong Zhang | ab87a63 | 2013-06-11 10:25:49 -0700 | [diff] [blame] | 307 | private void updateDisplaysLocked() { |
| 308 | List<WifiDisplay> displays = new ArrayList<WifiDisplay>( |
| 309 | mAvailableDisplays.length + mRememberedDisplays.length); |
| 310 | boolean[] remembered = new boolean[mAvailableDisplays.length]; |
| 311 | for (WifiDisplay d : mRememberedDisplays) { |
| 312 | boolean available = false; |
| 313 | for (int i = 0; i < mAvailableDisplays.length; i++) { |
| 314 | if (d.equals(mAvailableDisplays[i])) { |
| 315 | remembered[i] = available = true; |
| 316 | break; |
| 317 | } |
| 318 | } |
| 319 | if (!available) { |
| 320 | displays.add(new WifiDisplay(d.getDeviceAddress(), d.getDeviceName(), |
| 321 | d.getDeviceAlias(), false, false, true)); |
| 322 | } |
| 323 | } |
| 324 | for (int i = 0; i < mAvailableDisplays.length; i++) { |
| 325 | WifiDisplay d = mAvailableDisplays[i]; |
| 326 | displays.add(new WifiDisplay(d.getDeviceAddress(), d.getDeviceName(), |
| 327 | d.getDeviceAlias(), true, d.canConnect(), remembered[i])); |
| 328 | } |
| 329 | mDisplays = displays.toArray(WifiDisplay.EMPTY_ARRAY); |
| 330 | } |
| 331 | |
Jeff Brown | 89d5546 | 2012-09-19 11:33:42 -0700 | [diff] [blame] | 332 | private void updateRememberedDisplaysLocked() { |
| 333 | mRememberedDisplays = mPersistentDataStore.getRememberedWifiDisplays(); |
| 334 | mActiveDisplay = mPersistentDataStore.applyWifiDisplayAlias(mActiveDisplay); |
| 335 | mAvailableDisplays = mPersistentDataStore.applyWifiDisplayAliases(mAvailableDisplays); |
Chong Zhang | ab87a63 | 2013-06-11 10:25:49 -0700 | [diff] [blame] | 336 | updateDisplaysLocked(); |
Jeff Brown | 89d5546 | 2012-09-19 11:33:42 -0700 | [diff] [blame] | 337 | } |
| 338 | |
Jeff Brown | 74da109 | 2012-11-07 16:02:13 -0800 | [diff] [blame] | 339 | private void fixRememberedDisplayNamesFromAvailableDisplaysLocked() { |
| 340 | // It may happen that a display name has changed since it was remembered. |
| 341 | // Consult the list of available displays and update the name if needed. |
| 342 | // We don't do anything special for the active display here. The display |
| 343 | // controller will send a separate event when it needs to be updates. |
| 344 | boolean changed = false; |
| 345 | for (int i = 0; i < mRememberedDisplays.length; i++) { |
| 346 | WifiDisplay rememberedDisplay = mRememberedDisplays[i]; |
| 347 | WifiDisplay availableDisplay = findAvailableDisplayLocked( |
| 348 | rememberedDisplay.getDeviceAddress()); |
| 349 | if (availableDisplay != null && !rememberedDisplay.equals(availableDisplay)) { |
| 350 | if (DEBUG) { |
| 351 | Slog.d(TAG, "fixRememberedDisplayNamesFromAvailableDisplaysLocked: " |
| 352 | + "updating remembered display to " + availableDisplay); |
| 353 | } |
| 354 | mRememberedDisplays[i] = availableDisplay; |
| 355 | changed |= mPersistentDataStore.rememberWifiDisplay(availableDisplay); |
| 356 | } |
| 357 | } |
| 358 | if (changed) { |
| 359 | mPersistentDataStore.saveIfNeeded(); |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | private WifiDisplay findAvailableDisplayLocked(String address) { |
| 364 | for (WifiDisplay display : mAvailableDisplays) { |
| 365 | if (display.getDeviceAddress().equals(address)) { |
| 366 | return display; |
| 367 | } |
| 368 | } |
| 369 | return null; |
| 370 | } |
| 371 | |
| 372 | private void addDisplayDeviceLocked(WifiDisplay display, |
Jeff Brown | f8f0edd | 2012-09-11 17:05:11 -0700 | [diff] [blame] | 373 | Surface surface, int width, int height, int flags) { |
Jeff Brown | 74da109 | 2012-11-07 16:02:13 -0800 | [diff] [blame] | 374 | removeDisplayDeviceLocked(); |
Jeff Brown | e08ae38 | 2012-09-07 20:36:36 -0700 | [diff] [blame] | 375 | |
Jeff Brown | 89d5546 | 2012-09-19 11:33:42 -0700 | [diff] [blame] | 376 | if (mPersistentDataStore.rememberWifiDisplay(display)) { |
| 377 | mPersistentDataStore.saveIfNeeded(); |
| 378 | updateRememberedDisplaysLocked(); |
| 379 | scheduleStatusChangedBroadcastLocked(); |
| 380 | } |
| 381 | |
Jeff Brown | f0681b3 | 2012-10-23 17:35:57 -0700 | [diff] [blame] | 382 | boolean secure = (flags & RemoteDisplay.DISPLAY_FLAG_SECURE) != 0; |
Jeff Brown | 7d00aff | 2013-08-02 19:03:49 -0700 | [diff] [blame] | 383 | int deviceFlags = DisplayDeviceInfo.FLAG_PRESENTATION; |
Jeff Brown | f0681b3 | 2012-10-23 17:35:57 -0700 | [diff] [blame] | 384 | if (secure) { |
Jeff Brown | 77aebfd | 2012-10-01 21:07:03 -0700 | [diff] [blame] | 385 | deviceFlags |= DisplayDeviceInfo.FLAG_SECURE; |
Jeff Brown | f0681b3 | 2012-10-23 17:35:57 -0700 | [diff] [blame] | 386 | if (mSupportsProtectedBuffers) { |
| 387 | deviceFlags |= DisplayDeviceInfo.FLAG_SUPPORTS_PROTECTED_BUFFERS; |
| 388 | } |
Jeff Brown | f8f0edd | 2012-09-11 17:05:11 -0700 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | float refreshRate = 60.0f; // TODO: get this for real |
| 392 | |
Jeff Brown | 89d5546 | 2012-09-19 11:33:42 -0700 | [diff] [blame] | 393 | String name = display.getFriendlyDisplayName(); |
Jeff Brown | 92130f6 | 2012-10-24 21:28:33 -0700 | [diff] [blame] | 394 | String address = display.getDeviceAddress(); |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 395 | IBinder displayToken = SurfaceControl.createDisplay(name, secure); |
Jeff Brown | f8f0edd | 2012-09-11 17:05:11 -0700 | [diff] [blame] | 396 | mDisplayDevice = new WifiDisplayDevice(displayToken, name, width, height, |
Jeff Brown | 92130f6 | 2012-10-24 21:28:33 -0700 | [diff] [blame] | 397 | refreshRate, deviceFlags, address, surface); |
Jeff Brown | f8f0edd | 2012-09-11 17:05:11 -0700 | [diff] [blame] | 398 | sendDisplayDeviceEventLocked(mDisplayDevice, DISPLAY_DEVICE_EVENT_ADDED); |
Jeff Brown | e08ae38 | 2012-09-07 20:36:36 -0700 | [diff] [blame] | 399 | } |
| 400 | |
Jeff Brown | 74da109 | 2012-11-07 16:02:13 -0800 | [diff] [blame] | 401 | private void removeDisplayDeviceLocked() { |
Jeff Brown | f8f0edd | 2012-09-11 17:05:11 -0700 | [diff] [blame] | 402 | if (mDisplayDevice != null) { |
Jesse Hall | 6a6bc21 | 2013-08-08 12:15:03 -0700 | [diff] [blame] | 403 | mDisplayDevice.destroyLocked(); |
Jeff Brown | f8f0edd | 2012-09-11 17:05:11 -0700 | [diff] [blame] | 404 | sendDisplayDeviceEventLocked(mDisplayDevice, DISPLAY_DEVICE_EVENT_REMOVED); |
| 405 | mDisplayDevice = null; |
Jeff Brown | cbad976 | 2012-09-04 21:57:59 -0700 | [diff] [blame] | 406 | } |
| 407 | } |
| 408 | |
Jeff Brown | 74da109 | 2012-11-07 16:02:13 -0800 | [diff] [blame] | 409 | private void renameDisplayDeviceLocked(String name) { |
| 410 | if (mDisplayDevice != null && !mDisplayDevice.getNameLocked().equals(name)) { |
| 411 | mDisplayDevice.setNameLocked(name); |
| 412 | sendDisplayDeviceEventLocked(mDisplayDevice, DISPLAY_DEVICE_EVENT_CHANGED); |
| 413 | } |
| 414 | } |
| 415 | |
Jeff Brown | e08ae38 | 2012-09-07 20:36:36 -0700 | [diff] [blame] | 416 | private void scheduleStatusChangedBroadcastLocked() { |
Jeff Brown | 89d5546 | 2012-09-19 11:33:42 -0700 | [diff] [blame] | 417 | mCurrentStatus = null; |
Jeff Brown | e08ae38 | 2012-09-07 20:36:36 -0700 | [diff] [blame] | 418 | if (!mPendingStatusChangeBroadcast) { |
| 419 | mPendingStatusChangeBroadcast = true; |
Jeff Brown | a7f9c96 | 2012-10-17 15:15:12 -0700 | [diff] [blame] | 420 | mHandler.sendEmptyMessage(MSG_SEND_STATUS_CHANGE_BROADCAST); |
Jeff Brown | e08ae38 | 2012-09-07 20:36:36 -0700 | [diff] [blame] | 421 | } |
| 422 | } |
| 423 | |
Jeff Brown | a7f9c96 | 2012-10-17 15:15:12 -0700 | [diff] [blame] | 424 | private void scheduleUpdateNotificationLocked() { |
| 425 | if (!mPendingNotificationUpdate) { |
| 426 | mPendingNotificationUpdate = true; |
| 427 | mHandler.sendEmptyMessage(MSG_UPDATE_NOTIFICATION); |
| 428 | } |
| 429 | } |
Jeff Brown | e08ae38 | 2012-09-07 20:36:36 -0700 | [diff] [blame] | 430 | |
Jeff Brown | a7f9c96 | 2012-10-17 15:15:12 -0700 | [diff] [blame] | 431 | // Runs on the handler. |
| 432 | private void handleSendStatusChangeBroadcast() { |
| 433 | final Intent intent; |
| 434 | synchronized (getSyncRoot()) { |
| 435 | if (!mPendingStatusChangeBroadcast) { |
| 436 | return; |
Jeff Brown | e08ae38 | 2012-09-07 20:36:36 -0700 | [diff] [blame] | 437 | } |
| 438 | |
Jeff Brown | a7f9c96 | 2012-10-17 15:15:12 -0700 | [diff] [blame] | 439 | mPendingStatusChangeBroadcast = false; |
| 440 | intent = new Intent(DisplayManager.ACTION_WIFI_DISPLAY_STATUS_CHANGED); |
| 441 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
| 442 | intent.putExtra(DisplayManager.EXTRA_WIFI_DISPLAY_STATUS, |
| 443 | getWifiDisplayStatusLocked()); |
| 444 | } |
| 445 | |
| 446 | // Send protected broadcast about wifi display status to registered receivers. |
| 447 | getContext().sendBroadcastAsUser(intent, UserHandle.ALL); |
| 448 | } |
| 449 | |
| 450 | // Runs on the handler. |
| 451 | private void handleUpdateNotification() { |
Jeff Brown | e7ae644 | 2013-11-13 00:45:18 -0800 | [diff] [blame] | 452 | final int state; |
| 453 | final WifiDisplay display; |
Jeff Brown | a7f9c96 | 2012-10-17 15:15:12 -0700 | [diff] [blame] | 454 | synchronized (getSyncRoot()) { |
| 455 | if (!mPendingNotificationUpdate) { |
| 456 | return; |
| 457 | } |
| 458 | |
| 459 | mPendingNotificationUpdate = false; |
Jeff Brown | e7ae644 | 2013-11-13 00:45:18 -0800 | [diff] [blame] | 460 | state = mActiveDisplayState; |
| 461 | display = mActiveDisplay; |
Jeff Brown | a7f9c96 | 2012-10-17 15:15:12 -0700 | [diff] [blame] | 462 | } |
| 463 | |
Jeff Brown | 6669250 | 2012-10-18 16:13:44 -0700 | [diff] [blame] | 464 | // Cancel the old notification if there is one. |
Jeff Brown | a7f9c96 | 2012-10-17 15:15:12 -0700 | [diff] [blame] | 465 | mNotificationManager.cancelAsUser(null, |
Jeff Brown | e7ae644 | 2013-11-13 00:45:18 -0800 | [diff] [blame] | 466 | R.string.wifi_display_notification_disconnect, UserHandle.ALL); |
Jeff Brown | a7f9c96 | 2012-10-17 15:15:12 -0700 | [diff] [blame] | 467 | |
Jeff Brown | e7ae644 | 2013-11-13 00:45:18 -0800 | [diff] [blame] | 468 | if (state == WifiDisplayStatus.DISPLAY_STATE_CONNECTING |
| 469 | || state == WifiDisplayStatus.DISPLAY_STATE_CONNECTED) { |
Jeff Brown | a7f9c96 | 2012-10-17 15:15:12 -0700 | [diff] [blame] | 470 | Context context = getContext(); |
| 471 | |
Jeff Brown | 6669250 | 2012-10-18 16:13:44 -0700 | [diff] [blame] | 472 | // Initialize pending intents for the notification outside of the lock because |
| 473 | // creating a pending intent requires a call into the activity manager. |
| 474 | if (mSettingsPendingIntent == null) { |
| 475 | Intent settingsIntent = new Intent(Settings.ACTION_WIFI_DISPLAY_SETTINGS); |
| 476 | settingsIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
| 477 | | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED |
| 478 | | Intent.FLAG_ACTIVITY_CLEAR_TOP); |
| 479 | mSettingsPendingIntent = PendingIntent.getActivityAsUser( |
| 480 | context, 0, settingsIntent, 0, null, UserHandle.CURRENT); |
| 481 | } |
| 482 | |
| 483 | if (mDisconnectPendingIntent == null) { |
| 484 | Intent disconnectIntent = new Intent(ACTION_DISCONNECT); |
| 485 | mDisconnectPendingIntent = PendingIntent.getBroadcastAsUser( |
| 486 | context, 0, disconnectIntent, 0, UserHandle.CURRENT); |
| 487 | } |
| 488 | |
| 489 | // Post the notification. |
Jeff Brown | a7f9c96 | 2012-10-17 15:15:12 -0700 | [diff] [blame] | 490 | Resources r = context.getResources(); |
Jeff Brown | e7ae644 | 2013-11-13 00:45:18 -0800 | [diff] [blame] | 491 | Notification notification; |
| 492 | if (state == WifiDisplayStatus.DISPLAY_STATE_CONNECTING) { |
| 493 | notification = new Notification.Builder(context) |
| 494 | .setContentTitle(r.getString( |
| 495 | R.string.wifi_display_notification_connecting_title)) |
| 496 | .setContentText(r.getString( |
| 497 | R.string.wifi_display_notification_connecting_message, |
| 498 | display.getFriendlyDisplayName())) |
| 499 | .setContentIntent(mSettingsPendingIntent) |
| 500 | .setSmallIcon(R.drawable.ic_notification_cast_connecting) |
| 501 | .setOngoing(true) |
| 502 | .addAction(android.R.drawable.ic_menu_close_clear_cancel, |
| 503 | r.getString(R.string.wifi_display_notification_disconnect), |
| 504 | mDisconnectPendingIntent) |
| 505 | .build(); |
| 506 | } else { |
| 507 | notification = new Notification.Builder(context) |
| 508 | .setContentTitle(r.getString( |
| 509 | R.string.wifi_display_notification_connected_title)) |
| 510 | .setContentText(r.getString( |
| 511 | R.string.wifi_display_notification_connected_message, |
| 512 | display.getFriendlyDisplayName())) |
| 513 | .setContentIntent(mSettingsPendingIntent) |
| 514 | .setSmallIcon(R.drawable.ic_notification_cast_on) |
| 515 | .setOngoing(true) |
| 516 | .addAction(android.R.drawable.ic_menu_close_clear_cancel, |
| 517 | r.getString(R.string.wifi_display_notification_disconnect), |
| 518 | mDisconnectPendingIntent) |
| 519 | .build(); |
| 520 | } |
Jeff Brown | a7f9c96 | 2012-10-17 15:15:12 -0700 | [diff] [blame] | 521 | mNotificationManager.notifyAsUser(null, |
Jeff Brown | e7ae644 | 2013-11-13 00:45:18 -0800 | [diff] [blame] | 522 | R.string.wifi_display_notification_disconnect, |
Jeff Brown | a7f9c96 | 2012-10-17 15:15:12 -0700 | [diff] [blame] | 523 | notification, UserHandle.ALL); |
| 524 | } |
| 525 | } |
| 526 | |
| 527 | private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() { |
| 528 | @Override |
| 529 | public void onReceive(Context context, Intent intent) { |
| 530 | if (intent.getAction().equals(ACTION_DISCONNECT)) { |
| 531 | synchronized (getSyncRoot()) { |
| 532 | requestDisconnectLocked(); |
| 533 | } |
| 534 | } |
Jeff Brown | e08ae38 | 2012-09-07 20:36:36 -0700 | [diff] [blame] | 535 | } |
| 536 | }; |
| 537 | |
Jeff Brown | cbad976 | 2012-09-04 21:57:59 -0700 | [diff] [blame] | 538 | private final WifiDisplayController.Listener mWifiDisplayListener = |
| 539 | new WifiDisplayController.Listener() { |
| 540 | @Override |
Jeff Brown | 89d5546 | 2012-09-19 11:33:42 -0700 | [diff] [blame] | 541 | public void onFeatureStateChanged(int featureState) { |
Jeff Brown | cbad976 | 2012-09-04 21:57:59 -0700 | [diff] [blame] | 542 | synchronized (getSyncRoot()) { |
Jeff Brown | 89d5546 | 2012-09-19 11:33:42 -0700 | [diff] [blame] | 543 | if (mFeatureState != featureState) { |
| 544 | mFeatureState = featureState; |
Jeff Brown | e08ae38 | 2012-09-07 20:36:36 -0700 | [diff] [blame] | 545 | scheduleStatusChangedBroadcastLocked(); |
| 546 | } |
| 547 | } |
| 548 | } |
| 549 | |
| 550 | @Override |
| 551 | public void onScanStarted() { |
| 552 | synchronized (getSyncRoot()) { |
Jeff Brown | 180bbc7 | 2012-09-08 23:15:00 -0700 | [diff] [blame] | 553 | if (mScanState != WifiDisplayStatus.SCAN_STATE_SCANNING) { |
Jeff Brown | 180bbc7 | 2012-09-08 23:15:00 -0700 | [diff] [blame] | 554 | mScanState = WifiDisplayStatus.SCAN_STATE_SCANNING; |
Jeff Brown | e08ae38 | 2012-09-07 20:36:36 -0700 | [diff] [blame] | 555 | scheduleStatusChangedBroadcastLocked(); |
| 556 | } |
| 557 | } |
| 558 | } |
| 559 | |
Jeff Brown | 2444ae7 | 2012-10-11 14:30:21 -0700 | [diff] [blame] | 560 | @Override |
Jeff Brown | ce468a3 | 2013-11-21 16:42:03 -0800 | [diff] [blame] | 561 | public void onScanResults(WifiDisplay[] availableDisplays) { |
Jeff Brown | e08ae38 | 2012-09-07 20:36:36 -0700 | [diff] [blame] | 562 | synchronized (getSyncRoot()) { |
Jeff Brown | 89d5546 | 2012-09-19 11:33:42 -0700 | [diff] [blame] | 563 | availableDisplays = mPersistentDataStore.applyWifiDisplayAliases( |
| 564 | availableDisplays); |
| 565 | |
Chong Zhang | ab87a63 | 2013-06-11 10:25:49 -0700 | [diff] [blame] | 566 | boolean changed = !Arrays.equals(mAvailableDisplays, availableDisplays); |
Jeff Brown | ce468a3 | 2013-11-21 16:42:03 -0800 | [diff] [blame] | 567 | |
| 568 | // Check whether any of the available displays changed canConnect status. |
Chong Zhang | ab87a63 | 2013-06-11 10:25:49 -0700 | [diff] [blame] | 569 | for (int i = 0; !changed && i<availableDisplays.length; i++) { |
| 570 | changed = availableDisplays[i].canConnect() |
| 571 | != mAvailableDisplays[i].canConnect(); |
| 572 | } |
| 573 | |
Jeff Brown | ce468a3 | 2013-11-21 16:42:03 -0800 | [diff] [blame] | 574 | if (changed) { |
Jeff Brown | 89d5546 | 2012-09-19 11:33:42 -0700 | [diff] [blame] | 575 | mAvailableDisplays = availableDisplays; |
Jeff Brown | 74da109 | 2012-11-07 16:02:13 -0800 | [diff] [blame] | 576 | fixRememberedDisplayNamesFromAvailableDisplaysLocked(); |
Chong Zhang | ab87a63 | 2013-06-11 10:25:49 -0700 | [diff] [blame] | 577 | updateDisplaysLocked(); |
Jeff Brown | e08ae38 | 2012-09-07 20:36:36 -0700 | [diff] [blame] | 578 | scheduleStatusChangedBroadcastLocked(); |
| 579 | } |
| 580 | } |
| 581 | } |
| 582 | |
| 583 | @Override |
Jeff Brown | ce468a3 | 2013-11-21 16:42:03 -0800 | [diff] [blame] | 584 | public void onScanFinished() { |
| 585 | synchronized (getSyncRoot()) { |
| 586 | if (mScanState != WifiDisplayStatus.SCAN_STATE_NOT_SCANNING) { |
| 587 | mScanState = WifiDisplayStatus.SCAN_STATE_NOT_SCANNING; |
| 588 | scheduleStatusChangedBroadcastLocked(); |
| 589 | } |
| 590 | } |
| 591 | } |
| 592 | |
| 593 | @Override |
Jeff Brown | e08ae38 | 2012-09-07 20:36:36 -0700 | [diff] [blame] | 594 | public void onDisplayConnecting(WifiDisplay display) { |
| 595 | synchronized (getSyncRoot()) { |
Jeff Brown | 89d5546 | 2012-09-19 11:33:42 -0700 | [diff] [blame] | 596 | display = mPersistentDataStore.applyWifiDisplayAlias(display); |
| 597 | |
Jeff Brown | 180bbc7 | 2012-09-08 23:15:00 -0700 | [diff] [blame] | 598 | if (mActiveDisplayState != WifiDisplayStatus.DISPLAY_STATE_CONNECTING |
| 599 | || mActiveDisplay == null |
| 600 | || !mActiveDisplay.equals(display)) { |
Jeff Brown | 180bbc7 | 2012-09-08 23:15:00 -0700 | [diff] [blame] | 601 | mActiveDisplayState = WifiDisplayStatus.DISPLAY_STATE_CONNECTING; |
| 602 | mActiveDisplay = display; |
Jeff Brown | e08ae38 | 2012-09-07 20:36:36 -0700 | [diff] [blame] | 603 | scheduleStatusChangedBroadcastLocked(); |
Jeff Brown | e7ae644 | 2013-11-13 00:45:18 -0800 | [diff] [blame] | 604 | scheduleUpdateNotificationLocked(); |
Jeff Brown | e08ae38 | 2012-09-07 20:36:36 -0700 | [diff] [blame] | 605 | } |
| 606 | } |
| 607 | } |
| 608 | |
| 609 | @Override |
| 610 | public void onDisplayConnectionFailed() { |
| 611 | synchronized (getSyncRoot()) { |
Jeff Brown | 180bbc7 | 2012-09-08 23:15:00 -0700 | [diff] [blame] | 612 | if (mActiveDisplayState != WifiDisplayStatus.DISPLAY_STATE_NOT_CONNECTED |
| 613 | || mActiveDisplay != null) { |
Jeff Brown | 180bbc7 | 2012-09-08 23:15:00 -0700 | [diff] [blame] | 614 | mActiveDisplayState = WifiDisplayStatus.DISPLAY_STATE_NOT_CONNECTED; |
| 615 | mActiveDisplay = null; |
Jeff Brown | e08ae38 | 2012-09-07 20:36:36 -0700 | [diff] [blame] | 616 | scheduleStatusChangedBroadcastLocked(); |
Jeff Brown | e7ae644 | 2013-11-13 00:45:18 -0800 | [diff] [blame] | 617 | scheduleUpdateNotificationLocked(); |
Jeff Brown | e08ae38 | 2012-09-07 20:36:36 -0700 | [diff] [blame] | 618 | } |
| 619 | } |
| 620 | } |
| 621 | |
| 622 | @Override |
Jeff Brown | f8f0edd | 2012-09-11 17:05:11 -0700 | [diff] [blame] | 623 | public void onDisplayConnected(WifiDisplay display, Surface surface, |
| 624 | int width, int height, int flags) { |
Jeff Brown | e08ae38 | 2012-09-07 20:36:36 -0700 | [diff] [blame] | 625 | synchronized (getSyncRoot()) { |
Jeff Brown | 89d5546 | 2012-09-19 11:33:42 -0700 | [diff] [blame] | 626 | display = mPersistentDataStore.applyWifiDisplayAlias(display); |
Jeff Brown | 74da109 | 2012-11-07 16:02:13 -0800 | [diff] [blame] | 627 | addDisplayDeviceLocked(display, surface, width, height, flags); |
Jeff Brown | e08ae38 | 2012-09-07 20:36:36 -0700 | [diff] [blame] | 628 | |
Jeff Brown | 180bbc7 | 2012-09-08 23:15:00 -0700 | [diff] [blame] | 629 | if (mActiveDisplayState != WifiDisplayStatus.DISPLAY_STATE_CONNECTED |
| 630 | || mActiveDisplay == null |
| 631 | || !mActiveDisplay.equals(display)) { |
Jeff Brown | 180bbc7 | 2012-09-08 23:15:00 -0700 | [diff] [blame] | 632 | mActiveDisplayState = WifiDisplayStatus.DISPLAY_STATE_CONNECTED; |
| 633 | mActiveDisplay = display; |
Jeff Brown | e08ae38 | 2012-09-07 20:36:36 -0700 | [diff] [blame] | 634 | scheduleStatusChangedBroadcastLocked(); |
Jeff Brown | e7ae644 | 2013-11-13 00:45:18 -0800 | [diff] [blame] | 635 | scheduleUpdateNotificationLocked(); |
Jeff Brown | e08ae38 | 2012-09-07 20:36:36 -0700 | [diff] [blame] | 636 | } |
Jeff Brown | cbad976 | 2012-09-04 21:57:59 -0700 | [diff] [blame] | 637 | } |
| 638 | } |
| 639 | |
| 640 | @Override |
Chong Zhang | 1f3ecaa | 2013-05-03 15:55:36 -0700 | [diff] [blame] | 641 | public void onDisplaySessionInfo(WifiDisplaySessionInfo sessionInfo) { |
| 642 | synchronized (getSyncRoot()) { |
| 643 | mSessionInfo = sessionInfo; |
| 644 | scheduleStatusChangedBroadcastLocked(); |
| 645 | } |
| 646 | } |
| 647 | |
| 648 | @Override |
Jeff Brown | 74da109 | 2012-11-07 16:02:13 -0800 | [diff] [blame] | 649 | public void onDisplayChanged(WifiDisplay display) { |
| 650 | synchronized (getSyncRoot()) { |
| 651 | display = mPersistentDataStore.applyWifiDisplayAlias(display); |
| 652 | if (mActiveDisplay != null |
| 653 | && mActiveDisplay.hasSameAddress(display) |
| 654 | && !mActiveDisplay.equals(display)) { |
| 655 | mActiveDisplay = display; |
| 656 | renameDisplayDeviceLocked(display.getFriendlyDisplayName()); |
| 657 | scheduleStatusChangedBroadcastLocked(); |
Jeff Brown | e7ae644 | 2013-11-13 00:45:18 -0800 | [diff] [blame] | 658 | scheduleUpdateNotificationLocked(); |
Jeff Brown | 74da109 | 2012-11-07 16:02:13 -0800 | [diff] [blame] | 659 | } |
| 660 | } |
| 661 | } |
| 662 | |
| 663 | @Override |
Jeff Brown | cbad976 | 2012-09-04 21:57:59 -0700 | [diff] [blame] | 664 | public void onDisplayDisconnected() { |
| 665 | // Stop listening. |
| 666 | synchronized (getSyncRoot()) { |
Jeff Brown | 74da109 | 2012-11-07 16:02:13 -0800 | [diff] [blame] | 667 | removeDisplayDeviceLocked(); |
Jeff Brown | e08ae38 | 2012-09-07 20:36:36 -0700 | [diff] [blame] | 668 | |
Jeff Brown | 180bbc7 | 2012-09-08 23:15:00 -0700 | [diff] [blame] | 669 | if (mActiveDisplayState != WifiDisplayStatus.DISPLAY_STATE_NOT_CONNECTED |
| 670 | || mActiveDisplay != null) { |
Jeff Brown | 180bbc7 | 2012-09-08 23:15:00 -0700 | [diff] [blame] | 671 | mActiveDisplayState = WifiDisplayStatus.DISPLAY_STATE_NOT_CONNECTED; |
| 672 | mActiveDisplay = null; |
Jeff Brown | e08ae38 | 2012-09-07 20:36:36 -0700 | [diff] [blame] | 673 | scheduleStatusChangedBroadcastLocked(); |
Jeff Brown | e7ae644 | 2013-11-13 00:45:18 -0800 | [diff] [blame] | 674 | scheduleUpdateNotificationLocked(); |
Jeff Brown | e08ae38 | 2012-09-07 20:36:36 -0700 | [diff] [blame] | 675 | } |
Jeff Brown | cbad976 | 2012-09-04 21:57:59 -0700 | [diff] [blame] | 676 | } |
| 677 | } |
| 678 | }; |
| 679 | |
| 680 | private final class WifiDisplayDevice extends DisplayDevice { |
Jeff Brown | ee4f029 | 2012-10-15 15:31:59 -0700 | [diff] [blame] | 681 | private String mName; |
Jeff Brown | cbad976 | 2012-09-04 21:57:59 -0700 | [diff] [blame] | 682 | private final int mWidth; |
| 683 | private final int mHeight; |
| 684 | private final float mRefreshRate; |
| 685 | private final int mFlags; |
Jeff Brown | 92130f6 | 2012-10-24 21:28:33 -0700 | [diff] [blame] | 686 | private final String mAddress; |
Jeff Brown | cbad976 | 2012-09-04 21:57:59 -0700 | [diff] [blame] | 687 | |
| 688 | private Surface mSurface; |
| 689 | private DisplayDeviceInfo mInfo; |
| 690 | |
| 691 | public WifiDisplayDevice(IBinder displayToken, String name, |
Jeff Brown | 92130f6 | 2012-10-24 21:28:33 -0700 | [diff] [blame] | 692 | int width, int height, float refreshRate, int flags, String address, |
Jeff Brown | cbad976 | 2012-09-04 21:57:59 -0700 | [diff] [blame] | 693 | Surface surface) { |
| 694 | super(WifiDisplayAdapter.this, displayToken); |
| 695 | mName = name; |
| 696 | mWidth = width; |
| 697 | mHeight = height; |
| 698 | mRefreshRate = refreshRate; |
| 699 | mFlags = flags; |
Jeff Brown | 92130f6 | 2012-10-24 21:28:33 -0700 | [diff] [blame] | 700 | mAddress = address; |
Jeff Brown | cbad976 | 2012-09-04 21:57:59 -0700 | [diff] [blame] | 701 | mSurface = surface; |
| 702 | } |
| 703 | |
Jesse Hall | 6a6bc21 | 2013-08-08 12:15:03 -0700 | [diff] [blame] | 704 | public void destroyLocked() { |
| 705 | if (mSurface != null) { |
| 706 | mSurface.release(); |
| 707 | mSurface = null; |
| 708 | } |
| 709 | SurfaceControl.destroyDisplay(getDisplayTokenLocked()); |
Jeff Brown | cbad976 | 2012-09-04 21:57:59 -0700 | [diff] [blame] | 710 | } |
| 711 | |
Jeff Brown | ee4f029 | 2012-10-15 15:31:59 -0700 | [diff] [blame] | 712 | public void setNameLocked(String name) { |
| 713 | mName = name; |
| 714 | mInfo = null; |
| 715 | } |
| 716 | |
Jeff Brown | cbad976 | 2012-09-04 21:57:59 -0700 | [diff] [blame] | 717 | @Override |
| 718 | public void performTraversalInTransactionLocked() { |
Jesse Hall | 6a6bc21 | 2013-08-08 12:15:03 -0700 | [diff] [blame] | 719 | if (mSurface != null) { |
| 720 | setSurfaceInTransactionLocked(mSurface); |
| 721 | } |
Jeff Brown | cbad976 | 2012-09-04 21:57:59 -0700 | [diff] [blame] | 722 | } |
| 723 | |
| 724 | @Override |
| 725 | public DisplayDeviceInfo getDisplayDeviceInfoLocked() { |
| 726 | if (mInfo == null) { |
| 727 | mInfo = new DisplayDeviceInfo(); |
| 728 | mInfo.name = mName; |
| 729 | mInfo.width = mWidth; |
| 730 | mInfo.height = mHeight; |
| 731 | mInfo.refreshRate = mRefreshRate; |
Andy McFadden | e8b1aeb | 2014-06-13 14:05:40 -0700 | [diff] [blame] | 732 | mInfo.presentationDeadlineNanos = 1000000000L / (int) mRefreshRate; // 1 frame |
Jeff Brown | cbad976 | 2012-09-04 21:57:59 -0700 | [diff] [blame] | 733 | mInfo.flags = mFlags; |
Jeff Brown | 92130f6 | 2012-10-24 21:28:33 -0700 | [diff] [blame] | 734 | mInfo.type = Display.TYPE_WIFI; |
| 735 | mInfo.address = mAddress; |
Jeff Brown | d728bf5 | 2012-09-08 18:05:28 -0700 | [diff] [blame] | 736 | mInfo.touch = DisplayDeviceInfo.TOUCH_EXTERNAL; |
Jeff Brown | cbad976 | 2012-09-04 21:57:59 -0700 | [diff] [blame] | 737 | mInfo.setAssumedDensityForExternalDisplay(mWidth, mHeight); |
| 738 | } |
| 739 | return mInfo; |
| 740 | } |
| 741 | } |
Jeff Brown | a7f9c96 | 2012-10-17 15:15:12 -0700 | [diff] [blame] | 742 | |
| 743 | private final class WifiDisplayHandler extends Handler { |
| 744 | public WifiDisplayHandler(Looper looper) { |
| 745 | super(looper, null, true /*async*/); |
| 746 | } |
| 747 | |
| 748 | @Override |
| 749 | public void handleMessage(Message msg) { |
| 750 | switch (msg.what) { |
| 751 | case MSG_SEND_STATUS_CHANGE_BROADCAST: |
| 752 | handleSendStatusChangeBroadcast(); |
| 753 | break; |
| 754 | |
| 755 | case MSG_UPDATE_NOTIFICATION: |
| 756 | handleUpdateNotification(); |
| 757 | break; |
| 758 | } |
| 759 | } |
| 760 | } |
Jeff Brown | cbad976 | 2012-09-04 21:57:59 -0700 | [diff] [blame] | 761 | } |