1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2011 Antoine Cellerier <dionoea at videolan dot org>
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
22 package org
.rockbox
.widgets
;
25 import org
.rockbox
.RockboxActivity
;
26 import org
.rockbox
.RockboxService
;
27 import android
.app
.PendingIntent
;
28 import android
.appwidget
.AppWidgetManager
;
29 import android
.appwidget
.AppWidgetProvider
;
30 import android
.appwidget
.AppWidgetProviderInfo
;
31 import android
.content
.ComponentName
;
32 import android
.content
.Context
;
33 import android
.content
.Intent
;
34 import android
.net
.Uri
;
35 import android
.view
.KeyEvent
;
36 import android
.view
.View
;
37 import android
.widget
.RemoteViews
;
39 public class RockboxWidgetProvider
extends AppWidgetProvider
41 static RockboxWidgetProvider mInstance
;
42 public RockboxWidgetProvider()
48 public void onUpdate(Context context
, AppWidgetManager appWidgetManager
, int[] appWidgetIds
)
50 final int N
= appWidgetIds
.length
;
51 for (int i
= 0; i
< N
; i
++)
53 int appWidgetId
= appWidgetIds
[i
];
54 updateAppWidget(context
, appWidgetManager
, appWidgetId
, null);
59 public static RockboxWidgetProvider
getInstance()
61 /* no new instance here, instanced by android */
66 public void onDeleted(Context context
, int[] appWidgetIds
)
71 public void onEnabled(Context context
)
76 public void onDisabled(Context context
)
81 public void onReceive(Context context
, Intent intent
)
83 String action
= intent
.getAction();
84 if (action
.equals("org.rockbox.TrackUpdateInfo") ||
85 action
.equals("org.rockbox.TrackFinish") ||
86 action
.equals("org.rockbox.UpdateState"))
88 AppWidgetManager gm
= AppWidgetManager
.getInstance(context
);
89 int[] appWidgetIds
= gm
.getAppWidgetIds(new ComponentName(context
, this.getClass()));
90 final int N
= appWidgetIds
.length
;
91 for (int i
= 0; i
< N
; i
++)
93 updateAppWidget(context
, gm
, appWidgetIds
[i
], intent
);
98 super.onReceive(context
, intent
);
102 public void updateAppWidget(Context context
, AppWidgetManager appWidgetManager
, int appWidgetId
, Intent args
)
104 AppWidgetProviderInfo provider
= appWidgetManager
.getAppWidgetInfo(appWidgetId
);
105 RemoteViews views
= null;
106 views
= new RemoteViews(context
.getPackageName(), provider
.initialLayout
);
108 Intent intent
= new Intent(context
, RockboxActivity
.class);
109 PendingIntent pendingIntent
= PendingIntent
.getActivity(context
, 0, intent
, 0);
110 views
.setOnClickPendingIntent(R
.id
.infoDisplay
, pendingIntent
);
112 RockboxWidgetConfigure
.WidgetPref state
= RockboxWidgetConfigure
.loadWidgetPref(context
, appWidgetId
);
114 /* enable/disable PREV */
115 if (state
.enablePrev
)
117 views
.setOnClickPendingIntent(R
.id
.prev
,
118 RockboxMediaIntent
.newPendingIntent(context
,
119 KeyEvent
.KEYCODE_MEDIA_PREVIOUS
));
122 views
.setViewVisibility(R
.id
.prev
, View
.GONE
);
124 /* enable/disable PLAY/PAUSE */
125 if (state
.enablePlayPause
)
127 views
.setOnClickPendingIntent(R
.id
.playPause
,
128 RockboxMediaIntent
.newPendingIntent(context
,
129 KeyEvent
.KEYCODE_MEDIA_PLAY_PAUSE
));
132 views
.setViewVisibility(R
.id
.playPause
, View
.GONE
);
134 /* enable/disable NEXT */
135 if (state
.enableNext
)
137 views
.setOnClickPendingIntent(R
.id
.next
,
138 RockboxMediaIntent
.newPendingIntent(context
,
139 KeyEvent
.KEYCODE_MEDIA_NEXT
));
142 views
.setViewVisibility(R
.id
.next
, View
.GONE
);
144 /* enable/disable STOP */
145 if (state
.enableStop
)
147 views
.setOnClickPendingIntent(R
.id
.stop
,
148 RockboxMediaIntent
.newPendingIntent(context
,
149 KeyEvent
.KEYCODE_MEDIA_STOP
));
152 views
.setViewVisibility(R
.id
.stop
, View
.GONE
);
156 if (args
.getAction().equals("org.rockbox.TrackUpdateInfo"))
158 CharSequence title
= args
.getCharSequenceExtra("title");
159 CharSequence artist
= args
.getCharSequenceExtra("artist");
160 CharSequence album
= args
.getCharSequenceExtra("album");
161 views
.setTextViewText(R
.id
.infoDisplay
, title
+"\n"+artist
+"\n"+album
);
163 else if (args
.getAction().equals("org.rockbox.TrackFinish"))
165 // FIXME: looks like this event is always fired earlier than
166 // the actual track change (a few seconds)
167 views
.setTextViewText(R
.id
.infoDisplay
, context
.getString(R
.string
.appwidget_infoDisplay
));
169 else if (args
.getAction().equals("org.rockbox.UpdateState"))
171 CharSequence playstate
= args
.getCharSequenceExtra("state");
172 if (playstate
.equals("play"))
173 views
.setImageViewResource(R
.id
.playPause
, R
.drawable
.appwidget_pause
);
174 else /* pause or stop */
175 views
.setImageViewResource(R
.id
.playPause
, R
.drawable
.appwidget_play
);
179 appWidgetManager
.updateAppWidget(appWidgetId
, views
);
182 private static class RockboxMediaIntent
extends Intent
184 private RockboxMediaIntent(Context c
, int keycode
)
186 super(ACTION_MEDIA_BUTTON
, Uri
.EMPTY
, c
, RockboxService
.class);
187 putExtra(EXTRA_KEY_EVENT
, new KeyEvent(KeyEvent
.ACTION_UP
,
191 public static PendingIntent
newPendingIntent(Context c
, int keycode
)
193 /* Use keycode as request to code to prevent successive
194 * PendingIntents from overwritting one another.
195 * This seems hackish but at least it works.
196 * see: http://code.google.com/p/android/issues/detail?id=863
198 return PendingIntent
.getService(c
, keycode
, new RockboxMediaIntent(c
, keycode
), 0);