Do not connect to server when user/pass is missing
[fonbot.git] / src / ro / ieval / fonbot / FonBotMainService.java
index 421caa7e03dfbfc90ebbf2981889b7d4cfbb40cd..5508557d749fcc5699312dbc54e2e1144710acf0 100644 (file)
@@ -3,12 +3,16 @@ 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;
 
+import javax.net.ssl.SSLException;
+
 import org.eclipse.jdt.annotation.Nullable;
 
+import ro.ieval.fonbot.Address.Protocol;
 import ro.ieval.fonbot.Utils.OngoingEvent;
 
 import android.app.Notification;
@@ -19,7 +23,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 +74,18 @@ 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 SSLException e){
+                                       Log.d("LongPollRunnable", "Got SSLException, refreshing connection");
                                } catch (final Exception ex){
                                        ex.printStackTrace();
                                        break;
@@ -107,6 +103,21 @@ public final class FonBotMainService extends Service {
         */
        public static final String ACTION_DELETE_ONGOING="ro.ieval.fonbot.FonBotMainService.ACTION_DELETE_ONGOING";
 
+       /**
+        * Broadcast action: process a command received via SMS
+        */
+       public static final String ACTION_PROCESS_COMMAND="ro.ieval.fonbot.FonBotMainService.ACTION_PROCESS_COMMAND";
+
+       /**
+        * Extra: command line
+        */
+       public static final String EXTRA_COMMAND_LINE="ro.ieval.fonbot.FonBotMainService.EXTRA_COMMAND_LINE";
+
+       /**
+        * Extra: SMS originating address
+        */
+       public static final String EXTRA_SMS_ORIGIN_ADDRESS="ro.ieval.fonbot.FonBotMainService.EXTRA_SMS_ORIGIN_ADDRESS";
+
        /**
         * Extra: ongoing event id
         *
@@ -184,12 +195,27 @@ public final class FonBotMainService extends Service {
                        LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent(ACTION_ONGOING_UPDATE));
                        updateNotification=true;
                }
+               if(intent!=null && intent.getAction()==ACTION_PROCESS_COMMAND) {
+                       final String cmdline = intent.getStringExtra(EXTRA_COMMAND_LINE);
+                       final String origin  = intent.getStringExtra(EXTRA_SMS_ORIGIN_ADDRESS);
+                       final Address address= new Address(toNonNull(Protocol.SMS), origin);//NOPMD variable depends on originAddress
+                       final String[] words=Utils.shellwords(toNonNull(cmdline));
+                       final String[] args=new String[words.length-1];//NOPMD variable size depends on words.length
+                       System.arraycopy(words, 1, args, 0, args.length);
+
+                       Utils.processCommand(this, toNonNull(words[0]), args, toNonNull(address));
+               }
 
-               if(longPollThread == null || !longPollThread.isAlive()){
+               final String user=PreferenceManager.getDefaultSharedPreferences(this).getString("username", null);
+               final String password=PreferenceManager.getDefaultSharedPreferences(this).getString("password", null);
+               final boolean has_user_pass = user != null && password != null && user.length() > 0 && password.length() > 0;
+               if((longPollThread == null || !longPollThread.isAlive()) && has_user_pass){
                        longPollThread = new Thread(new LongPollRunnable());
                        longPollThread.start();
                }
 
+               ExecutableRunnable.retryTasks();
+
                final boolean runForeground=PreferenceManager.getDefaultSharedPreferences(this).getBoolean("foreground", false);
                if(!runForeground)
                        stopForeground(true);
This page took 0.011693 seconds and 4 git commands to generate.