Move android notification display format logic to java code (no functional change...
[kugel-rb.git] / android / src / org / rockbox / Helper / RunForegroundManager.java
blob197c51a56742da39d169b78553790c34f2b2ee09
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;
8 import org.rockbox.RockboxWidgetProvider;
10 import android.app.Notification;
11 import android.app.NotificationManager;
12 import android.app.PendingIntent;
13 import android.app.Service;
14 import android.content.Intent;
15 import android.util.Log;
16 import android.widget.RemoteViews;
18 public class RunForegroundManager
20 /* all below is heavily based on the examples found on
21 * http://developer.android.com/reference/android/app/Service.html#setForeground(boolean)
23 private Notification mNotification;
24 private NotificationManager mNM;
25 private IRunForeground api;
26 private Service mCurrentService;
28 public RunForegroundManager(Service service) throws Exception
30 mCurrentService = service;
31 mNM = (NotificationManager)
32 service.getSystemService(Service.NOTIFICATION_SERVICE);
33 RemoteViews views = new RemoteViews(service.getPackageName(), R.layout.statusbar);
34 /* create Intent for clicking on the expanded notifcation area */
35 Intent intent = new Intent(service, RockboxActivity.class);
36 intent = intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
38 mNotification = new Notification();
39 mNotification.tickerText = service.getString(R.string.notification);
40 mNotification.icon = R.drawable.notification;
41 mNotification.contentView = views;
42 mNotification.flags |= Notification.FLAG_ONGOING_EVENT;
43 mNotification.contentIntent = PendingIntent.getActivity(service, 0, intent, 0);
45 try {
46 api = new newForegroundApi(R.string.notification, mNotification);
47 } catch (NoSuchMethodException e) {
48 /* Fall back on the old API */
49 api = new oldForegroundApi();
52 private void LOG(CharSequence text)
54 Log.d("Rockbox", (String)text);
56 private void LOG(CharSequence text, Throwable tr)
58 Log.d("Rockbox", (String)text, tr);
61 public void startForeground()
63 /*
64 * Send the notification.
65 * We use a layout id because it is a unique number.
66 * We use it later to cancel.
68 mNM.notify(R.string.notification, mNotification);
70 * this call makes the service run as foreground, which
71 * provides enough cpu time to do music decoding in the
72 * background
74 api.startForeground();
77 public void stopForeground()
79 /* Note to cancel BEFORE changing the
80 * foreground state, since we could be killed at that point.
82 mNM.cancel(R.string.notification);
83 api.stopForeground();
86 public void updateNotification(String title, String artist, String album)
88 RemoteViews views = mNotification.contentView;
89 views.setTextViewText(R.id.title, title);
90 views.setTextViewText(R.id.content, artist+"\n"+album);
91 if (artist.equals(""))
92 mNotification.tickerText = title;
93 else
94 mNotification.tickerText = title+" - "+artist;
95 mNM.notify(R.string.notification, mNotification);
98 private interface IRunForeground
100 void startForeground();
101 void stopForeground();
104 private class newForegroundApi implements IRunForeground
106 Class<?>[] mStartForegroundSignature =
107 new Class[] { int.class, Notification.class };
108 Class<?>[] mStopForegroundSignature =
109 new Class[] { boolean.class };
110 private Method mStartForeground;
111 private Method mStopForeground;
112 private Object[] mStartForegroundArgs = new Object[2];
113 private Object[] mStopForegroundArgs = new Object[1];
115 newForegroundApi(int id, Notification notification)
116 throws SecurityException, NoSuchMethodException
119 * Get the new API through reflection
121 mStartForeground = Service.class.getMethod("startForeground",
122 mStartForegroundSignature);
123 mStopForeground = Service.class.getMethod("stopForeground",
124 mStopForegroundSignature);
125 mStartForegroundArgs[0] = id;
126 mStartForegroundArgs[1] = notification;
127 mStopForegroundArgs[0] = Boolean.TRUE;
130 public void startForeground()
132 try {
133 mStartForeground.invoke(mCurrentService, mStartForegroundArgs);
134 } catch (InvocationTargetException e) {
135 /* Should not happen. */
136 LOG("Unable to invoke startForeground", e);
137 } catch (IllegalAccessException e) {
138 /* Should not happen. */
139 LOG("Unable to invoke startForeground", e);
143 public void stopForeground()
145 try {
146 mStopForeground.invoke(mCurrentService, mStopForegroundArgs);
147 } catch (InvocationTargetException e) {
148 /* Should not happen. */
149 LOG("Unable to invoke stopForeground", e);
150 } catch (IllegalAccessException e) {
151 /* Should not happen. */
152 LOG("Unable to invoke stopForeground", e);
157 private class oldForegroundApi implements IRunForeground
159 public void startForeground()
161 mCurrentService.setForeground(false);
163 public void stopForeground()
165 mCurrentService.setForeground(false);