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