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