The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 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 | |
Jim Miller | 5ecd811 | 2013-01-09 18:50:26 -0800 | [diff] [blame] | 17 | package com.android.keyguard; |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 18 | |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 19 | import android.app.Activity; |
Jim Miller | 3eb4971 | 2014-01-28 18:22:42 -0800 | [diff] [blame] | 20 | import android.app.ActivityManager; |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 21 | import android.content.Context; |
Jim Miller | 3eb4971 | 2014-01-28 18:22:42 -0800 | [diff] [blame] | 22 | import android.content.res.Resources; |
| 23 | import android.graphics.Canvas; |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 24 | import android.media.AudioManager; |
Jim Miller | 3eb4971 | 2014-01-28 18:22:42 -0800 | [diff] [blame] | 25 | import android.os.SystemClock; |
The Android Open Source Project | bc8d29f | 2009-03-05 20:00:44 -0800 | [diff] [blame] | 26 | import android.telephony.TelephonyManager; |
Karl Rosaen | ad29734 | 2009-03-24 18:55:19 -0700 | [diff] [blame] | 27 | import android.util.AttributeSet; |
Jean-Michel Trivi | c680222 | 2012-04-30 11:15:03 -0700 | [diff] [blame] | 28 | import android.util.Log; |
Jim Miller | 838906b | 2012-10-19 18:41:25 -0700 | [diff] [blame] | 29 | import android.view.KeyEvent; |
Jorim Jaggi | 10c85c7 | 2015-02-02 20:45:32 +0100 | [diff] [blame] | 30 | import android.view.accessibility.AccessibilityEvent; |
Jim Miller | 838906b | 2012-10-19 18:41:25 -0700 | [diff] [blame] | 31 | import android.widget.FrameLayout; |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 32 | |
Jim Miller | 3eb4971 | 2014-01-28 18:22:42 -0800 | [diff] [blame] | 33 | import com.android.internal.widget.LockPatternUtils; |
Jim Miller | 3eb4971 | 2014-01-28 18:22:42 -0800 | [diff] [blame] | 34 | import com.android.keyguard.KeyguardSecurityContainer.SecurityCallback; |
| 35 | import com.android.keyguard.KeyguardSecurityModel.SecurityMode; |
| 36 | |
| 37 | import java.io.File; |
| 38 | |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 39 | /** |
Jim Miller | dcb3d84 | 2012-08-23 19:18:12 -0700 | [diff] [blame] | 40 | * Base class for keyguard view. {@link #reset} is where you should |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 41 | * reset the state of your view. Use the {@link KeyguardViewCallback} via |
| 42 | * {@link #getCallback()} to send information back (such as poking the wake lock, |
| 43 | * or finishing the keyguard). |
| 44 | * |
| 45 | * Handles intercepting of media keys that still work when the keyguard is |
| 46 | * showing. |
| 47 | */ |
Jorim Jaggi | 6b88cdf | 2014-12-22 20:56:50 +0100 | [diff] [blame] | 48 | public class KeyguardHostView extends FrameLayout implements SecurityCallback { |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 49 | |
Jorim Jaggi | 5c60b64 | 2014-12-19 18:13:28 +0100 | [diff] [blame] | 50 | public interface OnDismissAction { |
| 51 | /** |
| 52 | * @return true if the dismiss should be deferred |
| 53 | */ |
| 54 | boolean onDismiss(); |
| 55 | } |
| 56 | |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 57 | private AudioManager mAudioManager; |
The Android Open Source Project | bc8d29f | 2009-03-05 20:00:44 -0800 | [diff] [blame] | 58 | private TelephonyManager mTelephonyManager = null; |
Jorim Jaggi | 5cf1787 | 2014-03-26 18:31:48 +0100 | [diff] [blame] | 59 | protected ViewMediatorCallback mViewMediatorCallback; |
Jim Miller | 3eb4971 | 2014-01-28 18:22:42 -0800 | [diff] [blame] | 60 | protected LockPatternUtils mLockPatternUtils; |
| 61 | private OnDismissAction mDismissAction; |
Jim Miller | dcb3d84 | 2012-08-23 19:18:12 -0700 | [diff] [blame] | 62 | |
Jorim Jaggi | 6b88cdf | 2014-12-22 20:56:50 +0100 | [diff] [blame] | 63 | private final KeyguardUpdateMonitorCallback mUpdateCallback = |
| 64 | new KeyguardUpdateMonitorCallback() { |
| 65 | |
| 66 | @Override |
| 67 | public void onUserSwitchComplete(int userId) { |
| 68 | getSecurityContainer().showPrimarySecurityScreen(false /* turning off */); |
| 69 | } |
| 70 | |
| 71 | @Override |
| 72 | public void onTrustInitiatedByUser(int userId) { |
| 73 | if (userId != mLockPatternUtils.getCurrentUser()) return; |
| 74 | if (!isAttachedToWindow()) return; |
| 75 | |
| 76 | if (isVisibleToUser()) { |
| 77 | dismiss(false /* authenticated */); |
| 78 | } else { |
| 79 | mViewMediatorCallback.playTrustedSound(); |
| 80 | } |
| 81 | } |
| 82 | }; |
| 83 | |
Amith Yamasani | 2ef6f1b | 2011-12-01 14:01:30 -0800 | [diff] [blame] | 84 | // Whether the volume keys should be handled by keyguard. If true, then |
| 85 | // they will be handled here for specific media types such as music, otherwise |
| 86 | // the audio service will bring up the volume dialog. |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 87 | private static final boolean KEYGUARD_MANAGES_VOLUME = false; |
Jorim Jaggi | 5cf1787 | 2014-03-26 18:31:48 +0100 | [diff] [blame] | 88 | public static final boolean DEBUG = KeyguardConstants.DEBUG; |
Jim Miller | 3eb4971 | 2014-01-28 18:22:42 -0800 | [diff] [blame] | 89 | private static final String TAG = "KeyguardViewBase"; |
| 90 | |
| 91 | private KeyguardSecurityContainer mSecurityContainer; |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 92 | |
Jorim Jaggi | 6b88cdf | 2014-12-22 20:56:50 +0100 | [diff] [blame] | 93 | public KeyguardHostView(Context context) { |
Jim Miller | dcb3d84 | 2012-08-23 19:18:12 -0700 | [diff] [blame] | 94 | this(context, null); |
| 95 | } |
| 96 | |
Jorim Jaggi | 6b88cdf | 2014-12-22 20:56:50 +0100 | [diff] [blame] | 97 | public KeyguardHostView(Context context, AttributeSet attrs) { |
Jim Miller | dcb3d84 | 2012-08-23 19:18:12 -0700 | [diff] [blame] | 98 | super(context, attrs); |
Jorim Jaggi | 6b88cdf | 2014-12-22 20:56:50 +0100 | [diff] [blame] | 99 | KeyguardUpdateMonitor.getInstance(context).registerCallback(mUpdateCallback); |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 100 | } |
| 101 | |
Jim Miller | 3eb4971 | 2014-01-28 18:22:42 -0800 | [diff] [blame] | 102 | @Override |
| 103 | protected void dispatchDraw(Canvas canvas) { |
| 104 | super.dispatchDraw(canvas); |
| 105 | if (mViewMediatorCallback != null) { |
| 106 | mViewMediatorCallback.keyguardDoneDrawing(); |
| 107 | } |
| 108 | } |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 109 | |
| 110 | /** |
Jim Miller | 3eb4971 | 2014-01-28 18:22:42 -0800 | [diff] [blame] | 111 | * Sets an action to run when keyguard finishes. |
| 112 | * |
| 113 | * @param action |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 114 | */ |
Jim Miller | 3eb4971 | 2014-01-28 18:22:42 -0800 | [diff] [blame] | 115 | public void setOnDismissAction(OnDismissAction action) { |
| 116 | mDismissAction = action; |
| 117 | } |
| 118 | |
| 119 | @Override |
| 120 | protected void onFinishInflate() { |
| 121 | mSecurityContainer = |
| 122 | (KeyguardSecurityContainer) findViewById(R.id.keyguard_security_container); |
Jim Miller | 5e612cf | 2014-02-03 17:57:23 -0800 | [diff] [blame] | 123 | mLockPatternUtils = new LockPatternUtils(mContext); |
Jim Miller | 3eb4971 | 2014-01-28 18:22:42 -0800 | [diff] [blame] | 124 | mSecurityContainer.setLockPatternUtils(mLockPatternUtils); |
Jim Miller | ba7d94b | 2014-02-05 17:30:50 -0800 | [diff] [blame] | 125 | mSecurityContainer.setSecurityCallback(this); |
Jim Miller | 3eb4971 | 2014-01-28 18:22:42 -0800 | [diff] [blame] | 126 | mSecurityContainer.showPrimarySecurityScreen(false); |
| 127 | // mSecurityContainer.updateSecurityViews(false /* not bouncing */); |
Jim Miller | 3eb4971 | 2014-01-28 18:22:42 -0800 | [diff] [blame] | 128 | } |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 129 | |
| 130 | /** |
Brian Colonna | 4284e9d | 2011-09-28 12:08:58 -0400 | [diff] [blame] | 131 | * Called when the view needs to be shown. |
| 132 | */ |
Jorim Jaggi | 95e89ca | 2014-11-24 20:12:50 +0100 | [diff] [blame] | 133 | public void showPrimarySecurityScreen() { |
Jim Miller | 3eb4971 | 2014-01-28 18:22:42 -0800 | [diff] [blame] | 134 | if (DEBUG) Log.d(TAG, "show()"); |
| 135 | mSecurityContainer.showPrimarySecurityScreen(false); |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Dismisses the keyguard by going to the next screen or making it gone. |
Jorim Jaggi | 0bed7f2 | 2014-04-03 16:12:54 +0200 | [diff] [blame] | 140 | * |
| 141 | * @return True if the keyguard is done. |
Jim Miller | 3eb4971 | 2014-01-28 18:22:42 -0800 | [diff] [blame] | 142 | */ |
Jorim Jaggi | 0bed7f2 | 2014-04-03 16:12:54 +0200 | [diff] [blame] | 143 | public boolean dismiss() { |
| 144 | return dismiss(false); |
Jim Miller | 3eb4971 | 2014-01-28 18:22:42 -0800 | [diff] [blame] | 145 | } |
| 146 | |
Jim Miller | 3eb4971 | 2014-01-28 18:22:42 -0800 | [diff] [blame] | 147 | public boolean handleBackKey() { |
Jim Miller | 3eb4971 | 2014-01-28 18:22:42 -0800 | [diff] [blame] | 148 | if (mSecurityContainer.getCurrentSecuritySelection() != SecurityMode.None) { |
| 149 | mSecurityContainer.dismiss(false); |
| 150 | return true; |
| 151 | } |
| 152 | return false; |
| 153 | } |
| 154 | |
Jorim Jaggi | 10c85c7 | 2015-02-02 20:45:32 +0100 | [diff] [blame] | 155 | @Override |
| 156 | public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) { |
| 157 | if (event.getEventType() == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) { |
| 158 | event.getText().add(mSecurityContainer.getCurrentSecurityModeContentDescription()); |
| 159 | return true; |
Jim Miller | 9ccf123 | 2013-10-10 22:23:07 -0700 | [diff] [blame] | 160 | } else { |
Jorim Jaggi | 10c85c7 | 2015-02-02 20:45:32 +0100 | [diff] [blame] | 161 | return super.dispatchPopulateAccessibilityEvent(event); |
Jim Miller | 9ccf123 | 2013-10-10 22:23:07 -0700 | [diff] [blame] | 162 | } |
Jim Miller | e46efc0 | 2012-09-07 16:43:51 -0700 | [diff] [blame] | 163 | } |
Jim Miller | dcb3d84 | 2012-08-23 19:18:12 -0700 | [diff] [blame] | 164 | |
Jim Miller | ba7d94b | 2014-02-05 17:30:50 -0800 | [diff] [blame] | 165 | protected KeyguardSecurityContainer getSecurityContainer() { |
| 166 | return mSecurityContainer; |
| 167 | } |
Jim Miller | 3eb4971 | 2014-01-28 18:22:42 -0800 | [diff] [blame] | 168 | |
Jim Miller | ba7d94b | 2014-02-05 17:30:50 -0800 | [diff] [blame] | 169 | @Override |
| 170 | public boolean dismiss(boolean authenticated) { |
| 171 | return mSecurityContainer.showNextSecurityScreenOrFinish(authenticated); |
Jim Miller | 3eb4971 | 2014-01-28 18:22:42 -0800 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Authentication has happened and it's time to dismiss keyguard. This function |
| 176 | * should clean up and inform KeyguardViewMediator. |
| 177 | */ |
Jim Miller | ba7d94b | 2014-02-05 17:30:50 -0800 | [diff] [blame] | 178 | @Override |
Jim Miller | 3eb4971 | 2014-01-28 18:22:42 -0800 | [diff] [blame] | 179 | public void finish() { |
Jim Miller | 3eb4971 | 2014-01-28 18:22:42 -0800 | [diff] [blame] | 180 | // If there's a pending runnable because the user interacted with a widget |
| 181 | // and we're leaving keyguard, then run it. |
| 182 | boolean deferKeyguardDone = false; |
| 183 | if (mDismissAction != null) { |
| 184 | deferKeyguardDone = mDismissAction.onDismiss(); |
| 185 | mDismissAction = null; |
| 186 | } |
| 187 | if (mViewMediatorCallback != null) { |
| 188 | if (deferKeyguardDone) { |
| 189 | mViewMediatorCallback.keyguardDonePending(); |
| 190 | } else { |
| 191 | mViewMediatorCallback.keyguardDone(true); |
| 192 | } |
| 193 | } |
| 194 | } |
| 195 | |
Jim Miller | ba7d94b | 2014-02-05 17:30:50 -0800 | [diff] [blame] | 196 | @Override |
Andrew Lee | 72b46d4 | 2015-01-30 13:23:21 -0800 | [diff] [blame] | 197 | public void reset() { |
| 198 | mViewMediatorCallback.resetKeyguard(); |
| 199 | } |
| 200 | |
| 201 | @Override |
Jim Miller | ba7d94b | 2014-02-05 17:30:50 -0800 | [diff] [blame] | 202 | public void onSecurityModeChanged(SecurityMode securityMode, boolean needsInput) { |
Jim Miller | 3eb4971 | 2014-01-28 18:22:42 -0800 | [diff] [blame] | 203 | if (mViewMediatorCallback != null) { |
Jim Miller | ba7d94b | 2014-02-05 17:30:50 -0800 | [diff] [blame] | 204 | mViewMediatorCallback.setNeedsInput(needsInput); |
Jim Miller | 3eb4971 | 2014-01-28 18:22:42 -0800 | [diff] [blame] | 205 | } |
| 206 | } |
| 207 | |
Jim Miller | ba7d94b | 2014-02-05 17:30:50 -0800 | [diff] [blame] | 208 | public void userActivity() { |
Jim Miller | 3eb4971 | 2014-01-28 18:22:42 -0800 | [diff] [blame] | 209 | if (mViewMediatorCallback != null) { |
| 210 | mViewMediatorCallback.userActivity(); |
| 211 | } |
| 212 | } |
| 213 | |
Jim Miller | 3eb4971 | 2014-01-28 18:22:42 -0800 | [diff] [blame] | 214 | /** |
Jorim Jaggi | a005f1b | 2014-04-16 19:06:10 +0200 | [diff] [blame] | 215 | * Called when the Keyguard is not actively shown anymore on the screen. |
Jim Miller | 3eb4971 | 2014-01-28 18:22:42 -0800 | [diff] [blame] | 216 | */ |
Jorim Jaggi | a005f1b | 2014-04-16 19:06:10 +0200 | [diff] [blame] | 217 | public void onPause() { |
Jim Miller | 3eb4971 | 2014-01-28 18:22:42 -0800 | [diff] [blame] | 218 | if (DEBUG) Log.d(TAG, String.format("screen off, instance %s at %s", |
| 219 | Integer.toHexString(hashCode()), SystemClock.uptimeMillis())); |
Jim Miller | 3eb4971 | 2014-01-28 18:22:42 -0800 | [diff] [blame] | 220 | mSecurityContainer.showPrimarySecurityScreen(true); |
| 221 | mSecurityContainer.onPause(); |
| 222 | clearFocus(); |
| 223 | } |
| 224 | |
| 225 | /** |
Jorim Jaggi | a005f1b | 2014-04-16 19:06:10 +0200 | [diff] [blame] | 226 | * Called when the Keyguard is actively shown on the screen. |
Jim Miller | 3eb4971 | 2014-01-28 18:22:42 -0800 | [diff] [blame] | 227 | */ |
Jorim Jaggi | a005f1b | 2014-04-16 19:06:10 +0200 | [diff] [blame] | 228 | public void onResume() { |
Jim Miller | 3eb4971 | 2014-01-28 18:22:42 -0800 | [diff] [blame] | 229 | if (DEBUG) Log.d(TAG, "screen on, instance " + Integer.toHexString(hashCode())); |
Jim Miller | 3eb4971 | 2014-01-28 18:22:42 -0800 | [diff] [blame] | 230 | mSecurityContainer.onResume(KeyguardSecurityView.SCREEN_ON); |
Jim Miller | 3eb4971 | 2014-01-28 18:22:42 -0800 | [diff] [blame] | 231 | requestFocus(); |
| 232 | } |
Brian Colonna | 4284e9d | 2011-09-28 12:08:58 -0400 | [diff] [blame] | 233 | |
| 234 | /** |
Jorim Jaggi | c14f829 | 2014-05-27 02:25:45 +0200 | [diff] [blame] | 235 | * Starts the animation when the Keyguard gets shown. |
| 236 | */ |
| 237 | public void startAppearAnimation() { |
| 238 | mSecurityContainer.startAppearAnimation(); |
| 239 | } |
| 240 | |
Jorim Jaggi | 76a1623 | 2014-08-08 17:00:47 +0200 | [diff] [blame] | 241 | public void startDisappearAnimation(Runnable finishRunnable) { |
Jorim Jaggi | 8de4311c | 2014-08-11 22:36:20 +0200 | [diff] [blame] | 242 | if (!mSecurityContainer.startDisappearAnimation(finishRunnable) && finishRunnable != null) { |
Jorim Jaggi | 76a1623 | 2014-08-08 17:00:47 +0200 | [diff] [blame] | 243 | finishRunnable.run(); |
| 244 | } |
| 245 | } |
| 246 | |
Jorim Jaggi | c14f829 | 2014-05-27 02:25:45 +0200 | [diff] [blame] | 247 | /** |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 248 | * Verify that the user can get past the keyguard securely. This is called, |
| 249 | * for example, when the phone disables the keyguard but then wants to launch |
| 250 | * something else that requires secure access. |
| 251 | * |
| 252 | * The result will be propogated back via {@link KeyguardViewCallback#keyguardDone(boolean)} |
| 253 | */ |
Jim Miller | 3eb4971 | 2014-01-28 18:22:42 -0800 | [diff] [blame] | 254 | public void verifyUnlock() { |
Jim Miller | ba7d94b | 2014-02-05 17:30:50 -0800 | [diff] [blame] | 255 | SecurityMode securityMode = mSecurityContainer.getSecurityMode(); |
Jim Miller | 3eb4971 | 2014-01-28 18:22:42 -0800 | [diff] [blame] | 256 | if (securityMode == KeyguardSecurityModel.SecurityMode.None) { |
| 257 | if (mViewMediatorCallback != null) { |
| 258 | mViewMediatorCallback.keyguardDone(true); |
| 259 | } |
| 260 | } else if (securityMode != KeyguardSecurityModel.SecurityMode.Pattern |
| 261 | && securityMode != KeyguardSecurityModel.SecurityMode.PIN |
| 262 | && securityMode != KeyguardSecurityModel.SecurityMode.Password) { |
| 263 | // can only verify unlock when in pattern/password mode |
| 264 | if (mViewMediatorCallback != null) { |
| 265 | mViewMediatorCallback.keyguardDone(false); |
| 266 | } |
| 267 | } else { |
| 268 | // otherwise, go to the unlock screen, see if they can verify it |
| 269 | mSecurityContainer.verifyUnlock(); |
| 270 | } |
| 271 | } |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 272 | |
| 273 | /** |
| 274 | * Called before this view is being removed. |
| 275 | */ |
Jorim Jaggi | 6b88cdf | 2014-12-22 20:56:50 +0100 | [diff] [blame] | 276 | public void cleanUp() { |
| 277 | getSecurityContainer().onPause(); |
| 278 | } |
Jeff Brown | c7505bc | 2012-10-05 21:58:15 -0700 | [diff] [blame] | 279 | |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 280 | @Override |
| 281 | public boolean dispatchKeyEvent(KeyEvent event) { |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 282 | if (interceptMediaKey(event)) { |
| 283 | return true; |
| 284 | } |
| 285 | return super.dispatchKeyEvent(event); |
| 286 | } |
| 287 | |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 288 | /** |
Marco Nelissen | 24d1056 | 2009-05-12 14:15:17 -0700 | [diff] [blame] | 289 | * Allows the media keys to work when the keyguard is showing. |
| 290 | * The media keys should be of no interest to the actual keyguard view(s), |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 291 | * so intercepting them here should not be of any harm. |
| 292 | * @param event The key event |
| 293 | * @return whether the event was consumed as a media key. |
| 294 | */ |
Jorim Jaggi | df99351 | 2014-05-13 23:06:35 +0200 | [diff] [blame] | 295 | public boolean interceptMediaKey(KeyEvent event) { |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 296 | final int keyCode = event.getKeyCode(); |
| 297 | if (event.getAction() == KeyEvent.ACTION_DOWN) { |
| 298 | switch (keyCode) { |
Marco Nelissen | a6face4 | 2010-10-25 10:21:59 -0700 | [diff] [blame] | 299 | case KeyEvent.KEYCODE_MEDIA_PLAY: |
| 300 | case KeyEvent.KEYCODE_MEDIA_PAUSE: |
Andy Stadler | 8b89d69 | 2009-04-10 16:24:49 -0700 | [diff] [blame] | 301 | case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE: |
Jeff Brown | 4d39605 | 2010-10-29 21:50:21 -0700 | [diff] [blame] | 302 | /* Suppress PLAY/PAUSE toggle when phone is ringing or |
The Android Open Source Project | bc8d29f | 2009-03-05 20:00:44 -0800 | [diff] [blame] | 303 | * in-call to avoid music playback */ |
| 304 | if (mTelephonyManager == null) { |
| 305 | mTelephonyManager = (TelephonyManager) getContext().getSystemService( |
| 306 | Context.TELEPHONY_SERVICE); |
| 307 | } |
| 308 | if (mTelephonyManager != null && |
| 309 | mTelephonyManager.getCallState() != TelephonyManager.CALL_STATE_IDLE) { |
| 310 | return true; // suppress key event |
| 311 | } |
Jeff Brown | 4d39605 | 2010-10-29 21:50:21 -0700 | [diff] [blame] | 312 | case KeyEvent.KEYCODE_MUTE: |
| 313 | case KeyEvent.KEYCODE_HEADSETHOOK: |
| 314 | case KeyEvent.KEYCODE_MEDIA_STOP: |
| 315 | case KeyEvent.KEYCODE_MEDIA_NEXT: |
| 316 | case KeyEvent.KEYCODE_MEDIA_PREVIOUS: |
| 317 | case KeyEvent.KEYCODE_MEDIA_REWIND: |
| 318 | case KeyEvent.KEYCODE_MEDIA_RECORD: |
Jaekyun Seok | bfdad8e | 2013-07-08 13:53:21 +0900 | [diff] [blame] | 319 | case KeyEvent.KEYCODE_MEDIA_FAST_FORWARD: |
| 320 | case KeyEvent.KEYCODE_MEDIA_AUDIO_TRACK: { |
Jean-Michel Trivi | c680222 | 2012-04-30 11:15:03 -0700 | [diff] [blame] | 321 | handleMediaKeyEvent(event); |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 322 | return true; |
| 323 | } |
| 324 | |
| 325 | case KeyEvent.KEYCODE_VOLUME_UP: |
Jeff Brown | b0418da | 2010-11-01 15:24:01 -0700 | [diff] [blame] | 326 | case KeyEvent.KEYCODE_VOLUME_DOWN: |
| 327 | case KeyEvent.KEYCODE_VOLUME_MUTE: { |
Amith Yamasani | 2ef6f1b | 2011-12-01 14:01:30 -0800 | [diff] [blame] | 328 | if (KEYGUARD_MANAGES_VOLUME) { |
| 329 | synchronized (this) { |
| 330 | if (mAudioManager == null) { |
| 331 | mAudioManager = (AudioManager) getContext().getSystemService( |
| 332 | Context.AUDIO_SERVICE); |
| 333 | } |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 334 | } |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 335 | // Volume buttons should only function for music (local or remote). |
| 336 | // TODO: Actually handle MUTE. |
RoboErik | d3c8642 | 2014-06-16 14:00:48 -0700 | [diff] [blame] | 337 | mAudioManager.adjustSuggestedStreamVolume( |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 338 | keyCode == KeyEvent.KEYCODE_VOLUME_UP |
| 339 | ? AudioManager.ADJUST_RAISE |
RoboErik | d3c8642 | 2014-06-16 14:00:48 -0700 | [diff] [blame] | 340 | : AudioManager.ADJUST_LOWER /* direction */, |
| 341 | AudioManager.STREAM_MUSIC /* stream */, 0 /* flags */); |
Amith Yamasani | 2ef6f1b | 2011-12-01 14:01:30 -0800 | [diff] [blame] | 342 | // Don't execute default volume behavior |
| 343 | return true; |
| 344 | } else { |
| 345 | return false; |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 346 | } |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 347 | } |
| 348 | } |
| 349 | } else if (event.getAction() == KeyEvent.ACTION_UP) { |
| 350 | switch (keyCode) { |
| 351 | case KeyEvent.KEYCODE_MUTE: |
Jeff Brown | 4d39605 | 2010-10-29 21:50:21 -0700 | [diff] [blame] | 352 | case KeyEvent.KEYCODE_HEADSETHOOK: |
| 353 | case KeyEvent.KEYCODE_MEDIA_PLAY: |
| 354 | case KeyEvent.KEYCODE_MEDIA_PAUSE: |
| 355 | case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE: |
| 356 | case KeyEvent.KEYCODE_MEDIA_STOP: |
| 357 | case KeyEvent.KEYCODE_MEDIA_NEXT: |
| 358 | case KeyEvent.KEYCODE_MEDIA_PREVIOUS: |
| 359 | case KeyEvent.KEYCODE_MEDIA_REWIND: |
| 360 | case KeyEvent.KEYCODE_MEDIA_RECORD: |
Jaekyun Seok | bfdad8e | 2013-07-08 13:53:21 +0900 | [diff] [blame] | 361 | case KeyEvent.KEYCODE_MEDIA_FAST_FORWARD: |
| 362 | case KeyEvent.KEYCODE_MEDIA_AUDIO_TRACK: { |
Jean-Michel Trivi | c680222 | 2012-04-30 11:15:03 -0700 | [diff] [blame] | 363 | handleMediaKeyEvent(event); |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 364 | return true; |
| 365 | } |
| 366 | } |
| 367 | } |
| 368 | return false; |
| 369 | } |
| 370 | |
Jim Miller | 3eb4971 | 2014-01-28 18:22:42 -0800 | [diff] [blame] | 371 | private void handleMediaKeyEvent(KeyEvent keyEvent) { |
RoboErik | d3c8642 | 2014-06-16 14:00:48 -0700 | [diff] [blame] | 372 | synchronized (this) { |
| 373 | if (mAudioManager == null) { |
| 374 | mAudioManager = (AudioManager) getContext().getSystemService( |
| 375 | Context.AUDIO_SERVICE); |
Jean-Michel Trivi | c680222 | 2012-04-30 11:15:03 -0700 | [diff] [blame] | 376 | } |
Jean-Michel Trivi | c680222 | 2012-04-30 11:15:03 -0700 | [diff] [blame] | 377 | } |
RoboErik | d3c8642 | 2014-06-16 14:00:48 -0700 | [diff] [blame] | 378 | mAudioManager.dispatchMediaKeyEvent(keyEvent); |
Jean-Michel Trivi | c680222 | 2012-04-30 11:15:03 -0700 | [diff] [blame] | 379 | } |
| 380 | |
Joe Onorato | 4671ce5 | 2011-01-27 21:15:42 -0800 | [diff] [blame] | 381 | @Override |
| 382 | public void dispatchSystemUiVisibilityChanged(int visibility) { |
| 383 | super.dispatchSystemUiVisibilityChanged(visibility); |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 384 | |
| 385 | if (!(mContext instanceof Activity)) { |
| 386 | setSystemUiVisibility(STATUS_BAR_DISABLE_BACK); |
| 387 | } |
Joe Onorato | 4671ce5 | 2011-01-27 21:15:42 -0800 | [diff] [blame] | 388 | } |
Jim Miller | dcb3d84 | 2012-08-23 19:18:12 -0700 | [diff] [blame] | 389 | |
Jim Miller | 3eb4971 | 2014-01-28 18:22:42 -0800 | [diff] [blame] | 390 | /** |
| 391 | * In general, we enable unlocking the insecure keyguard with the menu key. However, there are |
| 392 | * some cases where we wish to disable it, notably when the menu button placement or technology |
| 393 | * is prone to false positives. |
| 394 | * |
| 395 | * @return true if the menu key should be enabled |
| 396 | */ |
| 397 | private static final String ENABLE_MENU_KEY_FILE = "/data/local/enable_menu_key"; |
| 398 | private boolean shouldEnableMenuKey() { |
| 399 | final Resources res = getResources(); |
| 400 | final boolean configDisabled = res.getBoolean(R.bool.config_disableMenuKeyInLockScreen); |
| 401 | final boolean isTestHarness = ActivityManager.isRunningInTestHarness(); |
| 402 | final boolean fileOverride = (new File(ENABLE_MENU_KEY_FILE)).exists(); |
| 403 | return !configDisabled || isTestHarness || fileOverride; |
| 404 | } |
| 405 | |
| 406 | public boolean handleMenuKey() { |
| 407 | // The following enables the MENU key to work for testing automation |
| 408 | if (shouldEnableMenuKey()) { |
| 409 | dismiss(); |
| 410 | return true; |
| 411 | } |
| 412 | return false; |
| 413 | } |
| 414 | |
Jorim Jaggi | 5cf1787 | 2014-03-26 18:31:48 +0100 | [diff] [blame] | 415 | public void setViewMediatorCallback(ViewMediatorCallback viewMediatorCallback) { |
Jim Miller | dcb3d84 | 2012-08-23 19:18:12 -0700 | [diff] [blame] | 416 | mViewMediatorCallback = viewMediatorCallback; |
Jim Miller | ba7d94b | 2014-02-05 17:30:50 -0800 | [diff] [blame] | 417 | // Update ViewMediator with the current input method requirements |
| 418 | mViewMediatorCallback.setNeedsInput(mSecurityContainer.needsInput()); |
Jim Miller | dcb3d84 | 2012-08-23 19:18:12 -0700 | [diff] [blame] | 419 | } |
Jim Miller | 3eb4971 | 2014-01-28 18:22:42 -0800 | [diff] [blame] | 420 | |
Jorim Jaggi | 5cf1787 | 2014-03-26 18:31:48 +0100 | [diff] [blame] | 421 | public void setLockPatternUtils(LockPatternUtils utils) { |
Jim Miller | 5e612cf | 2014-02-03 17:57:23 -0800 | [diff] [blame] | 422 | mLockPatternUtils = utils; |
| 423 | mSecurityContainer.setLockPatternUtils(utils); |
| 424 | } |
| 425 | |
Jorim Jaggi | a005f1b | 2014-04-16 19:06:10 +0200 | [diff] [blame] | 426 | public SecurityMode getSecurityMode() { |
| 427 | return mSecurityContainer.getSecurityMode(); |
| 428 | } |
| 429 | |
Jorim Jaggi | 95e89ca | 2014-11-24 20:12:50 +0100 | [diff] [blame] | 430 | public SecurityMode getCurrentSecurityMode() { |
| 431 | return mSecurityContainer.getCurrentSecurityMode(); |
| 432 | } |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 433 | } |