KillerService: ensure all the services are killed

This commit is contained in:
Peter Cai 2018-08-24 19:35:38 +08:00
parent 9d545a7196
commit 7ddb4a8dd2
No known key found for this signature in database
GPG key ID: 71F5FB4E4F3FD54F
4 changed files with 41 additions and 0 deletions

View file

@ -9,6 +9,7 @@ import net.typeblog.shelter.services.ILoadIconCallback;
import net.typeblog.shelter.util.ApplicationInfoWrapper;
interface IShelterService {
void ping();
void stopShelterService(boolean kill);
void getApps(IGetAppsCallback callback);
void loadIcon(in ApplicationInfoWrapper info, ILoadIconCallback callback);

View file

@ -40,6 +40,12 @@ public class KillerService extends Service {
}
}
@Override
public void onDestroy() {
super.onDestroy();
killEverything();
}
@Override
public void onTaskRemoved(Intent rootIntent) {
super.onTaskRemoved(rootIntent);

View file

@ -35,6 +35,11 @@ public class ShelterService extends Service {
private PackageManager mPackageManager = null;
private ComponentName mAdminComponent = null;
private IShelterService.Stub mBinder = new IShelterService.Stub() {
@Override
public void ping() {
// Do nothing, just let the other side know we are alive
}
@Override
public void stopShelterService(boolean kill) {
// dirty: just wait for some time and kill this service itself

View file

@ -189,6 +189,35 @@ public class MainActivity extends AppCompatActivity {
return isRemote ? mServiceMain : mServiceWork;
}
boolean servicesAlive() {
try {
mServiceMain.ping();
} catch (Exception e) {
return false;
}
try {
mServiceWork.ping();
} catch (Exception e) {
return false;
}
return true;
}
@Override
protected void onResume() {
super.onResume();
if (mServiceMain != null && mServiceWork != null && !servicesAlive()) {
// Restart the activity if the services are no longer alive
// This might be caused by KillerService being destroyed and
// bringing all the other services with it
Intent intent = getIntent();
finish();
startActivity(intent);
}
}
@Override
protected void onDestroy() {
super.onDestroy();