Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 1b3486ad authored by Vladimir Komsiyski's avatar Vladimir Komsiyski
Browse files

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
parent 91824e80
Loading
Loading
Loading
Loading
+14 −6
Original line number Diff line number Diff line
@@ -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<Boolean> 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<Sensor> 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<Sensor> 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.