Cache managers in ServiceManager

To guarantee that we instantiate services only one, cache the value the
first time.
This commit is contained in:
Romain Vimont 2018-02-02 16:25:37 +01:00
parent 69a359c7f4
commit 06333e1e08

View file

@ -8,6 +8,11 @@ import java.lang.reflect.Method;
public class ServiceManager { public class ServiceManager {
private final Method getServiceMethod; private final Method getServiceMethod;
private WindowManager windowManager;
private DisplayManager displayManager;
private InputManager inputManager;
private PowerManager powerManager;
public ServiceManager() { public ServiceManager() {
try { try {
getServiceMethod = Class.forName("android.os.ServiceManager").getDeclaredMethod("getService", String.class); getServiceMethod = Class.forName("android.os.ServiceManager").getDeclaredMethod("getService", String.class);
@ -27,18 +32,30 @@ public class ServiceManager {
} }
public WindowManager getWindowManager() { public WindowManager getWindowManager() {
return new WindowManager(getService("window", "android.view.IWindowManager")); if (windowManager == null) {
windowManager = new WindowManager(getService("window", "android.view.IWindowManager"));
}
return windowManager;
} }
public DisplayManager getDisplayManager() { public DisplayManager getDisplayManager() {
return new DisplayManager(getService("display", "android.hardware.display.IDisplayManager")); if (displayManager == null) {
displayManager = new DisplayManager(getService("display", "android.hardware.display.IDisplayManager"));
}
return displayManager;
} }
public InputManager getInputManager() { public InputManager getInputManager() {
return new InputManager(getService("input", "android.hardware.input.IInputManager")); if (inputManager == null) {
inputManager = new InputManager(getService("input", "android.hardware.input.IInputManager"));
}
return inputManager;
} }
public PowerManager getPowerManager() { public PowerManager getPowerManager() {
return new PowerManager(getService("power", "android.os.IPowerManager")); if (powerManager == null) {
powerManager = new PowerManager(getService("power", "android.os.IPowerManager"));
}
return powerManager;
} }
} }