Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Reliable lifecycle for BroadcastExecutiveService.

  • Loading branch information...
commit 9eb0f71be7d6a6950c59cbd5361e984204183e29 1 parent ca9fd65
@sblom authored
Showing with 51 additions and 36 deletions.
  1. +51 −36 src/com/greenesse/mytracksplugin/BroadcastExecutiveService.java
View
87 src/com/greenesse/mytracksplugin/BroadcastExecutiveService.java
@@ -1,5 +1,8 @@
package com.greenesse.mytracksplugin;
+import java.util.concurrent.ArrayBlockingQueue;
+import java.util.concurrent.BlockingQueue;
+
import com.google.android.apps.mytracks.services.ITrackRecordingService;
import android.app.IntentService;
@@ -12,40 +15,64 @@
import android.util.Log;
import android.view.View;
-public class BroadcastExecutiveService extends IntentService {
- public BroadcastExecutiveService(String name) {
- super(name);
- }
-
- public BroadcastExecutiveService() {
- // TODO: WTF should this be?
- super("BroadcaseExecutiveService");
- }
-
+public class BroadcastExecutiveService extends Service {
private ITrackRecordingService mytracksService;
private Intent myTracksServiceIntent;
- private Intent pending;
+ private BlockingQueue<Intent> tasks = new ArrayBlockingQueue<Intent>(2);
private ServiceConnection connection = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
- Log.i("MyTracksPlugin", "Service disconnected.");
+ Log.i("MyTracksPlugin", "Service disconnected, reconnecting.");
+ startAndBindMyTracksService();
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.i("MyTracksPlugin", "Service connected.");
- synchronized (this) {
- mytracksService = ITrackRecordingService.Stub.asInterface(service);
- if (pending != null) {
- onHandleIntent(pending);
- pending = null;
- }
- }
+ mytracksService = ITrackRecordingService.Stub.asInterface(service);
+ processTasks();
}
};
+ private void processTasks() {
+ Intent intent;
+ try {
+ intent = tasks.take();
+ while (intent != null) {
+ if ("start".equals(intent.getAction())) {
+ start();
+ }
+ else if ("stop".equals(intent.getAction())) {
+ stop();
+ }
+
+ intent = tasks.poll();
+
+ if (intent == null) {
+ Log.i("BES queue", "Work queue is empty; quitting.");
+ stopSelf();
+ return;
+ }
+ }
+ } catch (InterruptedException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ }
+
+ @Override
+ public IBinder onBind(Intent intent) {
+ return null;
+ }
+
+ @Override
+ public int onStartCommand(Intent intent, int flags, int startId) {
+ tasks.add(intent);
+ return super.onStartCommand(intent, flags, startId);
+ }
+
@Override
public void onCreate() {
super.onCreate();
@@ -55,29 +82,17 @@ public void onCreate() {
getString(R.string.mytracks_service_package),
getString(R.string.mytracks_service_class)));
- startService(myTracksServiceIntent);
+ startAndBindMyTracksService();
+ }
+
+ private void startAndBindMyTracksService() {
+ startService(myTracksServiceIntent);
if (!bindService(myTracksServiceIntent, connection, 0)) {
Log.e("MyTracksPlugin", "Couldn't bind to service.");
}
}
@Override
- protected void onHandleIntent(Intent intent) {
- synchronized (this) {
- if (mytracksService == null) {
- pending = intent;
- return;
- }
- }
- if ("start".equals(intent.getAction())) {
- start();
- }
- else if ("stop".equals(intent.getAction())) {
- stop();
- }
- }
-
- @Override
public void onDestroy() {
unbindService(connection);
super.onDestroy();
Please sign in to comment.
Something went wrong with that request. Please try again.