cleanup of notification and process kill code

This commit is contained in:
Nathan Freitas 2014-05-05 00:00:50 -04:00
parent 76ec147c7a
commit ba90f73828
1 changed files with 51 additions and 65 deletions

View File

@ -208,7 +208,7 @@ public class TorService extends Service implements TorServiceConstants, TorConst
} }
private void showToolbarNotification (String notifyMsg, int notifyId, int icon, int flags, boolean isOngoing) private void showToolbarNotification (String notifyMsg, int notifyId, int icon, boolean isOngoing)
{ {
if (mNotifyBuilder == null) if (mNotifyBuilder == null)
@ -239,20 +239,21 @@ public class TorService extends Service implements TorServiceConstants, TorConst
{ {
mNotifyBuilder.setTicker(notifyMsg); mNotifyBuilder.setTicker(notifyMsg);
mNotifyBuilder.setOngoing(false); mNotifyBuilder.setOngoing(false);
mNotifyBuilder.setLights(Color.GREEN, 1000, 1000); mNotifyBuilder.setLights(Color.RED, 1000, 1000);
mNotifyBuilder.setSmallIcon(R.drawable.ic_stat_notifyerr);
} }
if (isOngoing) if (isOngoing)
{ {
startForeground(notifyId, startForeground(notifyId,
mNotifyBuilder.getNotification()); mNotifyBuilder.build());
} }
else else
{ {
mNotificationManager.notify( mNotificationManager.notify(
notifyId, notifyId,
mNotifyBuilder.getNotification()); mNotifyBuilder.build());
} }
} }
@ -328,6 +329,10 @@ public class TorService extends Service implements TorServiceConstants, TorConst
{ {
super.onDestroy(); super.onDestroy();
if (currentStatus == STATUS_ON)
{
this.showToolbarNotification("Tor service stopped unexpectedly", ERROR_NOTIFY_ID, R.drawable.ic_stat_notifyerr, false);
}
//Log.d(TAG,"onDestroy called"); //Log.d(TAG,"onDestroy called");
// Unregister all callbacks. // Unregister all callbacks.
@ -382,7 +387,7 @@ public class TorService extends Service implements TorServiceConstants, TorConst
{ {
try { try {
String onionHostname = Utils.readString(new FileInputStream(file)).trim(); String onionHostname = Utils.readString(new FileInputStream(file)).trim();
showToolbarNotification(getString(R.string.hidden_service_on) + ' ' + onionHostname, HS_NOTIFY_ID, R.drawable.ic_stat_tor, Notification.FLAG_ONGOING_EVENT, true); showToolbarNotification(getString(R.string.hidden_service_on) + ' ' + onionHostname, HS_NOTIFY_ID, R.drawable.ic_stat_tor, true);
Editor pEdit = prefs.edit(); Editor pEdit = prefs.edit();
pEdit.putString("pref_hs_hostname",onionHostname); pEdit.putString("pref_hs_hostname",onionHostname);
pEdit.commit(); pEdit.commit();
@ -391,13 +396,13 @@ public class TorService extends Service implements TorServiceConstants, TorConst
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
logException("unable to read onion hostname file",e); logException("unable to read onion hostname file",e);
showToolbarNotification(getString(R.string.unable_to_read_hidden_service_name), ERROR_NOTIFY_ID, R.drawable.ic_stat_notifyerr, -1, false); showToolbarNotification(getString(R.string.unable_to_read_hidden_service_name), ERROR_NOTIFY_ID, R.drawable.ic_stat_notifyerr, false);
return null; return null;
} }
} }
else else
{ {
showToolbarNotification(getString(R.string.unable_to_read_hidden_service_name), HS_NOTIFY_ID, R.drawable.ic_stat_notifyerr, -1, false); showToolbarNotification(getString(R.string.unable_to_read_hidden_service_name), HS_NOTIFY_ID, R.drawable.ic_stat_notifyerr, false);
} }
@ -409,7 +414,6 @@ public class TorService extends Service implements TorServiceConstants, TorConst
private void killTorProcess () throws Exception private void killTorProcess () throws Exception
{ {
int procId = -1;
if (conn != null) if (conn != null)
{ {
@ -429,38 +433,17 @@ public class TorService extends Service implements TorServiceConstants, TorConst
} }
killProcess(filePolipo);
killProcess(fileObfsclient);
}
private void killProcess (File fileProcBin) throws IOException
{
int procId = -1;
Shell shell = Shell.startShell(); Shell shell = Shell.startShell();
/* while ((procId = TorServiceUtils.findProcessId(fileProcBin.getAbsolutePath())) != -1)
*
int maxTry = 5;
int currTry = 0;
while ((procId = TorServiceUtils.findProcessId(fileTor.getAbsolutePath())) != -1 && currTry++ < maxTry)
{
sendCallbackStatusMessage ("Found existing orphan Tor process; Trying to shutdown now (device restart may be needed)...");
logNotice("Found Tor PID=" + procId + " - attempt to shutdown now...");
SimpleCommand killCommand = new SimpleCommand("toolbox kill -9 " + procId);
shell.add(killCommand);
killCommand = new SimpleCommand("kill -9 " + procId);
shell.add(killCommand);
}*/
while ((procId = TorServiceUtils.findProcessId(filePolipo.getAbsolutePath())) != -1)
{
logNotice("Found Polipo PID=" + procId + " - killing now...");
SimpleCommand killCommand = new SimpleCommand("toolbox kill " + procId);
shell.add(killCommand);
killCommand = new SimpleCommand("kill " + procId);
shell.add(killCommand);
}
while ((procId = TorServiceUtils.findProcessId(fileObfsclient.getAbsolutePath())) != -1)
{ {
logNotice("Found fileObfsclient PID=" + procId + " - killing now..."); logNotice("Found fileObfsclient PID=" + procId + " - killing now...");
@ -471,7 +454,7 @@ public class TorService extends Service implements TorServiceConstants, TorConst
shell.add(killCommand); shell.add(killCommand);
} }
shell.close(); shell.close();
} }
private void logNotice (String msg) private void logNotice (String msg)
@ -635,13 +618,13 @@ public class TorService extends Service implements TorServiceConstants, TorConst
if(proxyAll) if(proxyAll)
{ {
showToolbarNotification(getString(R.string.setting_up_full_transparent_proxying_), TRANSPROXY_NOTIFY_ID, R.drawable.ic_stat_tor, -1, false); showToolbarNotification(getString(R.string.setting_up_full_transparent_proxying_), TRANSPROXY_NOTIFY_ID, R.drawable.ic_stat_tor, false);
code = mTransProxy.setTransparentProxyingAll(this); code = mTransProxy.setTransparentProxyingAll(this);
} }
else else
{ {
showToolbarNotification(getString(R.string.setting_up_app_based_transparent_proxying_), TRANSPROXY_NOTIFY_ID, R.drawable.ic_stat_tor, -1, false); showToolbarNotification(getString(R.string.setting_up_app_based_transparent_proxying_), TRANSPROXY_NOTIFY_ID, R.drawable.ic_stat_tor, false);
code = mTransProxy.setTransparentProxyingByApp(this,AppManager.getApps(this, TorServiceUtils.getSharedPrefs(getApplicationContext()))); code = mTransProxy.setTransparentProxyingByApp(this,AppManager.getApps(this, TorServiceUtils.getSharedPrefs(getApplicationContext())));
} }
@ -651,11 +634,11 @@ public class TorService extends Service implements TorServiceConstants, TorConst
if (code == 0) if (code == 0)
{ {
showToolbarNotification(getString(R.string.transparent_proxying_enabled), TRANSPROXY_NOTIFY_ID, R.drawable.ic_stat_tor, -1, false); showToolbarNotification(getString(R.string.transparent_proxying_enabled), TRANSPROXY_NOTIFY_ID, R.drawable.ic_stat_tor, false);
if (enableTether) if (enableTether)
{ {
showToolbarNotification(getString(R.string.transproxy_enabled_for_tethering_), TRANSPROXY_NOTIFY_ID, R.drawable.ic_stat_tor, -1, false); showToolbarNotification(getString(R.string.transproxy_enabled_for_tethering_), TRANSPROXY_NOTIFY_ID, R.drawable.ic_stat_tor, false);
mTransProxy.enableTetheringRules(this); mTransProxy.enableTetheringRules(this);
@ -663,7 +646,7 @@ public class TorService extends Service implements TorServiceConstants, TorConst
} }
else else
{ {
showToolbarNotification(getString(R.string.warning_error_starting_transparent_proxying_), TRANSPROXY_NOTIFY_ID, R.drawable.ic_stat_tor, -1, false); showToolbarNotification(getString(R.string.warning_error_starting_transparent_proxying_), TRANSPROXY_NOTIFY_ID, R.drawable.ic_stat_tor, false);
} }
@ -990,7 +973,7 @@ public class TorService extends Service implements TorServiceConstants, TorConst
logException("Unable to start Tor: " + e.toString(),e); logException("Unable to start Tor: " + e.toString(),e);
currentStatus = STATUS_OFF; currentStatus = STATUS_OFF;
showToolbarNotification(getString(R.string.unable_to_start_tor) + ": " + e.getMessage(), ERROR_NOTIFY_ID, R.drawable.ic_stat_notifyerr, -1, false); showToolbarNotification(getString(R.string.unable_to_start_tor) + ": " + e.getMessage(), ERROR_NOTIFY_ID, R.drawable.ic_stat_notifyerr, false);
} }
} }
else else
@ -1033,7 +1016,7 @@ public class TorService extends Service implements TorServiceConstants, TorConst
{ {
currentStatus = STATUS_ON; currentStatus = STATUS_ON;
showToolbarNotification(getString(R.string.status_activated), NOTIFY_ID, R.drawable.ic_stat_tor, -1, prefPersistNotifications); showToolbarNotification(getString(R.string.status_activated), NOTIFY_ID, R.drawable.ic_stat_tor, prefPersistNotifications);
} }
@ -1106,7 +1089,7 @@ public class TorService extends Service implements TorServiceConstants, TorConst
iconId = R.drawable.ic_stat_tor_xfer; iconId = R.drawable.ic_stat_tor_xfer;
if (mConnectivity && prefPersistNotifications) if (mConnectivity && prefPersistNotifications)
showToolbarNotification(sb.toString(), NOTIFY_ID, iconId, -1, prefPersistNotifications); showToolbarNotification(sb.toString(), NOTIFY_ID, iconId, prefPersistNotifications);
mTotalTrafficWritten += written; mTotalTrafficWritten += written;
mTotalTrafficRead += read; mTotalTrafficRead += read;
@ -1601,18 +1584,21 @@ public class TorService extends Service implements TorServiceConstants, TorConst
mBinder.updateConfiguration("DisableNetwork", mConnectivity ? "0" : "1", false); mBinder.updateConfiguration("DisableNetwork", mConnectivity ? "0" : "1", false);
mBinder.saveConfiguration(); mBinder.saveConfiguration();
if (!mConnectivity) if (currentStatus == STATUS_ON)
{ {
logNotice("No network connectivity. Putting Tor to sleep..."); if (!mConnectivity)
showToolbarNotification(getString(R.string.no_internet_connection_tor),NOTIFY_ID,R.drawable.ic_stat_tor_off,-1,prefPersistNotifications); {
logNotice(context.getString(R.string.no_network_connectivity_putting_tor_to_sleep_));
showToolbarNotification(getString(R.string.no_internet_connection_tor),NOTIFY_ID,R.drawable.ic_stat_tor_off,prefPersistNotifications);
}
else
{
logNotice(context.getString(R.string.network_connectivity_is_good_waking_tor_up_));
showToolbarNotification(getString(R.string.status_activated),NOTIFY_ID,R.drawable.ic_stat_tor,prefPersistNotifications);
}
} }
else
{
logNotice("Network connectivity is good. Waking Tor up...");
showToolbarNotification(getString(R.string.status_activated),NOTIFY_ID,R.drawable.ic_stat_tor,-1,prefPersistNotifications);
}
} catch (Exception e) { } catch (Exception e) {
logException ("error updating state after network restart",e); logException ("error updating state after network restart",e);
@ -1624,7 +1610,7 @@ public class TorService extends Service implements TorServiceConstants, TorConst
private boolean processSettingsImpl () throws RemoteException, IOException private boolean processSettingsImpl () throws RemoteException, IOException
{ {
logNotice("updating settings in Tor service"); logNotice(getString(R.string.updating_settings_in_tor_service));
SharedPreferences prefs = TorServiceUtils.getSharedPrefs(getApplicationContext()); SharedPreferences prefs = TorServiceUtils.getSharedPrefs(getApplicationContext());
@ -1695,7 +1681,7 @@ public class TorService extends Service implements TorServiceConstants, TorConst
} }
catch (Exception e) catch (Exception e)
{ {
showToolbarNotification (getString(R.string.error_installing_binares),ERROR_NOTIFY_ID,R.drawable.ic_stat_notifyerr, Notification.FLAG_ONGOING_EVENT, false); showToolbarNotification (getString(R.string.error_installing_binares),ERROR_NOTIFY_ID,R.drawable.ic_stat_notifyerr, false);
return false; return false;
} }
@ -1718,7 +1704,7 @@ public class TorService extends Service implements TorServiceConstants, TorConst
{ {
String msgBridge = getString(R.string.bridge_requires_ip) + String msgBridge = getString(R.string.bridge_requires_ip) +
getString(R.string.send_email_for_bridges); getString(R.string.send_email_for_bridges);
showToolbarNotification(msgBridge, ERROR_NOTIFY_ID, R.drawable.ic_stat_tor, -1, false); showToolbarNotification(msgBridge, ERROR_NOTIFY_ID, R.drawable.ic_stat_tor, false);
logMessage(msgBridge); logMessage(msgBridge);
return false; return false;
@ -1732,7 +1718,7 @@ public class TorService extends Service implements TorServiceConstants, TorConst
bridgeDelim = ","; bridgeDelim = ",";
} }
showToolbarNotification(getString(R.string.notification_using_bridges) + ": " + bridgeList, TRANSPROXY_NOTIFY_ID, R.drawable.ic_stat_tor, -1, false); showToolbarNotification(getString(R.string.notification_using_bridges) + ": " + bridgeList, TRANSPROXY_NOTIFY_ID, R.drawable.ic_stat_tor, false);
StringTokenizer st = new StringTokenizer(bridgeList,bridgeDelim); StringTokenizer st = new StringTokenizer(bridgeList,bridgeDelim);
while (st.hasMoreTokens()) while (st.hasMoreTokens())
@ -1791,7 +1777,7 @@ public class TorService extends Service implements TorServiceConstants, TorConst
} }
catch (Exception e) catch (Exception e)
{ {
showToolbarNotification (getString(R.string.your_reachableaddresses_settings_caused_an_exception_),ERROR_NOTIFY_ID,R.drawable.ic_stat_notifyerr, Notification.FLAG_ONGOING_EVENT, false); showToolbarNotification (getString(R.string.your_reachableaddresses_settings_caused_an_exception_),ERROR_NOTIFY_ID,R.drawable.ic_stat_notifyerr, false);
return false; return false;
} }
@ -1820,7 +1806,7 @@ public class TorService extends Service implements TorServiceConstants, TorConst
} }
catch (Exception e) catch (Exception e)
{ {
showToolbarNotification (getString(R.string.your_relay_settings_caused_an_exception_),ERROR_NOTIFY_ID,R.drawable.ic_stat_notifyerr, Notification.FLAG_ONGOING_EVENT, false); showToolbarNotification (getString(R.string.your_relay_settings_caused_an_exception_),ERROR_NOTIFY_ID,R.drawable.ic_stat_notifyerr, false);
return false; return false;