diff options
author | RoboErik <epastern@google.com> | 2014-05-21 19:59:10 +0000 |
---|---|---|
committer | Android (Google) Code Review <android-gerrit@google.com> | 2014-05-21 19:59:11 +0000 |
commit | 5b53148a4e8d47b88362703445010c54f9fd6ecc (patch) | |
tree | 879e7fa812213756a040528de94894f34e061a5c /services | |
parent | afe18ed6cd409f75315da8e834554a56f0ec4651 (diff) | |
parent | 9a9d0b5f6f4be758ed6c8b837a9dd01a451bc0c0 (diff) | |
download | frameworks_base-5b53148a4e8d47b88362703445010c54f9fd6ecc.zip frameworks_base-5b53148a4e8d47b88362703445010c54f9fd6ecc.tar.gz frameworks_base-5b53148a4e8d47b88362703445010c54f9fd6ecc.tar.bz2 |
Merge changes I1cbdff1d,I23906b4c
* changes:
Handle headsethook voice launching
Add 5s timeout to wakelock
Diffstat (limited to 'services')
-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 | 202 |
2 files changed, 181 insertions, 31 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..9d85167 100644 --- a/services/core/java/com/android/server/media/MediaSessionService.java +++ b/services/core/java/com/android/server/media/MediaSessionService.java @@ -19,6 +19,8 @@ package com.android.server.media; import android.Manifest; import android.app.Activity; import android.app.ActivityManager; +import android.app.KeyguardManager; +import android.content.ActivityNotFoundException; import android.content.BroadcastReceiver; import android.content.ComponentName; import android.content.Context; @@ -40,6 +42,7 @@ import android.os.RemoteException; import android.os.ResultReceiver; import android.os.UserHandle; import android.provider.Settings; +import android.speech.RecognizerIntent; import android.text.TextUtils; import android.util.Log; import android.util.SparseArray; @@ -61,6 +64,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; @@ -73,6 +78,8 @@ public class MediaSessionService extends SystemService implements Monitor { private final Handler mHandler = new Handler(); private final PowerManager.WakeLock mMediaEventWakeLock; + private KeyguardManager mKeyguardManager; + private MediaSessionRecord mPrioritySession; private int mCurrentUserId = -1; @@ -96,6 +103,8 @@ public class MediaSessionService extends SystemService implements Monitor { publishBinderService(Context.MEDIA_SESSION_SERVICE, mSessionManagerImpl); Watchdog.getInstance().addMonitor(this); updateUser(); + mKeyguardManager = + (KeyguardManager) getContext().getSystemService(Context.KEYGUARD_SERVICE); } /** @@ -599,6 +608,9 @@ public class MediaSessionService extends SystemService implements Monitor { "android.media.AudioService.WAKELOCK_ACQUIRED"; private static final int WAKELOCK_RELEASE_ON_FINISHED = 1980; // magic number + private boolean mVoiceButtonDown = false; + private boolean mVoiceButtonHandled = false; + @Override public ISession createSession(String packageName, ISessionCallback cb, String tag, int userId) throws RemoteException { @@ -676,31 +688,13 @@ public class MediaSessionService extends SystemService implements Monitor { final long token = Binder.clearCallingIdentity(); try { - if (needWakeLock) { - mMediaEventWakeLock.acquire(); - } synchronized (mLock) { - MediaSessionRecord mbSession = mPriorityStack + MediaSessionRecord session = mPriorityStack .getDefaultMediaButtonSession(mCurrentUserId); - if (mbSession != null) { - if (DEBUG) { - Log.d(TAG, "Sending media key to " + mbSession.getSessionInfo()); - } - mbSession.sendMediaButton(keyEvent, - needWakeLock ? mKeyEventDoneReceiver : null); + if (isVoiceKey(keyEvent.getKeyCode())) { + handleVoiceKeyEventLocked(keyEvent, needWakeLock, session); } else { - if (DEBUG) { - Log.d(TAG, "Sending media key ordered broadcast"); - } - // Fallback to legacy behavior - Intent keyIntent = new Intent(Intent.ACTION_MEDIA_BUTTON, null); - keyIntent.putExtra(Intent.EXTRA_KEY_EVENT, keyEvent); - if (needWakeLock) { - keyIntent.putExtra(EXTRA_WAKELOCK_ACQUIRED, - WAKELOCK_RELEASE_ON_FINISHED); - } - getContext().sendOrderedBroadcastAsUser(keyIntent, UserHandle.ALL, - null, mKeyEventDone, mHandler, Activity.RESULT_OK, null, null); + dispatchMediaKeyEventLocked(keyEvent, needWakeLock, session); } } } finally { @@ -743,15 +737,171 @@ public class MediaSessionService extends SystemService implements Monitor { } } - ResultReceiver mKeyEventDoneReceiver = new ResultReceiver(mHandler) { + private void handleVoiceKeyEventLocked(KeyEvent keyEvent, boolean needWakeLock, + MediaSessionRecord session) { + if (session != null && session.hasFlag(MediaSession.FLAG_EXCLUSIVE_GLOBAL_PRIORITY)) { + // If the phone app has priority just give it the event + dispatchMediaKeyEventLocked(keyEvent, needWakeLock, session); + return; + } + int action = keyEvent.getAction(); + boolean isLongPress = (keyEvent.getFlags() & KeyEvent.FLAG_LONG_PRESS) != 0; + if (action == KeyEvent.ACTION_DOWN) { + if (keyEvent.getRepeatCount() == 0) { + mVoiceButtonDown = true; + mVoiceButtonHandled = false; + } else if (mVoiceButtonDown && !mVoiceButtonHandled && isLongPress) { + mVoiceButtonHandled = true; + startVoiceInput(needWakeLock); + } + } else if (action == KeyEvent.ACTION_UP) { + if (mVoiceButtonDown) { + mVoiceButtonDown = false; + if (!mVoiceButtonHandled && !keyEvent.isCanceled()) { + // Resend the down then send this event through + KeyEvent downEvent = KeyEvent.changeAction(keyEvent, KeyEvent.ACTION_DOWN); + dispatchMediaKeyEventLocked(downEvent, needWakeLock, session); + dispatchMediaKeyEventLocked(keyEvent, needWakeLock, session); + } + } + } + } + + private void dispatchMediaKeyEventLocked(KeyEvent keyEvent, boolean needWakeLock, + MediaSessionRecord session) { + if (session != null) { + if (DEBUG) { + Log.d(TAG, "Sending media key to " + session.getSessionInfo()); + } + if (needWakeLock) { + mKeyEventReceiver.aquireWakeLockLocked(); + } + // If we don't need a wakelock use -1 as the id so we + // won't release it later + session.sendMediaButton(keyEvent, + needWakeLock ? mKeyEventReceiver.mLastTimeoutId : -1, + mKeyEventReceiver); + } else { + if (needWakeLock) { + mMediaEventWakeLock.acquire(); + } + if (DEBUG) { + Log.d(TAG, "Sending media key ordered broadcast"); + } + // Fallback to legacy behavior + Intent keyIntent = new Intent(Intent.ACTION_MEDIA_BUTTON, null); + keyIntent.putExtra(Intent.EXTRA_KEY_EVENT, keyEvent); + if (needWakeLock) { + keyIntent.putExtra(EXTRA_WAKELOCK_ACQUIRED, + WAKELOCK_RELEASE_ON_FINISHED); + } + getContext().sendOrderedBroadcastAsUser(keyIntent, UserHandle.ALL, + null, mKeyEventDone, mHandler, Activity.RESULT_OK, null, null); + } + } + + private void startVoiceInput(boolean needWakeLock) { + Intent voiceIntent = null; + // select which type of search to launch: + // - screen on and device unlocked: action is ACTION_WEB_SEARCH + // - device locked or screen off: action is + // ACTION_VOICE_SEARCH_HANDS_FREE + // with EXTRA_SECURE set to true if the device is securely locked + PowerManager pm = (PowerManager) getContext().getSystemService(Context.POWER_SERVICE); + boolean isLocked = mKeyguardManager != null && mKeyguardManager.isKeyguardLocked(); + if (!isLocked && pm.isScreenOn()) { + voiceIntent = new Intent(android.speech.RecognizerIntent.ACTION_WEB_SEARCH); + Log.i(TAG, "voice-based interactions: about to use ACTION_WEB_SEARCH"); + } else { + voiceIntent = new Intent(RecognizerIntent.ACTION_VOICE_SEARCH_HANDS_FREE); + voiceIntent.putExtra(RecognizerIntent.EXTRA_SECURE, + isLocked && mKeyguardManager.isKeyguardSecure()); + Log.i(TAG, "voice-based interactions: about to use ACTION_VOICE_SEARCH_HANDS_FREE"); + } + // start the search activity + if (needWakeLock) { + mMediaEventWakeLock.acquire(); + } + try { + if (voiceIntent != null) { + voiceIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK + | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS); + getContext().startActivityAsUser(voiceIntent, UserHandle.CURRENT); + } + } catch (ActivityNotFoundException e) { + Log.w(TAG, "No activity for search: " + e); + } finally { + if (needWakeLock) { + mMediaEventWakeLock.release(); + } + } + } + + private boolean isVoiceKey(int keyCode) { + return keyCode == KeyEvent.KEYCODE_HEADSETHOOK; + } + + 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() { |