1 /* 2 * Copyright (C) 2007 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 17 package com.android.music; 18 19 import android.content.BroadcastReceiver; 20 import android.content.Context; 21 import android.content.Intent; 22 import android.media.AudioManager; 23 import android.os.Handler; 24 import android.os.Message; 25 import android.view.KeyEvent; 26 27 /** 28 * 29 */ 30 public class MediaButtonIntentReceiver extends BroadcastReceiver { 31 32 private static final int MSG_LONGPRESS_TIMEOUT = 1; 33 private static final int LONG_PRESS_DELAY = 1000; 34 35 private static long mLastClickTime = 0; 36 private static boolean mDown = false; 37 private static boolean mLaunched = false; 38 39 private static Handler mHandler = new Handler() { 40 @Override 41 public void handleMessage(Message msg) { 42 switch (msg.what) { 43 case MSG_LONGPRESS_TIMEOUT: 44 if (!mLaunched) { 45 Context context = (Context)msg.obj; 46 Intent i = new Intent(); 47 i.putExtra("autoshuffle", "true"); 48 i.setClass(context, MusicBrowserActivity.class); 49 i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP); 50 context.startActivity(i); 51 mLaunched = true; 52 } 53 break; 54 } 55 } 56 }; 57 58 @Override 59 public void onReceive(Context context, Intent intent) { 60 String intentAction = intent.getAction(); 61 if (AudioManager.ACTION_AUDIO_BECOMING_NOISY.equals(intentAction)) { 62 Intent i = new Intent(context, MediaPlaybackService.class); 63 i.setAction(MediaPlaybackService.SERVICECMD); 64 i.putExtra(MediaPlaybackService.CMDNAME, MediaPlaybackService.CMDPAUSE); 65 context.startService(i); 66 } else if (Intent.ACTION_MEDIA_BUTTON.equals(intentAction)) { 67 KeyEvent event = (KeyEvent) 68 intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT); 69 70 if (event == null) { 71 return; 72 } 73 74 int keycode = event.getKeyCode(); 75 int action = event.getAction(); 76 long eventtime = event.getEventTime(); 77 78 // single quick press: pause/resume. 79 // double press: next track 80 // long press: start auto-shuffle mode. 81 82 String command = null; 83 switch (keycode) { 84 case KeyEvent.KEYCODE_MEDIA_STOP: 85 command = MediaPlaybackService.CMDSTOP; 86 break; 87 case KeyEvent.KEYCODE_HEADSETHOOK: 88 case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE: 89 command = MediaPlaybackService.CMDTOGGLEPAUSE; 90 break; 91 case KeyEvent.KEYCODE_MEDIA_NEXT: 92 command = MediaPlaybackService.CMDNEXT; 93 break; 94 case KeyEvent.KEYCODE_MEDIA_PREVIOUS: 95 command = MediaPlaybackService.CMDPREVIOUS; 96 break; 97 } 98 99 if (command != null) { 100 if (action == KeyEvent.ACTION_DOWN) { 101 if (mDown) { 102 if (MediaPlaybackService.CMDTOGGLEPAUSE.equals(command) 103 && mLastClickTime != 0 104 && eventtime - mLastClickTime > LONG_PRESS_DELAY) { 105 mHandler.sendMessage( 106 mHandler.obtainMessage(MSG_LONGPRESS_TIMEOUT, context)); 107 } 108 } else { 109 // if this isn't a repeat event 110 111 // The service may or may not be running, but we need to send it 112 // a command. 113 Intent i = new Intent(context, MediaPlaybackService.class); 114 i.setAction(MediaPlaybackService.SERVICECMD); 115 if (keycode == KeyEvent.KEYCODE_HEADSETHOOK && 116 eventtime - mLastClickTime < 300) { 117 i.putExtra(MediaPlaybackService.CMDNAME, MediaPlaybackService.CMDNEXT); 118 context.startService(i); 119 mLastClickTime = 0; 120 } else { 121 i.putExtra(MediaPlaybackService.CMDNAME, command); 122 context.startService(i); 123 mLastClickTime = eventtime; 124 } 125 126 mLaunched = false; 127 mDown = true; 128 } 129 } else { 130 mHandler.removeMessages(MSG_LONGPRESS_TIMEOUT); 131 mDown = false; 132 } 133 if (isOrderedBroadcast()) { 134 abortBroadcast(); 135 } 136 } 137 } 138 } 139 } 140