MediaButtonIntentReceiver.java revision a857e7ab7608a85c8d66e971e5807051bdb6daba
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.content.Context; 20import android.content.Intent; 21import android.content.BroadcastReceiver; 22import android.content.SharedPreferences; 23import android.util.Log; 24import android.view.KeyEvent; 25import android.os.Handler; 26import android.os.Message; 27 28/** 29 * 30 */ 31public class MediaButtonIntentReceiver extends BroadcastReceiver { 32 33 private static final int MSG_LONGPRESS_TIMEOUT = 1; 34 private static final int LONG_PRESS_DELAY = 1000; 35 36 private static long mLastClickTime = 0; 37 private static boolean mDown = false; 38 private static boolean mLaunched = false; 39 40 private static Handler mHandler = new Handler() { 41 @Override 42 public void handleMessage(Message msg) { 43 switch (msg.what) { 44 case MSG_LONGPRESS_TIMEOUT: 45 if (!mLaunched) { 46 Context context = (Context)msg.obj; 47 Intent i = new Intent(); 48 i.putExtra("autoshuffle", "true"); 49 i.setClass(context, MusicBrowserActivity.class); 50 i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP); 51 context.startActivity(i); 52 mLaunched = true; 53 } 54 break; 55 } 56 } 57 }; 58 59 @Override 60 public void onReceive(Context context, Intent intent) { 61 KeyEvent event = (KeyEvent) 62 intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT); 63 64 if (event == null) { 65 return; 66 } 67 68 int keycode = event.getKeyCode(); 69 int action = event.getAction(); 70 long eventtime = event.getEventTime(); 71 72 // single quick press: pause/resume. 73 // double press: next track 74 // long press: start auto-shuffle mode. 75 76 String command = null; 77 switch (keycode) { 78 case KeyEvent.KEYCODE_HEADSETHOOK: 79 case KeyEvent.KEYCODE_PLAYPAUSE: 80 command = MediaPlaybackService.CMDTOGGLEPAUSE; 81 break; 82 case KeyEvent.KEYCODE_NEXTSONG: 83 command = MediaPlaybackService.CMDNEXT; 84 break; 85 case KeyEvent.KEYCODE_PREVIOUSSONG: 86 command = MediaPlaybackService.CMDPREVIOUS; 87 break; 88 } 89 90 if (command != null) { 91 if (action == KeyEvent.ACTION_DOWN) { 92 if (!mDown) { 93 // only if this isn't a repeat event 94 95 if (MediaPlaybackService.CMDTOGGLEPAUSE.equals(command)) { 96 // We're not using the original time of the event as the 97 // base here, because in some cases it can take more than 98 // one second for us to receive the event, in which case 99 // we would go immediately to auto shuffle mode, even if 100 // the user didn't long press. 101 mHandler.sendMessageDelayed( 102 mHandler.obtainMessage(MSG_LONGPRESS_TIMEOUT, context), 103 LONG_PRESS_DELAY); 104 } 105 106 SharedPreferences pref = context.getSharedPreferences("Music", 107 Context.MODE_WORLD_READABLE | Context.MODE_WORLD_WRITEABLE); 108 String q = pref.getString("queue", ""); 109 // The service may or may not be running, but we need to send it 110 // a command. 111 Intent i = new Intent(context, MediaPlaybackService.class); 112 i.setAction(MediaPlaybackService.SERVICECMD); 113 if (keycode == KeyEvent.KEYCODE_HEADSETHOOK && eventtime - mLastClickTime < 300) { 114 i.putExtra(MediaPlaybackService.CMDNAME, MediaPlaybackService.CMDNEXT); 115 context.startService(i); 116 mLastClickTime = 0; 117 } else { 118 i.putExtra(MediaPlaybackService.CMDNAME, command); 119 context.startService(i); 120 mLastClickTime = eventtime; 121 } 122 123 mLaunched = false; 124 mDown = true; 125 } 126 } else { 127 mHandler.removeMessages(MSG_LONGPRESS_TIMEOUT); 128 mDown = false; 129 } 130 abortBroadcast(); 131 } 132 } 133} 134