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.net.Uri;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
@ -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)
@ -967,19 +965,27 @@ public class Orbot extends ActionBarActivity implements TorConstants, OnLongClic
mTxtOrbotLog.setText("");
// 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
if (mService != null)
{
//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));
// 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
//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);
//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 {

View File

@ -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();
if (mNotificationManager == null)
{
IntentFilter mNetworkStateFilter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
registerReceiver(mNetworkStateReceiver , mNetworkStateFilter);
IntentFilter mNetworkStateFilter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
registerReceiver(mNetworkStateReceiver , mNetworkStateFilter);
mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (intent != null && intent.getAction()!=null && intent.getAction().equals("onboot"))
{
if (intent != null && intent.getAction()!=null && intent.getAction().equals("onboot"))
{
boolean startOnBoot = TorServiceUtils.getSharedPrefs(getApplicationContext()).getBoolean("pref_start_boot",false);
boolean startOnBoot = TorServiceUtils.getSharedPrefs(getApplicationContext()).getBoolean("pref_start_boot",false);
if (startOnBoot)
{
setTorProfile(PROFILE_ON);
}
}
else
{
findExistingProc();
}
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)
@ -1445,6 +1445,8 @@ public class TorService extends Service implements TorServiceConstants, TorConst
{
try {
processSettingsImpl ();
@ -1490,6 +1492,8 @@ public class TorService extends Service implements TorServiceConstants, TorConst
Iterator<ConfigEntry> itCe = listCe.iterator();
ConfigEntry ce = null;
while (itCe.hasNext())
{
ce = itCe.next();
@ -1518,6 +1522,8 @@ public class TorService extends Service implements TorServiceConstants, TorConst
**/
public boolean updateConfiguration (String name, String value, boolean saveToDisk)
{
if (configBuffer == null)
configBuffer = new ArrayList<String>();