[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [orbot/master] create String constants for Intent actions and extras



commit f09379d86c3c529e77b057627a34577208d35e7b
Author: Hans-Christoph Steiner <hans@xxxxxxx>
Date:   Mon Jun 8 20:04:29 2015 -0400

    create String constants for Intent actions and extras
    
    Following the Android system naming convention, this uses constants for
    the action and extra names for Intents.  This makes it much easier to track
    which "log" is which, since there are "log" actions, extras, and messages.
---
 src/org/torproject/android/OrbotMainActivity.java  |   20 ++++++++++----------
 src/org/torproject/android/service/TorService.java |   12 +++++-------
 .../android/service/TorServiceConstants.java       |    6 ++++++
 .../torproject/android/ui/OrbotLogActivity.java    |    7 ++++---
 4 files changed, 25 insertions(+), 20 deletions(-)

diff --git a/src/org/torproject/android/OrbotMainActivity.java b/src/org/torproject/android/OrbotMainActivity.java
index 15faf31..fa17655 100644
--- a/src/org/torproject/android/OrbotMainActivity.java
+++ b/src/org/torproject/android/OrbotMainActivity.java
@@ -105,14 +105,14 @@ public class OrbotMainActivity extends Activity implements OrbotConstants, OnLon
         
     	doLayout();
 
-  	  // Register to receive messages.
-  	  // We are registering an observer (mMessageReceiver) to receive Intents
-  	  // with actions named "custom-event-name".
-  	  LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver,
-  	      new IntentFilter("status"));
+    	/* receive the internal status broadcasts, which are separate from the public
+    	 * status broadcasts to prevent other apps from sending fake/wrong status
+    	 * info to this app */
+    	LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver,
+  	      new IntentFilter(TorServiceConstants.LOCAL_ACTION_STATUS));
   	  
 		LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver,
-			      new IntentFilter("log"));
+			      new IntentFilter(TorServiceConstants.LOCAL_ACTION_LOG));
 
 		mHandler.postDelayed(new Runnable ()
 		{
@@ -155,9 +155,9 @@ public class OrbotMainActivity extends Activity implements OrbotConstants, OnLon
       public void onReceive(Context context, Intent intent) {
         // Get extra data included in the Intent
           
-        if (intent.hasExtra("log"))
+        if (intent.hasExtra(TorServiceConstants.LOCAL_EXTRA_LOG))
         {
-            String log = intent.getStringExtra("log");
+            String log = intent.getStringExtra(TorServiceConstants.LOCAL_EXTRA_LOG);
             updateStatus(log);
         }
         else if (intent.hasExtra("up"))
@@ -175,9 +175,9 @@ public class OrbotMainActivity extends Activity implements OrbotConstants, OnLon
             mHandler.sendMessage(msg);
             
         }
-        else if (intent.hasExtra("status"))
+        else if (intent.hasExtra(TorServiceConstants.EXTRA_STATUS))
         {
-            torStatus = intent.getStringExtra("status");
+            torStatus = intent.getStringExtra(TorServiceConstants.EXTRA_STATUS);
             updateStatus("");
         }
         
diff --git a/src/org/torproject/android/service/TorService.java b/src/org/torproject/android/service/TorService.java
index 7a97e70..012bbef 100644
--- a/src/org/torproject/android/service/TorService.java
+++ b/src/org/torproject/android/service/TorService.java
@@ -2083,7 +2083,7 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
     {
          
         
-        Intent intent = new Intent("log");
+        Intent intent = new Intent(LOCAL_ACTION_LOG);
         // You can also include some extra data.
         intent.putExtra("up",upload);
       intent.putExtra("down",download);
@@ -2100,19 +2100,17 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
     {
          
         
-        Intent intent = new Intent("log");
+        Intent intent = new Intent(LOCAL_ACTION_LOG);
           // You can also include some extra data.
-          intent.putExtra("log", logMessage);
+          intent.putExtra(LOCAL_EXTRA_LOG, logMessage);
           LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
 
     }
     
     private void sendCallbackStatus(String currentStatus) {
-        Intent intent = new Intent("status"); // TODO rename to proper action
-        // You can also include some extra data.
-        intent.putExtra("status", currentStatus);
+        Intent intent = new Intent(ACTION_STATUS);
+        intent.putExtra(EXTRA_STATUS, currentStatus);
         LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
-
     }
     
     /*
diff --git a/src/org/torproject/android/service/TorServiceConstants.java b/src/org/torproject/android/service/TorServiceConstants.java
index 29ac47b..a269ef0 100644
--- a/src/org/torproject/android/service/TorServiceConstants.java
+++ b/src/org/torproject/android/service/TorServiceConstants.java
@@ -67,6 +67,12 @@ public interface TorServiceConstants {
     //control port 
     public final static String TOR_CONTROL_PORT_MSG_BOOTSTRAP_DONE = "Bootstrapped 100%";
 
+    public final static String ACTION_STATUS = "org.torproject.android.intent.action.STATUS";
+    public final static String EXTRA_STATUS = "org.torproject.android.intent.extra.STATUS";
+    public final static String LOCAL_ACTION_STATUS = "status";
+    public final static String LOCAL_ACTION_LOG = "log";
+    public final static String LOCAL_EXTRA_LOG = "log";
+
     public final static String STATUS_OFF = "OFF";
     public final static String STATUS_ON = "ON";
     public final static String STATUS_CONNECTING = "CONNECTING";
diff --git a/src/org/torproject/android/ui/OrbotLogActivity.java b/src/org/torproject/android/ui/OrbotLogActivity.java
index 4a24d3f..fbf05d2 100644
--- a/src/org/torproject/android/ui/OrbotLogActivity.java
+++ b/src/org/torproject/android/ui/OrbotLogActivity.java
@@ -4,6 +4,7 @@
 package org.torproject.android.ui;
 
 import org.torproject.android.OrbotConstants;
+import org.torproject.android.service.TorServiceConstants;
 
 import android.app.Activity;
 import android.content.BroadcastReceiver;
@@ -23,7 +24,7 @@ public class OrbotLogActivity extends Activity implements OrbotConstants
         
     	doLayout();
 		LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver,
-			      new IntentFilter("log"));
+			      new IntentFilter(TorServiceConstants.LOCAL_ACTION_LOG));
 
 		
 	}
@@ -38,9 +39,9 @@ public class OrbotLogActivity extends Activity implements OrbotConstants
 	  public void onReceive(Context context, Intent intent) {
 	    // Get extra data included in the Intent
 		  
-		if (intent.hasExtra("log"))
+		if (intent.hasExtra(TorServiceConstants.LOCAL_EXTRA_LOG))
 		{
-			String log = intent.getStringExtra("log");
+			String log = intent.getStringExtra(TorServiceConstants.LOCAL_EXTRA_LOG);
 			updateStatus(log);
 		}
 		



_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits