|
59 | 59 | import android.os.Vibrator; |
60 | 60 | import android.provider.Settings; |
61 | 61 | import android.provider.Settings.System; |
| 62 | +import android.speech.RecognizerIntent; |
62 | 63 | import android.telephony.PhoneStateListener; |
63 | 64 | import android.telephony.TelephonyManager; |
64 | 65 | import android.util.Log; |
@@ -3818,45 +3819,39 @@ private static boolean isValidVoiceInputKeyCode(int keyCode) { |
3818 | 3819 | * Tell the system to start voice-based interactions / voice commands |
3819 | 3820 | */ |
3820 | 3821 | 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 |
3822 | 3837 | if (needWakeLock) { |
3823 | 3838 | mMediaEventWakeLock.acquire(); |
3824 | 3839 | } |
3825 | | - voiceIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
3826 | | - | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS); |
3827 | 3840 | 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); |
3836 | 3845 | } |
3837 | 3846 | } catch (ActivityNotFoundException e) { |
3838 | | - Log.e(TAG, "Error launching activity for ACTION_WEB_SEARCH: " + e); |
| 3847 | + Log.w(TAG, "No activity for search: " + e); |
3839 | 3848 | } finally { |
3840 | 3849 | if (needWakeLock) { |
3841 | 3850 | mMediaEventWakeLock.release(); |
3842 | 3851 | } |
3843 | 3852 | } |
3844 | 3853 | } |
3845 | 3854 |
|
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 | | - |
3860 | 3855 | private PowerManager.WakeLock mMediaEventWakeLock; |
3861 | 3856 |
|
3862 | 3857 | private static final int WAKELOCK_RELEASE_ON_FINISHED = 1980; //magic number |
|
0 commit comments