2 * Copyright (C) 2007 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.music;
19 import android.content.Context;
20 import android.content.Intent;
21 import android.content.BroadcastReceiver;
22 import android.content.SharedPreferences;
23 import android.util.Log;
24 import android.view.KeyEvent;
25 import android.os.Handler;
26 import android.os.Message;
31 public class MediaButtonIntentReceiver extends BroadcastReceiver {
33 private static final int MSG_LONGPRESS_TIMEOUT = 1;
34 private static final int LONG_PRESS_DELAY = 1000;
36 private static long mLastClickTime = 0;
37 private static boolean mDown = false;
38 private static boolean mLaunched = false;
40 private static Handler mHandler = new Handler() {
42 public void handleMessage(Message msg) {
44 case MSG_LONGPRESS_TIMEOUT:
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);
51 context.startActivity(i);
60 public void onReceive(Context context, Intent intent) {
61 KeyEvent event = (KeyEvent)
62 intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
68 int keycode = event.getKeyCode();
69 int action = event.getAction();
70 long eventtime = event.getEventTime();
72 // single quick press: pause/resume.
73 // double press: next track
74 // long press: start auto-shuffle mode.
76 if (keycode == KeyEvent.KEYCODE_HEADSETHOOK) {
77 if (action == KeyEvent.ACTION_DOWN) {
79 // only if this isn't a repeat event
81 // We're not using the original time of the event as the
82 // base here, because in some cases it can take more than
83 // one second for us to receive the event, in which case
84 // we would go immediately to auto shuffle mode, even if
85 // the user didn't long press.
86 mHandler.sendMessageDelayed(
87 mHandler.obtainMessage(MSG_LONGPRESS_TIMEOUT, context),
91 SharedPreferences pref = context.getSharedPreferences("Music",
92 Context.MODE_WORLD_READABLE | Context.MODE_WORLD_WRITEABLE);
93 String q = pref.getString("queue", "");
94 // The service may or may not be running, but we need to send it
96 Intent i = new Intent(context, MediaPlaybackService.class);
97 i.setAction(MediaPlaybackService.SERVICECMD);
98 if (eventtime - mLastClickTime < 300) {
99 i.putExtra(MediaPlaybackService.CMDNAME, MediaPlaybackService.CMDNEXT);
100 context.startService(i);
103 i.putExtra(MediaPlaybackService.CMDNAME,
104 MediaPlaybackService.CMDTOGGLEPAUSE);
105 context.startService(i);
106 mLastClickTime = eventtime;
113 mHandler.removeMessages(MSG_LONGPRESS_TIMEOUT);