commit 90b731cc8d6c7935cf3ac95144e642b0b6eedc71 Author: Hans-Christoph Steiner hans@eds.org Date: Tue Jun 9 15:10:52 2015 -0400
strictly target local broadcasts
This sets an action for each kind of local broadcast, and uses the action to choose how to handle it. Before, it was a mix of the action and which extras the Intent included. --- src/org/torproject/android/OrbotMainActivity.java | 79 ++++++++++---------- src/org/torproject/android/service/TorService.java | 26 ++----- .../android/service/TorServiceConstants.java | 1 + 3 files changed, 48 insertions(+), 58 deletions(-)
diff --git a/src/org/torproject/android/OrbotMainActivity.java b/src/org/torproject/android/OrbotMainActivity.java index a73e0db..c540163 100644 --- a/src/org/torproject/android/OrbotMainActivity.java +++ b/src/org/torproject/android/OrbotMainActivity.java @@ -107,11 +107,13 @@ public class OrbotMainActivity extends Activity implements OrbotConstants, OnLon /* 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(mLocalBroadcastReceiver, - new IntentFilter(TorServiceConstants.LOCAL_ACTION_STATUS)); - - LocalBroadcastManager.getInstance(this).registerReceiver(mLocalBroadcastReceiver, - new IntentFilter(TorServiceConstants.LOCAL_ACTION_LOG)); + LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(this); + lbm.registerReceiver(mLocalBroadcastReceiver, + new IntentFilter(TorServiceConstants.LOCAL_ACTION_STATUS)); + lbm.registerReceiver(mLocalBroadcastReceiver, + new IntentFilter(TorServiceConstants.LOCAL_ACTION_BANDWIDTH)); + lbm.registerReceiver(mLocalBroadcastReceiver, + new IntentFilter(TorServiceConstants.LOCAL_ACTION_LOG)); } @@ -127,43 +129,44 @@ public class OrbotMainActivity extends Activity implements OrbotConstants, OnLon stopService(torService); }
- // Our handler for received Intents. This will be called whenever an Intent - // with an action named "custom-event-name" is broadcasted. + /** + * The state and log info from {@link TorService} are sent to the UI here in + * the form of a local broadcast. Regular broadcasts can be sent by any app, + * so local ones are used here so other apps cannot interfere with Orbot's + * operation. + */ private BroadcastReceiver mLocalBroadcastReceiver = new BroadcastReceiver() {
- @Override - public void onReceive(Context context, Intent intent) { - // Get extra data included in the Intent - - if (intent.hasExtra(TorServiceConstants.LOCAL_EXTRA_LOG)) - { - String log = intent.getStringExtra(TorServiceConstants.LOCAL_EXTRA_LOG); - updateStatus(log); - } - else if (intent.hasExtra("up")) - { - long upload = intent.getLongExtra("up",0); - long download = intent.getLongExtra("down",0); - long written = intent.getLongExtra("written",0); - long read = intent.getLongExtra("read",0); - - Message msg = mStatusUpdateHandler.obtainMessage(TorServiceConstants.MESSAGE_TRAFFIC_COUNT); - msg.getData().putLong("download", download); - msg.getData().putLong("upload", upload); - msg.getData().putLong("readTotal", read); - msg.getData().putLong("writeTotal", written); - mStatusUpdateHandler.sendMessage(msg); - - } - else if (intent.hasExtra(TorServiceConstants.EXTRA_STATUS)) - { - torStatus = intent.getStringExtra(TorServiceConstants.EXTRA_STATUS); - updateStatus(""); + @Override + public void onReceive(Context context, Intent intent) { + String action = intent.getAction(); + if (action == null) + return; + + if (action.equals(TorServiceConstants.LOCAL_ACTION_LOG)) { + String log = intent.getStringExtra(TorServiceConstants.LOCAL_EXTRA_LOG); + updateStatus(log); + + } else if (action.equals(TorServiceConstants.LOCAL_ACTION_BANDWIDTH)) { + long upload = intent.getLongExtra("up", 0); + long download = intent.getLongExtra("down", 0); + long written = intent.getLongExtra("written", 0); + long read = intent.getLongExtra("read", 0); + + Message msg = mStatusUpdateHandler + .obtainMessage(TorServiceConstants.MESSAGE_TRAFFIC_COUNT); + msg.getData().putLong("download", download); + msg.getData().putLong("upload", upload); + msg.getData().putLong("readTotal", read); + msg.getData().putLong("writeTotal", written); + mStatusUpdateHandler.sendMessage(msg); + + } else if (action.equals(TorServiceConstants.LOCAL_ACTION_STATUS)) { + torStatus = intent.getStringExtra(TorServiceConstants.EXTRA_STATUS); + updateStatus(""); + } } - - } }; -
private void doLayout () { diff --git a/src/org/torproject/android/service/TorService.java b/src/org/torproject/android/service/TorService.java index 516b56e..089e2c6 100644 --- a/src/org/torproject/android/service/TorService.java +++ b/src/org/torproject/android/service/TorService.java @@ -1441,7 +1441,7 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon lastWritten = written; lastRead = read;
- sendCallbackStatusMessage(lastWritten, lastRead, mTotalTrafficWritten, mTotalTrafficRead); + sendCallbackBandwidth(lastWritten, lastRead, mTotalTrafficWritten, mTotalTrafficRead); }
private String formatCount(long count) { @@ -1841,39 +1841,25 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon } catch (Exception ioe) { - logException("Unable to update Tor configuration: " + ioe.getMessage(),ioe); - }
return false; - }
- - - - - private void sendCallbackStatusMessage (long upload, long download, long written, long read) - { - - - Intent intent = new Intent(LOCAL_ACTION_LOG); - // You can also include some extra data. + private void sendCallbackBandwidth(long upload, long download, long written, long read) { + Intent intent = new Intent(LOCAL_ACTION_BANDWIDTH); + intent.putExtra("up",upload); intent.putExtra("down",download); intent.putExtra("written",written); intent.putExtra("read",read); - - LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
+ LocalBroadcastManager.getInstance(this).sendBroadcast(intent); } - - // private ArrayList<String> mLogBuffer = new ArrayList<String>(); - + private void sendCallbackLogMessage (String logMessage) { -
Intent intent = new Intent(LOCAL_ACTION_LOG); // You can also include some extra data. diff --git a/src/org/torproject/android/service/TorServiceConstants.java b/src/org/torproject/android/service/TorServiceConstants.java index 745d74f..a79b7d7 100644 --- a/src/org/torproject/android/service/TorServiceConstants.java +++ b/src/org/torproject/android/service/TorServiceConstants.java @@ -71,6 +71,7 @@ public interface TorServiceConstants { 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_ACTION_BANDWIDTH = "bandwidth"; public final static String LOCAL_EXTRA_LOG = "log";
public final static String STATUS_OFF = "OFF";