force all UI status updates through mStatusUpdateHandler

The Handler is a message queue for the main thread, so it should help keep
the UI working while status updates are coming in.

* This removes the constants in TorServiceConstants because the Handler
  messages are only for OrbotMainActivity

* this uses the handy shortcut msg.obj for the status message
This commit is contained in:
Hans-Christoph Steiner 2015-06-09 17:02:21 -04:00
parent 5c2d4501fa
commit 25a6bb63b2
3 changed files with 24 additions and 57 deletions

View File

@ -16,11 +16,7 @@ public interface OrbotConstants {
public final static String URL_TOR_CHECK = "https://check.torproject.org"; public final static String URL_TOR_CHECK = "https://check.torproject.org";
public final static String URL_TOR_BRIDGES = "https://bridges.torproject.org/bridges?transport="; public final static String URL_TOR_BRIDGES = "https://bridges.torproject.org/bridges?transport=";
public final static String NEWLINE = "\n";
public final static String HANDLER_TOR_MSG = "torServiceMsg";
public final static String PREF_BRIDGES_UPDATED = "pref_bridges_enabled"; public final static String PREF_BRIDGES_UPDATED = "pref_bridges_enabled";
//public final static String PREF_BRIDGES_OBFUSCATED = "pref_bridges_obfuscated"; //public final static String PREF_BRIDGES_OBFUSCATED = "pref_bridges_obfuscated";
public final static String PREF_OR = "pref_or"; public final static String PREF_OR = "pref_or";

View File

@ -92,7 +92,11 @@ public class OrbotMainActivity extends Activity
private final static int REQUEST_VPN = 8888; private final static int REQUEST_VPN = 8888;
private final static int REQUEST_SETTINGS = 0x9874; private final static int REQUEST_SETTINGS = 0x9874;
// message types for mStatusUpdateHandler
private final static int STATUS_UPDATE = 1;
private static final int MESSAGE_TRAFFIC_COUNT = 2;
/** Called when the activity is first created. */ /** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
@ -141,8 +145,9 @@ public class OrbotMainActivity extends Activity
return; return;
if (action.equals(TorServiceConstants.LOCAL_ACTION_LOG)) { if (action.equals(TorServiceConstants.LOCAL_ACTION_LOG)) {
String log = intent.getStringExtra(TorServiceConstants.LOCAL_EXTRA_LOG); Message msg = mStatusUpdateHandler.obtainMessage(STATUS_UPDATE);
updateStatus(log); msg.obj = intent.getStringExtra(TorServiceConstants.LOCAL_EXTRA_LOG);
mStatusUpdateHandler.sendMessage(msg);
} else if (action.equals(TorServiceConstants.LOCAL_ACTION_BANDWIDTH)) { } else if (action.equals(TorServiceConstants.LOCAL_ACTION_BANDWIDTH)) {
long upload = intent.getLongExtra("up", 0); long upload = intent.getLongExtra("up", 0);
@ -150,8 +155,7 @@ public class OrbotMainActivity extends Activity
long written = intent.getLongExtra("written", 0); long written = intent.getLongExtra("written", 0);
long read = intent.getLongExtra("read", 0); long read = intent.getLongExtra("read", 0);
Message msg = mStatusUpdateHandler Message msg = mStatusUpdateHandler.obtainMessage(MESSAGE_TRAFFIC_COUNT);
.obtainMessage(TorServiceConstants.MESSAGE_TRAFFIC_COUNT);
msg.getData().putLong("download", download); msg.getData().putLong("download", download);
msg.getData().putLong("upload", upload); msg.getData().putLong("upload", upload);
msg.getData().putLong("readTotal", read); msg.getData().putLong("readTotal", read);
@ -160,7 +164,9 @@ public class OrbotMainActivity extends Activity
} else if (action.equals(TorServiceConstants.ACTION_STATUS)) { } else if (action.equals(TorServiceConstants.ACTION_STATUS)) {
torStatus = intent.getStringExtra(TorServiceConstants.EXTRA_STATUS); torStatus = intent.getStringExtra(TorServiceConstants.EXTRA_STATUS);
updateStatus(""); Message msg = mStatusUpdateHandler.obtainMessage(STATUS_UPDATE);
msg.obj = "";
mStatusUpdateHandler.sendMessage(msg);
} }
} }
}; };
@ -1122,8 +1128,8 @@ public class OrbotMainActivity extends Activity
lblStatus.setText(getString(R.string.status_starting_up)); lblStatus.setText(getString(R.string.status_starting_up));
//we send a message here to the progressDialog i believe, but we can clarify that shortly //we send a message here to the progressDialog i believe, but we can clarify that shortly
Message msg = mStatusUpdateHandler.obtainMessage(TorServiceConstants.ENABLE_TOR_MSG); Message msg = mStatusUpdateHandler.obtainMessage(STATUS_UPDATE);
msg.getData().putString(HANDLER_TOR_MSG, getString(R.string.status_starting_up)); msg.obj = getString(R.string.status_starting_up);
mStatusUpdateHandler.sendMessage(msg); mStatusUpdateHandler.sendMessage(msg);
} }
@ -1144,38 +1150,16 @@ public class OrbotMainActivity extends Activity
// this is what takes messages or values from the callback threads or other non-mainUI threads // this is what takes messages or values from the callback threads or other non-mainUI threads
//and passes them back into the main UI thread for display to the user //and passes them back into the main UI thread for display to the user
private Handler mStatusUpdateHandler = new Handler() { private Handler mStatusUpdateHandler = new Handler() {
private String lastServiceMsg = null; @Override
public void handleMessage(final Message msg) {
public void handleMessage(Message msg) {
switch (msg.what) { switch (msg.what) {
case TorServiceConstants.STATUS_MSG: case STATUS_UPDATE:
case TorServiceConstants.LOG_MSG: updateStatus((String) msg.obj);
String torServiceMsg = (String)msg.getData().getString(HANDLER_TOR_MSG);
if (lastServiceMsg == null || !lastServiceMsg.equals(torServiceMsg))
{
updateStatus(torServiceMsg);
lastServiceMsg = torServiceMsg;
}
break; break;
case TorServiceConstants.ENABLE_TOR_MSG:
updateStatus((String)msg.getData().getString(HANDLER_TOR_MSG));
break;
case TorServiceConstants.DISABLE_TOR_MSG:
updateStatus((String)msg.getData().getString(HANDLER_TOR_MSG));
break;
case TorServiceConstants.MESSAGE_TRAFFIC_COUNT : case MESSAGE_TRAFFIC_COUNT:
Bundle data = msg.getData(); Bundle data = msg.getData();
DataCount datacount = new DataCount(data.getLong("upload"),data.getLong("download")); DataCount datacount = new DataCount(data.getLong("upload"),data.getLong("download"));
@ -1190,12 +1174,9 @@ public class OrbotMainActivity extends Activity
super.handleMessage(msg); super.handleMessage(msg);
} }
} }
}; };
@Override @Override
protected void onDestroy() { protected void onDestroy() {
super.onDestroy(); super.onDestroy();
LocalBroadcastManager.getInstance(this).unregisterReceiver(mLocalBroadcastReceiver); LocalBroadcastManager.getInstance(this).unregisterReceiver(mLocalBroadcastReceiver);

View File

@ -78,12 +78,7 @@ public interface TorServiceConstants {
public final static String STATUS_ON = "ON"; public final static String STATUS_ON = "ON";
public final static String STATUS_STARTING = "STARTING"; public final static String STATUS_STARTING = "STARTING";
public final static String STATUS_STOPPING = "STOPPING"; public final static String STATUS_STOPPING = "STOPPING";
public static final int STATUS_MSG = 1;
public static final int ENABLE_TOR_MSG = 2;
public static final int DISABLE_TOR_MSG = 3;
public static final int LOG_MSG = 4;
public static final String CMD_START = "start"; public static final String CMD_START = "start";
public static final String CMD_SIGNAL_HUP = "signal_hup"; public static final String CMD_SIGNAL_HUP = "signal_hup";
public static final String CMD_FLUSH = "flush"; public static final String CMD_FLUSH = "flush";
@ -100,11 +95,6 @@ public interface TorServiceConstants {
public static final String OBFSCLIENT_ASSET_KEY = "obfs4proxy"; public static final String OBFSCLIENT_ASSET_KEY = "obfs4proxy";
public static final String MEEK_ASSET_KEY = "meek-client"; public static final String MEEK_ASSET_KEY = "meek-client";
public static final int MESSAGE_TRAFFIC_COUNT = 5;
//name of the iptables binary //name of the iptables binary
public final static String IPTABLES_ASSET_KEY = "xtables"; public final static String IPTABLES_ASSET_KEY = "xtables";