From 1b3486ad445e4085a64b557a743cfe2d4fd9db3f Mon Sep 17 00:00:00 2001 From: Vladimir Komsiyski Date: Thu, 8 Dec 2022 17:21:37 +0100 Subject: [PATCH] Create VDM on demand in SystemSensorManager. It is not necessary until there's a non-default device association with the context and this reduces the system memory requirements. Test: atest VirtualSensorTest Bug: 260943917 Change-Id: I15ae520a88bfce6e23e7c3400962233dd223fe90 --- .../android/hardware/SystemSensorManager.java | 20 +++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/core/java/android/hardware/SystemSensorManager.java b/core/java/android/hardware/SystemSensorManager.java index 18118f5bfe29a..161b1b71121ee 100644 --- a/core/java/android/hardware/SystemSensorManager.java +++ b/core/java/android/hardware/SystemSensorManager.java @@ -135,7 +135,7 @@ public class SystemSensorManager extends SensorManager { private final boolean mIsPackageDebuggable; private final Context mContext; private final long mNativeInstance; - private final VirtualDeviceManager mVdm; + private VirtualDeviceManager mVdm; private Optional mHasHighSamplingRateSensorsPermission = Optional.empty(); @@ -154,7 +154,6 @@ public class SystemSensorManager extends SensorManager { mContext = context; mNativeInstance = nativeCreate(context.getOpPackageName()); mIsPackageDebuggable = (0 != (appInfo.flags & ApplicationInfo.FLAG_DEBUGGABLE)); - mVdm = mContext.getSystemService(VirtualDeviceManager.class); // initialize the sensor list for (int index = 0;; ++index) { @@ -170,8 +169,7 @@ public class SystemSensorManager extends SensorManager { @Override public List getSensorList(int type) { final int deviceId = mContext.getDeviceId(); - if (deviceId == DEFAULT_DEVICE_ID || mVdm == null - || mVdm.getDevicePolicy(deviceId, POLICY_TYPE_SENSORS) == DEVICE_POLICY_DEFAULT) { + if (isDeviceSensorPolicyDefault(deviceId)) { return super.getSensorList(type); } @@ -207,8 +205,7 @@ public class SystemSensorManager extends SensorManager { @Override protected List getFullSensorList() { final int deviceId = mContext.getDeviceId(); - if (deviceId == DEFAULT_DEVICE_ID || mVdm == null - || mVdm.getDevicePolicy(deviceId, POLICY_TYPE_SENSORS) == DEVICE_POLICY_DEFAULT) { + if (isDeviceSensorPolicyDefault(deviceId)) { return mFullSensorsList; } @@ -1136,6 +1133,17 @@ public class SystemSensorManager extends SensorManager { parameter.type, parameter.floatValues, parameter.intValues) == 0; } + private boolean isDeviceSensorPolicyDefault(int deviceId) { + if (deviceId == DEFAULT_DEVICE_ID) { + return true; + } + if (mVdm == null) { + mVdm = mContext.getSystemService(VirtualDeviceManager.class); + } + return mVdm == null + || mVdm.getDevicePolicy(deviceId, POLICY_TYPE_SENSORS) == DEVICE_POLICY_DEFAULT; + } + /** * Checks if a sensor should be capped according to HIGH_SAMPLING_RATE_SENSORS * permission.