diff options
author | RoboErik <epastern@google.com> | 2014-05-19 09:25:25 -0700 |
---|---|---|
committer | RoboErik <epastern@google.com> | 2014-05-20 14:57:46 -0700 |
commit | 418c10ca9df1505509afeffd558cd92fc97bc635 (patch) | |
tree | 6055e734f0029497467308fb02d55acc49ced3d9 /services/core/java/com/android/server/media | |
parent | 48dfae92d24de47672d01612f66102c7b435b16f (diff) | |
download | frameworks_base-418c10ca9df1505509afeffd558cd92fc97bc635.zip frameworks_base-418c10ca9df1505509afeffd558cd92fc97bc635.tar.gz frameworks_base-418c10ca9df1505509afeffd558cd92fc97bc635.tar.bz2 |
Add 5s timeout to wakelock
Also filed a b/15110168 to anr the app if it doesn't finish with the
button event in 5s.
Change-Id: I23906b4c7f17d3d3a29eef92b523d8653c87017e
Diffstat (limited to 'services/core/java/com/android/server/media')
-rw-r--r-- | services/core/java/com/android/server/media/MediaSessionRecord.java | 10 | ||||
-rw-r--r-- | services/core/java/com/android/server/media/MediaSessionService.java | 76 |
2 files changed, 73 insertions, 13 deletions
diff --git a/services/core/java/com/android/server/media/MediaSessionRecord.java b/services/core/java/com/android/server/media/MediaSessionRecord.java index 00d364b..030e3ed 100644 --- a/services/core/java/com/android/server/media/MediaSessionRecord.java +++ b/services/core/java/com/android/server/media/MediaSessionRecord.java @@ -365,8 +365,8 @@ public class MediaSessionRecord implements IBinder.DeathRecipient { return mSessionCb.mCb; } - public void sendMediaButton(KeyEvent ke, ResultReceiver cb) { - mSessionCb.sendMediaButton(ke, cb); + public void sendMediaButton(KeyEvent ke, int sequenceId, ResultReceiver cb) { + mSessionCb.sendMediaButton(ke, sequenceId, cb); } public void dump(PrintWriter pw, String prefix) { @@ -649,11 +649,11 @@ public class MediaSessionRecord implements IBinder.DeathRecipient { mCb = cb; } - public void sendMediaButton(KeyEvent keyEvent, ResultReceiver cb) { + public void sendMediaButton(KeyEvent keyEvent, int sequenceId, ResultReceiver cb) { Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON); mediaButtonIntent.putExtra(Intent.EXTRA_KEY_EVENT, keyEvent); try { - mCb.onMediaButton(mediaButtonIntent, cb); + mCb.onMediaButton(mediaButtonIntent, sequenceId, cb); } catch (RemoteException e) { Slog.e(TAG, "Remote failure in sendMediaRequest.", e); } @@ -789,7 +789,7 @@ public class MediaSessionRecord implements IBinder.DeathRecipient { @Override public void sendMediaButton(KeyEvent mediaButtonIntent) { - mSessionCb.sendMediaButton(mediaButtonIntent, null); + mSessionCb.sendMediaButton(mediaButtonIntent, 0, null); } @Override diff --git a/services/core/java/com/android/server/media/MediaSessionService.java b/services/core/java/com/android/server/media/MediaSessionService.java index d9e45f5ba..9a1c6d5 100644 --- a/services/core/java/com/android/server/media/MediaSessionService.java +++ b/services/core/java/com/android/server/media/MediaSessionService.java @@ -61,6 +61,8 @@ public class MediaSessionService extends SystemService implements Monitor { private static final String TAG = "MediaSessionService"; private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG); + private static final int WAKELOCK_TIMEOUT = 5000; + private final SessionManagerImpl mSessionManagerImpl; // private final MediaRouteProviderWatcher mRouteProviderWatcher; private final MediaSessionStack mPriorityStack; @@ -676,9 +678,6 @@ public class MediaSessionService extends SystemService implements Monitor { final long token = Binder.clearCallingIdentity(); try { - if (needWakeLock) { - mMediaEventWakeLock.acquire(); - } synchronized (mLock) { MediaSessionRecord mbSession = mPriorityStack .getDefaultMediaButtonSession(mCurrentUserId); @@ -686,9 +685,18 @@ public class MediaSessionService extends SystemService implements Monitor { if (DEBUG) { Log.d(TAG, "Sending media key to " + mbSession.getSessionInfo()); } + if (needWakeLock) { + mKeyEventReceiver.aquireWakeLockLocked(); + } + // If we don't need a wakelock use -1 as the id so we + // won't release it later mbSession.sendMediaButton(keyEvent, - needWakeLock ? mKeyEventDoneReceiver : null); + needWakeLock ? mKeyEventReceiver.mLastTimeoutId : -1, + mKeyEventReceiver); } else { + if (needWakeLock) { + mMediaEventWakeLock.acquire(); + } if (DEBUG) { Log.d(TAG, "Sending media key ordered broadcast"); } @@ -743,15 +751,67 @@ public class MediaSessionService extends SystemService implements Monitor { } } - ResultReceiver mKeyEventDoneReceiver = new ResultReceiver(mHandler) { + private KeyEventWakeLockReceiver mKeyEventReceiver = new KeyEventWakeLockReceiver(mHandler); + + class KeyEventWakeLockReceiver extends ResultReceiver implements Runnable { + private final Handler mHandler; + private int mRefCount = 0; + private int mLastTimeoutId = 0; + + public KeyEventWakeLockReceiver(Handler handler) { + super(handler); + mHandler = handler; + } + + public void onTimeout() { + synchronized (mLock) { + if (mRefCount == 0) { + // We've already released it, so just return + return; + } + mLastTimeoutId++; + mRefCount = 0; + releaseWakeLockLocked(); + } + } + + public void aquireWakeLockLocked() { + if (mRefCount == 0) { + mMediaEventWakeLock.acquire(); + } + mRefCount++; + mHandler.removeCallbacks(this); + mHandler.postDelayed(this, WAKELOCK_TIMEOUT); + + } + + @Override + public void run() { + onTimeout(); + } + @Override protected void onReceiveResult(int resultCode, Bundle resultData) { - synchronized (mLock) { - if (mMediaEventWakeLock.isHeld()) { - mMediaEventWakeLock.release(); + if (resultCode < mLastTimeoutId) { + // Ignore results from calls that were before the last + // timeout, just in case. + return; + } else { + synchronized (mLock) { + if (mRefCount > 0) { + mRefCount--; + if (mRefCount == 0) { + releaseWakeLockLocked(); + } + } } } } + + private void releaseWakeLockLocked() { + mMediaEventWakeLock.release(); + mHandler.removeCallbacks(this); + } }; BroadcastReceiver mKeyEventDone = new BroadcastReceiver() { |