altosdroid: add timer to stop service
authorMike Beattie <mike@ethernal.org>
Tue, 28 Aug 2012 10:10:26 +0000 (22:10 +1200)
committerMike Beattie <mike@ethernal.org>
Tue, 28 Aug 2012 10:10:26 +0000 (22:10 +1200)
* Stops when no UI clients, and no bluetooth connection remains

Signed-off-by: Mike Beattie <mike@ethernal.org>
altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java

index d11fc53a8c47df6a6273b526fa4a13322af9425b..ffe96946772a114f92c770c1cfb768b09200244a 100644 (file)
@@ -20,6 +20,8 @@ package org.altusmetrum.AltosDroid;
 import java.lang.ref.WeakReference;
 import java.util.ArrayList;
 import java.util.concurrent.TimeoutException;
+import java.util.Timer;
+import java.util.TimerTask;
 
 import android.app.Notification;
 //import android.app.NotificationManager;
@@ -61,6 +63,9 @@ public class TelemetryService extends Service {
        private int NOTIFICATION = R.string.telemetry_service_label;
        //private NotificationManager mNM;
 
+       // Timer - we wake up every now and then to decide if the service should stop
+       private Timer timer = new Timer();
+
        ArrayList<Messenger> mClients = new ArrayList<Messenger>(); // Keeps track of all current registered clients.
        final Handler   mHandler   = new IncomingHandler(this);
        final Messenger mMessenger = new Messenger(mHandler); // Target we publish for clients to send messages to IncomingHandler.
@@ -204,12 +209,28 @@ public class TelemetryService extends Service {
        }
 
 
+       private void onTimerTick() {
+               if (D) Log.d(TAG, "Timer wakeup");
+               try {
+                       if (mClients.size() <= 0 && state != STATE_CONNECTED) {
+                               stopSelf();
+                       }
+               } catch (Throwable t) {
+                       Log.e(TAG, "Timer failed: ", t);
+               }
+       }
+
+
        @Override
        public void onCreate() {
                // Create a reference to the NotificationManager so that we can update our notifcation text later
                //mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
 
                setState(STATE_READY);
+
+               // Start our timer - first event in 10 seconds, then every 10 seconds after that.
+               timer.scheduleAtFixedRate(new TimerTask(){ public void run() {onTimerTick();}}, 10000L, 10000L);
+
        }
 
        @Override
@@ -248,6 +269,9 @@ public class TelemetryService extends Service {
                // Demote us from the foreground, and cancel the persistent notification.
                stopForeground(true);
 
+               // Stop our timer
+               if (timer != null) {timer.cancel();}
+
                // Tell the user we stopped.
                Toast.makeText(this, R.string.telemetry_service_stopped, Toast.LENGTH_SHORT).show();
        }