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