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.
12 package org.lineageos.eleven;
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;
21 public class MediaButtonIntentReceiver extends WakefulBroadcastReceiver {
22 private static final boolean DEBUG = false;
23 private static final String TAG = "MediaButtonIntentReceiver";
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) {
40 String command = null;
41 switch (event.getKeyCode()) {
42 case KeyEvent.KEYCODE_HEADSETHOOK:
43 command = MusicPlaybackService.CMDHEADSETHOOK;
45 case KeyEvent.KEYCODE_MEDIA_STOP:
46 command = MusicPlaybackService.CMDSTOP;
48 case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
49 command = MusicPlaybackService.CMDTOGGLEPAUSE;
51 case KeyEvent.KEYCODE_MEDIA_NEXT:
52 command = MusicPlaybackService.CMDNEXT;
54 case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
55 command = MusicPlaybackService.CMDPREVIOUS;
57 case KeyEvent.KEYCODE_MEDIA_PAUSE:
58 command = MusicPlaybackService.CMDPAUSE;
60 case KeyEvent.KEYCODE_MEDIA_PLAY:
61 command = MusicPlaybackService.CMDPLAY;
64 if (command != null) {
65 startService(context, command, event.getEventTime());
66 if (isOrderedBroadcast()) {
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);