diff --git a/src/org/torproject/android/OrbotMainActivity.java b/src/org/torproject/android/OrbotMainActivity.java index a73e0db9..c5401632 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 516b56e7..089e2c65 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 mLogBuffer = new ArrayList(); - + 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 745d74f7..a79b7d78 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";