From 70615d669bc3dd56f83a5715a6cb215a5da882ee Mon Sep 17 00:00:00 2001 From: Nathan Freitas Date: Mon, 30 Jun 2014 15:45:14 -0400 Subject: [PATCH] fixes for status, service, binding and start/stop --- src/org/torproject/android/Orbot.java | 52 ++++++----- .../android/service/TorService.java | 86 ++++++++++--------- 2 files changed, 77 insertions(+), 61 deletions(-) diff --git a/src/org/torproject/android/Orbot.java b/src/org/torproject/android/Orbot.java index 83d8e427..106c20b7 100644 --- a/src/org/torproject/android/Orbot.java +++ b/src/org/torproject/android/Orbot.java @@ -34,7 +34,6 @@ import android.content.pm.PackageManager.NameNotFoundException; import android.content.res.Configuration; import android.net.Uri; import android.os.AsyncTask; -import android.os.Build; import android.os.Bundle; import android.os.Handler; import android.os.IBinder; @@ -82,7 +81,7 @@ public class Orbot extends ActionBarActivity implements TorConstants, OnLongClic /* Tor Service interaction */ /* The primary interface we will be calling on the service. */ ITorService mService = null; - + private SharedPreferences mPrefs = null; private boolean autoStartFromIntent = false; @@ -104,11 +103,10 @@ public class Orbot extends ActionBarActivity implements TorConstants, OnLongClic } - Intent torService; private void startService () { - torService = new Intent(this, TorService.class); + Intent torService = new Intent(this, TorService.class); startService(torService); bindService(torService, @@ -435,6 +433,7 @@ public class Orbot extends ActionBarActivity implements TorConstants, OnLongClic mConnection = null; mService = null; + } catch (RemoteException e) { Log.w(TAG, e); } @@ -645,7 +644,6 @@ public class Orbot extends ActionBarActivity implements TorConstants, OnLongClic } - torStatus = -1; updateStatus (""); } @@ -844,7 +842,7 @@ public class Orbot extends ActionBarActivity implements TorConstants, OnLongClic protected Integer doInBackground(String... params) { mTorServiceMsg = params[0]; - int newTorStatus = -1; + int newTorStatus = TorServiceConstants.STATUS_OFF; try { if (mService != null) @@ -966,20 +964,28 @@ public class Orbot extends ActionBarActivity implements TorConstants, OnLongClic mTxtOrbotLog.setText(""); + + if (mService != null) + { - // this is a bit of a strange/old/borrowed code/design i used to change the service state - // not sure it really makes sense when what we want to say is just "startTor" - mService.setProfile(TorServiceConstants.PROFILE_ON); //this means turn on - - //here we update the UI which is a bit sloppy and mixed up code wise - //might be best to just call updateStatus() instead of directly manipulating UI in this method - yep makes sense - imgStatus.setImageResource(R.drawable.torstarting); - lblStatus.setText(getString(R.string.status_starting_up)); - - //we send a message here to the progressDialog i believe, but we can clarify that shortly - Message msg = mHandler.obtainMessage(TorServiceConstants.ENABLE_TOR_MSG); - msg.getData().putString(HANDLER_TOR_MSG, getString(R.string.status_starting_up)); - mHandler.sendMessage(msg); + // this is a bit of a strange/old/borrowed code/design i used to change the service state + // not sure it really makes sense when what we want to say is just "startTor" + mService.setProfile(TorServiceConstants.PROFILE_ON); //this means turn on + + //here we update the UI which is a bit sloppy and mixed up code wise + //might be best to just call updateStatus() instead of directly manipulating UI in this method - yep makes sense + imgStatus.setImageResource(R.drawable.torstarting); + lblStatus.setText(getString(R.string.status_starting_up)); + + //we send a message here to the progressDialog i believe, but we can clarify that shortly + Message msg = mHandler.obtainMessage(TorServiceConstants.ENABLE_TOR_MSG); + msg.getData().putString(HANDLER_TOR_MSG, getString(R.string.status_starting_up)); + mHandler.sendMessage(msg); + } + else + { + showAlert(getString(R.string.error),"Tor Service has not started yet. Please wait and try again.",false); + } } @@ -1169,7 +1175,7 @@ public class Orbot extends ActionBarActivity implements TorConstants, OnLongClic // representation of that from the raw service object. mService = ITorService.Stub.asInterface(service); - torStatus = -1; + torStatus = TorServiceConstants.STATUS_OFF; // We want to monitor the service for as long as we are // connected to it. @@ -1259,8 +1265,12 @@ public class Orbot extends ActionBarActivity implements TorConstants, OnLongClic protected void onDestroy() { super.onDestroy(); - if (mConnection != null) + if (mConnection != null && mService != null) + { unbindService(mConnection); + mConnection = null; + mService = null; + } } public class DataCount { diff --git a/src/org/torproject/android/service/TorService.java b/src/org/torproject/android/service/TorService.java index 3ddc112f..358e717e 100644 --- a/src/org/torproject/android/service/TorService.java +++ b/src/org/torproject/android/service/TorService.java @@ -154,7 +154,6 @@ public class TorService extends Service implements TorServiceConstants, TorConst if (mLastProcessId != -1) { - sendCallbackLogMessage (getString(R.string.found_existing_tor_process)); String state = conn.getInfo("dormant"); @@ -293,29 +292,25 @@ public class TorService extends Service implements TorServiceConstants, TorConst { Intent intent = params[0]; - initBinaries(); - - - IntentFilter mNetworkStateFilter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION); - registerReceiver(mNetworkStateReceiver , mNetworkStateFilter); - - mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); - - if (intent != null && intent.getAction()!=null && intent.getAction().equals("onboot")) - { - - boolean startOnBoot = TorServiceUtils.getSharedPrefs(getApplicationContext()).getBoolean("pref_start_boot",false); - - if (startOnBoot) - { - setTorProfile(PROFILE_ON); - } - } - else - { - findExistingProc(); - - } + if (mNotificationManager == null) + { + + IntentFilter mNetworkStateFilter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION); + registerReceiver(mNetworkStateReceiver , mNetworkStateFilter); + + mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); + + if (intent != null && intent.getAction()!=null && intent.getAction().equals("onboot")) + { + + boolean startOnBoot = TorServiceUtils.getSharedPrefs(getApplicationContext()).getBoolean("pref_start_boot",false); + + if (startOnBoot) + { + setTorProfile(PROFILE_ON); + } + } + } } catch (Exception e) { @@ -507,7 +502,23 @@ public class TorService extends Service implements TorServiceConstants, TorConst } } - private void initBinaries () throws Exception + @Override + public void onCreate() { + super.onCreate(); + + try + { + initBinariesAndDirectories(); + } + catch (Exception e) + { + //what error here + Log.e(TAG, "Error installing Orbot binaries",e); + logNotice("There was an error installing Orbot binaries"); + } + } + + private void initBinariesAndDirectories () throws Exception { if (appBinHome == null) @@ -617,16 +628,6 @@ public class TorService extends Service implements TorServiceConstants, TorConst currentStatus = STATUS_CONNECTING; - try - { - initBinaries(); - } - catch (IOException e) - { - logNotice("There was a problem installing the Tor binaries: " + e.getLocalizedMessage()); - Log.d(TAG,"error installing binaries",e); - return; - } enableBinExec(fileTor); enableBinExec(filePolipo); @@ -1126,7 +1127,7 @@ public class TorService extends Service implements TorServiceConstants, TorConst public void setTorProfile(int profile) { - if (currentStatus == STATUS_OFF) + if (profile == PROFILE_ON) { sendCallbackStatusMessage (getString(R.string.status_starting_up)); @@ -1368,7 +1369,6 @@ public class TorService extends Service implements TorServiceConstants, TorConst try { - initBinaries(); findExistingProc (); } catch (Exception e) @@ -1444,7 +1444,9 @@ public class TorService extends Service implements TorServiceConstants, TorConst public void run () { try { - + + + processSettingsImpl (); @@ -1488,7 +1490,9 @@ public class TorService extends Service implements TorServiceConstants, TorConst List listCe = conn.getConf(name); Iterator itCe = listCe.iterator(); - ConfigEntry ce = null; + ConfigEntry ce = null; + + while (itCe.hasNext()) { @@ -1517,7 +1521,9 @@ public class TorService extends Service implements TorServiceConstants, TorConst * Set configuration **/ public boolean updateConfiguration (String name, String value, boolean saveToDisk) - { + { + + if (configBuffer == null) configBuffer = new ArrayList();