fixes for status, service, binding and start/stop

This commit is contained in:
Nathan Freitas 2014-06-30 15:45:14 -04:00
parent fbc1a87c28
commit 70615d669b
2 changed files with 77 additions and 61 deletions

View File

@ -34,7 +34,6 @@ import android.content.pm.PackageManager.NameNotFoundException;
import android.content.res.Configuration; import android.content.res.Configuration;
import android.net.Uri; import android.net.Uri;
import android.os.AsyncTask; import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler; import android.os.Handler;
import android.os.IBinder; import android.os.IBinder;
@ -104,11 +103,10 @@ public class Orbot extends ActionBarActivity implements TorConstants, OnLongClic
} }
Intent torService;
private void startService () private void startService ()
{ {
torService = new Intent(this, TorService.class); Intent torService = new Intent(this, TorService.class);
startService(torService); startService(torService);
bindService(torService, bindService(torService,
@ -435,6 +433,7 @@ public class Orbot extends ActionBarActivity implements TorConstants, OnLongClic
mConnection = null; mConnection = null;
mService = null; mService = null;
} catch (RemoteException e) { } catch (RemoteException e) {
Log.w(TAG, e); Log.w(TAG, e);
} }
@ -645,7 +644,6 @@ public class Orbot extends ActionBarActivity implements TorConstants, OnLongClic
} }
torStatus = -1;
updateStatus (""); updateStatus ("");
} }
@ -844,7 +842,7 @@ public class Orbot extends ActionBarActivity implements TorConstants, OnLongClic
protected Integer doInBackground(String... params) { protected Integer doInBackground(String... params) {
mTorServiceMsg = params[0]; mTorServiceMsg = params[0];
int newTorStatus = -1; int newTorStatus = TorServiceConstants.STATUS_OFF;
try try
{ {
if (mService != null) if (mService != null)
@ -967,6 +965,9 @@ public class Orbot extends ActionBarActivity implements TorConstants, OnLongClic
mTxtOrbotLog.setText(""); mTxtOrbotLog.setText("");
if (mService != null)
{
// this is a bit of a strange/old/borrowed code/design i used to change the service state // 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" // not sure it really makes sense when what we want to say is just "startTor"
mService.setProfile(TorServiceConstants.PROFILE_ON); //this means turn on mService.setProfile(TorServiceConstants.PROFILE_ON); //this means turn on
@ -980,6 +981,11 @@ public class Orbot extends ActionBarActivity implements TorConstants, OnLongClic
Message msg = mHandler.obtainMessage(TorServiceConstants.ENABLE_TOR_MSG); Message msg = mHandler.obtainMessage(TorServiceConstants.ENABLE_TOR_MSG);
msg.getData().putString(HANDLER_TOR_MSG, getString(R.string.status_starting_up)); msg.getData().putString(HANDLER_TOR_MSG, getString(R.string.status_starting_up));
mHandler.sendMessage(msg); 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. // representation of that from the raw service object.
mService = ITorService.Stub.asInterface(service); mService = ITorService.Stub.asInterface(service);
torStatus = -1; torStatus = TorServiceConstants.STATUS_OFF;
// We want to monitor the service for as long as we are // We want to monitor the service for as long as we are
// connected to it. // connected to it.
@ -1259,8 +1265,12 @@ public class Orbot extends ActionBarActivity implements TorConstants, OnLongClic
protected void onDestroy() { protected void onDestroy() {
super.onDestroy(); super.onDestroy();
if (mConnection != null) if (mConnection != null && mService != null)
{
unbindService(mConnection); unbindService(mConnection);
mConnection = null;
mService = null;
}
} }
public class DataCount { public class DataCount {

View File

@ -154,7 +154,6 @@ public class TorService extends Service implements TorServiceConstants, TorConst
if (mLastProcessId != -1) if (mLastProcessId != -1)
{ {
sendCallbackLogMessage (getString(R.string.found_existing_tor_process)); sendCallbackLogMessage (getString(R.string.found_existing_tor_process));
String state = conn.getInfo("dormant"); String state = conn.getInfo("dormant");
@ -293,8 +292,8 @@ public class TorService extends Service implements TorServiceConstants, TorConst
{ {
Intent intent = params[0]; Intent intent = params[0];
initBinaries(); if (mNotificationManager == null)
{
IntentFilter mNetworkStateFilter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION); IntentFilter mNetworkStateFilter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
registerReceiver(mNetworkStateReceiver , mNetworkStateFilter); registerReceiver(mNetworkStateReceiver , mNetworkStateFilter);
@ -311,10 +310,6 @@ public class TorService extends Service implements TorServiceConstants, TorConst
setTorProfile(PROFILE_ON); setTorProfile(PROFILE_ON);
} }
} }
else
{
findExistingProc();
} }
} }
catch (Exception e) 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) if (appBinHome == null)
@ -617,16 +628,6 @@ public class TorService extends Service implements TorServiceConstants, TorConst
currentStatus = STATUS_CONNECTING; 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(fileTor);
enableBinExec(filePolipo); enableBinExec(filePolipo);
@ -1126,7 +1127,7 @@ public class TorService extends Service implements TorServiceConstants, TorConst
public void setTorProfile(int profile) { public void setTorProfile(int profile) {
if (currentStatus == STATUS_OFF) if (profile == PROFILE_ON)
{ {
sendCallbackStatusMessage (getString(R.string.status_starting_up)); sendCallbackStatusMessage (getString(R.string.status_starting_up));
@ -1368,7 +1369,6 @@ public class TorService extends Service implements TorServiceConstants, TorConst
try try
{ {
initBinaries();
findExistingProc (); findExistingProc ();
} }
catch (Exception e) catch (Exception e)
@ -1445,6 +1445,8 @@ public class TorService extends Service implements TorServiceConstants, TorConst
{ {
try { try {
processSettingsImpl (); processSettingsImpl ();
@ -1490,6 +1492,8 @@ public class TorService extends Service implements TorServiceConstants, TorConst
Iterator<ConfigEntry> itCe = listCe.iterator(); Iterator<ConfigEntry> itCe = listCe.iterator();
ConfigEntry ce = null; ConfigEntry ce = null;
while (itCe.hasNext()) while (itCe.hasNext())
{ {
ce = itCe.next(); ce = itCe.next();
@ -1518,6 +1522,8 @@ public class TorService extends Service implements TorServiceConstants, TorConst
**/ **/
public boolean updateConfiguration (String name, String value, boolean saveToDisk) public boolean updateConfiguration (String name, String value, boolean saveToDisk)
{ {
if (configBuffer == null) if (configBuffer == null)
configBuffer = new ArrayList<String>(); configBuffer = new ArrayList<String>();