From f09379d86c3c529e77b057627a34577208d35e7b Mon Sep 17 00:00:00 2001 From: Hans-Christoph Steiner Date: Mon, 8 Jun 2015 20:04:29 -0400 Subject: [PATCH] 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. --- .../torproject/android/OrbotMainActivity.java | 20 +++++++++---------- .../android/service/TorService.java | 12 +++++------ .../android/service/TorServiceConstants.java | 6 ++++++ .../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 15faf31b..fa176556 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 7a97e70a..012bbef6 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 29ac47bf..a269ef01 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 4a24d3f1..fbf05d2b 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); }