[tor-commits] [orbot/master] when OrbotMainActivity starts, query TorService for current status

n8fr8 at torproject.org n8fr8 at torproject.org
Thu Jun 25 14:59:59 UTC 2015


commit e5c2e1a040045781507fca48138d4bdaa672be10
Author: Hans-Christoph Steiner <hans at eds.org>
Date:   Wed Jun 17 11:00:48 2015 -0400

    when OrbotMainActivity starts, query TorService for current status
    
    If OrbotMainActivity gets killed while TorService is running, then when
    OrbotMainActivity starts again, it needs to get the current status from
    TorService to correctly represent things to the user.
---
 src/org/torproject/android/OrbotMainActivity.java  |    6 ++++-
 src/org/torproject/android/service/TorService.java |   28 +++++++++++++++-----
 .../android/service/TorServiceConstants.java       |    1 +
 3 files changed, 28 insertions(+), 7 deletions(-)

diff --git a/src/org/torproject/android/OrbotMainActivity.java b/src/org/torproject/android/OrbotMainActivity.java
index e13f55d..b1ea635 100644
--- a/src/org/torproject/android/OrbotMainActivity.java
+++ b/src/org/torproject/android/OrbotMainActivity.java
@@ -589,7 +589,11 @@ public class OrbotMainActivity extends Activity
 				}
 			}
 		}
-		
+        else if (action.equals(Intent.ACTION_MAIN))
+        {
+            // OrbotMainActivity was restarted after being killed
+            sendIntentToService(TorServiceConstants.CMD_STATUS);
+        }
 		updateStatus("");
 		
 		setIntent(null);
diff --git a/src/org/torproject/android/service/TorService.java b/src/org/torproject/android/service/TorService.java
index f5fe6e1..7830e5e 100644
--- a/src/org/torproject/android/service/TorService.java
+++ b/src/org/torproject/android/service/TorService.java
@@ -16,6 +16,7 @@ import android.app.PendingIntent;
 import android.app.Service;
 import android.content.BroadcastReceiver;
 import android.content.Context;
+import android.content.ContextWrapper;
 import android.content.Intent;
 import android.content.IntentFilter;
 import android.content.SharedPreferences;
@@ -346,6 +347,9 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
                     replyWithStatus(mIntent);
                     startTor();
                     // stopTor() is called when the Service is destroyed
+                } else if (action.equals(CMD_STATUS)) {
+                    Intent intent = getActionStatusIntent(mCurrentStatus);
+                    sendBroadcastOnlyToOrbot(intent);
                 } else if (action.equals(CMD_SIGNAL_HUP)) {
                     requestTorRereadConfig();
                 } else if (action.equals(CMD_NEWNYM)) {
@@ -1901,20 +1905,32 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
     
     private void sendCallbackStatus(String currentStatus) {
         mCurrentStatus = currentStatus;
-
-        Intent intent = new Intent(ACTION_STATUS);
-        intent.putExtra(EXTRA_STATUS, currentStatus);
+        Intent intent = getActionStatusIntent(currentStatus);
         // send for Orbot internals, using secure local broadcast
-        LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
+        sendBroadcastOnlyToOrbot(intent);
         // send for any apps that are interested
         sendBroadcast(intent);
     }
-    
+
+    /**
+     * Send a secure broadcast only to Orbot itself
+     * @see {@link ContextWrapper#sendBroadcast(Intent)}
+     * @see {@link LocalBroadcastManager}
+     */
+    private boolean sendBroadcastOnlyToOrbot(Intent intent) {
+        return LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
+    }
+
+    private Intent getActionStatusIntent(String currentStatus) {
+        Intent intent = new Intent(ACTION_STATUS);
+        intent.putExtra(EXTRA_STATUS, currentStatus);
+        return intent;
+    }
+
     /*
      *  Another way to do this would be to use the Observer pattern by defining the 
      *  BroadcastReciever in the Android manifest.
      */
-    
     private final BroadcastReceiver mNetworkStateReceiver = new BroadcastReceiver() {
         @Override
         public void onReceive(Context context, Intent intent) {
diff --git a/src/org/torproject/android/service/TorServiceConstants.java b/src/org/torproject/android/service/TorServiceConstants.java
index adaa674..fa26e10 100644
--- a/src/org/torproject/android/service/TorServiceConstants.java
+++ b/src/org/torproject/android/service/TorServiceConstants.java
@@ -119,6 +119,7 @@ public interface TorServiceConstants {
 
     // actions for internal command Intents
     public static final String CMD_SIGNAL_HUP = "signal_hup";
+    public static final String CMD_STATUS = "status";
     public static final String CMD_FLUSH = "flush";
     public static final String CMD_NEWNYM = "newnym";
     public static final String CMD_VPN = "vpn";





More information about the tor-commits mailing list