Moved WidgetUpdater to background thread

This commit is contained in:
daniel oeh 2012-09-23 13:00:02 +02:00
parent 7a0297f937
commit 4a472ff3bb
1 changed files with 30 additions and 6 deletions

View File

@ -25,7 +25,7 @@ public class PlayerWidgetService extends Service {
private PlaybackService playbackService; private PlaybackService playbackService;
/** True while service is updating the widget */ /** True while service is updating the widget */
private boolean isUpdating; private volatile boolean isUpdating;
public PlayerWidgetService() { public PlayerWidgetService() {
} }
@ -58,13 +58,11 @@ public class PlayerWidgetService extends Service {
@Override @Override
public int onStartCommand(Intent intent, int flags, int startId) { public int onStartCommand(Intent intent, int flags, int startId) {
if (!isUpdating) { if (!isUpdating) {
isUpdating = true;
if (playbackService == null && PlaybackService.isRunning) { if (playbackService == null && PlaybackService.isRunning) {
bindService(new Intent(this, PlaybackService.class), bindService(new Intent(this, PlaybackService.class),
mConnection, 0); mConnection, 0);
} else { } else {
updateViews(); startViewUpdaterIfNotRunning();
isUpdating = false;
} }
} else { } else {
if (AppConfig.DEBUG) if (AppConfig.DEBUG)
@ -75,6 +73,7 @@ public class PlayerWidgetService extends Service {
} }
private void updateViews() { private void updateViews() {
isUpdating = true;
if (AppConfig.DEBUG) if (AppConfig.DEBUG)
Log.d(TAG, "Updating widget views"); Log.d(TAG, "Updating widget views");
ComponentName playerWidget = new ComponentName(this, PlayerWidget.class); ComponentName playerWidget = new ComponentName(this, PlayerWidget.class);
@ -112,6 +111,7 @@ public class PlayerWidgetService extends Service {
} }
manager.updateAppWidget(playerWidget, views); manager.updateAppWidget(playerWidget, views);
isUpdating = false;
} }
/** Creates an intent which fakes a mediabutton press */ /** Creates an intent which fakes a mediabutton press */
@ -137,8 +137,7 @@ public class PlayerWidgetService extends Service {
Log.d(TAG, "Connection to service established"); Log.d(TAG, "Connection to service established");
playbackService = ((PlaybackService.LocalBinder) service) playbackService = ((PlaybackService.LocalBinder) service)
.getService(); .getService();
updateViews(); startViewUpdaterIfNotRunning();
isUpdating = false;
} }
@Override @Override
@ -149,5 +148,30 @@ public class PlayerWidgetService extends Service {
} }
}; };
private void startViewUpdaterIfNotRunning() {
if (!isUpdating) {
ViewUpdater updateThread = new ViewUpdater(this);
updateThread.start();
}
}
static class ViewUpdater extends Thread {
private static final String THREAD_NAME = "ViewUpdater";
private PlayerWidgetService service;
public ViewUpdater(PlayerWidgetService service) {
super();
setName(THREAD_NAME);
this.service = service;
}
@Override
public void run() {
service.updateViews();
}
}
} }