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)
{
startForeground(notifyId,
mNotifyBuilder.getNotification());
}
else
{
mNotificationManager.notify( mNotificationManager.notify(
notifyId, notifyId,
mNotifyBuilder.getNotification()); mNotifyBuilder.getNotification());
}
} }
@ -272,8 +278,10 @@ 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); {
initBinaries();
IntentFilter mNetworkStateFilter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION); IntentFilter mNetworkStateFilter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
registerReceiver(mNetworkStateReceiver , mNetworkStateFilter); registerReceiver(mNetworkStateReceiver , mNetworkStateFilter);
@ -292,23 +300,23 @@ public class TorService extends Service implements TorServiceConstants, TorConst
} }
else if (intent == null) else if (intent == null)
{ {
try {
initBinaries();
findExistingProc () ; findExistingProc () ;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} }
}
// We want this service to continue running until it is explicitly // We want this service to continue running until it is explicitly
// stopped, so return sticky. // stopped, so return sticky.
return START_STICKY; return START_STICKY;
}
catch (Exception e)
{
logException ("Error starting service",e);
return Service.START_REDELIVER_INTENT;
}
} }
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)