ensure Service has foreground priority so it is not killed

(and improve onBind() calls to reset state if it is killed)
This commit is contained in:
Nathan Freitas 2014-04-15 22:31:24 -04:00
parent 3824bc56e5
commit b9eeb3711b
1 changed files with 52 additions and 36 deletions

View File

@ -183,7 +183,7 @@ public class TorService extends Service implements TorServiceConstants, TorConst
@Override @Override
public boolean onUnbind(Intent intent) { public boolean onUnbind(Intent intent) {
// logNotice( "onUnbind Called: " + intent.getAction()); logNotice( "onUnbind Called: " + intent.getAction());
return super.onUnbind(intent); return super.onUnbind(intent);
@ -230,7 +230,6 @@ public class TorService extends Service implements TorServiceConstants, TorConst
mNotifyBuilder.setContentText(notifyMsg); mNotifyBuilder.setContentText(notifyMsg);
mNotifyBuilder.setSmallIcon(icon); mNotifyBuilder.setSmallIcon(icon);
mNotifyBuilder.setOngoing(isOngoing);
if (notifyId == ERROR_NOTIFY_ID) if (notifyId == ERROR_NOTIFY_ID)
{ {
@ -239,11 +238,18 @@ public class TorService extends Service implements TorServiceConstants, TorConst
mNotifyBuilder.setLights(Color.GREEN, 1000, 1000); mNotifyBuilder.setLights(Color.GREEN, 1000, 1000);
} }
if (isOngoing)
mNotificationManager.notify( {
notifyId, startForeground(notifyId,
mNotifyBuilder.getNotification()); mNotifyBuilder.getNotification());
}
else
{
mNotificationManager.notify(
notifyId,
mNotifyBuilder.getNotification());
}
} }
@ -272,43 +278,45 @@ public class TorService extends Service implements TorServiceConstants, TorConst
public int onStartCommand(Intent intent, int flags, int startId) { public int onStartCommand(Intent intent, int flags, int startId) {
appBinHome = getDir(DIRECTORY_TOR_BINARY,Application.MODE_PRIVATE); try
appCacheHome = getDir(DIRECTORY_TOR_DATA,Application.MODE_PRIVATE);
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"))
{ {
initBinaries();
boolean startOnBoot = getSharedPrefs(getApplicationContext()).getBoolean("pref_start_boot",false);
if (startOnBoot) 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"))
{ {
setTorProfile(PROFILE_ON);
boolean startOnBoot = getSharedPrefs(getApplicationContext()).getBoolean("pref_start_boot",false);
if (startOnBoot)
{
setTorProfile(PROFILE_ON);
}
} }
else if (intent == null)
{
findExistingProc () ;
}
// We want this service to continue running until it is explicitly
// stopped, so return sticky.
return START_STICKY;
} }
else if (intent == null) catch (Exception e)
{ {
try { logException ("Error starting service",e);
initBinaries(); return Service.START_REDELIVER_INTENT;
findExistingProc () ;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} }
// We want this service to continue running until it is explicitly
// stopped, so return sticky.
return START_STICKY;
} }
public static SharedPreferences getSharedPrefs (Context context) public static SharedPreferences getSharedPrefs (Context context)
@ -517,6 +525,13 @@ public class TorService extends Service implements TorServiceConstants, TorConst
private void initBinaries () throws Exception private void initBinaries () throws Exception
{ {
if (appBinHome == null)
appBinHome = getDir(DIRECTORY_TOR_BINARY,Application.MODE_PRIVATE);
if (appCacheHome == null)
appCacheHome = getDir(DIRECTORY_TOR_DATA,Application.MODE_PRIVATE);
fileTor= new File(appBinHome, TOR_ASSET_KEY); fileTor= new File(appBinHome, TOR_ASSET_KEY);
filePrivoxy = new File(appBinHome, PRIVOXY_ASSET_KEY); filePrivoxy = new File(appBinHome, PRIVOXY_ASSET_KEY);
@ -1211,6 +1226,7 @@ public class TorService extends Service implements TorServiceConstants, TorConst
{ {
try try
{ {
initBinaries();
findExistingProc (); findExistingProc ();
} }
catch (Exception e) catch (Exception e)