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
17package com.android.internal.policy.impl;
18
19import android.content.Context;
20import android.content.Intent;
21import android.media.AudioManager;
22import android.telephony.TelephonyManager;
23import android.view.KeyEvent;
24import android.view.View;
25import android.view.Gravity;
26import android.widget.FrameLayout;
27import android.util.AttributeSet;
28
29/**
30 * Base class for keyguard views.  {@link #reset} is where you should
31 * reset the state of your view.  Use the {@link KeyguardViewCallback} via
32 * {@link #getCallback()} to send information back (such as poking the wake lock,
33 * or finishing the keyguard).
34 *
35 * Handles intercepting of media keys that still work when the keyguard is
36 * showing.
37 */
38public abstract class KeyguardViewBase extends FrameLayout {
39
40    private KeyguardViewCallback mCallback;
41    private AudioManager mAudioManager;
42    private TelephonyManager mTelephonyManager = null;
43
44    public KeyguardViewBase(Context context) {
45        super(context);
46
47        // drop shadow below status bar in keyguard too
48        mForegroundInPadding = false;
49        setForegroundGravity(Gravity.FILL_HORIZONTAL | Gravity.TOP);
50        setForeground(
51                context.getResources().getDrawable(
52                        com.android.internal.R.drawable.title_bar_shadow));
53    }
54
55    // used to inject callback
56    void setCallback(KeyguardViewCallback callback) {
57        mCallback = callback;
58    }
59
60    public KeyguardViewCallback getCallback() {
61        return mCallback;
62    }
63
64    /**
65     * Called when you need to reset the state of your view.
66     */
67    abstract public void reset();
68
69    /**
70     * Called when the screen turned off.
71     */
72    abstract public void onScreenTurnedOff();
73
74    /**
75     * Called when the screen turned on.
76     */
77    abstract public void onScreenTurnedOn();
78
79    /**
80     * Called when a key has woken the device to give us a chance to adjust our
81     * state according the the key.  We are responsible for waking the device
82     * (by poking the wake lock) once we are ready.
83     *
84     * The 'Tq' suffix is per the documentation in {@link android.view.WindowManagerPolicy}.
85     * Be sure not to take any action that takes a long time; any significant
86     * action should be posted to a handler.
87     *
88     * @param keyCode The wake key, which may be relevant for configuring the
89     *   keyguard.
90     */
91    abstract public void wakeWhenReadyTq(int keyCode);
92
93    /**
94     * Verify that the user can get past the keyguard securely.  This is called,
95     * for example, when the phone disables the keyguard but then wants to launch
96     * something else that requires secure access.
97     *
98     * The result will be propogated back via {@link KeyguardViewCallback#keyguardDone(boolean)}
99     */
100    abstract public void verifyUnlock();
101
102    /**
103     * Called before this view is being removed.
104     */
105    abstract public void cleanUp();
106
107    @Override
108    public boolean dispatchKeyEvent(KeyEvent event) {
109        if (shouldEventKeepScreenOnWhileKeyguardShowing(event)) {
110            mCallback.pokeWakelock();
111        }
112
113        if (interceptMediaKey(event)) {
114            return true;
115        }
116        return super.dispatchKeyEvent(event);
117    }
118
119    private boolean shouldEventKeepScreenOnWhileKeyguardShowing(KeyEvent event) {
120        if (event.getAction() != KeyEvent.ACTION_DOWN) {
121            return false;
122        }
123        switch (event.getKeyCode()) {
124            case KeyEvent.KEYCODE_DPAD_DOWN:
125            case KeyEvent.KEYCODE_DPAD_LEFT:
126            case KeyEvent.KEYCODE_DPAD_RIGHT:
127            case KeyEvent.KEYCODE_DPAD_UP:
128                return false;
129            default:
130                return true;
131        }
132    }
133
134    /**
135     * Allows the media keys to work when the keyguard is showing.
136     * The media keys should be of no interest to the actual keyguard view(s),
137     * so intercepting them here should not be of any harm.
138     * @param event The key event
139     * @return whether the event was consumed as a media key.
140     */
141    private boolean interceptMediaKey(KeyEvent event) {
142        final int keyCode = event.getKeyCode();
143        if (event.getAction() == KeyEvent.ACTION_DOWN) {
144            switch (keyCode) {
145                case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
146                    /* Suppress PLAYPAUSE toggle when phone is ringing or
147                     * in-call to avoid music playback */
148                    if (mTelephonyManager == null) {
149                        mTelephonyManager = (TelephonyManager) getContext().getSystemService(
150                                Context.TELEPHONY_SERVICE);
151                    }
152                    if (mTelephonyManager != null &&
153                            mTelephonyManager.getCallState() != TelephonyManager.CALL_STATE_IDLE) {
154                        return true;  // suppress key event
155                    }
156                case KeyEvent.KEYCODE_HEADSETHOOK:
157                case KeyEvent.KEYCODE_MEDIA_STOP:
158                case KeyEvent.KEYCODE_MEDIA_NEXT:
159                case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
160                case KeyEvent.KEYCODE_MEDIA_REWIND:
161                case KeyEvent.KEYCODE_MEDIA_FAST_FORWARD: {
162                    Intent intent = new Intent(Intent.ACTION_MEDIA_BUTTON, null);
163                    intent.putExtra(Intent.EXTRA_KEY_EVENT, event);
164                    getContext().sendOrderedBroadcast(intent, null);
165                    return true;
166                }
167
168                case KeyEvent.KEYCODE_VOLUME_UP:
169                case KeyEvent.KEYCODE_VOLUME_DOWN: {
170                    synchronized (this) {
171                        if (mAudioManager == null) {
172                            mAudioManager = (AudioManager) getContext().getSystemService(
173                                    Context.AUDIO_SERVICE);
174                        }
175                    }
176                    // Volume buttons should only function for music.
177                    if (mAudioManager.isMusicActive()) {
178                        mAudioManager.adjustStreamVolume(
179                                    AudioManager.STREAM_MUSIC,
180                                    keyCode == KeyEvent.KEYCODE_VOLUME_UP
181                                            ? AudioManager.ADJUST_RAISE
182                                            : AudioManager.ADJUST_LOWER,
183                                    0);
184                    }
185                    // Don't execute default volume behavior
186                    return true;
187                }
188            }
189        } else if (event.getAction() == KeyEvent.ACTION_UP) {
190            switch (keyCode) {
191                case KeyEvent.KEYCODE_MUTE:
192                case KeyEvent.KEYCODE_HEADSETHOOK:
193                case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
194                case KeyEvent.KEYCODE_MEDIA_STOP:
195                case KeyEvent.KEYCODE_MEDIA_NEXT:
196                case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
197                case KeyEvent.KEYCODE_MEDIA_REWIND:
198                case KeyEvent.KEYCODE_MEDIA_FAST_FORWARD: {
199                    Intent intent = new Intent(Intent.ACTION_MEDIA_BUTTON, null);
200                    intent.putExtra(Intent.EXTRA_KEY_EVENT, event);
201                    getContext().sendOrderedBroadcast(intent, null);
202                    return true;
203                }
204            }
205        }
206        return false;
207    }
208
209}
210