1 package org
.rockbox
.Helper
;
3 import java
.lang
.reflect
.InvocationTargetException
;
4 import java
.lang
.reflect
.Method
;
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
) throws Exception
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);
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()
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
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
);
85 public void updateNotification(String title
, String artist
, String album
)
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
;
93 mNotification
.tickerText
= title
+" - "+artist
;
94 mNM
.notify(R
.string
.notification
, mNotification
);
97 private interface IRunForeground
99 void startForeground();
100 void stopForeground();
103 private class newForegroundApi
implements IRunForeground
105 Class
<?
>[] mStartForegroundSignature
=
106 new Class
[] { int.class, Notification
.class };
107 Class
<?
>[] mStopForegroundSignature
=
108 new Class
[] { boolean.class };
109 private Method mStartForeground
;
110 private Method mStopForeground
;
111 private Object
[] mStartForegroundArgs
= new Object
[2];
112 private Object
[] mStopForegroundArgs
= new Object
[1];
114 newForegroundApi(int id
, Notification notification
)
115 throws SecurityException
, NoSuchMethodException
118 * Get the new API through reflection
120 mStartForeground
= Service
.class.getMethod("startForeground",
121 mStartForegroundSignature
);
122 mStopForeground
= Service
.class.getMethod("stopForeground",
123 mStopForegroundSignature
);
124 mStartForegroundArgs
[0] = id
;
125 mStartForegroundArgs
[1] = notification
;
126 mStopForegroundArgs
[0] = Boolean
.TRUE
;
129 public void startForeground()
132 mStartForeground
.invoke(mCurrentService
, mStartForegroundArgs
);
133 } catch (InvocationTargetException e
) {
134 /* Should not happen. */
135 LOG("Unable to invoke startForeground", e
);
136 } catch (IllegalAccessException e
) {
137 /* Should not happen. */
138 LOG("Unable to invoke startForeground", e
);
142 public void stopForeground()
145 mStopForeground
.invoke(mCurrentService
, mStopForegroundArgs
);
146 } catch (InvocationTargetException e
) {
147 /* Should not happen. */
148 LOG("Unable to invoke stopForeground", e
);
149 } catch (IllegalAccessException e
) {
150 /* Should not happen. */
151 LOG("Unable to invoke stopForeground", e
);
156 private class oldForegroundApi
implements IRunForeground
158 public void startForeground()
160 mCurrentService
.setForeground(false);
162 public void stopForeground()
164 mCurrentService
.setForeground(false);