Do not connect to car service from car service
- All car service components should not bind to itself and create unnecessary
binding - connection flow.
- To use Car*Manager API for convenience, get necessary binder interface and
directly create the Manager instance.
- For CarPowerManager, add CarLocalServices.createCarPowerManager().
Bug: 130362435
Test: build and run all car service tests
Change-Id: I759bdc37571ec8a8f9b6ed1cee2dac2fde981087
diff --git a/service/src/com/android/car/CarLocalServices.java b/service/src/com/android/car/CarLocalServices.java
index fe3dc07..6c756b8 100644
--- a/service/src/com/android/car/CarLocalServices.java
+++ b/service/src/com/android/car/CarLocalServices.java
@@ -16,6 +16,9 @@
package com.android.car;
+import android.annotation.Nullable;
+import android.car.hardware.power.CarPowerManager;
+import android.content.Context;
import android.util.ArrayMap;
import android.util.Log;
@@ -78,4 +81,19 @@
sLocalServiceObjects.clear();
}
}
+
+ /**
+ * Create CarPowerManager from registered CarPowerManagementService.
+ * @param context
+ * @return Newly created CarPowerManager. It will return null if CarPowerManagementService is
+ * not registered, which can only happen in test setup.
+ */
+ @Nullable
+ public static CarPowerManager createCarPowerManager(Context context) {
+ CarPowerManagementService service = getService(CarPowerManagementService.class);
+ if (service == null) {
+ return null;
+ }
+ return new CarPowerManager(service, context, null);
+ }
}