OSDN Git Service

Initial Contribution
[android-x86/packages-apps-Music.git] / src / com / android / music / MediaButtonIntentReceiver.java
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.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;
27
28 /**
29  * 
30  */
31 public 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);
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         if (keycode == KeyEvent.KEYCODE_HEADSETHOOK) {
77             if (action == KeyEvent.ACTION_DOWN) {
78                 if (!mDown) {
79                     // only if this isn't a repeat event
80                     
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),
88                             LONG_PRESS_DELAY);
89
90                     
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
95                     // a command.
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);
101                         mLastClickTime = 0;
102                     } else {
103                         i.putExtra(MediaPlaybackService.CMDNAME,
104                                 MediaPlaybackService.CMDTOGGLEPAUSE);
105                         context.startService(i);
106                         mLastClickTime = eventtime;
107                     }
108
109                     mLaunched = false;
110                     mDown = true;
111                 }
112             } else {
113                 mHandler.removeMessages(MSG_LONGPRESS_TIMEOUT);
114                 mDown = false;
115             }
116             abortBroadcast();
117         }
118     }
119 }