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

Commit 79c004b2 authored by Peng Xu's avatar Peng Xu Committed by Nick Vaccaro
Browse files

[DO NOT MERGE ANYWHERE] Fix bodysensor permission

Bug: 26481493
Bug: 26447970
Change-Id: Ibeaf5a5e39f0dc552cf115d2834a1ec28ecef5ab
parent 2e7ecddd
Loading
Loading
Loading
Loading
+11 −23
Original line number Diff line number Diff line
@@ -407,12 +407,8 @@ public abstract class SensorManager {
     * @see Sensor
     */
    public List<Sensor> getSensorList(int type) {
        // cache the returned lists the first time
        List<Sensor> list;
        final List<Sensor> fullList = getFullSensorList();
        synchronized (mSensorListByType) {
            list = mSensorListByType.get(type);
            if (list == null) {
        if (type == Sensor.TYPE_ALL) {
            list = fullList;
        } else {
@@ -423,9 +419,6 @@ public abstract class SensorManager {
            }
        }
        list = Collections.unmodifiableList(list);
                mSensorListByType.append(type, list);
            }
        }
        return list;
    }

@@ -446,7 +439,6 @@ public abstract class SensorManager {
     */
    public Sensor getDefaultSensor(int type) {
        // TODO: need to be smarter, for now, just return the 1st sensor
        List<Sensor> l = getSensorList(type);
        boolean wakeUpSensor = false;
        // For the following sensor types, return a wake-up sensor. These types are by default
        // defined as wake-up sensors. For the rest of the SDK defined sensor types return a
@@ -457,11 +449,7 @@ public abstract class SensorManager {
                type == Sensor.TYPE_WRIST_TILT_GESTURE) {
            wakeUpSensor = true;
        }

        for (Sensor sensor : l) {
            if (sensor.isWakeUpSensor() == wakeUpSensor) return sensor;
        }
        return null;
        return getDefaultSensor(type, wakeUpSensor);
    }

    /**
@@ -491,9 +479,9 @@ public abstract class SensorManager {
     * @see Sensor#isWakeUpSensor()
     */
    public Sensor getDefaultSensor(int type, boolean wakeUp) {
        List<Sensor> l = getSensorList(type);
        final List<Sensor> l = getFullSensorList();
        for (Sensor sensor : l) {
            if (sensor.isWakeUpSensor() == wakeUp)
            if (sensor.getType() == type && sensor.isWakeUpSensor() == wakeUp)
                return sensor;
        }
        return null;
+33 −2
Original line number Diff line number Diff line
@@ -52,6 +52,7 @@ public class SystemSensorManager extends SensorManager {
    private final Object mLock = new Object();

    private final ArrayList<Sensor> mFullSensorsList = new ArrayList<>();
    private List<Sensor> mSensorsListCached = null;
    private final SparseArray<Sensor> mHandleToSensor = new SparseArray<>();

    // Listener list
@@ -60,6 +61,8 @@ public class SystemSensorManager extends SensorManager {
    private final HashMap<TriggerEventListener, TriggerEventQueue> mTriggerListeners =
            new HashMap<TriggerEventListener, TriggerEventQueue>();

    private boolean mBodySensorPermission;

    // Looper associated with the context in which this instance was created.
    private final Looper mMainLooper;
    private final int mTargetSdkLevel;
@@ -74,6 +77,7 @@ public class SystemSensorManager extends SensorManager {
        mNativeInstance = nativeCreate(context.getOpPackageName());

        synchronized(mLock) {
            updatePermission();
            if (!sSensorModuleInitialized) {
                sSensorModuleInitialized = true;
                nativeClassInit();
@@ -89,11 +93,27 @@ public class SystemSensorManager extends SensorManager {
        }
    }


    /** @hide */
    @Override
    protected List<Sensor> getFullSensorList() {
        return mFullSensorsList;
        synchronized (mLock) {
            if ( updatePermission() || mSensorsListCached == null) {
                List<Sensor> list = new ArrayList();
                for (Sensor s: mFullSensorsList) {
                    switch (s.getRequiredPermission()) {
                    case Manifest.permission.BODY_SENSORS:
                        if (mBodySensorPermission) {
                            list.add(s);
                        }
                        break;
                    default:
                        list.add(s);
                    }
                }
                mSensorsListCached = list;
            }
        }
        return mSensorsListCached;
    }


@@ -274,6 +294,17 @@ public class SystemSensorManager extends SensorManager {
        }
    }

    /** Returns true if permission is changed */
    private boolean updatePermission() {
        boolean bodySensorPermission =
                (mContext.checkSelfPermission(Manifest.permission.BODY_SENSORS) ==
                        PackageManager.PERMISSION_GRANTED);

        boolean ret = bodySensorPermission != mBodySensorPermission;
        mBodySensorPermission = bodySensorPermission;
        return ret;
    }

    /*
     * BaseEventQueue is the communication channel with the sensor service,
     * SensorEventQueue, TriggerEventQueue are subclases and there is one-to-one mapping between
+5 −6
Original line number Diff line number Diff line
@@ -52,13 +52,12 @@ ASensorManager* ASensorManager_getInstanceForPackage(const char* packageName)
    }
}

int ASensorManager_getSensorList(ASensorManager* manager,
        ASensorList* list)
int ASensorManager_getSensorList(ASensorManager* manager, ASensorList* list)
{
    Sensor const* const* l;
    int c = static_cast<SensorManager*>(manager)->getSensorList(&l);
    Sensor const* const* sensorPointerList;
    int c = static_cast<SensorManager*>(manager)->getAvailableSensorList(&sensorPointerList);
    if (list) {
        *list = reinterpret_cast<ASensorList>(l);
        *list = reinterpret_cast<ASensorList>(sensorPointerList);
    }
    return c;
}
@@ -71,7 +70,7 @@ ASensor const* ASensorManager_getDefaultSensor(ASensorManager* manager, int type
ASensor const* ASensorManager_getDefaultSensorEx(ASensorManager* manager,
        int type, bool wakeUp) {
    Sensor const* const* sensorList;
    size_t size = static_cast<SensorManager*>(manager)->getSensorList(&sensorList);
    size_t size = static_cast<SensorManager*>(manager)->getAvailableSensorList(&sensorList);
    for (size_t i = 0; i < size; ++i) {
        if (ASensor_getType(sensorList[i]) == type &&
            ASensor_isWakeUpSensor(sensorList[i]) == wakeUp) {