Replace GCM and polling with long polling
[fonbot.git] / src / ro / ieval / fonbot / FonBotMainService.java
index dba53894b38f28e8295d3152c798da4e6a35845d..e0bc6c0cf5452b7a76868843a167be08a9d372b6 100644 (file)
@@ -17,10 +17,12 @@ import android.app.PendingIntent;
 import android.app.Service;
 import android.content.Intent;
 import android.content.IntentFilter;
+import android.net.ConnectivityManager;
 import android.os.IBinder;
 import android.preference.PreferenceManager;
 import android.support.v4.app.NotificationCompat;
 import android.support.v4.content.LocalBroadcastManager;
+import android.util.Log;
 
 /*
  * Copyright © 2013 Marius Gavrilescu
@@ -62,6 +64,27 @@ public final class FonBotMainService extends Service {
                }
        }
 
+       /**
+        * Runnable that continously long polls the server for commands
+        *
+        * @author Marius Gavrilescu <marius@ieval.ro>
+        */
+       private final class LongPollRunnable implements Runnable{
+               public void run(){
+                       final ConnectivityManager man=(ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
+                       final HttpCallExecutableRunnable runnable=new HttpCallExecutableRunnable("/get", null, FonBotMainService.this, new PollResultCallback(FonBotMainService.this), false);
+
+                       Log.d("LongPollRunnable", "Long polling started");
+                       while(man.getActiveNetworkInfo() != null && man.getActiveNetworkInfo().isConnected())
+                               try {
+                                       runnable.run();
+                               } catch (final Exception ex){
+                                       ex.printStackTrace();
+                               }
+                       Log.d("LongPollRunnable", "Long polling stopped");
+               }
+       }
+
        /**
         * Broadcast action: add an ongoing event
         */
@@ -90,7 +113,6 @@ public final class FonBotMainService extends Service {
        private static final IntentFilter DYNAMIC_BROADCAST_FILTER=new IntentFilter();
 
        static{
-               DYNAMIC_BROADCAST_FILTER.addAction(Intent.ACTION_SCREEN_ON);
                DYNAMIC_BROADCAST_FILTER.addAction(Intent.ACTION_BATTERY_CHANGED);
                DYNAMIC_BROADCAST_FILTER.addAction(Intent.ACTION_HEADSET_PLUG);
        }
@@ -107,6 +129,9 @@ public final class FonBotMainService extends Service {
        /** true if running in foreground, false otherwise */
        private boolean isForeground = false;
 
+       /** Thread that runs a {@link LongPollRunnable} */
+       private Thread longPollThread;
+
        /**
         * Get the set of ongoing events.
         *
@@ -147,8 +172,10 @@ public final class FonBotMainService extends Service {
                        updateNotification=true;
                }
 
-               if(intent!=null && intent.getAction()==ACTION_TRIGGER_POLL)
-                       Utils.pollServer(this);
+               if(longPollThread == null || !longPollThread.isAlive()){
+                       longPollThread = new Thread(new LongPollRunnable());
+                       longPollThread.start();
+               }
 
                final boolean runForeground=PreferenceManager.getDefaultSharedPreferences(this).getBoolean("foreground", false);
                if(!runForeground)
This page took 0.010208 seconds and 4 git commands to generate.