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; |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 20 | import android.content.Context; |
Jim Miller | 39d4b06 | 2011-01-11 20:30:20 -0800 | [diff] [blame] | 21 | import android.graphics.Canvas; |
| 22 | import android.graphics.ColorFilter; |
| 23 | import android.graphics.PixelFormat; |
| 24 | import android.graphics.PorterDuff; |
| 25 | import android.graphics.drawable.Drawable; |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 26 | import android.media.AudioManager; |
Jean-Michel Trivi | c680222 | 2012-04-30 11:15:03 -0700 | [diff] [blame] | 27 | import android.media.IAudioService; |
| 28 | import android.os.RemoteException; |
| 29 | import android.os.ServiceManager; |
The Android Open Source Project | bc8d29f | 2009-03-05 20:00:44 -0800 | [diff] [blame] | 30 | import android.telephony.TelephonyManager; |
Karl Rosaen | ad29734 | 2009-03-24 18:55:19 -0700 | [diff] [blame] | 31 | import android.util.AttributeSet; |
Jean-Michel Trivi | c680222 | 2012-04-30 11:15:03 -0700 | [diff] [blame] | 32 | import android.util.Log; |
| 33 | import android.util.Slog; |
Jim Miller | 838906b | 2012-10-19 18:41:25 -0700 | [diff] [blame] | 34 | import android.view.KeyEvent; |
| 35 | import android.widget.FrameLayout; |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 36 | |
| 37 | /** |
Jim Miller | dcb3d84 | 2012-08-23 19:18:12 -0700 | [diff] [blame] | 38 | * 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] | 39 | * reset the state of your view. Use the {@link KeyguardViewCallback} via |
| 40 | * {@link #getCallback()} to send information back (such as poking the wake lock, |
| 41 | * or finishing the keyguard). |
| 42 | * |
| 43 | * Handles intercepting of media keys that still work when the keyguard is |
| 44 | * showing. |
| 45 | */ |
Jim Miller | 838906b | 2012-10-19 18:41:25 -0700 | [diff] [blame] | 46 | public abstract class KeyguardViewBase extends FrameLayout { |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 47 | |
Jim Miller | 39d4b06 | 2011-01-11 20:30:20 -0800 | [diff] [blame] | 48 | private static final int BACKGROUND_COLOR = 0x70000000; |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 49 | private AudioManager mAudioManager; |
The Android Open Source Project | bc8d29f | 2009-03-05 20:00:44 -0800 | [diff] [blame] | 50 | private TelephonyManager mTelephonyManager = null; |
Jim Miller | dcb3d84 | 2012-08-23 19:18:12 -0700 | [diff] [blame] | 51 | protected KeyguardViewMediator.ViewMediatorCallback mViewMediatorCallback; |
| 52 | |
Amith Yamasani | 2ef6f1b | 2011-12-01 14:01:30 -0800 | [diff] [blame] | 53 | // Whether the volume keys should be handled by keyguard. If true, then |
| 54 | // they will be handled here for specific media types such as music, otherwise |
| 55 | // the audio service will bring up the volume dialog. |
Amith Yamasani | 6243edd | 2011-12-05 19:58:48 -0800 | [diff] [blame] | 56 | private static final boolean KEYGUARD_MANAGES_VOLUME = true; |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 57 | |
Jim Miller | 2928c9d | 2011-10-20 17:00:38 -0700 | [diff] [blame] | 58 | // This is a faster way to draw the background on devices without hardware acceleration |
Jim Miller | dcb3d84 | 2012-08-23 19:18:12 -0700 | [diff] [blame] | 59 | private static final Drawable mBackgroundDrawable = new Drawable() { |
Jim Miller | 2928c9d | 2011-10-20 17:00:38 -0700 | [diff] [blame] | 60 | @Override |
| 61 | public void draw(Canvas canvas) { |
| 62 | canvas.drawColor(BACKGROUND_COLOR, PorterDuff.Mode.SRC); |
| 63 | } |
| 64 | |
| 65 | @Override |
| 66 | public void setAlpha(int alpha) { |
| 67 | } |
| 68 | |
| 69 | @Override |
| 70 | public void setColorFilter(ColorFilter cf) { |
| 71 | } |
| 72 | |
| 73 | @Override |
| 74 | public int getOpacity() { |
| 75 | return PixelFormat.TRANSLUCENT; |
| 76 | } |
| 77 | }; |
| 78 | |
Jim Miller | dcb3d84 | 2012-08-23 19:18:12 -0700 | [diff] [blame] | 79 | public KeyguardViewBase(Context context) { |
| 80 | this(context, null); |
| 81 | } |
| 82 | |
| 83 | public KeyguardViewBase(Context context, AttributeSet attrs) { |
| 84 | super(context, attrs); |
Jim Miller | 2928c9d | 2011-10-20 17:00:38 -0700 | [diff] [blame] | 85 | resetBackground(); |
| 86 | } |
Jim Miller | 39d4b06 | 2011-01-11 20:30:20 -0800 | [diff] [blame] | 87 | |
Jim Miller | 2928c9d | 2011-10-20 17:00:38 -0700 | [diff] [blame] | 88 | public void resetBackground() { |
Jim Miller | dcb3d84 | 2012-08-23 19:18:12 -0700 | [diff] [blame] | 89 | setBackground(mBackgroundDrawable); |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | /** |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 93 | * Called when the screen turned off. |
| 94 | */ |
| 95 | abstract public void onScreenTurnedOff(); |
| 96 | |
| 97 | /** |
| 98 | * Called when the screen turned on. |
| 99 | */ |
| 100 | abstract public void onScreenTurnedOn(); |
| 101 | |
| 102 | /** |
Brian Colonna | 4284e9d | 2011-09-28 12:08:58 -0400 | [diff] [blame] | 103 | * Called when the view needs to be shown. |
| 104 | */ |
| 105 | abstract public void show(); |
| 106 | |
| 107 | /** |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 108 | * Verify that the user can get past the keyguard securely. This is called, |
| 109 | * for example, when the phone disables the keyguard but then wants to launch |
| 110 | * something else that requires secure access. |
| 111 | * |
| 112 | * The result will be propogated back via {@link KeyguardViewCallback#keyguardDone(boolean)} |
| 113 | */ |
| 114 | abstract public void verifyUnlock(); |
| 115 | |
| 116 | /** |
| 117 | * Called before this view is being removed. |
| 118 | */ |
| 119 | abstract public void cleanUp(); |
| 120 | |
Jeff Brown | c7505bc | 2012-10-05 21:58:15 -0700 | [diff] [blame] | 121 | /** |
| 122 | * Gets the desired user activity timeout in milliseconds, or -1 if the |
| 123 | * default should be used. |
| 124 | */ |
| 125 | abstract public long getUserActivityTimeout(); |
| 126 | |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 127 | @Override |
| 128 | public boolean dispatchKeyEvent(KeyEvent event) { |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 129 | if (interceptMediaKey(event)) { |
| 130 | return true; |
| 131 | } |
| 132 | return super.dispatchKeyEvent(event); |
| 133 | } |
| 134 | |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 135 | /** |
Marco Nelissen | 24d1056 | 2009-05-12 14:15:17 -0700 | [diff] [blame] | 136 | * Allows the media keys to work when the keyguard is showing. |
| 137 | * 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] | 138 | * so intercepting them here should not be of any harm. |
| 139 | * @param event The key event |
| 140 | * @return whether the event was consumed as a media key. |
| 141 | */ |
| 142 | private boolean interceptMediaKey(KeyEvent event) { |
| 143 | final int keyCode = event.getKeyCode(); |
| 144 | if (event.getAction() == KeyEvent.ACTION_DOWN) { |
| 145 | switch (keyCode) { |
Marco Nelissen | a6face4 | 2010-10-25 10:21:59 -0700 | [diff] [blame] | 146 | case KeyEvent.KEYCODE_MEDIA_PLAY: |
| 147 | case KeyEvent.KEYCODE_MEDIA_PAUSE: |
Andy Stadler | 8b89d69 | 2009-04-10 16:24:49 -0700 | [diff] [blame] | 148 | case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE: |
Jeff Brown | 4d39605 | 2010-10-29 21:50:21 -0700 | [diff] [blame] | 149 | /* Suppress PLAY/PAUSE toggle when phone is ringing or |
The Android Open Source Project | bc8d29f | 2009-03-05 20:00:44 -0800 | [diff] [blame] | 150 | * in-call to avoid music playback */ |
| 151 | if (mTelephonyManager == null) { |
| 152 | mTelephonyManager = (TelephonyManager) getContext().getSystemService( |
| 153 | Context.TELEPHONY_SERVICE); |
| 154 | } |
| 155 | if (mTelephonyManager != null && |
| 156 | mTelephonyManager.getCallState() != TelephonyManager.CALL_STATE_IDLE) { |
| 157 | return true; // suppress key event |
| 158 | } |
Jeff Brown | 4d39605 | 2010-10-29 21:50:21 -0700 | [diff] [blame] | 159 | case KeyEvent.KEYCODE_MUTE: |
| 160 | case KeyEvent.KEYCODE_HEADSETHOOK: |
| 161 | case KeyEvent.KEYCODE_MEDIA_STOP: |
| 162 | case KeyEvent.KEYCODE_MEDIA_NEXT: |
| 163 | case KeyEvent.KEYCODE_MEDIA_PREVIOUS: |
| 164 | case KeyEvent.KEYCODE_MEDIA_REWIND: |
| 165 | case KeyEvent.KEYCODE_MEDIA_RECORD: |
Jaekyun Seok | bfdad8e | 2013-07-08 13:53:21 +0900 | [diff] [blame] | 166 | case KeyEvent.KEYCODE_MEDIA_FAST_FORWARD: |
| 167 | case KeyEvent.KEYCODE_MEDIA_AUDIO_TRACK: { |
Jean-Michel Trivi | c680222 | 2012-04-30 11:15:03 -0700 | [diff] [blame] | 168 | handleMediaKeyEvent(event); |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 169 | return true; |
| 170 | } |
| 171 | |
| 172 | case KeyEvent.KEYCODE_VOLUME_UP: |
Jeff Brown | b0418da | 2010-11-01 15:24:01 -0700 | [diff] [blame] | 173 | case KeyEvent.KEYCODE_VOLUME_DOWN: |
| 174 | case KeyEvent.KEYCODE_VOLUME_MUTE: { |
Amith Yamasani | 2ef6f1b | 2011-12-01 14:01:30 -0800 | [diff] [blame] | 175 | if (KEYGUARD_MANAGES_VOLUME) { |
| 176 | synchronized (this) { |
| 177 | if (mAudioManager == null) { |
| 178 | mAudioManager = (AudioManager) getContext().getSystemService( |
| 179 | Context.AUDIO_SERVICE); |
| 180 | } |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 181 | } |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 182 | // Volume buttons should only function for music (local or remote). |
| 183 | // TODO: Actually handle MUTE. |
| 184 | mAudioManager.adjustLocalOrRemoteStreamVolume( |
| 185 | AudioManager.STREAM_MUSIC, |
| 186 | keyCode == KeyEvent.KEYCODE_VOLUME_UP |
| 187 | ? AudioManager.ADJUST_RAISE |
| 188 | : AudioManager.ADJUST_LOWER); |
Amith Yamasani | 2ef6f1b | 2011-12-01 14:01:30 -0800 | [diff] [blame] | 189 | // Don't execute default volume behavior |
| 190 | return true; |
| 191 | } else { |
| 192 | return false; |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 193 | } |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 194 | } |
| 195 | } |
| 196 | } else if (event.getAction() == KeyEvent.ACTION_UP) { |
| 197 | switch (keyCode) { |
| 198 | case KeyEvent.KEYCODE_MUTE: |
Jeff Brown | 4d39605 | 2010-10-29 21:50:21 -0700 | [diff] [blame] | 199 | case KeyEvent.KEYCODE_HEADSETHOOK: |
| 200 | case KeyEvent.KEYCODE_MEDIA_PLAY: |
| 201 | case KeyEvent.KEYCODE_MEDIA_PAUSE: |
| 202 | case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE: |
| 203 | case KeyEvent.KEYCODE_MEDIA_STOP: |
| 204 | case KeyEvent.KEYCODE_MEDIA_NEXT: |
| 205 | case KeyEvent.KEYCODE_MEDIA_PREVIOUS: |
| 206 | case KeyEvent.KEYCODE_MEDIA_REWIND: |
| 207 | case KeyEvent.KEYCODE_MEDIA_RECORD: |
Jaekyun Seok | bfdad8e | 2013-07-08 13:53:21 +0900 | [diff] [blame] | 208 | case KeyEvent.KEYCODE_MEDIA_FAST_FORWARD: |
| 209 | case KeyEvent.KEYCODE_MEDIA_AUDIO_TRACK: { |
Jean-Michel Trivi | c680222 | 2012-04-30 11:15:03 -0700 | [diff] [blame] | 210 | handleMediaKeyEvent(event); |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 211 | return true; |
| 212 | } |
| 213 | } |
| 214 | } |
| 215 | return false; |
| 216 | } |
| 217 | |
Jean-Michel Trivi | c680222 | 2012-04-30 11:15:03 -0700 | [diff] [blame] | 218 | void handleMediaKeyEvent(KeyEvent keyEvent) { |
| 219 | IAudioService audioService = IAudioService.Stub.asInterface( |
| 220 | ServiceManager.checkService(Context.AUDIO_SERVICE)); |
| 221 | if (audioService != null) { |
| 222 | try { |
| 223 | audioService.dispatchMediaKeyEvent(keyEvent); |
| 224 | } catch (RemoteException e) { |
| 225 | Log.e("KeyguardViewBase", "dispatchMediaKeyEvent threw exception " + e); |
| 226 | } |
| 227 | } else { |
| 228 | Slog.w("KeyguardViewBase", "Unable to find IAudioService for media key event"); |
| 229 | } |
| 230 | } |
| 231 | |
Joe Onorato | 4671ce5 | 2011-01-27 21:15:42 -0800 | [diff] [blame] | 232 | @Override |
| 233 | public void dispatchSystemUiVisibilityChanged(int visibility) { |
| 234 | super.dispatchSystemUiVisibilityChanged(visibility); |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 235 | |
| 236 | if (!(mContext instanceof Activity)) { |
| 237 | setSystemUiVisibility(STATUS_BAR_DISABLE_BACK); |
| 238 | } |
Joe Onorato | 4671ce5 | 2011-01-27 21:15:42 -0800 | [diff] [blame] | 239 | } |
Jim Miller | dcb3d84 | 2012-08-23 19:18:12 -0700 | [diff] [blame] | 240 | |
| 241 | public void setViewMediatorCallback( |
| 242 | KeyguardViewMediator.ViewMediatorCallback viewMediatorCallback) { |
| 243 | mViewMediatorCallback = viewMediatorCallback; |
| 244 | } |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 245 | } |