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