MediaFocusControl.java revision 19566543c4833f50e7c22aff0c707388f8a338d2
1/* 2 * Copyright (C) 2013 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 17package android.media; 18 19import android.app.Activity; 20import android.app.ActivityManager; 21import android.app.AppOpsManager; 22import android.app.KeyguardManager; 23import android.app.PendingIntent; 24import android.app.PendingIntent.CanceledException; 25import android.app.PendingIntent.OnFinished; 26import android.content.ActivityNotFoundException; 27import android.content.BroadcastReceiver; 28import android.content.ComponentName; 29import android.content.ContentResolver; 30import android.content.Context; 31import android.content.Intent; 32import android.content.IntentFilter; 33import android.content.pm.PackageManager; 34import android.database.ContentObserver; 35import android.net.Uri; 36import android.os.Binder; 37import android.os.Bundle; 38import android.os.Handler; 39import android.os.IBinder; 40import android.os.Looper; 41import android.os.Message; 42import android.os.PowerManager; 43import android.os.RemoteException; 44import android.os.UserHandle; 45import android.os.IBinder.DeathRecipient; 46import android.provider.Settings; 47import android.speech.RecognizerIntent; 48import android.telephony.PhoneStateListener; 49import android.telephony.TelephonyManager; 50import android.util.Log; 51import android.util.Slog; 52import android.view.KeyEvent; 53 54import java.io.FileDescriptor; 55import java.io.PrintWriter; 56import java.util.ArrayList; 57import java.util.Iterator; 58import java.util.Stack; 59 60/** 61 * @hide 62 * 63 */ 64public class MediaFocusControl implements OnFinished { 65 66 private static final String TAG = "MediaFocusControl"; 67 68 /** Debug remote control client/display feature */ 69 protected static final boolean DEBUG_RC = false; 70 /** Debug volumes */ 71 protected static final boolean DEBUG_VOL = false; 72 73 /** Used to alter media button redirection when the phone is ringing. */ 74 private boolean mIsRinging = false; 75 76 private final PowerManager.WakeLock mMediaEventWakeLock; 77 private final MediaEventHandler mEventHandler; 78 private final Context mContext; 79 private final ContentResolver mContentResolver; 80 private final VolumeController mVolumeController; 81 private final BroadcastReceiver mReceiver = new PackageIntentsReceiver(); 82 private final AppOpsManager mAppOps; 83 private final KeyguardManager mKeyguardManager; 84 private final AudioService mAudioService; 85 private final NotificationListenerObserver mNotifListenerObserver; 86 87 protected MediaFocusControl(Looper looper, Context cntxt, 88 VolumeController volumeCtrl, AudioService as) { 89 mEventHandler = new MediaEventHandler(looper); 90 mContext = cntxt; 91 mContentResolver = mContext.getContentResolver(); 92 mVolumeController = volumeCtrl; 93 mAudioService = as; 94 95 PowerManager pm = (PowerManager)mContext.getSystemService(Context.POWER_SERVICE); 96 mMediaEventWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "handleMediaEvent"); 97 mMainRemote = new RemotePlaybackState(-1, 98 AudioService.getMaxStreamVolume(AudioManager.STREAM_MUSIC), 99 AudioService.getMaxStreamVolume(AudioManager.STREAM_MUSIC)); 100 101 // Register for phone state monitoring 102 TelephonyManager tmgr = (TelephonyManager) 103 mContext.getSystemService(Context.TELEPHONY_SERVICE); 104 tmgr.listen(mPhoneStateListener, PhoneStateListener.LISTEN_CALL_STATE); 105 106 // Register for package addition/removal/change intent broadcasts 107 // for media button receiver persistence 108 IntentFilter pkgFilter = new IntentFilter(); 109 pkgFilter.addAction(Intent.ACTION_PACKAGE_REMOVED); 110 pkgFilter.addAction(Intent.ACTION_PACKAGE_ADDED); 111 pkgFilter.addAction(Intent.ACTION_PACKAGE_CHANGED); 112 pkgFilter.addAction(Intent.ACTION_PACKAGE_DATA_CLEARED); 113 pkgFilter.addDataScheme("package"); 114 mContext.registerReceiver(mReceiver, pkgFilter); 115 116 mAppOps = (AppOpsManager)mContext.getSystemService(Context.APP_OPS_SERVICE); 117 mKeyguardManager = 118 (KeyguardManager) mContext.getSystemService(Context.KEYGUARD_SERVICE); 119 mNotifListenerObserver = new NotificationListenerObserver(); 120 121 mHasRemotePlayback = false; 122 mMainRemoteIsActive = false; 123 postReevaluateRemote(); 124 } 125 126 protected void dump(PrintWriter pw) { 127 dumpFocusStack(pw); 128 dumpRCStack(pw); 129 dumpRCCStack(pw); 130 dumpRCDList(pw); 131 } 132 133 //========================================================================================== 134 // Management of RemoteControlDisplay registration permissions 135 //========================================================================================== 136 private final static Uri ENABLED_NOTIFICATION_LISTENERS_URI = 137 Settings.Secure.getUriFor(Settings.Secure.ENABLED_NOTIFICATION_LISTENERS); 138 139 private class NotificationListenerObserver extends ContentObserver { 140 141 NotificationListenerObserver() { 142 super(mEventHandler); 143 mContentResolver.registerContentObserver(Settings.Secure.getUriFor( 144 Settings.Secure.ENABLED_NOTIFICATION_LISTENERS), false, this); 145 } 146 147 @Override 148 public void onChange(boolean selfChange, Uri uri) { 149 if (!ENABLED_NOTIFICATION_LISTENERS_URI.equals(uri) || selfChange) { 150 return; 151 } 152 if (DEBUG_RC) { Log.d(TAG, "NotificationListenerObserver.onChange()"); } 153 postReevaluateRemoteControlDisplays(); 154 } 155 } 156 157 private final static int RCD_REG_FAILURE = 0; 158 private final static int RCD_REG_SUCCESS_PERMISSION = 1; 159 private final static int RCD_REG_SUCCESS_ENABLED_NOTIF = 2; 160 161 /** 162 * Checks a caller's authorization to register an IRemoteControlDisplay. 163 * Authorization is granted if one of the following is true: 164 * <ul> 165 * <li>the caller has android.Manifest.permission.MEDIA_CONTENT_CONTROL permission</li> 166 * <li>the caller's listener is one of the enabled notification listeners</li> 167 * </ul> 168 * @return RCD_REG_FAILURE if it's not safe to proceed with the IRemoteControlDisplay 169 * registration. 170 */ 171 private int checkRcdRegistrationAuthorization(ComponentName listenerComp) { 172 // MEDIA_CONTENT_CONTROL permission check 173 if (PackageManager.PERMISSION_GRANTED == mContext.checkCallingOrSelfPermission( 174 android.Manifest.permission.MEDIA_CONTENT_CONTROL)) { 175 if (DEBUG_RC) { Log.d(TAG, "ok to register Rcd: has MEDIA_CONTENT_CONTROL permission");} 176 return RCD_REG_SUCCESS_PERMISSION; 177 } 178 179 // ENABLED_NOTIFICATION_LISTENERS settings check 180 if (listenerComp != null) { 181 // this call is coming from an app, can't use its identity to read secure settings 182 final long ident = Binder.clearCallingIdentity(); 183 try { 184 final int currentUser = ActivityManager.getCurrentUser(); 185 final String enabledNotifListeners = Settings.Secure.getStringForUser( 186 mContext.getContentResolver(), 187 Settings.Secure.ENABLED_NOTIFICATION_LISTENERS, 188 currentUser); 189 if (enabledNotifListeners != null) { 190 final String[] components = enabledNotifListeners.split(":"); 191 for (int i=0; i<components.length; i++) { 192 final ComponentName component = 193 ComponentName.unflattenFromString(components[i]); 194 if (component != null) { 195 if (listenerComp.equals(component)) { 196 if (DEBUG_RC) { Log.d(TAG, "ok to register RCC: " + component + 197 " is authorized notification listener"); } 198 return RCD_REG_SUCCESS_ENABLED_NOTIF; 199 } 200 } 201 } 202 } 203 if (DEBUG_RC) { Log.d(TAG, "not ok to register RCD, " + listenerComp + 204 " is not in list of ENABLED_NOTIFICATION_LISTENERS"); } 205 } finally { 206 Binder.restoreCallingIdentity(ident); 207 } 208 } 209 210 return RCD_REG_FAILURE; 211 } 212 213 protected boolean registerRemoteController(IRemoteControlDisplay rcd, int w, int h, 214 ComponentName listenerComp) { 215 int reg = checkRcdRegistrationAuthorization(listenerComp); 216 if (reg != RCD_REG_FAILURE) { 217 registerRemoteControlDisplay_int(rcd, w, h, listenerComp); 218 return true; 219 } else { 220 Slog.w(TAG, "Access denied to process: " + Binder.getCallingPid() + 221 ", must have permission " + android.Manifest.permission.MEDIA_CONTENT_CONTROL + 222 " or be an enabled NotificationListenerService for registerRemoteController"); 223 return false; 224 } 225 } 226 227 protected boolean registerRemoteControlDisplay(IRemoteControlDisplay rcd, int w, int h) { 228 int reg = checkRcdRegistrationAuthorization(null); 229 if (reg != RCD_REG_FAILURE) { 230 registerRemoteControlDisplay_int(rcd, w, h, null); 231 return true; 232 } else { 233 Slog.w(TAG, "Access denied to process: " + Binder.getCallingPid() + 234 ", must have permission " + android.Manifest.permission.MEDIA_CONTENT_CONTROL + 235 " to register IRemoteControlDisplay"); 236 return false; 237 } 238 } 239 240 private void postReevaluateRemoteControlDisplays() { 241 sendMsg(mEventHandler, MSG_REEVALUATE_RCD, SENDMSG_QUEUE, 0, 0, null, 0); 242 } 243 244 private void onReevaluateRemoteControlDisplays() { 245 if (DEBUG_RC) { Log.d(TAG, "onReevaluateRemoteControlDisplays()"); } 246 // read which components are enabled notification listeners 247 final int currentUser = ActivityManager.getCurrentUser(); 248 final String enabledNotifListeners = Settings.Secure.getStringForUser( 249 mContext.getContentResolver(), 250 Settings.Secure.ENABLED_NOTIFICATION_LISTENERS, 251 currentUser); 252 if (DEBUG_RC) { Log.d(TAG, " > enabled list: " + enabledNotifListeners); } 253 synchronized(mAudioFocusLock) { 254 synchronized(mRCStack) { 255 // check whether the "enable" status of each RCD with a notification listener 256 // has changed 257 final String[] enabledComponents; 258 if (enabledNotifListeners == null) { 259 enabledComponents = null; 260 } else { 261 enabledComponents = enabledNotifListeners.split(":"); 262 } 263 final Iterator<DisplayInfoForServer> displayIterator = mRcDisplays.iterator(); 264 while (displayIterator.hasNext()) { 265 final DisplayInfoForServer di = 266 (DisplayInfoForServer) displayIterator.next(); 267 if (di.mClientNotifListComp != null) { 268 boolean wasEnabled = di.mEnabled; 269 di.mEnabled = isComponentInStringArray(di.mClientNotifListComp, 270 enabledComponents); 271 if (wasEnabled != di.mEnabled){ 272 try { 273 // tell the RCD whether it's enabled 274 di.mRcDisplay.setEnabled(di.mEnabled); 275 // tell the RCCs about the change for this RCD 276 enableRemoteControlDisplayForClient_syncRcStack( 277 di.mRcDisplay, di.mEnabled); 278 // when enabling, refresh the information on the display 279 if (di.mEnabled) { 280 sendMsg(mEventHandler, MSG_RCDISPLAY_INIT_INFO, SENDMSG_QUEUE, 281 di.mArtworkExpectedWidth /*arg1*/, 282 di.mArtworkExpectedHeight/*arg2*/, 283 di.mRcDisplay /*obj*/, 0/*delay*/); 284 } 285 } catch (RemoteException e) { 286 Log.e(TAG, "Error en/disabling RCD: ", e); 287 } 288 } 289 } 290 } 291 } 292 } 293 } 294 295 /** 296 * @param comp a non-null ComponentName 297 * @param enabledArray may be null 298 * @return 299 */ 300 private boolean isComponentInStringArray(ComponentName comp, String[] enabledArray) { 301 if (enabledArray == null || enabledArray.length == 0) { 302 if (DEBUG_RC) { Log.d(TAG, " > " + comp + " is NOT enabled"); } 303 return false; 304 } 305 final String compString = comp.flattenToString(); 306 for (int i=0; i<enabledArray.length; i++) { 307 if (compString.equals(enabledArray[i])) { 308 if (DEBUG_RC) { Log.d(TAG, " > " + compString + " is enabled"); } 309 return true; 310 } 311 } 312 if (DEBUG_RC) { Log.d(TAG, " > " + compString + " is NOT enabled"); } 313 return false; 314 } 315 316 //========================================================================================== 317 // Internal event handling 318 //========================================================================================== 319 320 // event handler messages 321 private static final int MSG_PERSIST_MEDIABUTTONRECEIVER = 0; 322 private static final int MSG_RCDISPLAY_CLEAR = 1; 323 private static final int MSG_RCDISPLAY_UPDATE = 2; 324 private static final int MSG_REEVALUATE_REMOTE = 3; 325 private static final int MSG_RCC_NEW_PLAYBACK_INFO = 4; 326 private static final int MSG_RCC_NEW_VOLUME_OBS = 5; 327 private static final int MSG_PROMOTE_RCC = 6; 328 private static final int MSG_RCC_NEW_PLAYBACK_STATE = 7; 329 private static final int MSG_RCC_SEEK_REQUEST = 8; 330 private static final int MSG_RCC_UPDATE_METADATA = 9; 331 private static final int MSG_RCDISPLAY_INIT_INFO = 10; 332 private static final int MSG_REEVALUATE_RCD = 11; 333 334 // sendMsg() flags 335 /** If the msg is already queued, replace it with this one. */ 336 private static final int SENDMSG_REPLACE = 0; 337 /** If the msg is already queued, ignore this one and leave the old. */ 338 private static final int SENDMSG_NOOP = 1; 339 /** If the msg is already queued, queue this one and leave the old. */ 340 private static final int SENDMSG_QUEUE = 2; 341 342 private static void sendMsg(Handler handler, int msg, 343 int existingMsgPolicy, int arg1, int arg2, Object obj, int delay) { 344 345 if (existingMsgPolicy == SENDMSG_REPLACE) { 346 handler.removeMessages(msg); 347 } else if (existingMsgPolicy == SENDMSG_NOOP && handler.hasMessages(msg)) { 348 return; 349 } 350 351 handler.sendMessageDelayed(handler.obtainMessage(msg, arg1, arg2, obj), delay); 352 } 353 354 private class MediaEventHandler extends Handler { 355 MediaEventHandler(Looper looper) { 356 super(looper); 357 } 358 359 @Override 360 public void handleMessage(Message msg) { 361 switch(msg.what) { 362 case MSG_PERSIST_MEDIABUTTONRECEIVER: 363 onHandlePersistMediaButtonReceiver( (ComponentName) msg.obj ); 364 break; 365 366 case MSG_RCDISPLAY_CLEAR: 367 onRcDisplayClear(); 368 break; 369 370 case MSG_RCDISPLAY_UPDATE: 371 // msg.obj is guaranteed to be non null 372 onRcDisplayUpdate( (RemoteControlStackEntry) msg.obj, msg.arg1); 373 break; 374 375 case MSG_REEVALUATE_REMOTE: 376 onReevaluateRemote(); 377 break; 378 379 case MSG_RCC_NEW_PLAYBACK_INFO: 380 onNewPlaybackInfoForRcc(msg.arg1 /* rccId */, msg.arg2 /* key */, 381 ((Integer)msg.obj).intValue() /* value */); 382 break; 383 384 case MSG_RCC_NEW_VOLUME_OBS: 385 onRegisterVolumeObserverForRcc(msg.arg1 /* rccId */, 386 (IRemoteVolumeObserver)msg.obj /* rvo */); 387 break; 388 389 case MSG_RCC_NEW_PLAYBACK_STATE: 390 onNewPlaybackStateForRcc(msg.arg1 /* rccId */, 391 msg.arg2 /* state */, 392 (RccPlaybackState)msg.obj /* newState */); 393 break; 394 395 case MSG_RCC_SEEK_REQUEST: 396 onSetRemoteControlClientPlaybackPosition( 397 msg.arg1 /* generationId */, ((Long)msg.obj).longValue() /* timeMs */); 398 break; 399 400 case MSG_RCC_UPDATE_METADATA: 401 onUpdateRemoteControlClientMetadata(msg.arg1 /*genId*/, msg.arg2 /*key*/, 402 (Rating) msg.obj /* value */); 403 break; 404 405 case MSG_PROMOTE_RCC: 406 onPromoteRcc(msg.arg1); 407 break; 408 409 case MSG_RCDISPLAY_INIT_INFO: 410 // msg.obj is guaranteed to be non null 411 onRcDisplayInitInfo((IRemoteControlDisplay)msg.obj /*newRcd*/, 412 msg.arg1/*w*/, msg.arg2/*h*/); 413 break; 414 415 case MSG_REEVALUATE_RCD: 416 onReevaluateRemoteControlDisplays(); 417 break; 418 } 419 } 420 } 421 422 423 //========================================================================================== 424 // AudioFocus 425 //========================================================================================== 426 427 /* constant to identify focus stack entry that is used to hold the focus while the phone 428 * is ringing or during a call. Used by com.android.internal.telephony.CallManager when 429 * entering and exiting calls. 430 */ 431 protected final static String IN_VOICE_COMM_FOCUS_ID = "AudioFocus_For_Phone_Ring_And_Calls"; 432 433 private final static Object mAudioFocusLock = new Object(); 434 435 private final static Object mRingingLock = new Object(); 436 437 private PhoneStateListener mPhoneStateListener = new PhoneStateListener() { 438 @Override 439 public void onCallStateChanged(int state, String incomingNumber) { 440 if (state == TelephonyManager.CALL_STATE_RINGING) { 441 //Log.v(TAG, " CALL_STATE_RINGING"); 442 synchronized(mRingingLock) { 443 mIsRinging = true; 444 } 445 } else if ((state == TelephonyManager.CALL_STATE_OFFHOOK) 446 || (state == TelephonyManager.CALL_STATE_IDLE)) { 447 synchronized(mRingingLock) { 448 mIsRinging = false; 449 } 450 } 451 } 452 }; 453 454 /** 455 * Discard the current audio focus owner. 456 * Notify top of audio focus stack that it lost focus (regardless of possibility to reassign 457 * focus), remove it from the stack, and clear the remote control display. 458 */ 459 protected void discardAudioFocusOwner() { 460 synchronized(mAudioFocusLock) { 461 if (!mFocusStack.empty()) { 462 // notify the current focus owner it lost focus after removing it from stack 463 final FocusRequester exFocusOwner = mFocusStack.pop(); 464 exFocusOwner.handleFocusLoss(AudioManager.AUDIOFOCUS_LOSS); 465 exFocusOwner.release(); 466 // clear RCD 467 synchronized(mRCStack) { 468 clearRemoteControlDisplay_syncAfRcs(); 469 } 470 } 471 } 472 } 473 474 private void notifyTopOfAudioFocusStack() { 475 // notify the top of the stack it gained focus 476 if (!mFocusStack.empty()) { 477 if (canReassignAudioFocus()) { 478 mFocusStack.peek().handleFocusGain(AudioManager.AUDIOFOCUS_GAIN); 479 } 480 } 481 } 482 483 /** 484 * Focus is requested, propagate the associated loss throughout the stack. 485 * @param focusGain the new focus gain that will later be added at the top of the stack 486 */ 487 private void propagateFocusLossFromGain_syncAf(int focusGain) { 488 // going through the audio focus stack to signal new focus, traversing order doesn't 489 // matter as all entries respond to the same external focus gain 490 Iterator<FocusRequester> stackIterator = mFocusStack.iterator(); 491 while(stackIterator.hasNext()) { 492 stackIterator.next().handleExternalFocusGain(focusGain); 493 } 494 } 495 496 private final Stack<FocusRequester> mFocusStack = new Stack<FocusRequester>(); 497 498 /** 499 * Helper function: 500 * Display in the log the current entries in the audio focus stack 501 */ 502 private void dumpFocusStack(PrintWriter pw) { 503 pw.println("\nAudio Focus stack entries (last is top of stack):"); 504 synchronized(mAudioFocusLock) { 505 Iterator<FocusRequester> stackIterator = mFocusStack.iterator(); 506 while(stackIterator.hasNext()) { 507 stackIterator.next().dump(pw); 508 } 509 } 510 } 511 512 /** 513 * Helper function: 514 * Called synchronized on mAudioFocusLock 515 * Remove a focus listener from the focus stack. 516 * @param clientToRemove the focus listener 517 * @param signal if true and the listener was at the top of the focus stack, i.e. it was holding 518 * focus, notify the next item in the stack it gained focus. 519 */ 520 private void removeFocusStackEntry(String clientToRemove, boolean signal) { 521 // is the current top of the focus stack abandoning focus? (because of request, not death) 522 if (!mFocusStack.empty() && mFocusStack.peek().hasSameClient(clientToRemove)) 523 { 524 //Log.i(TAG, " removeFocusStackEntry() removing top of stack"); 525 FocusRequester fr = mFocusStack.pop(); 526 fr.release(); 527 if (signal) { 528 // notify the new top of the stack it gained focus 529 notifyTopOfAudioFocusStack(); 530 // there's a new top of the stack, let the remote control know 531 synchronized(mRCStack) { 532 checkUpdateRemoteControlDisplay_syncAfRcs(RC_INFO_ALL); 533 } 534 } 535 } else { 536 // focus is abandoned by a client that's not at the top of the stack, 537 // no need to update focus. 538 // (using an iterator on the stack so we can safely remove an entry after having 539 // evaluated it, traversal order doesn't matter here) 540 Iterator<FocusRequester> stackIterator = mFocusStack.iterator(); 541 while(stackIterator.hasNext()) { 542 FocusRequester fr = (FocusRequester)stackIterator.next(); 543 if(fr.hasSameClient(clientToRemove)) { 544 Log.i(TAG, "AudioFocus removeFocusStackEntry(): removing entry for " 545 + clientToRemove); 546 stackIterator.remove(); 547 fr.release(); 548 } 549 } 550 } 551 } 552 553 /** 554 * Helper function: 555 * Called synchronized on mAudioFocusLock 556 * Remove focus listeners from the focus stack for a particular client when it has died. 557 */ 558 private void removeFocusStackEntryForClient(IBinder cb) { 559 // is the owner of the audio focus part of the client to remove? 560 boolean isTopOfStackForClientToRemove = !mFocusStack.isEmpty() && 561 mFocusStack.peek().hasSameBinder(cb); 562 // (using an iterator on the stack so we can safely remove an entry after having 563 // evaluated it, traversal order doesn't matter here) 564 Iterator<FocusRequester> stackIterator = mFocusStack.iterator(); 565 while(stackIterator.hasNext()) { 566 FocusRequester fr = (FocusRequester)stackIterator.next(); 567 if(fr.hasSameBinder(cb)) { 568 Log.i(TAG, "AudioFocus removeFocusStackEntry(): removing entry for " + cb); 569 stackIterator.remove(); 570 // the client just died, no need to unlink to its death 571 } 572 } 573 if (isTopOfStackForClientToRemove) { 574 // we removed an entry at the top of the stack: 575 // notify the new top of the stack it gained focus. 576 notifyTopOfAudioFocusStack(); 577 // there's a new top of the stack, let the remote control know 578 synchronized(mRCStack) { 579 checkUpdateRemoteControlDisplay_syncAfRcs(RC_INFO_ALL); 580 } 581 } 582 } 583 584 /** 585 * Helper function: 586 * Returns true if the system is in a state where the focus can be reevaluated, false otherwise. 587 */ 588 private boolean canReassignAudioFocus() { 589 // focus requests are rejected during a phone call or when the phone is ringing 590 // this is equivalent to IN_VOICE_COMM_FOCUS_ID having the focus 591 if (!mFocusStack.isEmpty() && mFocusStack.peek().hasSameClient(IN_VOICE_COMM_FOCUS_ID)) { 592 return false; 593 } 594 return true; 595 } 596 597 /** 598 * Inner class to monitor audio focus client deaths, and remove them from the audio focus 599 * stack if necessary. 600 */ 601 protected class AudioFocusDeathHandler implements IBinder.DeathRecipient { 602 private IBinder mCb; // To be notified of client's death 603 604 AudioFocusDeathHandler(IBinder cb) { 605 mCb = cb; 606 } 607 608 public void binderDied() { 609 synchronized(mAudioFocusLock) { 610 Log.w(TAG, " AudioFocus audio focus client died"); 611 removeFocusStackEntryForClient(mCb); 612 } 613 } 614 615 public IBinder getBinder() { 616 return mCb; 617 } 618 } 619 620 protected int getCurrentAudioFocus() { 621 synchronized(mAudioFocusLock) { 622 if (mFocusStack.empty()) { 623 return AudioManager.AUDIOFOCUS_NONE; 624 } else { 625 return mFocusStack.peek().getGainRequest(); 626 } 627 } 628 } 629 630 /** @see AudioManager#requestAudioFocus(AudioManager.OnAudioFocusChangeListener, int, int) */ 631 protected int requestAudioFocus(int mainStreamType, int focusChangeHint, IBinder cb, 632 IAudioFocusDispatcher fd, String clientId, String callingPackageName) { 633 Log.i(TAG, " AudioFocus requestAudioFocus() from " + clientId); 634 // we need a valid binder callback for clients 635 if (!cb.pingBinder()) { 636 Log.e(TAG, " AudioFocus DOA client for requestAudioFocus(), aborting."); 637 return AudioManager.AUDIOFOCUS_REQUEST_FAILED; 638 } 639 640 if (mAppOps.noteOp(AppOpsManager.OP_TAKE_AUDIO_FOCUS, Binder.getCallingUid(), 641 callingPackageName) != AppOpsManager.MODE_ALLOWED) { 642 return AudioManager.AUDIOFOCUS_REQUEST_FAILED; 643 } 644 645 synchronized(mAudioFocusLock) { 646 if (!canReassignAudioFocus()) { 647 return AudioManager.AUDIOFOCUS_REQUEST_FAILED; 648 } 649 650 // handle the potential premature death of the new holder of the focus 651 // (premature death == death before abandoning focus) 652 // Register for client death notification 653 AudioFocusDeathHandler afdh = new AudioFocusDeathHandler(cb); 654 try { 655 cb.linkToDeath(afdh, 0); 656 } catch (RemoteException e) { 657 // client has already died! 658 Log.w(TAG, "AudioFocus requestAudioFocus() could not link to "+cb+" binder death"); 659 return AudioManager.AUDIOFOCUS_REQUEST_FAILED; 660 } 661 662 if (!mFocusStack.empty() && mFocusStack.peek().hasSameClient(clientId)) { 663 // if focus is already owned by this client and the reason for acquiring the focus 664 // hasn't changed, don't do anything 665 if (mFocusStack.peek().getGainRequest() == focusChangeHint) { 666 // unlink death handler so it can be gc'ed. 667 // linkToDeath() creates a JNI global reference preventing collection. 668 cb.unlinkToDeath(afdh, 0); 669 return AudioManager.AUDIOFOCUS_REQUEST_GRANTED; 670 } 671 // the reason for the audio focus request has changed: remove the current top of 672 // stack and respond as if we had a new focus owner 673 FocusRequester fr = mFocusStack.pop(); 674 fr.release(); 675 } 676 677 // focus requester might already be somewhere below in the stack, remove it 678 removeFocusStackEntry(clientId, false /* signal */); 679 680 // propagate the focus change through the stack 681 if (!mFocusStack.empty()) { 682 propagateFocusLossFromGain_syncAf(focusChangeHint); 683 } 684 685 // push focus requester at the top of the audio focus stack 686 mFocusStack.push(new FocusRequester(mainStreamType, focusChangeHint, fd, cb, 687 clientId, afdh, callingPackageName, Binder.getCallingUid())); 688 689 // there's a new top of the stack, let the remote control know 690 synchronized(mRCStack) { 691 checkUpdateRemoteControlDisplay_syncAfRcs(RC_INFO_ALL); 692 } 693 }//synchronized(mAudioFocusLock) 694 695 return AudioManager.AUDIOFOCUS_REQUEST_GRANTED; 696 } 697 698 /** @see AudioManager#abandonAudioFocus(AudioManager.OnAudioFocusChangeListener) */ 699 protected int abandonAudioFocus(IAudioFocusDispatcher fl, String clientId) { 700 Log.i(TAG, " AudioFocus abandonAudioFocus() from " + clientId); 701 try { 702 // this will take care of notifying the new focus owner if needed 703 synchronized(mAudioFocusLock) { 704 removeFocusStackEntry(clientId, true /*signal*/); 705 } 706 } catch (java.util.ConcurrentModificationException cme) { 707 // Catching this exception here is temporary. It is here just to prevent 708 // a crash seen when the "Silent" notification is played. This is believed to be fixed 709 // but this try catch block is left just to be safe. 710 Log.e(TAG, "FATAL EXCEPTION AudioFocus abandonAudioFocus() caused " + cme); 711 cme.printStackTrace(); 712 } 713 714 return AudioManager.AUDIOFOCUS_REQUEST_GRANTED; 715 } 716 717 718 protected void unregisterAudioFocusClient(String clientId) { 719 synchronized(mAudioFocusLock) { 720 removeFocusStackEntry(clientId, false); 721 } 722 } 723 724 725 //========================================================================================== 726 // RemoteControl 727 //========================================================================================== 728 /** 729 * No-op if the key code for keyEvent is not a valid media key 730 * (see {@link #isValidMediaKeyEvent(KeyEvent)}) 731 * @param keyEvent the key event to send 732 */ 733 protected void dispatchMediaKeyEvent(KeyEvent keyEvent) { 734 filterMediaKeyEvent(keyEvent, false /*needWakeLock*/); 735 } 736 737 /** 738 * No-op if the key code for keyEvent is not a valid media key 739 * (see {@link #isValidMediaKeyEvent(KeyEvent)}) 740 * @param keyEvent the key event to send 741 */ 742 protected void dispatchMediaKeyEventUnderWakelock(KeyEvent keyEvent) { 743 filterMediaKeyEvent(keyEvent, true /*needWakeLock*/); 744 } 745 746 private void filterMediaKeyEvent(KeyEvent keyEvent, boolean needWakeLock) { 747 // sanity check on the incoming key event 748 if (!isValidMediaKeyEvent(keyEvent)) { 749 Log.e(TAG, "not dispatching invalid media key event " + keyEvent); 750 return; 751 } 752 // event filtering for telephony 753 synchronized(mRingingLock) { 754 synchronized(mRCStack) { 755 if ((mMediaReceiverForCalls != null) && 756 (mIsRinging || (mAudioService.getMode() == AudioSystem.MODE_IN_CALL))) { 757 dispatchMediaKeyEventForCalls(keyEvent, needWakeLock); 758 return; 759 } 760 } 761 } 762 // event filtering based on voice-based interactions 763 if (isValidVoiceInputKeyCode(keyEvent.getKeyCode())) { 764 filterVoiceInputKeyEvent(keyEvent, needWakeLock); 765 } else { 766 dispatchMediaKeyEvent(keyEvent, needWakeLock); 767 } 768 } 769 770 /** 771 * Handles the dispatching of the media button events to the telephony package. 772 * Precondition: mMediaReceiverForCalls != null 773 * @param keyEvent a non-null KeyEvent whose key code is one of the supported media buttons 774 * @param needWakeLock true if a PARTIAL_WAKE_LOCK needs to be held while this key event 775 * is dispatched. 776 */ 777 private void dispatchMediaKeyEventForCalls(KeyEvent keyEvent, boolean needWakeLock) { 778 Intent keyIntent = new Intent(Intent.ACTION_MEDIA_BUTTON, null); 779 keyIntent.putExtra(Intent.EXTRA_KEY_EVENT, keyEvent); 780 keyIntent.setPackage(mMediaReceiverForCalls.getPackageName()); 781 if (needWakeLock) { 782 mMediaEventWakeLock.acquire(); 783 keyIntent.putExtra(EXTRA_WAKELOCK_ACQUIRED, WAKELOCK_RELEASE_ON_FINISHED); 784 } 785 final long ident = Binder.clearCallingIdentity(); 786 try { 787 mContext.sendOrderedBroadcastAsUser(keyIntent, UserHandle.ALL, 788 null, mKeyEventDone, mEventHandler, Activity.RESULT_OK, null, null); 789 } finally { 790 Binder.restoreCallingIdentity(ident); 791 } 792 } 793 794 /** 795 * Handles the dispatching of the media button events to one of the registered listeners, 796 * or if there was none, broadcast an ACTION_MEDIA_BUTTON intent to the rest of the system. 797 * @param keyEvent a non-null KeyEvent whose key code is one of the supported media buttons 798 * @param needWakeLock true if a PARTIAL_WAKE_LOCK needs to be held while this key event 799 * is dispatched. 800 */ 801 private void dispatchMediaKeyEvent(KeyEvent keyEvent, boolean needWakeLock) { 802 if (needWakeLock) { 803 mMediaEventWakeLock.acquire(); 804 } 805 Intent keyIntent = new Intent(Intent.ACTION_MEDIA_BUTTON, null); 806 keyIntent.putExtra(Intent.EXTRA_KEY_EVENT, keyEvent); 807 synchronized(mRCStack) { 808 if (!mRCStack.empty()) { 809 // send the intent that was registered by the client 810 try { 811 mRCStack.peek().mMediaIntent.send(mContext, 812 needWakeLock ? WAKELOCK_RELEASE_ON_FINISHED : 0 /*code*/, 813 keyIntent, this, mEventHandler); 814 } catch (CanceledException e) { 815 Log.e(TAG, "Error sending pending intent " + mRCStack.peek()); 816 e.printStackTrace(); 817 } 818 } else { 819 // legacy behavior when nobody registered their media button event receiver 820 // through AudioManager 821 if (needWakeLock) { 822 keyIntent.putExtra(EXTRA_WAKELOCK_ACQUIRED, WAKELOCK_RELEASE_ON_FINISHED); 823 } 824 final long ident = Binder.clearCallingIdentity(); 825 try { 826 mContext.sendOrderedBroadcastAsUser(keyIntent, UserHandle.ALL, 827 null, mKeyEventDone, 828 mEventHandler, Activity.RESULT_OK, null, null); 829 } finally { 830 Binder.restoreCallingIdentity(ident); 831 } 832 } 833 } 834 } 835 836 /** 837 * The different actions performed in response to a voice button key event. 838 */ 839 private final static int VOICEBUTTON_ACTION_DISCARD_CURRENT_KEY_PRESS = 1; 840 private final static int VOICEBUTTON_ACTION_START_VOICE_INPUT = 2; 841 private final static int VOICEBUTTON_ACTION_SIMULATE_KEY_PRESS = 3; 842 843 private final Object mVoiceEventLock = new Object(); 844 private boolean mVoiceButtonDown; 845 private boolean mVoiceButtonHandled; 846 847 /** 848 * Filter key events that may be used for voice-based interactions 849 * @param keyEvent a non-null KeyEvent whose key code is that of one of the supported 850 * media buttons that can be used to trigger voice-based interactions. 851 * @param needWakeLock true if a PARTIAL_WAKE_LOCK needs to be held while this key event 852 * is dispatched. 853 */ 854 private void filterVoiceInputKeyEvent(KeyEvent keyEvent, boolean needWakeLock) { 855 if (DEBUG_RC) { 856 Log.v(TAG, "voice input key event: " + keyEvent + ", needWakeLock=" + needWakeLock); 857 } 858 859 int voiceButtonAction = VOICEBUTTON_ACTION_DISCARD_CURRENT_KEY_PRESS; 860 int keyAction = keyEvent.getAction(); 861 synchronized (mVoiceEventLock) { 862 if (keyAction == KeyEvent.ACTION_DOWN) { 863 if (keyEvent.getRepeatCount() == 0) { 864 // initial down 865 mVoiceButtonDown = true; 866 mVoiceButtonHandled = false; 867 } else if (mVoiceButtonDown && !mVoiceButtonHandled 868 && (keyEvent.getFlags() & KeyEvent.FLAG_LONG_PRESS) != 0) { 869 // long-press, start voice-based interactions 870 mVoiceButtonHandled = true; 871 voiceButtonAction = VOICEBUTTON_ACTION_START_VOICE_INPUT; 872 } 873 } else if (keyAction == KeyEvent.ACTION_UP) { 874 if (mVoiceButtonDown) { 875 // voice button up 876 mVoiceButtonDown = false; 877 if (!mVoiceButtonHandled && !keyEvent.isCanceled()) { 878 voiceButtonAction = VOICEBUTTON_ACTION_SIMULATE_KEY_PRESS; 879 } 880 } 881 } 882 }//synchronized (mVoiceEventLock) 883 884 // take action after media button event filtering for voice-based interactions 885 switch (voiceButtonAction) { 886 case VOICEBUTTON_ACTION_DISCARD_CURRENT_KEY_PRESS: 887 if (DEBUG_RC) Log.v(TAG, " ignore key event"); 888 break; 889 case VOICEBUTTON_ACTION_START_VOICE_INPUT: 890 if (DEBUG_RC) Log.v(TAG, " start voice-based interactions"); 891 // then start the voice-based interactions 892 startVoiceBasedInteractions(needWakeLock); 893 break; 894 case VOICEBUTTON_ACTION_SIMULATE_KEY_PRESS: 895 if (DEBUG_RC) Log.v(TAG, " send simulated key event, wakelock=" + needWakeLock); 896 sendSimulatedMediaButtonEvent(keyEvent, needWakeLock); 897 break; 898 } 899 } 900 901 private void sendSimulatedMediaButtonEvent(KeyEvent originalKeyEvent, boolean needWakeLock) { 902 // send DOWN event 903 KeyEvent keyEvent = KeyEvent.changeAction(originalKeyEvent, KeyEvent.ACTION_DOWN); 904 dispatchMediaKeyEvent(keyEvent, needWakeLock); 905 // send UP event 906 keyEvent = KeyEvent.changeAction(originalKeyEvent, KeyEvent.ACTION_UP); 907 dispatchMediaKeyEvent(keyEvent, needWakeLock); 908 909 } 910 911 private class PackageIntentsReceiver extends BroadcastReceiver { 912 @Override 913 public void onReceive(Context context, Intent intent) { 914 String action = intent.getAction(); 915 if (action.equals(Intent.ACTION_PACKAGE_REMOVED) 916 || action.equals(Intent.ACTION_PACKAGE_DATA_CLEARED)) { 917 if (!intent.getBooleanExtra(Intent.EXTRA_REPLACING, false)) { 918 // a package is being removed, not replaced 919 String packageName = intent.getData().getSchemeSpecificPart(); 920 if (packageName != null) { 921 cleanupMediaButtonReceiverForPackage(packageName, true); 922 } 923 } 924 } else if (action.equals(Intent.ACTION_PACKAGE_ADDED) 925 || action.equals(Intent.ACTION_PACKAGE_CHANGED)) { 926 String packageName = intent.getData().getSchemeSpecificPart(); 927 if (packageName != null) { 928 cleanupMediaButtonReceiverForPackage(packageName, false); 929 } 930 } 931 } 932 } 933 934 protected static boolean isMediaKeyCode(int keyCode) { 935 switch (keyCode) { 936 case KeyEvent.KEYCODE_MUTE: 937 case KeyEvent.KEYCODE_HEADSETHOOK: 938 case KeyEvent.KEYCODE_MEDIA_PLAY: 939 case KeyEvent.KEYCODE_MEDIA_PAUSE: 940 case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE: 941 case KeyEvent.KEYCODE_MEDIA_STOP: 942 case KeyEvent.KEYCODE_MEDIA_NEXT: 943 case KeyEvent.KEYCODE_MEDIA_PREVIOUS: 944 case KeyEvent.KEYCODE_MEDIA_REWIND: 945 case KeyEvent.KEYCODE_MEDIA_RECORD: 946 case KeyEvent.KEYCODE_MEDIA_FAST_FORWARD: 947 case KeyEvent.KEYCODE_MEDIA_CLOSE: 948 case KeyEvent.KEYCODE_MEDIA_EJECT: 949 case KeyEvent.KEYCODE_MEDIA_AUDIO_TRACK: 950 return true; 951 default: 952 return false; 953 } 954 } 955 956 private static boolean isValidMediaKeyEvent(KeyEvent keyEvent) { 957 if (keyEvent == null) { 958 return false; 959 } 960 return MediaFocusControl.isMediaKeyCode(keyEvent.getKeyCode()); 961 } 962 963 /** 964 * Checks whether the given key code is one that can trigger the launch of voice-based 965 * interactions. 966 * @param keyCode the key code associated with the key event 967 * @return true if the key is one of the supported voice-based interaction triggers 968 */ 969 private static boolean isValidVoiceInputKeyCode(int keyCode) { 970 if (keyCode == KeyEvent.KEYCODE_HEADSETHOOK) { 971 return true; 972 } else { 973 return false; 974 } 975 } 976 977 /** 978 * Tell the system to start voice-based interactions / voice commands 979 */ 980 private void startVoiceBasedInteractions(boolean needWakeLock) { 981 Intent voiceIntent = null; 982 // select which type of search to launch: 983 // - screen on and device unlocked: action is ACTION_WEB_SEARCH 984 // - device locked or screen off: action is ACTION_VOICE_SEARCH_HANDS_FREE 985 // with EXTRA_SECURE set to true if the device is securely locked 986 PowerManager pm = (PowerManager)mContext.getSystemService(Context.POWER_SERVICE); 987 boolean isLocked = mKeyguardManager != null && mKeyguardManager.isKeyguardLocked(); 988 if (!isLocked && pm.isScreenOn()) { 989 voiceIntent = new Intent(android.speech.RecognizerIntent.ACTION_WEB_SEARCH); 990 Log.i(TAG, "voice-based interactions: about to use ACTION_WEB_SEARCH"); 991 } else { 992 voiceIntent = new Intent(RecognizerIntent.ACTION_VOICE_SEARCH_HANDS_FREE); 993 voiceIntent.putExtra(RecognizerIntent.EXTRA_SECURE, 994 isLocked && mKeyguardManager.isKeyguardSecure()); 995 Log.i(TAG, "voice-based interactions: about to use ACTION_VOICE_SEARCH_HANDS_FREE"); 996 } 997 // start the search activity 998 if (needWakeLock) { 999 mMediaEventWakeLock.acquire(); 1000 } 1001 final long identity = Binder.clearCallingIdentity(); 1002 try { 1003 if (voiceIntent != null) { 1004 voiceIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK 1005 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS); 1006 mContext.startActivityAsUser(voiceIntent, UserHandle.CURRENT); 1007 } 1008 } catch (ActivityNotFoundException e) { 1009 Log.w(TAG, "No activity for search: " + e); 1010 } finally { 1011 Binder.restoreCallingIdentity(identity); 1012 if (needWakeLock) { 1013 mMediaEventWakeLock.release(); 1014 } 1015 } 1016 } 1017 1018 private static final int WAKELOCK_RELEASE_ON_FINISHED = 1980; //magic number 1019 1020 // only set when wakelock was acquired, no need to check value when received 1021 private static final String EXTRA_WAKELOCK_ACQUIRED = 1022 "android.media.AudioService.WAKELOCK_ACQUIRED"; 1023 1024 public void onSendFinished(PendingIntent pendingIntent, Intent intent, 1025 int resultCode, String resultData, Bundle resultExtras) { 1026 if (resultCode == WAKELOCK_RELEASE_ON_FINISHED) { 1027 mMediaEventWakeLock.release(); 1028 } 1029 } 1030 1031 BroadcastReceiver mKeyEventDone = new BroadcastReceiver() { 1032 public void onReceive(Context context, Intent intent) { 1033 if (intent == null) { 1034 return; 1035 } 1036 Bundle extras = intent.getExtras(); 1037 if (extras == null) { 1038 return; 1039 } 1040 if (extras.containsKey(EXTRA_WAKELOCK_ACQUIRED)) { 1041 mMediaEventWakeLock.release(); 1042 } 1043 } 1044 }; 1045 1046 /** 1047 * Synchronization on mCurrentRcLock always inside a block synchronized on mRCStack 1048 */ 1049 private final Object mCurrentRcLock = new Object(); 1050 /** 1051 * The one remote control client which will receive a request for display information. 1052 * This object may be null. 1053 * Access protected by mCurrentRcLock. 1054 */ 1055 private IRemoteControlClient mCurrentRcClient = null; 1056 /** 1057 * The PendingIntent associated with mCurrentRcClient. Its value is irrelevant 1058 * if mCurrentRcClient is null 1059 */ 1060 private PendingIntent mCurrentRcClientIntent = null; 1061 1062 private final static int RC_INFO_NONE = 0; 1063 private final static int RC_INFO_ALL = 1064 RemoteControlClient.FLAG_INFORMATION_REQUEST_ALBUM_ART | 1065 RemoteControlClient.FLAG_INFORMATION_REQUEST_KEY_MEDIA | 1066 RemoteControlClient.FLAG_INFORMATION_REQUEST_METADATA | 1067 RemoteControlClient.FLAG_INFORMATION_REQUEST_PLAYSTATE; 1068 1069 /** 1070 * A monotonically increasing generation counter for mCurrentRcClient. 1071 * Only accessed with a lock on mCurrentRcLock. 1072 * No value wrap-around issues as we only act on equal values. 1073 */ 1074 private int mCurrentRcClientGen = 0; 1075 1076 /** 1077 * Inner class to monitor remote control client deaths, and remove the client for the 1078 * remote control stack if necessary. 1079 */ 1080 private class RcClientDeathHandler implements IBinder.DeathRecipient { 1081 final private IBinder mCb; // To be notified of client's death 1082 final private PendingIntent mMediaIntent; 1083 1084 RcClientDeathHandler(IBinder cb, PendingIntent pi) { 1085 mCb = cb; 1086 mMediaIntent = pi; 1087 } 1088 1089 public void binderDied() { 1090 Log.w(TAG, " RemoteControlClient died"); 1091 // remote control client died, make sure the displays don't use it anymore 1092 // by setting its remote control client to null 1093 registerRemoteControlClient(mMediaIntent, null/*rcClient*/, null/*ignored*/); 1094 // the dead client was maybe handling remote playback, reevaluate 1095 postReevaluateRemote(); 1096 } 1097 1098 public IBinder getBinder() { 1099 return mCb; 1100 } 1101 } 1102 1103 /** 1104 * A global counter for RemoteControlClient identifiers 1105 */ 1106 private static int sLastRccId = 0; 1107 1108 private class RemotePlaybackState { 1109 int mRccId; 1110 int mVolume; 1111 int mVolumeMax; 1112 int mVolumeHandling; 1113 1114 private RemotePlaybackState(int id, int vol, int volMax) { 1115 mRccId = id; 1116 mVolume = vol; 1117 mVolumeMax = volMax; 1118 mVolumeHandling = RemoteControlClient.DEFAULT_PLAYBACK_VOLUME_HANDLING; 1119 } 1120 } 1121 1122 /** 1123 * Internal cache for the playback information of the RemoteControlClient whose volume gets to 1124 * be controlled by the volume keys ("main"), so we don't have to iterate over the RC stack 1125 * every time we need this info. 1126 */ 1127 private RemotePlaybackState mMainRemote; 1128 /** 1129 * Indicates whether the "main" RemoteControlClient is considered active. 1130 * Use synchronized on mMainRemote. 1131 */ 1132 private boolean mMainRemoteIsActive; 1133 /** 1134 * Indicates whether there is remote playback going on. True even if there is no "active" 1135 * remote playback (mMainRemoteIsActive is false), but a RemoteControlClient has declared it 1136 * handles remote playback. 1137 * Use synchronized on mMainRemote. 1138 */ 1139 private boolean mHasRemotePlayback; 1140 1141 private static class RccPlaybackState { 1142 public int mState; 1143 public long mPositionMs; 1144 public float mSpeed; 1145 1146 public RccPlaybackState(int state, long positionMs, float speed) { 1147 mState = state; 1148 mPositionMs = positionMs; 1149 mSpeed = speed; 1150 } 1151 1152 public void reset() { 1153 mState = RemoteControlClient.PLAYSTATE_STOPPED; 1154 mPositionMs = RemoteControlClient.PLAYBACK_POSITION_INVALID; 1155 mSpeed = RemoteControlClient.PLAYBACK_SPEED_1X; 1156 } 1157 1158 @Override 1159 public String toString() { 1160 return stateToString() + ", " + posToString() + ", " + mSpeed + "X"; 1161 } 1162 1163 private String posToString() { 1164 if (mPositionMs == RemoteControlClient.PLAYBACK_POSITION_INVALID) { 1165 return "PLAYBACK_POSITION_INVALID"; 1166 } else if (mPositionMs == RemoteControlClient.PLAYBACK_POSITION_ALWAYS_UNKNOWN) { 1167 return "PLAYBACK_POSITION_ALWAYS_UNKNOWN"; 1168 } else { 1169 return (String.valueOf(mPositionMs) + "ms"); 1170 } 1171 } 1172 1173 private String stateToString() { 1174 switch (mState) { 1175 case RemoteControlClient.PLAYSTATE_NONE: 1176 return "PLAYSTATE_NONE"; 1177 case RemoteControlClient.PLAYSTATE_STOPPED: 1178 return "PLAYSTATE_STOPPED"; 1179 case RemoteControlClient.PLAYSTATE_PAUSED: 1180 return "PLAYSTATE_PAUSED"; 1181 case RemoteControlClient.PLAYSTATE_PLAYING: 1182 return "PLAYSTATE_PLAYING"; 1183 case RemoteControlClient.PLAYSTATE_FAST_FORWARDING: 1184 return "PLAYSTATE_FAST_FORWARDING"; 1185 case RemoteControlClient.PLAYSTATE_REWINDING: 1186 return "PLAYSTATE_REWINDING"; 1187 case RemoteControlClient.PLAYSTATE_SKIPPING_FORWARDS: 1188 return "PLAYSTATE_SKIPPING_FORWARDS"; 1189 case RemoteControlClient.PLAYSTATE_SKIPPING_BACKWARDS: 1190 return "PLAYSTATE_SKIPPING_BACKWARDS"; 1191 case RemoteControlClient.PLAYSTATE_BUFFERING: 1192 return "PLAYSTATE_BUFFERING"; 1193 case RemoteControlClient.PLAYSTATE_ERROR: 1194 return "PLAYSTATE_ERROR"; 1195 default: 1196 return "[invalid playstate]"; 1197 } 1198 } 1199 } 1200 1201 protected static class RemoteControlStackEntry implements DeathRecipient { 1202 public int mRccId = RemoteControlClient.RCSE_ID_UNREGISTERED; 1203 final public MediaFocusControl mController; 1204 /** 1205 * The target for the ACTION_MEDIA_BUTTON events. 1206 * Always non null. 1207 */ 1208 final public PendingIntent mMediaIntent; 1209 /** 1210 * The registered media button event receiver. 1211 * Always non null. 1212 */ 1213 final public ComponentName mReceiverComponent; 1214 public IBinder mToken; 1215 public String mCallingPackageName; 1216 public int mCallingUid; 1217 /** 1218 * Provides access to the information to display on the remote control. 1219 * May be null (when a media button event receiver is registered, 1220 * but no remote control client has been registered) */ 1221 public IRemoteControlClient mRcClient; 1222 public RcClientDeathHandler mRcClientDeathHandler; 1223 /** 1224 * Information only used for non-local playback 1225 */ 1226 public int mPlaybackType; 1227 public int mPlaybackVolume; 1228 public int mPlaybackVolumeMax; 1229 public int mPlaybackVolumeHandling; 1230 public int mPlaybackStream; 1231 public RccPlaybackState mPlaybackState; 1232 public IRemoteVolumeObserver mRemoteVolumeObs; 1233 1234 public void resetPlaybackInfo() { 1235 mPlaybackType = RemoteControlClient.PLAYBACK_TYPE_LOCAL; 1236 mPlaybackVolume = RemoteControlClient.DEFAULT_PLAYBACK_VOLUME; 1237 mPlaybackVolumeMax = RemoteControlClient.DEFAULT_PLAYBACK_VOLUME; 1238 mPlaybackVolumeHandling = RemoteControlClient.DEFAULT_PLAYBACK_VOLUME_HANDLING; 1239 mPlaybackStream = AudioManager.STREAM_MUSIC; 1240 mPlaybackState.reset(); 1241 mRemoteVolumeObs = null; 1242 } 1243 1244 /** precondition: mediaIntent != null */ 1245 public RemoteControlStackEntry(MediaFocusControl controller, PendingIntent mediaIntent, 1246 ComponentName eventReceiver, IBinder token) { 1247 mController = controller; 1248 mMediaIntent = mediaIntent; 1249 mReceiverComponent = eventReceiver; 1250 mToken = token; 1251 mCallingUid = -1; 1252 mRcClient = null; 1253 mRccId = ++sLastRccId; 1254 mPlaybackState = new RccPlaybackState( 1255 RemoteControlClient.PLAYSTATE_STOPPED, 1256 RemoteControlClient.PLAYBACK_POSITION_INVALID, 1257 RemoteControlClient.PLAYBACK_SPEED_1X); 1258 1259 resetPlaybackInfo(); 1260 if (mToken != null) { 1261 try { 1262 mToken.linkToDeath(this, 0); 1263 } catch (RemoteException e) { 1264 mController.mEventHandler.post(new Runnable() { 1265 @Override public void run() { 1266 mController.unregisterMediaButtonIntent(mMediaIntent); 1267 } 1268 }); 1269 } 1270 } 1271 } 1272 1273 public void unlinkToRcClientDeath() { 1274 if ((mRcClientDeathHandler != null) && (mRcClientDeathHandler.mCb != null)) { 1275 try { 1276 mRcClientDeathHandler.mCb.unlinkToDeath(mRcClientDeathHandler, 0); 1277 mRcClientDeathHandler = null; 1278 } catch (java.util.NoSuchElementException e) { 1279 // not much we can do here 1280 Log.e(TAG, "Encountered " + e + " in unlinkToRcClientDeath()"); 1281 e.printStackTrace(); 1282 } 1283 } 1284 } 1285 1286 public void destroy() { 1287 unlinkToRcClientDeath(); 1288 if (mToken != null) { 1289 mToken.unlinkToDeath(this, 0); 1290 mToken = null; 1291 } 1292 } 1293 1294 @Override 1295 public void binderDied() { 1296 mController.unregisterMediaButtonIntent(mMediaIntent); 1297 } 1298 1299 @Override 1300 protected void finalize() throws Throwable { 1301 destroy(); // unlink exception handled inside method 1302 super.finalize(); 1303 } 1304 } 1305 1306 /** 1307 * The stack of remote control event receivers. 1308 * Code sections and methods that modify the remote control event receiver stack are 1309 * synchronized on mRCStack, but also BEFORE on mFocusLock as any change in either 1310 * stack, audio focus or RC, can lead to a change in the remote control display 1311 */ 1312 private final Stack<RemoteControlStackEntry> mRCStack = new Stack<RemoteControlStackEntry>(); 1313 1314 /** 1315 * The component the telephony package can register so telephony calls have priority to 1316 * handle media button events 1317 */ 1318 private ComponentName mMediaReceiverForCalls = null; 1319 1320 /** 1321 * Helper function: 1322 * Display in the log the current entries in the remote control focus stack 1323 */ 1324 private void dumpRCStack(PrintWriter pw) { 1325 pw.println("\nRemote Control stack entries (last is top of stack):"); 1326 synchronized(mRCStack) { 1327 Iterator<RemoteControlStackEntry> stackIterator = mRCStack.iterator(); 1328 while(stackIterator.hasNext()) { 1329 RemoteControlStackEntry rcse = stackIterator.next(); 1330 pw.println(" pi: " + rcse.mMediaIntent + 1331 " -- pack: " + rcse.mCallingPackageName + 1332 " -- ercvr: " + rcse.mReceiverComponent + 1333 " -- client: " + rcse.mRcClient + 1334 " -- uid: " + rcse.mCallingUid + 1335 " -- type: " + rcse.mPlaybackType + 1336 " state: " + rcse.mPlaybackState); 1337 } 1338 } 1339 } 1340 1341 /** 1342 * Helper function: 1343 * Display in the log the current entries in the remote control stack, focusing 1344 * on RemoteControlClient data 1345 */ 1346 private void dumpRCCStack(PrintWriter pw) { 1347 pw.println("\nRemote Control Client stack entries (last is top of stack):"); 1348 synchronized(mRCStack) { 1349 Iterator<RemoteControlStackEntry> stackIterator = mRCStack.iterator(); 1350 while(stackIterator.hasNext()) { 1351 RemoteControlStackEntry rcse = stackIterator.next(); 1352 pw.println(" uid: " + rcse.mCallingUid + 1353 " -- id: " + rcse.mRccId + 1354 " -- type: " + rcse.mPlaybackType + 1355 " -- state: " + rcse.mPlaybackState + 1356 " -- vol handling: " + rcse.mPlaybackVolumeHandling + 1357 " -- vol: " + rcse.mPlaybackVolume + 1358 " -- volMax: " + rcse.mPlaybackVolumeMax + 1359 " -- volObs: " + rcse.mRemoteVolumeObs); 1360 } 1361 synchronized(mCurrentRcLock) { 1362 pw.println("\nCurrent remote control generation ID = " + mCurrentRcClientGen); 1363 } 1364 } 1365 synchronized (mMainRemote) { 1366 pw.println("\nRemote Volume State:"); 1367 pw.println(" has remote: " + mHasRemotePlayback); 1368 pw.println(" is remote active: " + mMainRemoteIsActive); 1369 pw.println(" rccId: " + mMainRemote.mRccId); 1370 pw.println(" volume handling: " 1371 + ((mMainRemote.mVolumeHandling == RemoteControlClient.PLAYBACK_VOLUME_FIXED) ? 1372 "PLAYBACK_VOLUME_FIXED(0)" : "PLAYBACK_VOLUME_VARIABLE(1)")); 1373 pw.println(" volume: " + mMainRemote.mVolume); 1374 pw.println(" volume steps: " + mMainRemote.mVolumeMax); 1375 } 1376 } 1377 1378 /** 1379 * Helper function: 1380 * Display in the log the current entries in the list of remote control displays 1381 */ 1382 private void dumpRCDList(PrintWriter pw) { 1383 pw.println("\nRemote Control Display list entries:"); 1384 synchronized(mRCStack) { 1385 final Iterator<DisplayInfoForServer> displayIterator = mRcDisplays.iterator(); 1386 while (displayIterator.hasNext()) { 1387 final DisplayInfoForServer di = (DisplayInfoForServer) displayIterator.next(); 1388 pw.println(" IRCD: " + di.mRcDisplay + 1389 " -- w:" + di.mArtworkExpectedWidth + 1390 " -- h:" + di.mArtworkExpectedHeight + 1391 " -- wantsPosSync:" + di.mWantsPositionSync + 1392 " -- " + (di.mEnabled ? "enabled" : "disabled")); 1393 } 1394 } 1395 } 1396 1397 /** 1398 * Helper function: 1399 * Remove any entry in the remote control stack that has the same package name as packageName 1400 * Pre-condition: packageName != null 1401 */ 1402 private void cleanupMediaButtonReceiverForPackage(String packageName, boolean removeAll) { 1403 synchronized(mRCStack) { 1404 if (mRCStack.empty()) { 1405 return; 1406 } else { 1407 final PackageManager pm = mContext.getPackageManager(); 1408 RemoteControlStackEntry oldTop = mRCStack.peek(); 1409 Iterator<RemoteControlStackEntry> stackIterator = mRCStack.iterator(); 1410 // iterate over the stack entries 1411 // (using an iterator on the stack so we can safely remove an entry after having 1412 // evaluated it, traversal order doesn't matter here) 1413 while(stackIterator.hasNext()) { 1414 RemoteControlStackEntry rcse = (RemoteControlStackEntry)stackIterator.next(); 1415 if (removeAll && packageName.equals(rcse.mMediaIntent.getCreatorPackage())) { 1416 // a stack entry is from the package being removed, remove it from the stack 1417 stackIterator.remove(); 1418 rcse.destroy(); 1419 } else if (rcse.mReceiverComponent != null) { 1420 try { 1421 // Check to see if this receiver still exists. 1422 pm.getReceiverInfo(rcse.mReceiverComponent, 0); 1423 } catch (PackageManager.NameNotFoundException e) { 1424 // Not found -- remove it! 1425 stackIterator.remove(); 1426 rcse.destroy(); 1427 } 1428 } 1429 } 1430 if (mRCStack.empty()) { 1431 // no saved media button receiver 1432 mEventHandler.sendMessage( 1433 mEventHandler.obtainMessage(MSG_PERSIST_MEDIABUTTONRECEIVER, 0, 0, 1434 null)); 1435 } else if (oldTop != mRCStack.peek()) { 1436 // the top of the stack has changed, save it in the system settings 1437 // by posting a message to persist it; only do this however if it has 1438 // a concrete component name (is not a transient registration) 1439 RemoteControlStackEntry rcse = mRCStack.peek(); 1440 if (rcse.mReceiverComponent != null) { 1441 mEventHandler.sendMessage( 1442 mEventHandler.obtainMessage(MSG_PERSIST_MEDIABUTTONRECEIVER, 0, 0, 1443 rcse.mReceiverComponent)); 1444 } 1445 } 1446 } 1447 } 1448 } 1449 1450 /** 1451 * Helper function: 1452 * Restore remote control receiver from the system settings. 1453 */ 1454 protected void restoreMediaButtonReceiver() { 1455 String receiverName = Settings.System.getStringForUser(mContentResolver, 1456 Settings.System.MEDIA_BUTTON_RECEIVER, UserHandle.USER_CURRENT); 1457 if ((null != receiverName) && !receiverName.isEmpty()) { 1458 ComponentName eventReceiver = ComponentName.unflattenFromString(receiverName); 1459 if (eventReceiver == null) { 1460 // an invalid name was persisted 1461 return; 1462 } 1463 // construct a PendingIntent targeted to the restored component name 1464 // for the media button and register it 1465 Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON); 1466 // the associated intent will be handled by the component being registered 1467 mediaButtonIntent.setComponent(eventReceiver); 1468 PendingIntent pi = PendingIntent.getBroadcast(mContext, 1469 0/*requestCode, ignored*/, mediaButtonIntent, 0/*flags*/); 1470 registerMediaButtonIntent(pi, eventReceiver, null); 1471 } 1472 } 1473 1474 /** 1475 * Helper function: 1476 * Set the new remote control receiver at the top of the RC focus stack. 1477 * Called synchronized on mAudioFocusLock, then mRCStack 1478 * precondition: mediaIntent != null 1479 */ 1480 private void pushMediaButtonReceiver_syncAfRcs(PendingIntent mediaIntent, ComponentName target, 1481 IBinder token) { 1482 // already at top of stack? 1483 if (!mRCStack.empty() && mRCStack.peek().mMediaIntent.equals(mediaIntent)) { 1484 return; 1485 } 1486 if (mAppOps.noteOp(AppOpsManager.OP_TAKE_MEDIA_BUTTONS, Binder.getCallingUid(), 1487 mediaIntent.getCreatorPackage()) != AppOpsManager.MODE_ALLOWED) { 1488 return; 1489 } 1490 RemoteControlStackEntry rcse = null; 1491 boolean wasInsideStack = false; 1492 try { 1493 for (int index = mRCStack.size()-1; index >= 0; index--) { 1494 rcse = mRCStack.elementAt(index); 1495 if(rcse.mMediaIntent.equals(mediaIntent)) { 1496 // ok to remove element while traversing the stack since we're leaving the loop 1497 mRCStack.removeElementAt(index); 1498 wasInsideStack = true; 1499 break; 1500 } 1501 } 1502 } catch (ArrayIndexOutOfBoundsException e) { 1503 // not expected to happen, indicates improper concurrent modification 1504 Log.e(TAG, "Wrong index accessing media button stack, lock error? ", e); 1505 } 1506 if (!wasInsideStack) { 1507 rcse = new RemoteControlStackEntry(this, mediaIntent, target, token); 1508 } 1509 mRCStack.push(rcse); // rcse is never null 1510 1511 // post message to persist the default media button receiver 1512 if (target != null) { 1513 mEventHandler.sendMessage( mEventHandler.obtainMessage( 1514 MSG_PERSIST_MEDIABUTTONRECEIVER, 0, 0, target/*obj*/) ); 1515 } 1516 } 1517 1518 /** 1519 * Helper function: 1520 * Remove the remote control receiver from the RC focus stack. 1521 * Called synchronized on mAudioFocusLock, then mRCStack 1522 * precondition: pi != null 1523 */ 1524 private void removeMediaButtonReceiver_syncAfRcs(PendingIntent pi) { 1525 try { 1526 for (int index = mRCStack.size()-1; index >= 0; index--) { 1527 final RemoteControlStackEntry rcse = mRCStack.elementAt(index); 1528 if (rcse.mMediaIntent.equals(pi)) { 1529 rcse.destroy(); 1530 // ok to remove element while traversing the stack since we're leaving the loop 1531 mRCStack.removeElementAt(index); 1532 break; 1533 } 1534 } 1535 } catch (ArrayIndexOutOfBoundsException e) { 1536 // not expected to happen, indicates improper concurrent modification 1537 Log.e(TAG, "Wrong index accessing media button stack, lock error? ", e); 1538 } 1539 } 1540 1541 /** 1542 * Helper function: 1543 * Called synchronized on mRCStack 1544 */ 1545 private boolean isCurrentRcController(PendingIntent pi) { 1546 if (!mRCStack.empty() && mRCStack.peek().mMediaIntent.equals(pi)) { 1547 return true; 1548 } 1549 return false; 1550 } 1551 1552 private void onHandlePersistMediaButtonReceiver(ComponentName receiver) { 1553 Settings.System.putStringForUser(mContentResolver, 1554 Settings.System.MEDIA_BUTTON_RECEIVER, 1555 receiver == null ? "" : receiver.flattenToString(), 1556 UserHandle.USER_CURRENT); 1557 } 1558 1559 //========================================================================================== 1560 // Remote control display / client 1561 //========================================================================================== 1562 /** 1563 * Update the remote control displays with the new "focused" client generation 1564 */ 1565 private void setNewRcClientOnDisplays_syncRcsCurrc(int newClientGeneration, 1566 PendingIntent newMediaIntent, boolean clearing) { 1567 synchronized(mRCStack) { 1568 if (mRcDisplays.size() > 0) { 1569 final Iterator<DisplayInfoForServer> displayIterator = mRcDisplays.iterator(); 1570 while (displayIterator.hasNext()) { 1571 final DisplayInfoForServer di = displayIterator.next(); 1572 try { 1573 di.mRcDisplay.setCurrentClientId( 1574 newClientGeneration, newMediaIntent, clearing); 1575 } catch (RemoteException e) { 1576 Log.e(TAG, "Dead display in setNewRcClientOnDisplays_syncRcsCurrc()",e); 1577 di.release(); 1578 displayIterator.remove(); 1579 } 1580 } 1581 } 1582 } 1583 } 1584 1585 /** 1586 * Update the remote control clients with the new "focused" client generation 1587 */ 1588 private void setNewRcClientGenerationOnClients_syncRcsCurrc(int newClientGeneration) { 1589 // (using an iterator on the stack so we can safely remove an entry if needed, 1590 // traversal order doesn't matter here as we update all entries) 1591 Iterator<RemoteControlStackEntry> stackIterator = mRCStack.iterator(); 1592 while(stackIterator.hasNext()) { 1593 RemoteControlStackEntry se = stackIterator.next(); 1594 if ((se != null) && (se.mRcClient != null)) { 1595 try { 1596 se.mRcClient.setCurrentClientGenerationId(newClientGeneration); 1597 } catch (RemoteException e) { 1598 Log.w(TAG, "Dead client in setNewRcClientGenerationOnClients_syncRcsCurrc()",e); 1599 stackIterator.remove(); 1600 se.unlinkToRcClientDeath(); 1601 } 1602 } 1603 } 1604 } 1605 1606 /** 1607 * Update the displays and clients with the new "focused" client generation and name 1608 * @param newClientGeneration the new generation value matching a client update 1609 * @param newMediaIntent the media button event receiver associated with the client. 1610 * May be null, which implies there is no registered media button event receiver. 1611 * @param clearing true if the new client generation value maps to a remote control update 1612 * where the display should be cleared. 1613 */ 1614 private void setNewRcClient_syncRcsCurrc(int newClientGeneration, 1615 PendingIntent newMediaIntent, boolean clearing) { 1616 // send the new valid client generation ID to all displays 1617 setNewRcClientOnDisplays_syncRcsCurrc(newClientGeneration, newMediaIntent, clearing); 1618 // send the new valid client generation ID to all clients 1619 setNewRcClientGenerationOnClients_syncRcsCurrc(newClientGeneration); 1620 } 1621 1622 /** 1623 * Called when processing MSG_RCDISPLAY_CLEAR event 1624 */ 1625 private void onRcDisplayClear() { 1626 if (DEBUG_RC) Log.i(TAG, "Clear remote control display"); 1627 1628 synchronized(mRCStack) { 1629 synchronized(mCurrentRcLock) { 1630 mCurrentRcClientGen++; 1631 // synchronously update the displays and clients with the new client generation 1632 setNewRcClient_syncRcsCurrc(mCurrentRcClientGen, 1633 null /*newMediaIntent*/, true /*clearing*/); 1634 } 1635 } 1636 } 1637 1638 /** 1639 * Called when processing MSG_RCDISPLAY_UPDATE event 1640 */ 1641 private void onRcDisplayUpdate(RemoteControlStackEntry rcse, int flags /* USED ?*/) { 1642 synchronized(mRCStack) { 1643 synchronized(mCurrentRcLock) { 1644 if ((mCurrentRcClient != null) && (mCurrentRcClient.equals(rcse.mRcClient))) { 1645 if (DEBUG_RC) Log.i(TAG, "Display/update remote control "); 1646 1647 mCurrentRcClientGen++; 1648 // synchronously update the displays and clients with 1649 // the new client generation 1650 setNewRcClient_syncRcsCurrc(mCurrentRcClientGen, 1651 rcse.mMediaIntent /*newMediaIntent*/, 1652 false /*clearing*/); 1653 1654 // tell the current client that it needs to send info 1655 try { 1656 //TODO change name to informationRequestForAllDisplays() 1657 mCurrentRcClient.onInformationRequested(mCurrentRcClientGen, flags); 1658 } catch (RemoteException e) { 1659 Log.e(TAG, "Current valid remote client is dead: "+e); 1660 mCurrentRcClient = null; 1661 } 1662 } else { 1663 // the remote control display owner has changed between the 1664 // the message to update the display was sent, and the time it 1665 // gets to be processed (now) 1666 } 1667 } 1668 } 1669 } 1670 1671 /** 1672 * Called when processing MSG_RCDISPLAY_INIT_INFO event 1673 * Causes the current RemoteControlClient to send its info (metadata, playstate...) to 1674 * a single RemoteControlDisplay, NOT all of them, as with MSG_RCDISPLAY_UPDATE. 1675 */ 1676 private void onRcDisplayInitInfo(IRemoteControlDisplay newRcd, int w, int h) { 1677 synchronized(mRCStack) { 1678 synchronized(mCurrentRcLock) { 1679 if (mCurrentRcClient != null) { 1680 if (DEBUG_RC) { Log.i(TAG, "Init RCD with current info"); } 1681 try { 1682 // synchronously update the new RCD with the current client generation 1683 // and matching PendingIntent 1684 newRcd.setCurrentClientId(mCurrentRcClientGen, mCurrentRcClientIntent, 1685 false); 1686 1687 // tell the current RCC that it needs to send info, but only to the new RCD 1688 try { 1689 mCurrentRcClient.informationRequestForDisplay(newRcd, w, h); 1690 } catch (RemoteException e) { 1691 Log.e(TAG, "Current valid remote client is dead: ", e); 1692 mCurrentRcClient = null; 1693 } 1694 } catch (RemoteException e) { 1695 Log.e(TAG, "Dead display in onRcDisplayInitInfo()", e); 1696 } 1697 } 1698 } 1699 } 1700 } 1701 1702 /** 1703 * Helper function: 1704 * Called synchronized on mRCStack 1705 */ 1706 private void clearRemoteControlDisplay_syncAfRcs() { 1707 synchronized(mCurrentRcLock) { 1708 mCurrentRcClient = null; 1709 } 1710 // will cause onRcDisplayClear() to be called in AudioService's handler thread 1711 mEventHandler.sendMessage( mEventHandler.obtainMessage(MSG_RCDISPLAY_CLEAR) ); 1712 } 1713 1714 /** 1715 * Helper function for code readability: only to be called from 1716 * checkUpdateRemoteControlDisplay_syncAfRcs() which checks the preconditions for 1717 * this method. 1718 * Preconditions: 1719 * - called synchronized mAudioFocusLock then on mRCStack 1720 * - mRCStack.isEmpty() is false 1721 */ 1722 private void updateRemoteControlDisplay_syncAfRcs(int infoChangedFlags) { 1723 RemoteControlStackEntry rcse = mRCStack.peek(); 1724 int infoFlagsAboutToBeUsed = infoChangedFlags; 1725 // this is where we enforce opt-in for information display on the remote controls 1726 // with the new AudioManager.registerRemoteControlClient() API 1727 if (rcse.mRcClient == null) { 1728 //Log.w(TAG, "Can't update remote control display with null remote control client"); 1729 clearRemoteControlDisplay_syncAfRcs(); 1730 return; 1731 } 1732 synchronized(mCurrentRcLock) { 1733 if (!rcse.mRcClient.equals(mCurrentRcClient)) { 1734 // new RC client, assume every type of information shall be queried 1735 infoFlagsAboutToBeUsed = RC_INFO_ALL; 1736 } 1737 mCurrentRcClient = rcse.mRcClient; 1738 mCurrentRcClientIntent = rcse.mMediaIntent; 1739 } 1740 // will cause onRcDisplayUpdate() to be called in AudioService's handler thread 1741 mEventHandler.sendMessage( mEventHandler.obtainMessage(MSG_RCDISPLAY_UPDATE, 1742 infoFlagsAboutToBeUsed /* arg1 */, 0, rcse /* obj, != null */) ); 1743 } 1744 1745 /** 1746 * Helper function: 1747 * Called synchronized on mAudioFocusLock, then mRCStack 1748 * Check whether the remote control display should be updated, triggers the update if required 1749 * @param infoChangedFlags the flags corresponding to the remote control client information 1750 * that has changed, if applicable (checking for the update conditions might trigger a 1751 * clear, rather than an update event). 1752 */ 1753 private void checkUpdateRemoteControlDisplay_syncAfRcs(int infoChangedFlags) { 1754 // determine whether the remote control display should be refreshed 1755 // if either stack is empty, there is a mismatch, so clear the RC display 1756 if (mRCStack.isEmpty() || mFocusStack.isEmpty()) { 1757 clearRemoteControlDisplay_syncAfRcs(); 1758 return; 1759 } 1760 1761 // determine which entry in the AudioFocus stack to consider, and compare against the 1762 // top of the stack for the media button event receivers : simply using the top of the 1763 // stack would make the entry disappear from the RemoteControlDisplay in conditions such as 1764 // notifications playing during music playback. 1765 // Crawl the AudioFocus stack from the top until an entry is found with the following 1766 // characteristics: 1767 // - focus gain on STREAM_MUSIC stream 1768 // - non-transient focus gain on a stream other than music 1769 FocusRequester af = null; 1770 try { 1771 for (int index = mFocusStack.size()-1; index >= 0; index--) { 1772 FocusRequester fr = mFocusStack.elementAt(index); 1773 if ((fr.getStreamType() == AudioManager.STREAM_MUSIC) 1774 || (fr.getGainRequest() == AudioManager.AUDIOFOCUS_GAIN)) { 1775 af = fr; 1776 break; 1777 } 1778 } 1779 } catch (ArrayIndexOutOfBoundsException e) { 1780 Log.e(TAG, "Wrong index accessing audio focus stack when updating RCD: " + e); 1781 af = null; 1782 } 1783 if (af == null) { 1784 clearRemoteControlDisplay_syncAfRcs(); 1785 return; 1786 } 1787 1788 // if the audio focus and RC owners belong to different packages, there is a mismatch, clear 1789 if (!af.hasSamePackage(mRCStack.peek().mCallingPackageName)) { 1790 clearRemoteControlDisplay_syncAfRcs(); 1791 return; 1792 } 1793 // if the audio focus didn't originate from the same Uid as the one in which the remote 1794 // control information will be retrieved, clear 1795 if (!af.hasSameUid(mRCStack.peek().mCallingUid)) { 1796 clearRemoteControlDisplay_syncAfRcs(); 1797 return; 1798 } 1799 1800 // refresh conditions were verified: update the remote controls 1801 // ok to call: synchronized mAudioFocusLock then on mRCStack, mRCStack is not empty 1802 updateRemoteControlDisplay_syncAfRcs(infoChangedFlags); 1803 } 1804 1805 /** 1806 * Helper function: 1807 * Post a message to asynchronously move the media button event receiver associated with the 1808 * given remote control client ID to the top of the remote control stack 1809 * @param rccId 1810 */ 1811 private void postPromoteRcc(int rccId) { 1812 sendMsg(mEventHandler, MSG_PROMOTE_RCC, SENDMSG_REPLACE, 1813 rccId /*arg1*/, 0, null, 0/*delay*/); 1814 } 1815 1816 private void onPromoteRcc(int rccId) { 1817 if (DEBUG_RC) { Log.d(TAG, "Promoting RCC " + rccId); } 1818 synchronized(mAudioFocusLock) { 1819 synchronized(mRCStack) { 1820 // ignore if given RCC ID is already at top of remote control stack 1821 if (!mRCStack.isEmpty() && (mRCStack.peek().mRccId == rccId)) { 1822 return; 1823 } 1824 int indexToPromote = -1; 1825 try { 1826 for (int index = mRCStack.size()-1; index >= 0; index--) { 1827 final RemoteControlStackEntry rcse = mRCStack.elementAt(index); 1828 if (rcse.mRccId == rccId) { 1829 indexToPromote = index; 1830 break; 1831 } 1832 } 1833 if (indexToPromote >= 0) { 1834 if (DEBUG_RC) { Log.d(TAG, " moving RCC from index " + indexToPromote 1835 + " to " + (mRCStack.size()-1)); } 1836 final RemoteControlStackEntry rcse = mRCStack.remove(indexToPromote); 1837 mRCStack.push(rcse); 1838 // the RC stack changed, reevaluate the display 1839 checkUpdateRemoteControlDisplay_syncAfRcs(RC_INFO_ALL); 1840 } 1841 } catch (ArrayIndexOutOfBoundsException e) { 1842 // not expected to happen, indicates improper concurrent modification 1843 Log.e(TAG, "Wrong index accessing RC stack, lock error? ", e); 1844 } 1845 }//synchronized(mRCStack) 1846 }//synchronized(mAudioFocusLock) 1847 } 1848 1849 /** 1850 * see AudioManager.registerMediaButtonIntent(PendingIntent pi, ComponentName c) 1851 * precondition: mediaIntent != null 1852 */ 1853 protected void registerMediaButtonIntent(PendingIntent mediaIntent, ComponentName eventReceiver, 1854 IBinder token) { 1855 Log.i(TAG, " Remote Control registerMediaButtonIntent() for " + mediaIntent); 1856 1857 synchronized(mAudioFocusLock) { 1858 synchronized(mRCStack) { 1859 pushMediaButtonReceiver_syncAfRcs(mediaIntent, eventReceiver, token); 1860 // new RC client, assume every type of information shall be queried 1861 checkUpdateRemoteControlDisplay_syncAfRcs(RC_INFO_ALL); 1862 } 1863 } 1864 } 1865 1866 /** 1867 * see AudioManager.unregisterMediaButtonIntent(PendingIntent mediaIntent) 1868 * precondition: mediaIntent != null, eventReceiver != null 1869 */ 1870 protected void unregisterMediaButtonIntent(PendingIntent mediaIntent) 1871 { 1872 Log.i(TAG, " Remote Control unregisterMediaButtonIntent() for " + mediaIntent); 1873 1874 synchronized(mAudioFocusLock) { 1875 synchronized(mRCStack) { 1876 boolean topOfStackWillChange = isCurrentRcController(mediaIntent); 1877 removeMediaButtonReceiver_syncAfRcs(mediaIntent); 1878 if (topOfStackWillChange) { 1879 // current RC client will change, assume every type of info needs to be queried 1880 checkUpdateRemoteControlDisplay_syncAfRcs(RC_INFO_ALL); 1881 } 1882 } 1883 } 1884 } 1885 1886 /** 1887 * see AudioManager.registerMediaButtonEventReceiverForCalls(ComponentName c) 1888 * precondition: c != null 1889 */ 1890 protected void registerMediaButtonEventReceiverForCalls(ComponentName c) { 1891 if (mContext.checkCallingPermission("android.permission.MODIFY_PHONE_STATE") 1892 != PackageManager.PERMISSION_GRANTED) { 1893 Log.e(TAG, "Invalid permissions to register media button receiver for calls"); 1894 return; 1895 } 1896 synchronized(mRCStack) { 1897 mMediaReceiverForCalls = c; 1898 } 1899 } 1900 1901 /** 1902 * see AudioManager.unregisterMediaButtonEventReceiverForCalls() 1903 */ 1904 protected void unregisterMediaButtonEventReceiverForCalls() { 1905 if (mContext.checkCallingPermission("android.permission.MODIFY_PHONE_STATE") 1906 != PackageManager.PERMISSION_GRANTED) { 1907 Log.e(TAG, "Invalid permissions to unregister media button receiver for calls"); 1908 return; 1909 } 1910 synchronized(mRCStack) { 1911 mMediaReceiverForCalls = null; 1912 } 1913 } 1914 1915 /** 1916 * see AudioManager.registerRemoteControlClient(ComponentName eventReceiver, ...) 1917 * @return the unique ID of the RemoteControlStackEntry associated with the RemoteControlClient 1918 * Note: using this method with rcClient == null is a way to "disable" the IRemoteControlClient 1919 * without modifying the RC stack, but while still causing the display to refresh (will 1920 * become blank as a result of this) 1921 */ 1922 protected int registerRemoteControlClient(PendingIntent mediaIntent, 1923 IRemoteControlClient rcClient, String callingPackageName) { 1924 if (DEBUG_RC) Log.i(TAG, "Register remote control client rcClient="+rcClient); 1925 int rccId = RemoteControlClient.RCSE_ID_UNREGISTERED; 1926 synchronized(mAudioFocusLock) { 1927 synchronized(mRCStack) { 1928 // store the new display information 1929 try { 1930 for (int index = mRCStack.size()-1; index >= 0; index--) { 1931 final RemoteControlStackEntry rcse = mRCStack.elementAt(index); 1932 if(rcse.mMediaIntent.equals(mediaIntent)) { 1933 // already had a remote control client? 1934 if (rcse.mRcClientDeathHandler != null) { 1935 // stop monitoring the old client's death 1936 rcse.unlinkToRcClientDeath(); 1937 } 1938 // save the new remote control client 1939 rcse.mRcClient = rcClient; 1940 rcse.mCallingPackageName = callingPackageName; 1941 rcse.mCallingUid = Binder.getCallingUid(); 1942 if (rcClient == null) { 1943 // here rcse.mRcClientDeathHandler is null; 1944 rcse.resetPlaybackInfo(); 1945 break; 1946 } 1947 rccId = rcse.mRccId; 1948 1949 // there is a new (non-null) client: 1950 // 1/ give the new client the displays (if any) 1951 if (mRcDisplays.size() > 0) { 1952 plugRemoteControlDisplaysIntoClient_syncRcStack(rcse.mRcClient); 1953 } 1954 // 2/ monitor the new client's death 1955 IBinder b = rcse.mRcClient.asBinder(); 1956 RcClientDeathHandler rcdh = 1957 new RcClientDeathHandler(b, rcse.mMediaIntent); 1958 try { 1959 b.linkToDeath(rcdh, 0); 1960 } catch (RemoteException e) { 1961 // remote control client is DOA, disqualify it 1962 Log.w(TAG, "registerRemoteControlClient() has a dead client " + b); 1963 rcse.mRcClient = null; 1964 } 1965 rcse.mRcClientDeathHandler = rcdh; 1966 break; 1967 } 1968 }//for 1969 } catch (ArrayIndexOutOfBoundsException e) { 1970 // not expected to happen, indicates improper concurrent modification 1971 Log.e(TAG, "Wrong index accessing RC stack, lock error? ", e); 1972 } 1973 1974 // if the eventReceiver is at the top of the stack 1975 // then check for potential refresh of the remote controls 1976 if (isCurrentRcController(mediaIntent)) { 1977 checkUpdateRemoteControlDisplay_syncAfRcs(RC_INFO_ALL); 1978 } 1979 }//synchronized(mRCStack) 1980 }//synchronized(mAudioFocusLock) 1981 return rccId; 1982 } 1983 1984 /** 1985 * see AudioManager.unregisterRemoteControlClient(PendingIntent pi, ...) 1986 * rcClient is guaranteed non-null 1987 */ 1988 protected void unregisterRemoteControlClient(PendingIntent mediaIntent, 1989 IRemoteControlClient rcClient) { 1990 if (DEBUG_RC) Log.i(TAG, "Unregister remote control client rcClient="+rcClient); 1991 synchronized(mAudioFocusLock) { 1992 synchronized(mRCStack) { 1993 boolean topRccChange = false; 1994 try { 1995 for (int index = mRCStack.size()-1; index >= 0; index--) { 1996 final RemoteControlStackEntry rcse = mRCStack.elementAt(index); 1997 if ((rcse.mMediaIntent.equals(mediaIntent)) 1998 && rcClient.equals(rcse.mRcClient)) { 1999 // we found the IRemoteControlClient to unregister 2000 // stop monitoring its death 2001 rcse.unlinkToRcClientDeath(); 2002 // reset the client-related fields 2003 rcse.mRcClient = null; 2004 rcse.mCallingPackageName = null; 2005 topRccChange = (index == mRCStack.size()-1); 2006 // there can only be one matching RCC in the RC stack, we're done 2007 break; 2008 } 2009 } 2010 } catch (ArrayIndexOutOfBoundsException e) { 2011 // not expected to happen, indicates improper concurrent modification 2012 Log.e(TAG, "Wrong index accessing RC stack, lock error? ", e); 2013 } 2014 if (topRccChange) { 2015 // no more RCC for the RCD, check for potential refresh of the remote controls 2016 checkUpdateRemoteControlDisplay_syncAfRcs(RC_INFO_ALL); 2017 } 2018 } 2019 } 2020 } 2021 2022 2023 /** 2024 * A class to encapsulate all the information about a remote control display. 2025 * After instanciation, init() must always be called before the object is added in the list 2026 * of displays. 2027 * Before being removed from the list of displays, release() must always be called (otherwise 2028 * it will leak death handlers). 2029 */ 2030 private class DisplayInfoForServer implements IBinder.DeathRecipient { 2031 /** may never be null */ 2032 private final IRemoteControlDisplay mRcDisplay; 2033 private final IBinder mRcDisplayBinder; 2034 private int mArtworkExpectedWidth = -1; 2035 private int mArtworkExpectedHeight = -1; 2036 private boolean mWantsPositionSync = false; 2037 private ComponentName mClientNotifListComp; 2038 private boolean mEnabled = true; 2039 2040 public DisplayInfoForServer(IRemoteControlDisplay rcd, int w, int h) { 2041 if (DEBUG_RC) Log.i(TAG, "new DisplayInfoForServer for " + rcd + " w=" + w + " h=" + h); 2042 mRcDisplay = rcd; 2043 mRcDisplayBinder = rcd.asBinder(); 2044 mArtworkExpectedWidth = w; 2045 mArtworkExpectedHeight = h; 2046 } 2047 2048 public boolean init() { 2049 try { 2050 mRcDisplayBinder.linkToDeath(this, 0); 2051 } catch (RemoteException e) { 2052 // remote control display is DOA, disqualify it 2053 Log.w(TAG, "registerRemoteControlDisplay() has a dead client " + mRcDisplayBinder); 2054 return false; 2055 } 2056 return true; 2057 } 2058 2059 public void release() { 2060 try { 2061 mRcDisplayBinder.unlinkToDeath(this, 0); 2062 } catch (java.util.NoSuchElementException e) { 2063 // not much we can do here, the display should have been unregistered anyway 2064 Log.e(TAG, "Error in DisplaInfoForServer.relase()", e); 2065 } 2066 } 2067 2068 public void binderDied() { 2069 synchronized(mRCStack) { 2070 Log.w(TAG, "RemoteControl: display " + mRcDisplay + " died"); 2071 // remove the display from the list 2072 final Iterator<DisplayInfoForServer> displayIterator = mRcDisplays.iterator(); 2073 while (displayIterator.hasNext()) { 2074 final DisplayInfoForServer di = (DisplayInfoForServer) displayIterator.next(); 2075 if (di.mRcDisplay == mRcDisplay) { 2076 if (DEBUG_RC) Log.w(TAG, " RCD removed from list"); 2077 displayIterator.remove(); 2078 return; 2079 } 2080 } 2081 } 2082 } 2083 } 2084 2085 /** 2086 * The remote control displays. 2087 * Access synchronized on mRCStack 2088 */ 2089 private ArrayList<DisplayInfoForServer> mRcDisplays = new ArrayList<DisplayInfoForServer>(1); 2090 2091 /** 2092 * Plug each registered display into the specified client 2093 * @param rcc, guaranteed non null 2094 */ 2095 private void plugRemoteControlDisplaysIntoClient_syncRcStack(IRemoteControlClient rcc) { 2096 final Iterator<DisplayInfoForServer> displayIterator = mRcDisplays.iterator(); 2097 while (displayIterator.hasNext()) { 2098 final DisplayInfoForServer di = (DisplayInfoForServer) displayIterator.next(); 2099 try { 2100 rcc.plugRemoteControlDisplay(di.mRcDisplay, di.mArtworkExpectedWidth, 2101 di.mArtworkExpectedHeight); 2102 if (di.mWantsPositionSync) { 2103 rcc.setWantsSyncForDisplay(di.mRcDisplay, true); 2104 } 2105 } catch (RemoteException e) { 2106 Log.e(TAG, "Error connecting RCD to RCC in RCC registration",e); 2107 } 2108 } 2109 } 2110 2111 private void enableRemoteControlDisplayForClient_syncRcStack(IRemoteControlDisplay rcd, 2112 boolean enabled) { 2113 // let all the remote control clients know whether the given display is enabled 2114 // (so the remote control stack traversal order doesn't matter). 2115 final Iterator<RemoteControlStackEntry> stackIterator = mRCStack.iterator(); 2116 while(stackIterator.hasNext()) { 2117 RemoteControlStackEntry rcse = stackIterator.next(); 2118 if(rcse.mRcClient != null) { 2119 try { 2120 rcse.mRcClient.enableRemoteControlDisplay(rcd, enabled); 2121 } catch (RemoteException e) { 2122 Log.e(TAG, "Error connecting RCD to client: ", e); 2123 } 2124 } 2125 } 2126 } 2127 2128 /** 2129 * Is the remote control display interface already registered 2130 * @param rcd 2131 * @return true if the IRemoteControlDisplay is already in the list of displays 2132 */ 2133 private boolean rcDisplayIsPluggedIn_syncRcStack(IRemoteControlDisplay rcd) { 2134 final Iterator<DisplayInfoForServer> displayIterator = mRcDisplays.iterator(); 2135 while (displayIterator.hasNext()) { 2136 final DisplayInfoForServer di = (DisplayInfoForServer) displayIterator.next(); 2137 if (di.mRcDisplay.asBinder().equals(rcd.asBinder())) { 2138 return true; 2139 } 2140 } 2141 return false; 2142 } 2143 2144 /** 2145 * Register an IRemoteControlDisplay. 2146 * Notify all IRemoteControlClient of the new display and cause the RemoteControlClient 2147 * at the top of the stack to update the new display with its information. 2148 * @see android.media.IAudioService#registerRemoteControlDisplay(android.media.IRemoteControlDisplay, int, int) 2149 * @param rcd the IRemoteControlDisplay to register. No effect if null. 2150 * @param w the maximum width of the expected bitmap. Negative or zero values indicate this 2151 * display doesn't need to receive artwork. 2152 * @param h the maximum height of the expected bitmap. Negative or zero values indicate this 2153 * display doesn't need to receive artwork. 2154 * @param listenerComp the component for the listener interface, may be null if it's not needed 2155 * to verify it belongs to one of the enabled notification listeners 2156 */ 2157 private void registerRemoteControlDisplay_int(IRemoteControlDisplay rcd, int w, int h, 2158 ComponentName listenerComp) { 2159 if (DEBUG_RC) Log.d(TAG, ">>> registerRemoteControlDisplay("+rcd+")"); 2160 synchronized(mAudioFocusLock) { 2161 synchronized(mRCStack) { 2162 if ((rcd == null) || rcDisplayIsPluggedIn_syncRcStack(rcd)) { 2163 return; 2164 } 2165 DisplayInfoForServer di = new DisplayInfoForServer(rcd, w, h); 2166 di.mEnabled = true; 2167 di.mClientNotifListComp = listenerComp; 2168 if (!di.init()) { 2169 if (DEBUG_RC) Log.e(TAG, " error registering RCD"); 2170 return; 2171 } 2172 // add RCD to list of displays 2173 mRcDisplays.add(di); 2174 2175 // let all the remote control clients know there is a new display (so the remote 2176 // control stack traversal order doesn't matter). 2177 Iterator<RemoteControlStackEntry> stackIterator = mRCStack.iterator(); 2178 while(stackIterator.hasNext()) { 2179 RemoteControlStackEntry rcse = stackIterator.next(); 2180 if(rcse.mRcClient != null) { 2181 try { 2182 rcse.mRcClient.plugRemoteControlDisplay(rcd, w, h); 2183 } catch (RemoteException e) { 2184 Log.e(TAG, "Error connecting RCD to client: ", e); 2185 } 2186 } 2187 } 2188 2189 // we have a new display, of which all the clients are now aware: have it be 2190 // initialized wih the current gen ID and the current client info, do not 2191 // reset the information for the other (existing) displays 2192 sendMsg(mEventHandler, MSG_RCDISPLAY_INIT_INFO, SENDMSG_QUEUE, 2193 w /*arg1*/, h /*arg2*/, 2194 rcd /*obj*/, 0/*delay*/); 2195 } 2196 } 2197 } 2198 2199 /** 2200 * Unregister an IRemoteControlDisplay. 2201 * No effect if the IRemoteControlDisplay hasn't been successfully registered. 2202 * @see android.media.IAudioService#unregisterRemoteControlDisplay(android.media.IRemoteControlDisplay) 2203 * @param rcd the IRemoteControlDisplay to unregister. No effect if null. 2204 */ 2205 protected void unregisterRemoteControlDisplay(IRemoteControlDisplay rcd) { 2206 if (DEBUG_RC) Log.d(TAG, "<<< unregisterRemoteControlDisplay("+rcd+")"); 2207 synchronized(mRCStack) { 2208 if (rcd == null) { 2209 return; 2210 } 2211 2212 boolean displayWasPluggedIn = false; 2213 final Iterator<DisplayInfoForServer> displayIterator = mRcDisplays.iterator(); 2214 while (displayIterator.hasNext() && !displayWasPluggedIn) { 2215 final DisplayInfoForServer di = (DisplayInfoForServer) displayIterator.next(); 2216 if (di.mRcDisplay.asBinder().equals(rcd.asBinder())) { 2217 displayWasPluggedIn = true; 2218 di.release(); 2219 displayIterator.remove(); 2220 } 2221 } 2222 2223 if (displayWasPluggedIn) { 2224 // disconnect this remote control display from all the clients, so the remote 2225 // control stack traversal order doesn't matter 2226 final Iterator<RemoteControlStackEntry> stackIterator = mRCStack.iterator(); 2227 while(stackIterator.hasNext()) { 2228 final RemoteControlStackEntry rcse = stackIterator.next(); 2229 if(rcse.mRcClient != null) { 2230 try { 2231 rcse.mRcClient.unplugRemoteControlDisplay(rcd); 2232 } catch (RemoteException e) { 2233 Log.e(TAG, "Error disconnecting remote control display to client: ", e); 2234 } 2235 } 2236 } 2237 } else { 2238 if (DEBUG_RC) Log.w(TAG, " trying to unregister unregistered RCD"); 2239 } 2240 } 2241 } 2242 2243 /** 2244 * Update the size of the artwork used by an IRemoteControlDisplay. 2245 * @see android.media.IAudioService#remoteControlDisplayUsesBitmapSize(android.media.IRemoteControlDisplay, int, int) 2246 * @param rcd the IRemoteControlDisplay with the new artwork size requirement 2247 * @param w the maximum width of the expected bitmap. Negative or zero values indicate this 2248 * display doesn't need to receive artwork. 2249 * @param h the maximum height of the expected bitmap. Negative or zero values indicate this 2250 * display doesn't need to receive artwork. 2251 */ 2252 protected void remoteControlDisplayUsesBitmapSize(IRemoteControlDisplay rcd, int w, int h) { 2253 synchronized(mRCStack) { 2254 final Iterator<DisplayInfoForServer> displayIterator = mRcDisplays.iterator(); 2255 boolean artworkSizeUpdate = false; 2256 while (displayIterator.hasNext() && !artworkSizeUpdate) { 2257 final DisplayInfoForServer di = (DisplayInfoForServer) displayIterator.next(); 2258 if (di.mRcDisplay.asBinder().equals(rcd.asBinder())) { 2259 if ((di.mArtworkExpectedWidth != w) || (di.mArtworkExpectedHeight != h)) { 2260 di.mArtworkExpectedWidth = w; 2261 di.mArtworkExpectedHeight = h; 2262 artworkSizeUpdate = true; 2263 } 2264 } 2265 } 2266 if (artworkSizeUpdate) { 2267 // RCD is currently plugged in and its artwork size has changed, notify all RCCs, 2268 // stack traversal order doesn't matter 2269 final Iterator<RemoteControlStackEntry> stackIterator = mRCStack.iterator(); 2270 while(stackIterator.hasNext()) { 2271 final RemoteControlStackEntry rcse = stackIterator.next(); 2272 if(rcse.mRcClient != null) { 2273 try { 2274 rcse.mRcClient.setBitmapSizeForDisplay(rcd, w, h); 2275 } catch (RemoteException e) { 2276 Log.e(TAG, "Error setting bitmap size for RCD on RCC: ", e); 2277 } 2278 } 2279 } 2280 } 2281 } 2282 } 2283 2284 /** 2285 * Controls whether a remote control display needs periodic checks of the RemoteControlClient 2286 * playback position to verify that the estimated position has not drifted from the actual 2287 * position. By default the check is not performed. 2288 * The IRemoteControlDisplay must have been previously registered for this to have any effect. 2289 * @param rcd the IRemoteControlDisplay for which the anti-drift mechanism will be enabled 2290 * or disabled. Not null. 2291 * @param wantsSync if true, RemoteControlClient instances which expose their playback position 2292 * to the framework will regularly compare the estimated playback position with the actual 2293 * position, and will update the IRemoteControlDisplay implementation whenever a drift is 2294 * detected. 2295 */ 2296 protected void remoteControlDisplayWantsPlaybackPositionSync(IRemoteControlDisplay rcd, 2297 boolean wantsSync) { 2298 synchronized(mRCStack) { 2299 boolean rcdRegistered = false; 2300 // store the information about this display 2301 // (display stack traversal order doesn't matter). 2302 final Iterator<DisplayInfoForServer> displayIterator = mRcDisplays.iterator(); 2303 while (displayIterator.hasNext()) { 2304 final DisplayInfoForServer di = (DisplayInfoForServer) displayIterator.next(); 2305 if (di.mRcDisplay.asBinder().equals(rcd.asBinder())) { 2306 di.mWantsPositionSync = wantsSync; 2307 rcdRegistered = true; 2308 break; 2309 } 2310 } 2311 if (!rcdRegistered) { 2312 return; 2313 } 2314 // notify all current RemoteControlClients 2315 // (stack traversal order doesn't matter as we notify all RCCs) 2316 final Iterator<RemoteControlStackEntry> stackIterator = mRCStack.iterator(); 2317 while (stackIterator.hasNext()) { 2318 final RemoteControlStackEntry rcse = stackIterator.next(); 2319 if (rcse.mRcClient != null) { 2320 try { 2321 rcse.mRcClient.setWantsSyncForDisplay(rcd, wantsSync); 2322 } catch (RemoteException e) { 2323 Log.e(TAG, "Error setting position sync flag for RCD on RCC: ", e); 2324 } 2325 } 2326 } 2327 } 2328 } 2329 2330 protected void setRemoteControlClientPlaybackPosition(int generationId, long timeMs) { 2331 // ignore position change requests if invalid generation ID 2332 synchronized(mRCStack) { 2333 synchronized(mCurrentRcLock) { 2334 if (mCurrentRcClientGen != generationId) { 2335 return; 2336 } 2337 } 2338 } 2339 // discard any unprocessed seek request in the message queue, and replace with latest 2340 sendMsg(mEventHandler, MSG_RCC_SEEK_REQUEST, SENDMSG_REPLACE, generationId /* arg1 */, 2341 0 /* arg2 ignored*/, new Long(timeMs) /* obj */, 0 /* delay */); 2342 } 2343 2344 private void onSetRemoteControlClientPlaybackPosition(int generationId, long timeMs) { 2345 if(DEBUG_RC) Log.d(TAG, "onSetRemoteControlClientPlaybackPosition(genId=" + generationId + 2346 ", timeMs=" + timeMs + ")"); 2347 synchronized(mRCStack) { 2348 synchronized(mCurrentRcLock) { 2349 if ((mCurrentRcClient != null) && (mCurrentRcClientGen == generationId)) { 2350 // tell the current client to seek to the requested location 2351 try { 2352 mCurrentRcClient.seekTo(generationId, timeMs); 2353 } catch (RemoteException e) { 2354 Log.e(TAG, "Current valid remote client is dead: "+e); 2355 mCurrentRcClient = null; 2356 } 2357 } 2358 } 2359 } 2360 } 2361 2362 protected void updateRemoteControlClientMetadata(int genId, int key, Rating value) { 2363 sendMsg(mEventHandler, MSG_RCC_UPDATE_METADATA, SENDMSG_QUEUE, 2364 genId /* arg1 */, key /* arg2 */, value /* obj */, 0 /* delay */); 2365 } 2366 2367 private void onUpdateRemoteControlClientMetadata(int genId, int key, Rating value) { 2368 if(DEBUG_RC) Log.d(TAG, "onUpdateRemoteControlClientMetadata(genId=" + genId + 2369 ", what=" + key + ",rating=" + value + ")"); 2370 synchronized(mRCStack) { 2371 synchronized(mCurrentRcLock) { 2372 if ((mCurrentRcClient != null) && (mCurrentRcClientGen == genId)) { 2373 try { 2374 switch (key) { 2375 case MediaMetadataEditor.RATING_KEY_BY_USER: 2376 mCurrentRcClient.updateMetadata(genId, key, value); 2377 break; 2378 default: 2379 Log.e(TAG, "unhandled metadata key " + key + " update for RCC " 2380 + genId); 2381 break; 2382 } 2383 } catch (RemoteException e) { 2384 Log.e(TAG, "Current valid remote client is dead", e); 2385 mCurrentRcClient = null; 2386 } 2387 } 2388 } 2389 } 2390 } 2391 2392 protected void setPlaybackInfoForRcc(int rccId, int what, int value) { 2393 sendMsg(mEventHandler, MSG_RCC_NEW_PLAYBACK_INFO, SENDMSG_QUEUE, 2394 rccId /* arg1 */, what /* arg2 */, Integer.valueOf(value) /* obj */, 0 /* delay */); 2395 } 2396 2397 // handler for MSG_RCC_NEW_PLAYBACK_INFO 2398 private void onNewPlaybackInfoForRcc(int rccId, int key, int value) { 2399 if(DEBUG_RC) Log.d(TAG, "onNewPlaybackInfoForRcc(id=" + rccId + 2400 ", what=" + key + ",val=" + value + ")"); 2401 synchronized(mRCStack) { 2402 // iterating from top of stack as playback information changes are more likely 2403 // on entries at the top of the remote control stack 2404 try { 2405 for (int index = mRCStack.size()-1; index >= 0; index--) { 2406 final RemoteControlStackEntry rcse = mRCStack.elementAt(index); 2407 if (rcse.mRccId == rccId) { 2408 switch (key) { 2409 case RemoteControlClient.PLAYBACKINFO_PLAYBACK_TYPE: 2410 rcse.mPlaybackType = value; 2411 postReevaluateRemote(); 2412 break; 2413 case RemoteControlClient.PLAYBACKINFO_VOLUME: 2414 rcse.mPlaybackVolume = value; 2415 synchronized (mMainRemote) { 2416 if (rccId == mMainRemote.mRccId) { 2417 mMainRemote.mVolume = value; 2418 mVolumeController.postHasNewRemotePlaybackInfo(); 2419 } 2420 } 2421 break; 2422 case RemoteControlClient.PLAYBACKINFO_VOLUME_MAX: 2423 rcse.mPlaybackVolumeMax = value; 2424 synchronized (mMainRemote) { 2425 if (rccId == mMainRemote.mRccId) { 2426 mMainRemote.mVolumeMax = value; 2427 mVolumeController.postHasNewRemotePlaybackInfo(); 2428 } 2429 } 2430 break; 2431 case RemoteControlClient.PLAYBACKINFO_VOLUME_HANDLING: 2432 rcse.mPlaybackVolumeHandling = value; 2433 synchronized (mMainRemote) { 2434 if (rccId == mMainRemote.mRccId) { 2435 mMainRemote.mVolumeHandling = value; 2436 mVolumeController.postHasNewRemotePlaybackInfo(); 2437 } 2438 } 2439 break; 2440 case RemoteControlClient.PLAYBACKINFO_USES_STREAM: 2441 rcse.mPlaybackStream = value; 2442 break; 2443 default: 2444 Log.e(TAG, "unhandled key " + key + " for RCC " + rccId); 2445 break; 2446 } 2447 return; 2448 } 2449 }//for 2450 } catch (ArrayIndexOutOfBoundsException e) { 2451 // not expected to happen, indicates improper concurrent modification 2452 Log.e(TAG, "Wrong index mRCStack on onNewPlaybackInfoForRcc, lock error? ", e); 2453 } 2454 } 2455 } 2456 2457 protected void setPlaybackStateForRcc(int rccId, int state, long timeMs, float speed) { 2458 sendMsg(mEventHandler, MSG_RCC_NEW_PLAYBACK_STATE, SENDMSG_QUEUE, 2459 rccId /* arg1 */, state /* arg2 */, 2460 new RccPlaybackState(state, timeMs, speed) /* obj */, 0 /* delay */); 2461 } 2462 2463 private void onNewPlaybackStateForRcc(int rccId, int state, RccPlaybackState newState) { 2464 if(DEBUG_RC) Log.d(TAG, "onNewPlaybackStateForRcc(id=" + rccId + ", state=" + state 2465 + ", time=" + newState.mPositionMs + ", speed=" + newState.mSpeed + ")"); 2466 synchronized(mRCStack) { 2467 // iterating from top of stack as playback information changes are more likely 2468 // on entries at the top of the remote control stack 2469 try { 2470 for (int index = mRCStack.size()-1; index >= 0; index--) { 2471 final RemoteControlStackEntry rcse = mRCStack.elementAt(index); 2472 if (rcse.mRccId == rccId) { 2473 rcse.mPlaybackState = newState; 2474 synchronized (mMainRemote) { 2475 if (rccId == mMainRemote.mRccId) { 2476 mMainRemoteIsActive = isPlaystateActive(state); 2477 postReevaluateRemote(); 2478 } 2479 } 2480 // an RCC moving to a "playing" state should become the media button 2481 // event receiver so it can be controlled, without requiring the 2482 // app to re-register its receiver 2483 if (isPlaystateActive(state)) { 2484 postPromoteRcc(rccId); 2485 } 2486 } 2487 }//for 2488 } catch (ArrayIndexOutOfBoundsException e) { 2489 // not expected to happen, indicates improper concurrent modification 2490 Log.e(TAG, "Wrong index on mRCStack in onNewPlaybackStateForRcc, lock error? ", e); 2491 } 2492 } 2493 } 2494 2495 protected void registerRemoteVolumeObserverForRcc(int rccId, IRemoteVolumeObserver rvo) { 2496 sendMsg(mEventHandler, MSG_RCC_NEW_VOLUME_OBS, SENDMSG_QUEUE, 2497 rccId /* arg1 */, 0, rvo /* obj */, 0 /* delay */); 2498 } 2499 2500 // handler for MSG_RCC_NEW_VOLUME_OBS 2501 private void onRegisterVolumeObserverForRcc(int rccId, IRemoteVolumeObserver rvo) { 2502 synchronized(mRCStack) { 2503 // The stack traversal order doesn't matter because there is only one stack entry 2504 // with this RCC ID, but the matching ID is more likely at the top of the stack, so 2505 // start iterating from the top. 2506 try { 2507 for (int index = mRCStack.size()-1; index >= 0; index--) { 2508 final RemoteControlStackEntry rcse = mRCStack.elementAt(index); 2509 if (rcse.mRccId == rccId) { 2510 rcse.mRemoteVolumeObs = rvo; 2511 break; 2512 } 2513 } 2514 } catch (ArrayIndexOutOfBoundsException e) { 2515 // not expected to happen, indicates improper concurrent modification 2516 Log.e(TAG, "Wrong index accessing media button stack, lock error? ", e); 2517 } 2518 } 2519 } 2520 2521 /** 2522 * Checks if a remote client is active on the supplied stream type. Update the remote stream 2523 * volume state if found and playing 2524 * @param streamType 2525 * @return false if no remote playing is currently playing 2526 */ 2527 protected boolean checkUpdateRemoteStateIfActive(int streamType) { 2528 synchronized(mRCStack) { 2529 // iterating from top of stack as active playback is more likely on entries at the top 2530 try { 2531 for (int index = mRCStack.size()-1; index >= 0; index--) { 2532 final RemoteControlStackEntry rcse = mRCStack.elementAt(index); 2533 if ((rcse.mPlaybackType == RemoteControlClient.PLAYBACK_TYPE_REMOTE) 2534 && isPlaystateActive(rcse.mPlaybackState.mState) 2535 && (rcse.mPlaybackStream == streamType)) { 2536 if (DEBUG_RC) Log.d(TAG, "remote playback active on stream " + streamType 2537 + ", vol =" + rcse.mPlaybackVolume); 2538 synchronized (mMainRemote) { 2539 mMainRemote.mRccId = rcse.mRccId; 2540 mMainRemote.mVolume = rcse.mPlaybackVolume; 2541 mMainRemote.mVolumeMax = rcse.mPlaybackVolumeMax; 2542 mMainRemote.mVolumeHandling = rcse.mPlaybackVolumeHandling; 2543 mMainRemoteIsActive = true; 2544 } 2545 return true; 2546 } 2547 } 2548 } catch (ArrayIndexOutOfBoundsException e) { 2549 // not expected to happen, indicates improper concurrent modification 2550 Log.e(TAG, "Wrong index accessing RC stack, lock error? ", e); 2551 } 2552 } 2553 synchronized (mMainRemote) { 2554 mMainRemoteIsActive = false; 2555 } 2556 return false; 2557 } 2558 2559 /** 2560 * Returns true if the given playback state is considered "active", i.e. it describes a state 2561 * where playback is happening, or about to 2562 * @param playState the playback state to evaluate 2563 * @return true if active, false otherwise (inactive or unknown) 2564 */ 2565 private static boolean isPlaystateActive(int playState) { 2566 switch (playState) { 2567 case RemoteControlClient.PLAYSTATE_PLAYING: 2568 case RemoteControlClient.PLAYSTATE_BUFFERING: 2569 case RemoteControlClient.PLAYSTATE_FAST_FORWARDING: 2570 case RemoteControlClient.PLAYSTATE_REWINDING: 2571 case RemoteControlClient.PLAYSTATE_SKIPPING_BACKWARDS: 2572 case RemoteControlClient.PLAYSTATE_SKIPPING_FORWARDS: 2573 return true; 2574 default: 2575 return false; 2576 } 2577 } 2578 2579 protected void adjustRemoteVolume(int streamType, int direction, int flags) { 2580 int rccId = RemoteControlClient.RCSE_ID_UNREGISTERED; 2581 boolean volFixed = false; 2582 synchronized (mMainRemote) { 2583 if (!mMainRemoteIsActive) { 2584 if (DEBUG_VOL) Log.w(TAG, "adjustRemoteVolume didn't find an active client"); 2585 return; 2586 } 2587 rccId = mMainRemote.mRccId; 2588 volFixed = (mMainRemote.mVolumeHandling == 2589 RemoteControlClient.PLAYBACK_VOLUME_FIXED); 2590 } 2591 // unlike "local" stream volumes, we can't compute the new volume based on the direction, 2592 // we can only notify the remote that volume needs to be updated, and we'll get an async' 2593 // update through setPlaybackInfoForRcc() 2594 if (!volFixed) { 2595 sendVolumeUpdateToRemote(rccId, direction); 2596 } 2597 2598 // fire up the UI 2599 mVolumeController.postRemoteVolumeChanged(streamType, flags); 2600 } 2601 2602 private void sendVolumeUpdateToRemote(int rccId, int direction) { 2603 if (DEBUG_VOL) { Log.d(TAG, "sendVolumeUpdateToRemote(rccId="+rccId+" , dir="+direction); } 2604 if (direction == 0) { 2605 // only handling discrete events 2606 return; 2607 } 2608 IRemoteVolumeObserver rvo = null; 2609 synchronized (mRCStack) { 2610 // The stack traversal order doesn't matter because there is only one stack entry 2611 // with this RCC ID, but the matching ID is more likely at the top of the stack, so 2612 // start iterating from the top. 2613 try { 2614 for (int index = mRCStack.size()-1; index >= 0; index--) { 2615 final RemoteControlStackEntry rcse = mRCStack.elementAt(index); 2616 //FIXME OPTIMIZE store this info in mMainRemote so we don't have to iterate? 2617 if (rcse.mRccId == rccId) { 2618 rvo = rcse.mRemoteVolumeObs; 2619 break; 2620 } 2621 } 2622 } catch (ArrayIndexOutOfBoundsException e) { 2623 // not expected to happen, indicates improper concurrent modification 2624 Log.e(TAG, "Wrong index accessing media button stack, lock error? ", e); 2625 } 2626 } 2627 if (rvo != null) { 2628 try { 2629 rvo.dispatchRemoteVolumeUpdate(direction, -1); 2630 } catch (RemoteException e) { 2631 Log.e(TAG, "Error dispatching relative volume update", e); 2632 } 2633 } 2634 } 2635 2636 protected int getRemoteStreamMaxVolume() { 2637 synchronized (mMainRemote) { 2638 if (mMainRemote.mRccId == RemoteControlClient.RCSE_ID_UNREGISTERED) { 2639 return 0; 2640 } 2641 return mMainRemote.mVolumeMax; 2642 } 2643 } 2644 2645 protected int getRemoteStreamVolume() { 2646 synchronized (mMainRemote) { 2647 if (mMainRemote.mRccId == RemoteControlClient.RCSE_ID_UNREGISTERED) { 2648 return 0; 2649 } 2650 return mMainRemote.mVolume; 2651 } 2652 } 2653 2654 protected void setRemoteStreamVolume(int vol) { 2655 if (DEBUG_VOL) { Log.d(TAG, "setRemoteStreamVolume(vol="+vol+")"); } 2656 int rccId = RemoteControlClient.RCSE_ID_UNREGISTERED; 2657 synchronized (mMainRemote) { 2658 if (mMainRemote.mRccId == RemoteControlClient.RCSE_ID_UNREGISTERED) { 2659 return; 2660 } 2661 rccId = mMainRemote.mRccId; 2662 } 2663 IRemoteVolumeObserver rvo = null; 2664 synchronized (mRCStack) { 2665 // The stack traversal order doesn't matter because there is only one stack entry 2666 // with this RCC ID, but the matching ID is more likely at the top of the stack, so 2667 // start iterating from the top. 2668 try { 2669 for (int index = mRCStack.size()-1; index >= 0; index--) { 2670 final RemoteControlStackEntry rcse = mRCStack.elementAt(index); 2671 //FIXME OPTIMIZE store this info in mMainRemote so we don't have to iterate? 2672 if (rcse.mRccId == rccId) { 2673 rvo = rcse.mRemoteVolumeObs; 2674 break; 2675 } 2676 } 2677 } catch (ArrayIndexOutOfBoundsException e) { 2678 // not expected to happen, indicates improper concurrent modification 2679 Log.e(TAG, "Wrong index accessing media button stack, lock error? ", e); 2680 } 2681 } 2682 if (rvo != null) { 2683 try { 2684 rvo.dispatchRemoteVolumeUpdate(0, vol); 2685 } catch (RemoteException e) { 2686 Log.e(TAG, "Error dispatching absolute volume update", e); 2687 } 2688 } 2689 } 2690 2691 /** 2692 * Call to make AudioService reevaluate whether it's in a mode where remote players should 2693 * have their volume controlled. In this implementation this is only to reset whether 2694 * VolumePanel should display remote volumes 2695 */ 2696 private void postReevaluateRemote() { 2697 sendMsg(mEventHandler, MSG_REEVALUATE_REMOTE, SENDMSG_QUEUE, 0, 0, null, 0); 2698 } 2699 2700 private void onReevaluateRemote() { 2701 if (DEBUG_VOL) { Log.w(TAG, "onReevaluateRemote()"); } 2702 // is there a registered RemoteControlClient that is handling remote playback 2703 boolean hasRemotePlayback = false; 2704 synchronized (mRCStack) { 2705 // iteration stops when PLAYBACK_TYPE_REMOTE is found, so remote control stack 2706 // traversal order doesn't matter 2707 Iterator<RemoteControlStackEntry> stackIterator = mRCStack.iterator(); 2708 while(stackIterator.hasNext()) { 2709 RemoteControlStackEntry rcse = stackIterator.next(); 2710 if (rcse.mPlaybackType == RemoteControlClient.PLAYBACK_TYPE_REMOTE) { 2711 hasRemotePlayback = true; 2712 break; 2713 } 2714 } 2715 } 2716 synchronized (mMainRemote) { 2717 if (mHasRemotePlayback != hasRemotePlayback) { 2718 mHasRemotePlayback = hasRemotePlayback; 2719 mVolumeController.postRemoteSliderVisibility(hasRemotePlayback); 2720 } 2721 } 2722 } 2723 2724} 2725