[core] Handler when system server dies

This commit is contained in:
LoveSy 2021-02-21 20:36:15 +08:00 committed by tehcneko
parent 43e878aabe
commit 535dd32e52
4 changed files with 48 additions and 1 deletions

View File

@ -27,6 +27,9 @@ import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.util.Log;
import static io.github.lsposed.lspd.service.ServiceManager.TAG;
public class ActivityManagerService {
private static IActivityManager am = null;
@ -35,6 +38,20 @@ public class ActivityManagerService {
public static IActivityManager getActivityManager() {
if (binder == null && am == null) {
binder = ServiceManager.getService("activity");
if (binder == null) return null;
try {
binder.linkToDeath(new IBinder.DeathRecipient() {
@Override
public void binderDied() {
Log.w(TAG, "am is dead");
binder.unlinkToDeath(this, 0);
binder = null;
am = null;
}
}, 0);
} catch (RemoteException e) {
Log.e(TAG, Log.getStackTraceString(e));
}
am = IActivityManager.Stub.asInterface(binder);
}
return am;

View File

@ -97,6 +97,7 @@ public class BridgeService {
}
bridgeService.unlinkToDeath(this, 0);
listener.onSystemServerDied();
sendToBridge(serviceBinder, true);
}
}
@ -113,6 +114,8 @@ public class BridgeService {
void onSystemServerRestarted();
void onResponseFromBridgeService(boolean response);
void onSystemServerDied();
}
private static Listener listener;

View File

@ -42,6 +42,10 @@ public class ServiceManager {
}
}
private static void putBinderForSystemServer() {
android.os.ServiceManager.addService("serial", (IBinder) mainService);
}
// call by ourselves
public static void start() {
Log.i(TAG, "starting server...");
@ -56,7 +60,7 @@ public class ServiceManager {
applicationService = new LSPApplicationService();
managerService = new LSPManagerService();
android.os.ServiceManager.addService("serial", (IBinder) mainService);
putBinderForSystemServer();
waitSystemService("package");
waitSystemService("activity");
@ -77,6 +81,12 @@ public class ServiceManager {
Log.w(TAG, "no response from bridge");
}
}
@Override
public void onSystemServerDied() {
Log.w(TAG, "system server died");
putBinderForSystemServer();
}
});
try {

View File

@ -25,9 +25,12 @@ import android.os.IBinder;
import android.os.IUserManager;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.util.Log;
import java.util.List;
import static io.github.lsposed.lspd.service.ServiceManager.TAG;
public class UserService {
private static IUserManager um = null;
private static IBinder binder = null;
@ -35,6 +38,20 @@ public class UserService {
public static IUserManager getUserManager() {
if (binder == null && um == null) {
binder = ServiceManager.getService("user");
if (binder == null) return null;
try {
binder.linkToDeath(new IBinder.DeathRecipient() {
@Override
public void binderDied() {
Log.w(TAG, "um is dead");
binder.unlinkToDeath(this, 0);
binder = null;
um = null;
}
}, 0);
} catch (RemoteException e) {
Log.e(TAG, Log.getStackTraceString(e));
}
um = IUserManager.Stub.asInterface(binder);
}
return um;