1 /* 2 * Copyright (C) 2008 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 17 package com.android.internal.policy; 18 19 import android.app.KeyguardManager; 20 import android.app.SearchManager; 21 import android.content.ActivityNotFoundException; 22 import android.content.Context; 23 import android.content.Intent; 24 import android.content.res.Configuration; 25 import android.media.AudioManager; 26 import android.media.session.MediaSessionLegacyHelper; 27 import android.os.UserHandle; 28 import android.provider.Settings; 29 import android.telephony.TelephonyManager; 30 import android.util.Log; 31 import android.view.FallbackEventHandler; 32 import android.view.HapticFeedbackConstants; 33 import android.view.KeyEvent; 34 import android.view.View; 35 import com.android.internal.policy.PhoneWindow; 36 37 /** 38 * @hide 39 */ 40 public class PhoneFallbackEventHandler implements FallbackEventHandler { 41 private static String TAG = "PhoneFallbackEventHandler"; 42 private static final boolean DEBUG = false; 43 44 Context mContext; 45 View mView; 46 47 AudioManager mAudioManager; 48 KeyguardManager mKeyguardManager; 49 SearchManager mSearchManager; 50 TelephonyManager mTelephonyManager; 51 PhoneFallbackEventHandler(Context context)52 public PhoneFallbackEventHandler(Context context) { 53 mContext = context; 54 } 55 setView(View v)56 public void setView(View v) { 57 mView = v; 58 } 59 preDispatchKeyEvent(KeyEvent event)60 public void preDispatchKeyEvent(KeyEvent event) { 61 getAudioManager().preDispatchKeyEvent(event, AudioManager.USE_DEFAULT_STREAM_TYPE); 62 } 63 dispatchKeyEvent(KeyEvent event)64 public boolean dispatchKeyEvent(KeyEvent event) { 65 66 final int action = event.getAction(); 67 final int keyCode = event.getKeyCode(); 68 69 if (action == KeyEvent.ACTION_DOWN) { 70 return onKeyDown(keyCode, event); 71 } else { 72 return onKeyUp(keyCode, event); 73 } 74 } 75 onKeyDown(int keyCode, KeyEvent event)76 boolean onKeyDown(int keyCode, KeyEvent event) { 77 /* **************************************************************************** 78 * HOW TO DECIDE WHERE YOUR KEY HANDLING GOES. 79 * See the comment in PhoneWindow.onKeyDown 80 * ****************************************************************************/ 81 final KeyEvent.DispatcherState dispatcher = mView.getKeyDispatcherState(); 82 83 switch (keyCode) { 84 case KeyEvent.KEYCODE_VOLUME_UP: 85 case KeyEvent.KEYCODE_VOLUME_DOWN: 86 case KeyEvent.KEYCODE_VOLUME_MUTE: { 87 MediaSessionLegacyHelper.getHelper(mContext).sendVolumeKeyEvent( 88 event, AudioManager.USE_DEFAULT_STREAM_TYPE, false); 89 return true; 90 } 91 92 93 case KeyEvent.KEYCODE_MEDIA_PLAY: 94 case KeyEvent.KEYCODE_MEDIA_PAUSE: 95 case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE: 96 /* Suppress PLAY/PAUSE toggle when phone is ringing or in-call 97 * to avoid music playback */ 98 if (getTelephonyManager().getCallState() != TelephonyManager.CALL_STATE_IDLE) { 99 return true; // suppress key event 100 } 101 case KeyEvent.KEYCODE_MUTE: 102 case KeyEvent.KEYCODE_HEADSETHOOK: 103 case KeyEvent.KEYCODE_MEDIA_STOP: 104 case KeyEvent.KEYCODE_MEDIA_NEXT: 105 case KeyEvent.KEYCODE_MEDIA_PREVIOUS: 106 case KeyEvent.KEYCODE_MEDIA_REWIND: 107 case KeyEvent.KEYCODE_MEDIA_RECORD: 108 case KeyEvent.KEYCODE_MEDIA_FAST_FORWARD: 109 case KeyEvent.KEYCODE_MEDIA_AUDIO_TRACK: { 110 handleMediaKeyEvent(event); 111 return true; 112 } 113 114 case KeyEvent.KEYCODE_CALL: { 115 if (getKeyguardManager().inKeyguardRestrictedInputMode() || dispatcher == null) { 116 break; 117 } 118 if (event.getRepeatCount() == 0) { 119 dispatcher.startTracking(event, this); 120 } else if (event.isLongPress() && dispatcher.isTracking(event)) { 121 dispatcher.performedLongPress(event); 122 if (isUserSetupComplete()) { 123 mView.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS); 124 // launch the VoiceDialer 125 Intent intent = new Intent(Intent.ACTION_VOICE_COMMAND); 126 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 127 try { 128 sendCloseSystemWindows(); 129 mContext.startActivity(intent); 130 } catch (ActivityNotFoundException e) { 131 startCallActivity(); 132 } 133 } else { 134 Log.i(TAG, "Not starting call activity because user " 135 + "setup is in progress."); 136 } 137 } 138 return true; 139 } 140 141 case KeyEvent.KEYCODE_CAMERA: { 142 if (getKeyguardManager().inKeyguardRestrictedInputMode() || dispatcher == null) { 143 break; 144 } 145 if (event.getRepeatCount() == 0) { 146 dispatcher.startTracking(event, this); 147 } else if (event.isLongPress() && dispatcher.isTracking(event)) { 148 dispatcher.performedLongPress(event); 149 if (isUserSetupComplete()) { 150 mView.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS); 151 sendCloseSystemWindows(); 152 // Broadcast an intent that the Camera button was longpressed 153 Intent intent = new Intent(Intent.ACTION_CAMERA_BUTTON, null); 154 intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND); 155 intent.putExtra(Intent.EXTRA_KEY_EVENT, event); 156 mContext.sendOrderedBroadcastAsUser(intent, UserHandle.CURRENT_OR_SELF, 157 null, null, null, 0, null, null); 158 } else { 159 Log.i(TAG, "Not dispatching CAMERA long press because user " 160 + "setup is in progress."); 161 } 162 } 163 return true; 164 } 165 166 case KeyEvent.KEYCODE_SEARCH: { 167 if (getKeyguardManager().inKeyguardRestrictedInputMode() || dispatcher == null) { 168 break; 169 } 170 if (event.getRepeatCount() == 0) { 171 dispatcher.startTracking(event, this); 172 } else if (event.isLongPress() && dispatcher.isTracking(event)) { 173 Configuration config = mContext.getResources().getConfiguration(); 174 if (config.keyboard == Configuration.KEYBOARD_NOKEYS 175 || config.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_YES) { 176 if (isUserSetupComplete()) { 177 // launch the search activity 178 Intent intent = new Intent(Intent.ACTION_SEARCH_LONG_PRESS); 179 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 180 try { 181 mView.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS); 182 sendCloseSystemWindows(); 183 getSearchManager().stopSearch(); 184 mContext.startActivity(intent); 185 // Only clear this if we successfully start the 186 // activity; otherwise we will allow the normal short 187 // press action to be performed. 188 dispatcher.performedLongPress(event); 189 return true; 190 } catch (ActivityNotFoundException e) { 191 // Ignore 192 } 193 } else { 194 Log.i(TAG, "Not dispatching SEARCH long press because user " 195 + "setup is in progress."); 196 } 197 } 198 } 199 break; 200 } 201 } 202 return false; 203 } 204 onKeyUp(int keyCode, KeyEvent event)205 boolean onKeyUp(int keyCode, KeyEvent event) { 206 if (DEBUG) { 207 Log.d(TAG, "up " + keyCode); 208 } 209 final KeyEvent.DispatcherState dispatcher = mView.getKeyDispatcherState(); 210 if (dispatcher != null) { 211 dispatcher.handleUpEvent(event); 212 } 213 214 switch (keyCode) { 215 case KeyEvent.KEYCODE_VOLUME_UP: 216 case KeyEvent.KEYCODE_VOLUME_DOWN: 217 case KeyEvent.KEYCODE_VOLUME_MUTE: { 218 if (!event.isCanceled()) { 219 MediaSessionLegacyHelper.getHelper(mContext).sendVolumeKeyEvent( 220 event, AudioManager.USE_DEFAULT_STREAM_TYPE, false); 221 } 222 return true; 223 } 224 225 case KeyEvent.KEYCODE_HEADSETHOOK: 226 case KeyEvent.KEYCODE_MUTE: 227 case KeyEvent.KEYCODE_MEDIA_PLAY: 228 case KeyEvent.KEYCODE_MEDIA_PAUSE: 229 case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE: 230 case KeyEvent.KEYCODE_MEDIA_STOP: 231 case KeyEvent.KEYCODE_MEDIA_NEXT: 232 case KeyEvent.KEYCODE_MEDIA_PREVIOUS: 233 case KeyEvent.KEYCODE_MEDIA_REWIND: 234 case KeyEvent.KEYCODE_MEDIA_RECORD: 235 case KeyEvent.KEYCODE_MEDIA_FAST_FORWARD: 236 case KeyEvent.KEYCODE_MEDIA_AUDIO_TRACK: { 237 handleMediaKeyEvent(event); 238 return true; 239 } 240 241 case KeyEvent.KEYCODE_CAMERA: { 242 if (getKeyguardManager().inKeyguardRestrictedInputMode()) { 243 break; 244 } 245 if (event.isTracking() && !event.isCanceled()) { 246 // Add short press behavior here if desired 247 } 248 return true; 249 } 250 251 case KeyEvent.KEYCODE_CALL: { 252 if (getKeyguardManager().inKeyguardRestrictedInputMode()) { 253 break; 254 } 255 if (event.isTracking() && !event.isCanceled()) { 256 if (isUserSetupComplete()) { 257 startCallActivity(); 258 } else { 259 Log.i(TAG, "Not starting call activity because user " 260 + "setup is in progress."); 261 } 262 } 263 return true; 264 } 265 } 266 return false; 267 } 268 startCallActivity()269 void startCallActivity() { 270 sendCloseSystemWindows(); 271 Intent intent = new Intent(Intent.ACTION_CALL_BUTTON); 272 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 273 try { 274 mContext.startActivity(intent); 275 } catch (ActivityNotFoundException e) { 276 Log.w(TAG, "No activity found for android.intent.action.CALL_BUTTON."); 277 } 278 } 279 getSearchManager()280 SearchManager getSearchManager() { 281 if (mSearchManager == null) { 282 mSearchManager = (SearchManager) mContext.getSystemService(Context.SEARCH_SERVICE); 283 } 284 return mSearchManager; 285 } 286 getTelephonyManager()287 TelephonyManager getTelephonyManager() { 288 if (mTelephonyManager == null) { 289 mTelephonyManager = (TelephonyManager)mContext.getSystemService( 290 Context.TELEPHONY_SERVICE); 291 } 292 return mTelephonyManager; 293 } 294 getKeyguardManager()295 KeyguardManager getKeyguardManager() { 296 if (mKeyguardManager == null) { 297 mKeyguardManager = (KeyguardManager)mContext.getSystemService(Context.KEYGUARD_SERVICE); 298 } 299 return mKeyguardManager; 300 } 301 getAudioManager()302 AudioManager getAudioManager() { 303 if (mAudioManager == null) { 304 mAudioManager = (AudioManager)mContext.getSystemService(Context.AUDIO_SERVICE); 305 } 306 return mAudioManager; 307 } 308 sendCloseSystemWindows()309 void sendCloseSystemWindows() { 310 PhoneWindow.sendCloseSystemWindows(mContext, null); 311 } 312 handleMediaKeyEvent(KeyEvent keyEvent)313 private void handleMediaKeyEvent(KeyEvent keyEvent) { 314 MediaSessionLegacyHelper.getHelper(mContext).sendMediaButtonEvent(keyEvent, false); 315 } 316 isUserSetupComplete()317 private boolean isUserSetupComplete() { 318 return Settings.Secure.getInt(mContext.getContentResolver(), 319 Settings.Secure.USER_SETUP_COMPLETE, 0) != 0; 320 } 321 } 322 323