]> iEval git - fonbot.git/blobdiff - src/ro/ieval/fonbot/FonBotMainService.java
Make FonBot usable
[fonbot.git] / src / ro / ieval / fonbot / FonBotMainService.java
index 421caa7e03dfbfc90ebbf2981889b7d4cfbb40cd..dd13affe715aef6f403d1fba71fc6c87e00bf8ac 100644 (file)
@@ -3,6 +3,7 @@ package ro.ieval.fonbot;
 import static ro.ieval.fonbot.R.string.*;
 import static ro.ieval.fonbot.Utils.toNonNull;
 
+import java.net.SocketTimeoutException;
 import java.util.Collections;
 import java.util.EnumSet;
 import java.util.Set;
@@ -19,7 +20,6 @@ import android.content.Intent;
 import android.content.IntentFilter;
 import android.net.ConnectivityManager;
 import android.os.IBinder;
-import android.os.SystemClock;
 import android.preference.PreferenceManager;
 import android.support.v4.app.NotificationCompat;
 import android.support.v4.content.LocalBroadcastManager;
@@ -71,25 +71,16 @@ public final class FonBotMainService extends Service {
         * @author Marius Gavrilescu <marius@ieval.ro>
         */
        private final class LongPollRunnable implements Runnable{
-               /**
-                * Minimum time between two runs of the long polling service, in milliseconds
-                *
-                * This ensures the long polling service won't use up all your mobile data.
-                */
-               private static final int MIN_MILLIS_BETWEEN_RUNS = 10000;
-
                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");
-                       long lastRun = 0;
                        while(man.getActiveNetworkInfo() != null && man.getActiveNetworkInfo().isConnected())
                                try {
-                                       if(lastRun + MIN_MILLIS_BETWEEN_RUNS > System.currentTimeMillis())
-                                               SystemClock.sleep(lastRun + MIN_MILLIS_BETWEEN_RUNS - System.currentTimeMillis());
-                                       else
-                                               runnable.run();
+                                       runnable.doRun();
+                               } catch (final SocketTimeoutException e){
+                                       Log.d("LongPollRunnable", "Socket timeout, refreshing connection");
                                } catch (final Exception ex){
                                        ex.printStackTrace();
                                        break;
@@ -190,6 +181,8 @@ public final class FonBotMainService extends Service {
                        longPollThread.start();
                }
 
+               ExecutableRunnable.retryTasks();
+
                final boolean runForeground=PreferenceManager.getDefaultSharedPreferences(this).getBoolean("foreground", false);
                if(!runForeground)
                        stopForeground(true);
This page took 0.018986 seconds and 4 git commands to generate.