summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Martitz <kugel@rockbox.org>2010-08-07 22:29:54 +0000
committerThomas Martitz <kugel@rockbox.org>2010-08-07 22:29:54 +0000
commit8978b2bafad5d97202f6ea8dc9a9129fbd8d3927 (patch)
tree0df926f15dd114a03fc263d2cc537dfcba75ce0a
parent2113e22c4691728edf23768e59a0bde55d80a367 (diff)
downloadrockbox-8978b2bafad5d97202f6ea8dc9a9129fbd8d3927.tar.gz
rockbox-8978b2bafad5d97202f6ea8dc9a9129fbd8d3927.zip
Android port: The RockboxService introduction broke 1.5/1.6 compability. This should bring it back (heaviliy based on the example given in the Android docs).
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@27748 a1c6a512-1295-4272-9138-f99709370657
-rw-r--r--android/default.properties2
-rw-r--r--android/src/org/rockbox/RockboxService.java98
2 files changed, 87 insertions, 13 deletions
diff --git a/android/default.properties b/android/default.properties
index 9d135cb85f..9d79b12c71 100644
--- a/android/default.properties
+++ b/android/default.properties
@@ -8,4 +8,4 @@
8# project structure. 8# project structure.
9 9
10# Project target. 10# Project target.
11target=android-7 11target=android-4
diff --git a/android/src/org/rockbox/RockboxService.java b/android/src/org/rockbox/RockboxService.java
index 5f8329d0c3..a73f614782 100644
--- a/android/src/org/rockbox/RockboxService.java
+++ b/android/src/org/rockbox/RockboxService.java
@@ -4,6 +4,8 @@ import java.io.BufferedInputStream;
4import java.io.BufferedOutputStream; 4import java.io.BufferedOutputStream;
5import java.io.File; 5import java.io.File;
6import java.io.FileOutputStream; 6import java.io.FileOutputStream;
7import java.lang.reflect.InvocationTargetException;
8import java.lang.reflect.Method;
7import java.util.Enumeration; 9import java.util.Enumeration;
8import java.util.zip.ZipEntry; 10import java.util.zip.ZipEntry;
9import java.util.zip.ZipFile; 11import java.util.zip.ZipFile;
@@ -23,10 +25,29 @@ public class RockboxService extends Service
23 public static RockboxFramebuffer fb = null; 25 public static RockboxFramebuffer fb = null;
24 private static RockboxService instance; 26 private static RockboxService instance;
25 private Notification notification; 27 private Notification notification;
28 private static final Class[] mStartForegroundSignature = new Class[] {
29 int.class, Notification.class};
30 private static final Class[] mStopForegroundSignature = new Class[] {
31 boolean.class};
32
33 private NotificationManager mNM;
34 private Method mStartForeground;
35 private Method mStopForeground;
36 private Object[] mStartForegroundArgs = new Object[2];
37 private Object[] mStopForegroundArgs = new Object[1];
26 @Override 38 @Override
27 public void onCreate() 39 public void onCreate()
28 { 40 {
29 mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); 41 mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
42 try {
43 mStartForeground = getClass().getMethod("startForeground",
44 mStartForegroundSignature);
45 mStopForeground = getClass().getMethod("stopForeground",
46 mStopForegroundSignature);
47 } catch (NoSuchMethodException e) {
48 // Running on an older platform.
49 mStartForeground = mStopForeground = null;
50 }
30 startservice(); 51 startservice();
31 instance = this; 52 instance = this;
32 } 53 }
@@ -34,25 +55,23 @@ public class RockboxService extends Service
34 private void do_start(Intent intent) 55 private void do_start(Intent intent)
35 { 56 {
36 LOG("Start Service"); 57 LOG("Start Service");
58 /* Display a notification about us starting. We put an icon in the status bar. */
59 create_notification();
37 } 60 }
38 61
39 private void LOG(CharSequence text) 62 private void LOG(CharSequence text)
40 { 63 {
41 Log.d("Rockbox", (String) text); 64 Log.d("Rockbox", (String) text);
42 } 65 }
43 66
44 @Override
45 public void onStart(Intent intent, int startId) { 67 public void onStart(Intent intent, int startId) {
46 do_start(intent); 68 do_start(intent);
47 } 69 }
48 70
49 @Override
50 public int onStartCommand(Intent intent, int flags, int startId) 71 public int onStartCommand(Intent intent, int flags, int startId)
51 { 72 {
52 do_start(intent); 73 do_start(intent);
53 /* Display a notification about us starting. We put an icon in the status bar. */ 74 return 1; /* START_STICKY */
54 create_notification();
55 return START_STICKY;
56 } 75 }
57 76
58 private void startservice() 77 private void startservice()
@@ -130,10 +149,7 @@ public class RockboxService extends Service
130 public IBinder onBind(Intent intent) { 149 public IBinder onBind(Intent intent) {
131 // TODO Auto-generated method stub 150 // TODO Auto-generated method stub
132 return null; 151 return null;
133 } 152 } /**
134 private NotificationManager mNM;
135
136 /**
137 * Class for clients to access. Because we know this service always 153 * Class for clients to access. Because we know this service always
138 * runs in the same process as its clients, we don't need to deal with 154 * runs in the same process as its clients, we don't need to deal with
139 * IPC. 155 * IPC.
@@ -179,7 +195,7 @@ public class RockboxService extends Service
179 * provides enough cpu time to do music decoding in the 195 * provides enough cpu time to do music decoding in the
180 * background 196 * background
181 */ 197 */
182 instance.startForeground(R.string.notification, instance.notification); 198 instance.startForegroundCompat(R.string.notification, instance.notification);
183 } 199 }
184 } 200 }
185 201
@@ -187,8 +203,66 @@ public class RockboxService extends Service
187 { 203 {
188 if (instance.notification != null) 204 if (instance.notification != null)
189 { 205 {
190 instance.stopForeground(true); 206 instance.stopForegroundCompat(R.string.notification);
191 instance.mNM.cancel(R.string.notification); 207 instance.mNM.cancel(R.string.notification);
192 } 208 }
193 } 209 }
210
211 /**
212 * This is a wrapper around the new startForeground method, using the older
213 * APIs if it is not available.
214 */
215 void startForegroundCompat(int id, Notification notification) {
216 // If we have the new startForeground API, then use it.
217 if (mStartForeground != null) {
218 mStartForegroundArgs[0] = Integer.valueOf(id);
219 mStartForegroundArgs[1] = notification;
220 try {
221 mStartForeground.invoke(this, mStartForegroundArgs);
222 } catch (InvocationTargetException e) {
223 // Should not happen.
224 Log.w("ApiDemos", "Unable to invoke startForeground", e);
225 } catch (IllegalAccessException e) {
226 // Should not happen.
227 Log.w("ApiDemos", "Unable to invoke startForeground", e);
228 }
229 return;
230 }
231
232 // Fall back on the old API.
233 setForeground(true);
234 mNM.notify(id, notification);
235 }
236
237 /**
238 * This is a wrapper around the new stopForeground method, using the older
239 * APIs if it is not available.
240 */
241 void stopForegroundCompat(int id) {
242 // If we have the new stopForeground API, then use it.
243 if (mStopForeground != null) {
244 mStopForegroundArgs[0] = Boolean.TRUE;
245 try {
246 mStopForeground.invoke(this, mStopForegroundArgs);
247 } catch (InvocationTargetException e) {
248 // Should not happen.
249 Log.w("ApiDemos", "Unable to invoke stopForeground", e);
250 } catch (IllegalAccessException e) {
251 // Should not happen.
252 Log.w("ApiDemos", "Unable to invoke stopForeground", e);
253 }
254 return;
255 }
256
257 // Fall back on the old API. Note to cancel BEFORE changing the
258 // foreground state, since we could be killed at that point.
259 mNM.cancel(id);
260 setForeground(false);
261 }
262
263 @Override
264 public void onDestroy() {
265 // Make sure our notification is gone.
266 stopForegroundCompat(R.string.notification);
267 }
194} 268}