Skip to content

Commit fca91a8

Browse files
jmtriviAndroid (Google) Code Review
authored andcommitted
Merge "Type of search on headset key long press must depend on device state" into jb-dev
2 parents af5c011 + 3c2711f commit fca91a8

File tree

1 file changed

+21
-26
lines changed

1 file changed

+21
-26
lines changed

media/java/android/media/AudioService.java

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
import android.os.Vibrator;
6060
import android.provider.Settings;
6161
import android.provider.Settings.System;
62+
import android.speech.RecognizerIntent;
6263
import android.telephony.PhoneStateListener;
6364
import android.telephony.TelephonyManager;
6465
import android.util.Log;
@@ -3818,45 +3819,39 @@ private static boolean isValidVoiceInputKeyCode(int keyCode) {
38183819
* Tell the system to start voice-based interactions / voice commands
38193820
*/
38203821
private void startVoiceBasedInteractions(boolean needWakeLock) {
3821-
Intent voiceIntent = new Intent(android.speech.RecognizerIntent.ACTION_WEB_SEARCH);
3822+
Intent voiceIntent = null;
3823+
// select which type of search to launch:
3824+
// - screen on and device unlocked: action is ACTION_WEB_SEARCH
3825+
// - device locked or screen off: action is ACTION_VOICE_SEARCH_HANDS_FREE
3826+
// with EXTRA_SECURE set to true if the device is securely locked
3827+
PowerManager pm = (PowerManager)mContext.getSystemService(Context.POWER_SERVICE);
3828+
boolean isLocked = mKeyguardManager != null && mKeyguardManager.isKeyguardLocked();
3829+
if (!isLocked && pm.isScreenOn()) {
3830+
voiceIntent = new Intent(android.speech.RecognizerIntent.ACTION_WEB_SEARCH);
3831+
} else {
3832+
voiceIntent = new Intent(RecognizerIntent.ACTION_VOICE_SEARCH_HANDS_FREE);
3833+
voiceIntent.putExtra(RecognizerIntent.EXTRA_SECURE,
3834+
isLocked && mKeyguardManager.isKeyguardSecure());
3835+
}
3836+
// start the search activity
38223837
if (needWakeLock) {
38233838
mMediaEventWakeLock.acquire();
38243839
}
3825-
voiceIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
3826-
| Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
38273840
try {
3828-
if (mKeyguardManager != null) {
3829-
// it's ok to start voice-based interactions when:
3830-
// - the device is locked but doesn't require a password to be unlocked
3831-
// - the device is not locked
3832-
if ((mKeyguardManager.isKeyguardLocked() && !mKeyguardManager.isKeyguardSecure())
3833-
|| !mKeyguardManager.isKeyguardLocked()) {
3834-
mContext.startActivity(voiceIntent);
3835-
}
3841+
if (voiceIntent != null) {
3842+
voiceIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
3843+
| Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
3844+
mContext.startActivity(voiceIntent);
38363845
}
38373846
} catch (ActivityNotFoundException e) {
3838-
Log.e(TAG, "Error launching activity for ACTION_WEB_SEARCH: " + e);
3847+
Log.w(TAG, "No activity for search: " + e);
38393848
} finally {
38403849
if (needWakeLock) {
38413850
mMediaEventWakeLock.release();
38423851
}
38433852
}
38443853
}
38453854

3846-
/**
3847-
* Verify whether it is safe to start voice-based interactions given the state of the system
3848-
* @return false is the Keyguard is locked and secure, true otherwise
3849-
*/
3850-
private boolean safeToStartVoiceBasedInteractions() {
3851-
KeyguardManager keyguard =
3852-
(KeyguardManager) mContext.getSystemService(Context.KEYGUARD_SERVICE);
3853-
if (keyguard == null) {
3854-
return false;
3855-
}
3856-
3857-
return true;
3858-
}
3859-
38603855
private PowerManager.WakeLock mMediaEventWakeLock;
38613856

38623857
private static final int WAKELOCK_RELEASE_ON_FINISHED = 1980; //magic number

0 commit comments

Comments
 (0)