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