improve status request/callback interaction and status UI layout

This commit is contained in:
Nathan Freitas 2015-06-22 12:25:37 -04:00
parent 67d1652e57
commit e132a79a61
4 changed files with 181 additions and 107 deletions

View File

@ -4,6 +4,8 @@
xmlns:android="http://schemas.android.com/apk/res/android" xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent" android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:background="@color/panel_background_main"
> >
<android.support.v7.widget.Toolbar <android.support.v7.widget.Toolbar
@ -123,45 +125,58 @@
</RelativeLayout> </RelativeLayout>
</LinearLayout> </LinearLayout>
<FrameLayout
android:layout_width="match_parent" <ProgressBar
android:layout_height="0dp" android:id="@+id/pbConnecting"
android:layout_weight="1" style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:background="@color/panel_background_main" android:layout_height="wrap_content"
> android:layout_marginTop="60dp"
<org.torproject.android.ui.ImageProgressView android:visibility="gone"
android:id="@+id/imgStatus"
android:layout_width="match_parent" android:layout_gravity="center_horizontal|center_vertical"/>
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:padding="0dp"
android:src="@drawable/toroff"
android:layout_gravity="center_horizontal|center_vertical" />
<TextView <LinearLayout
android:id="@+id/lblStatus" android:layout_width="match_parent"
android:textColor="@android:color/white" android:layout_height="wrap_content"
android:layout_width="wrap_content" android:id="@+id/frameMain"
android:layout_height="wrap_content" android:visibility="visible"
android:textSize="16sp" android:orientation="vertical"
android:gravity="center_vertical" android:layout_gravity="center_horizontal|center_vertical"
android:fontFamily="sans-serif-light" >
android:text="" <org.torproject.android.ui.ImageProgressView
android:layout_gravity="center_horizontal|bottom" android:id="@+id/imgStatus"
android:layout_margin="10dp" android:layout_width="match_parent"
android:layout_height="0dp"
/> android:layout_weight=".8"
android:padding="0dp"
android:layout_margin="0dp"
android:src="@drawable/toroff" />
<TextView
android:id="@+id/lblStatus"
android:textColor="@android:color/white"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="14sp"
android:gravity="center_horizontal"
android:fontFamily="sans-serif-light"
android:text=""
android:lines="2"
android:maxLines="2"
android:layout_gravity="center_horizontal"
android:layout_margin="0dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
/>
</FrameLayout>
<LinearLayout android:gravity="center_horizontal" <LinearLayout android:gravity="center_horizontal"
android:orientation="horizontal" android:layout_width="match_parent" android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:padding="5dp" android:padding="5dp"
android:background="@color/panel_background_dark" android:background="@color/panel_background_dark"
android:layout_gravity="bottom"
> >
<Button <Button
@ -203,8 +218,11 @@
/> />
</LinearLayout> </LinearLayout>
</LinearLayout>
</LinearLayout> </LinearLayout>
</LinearLayout> </LinearLayout>
<LinearLayout <LinearLayout
android:layout_width="320dp" android:layout_width="320dp"
android:layout_height="match_parent" android:layout_height="match_parent"

View File

@ -54,6 +54,7 @@ import android.view.View;
import android.view.View.OnClickListener; import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener; import android.view.View.OnLongClickListener;
import android.view.View.OnTouchListener; import android.view.View.OnTouchListener;
import android.view.Window;
import android.view.animation.AccelerateInterpolator; import android.view.animation.AccelerateInterpolator;
import android.widget.Button; import android.widget.Button;
import android.widget.TextView; import android.widget.TextView;
@ -84,7 +85,7 @@ public class OrbotMainActivity extends Activity
private Toolbar mToolbar; private Toolbar mToolbar;
/* Some tracking bits */ /* Some tracking bits */
private String torStatus = TorServiceConstants.STATUS_OFF; //latest status reported from the tor service private String torStatus = null; //latest status reported from the tor service
private Intent lastStatusIntent; // the last ACTION_STATUS Intent received private Intent lastStatusIntent; // the last ACTION_STATUS Intent received
private SharedPreferences mPrefs = null; private SharedPreferences mPrefs = null;
@ -102,7 +103,7 @@ public class OrbotMainActivity extends Activity
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
mPrefs = TorServiceUtils.getSharedPrefs(getApplicationContext()); mPrefs = TorServiceUtils.getSharedPrefs(getApplicationContext());
/* Create the widgets before registering for broadcasts to guarantee /* Create the widgets before registering for broadcasts to guarantee
* that the widgets exist when the status updates try to update them */ * that the widgets exist when the status updates try to update them */
@ -149,6 +150,7 @@ public class OrbotMainActivity extends Activity
if (action.equals(TorServiceConstants.LOCAL_ACTION_LOG)) { if (action.equals(TorServiceConstants.LOCAL_ACTION_LOG)) {
Message msg = mStatusUpdateHandler.obtainMessage(STATUS_UPDATE); Message msg = mStatusUpdateHandler.obtainMessage(STATUS_UPDATE);
msg.obj = intent.getStringExtra(TorServiceConstants.LOCAL_EXTRA_LOG); msg.obj = intent.getStringExtra(TorServiceConstants.LOCAL_EXTRA_LOG);
msg.getData().putString("status", intent.getStringExtra(TorServiceConstants.EXTRA_STATUS));
mStatusUpdateHandler.sendMessage(msg); mStatusUpdateHandler.sendMessage(msg);
} else if (action.equals(TorServiceConstants.LOCAL_ACTION_BANDWIDTH)) { } else if (action.equals(TorServiceConstants.LOCAL_ACTION_BANDWIDTH)) {
@ -162,13 +164,16 @@ public class OrbotMainActivity extends Activity
msg.getData().putLong("upload", upload); msg.getData().putLong("upload", upload);
msg.getData().putLong("readTotal", read); msg.getData().putLong("readTotal", read);
msg.getData().putLong("writeTotal", written); msg.getData().putLong("writeTotal", written);
msg.getData().putString("status", intent.getStringExtra(TorServiceConstants.EXTRA_STATUS));
mStatusUpdateHandler.sendMessage(msg); mStatusUpdateHandler.sendMessage(msg);
} else if (action.equals(TorServiceConstants.ACTION_STATUS)) { } else if (action.equals(TorServiceConstants.ACTION_STATUS)) {
lastStatusIntent = intent; lastStatusIntent = intent;
torStatus = intent.getStringExtra(TorServiceConstants.EXTRA_STATUS);
Message msg = mStatusUpdateHandler.obtainMessage(STATUS_UPDATE); Message msg = mStatusUpdateHandler.obtainMessage(STATUS_UPDATE);
msg.obj = ""; msg.getData().putString("status", intent.getStringExtra(TorServiceConstants.EXTRA_STATUS));
mStatusUpdateHandler.sendMessage(msg); mStatusUpdateHandler.sendMessage(msg);
} }
} }
@ -506,9 +511,9 @@ public class OrbotMainActivity extends Activity
// Get intent, action and MIME type // Get intent, action and MIME type
Intent intent = getIntent(); Intent intent = getIntent();
String action = intent.getAction(); String action = intent.getAction();
Log.e(TAG, "handleIntents " + action); Log.d(TAG, "handleIntents " + action);
String type = intent.getType(); //String type = intent.getType();
if (action == null) if (action == null)
return; return;
@ -556,21 +561,19 @@ public class OrbotMainActivity extends Activity
else if (action.equals("org.torproject.android.START_TOR")) else if (action.equals("org.torproject.android.START_TOR"))
{ {
autoStartFromIntent = true; autoStartFromIntent = true;
try {
startTor(); startTor();
Intent resultIntent; Intent resultIntent;
if (lastStatusIntent == null) { if (lastStatusIntent == null) {
resultIntent = new Intent(intent); resultIntent = new Intent(intent);
} else { } else {
resultIntent = lastStatusIntent; resultIntent = lastStatusIntent;
}
resultIntent.putExtra(TorServiceConstants.EXTRA_STATUS, torStatus);
setResult(RESULT_OK, resultIntent);
finish();
} catch (RemoteException e) {
e.printStackTrace();
} }
resultIntent.putExtra(TorServiceConstants.EXTRA_STATUS, torStatus);
setResult(RESULT_OK, resultIntent);
finish();
} }
else if (action.equals(Intent.ACTION_VIEW)) else if (action.equals(Intent.ACTION_VIEW))
{ {
@ -590,7 +593,7 @@ public class OrbotMainActivity extends Activity
} }
} }
updateStatus(""); updateStatus(null);
setIntent(null); setIntent(null);
@ -1003,18 +1006,10 @@ public class OrbotMainActivity extends Activity
mBtnBridges.setChecked(Prefs.bridgesEnabled()); mBtnBridges.setChecked(Prefs.bridgesEnabled());
} }
mStatusUpdateHandler.postDelayed(new Runnable () requestTorStatus();
{
public void run ()
{
handleIntents();
} updateStatus(null);
}
, 500);
} }
AlertDialog aDialog = null; AlertDialog aDialog = null;
@ -1059,13 +1054,23 @@ public class OrbotMainActivity extends Activity
*/ */
private void updateStatus(String torServiceMsg) { private void updateStatus(String torServiceMsg) {
if (torStatus == null)
return; //UI not init'd yet
if (torStatus == TorServiceConstants.STATUS_ON) { if (torStatus == TorServiceConstants.STATUS_ON) {
imgStatus.setImageResource(R.drawable.toron); imgStatus.setImageResource(R.drawable.toron);
mBtnBrowser.setEnabled(true); mBtnBrowser.setEnabled(true);
// everything is running, clear the status message if (torServiceMsg != null)
lblStatus.setText(""); {
if (torServiceMsg.contains(TorServiceConstants.LOG_NOTICE_HEADER))
lblStatus.setText(torServiceMsg);
}
else
lblStatus.setText(getString(R.string.status_activated));
boolean showFirstTime = mPrefs.getBoolean("connect_first_time", true); boolean showFirstTime = mPrefs.getBoolean("connect_first_time", true);
@ -1082,23 +1087,30 @@ public class OrbotMainActivity extends Activity
{ {
autoStartFromIntent = false; autoStartFromIntent = false;
finish(); finish();
Log.e(TAG, "autoStartFromIntent finish"); Log.d(TAG, "autoStartFromIntent finish");
} }
} else if (torStatus == TorServiceConstants.STATUS_STARTING) { } else if (torStatus == TorServiceConstants.STATUS_STARTING) {
imgStatus.setImageResource(R.drawable.torstarting); imgStatus.setImageResource(R.drawable.torstarting);
// only show Tor daemon's percentage complete messages if (torServiceMsg != null)
if (torServiceMsg.indexOf('%') != -1) { {
lblStatus.setText(torServiceMsg); if (torServiceMsg.contains(TorServiceConstants.LOG_NOTICE_BOOTSTRAPPED))
} else if (torServiceMsg.contains("tart")) { // Start and start lblStatus.setText(torServiceMsg);
lblStatus.setText(torServiceMsg);
} }
else
lblStatus.setText(getString(R.string.status_starting_up));
mBtnBrowser.setEnabled(false); mBtnBrowser.setEnabled(false);
} else if (torStatus == TorServiceConstants.STATUS_STOPPING) { } else if (torStatus == TorServiceConstants.STATUS_STOPPING) {
if (torServiceMsg != null && torServiceMsg.contains(TorServiceConstants.LOG_NOTICE_HEADER))
lblStatus.setText(torServiceMsg);
imgStatus.setImageResource(R.drawable.torstarting); imgStatus.setImageResource(R.drawable.torstarting);
lblStatus.setText(torServiceMsg); lblStatus.setText(torServiceMsg);
mBtnBrowser.setEnabled(false); mBtnBrowser.setEnabled(false);
@ -1121,22 +1133,32 @@ public class OrbotMainActivity extends Activity
* {@link TorServiceConstants#ACTION_START} {@link Intent} to * {@link TorServiceConstants#ACTION_START} {@link Intent} to
* {@link TorService} * {@link TorService}
*/ */
private void startTor() throws RemoteException { private void startTor() {
sendIntentToService(TorServiceConstants.ACTION_START); sendIntentToService(TorServiceConstants.ACTION_START);
} }
/**
* Request tor status without starting it
* {@link TorServiceConstants#ACTION_START} {@link Intent} to
* {@link TorService}
*/
private void requestTorStatus() {
sendIntentToService(TorServiceConstants.ACTION_STATUS);
}
public boolean onLongClick(View view) { public boolean onLongClick(View view) {
try {
if (torStatus == TorServiceConstants.STATUS_OFF) { if (torStatus == TorServiceConstants.STATUS_OFF) {
startTor(); lblStatus.setText(getString(R.string.status_starting_up));
} else { startTor();
stopTor(); } else {
} lblStatus.setText(getString(R.string.status_shutting_down));
return true;
} catch (RemoteException e) { stopTor();
Log.d(TAG, "error onclick", e);
} }
return false;
return true;
} }
// 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
@ -1145,11 +1167,30 @@ public class OrbotMainActivity extends Activity
@Override @Override
public void handleMessage(final Message msg) { public void handleMessage(final Message msg) {
String newTorStatus = msg.getData().getString("status");
String log = (String)msg.obj;
if (torStatus == null && newTorStatus != null) //first time status
{
torStatus = newTorStatus;
findViewById(R.id.pbConnecting).setVisibility(View.GONE);
findViewById(R.id.frameMain).setVisibility(View.VISIBLE);
updateStatus(log);
//now you can handle the intents properly
handleIntents();
}
else if (newTorStatus != null && !torStatus.equals(newTorStatus)) //status changed
{
torStatus = newTorStatus;
updateStatus(log);
}
else if (log != null) //it is just a log
updateStatus(log);
switch (msg.what) { switch (msg.what) {
case STATUS_UPDATE:
updateStatus((String) msg.obj);
break;
case MESSAGE_TRAFFIC_COUNT: case MESSAGE_TRAFFIC_COUNT:
Bundle data = msg.getData(); Bundle data = msg.getData();

View File

@ -346,7 +346,11 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
replyWithStatus(mIntent); replyWithStatus(mIntent);
startTor(); startTor();
// stopTor() is called when the Service is destroyed // stopTor() is called when the Service is destroyed
} else if (action.equals(CMD_SIGNAL_HUP)) { }
else if (action.equals(ACTION_STATUS)) {
replyWithStatus(mIntent);
}
else if (action.equals(CMD_SIGNAL_HUP)) {
requestTorRereadConfig(); requestTorRereadConfig();
} else if (action.equals(CMD_NEWNYM)) { } else if (action.equals(CMD_NEWNYM)) {
newIdentity(); newIdentity();
@ -609,10 +613,6 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
public void onCreate() { public void onCreate() {
super.onCreate(); super.onCreate();
sendCallbackStatus(STATUS_STARTING);
sendCallbackLogMessage(getString(R.string.status_starting_up));
logNotice(getString(R.string.status_starting_up));
try try
{ {
mNumberFormat = NumberFormat.getInstance(Locale.getDefault()); //localized numbers! mNumberFormat = NumberFormat.getInstance(Locale.getDefault()); //localized numbers!
@ -760,9 +760,7 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
*/ */
private void replyWithStatus(Intent startRequest) { private void replyWithStatus(Intent startRequest) {
String packageName = startRequest.getStringExtra(EXTRA_PACKAGE_NAME); String packageName = startRequest.getStringExtra(EXTRA_PACKAGE_NAME);
if (TextUtils.isEmpty(packageName)) {
return;
}
Intent reply = new Intent(ACTION_STATUS); Intent reply = new Intent(ACTION_STATUS);
reply.putExtra(EXTRA_STATUS, mCurrentStatus); reply.putExtra(EXTRA_STATUS, mCurrentStatus);
reply.putExtra(EXTRA_SOCKS_PROXY, "socks://127.0.0.1:" + mPortSOCKS); reply.putExtra(EXTRA_SOCKS_PROXY, "socks://127.0.0.1:" + mPortSOCKS);
@ -771,8 +769,18 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
reply.putExtra(EXTRA_HTTP_PROXY, "http://127.0.0.1" + mPortHTTP); reply.putExtra(EXTRA_HTTP_PROXY, "http://127.0.0.1" + mPortHTTP);
reply.putExtra(EXTRA_HTTP_PROXY_HOST, "127.0.0.1"); reply.putExtra(EXTRA_HTTP_PROXY_HOST, "127.0.0.1");
reply.putExtra(EXTRA_HTTP_PROXY_PORT, mPortHTTP); reply.putExtra(EXTRA_HTTP_PROXY_PORT, mPortHTTP);
reply.setPackage(packageName);
sendBroadcast(reply); if (packageName != null)
{
reply.setPackage(packageName);
sendBroadcast(reply);
}
else
{
LocalBroadcastManager.getInstance(this).sendBroadcast(reply);
}
} }
/** /**
@ -784,18 +792,20 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
// these states should probably be handled better // these states should probably be handled better
sendCallbackLogMessage("Ignoring start request, currently " + mCurrentStatus); sendCallbackLogMessage("Ignoring start request, currently " + mCurrentStatus);
return; return;
} else if (mCurrentStatus == STATUS_ON) { } else if (mCurrentStatus == STATUS_ON && findExistingTorDaemon()) {
sendCallbackLogMessage("Ignoring start request, already started."); sendCallbackLogMessage("Ignoring start request, already started.");
return; return;
} }
if (findExistingTorDaemon()) {
return; // an old tor is already running, nothing to do
}
// make sure there are no stray daemons running // make sure there are no stray daemons running
killAllDaemons(); killAllDaemons();
sendCallbackStatus(STATUS_STARTING);
sendCallbackLogMessage(getString(R.string.status_starting_up));
logNotice(getString(R.string.status_starting_up));
try { try {
if (fileTor == null) if (fileTor == null)
initBinariesAndDirectories(); initBinariesAndDirectories();
@ -1882,9 +1892,10 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
Intent intent = new Intent(LOCAL_ACTION_BANDWIDTH); Intent intent = new Intent(LOCAL_ACTION_BANDWIDTH);
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);
intent.putExtra(EXTRA_STATUS, mCurrentStatus);
LocalBroadcastManager.getInstance(this).sendBroadcast(intent); LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
} }
@ -1895,6 +1906,8 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
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);
intent.putExtra(EXTRA_STATUS, mCurrentStatus);
LocalBroadcastManager.getInstance(this).sendBroadcast(intent); LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
} }

View File

@ -64,7 +64,9 @@ public interface TorServiceConstants {
//control port //control port
public final static String TOR_CONTROL_PORT_MSG_BOOTSTRAP_DONE = "Bootstrapped 100%"; public final static String TOR_CONTROL_PORT_MSG_BOOTSTRAP_DONE = "Bootstrapped 100%";
public final static String LOG_NOTICE_HEADER = "NOTICE";
public final static String LOG_NOTICE_BOOTSTRAPPED = "Bootstrapped";
/** /**
* A request to Orbot to transparently start Tor services * A request to Orbot to transparently start Tor services
*/ */