Android: Show cover art in the widget (including option to hide it).
[maemo-rb.git] / android / src / org / rockbox / Helper / RunForegroundManager.java
blobc08e742abaf1ab8935872b2d1e9889099b6c3ffb
1 package org.rockbox.Helper;
3 import java.lang.reflect.InvocationTargetException;
4 import java.lang.reflect.Method;
6 import org.rockbox.R;
7 import org.rockbox.RockboxActivity;
9 import android.app.Notification;
10 import android.app.NotificationManager;
11 import android.app.PendingIntent;
12 import android.app.Service;
13 import android.content.Intent;
14 import android.util.Log;
15 import android.widget.RemoteViews;
17 public class RunForegroundManager
19 /* all below is heavily based on the examples found on
20 * http://developer.android.com/reference/android/app/Service.html#setForeground(boolean)
22 private Notification mNotification;
23 private NotificationManager mNM;
24 private IRunForeground api;
25 private Service mCurrentService;
27 public RunForegroundManager(Service service)
29 mCurrentService = service;
30 mNM = (NotificationManager)
31 service.getSystemService(Service.NOTIFICATION_SERVICE);
32 RemoteViews views = new RemoteViews(service.getPackageName(), R.layout.statusbar);
33 /* create Intent for clicking on the expanded notifcation area */
34 Intent intent = new Intent(service, RockboxActivity.class);
35 intent = intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
37 mNotification = new Notification();
38 mNotification.tickerText = service.getString(R.string.notification);
39 mNotification.icon = R.drawable.notification;
40 mNotification.contentView = views;
41 mNotification.flags |= Notification.FLAG_ONGOING_EVENT;
42 mNotification.contentIntent = PendingIntent.getActivity(service, 0, intent, 0);
44 try {
45 api = new newForegroundApi(R.string.notification, mNotification);
46 } catch (NoSuchMethodException e) {
47 /* Fall back on the old API */
48 api = new oldForegroundApi();
51 private void LOG(CharSequence text)
53 Log.d("Rockbox", (String)text);
55 private void LOG(CharSequence text, Throwable tr)
57 Log.d("Rockbox", (String)text, tr);
60 public void startForeground()
62 /*
63 * Send the notification.
64 * We use a layout id because it is a unique number.
65 * We use it later to cancel.
67 mNM.notify(R.string.notification, mNotification);
69 * this call makes the service run as foreground, which
70 * provides enough cpu time to do music decoding in the
71 * background
73 api.startForeground();
76 public void stopForeground()
78 /* Note to cancel BEFORE changing the
79 * foreground state, since we could be killed at that point.
81 mNM.cancel(R.string.notification);
82 api.stopForeground();
85 public void updateNotification(String title, String artist, String album, String albumart)
87 RemoteViews views = mNotification.contentView;
88 views.setTextViewText(R.id.title, title);
89 views.setTextViewText(R.id.content, artist+"\n"+album);
90 if (artist.equals(""))
91 mNotification.tickerText = title;
92 else
93 mNotification.tickerText = title+" - "+artist;
94 mNM.notify(R.string.notification, mNotification);
96 Intent widgetUpdate = new Intent("org.rockbox.TrackUpdateInfo");
97 widgetUpdate.putExtra("title", title);
98 widgetUpdate.putExtra("artist", artist);
99 widgetUpdate.putExtra("album", album);
100 widgetUpdate.putExtra("albumart", albumart);
101 mCurrentService.sendBroadcast(widgetUpdate);
104 public void finishNotification()
106 Log.d("Rockbox", "TrackFinish");
107 Intent widgetUpdate = new Intent("org.rockbox.TrackFinish");
108 mCurrentService.sendBroadcast(widgetUpdate);
111 private interface IRunForeground
113 void startForeground();
114 void stopForeground();
117 private class newForegroundApi implements IRunForeground
119 Class<?>[] mStartForegroundSignature =
120 new Class[] { int.class, Notification.class };
121 Class<?>[] mStopForegroundSignature =
122 new Class[] { boolean.class };
123 private Method mStartForeground;
124 private Method mStopForeground;
125 private Object[] mStartForegroundArgs = new Object[2];
126 private Object[] mStopForegroundArgs = new Object[1];
128 newForegroundApi(int id, Notification notification)
129 throws SecurityException, NoSuchMethodException
132 * Get the new API through reflection
134 mStartForeground = Service.class.getMethod("startForeground",
135 mStartForegroundSignature);
136 mStopForeground = Service.class.getMethod("stopForeground",
137 mStopForegroundSignature);
138 mStartForegroundArgs[0] = id;
139 mStartForegroundArgs[1] = notification;
140 mStopForegroundArgs[0] = Boolean.TRUE;
143 public void startForeground()
145 try {
146 mStartForeground.invoke(mCurrentService, mStartForegroundArgs);
147 } catch (InvocationTargetException e) {
148 /* Should not happen. */
149 LOG("Unable to invoke startForeground", e);
150 } catch (IllegalAccessException e) {
151 /* Should not happen. */
152 LOG("Unable to invoke startForeground", e);
156 public void stopForeground()
158 try {
159 mStopForeground.invoke(mCurrentService, mStopForegroundArgs);
160 } catch (InvocationTargetException e) {
161 /* Should not happen. */
162 LOG("Unable to invoke stopForeground", e);
163 } catch (IllegalAccessException e) {
164 /* Should not happen. */
165 LOG("Unable to invoke stopForeground", e);
170 private class oldForegroundApi implements IRunForeground
172 public void startForeground()
174 mCurrentService.setForeground(false);
176 public void stopForeground()
178 mCurrentService.setForeground(false);