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.
This commit is contained in:
Hans-Christoph Steiner 2015-06-09 15:10:52 -04:00
parent 65d8801005
commit 90b731cc8d
3 changed files with 49 additions and 59 deletions

View File

@ -107,11 +107,13 @@ public class OrbotMainActivity extends Activity implements OrbotConstants, OnLon
/* receive the internal status broadcasts, which are separate from the public /* receive the internal status broadcasts, which are separate from the public
* status broadcasts to prevent other apps from sending fake/wrong status * status broadcasts to prevent other apps from sending fake/wrong status
* info to this app */ * info to this app */
LocalBroadcastManager.getInstance(this).registerReceiver(mLocalBroadcastReceiver, LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(this);
new IntentFilter(TorServiceConstants.LOCAL_ACTION_STATUS)); lbm.registerReceiver(mLocalBroadcastReceiver,
new IntentFilter(TorServiceConstants.LOCAL_ACTION_STATUS));
LocalBroadcastManager.getInstance(this).registerReceiver(mLocalBroadcastReceiver, lbm.registerReceiver(mLocalBroadcastReceiver,
new IntentFilter(TorServiceConstants.LOCAL_ACTION_LOG)); new IntentFilter(TorServiceConstants.LOCAL_ACTION_BANDWIDTH));
lbm.registerReceiver(mLocalBroadcastReceiver,
new IntentFilter(TorServiceConstants.LOCAL_ACTION_LOG));
} }
@ -127,44 +129,45 @@ public class OrbotMainActivity extends Activity implements OrbotConstants, OnLon
stopService(torService); 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() { private BroadcastReceiver mLocalBroadcastReceiver = new BroadcastReceiver() {
@Override @Override
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {
// Get extra data included in the Intent String action = intent.getAction();
if (action == null)
return;
if (intent.hasExtra(TorServiceConstants.LOCAL_EXTRA_LOG)) if (action.equals(TorServiceConstants.LOCAL_ACTION_LOG)) {
{ String log = intent.getStringExtra(TorServiceConstants.LOCAL_EXTRA_LOG);
String log = intent.getStringExtra(TorServiceConstants.LOCAL_EXTRA_LOG); updateStatus(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("");
}
} }
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("");
}
}
}; };
private void doLayout () private void doLayout ()
{ {
setContentView(R.layout.layout_main); setContentView(R.layout.layout_main);

View File

@ -1441,7 +1441,7 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
lastWritten = written; lastWritten = written;
lastRead = read; lastRead = read;
sendCallbackStatusMessage(lastWritten, lastRead, mTotalTrafficWritten, mTotalTrafficRead); sendCallbackBandwidth(lastWritten, lastRead, mTotalTrafficWritten, mTotalTrafficRead);
} }
private String formatCount(long count) { private String formatCount(long count) {
@ -1841,40 +1841,26 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
} }
catch (Exception ioe) catch (Exception ioe)
{ {
logException("Unable to update Tor configuration: " + ioe.getMessage(),ioe); logException("Unable to update Tor configuration: " + ioe.getMessage(),ioe);
} }
return false; return false;
} }
private void sendCallbackBandwidth(long upload, long download, long written, long read) {
Intent intent = new Intent(LOCAL_ACTION_BANDWIDTH);
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.
intent.putExtra("up",upload); intent.putExtra("up",upload);
intent.putExtra("down",download); intent.putExtra("down",download);
intent.putExtra("written",written); intent.putExtra("written",written);
intent.putExtra("read",read); 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) private void sendCallbackLogMessage (String logMessage)
{ {
Intent intent = new Intent(LOCAL_ACTION_LOG); Intent intent = new Intent(LOCAL_ACTION_LOG);
// You can also include some extra data. // You can also include some extra data.
intent.putExtra(LOCAL_EXTRA_LOG, logMessage); intent.putExtra(LOCAL_EXTRA_LOG, logMessage);

View File

@ -71,6 +71,7 @@ public interface TorServiceConstants {
public final static String EXTRA_STATUS = "org.torproject.android.intent.extra.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_STATUS = "status";
public final static String LOCAL_ACTION_LOG = "log"; 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 LOCAL_EXTRA_LOG = "log";
public final static String STATUS_OFF = "OFF"; public final static String STATUS_OFF = "OFF";