OSDN Git Service

Eleven: Fix crash after receiving media button intent
[android-x86/packages-apps-Eleven.git] / src / org / lineageos / eleven / MediaButtonIntentReceiver.java
1 /*
2  * Copyright (C) 2007 The Android Open Source Project Licensed under the Apache
3  * License, Version 2.0 (the "License"); you may not use this file except in
4  * compliance with the License. You may obtain a copy of the License at
5  * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law
6  * or agreed to in writing, software distributed under the License is
7  * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
8  * KIND, either express or implied. See the License for the specific language
9  * governing permissions and limitations under the License.
10  */
11
12 package org.lineageos.eleven;
13
14 import android.content.Context;
15 import android.content.Intent;
16 import android.media.AudioManager;
17 import android.support.v4.content.WakefulBroadcastReceiver;
18 import android.util.Log;
19 import android.view.KeyEvent;
20
21 public class MediaButtonIntentReceiver extends WakefulBroadcastReceiver {
22     private static final boolean DEBUG = false;
23     private static final String TAG = "MediaButtonIntentReceiver";
24
25     /**
26      * {@inheritDoc}
27      */
28     @Override
29     public void onReceive(final Context context, final Intent intent) {
30         if (DEBUG) Log.v(TAG, "Received intent: " + intent);
31         final String intentAction = intent.getAction();
32         if (AudioManager.ACTION_AUDIO_BECOMING_NOISY.equals(intentAction)) {
33             startService(context, MusicPlaybackService.CMDPAUSE, System.currentTimeMillis());
34         } else if (Intent.ACTION_MEDIA_BUTTON.equals(intentAction)) {
35             final KeyEvent event = intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
36             if (event == null || event.getAction() != KeyEvent.ACTION_UP) {
37                 return;
38             }
39
40             String command = null;
41             switch (event.getKeyCode()) {
42                 case KeyEvent.KEYCODE_HEADSETHOOK:
43                     command = MusicPlaybackService.CMDHEADSETHOOK;
44                     break;
45                 case KeyEvent.KEYCODE_MEDIA_STOP:
46                     command = MusicPlaybackService.CMDSTOP;
47                     break;
48                 case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
49                     command = MusicPlaybackService.CMDTOGGLEPAUSE;
50                     break;
51                 case KeyEvent.KEYCODE_MEDIA_NEXT:
52                     command = MusicPlaybackService.CMDNEXT;
53                     break;
54                 case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
55                     command = MusicPlaybackService.CMDPREVIOUS;
56                     break;
57                 case KeyEvent.KEYCODE_MEDIA_PAUSE:
58                     command = MusicPlaybackService.CMDPAUSE;
59                     break;
60                 case KeyEvent.KEYCODE_MEDIA_PLAY:
61                     command = MusicPlaybackService.CMDPLAY;
62                     break;
63             }
64             if (command != null) {
65                 startService(context, command, event.getEventTime());
66                 if (isOrderedBroadcast()) {
67                     abortBroadcast();
68                 }
69             }
70         }
71     }
72
73     private static void startService(Context context, String command, long timestamp) {
74         final Intent i = new Intent(context, MusicPlaybackService.class);
75         i.setAction(MusicPlaybackService.SERVICECMD);
76         i.putExtra(MusicPlaybackService.CMDNAME, command);
77         i.putExtra(MusicPlaybackService.FROM_MEDIA_BUTTON, true);
78         i.putExtra(MusicPlaybackService.TIMESTAMP, timestamp);
79         context.startForegroundService(i);
80     }
81 }