commit e132a79a610deb730713b203299ce783533e3aaa
Author: Nathan Freitas <nat...@freitas.net>
Date:   Mon Jun 22 12:25:37 2015 -0400

    improve status request/callback interaction and status UI layout
---
 res/layout/layout_main.xml                         |   80 +++++++----
 src/org/torproject/android/OrbotMainActivity.java  |  149 +++++++++++++-------
 src/org/torproject/android/service/TorService.java |   53 ++++---
 .../android/service/TorServiceConstants.java       |    4 +-
 4 files changed, 180 insertions(+), 106 deletions(-)

diff --git a/res/layout/layout_main.xml b/res/layout/layout_main.xml
index 1b46e55..a0fc1a6 100644
--- a/res/layout/layout_main.xml
+++ b/res/layout/layout_main.xml
@@ -4,6 +4,8 @@
      xmlns:android="http://schemas.android.com/apk/res/android";
      android:orientation="vertical" android:layout_width="match_parent"
       android:layout_height="match_parent"
+                android:background="@color/panel_background_main"
+      
       >
       
     <android.support.v7.widget.Toolbar
@@ -123,45 +125,58 @@
                </RelativeLayout>
                    </LinearLayout>
                 
-       <FrameLayout
+        
+       <ProgressBar
+   android:id="@+id/pbConnecting"
+   style="?android:attr/progressBarStyleLarge"
+   android:layout_width="wrap_content"
+   android:layout_height="wrap_content"
+               android:layout_marginTop="60dp"
+                       android:visibility="gone"         
+               
+android:layout_gravity="center_horizontal|center_vertical"/>
+       
+       <LinearLayout
                android:layout_width="match_parent"
-               android:layout_height="0dp"
-               android:layout_weight="1"
-               
-          android:background="@color/panel_background_main"
-           >
-       <org.torproject.android.ui.ImageProgressView
-               android:id="@+id/imgStatus"
-               android:layout_width="match_parent"
                android:layout_height="wrap_content"
-               android:layout_marginTop="5dp"
-               android:padding="0dp"
-               
-               android:src="@drawable/toroff"
-               android:layout_gravity="center_horizontal|center_vertical" />
-       
-               <TextView
-                       android:id="@+id/lblStatus"
-                       android:textColor="@android:color/white"
-                       android:layout_width="wrap_content"
-                       android:layout_height="wrap_content"
-                       android:textSize="16sp"
-                       android:gravity="center_vertical"
-                       android:fontFamily="sans-serif-light"
-                       android:text=""
-                                       
android:layout_gravity="center_horizontal|bottom"
-                       android:layout_margin="10dp"
-                       
-                        />
+               android:id="@+id/frameMain"
+               android:visibility="visible"    
+                android:orientation="vertical"   
+               android:layout_gravity="center_horizontal|center_vertical"
+                >
+                      <org.torproject.android.ui.ImageProgressView
+                               android:id="@+id/imgStatus"
+                               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"
             android:orientation="horizontal" 
android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:padding="5dp"
                    android:background="@color/panel_background_dark"
-       
+                       android:layout_gravity="bottom"
              >
        
                        <Button
@@ -203,8 +218,11 @@
                            />
                         
                    </LinearLayout>
+                    </LinearLayout>
+       
            </LinearLayout>
            </LinearLayout>
+           
         <LinearLayout
     android:layout_width="320dp"
     android:layout_height="match_parent"
diff --git a/src/org/torproject/android/OrbotMainActivity.java 
b/src/org/torproject/android/OrbotMainActivity.java
index e13f55d..54fe5f1 100644
--- a/src/org/torproject/android/OrbotMainActivity.java
+++ b/src/org/torproject/android/OrbotMainActivity.java
@@ -54,6 +54,7 @@ import android.view.View;
 import android.view.View.OnClickListener;
 import android.view.View.OnLongClickListener;
 import android.view.View.OnTouchListener;
+import android.view.Window;
 import android.view.animation.AccelerateInterpolator;
 import android.widget.Button;
 import android.widget.TextView;
@@ -84,7 +85,7 @@ public class OrbotMainActivity extends Activity
        private Toolbar mToolbar;
                
     /* 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 SharedPreferences mPrefs = null;
@@ -102,7 +103,7 @@ public class OrbotMainActivity extends Activity
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
 
-        mPrefs = TorServiceUtils.getSharedPrefs(getApplicationContext());      
  
+        mPrefs = TorServiceUtils.getSharedPrefs(getApplicationContext());
 
         /* Create the widgets before registering for broadcasts to guarantee
          * 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)) {
                 Message msg = 
mStatusUpdateHandler.obtainMessage(STATUS_UPDATE);
                 msg.obj = 
intent.getStringExtra(TorServiceConstants.LOCAL_EXTRA_LOG);
+                msg.getData().putString("status", 
intent.getStringExtra(TorServiceConstants.EXTRA_STATUS));
                 mStatusUpdateHandler.sendMessage(msg);
 
             } 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("readTotal", read);
                 msg.getData().putLong("writeTotal", written);
+                msg.getData().putString("status", 
intent.getStringExtra(TorServiceConstants.EXTRA_STATUS));
+
                 mStatusUpdateHandler.sendMessage(msg);
 
             } else if (action.equals(TorServiceConstants.ACTION_STATUS)) {
                 lastStatusIntent = intent;
-                torStatus = 
intent.getStringExtra(TorServiceConstants.EXTRA_STATUS);
+                
                 Message msg = 
mStatusUpdateHandler.obtainMessage(STATUS_UPDATE);
-                msg.obj = "";
+                msg.getData().putString("status", 
intent.getStringExtra(TorServiceConstants.EXTRA_STATUS));
+
                 mStatusUpdateHandler.sendMessage(msg);
             }
         }
@@ -506,9 +511,9 @@ public class OrbotMainActivity extends Activity
            // Get intent, action and MIME type
            Intent intent = getIntent();
            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)
                        return;
@@ -556,21 +561,19 @@ public class OrbotMainActivity extends Activity
                else if (action.equals("org.torproject.android.START_TOR"))
                {
                        autoStartFromIntent = true;
-            try {
-                startTor();
+          
+            startTor();
 
-                Intent resultIntent;
-                if (lastStatusIntent == null) {
-                    resultIntent = new Intent(intent);
-                } else {
-                    resultIntent = lastStatusIntent;
-                }
-                resultIntent.putExtra(TorServiceConstants.EXTRA_STATUS, 
torStatus);
-                setResult(RESULT_OK, resultIntent);
-                finish();
-            } catch (RemoteException e) {
-                e.printStackTrace();
+            Intent resultIntent;
+            if (lastStatusIntent == null) {
+                resultIntent = new Intent(intent);
+            } else {
+                resultIntent = lastStatusIntent;
             }
+            resultIntent.putExtra(TorServiceConstants.EXTRA_STATUS, torStatus);
+            setResult(RESULT_OK, resultIntent);
+            finish();
+          
                }
                else if (action.equals(Intent.ACTION_VIEW))
                {
@@ -590,7 +593,7 @@ public class OrbotMainActivity extends Activity
                        }
                }
                
-               updateStatus("");
+               updateStatus(null);
                
                setIntent(null);
                
@@ -1003,18 +1006,10 @@ public class OrbotMainActivity extends Activity
                        mBtnBridges.setChecked(Prefs.bridgesEnabled());
         }
 
-        mStatusUpdateHandler.postDelayed(new Runnable ()
-        {
-            public void run ()
-            {
-        
-                handleIntents();
+               requestTorStatus();
 
-            }
-        }
-        , 500);
-        
-        
+               updateStatus(null);
+               
     }
 
     AlertDialog aDialog = null;
@@ -1059,13 +1054,23 @@ public class OrbotMainActivity extends Activity
      */
     private void updateStatus(String torServiceMsg) {
 
+       if (torStatus == null)
+               return; //UI not init'd yet
+       
         if (torStatus == TorServiceConstants.STATUS_ON) {
+               
             imgStatus.setImageResource(R.drawable.toron);
 
             mBtnBrowser.setEnabled(true);
 
-            // everything is running, clear the status message
-            lblStatus.setText("");
+            if (torServiceMsg != null)
+            {
+               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);
 
@@ -1082,23 +1087,30 @@ public class OrbotMainActivity extends Activity
             {
                 autoStartFromIntent = false;
                 finish();
-                Log.e(TAG, "autoStartFromIntent finish");
+                Log.d(TAG, "autoStartFromIntent finish");
             }
+            
+            
 
         } else if (torStatus == TorServiceConstants.STATUS_STARTING) {
 
             imgStatus.setImageResource(R.drawable.torstarting);
 
-            // only show Tor daemon's percentage complete messages
-            if (torServiceMsg.indexOf('%') != -1) {
-                lblStatus.setText(torServiceMsg);
-            } else if (torServiceMsg.contains("tart")) { // Start and start
-                lblStatus.setText(torServiceMsg);
+            if (torServiceMsg != null)
+            {
+               if 
(torServiceMsg.contains(TorServiceConstants.LOG_NOTICE_BOOTSTRAPPED))
+                       lblStatus.setText(torServiceMsg);                       
    
             }
+            else
+               lblStatus.setText(getString(R.string.status_starting_up));
+            
             mBtnBrowser.setEnabled(false);
 
         } else if (torStatus == TorServiceConstants.STATUS_STOPPING) {
 
+                 if (torServiceMsg != null && 
torServiceMsg.contains(TorServiceConstants.LOG_NOTICE_HEADER))
+               lblStatus.setText(torServiceMsg);       
+                 
             imgStatus.setImageResource(R.drawable.torstarting);
             lblStatus.setText(torServiceMsg);
             mBtnBrowser.setEnabled(false);
@@ -1121,22 +1133,32 @@ public class OrbotMainActivity extends Activity
      * {@link TorServiceConstants#ACTION_START} {@link Intent} to
      * {@link TorService}
      */
-    private void startTor() throws RemoteException {
+    private void startTor() {
         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) {
-        try {
-            if (torStatus == TorServiceConstants.STATUS_OFF) {
-                startTor();
-            } else {
-                stopTor();
-            }
-            return true;
-        } catch (RemoteException e) {
-            Log.d(TAG, "error onclick", e);
+
+        if (torStatus == TorServiceConstants.STATUS_OFF) {
+            lblStatus.setText(getString(R.string.status_starting_up));
+            startTor();
+        } else {
+               lblStatus.setText(getString(R.string.status_shutting_down));
+               
+            stopTor();
         }
-        return false;
+        
+        return true;
+                
     }
 
 // 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
         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) {
-                case STATUS_UPDATE:
-                    updateStatus((String) msg.obj);
-                    break;
-
                 case MESSAGE_TRAFFIC_COUNT:
 
                     Bundle data = msg.getData();
diff --git a/src/org/torproject/android/service/TorService.java 
b/src/org/torproject/android/service/TorService.java
index f5fe6e1..bf065c3 100644
--- a/src/org/torproject/android/service/TorService.java
+++ b/src/org/torproject/android/service/TorService.java
@@ -346,7 +346,11 @@ public class TorService extends Service implements 
TorServiceConstants, OrbotCon
                     replyWithStatus(mIntent);
                     startTor();
                     // 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();
                 } else if (action.equals(CMD_NEWNYM)) {
                     newIdentity();
@@ -609,10 +613,6 @@ public class TorService extends Service implements 
TorServiceConstants, OrbotCon
     public void onCreate() {
         super.onCreate();
 
-        sendCallbackStatus(STATUS_STARTING);
-        sendCallbackLogMessage(getString(R.string.status_starting_up));
-        logNotice(getString(R.string.status_starting_up));
-
         try
         {
             mNumberFormat = NumberFormat.getInstance(Locale.getDefault()); 
//localized numbers!
@@ -760,9 +760,7 @@ public class TorService extends Service implements 
TorServiceConstants, OrbotCon
      */
     private void replyWithStatus(Intent startRequest) {
         String packageName = startRequest.getStringExtra(EXTRA_PACKAGE_NAME);
-        if (TextUtils.isEmpty(packageName)) {
-            return;
-        }
+
         Intent reply = new Intent(ACTION_STATUS);
         reply.putExtra(EXTRA_STATUS, mCurrentStatus);
         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_HOST, "127.0.0.1");
         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
             sendCallbackLogMessage("Ignoring start request, currently " + 
mCurrentStatus);
             return;
-        } else if (mCurrentStatus == STATUS_ON) {
+        } else if (mCurrentStatus == STATUS_ON && findExistingTorDaemon()) {
+        
             sendCallbackLogMessage("Ignoring start request, already started.");
+            
             return;
-        }
-
-        if (findExistingTorDaemon()) {
-            return; // an old tor is already running, nothing to do
-        }
-
+        }        
+        
         // make sure there are no stray daemons running
         killAllDaemons();
 
+        sendCallbackStatus(STATUS_STARTING);
+        sendCallbackLogMessage(getString(R.string.status_starting_up));
+        logNotice(getString(R.string.status_starting_up));
+        
         try {
         if (fileTor == null)
             initBinariesAndDirectories();
@@ -1882,9 +1892,10 @@ public class TorService extends Service implements 
TorServiceConstants, OrbotCon
         Intent intent = new Intent(LOCAL_ACTION_BANDWIDTH);
 
         intent.putExtra("up",upload);
-      intent.putExtra("down",download);
-      intent.putExtra("written",written);
-      intent.putExtra("read",read);
+             intent.putExtra("down",download);
+             intent.putExtra("written",written);
+             intent.putExtra("read",read);
+             intent.putExtra(EXTRA_STATUS, mCurrentStatus);
 
         LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
     }
@@ -1895,6 +1906,8 @@ public class TorService extends Service implements 
TorServiceConstants, OrbotCon
         Intent intent = new Intent(LOCAL_ACTION_LOG);
           // You can also include some extra data.
           intent.putExtra(LOCAL_EXTRA_LOG, logMessage);
+             intent.putExtra(EXTRA_STATUS, mCurrentStatus);
+
           LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
 
     }
diff --git a/src/org/torproject/android/service/TorServiceConstants.java 
b/src/org/torproject/android/service/TorServiceConstants.java
index adaa674..3d34deb 100644
--- a/src/org/torproject/android/service/TorServiceConstants.java
+++ b/src/org/torproject/android/service/TorServiceConstants.java
@@ -64,7 +64,9 @@ public interface TorServiceConstants {
 
     //control port 
     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
      */



_______________________________________________
tor-commits mailing list
tor-commits@lists.torproject.org
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits

Reply via email to