1 package org
.rockbox
.Helper
;
3 import java
.lang
.reflect
.Method
;
5 import org
.rockbox
.RockboxActivity
;
6 import android
.app
.Notification
;
7 import android
.app
.NotificationManager
;
8 import android
.app
.PendingIntent
;
9 import android
.app
.Service
;
10 import android
.content
.Intent
;
11 import android
.widget
.RemoteViews
;
13 public class RunForegroundManager
15 /* all below is heavily based on the examples found on
16 * http://developer.android.com/reference/android/app/Service.html#setForeground(boolean)
18 private Notification mNotification
;
19 private NotificationManager mNM
;
20 private IRunForeground api
;
21 private Service mCurrentService
;
22 private Intent mWidgetUpdate
;
24 public RunForegroundManager(Service service
)
26 mCurrentService
= service
;
27 mNM
= (NotificationManager
)
28 service
.getSystemService(Service
.NOTIFICATION_SERVICE
);
29 RemoteViews views
= new RemoteViews(service
.getPackageName(), R
.layout
.statusbar
);
30 /* create Intent for clicking on the expanded notifcation area */
31 Intent intent
= new Intent(service
, RockboxActivity
.class);
32 intent
= intent
.addFlags(Intent
.FLAG_ACTIVITY_NEW_TASK
);
34 mNotification
= new Notification();
35 mNotification
.tickerText
= service
.getString(R
.string
.notification
);
36 mNotification
.icon
= R
.drawable
.notification
;
37 mNotification
.contentView
= views
;
38 mNotification
.flags
|= Notification
.FLAG_ONGOING_EVENT
;
39 mNotification
.contentIntent
= PendingIntent
.getActivity(service
, 0, intent
, 0);
42 api
= new NewForegroundApi(R
.string
.notification
, mNotification
);
43 } catch (Throwable t
) {
44 /* Throwable includes Exception and the expected
45 * NoClassDefFoundError for Android 1.x */
47 api
= new OldForegroundApi();
48 Logger
.i("RunForegroundManager: Falling back to compatibility API");
49 } catch (Exception e
) {
50 Logger
.e("Cannot run in foreground: No available API");
55 public void startForeground()
58 * Send the notification.
59 * We use a layout id because it is a unique number.
60 * We use it later to cancel.
62 mNM
.notify(R
.string
.notification
, mNotification
);
64 * this call makes the service run as foreground, which
65 * provides enough cpu time to do music decoding in the
68 api
.startForeground();
71 public void stopForeground()
73 /* Note to cancel BEFORE changing the
74 * foreground state, since we could be killed at that point.
76 mNM
.cancel(R
.string
.notification
);
81 public void updateNotification(String title
, String artist
, String album
, String albumart
)
83 RemoteViews views
= mNotification
.contentView
;
84 views
.setTextViewText(R
.id
.title
, title
);
85 views
.setTextViewText(R
.id
.content
, artist
+"\n"+album
);
86 if (artist
.equals(""))
87 mNotification
.tickerText
= title
;
89 mNotification
.tickerText
= title
+" - "+artist
;
90 mNM
.notify(R
.string
.notification
, mNotification
);
92 mWidgetUpdate
= new Intent("org.rockbox.TrackUpdateInfo");
93 mWidgetUpdate
.putExtra("title", title
);
94 mWidgetUpdate
.putExtra("artist", artist
);
95 mWidgetUpdate
.putExtra("album", album
);
96 mWidgetUpdate
.putExtra("albumart", albumart
);
97 mCurrentService
.sendBroadcast(mWidgetUpdate
);
100 public void resendUpdateNotification()
102 if (mWidgetUpdate
!= null)
103 mCurrentService
.sendBroadcast(mWidgetUpdate
);
106 public void finishNotification()
108 Logger
.d("TrackFinish");
109 Intent widgetUpdate
= new Intent("org.rockbox.TrackFinish");
110 mCurrentService
.sendBroadcast(widgetUpdate
);
113 private interface IRunForeground
115 void startForeground();
116 void stopForeground();
119 private class NewForegroundApi
implements IRunForeground
122 Notification mNotification
;
123 NewForegroundApi(int _id
, Notification _notification
)
126 mNotification
= _notification
;
129 public void startForeground()
131 mCurrentService
.startForeground(id
, mNotification
);
134 public void stopForeground()
136 mCurrentService
.stopForeground(true);
140 private class OldForegroundApi
implements IRunForeground
143 * Get the new API through reflection because it's unavailable
146 private Method mSetForeground
;
148 public OldForegroundApi() throws SecurityException
, NoSuchMethodException
150 mSetForeground
= getClass().getMethod("setForeground",
151 new Class
[] { boolean.class });
154 public void startForeground()
157 mSetForeground
.invoke(mCurrentService
, Boolean
.TRUE
);
158 } catch (Exception e
) {
159 Logger
.e("startForeground compat error: " + e
.getMessage());
163 public void stopForeground()
166 mSetForeground
.invoke(mCurrentService
, Boolean
.FALSE
);
167 } catch (Exception e
) {
168 Logger
.e("stopForeground compat error: " + e
.getMessage());