]> iEval git - fonbot.git/blobdiff - src/ro/ieval/fonbot/FonBotMainService.java
Prevent against future use-up-lots-of-mobile-data bugs
[fonbot.git] / src / ro / ieval / fonbot / FonBotMainService.java
index e0bc6c0cf5452b7a76868843a167be08a9d372b6..421caa7e03dfbfc90ebbf2981889b7d4cfbb40cd 100644 (file)
@@ -19,6 +19,7 @@ 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;
@@ -70,16 +71,28 @@ 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 {
-                                       runnable.run();
+                                       if(lastRun + MIN_MILLIS_BETWEEN_RUNS > System.currentTimeMillis())
+                                               SystemClock.sleep(lastRun + MIN_MILLIS_BETWEEN_RUNS - System.currentTimeMillis());
+                                       else
+                                               runnable.run();
                                } catch (final Exception ex){
                                        ex.printStackTrace();
+                                       break;
                                }
                        Log.d("LongPollRunnable", "Long polling stopped");
                }
@@ -93,8 +106,7 @@ public final class FonBotMainService extends Service {
         * Broadcast action: remove an ongoing event
         */
        public static final String ACTION_DELETE_ONGOING="ro.ieval.fonbot.FonBotMainService.ACTION_DELETE_ONGOING";
-       /** Broadcast action: trigger a server poll */
-       public static final String ACTION_TRIGGER_POLL="ro.ieval.fonbot.FonBotMainService.ACTION_TRIGGER_POLL";
+
        /**
         * Extra: ongoing event id
         *
@@ -160,13 +172,14 @@ public final class FonBotMainService extends Service {
 
        @Override
        public int onStartCommand(final @Nullable Intent intent, final int flags, final int startId) {
+               final boolean showOngoing=PreferenceManager.getDefaultSharedPreferences(this).getBoolean("ongoing", false);
                boolean updateNotification = false;
-               if(intent!=null && intent.getAction()==ACTION_PUT_ONGOING && intent.hasExtra(EXTRA_ONGOING_ID)){
+               if(intent!=null && intent.getAction()==ACTION_PUT_ONGOING && intent.hasExtra(EXTRA_ONGOING_ID) && showOngoing){
                        ongoing.add(OngoingEvent.values()[intent.getIntExtra(EXTRA_ONGOING_ID, 0)]);
                        LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent(ACTION_ONGOING_UPDATE));
                        updateNotification=true;
                }
-               if(intent!=null && intent.getAction()==ACTION_DELETE_ONGOING && intent.hasExtra(EXTRA_ONGOING_ID)){
+               if(intent!=null && intent.getAction()==ACTION_DELETE_ONGOING && intent.hasExtra(EXTRA_ONGOING_ID) && showOngoing){
                        ongoing.remove(OngoingEvent.values()[intent.getIntExtra(EXTRA_ONGOING_ID, 0)]);
                        LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent(ACTION_ONGOING_UPDATE));
                        updateNotification=true;
@@ -194,7 +207,7 @@ public final class FonBotMainService extends Service {
                                        setContentIntent(PendingIntent.getActivity(this, 0, mainIntent, 0)).
                                        setOngoing(true);
 
-                       if(!ongoing.isEmpty()) {
+                       if(showOngoing && !ongoing.isEmpty()) {
                                final NotificationCompat.InboxStyle inboxBuilder=new NotificationCompat.InboxStyle(builder);
 
                                for(final OngoingEvent event : ongoing)
This page took 0.025358 seconds and 4 git commands to generate.