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.os.UserHandle;
30import android.telephony.TelephonyManager;
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.sendOrderedBroadcastAsUser(intent, UserHandle.CURRENT_OR_SELF,
146                            null, null, null, 0, null, null);
147                }
148                return true;
149            }
150
151            case KeyEvent.KEYCODE_SEARCH: {
152                if (getKeyguardManager().inKeyguardRestrictedInputMode() || dispatcher == null) {
153                    break;
154                }
155                if (event.getRepeatCount() == 0) {
156                    dispatcher.startTracking(event, this);
157                } else if (event.isLongPress() && dispatcher.isTracking(event)) {
158                    Configuration config = mContext.getResources().getConfiguration();
159                    if (config.keyboard == Configuration.KEYBOARD_NOKEYS
160                            || config.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_YES) {
161                        // launch the search activity
162                        Intent intent = new Intent(Intent.ACTION_SEARCH_LONG_PRESS);
163                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
164                        try {
165                            mView.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
166                            sendCloseSystemWindows();
167                            getSearchManager().stopSearch();
168                            mContext.startActivity(intent);
169                            // Only clear this if we successfully start the
170                            // activity; otherwise we will allow the normal short
171                            // press action to be performed.
172                            dispatcher.performedLongPress(event);
173                            return true;
174                        } catch (ActivityNotFoundException e) {
175                            // Ignore
176                        }
177                    }
178                }
179                break;
180            }
181        }
182        return false;
183    }
184
185    boolean onKeyUp(int keyCode, KeyEvent event) {
186        if (DEBUG) {
187            Slog.d(TAG, "up " + keyCode);
188        }
189        final KeyEvent.DispatcherState dispatcher = mView.getKeyDispatcherState();
190        if (dispatcher != null) {
191            dispatcher.handleUpEvent(event);
192        }
193
194        switch (keyCode) {
195            case KeyEvent.KEYCODE_VOLUME_UP:
196            case KeyEvent.KEYCODE_VOLUME_DOWN:
197            case KeyEvent.KEYCODE_VOLUME_MUTE: {
198                if (!event.isCanceled()) {
199                    AudioManager audioManager = (AudioManager)mContext.getSystemService(
200                            Context.AUDIO_SERVICE);
201                    if (audioManager != null) {
202                        getAudioManager().handleKeyUp(event, AudioManager.USE_DEFAULT_STREAM_TYPE);
203                    }
204                }
205                return true;
206            }
207
208            case KeyEvent.KEYCODE_HEADSETHOOK:
209            case KeyEvent.KEYCODE_MUTE:
210            case KeyEvent.KEYCODE_MEDIA_PLAY:
211            case KeyEvent.KEYCODE_MEDIA_PAUSE:
212            case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
213            case KeyEvent.KEYCODE_MEDIA_STOP:
214            case KeyEvent.KEYCODE_MEDIA_NEXT:
215            case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
216            case KeyEvent.KEYCODE_MEDIA_REWIND:
217            case KeyEvent.KEYCODE_MEDIA_RECORD:
218            case KeyEvent.KEYCODE_MEDIA_FAST_FORWARD: {
219                handleMediaKeyEvent(event);
220                return true;
221            }
222
223            case KeyEvent.KEYCODE_CAMERA: {
224                if (getKeyguardManager().inKeyguardRestrictedInputMode()) {
225                    break;
226                }
227                if (event.isTracking() && !event.isCanceled()) {
228                    // Add short press behavior here if desired
229                }
230                return true;
231            }
232
233            case KeyEvent.KEYCODE_CALL: {
234                if (getKeyguardManager().inKeyguardRestrictedInputMode()) {
235                    break;
236                }
237                if (event.isTracking() && !event.isCanceled()) {
238                    startCallActivity();
239                }
240                return true;
241            }
242        }
243        return false;
244    }
245
246    void startCallActivity() {
247        sendCloseSystemWindows();
248        Intent intent = new Intent(Intent.ACTION_CALL_BUTTON);
249        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
250        try {
251            mContext.startActivity(intent);
252        } catch (ActivityNotFoundException e) {
253            Slog.w(TAG, "No activity found for android.intent.action.CALL_BUTTON.");
254        }
255    }
256
257    SearchManager getSearchManager() {
258        if (mSearchManager == null) {
259            mSearchManager = (SearchManager) mContext.getSystemService(Context.SEARCH_SERVICE);
260        }
261        return mSearchManager;
262    }
263
264    TelephonyManager getTelephonyManager() {
265        if (mTelephonyManager == null) {
266            mTelephonyManager = (TelephonyManager)mContext.getSystemService(
267                    Context.TELEPHONY_SERVICE);
268        }
269        return mTelephonyManager;
270    }
271
272    KeyguardManager getKeyguardManager() {
273        if (mKeyguardManager == null) {
274            mKeyguardManager = (KeyguardManager)mContext.getSystemService(Context.KEYGUARD_SERVICE);
275        }
276        return mKeyguardManager;
277    }
278
279    AudioManager getAudioManager() {
280        if (mAudioManager == null) {
281            mAudioManager = (AudioManager)mContext.getSystemService(Context.AUDIO_SERVICE);
282        }
283        return mAudioManager;
284    }
285
286    void sendCloseSystemWindows() {
287        PhoneWindowManager.sendCloseSystemWindows(mContext, null);
288    }
289
290    private void handleMediaKeyEvent(KeyEvent keyEvent) {
291        IAudioService audioService = IAudioService.Stub.asInterface(
292                ServiceManager.checkService(Context.AUDIO_SERVICE));
293        if (audioService != null) {
294            try {
295                audioService.dispatchMediaKeyEvent(keyEvent);
296            } catch (RemoteException e) {
297                Log.e(TAG, "dispatchMediaKeyEvent threw exception " + e);
298            }
299        } else {
300            Slog.w(TAG, "Unable to find IAudioService for media key event.");
301        }
302    }
303}
304
305