summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Martitz <kugel@rockbox.org>2011-10-22 15:28:09 +0000
committerThomas Martitz <kugel@rockbox.org>2011-10-22 15:28:09 +0000
commit4d2ab32339fb8e7c02841e52d35498081503d188 (patch)
tree16ae417c7bc5666aea97a5da41d839e831967ecb
parent5c509d17db86e889560e7d938b5a7acf083948e2 (diff)
downloadrockbox-4d2ab32339fb8e7c02841e52d35498081503d188.tar.gz
rockbox-4d2ab32339fb8e7c02841e52d35498081503d188.zip
Android: Work around an android bug.
BitmapFactory.decodeFile() fails when called outside the main thread. Work around by running the code on the main thread. This also has the advantage of off-loading from the Rockbox thread which should improve track skipping delays. git-svn-id: svn://svn.rockbox.org/rockbox/trunk@30825 a1c6a512-1295-4272-9138-f99709370657
-rw-r--r--android/src/org/rockbox/Helper/RunForegroundManager.java89
1 files changed, 53 insertions, 36 deletions
diff --git a/android/src/org/rockbox/Helper/RunForegroundManager.java b/android/src/org/rockbox/Helper/RunForegroundManager.java
index 5384e1b595..5e3f38eb84 100644
--- a/android/src/org/rockbox/Helper/RunForegroundManager.java
+++ b/android/src/org/rockbox/Helper/RunForegroundManager.java
@@ -12,6 +12,7 @@ import android.content.res.Resources;
12import android.graphics.Bitmap; 12import android.graphics.Bitmap;
13import android.graphics.BitmapFactory; 13import android.graphics.BitmapFactory;
14import android.graphics.drawable.Drawable; 14import android.graphics.drawable.Drawable;
15import android.os.Handler;
15import android.widget.RemoteViews; 16import android.widget.RemoteViews;
16 17
17public class RunForegroundManager 18public class RunForegroundManager
@@ -23,6 +24,7 @@ public class RunForegroundManager
23 private NotificationManager mNM; 24 private NotificationManager mNM;
24 private IRunForeground api; 25 private IRunForeground api;
25 private Service mCurrentService; 26 private Service mCurrentService;
27 private Handler mServiceHandler;
26 private Intent mWidgetUpdate; 28 private Intent mWidgetUpdate;
27 private int iconheight; 29 private int iconheight;
28 30
@@ -60,6 +62,7 @@ public class RunForegroundManager
60 Logger.e("Cannot run in foreground: No available API"); 62 Logger.e("Cannot run in foreground: No available API");
61 } 63 }
62 } 64 }
65 mServiceHandler = new Handler(service.getMainLooper());
63 } 66 }
64 67
65 public void startForeground() 68 public void startForeground()
@@ -88,44 +91,58 @@ public class RunForegroundManager
88 mWidgetUpdate = null; 91 mWidgetUpdate = null;
89 } 92 }
90 93
91 public void updateNotification(String title, String artist, String album, String albumart) 94 public void updateNotification(final String title, final String artist, final String album, final String albumart)
92 { 95 {
93 RemoteViews views = mNotification.contentView; 96 /* do this on the main thread for 2 reasons
94 views.setTextViewText(R.id.title, title); 97 * 1) Don't delay track switching with possibly costly albumart
95 views.setTextViewText(R.id.content, artist+"\n"+album); 98 * loading (i.e. off-load from the Rockbox thread)
96 if (artist.equals("")) 99 * 2) Work around a bug in Android where decodeFile() fails outside
97 mNotification.tickerText = title; 100 * of the main thread (http://stackoverflow.com/q/7228633)
98 else 101 */
99 mNotification.tickerText = title+" - "+artist; 102 mServiceHandler.post(new Runnable()
100 103 {
101 if (albumart != null) { 104 @Override
102 /* The notification area doesn't have permissions to access the SD card. 105 public void run()
103 * Push the data as Bitmap instead of Uri. Scale down to size of 106 {
104 * launcher icon -- broadcasting the unscaled image may yield in 107 final RemoteViews views = mNotification.contentView;
105 * too much data, causing UI hangs of Rockbox. */ 108 views.setTextViewText(R.id.title, title);
106 Bitmap b = BitmapFactory.decodeFile(albumart); 109 views.setTextViewText(R.id.content, artist+"\n"+album);
107 if(b != null) { 110 if (artist.equals(""))
108 /* scale width to keep aspect ratio -- height is the constraint */ 111 mNotification.tickerText = title;
109 int scaledwidth = Math.round(iconheight*((float)b.getWidth()/b.getHeight())); 112 else
110 views.setImageViewBitmap(R.id.artwork, 113 mNotification.tickerText = title+" - "+artist;
111 Bitmap.createScaledBitmap(b, scaledwidth, iconheight, false)); 114
112 } 115 if (albumart != null) {
113 else { 116 /* The notification area doesn't have permissions to access the SD card.
114 views.setImageViewResource(R.id.artwork, R.drawable.launcher); 117 * Push the data as Bitmap instead of Uri. Scale down to size of
118 * launcher icon -- broadcasting the unscaled image may yield in
119 * too much data, causing UI hangs of Rockbox. */
120 Bitmap b = BitmapFactory.decodeFile(albumart);
121 if(b != null) {
122 /* scale width to keep aspect ratio -- height is the constraint */
123 int scaledwidth = Math.round(iconheight*((float)b.getWidth()/b.getHeight()));
124 views.setImageViewBitmap(R.id.artwork,
125 Bitmap.createScaledBitmap(b, scaledwidth, iconheight, false));
126 }
127 else {
128 views.setImageViewResource(R.id.artwork, R.drawable.launcher);
129 }
130 }
131 else {
132 views.setImageViewResource(R.id.artwork, R.drawable.launcher);
133 }
134 mWidgetUpdate = new Intent("org.rockbox.TrackUpdateInfo");
135 mWidgetUpdate.putExtra("title", title);
136 mWidgetUpdate.putExtra("artist", artist);
137 mWidgetUpdate.putExtra("album", album);
138 mWidgetUpdate.putExtra("albumart", albumart);
139 mCurrentService.sendBroadcast(mWidgetUpdate);
140
141 /* notify in this runnable to make sure the notification
142 * has the correct albumart */
143 mNM.notify(R.string.notification, mNotification);
115 } 144 }
116 } 145 });
117 else {
118 views.setImageViewResource(R.id.artwork, R.drawable.launcher);
119 }
120
121 mNM.notify(R.string.notification, mNotification);
122
123 mWidgetUpdate = new Intent("org.rockbox.TrackUpdateInfo");
124 mWidgetUpdate.putExtra("title", title);
125 mWidgetUpdate.putExtra("artist", artist);
126 mWidgetUpdate.putExtra("album", album);
127 mWidgetUpdate.putExtra("albumart", albumart);
128 mCurrentService.sendBroadcast(mWidgetUpdate);
129 } 146 }
130 147
131 public void resendUpdateNotification() 148 public void resendUpdateNotification()