MotoActions: Use stow sensor for both hand wave and pick up gestures
authordianlujitao <dianlujitao@lineageos.org>
Wed, 2 Feb 2022 10:07:22 +0000 (18:07 +0800)
committerNolen Johnson <johnsonnolen@gmail.com>
Sun, 13 Feb 2022 04:28:07 +0000 (23:28 -0500)
 * The behaviors of stow and proximity sensors are pretty similar, so
   there's no need to use both at the same time. Simply use the stow
   sensor to handle both hand wave and pick up gestures.
 * Switch to timestamp associated with the sensor event to determine the
   operation while at it.

Change-Id: I39767cf7b2e48bc2b0baabfac1e47240012f71d6

MotoActions/src/org/lineageos/settings/device/MotoActionsService.java
MotoActions/src/org/lineageos/settings/device/doze/ProximitySensor.java [deleted file]
MotoActions/src/org/lineageos/settings/device/doze/StowSensor.java

index 3f8de813d016e4fdbe75b4826d4d38c2fc64de20..e6e3ec4800a015f783e7cd380ed0ac842e03c448 100644 (file)
@@ -35,7 +35,6 @@ import org.lineageos.settings.device.actions.LiftToSilence;
 import org.lineageos.settings.device.actions.ProximitySilencer;
 
 import org.lineageos.settings.device.doze.DozePulseAction;
-import org.lineageos.settings.device.doze.ProximitySensor;
 import org.lineageos.settings.device.doze.FlatUpSensor;
 import org.lineageos.settings.device.doze.ScreenReceiver;
 import org.lineageos.settings.device.doze.ScreenStateNotifier;
@@ -71,7 +70,6 @@ public class MotoActionsService extends IntentService implements ScreenStateNoti
         mScreenStateNotifiers.add(mDozePulseAction);
 
         // Actionable sensors get screen on/off notifications
-        mScreenStateNotifiers.add(new ProximitySensor(MotoActionsSettings, mSensorHelper, mDozePulseAction));
         mScreenStateNotifiers.add(new StowSensor(MotoActionsSettings, mSensorHelper, mDozePulseAction));
         mScreenStateNotifiers.add(new FlatUpSensor(MotoActionsSettings, mSensorHelper, mDozePulseAction));
 
diff --git a/MotoActions/src/org/lineageos/settings/device/doze/ProximitySensor.java b/MotoActions/src/org/lineageos/settings/device/doze/ProximitySensor.java
deleted file mode 100644 (file)
index 5ba148c..0000000
+++ /dev/null
@@ -1,81 +0,0 @@
-/*
- * Copyright (c) 2015 The CyanogenMod Project
- * Copyright (c) 2017 The LineageOS Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.lineageos.settings.device.doze;
-
-import android.hardware.Sensor;
-import android.hardware.SensorEvent;
-import android.hardware.SensorEventListener;
-import android.util.Log;
-
-import org.lineageos.settings.device.MotoActionsSettings;
-import org.lineageos.settings.device.SensorAction;
-import org.lineageos.settings.device.SensorHelper;
-
-public class ProximitySensor implements ScreenStateNotifier, SensorEventListener {
-    private static final String TAG = "MotoActions-ProximitySensor";
-
-    private final MotoActionsSettings mMotoActionsSettings;
-    private final SensorHelper mSensorHelper;
-    private final SensorAction mSensorAction;
-    private final Sensor mSensor;
-
-    private boolean mEnabled;
-
-    private boolean mSawNear = false;
-
-    public ProximitySensor(MotoActionsSettings MotoActionsSettings, SensorHelper sensorHelper,
-                SensorAction action) {
-        mMotoActionsSettings = MotoActionsSettings;
-        mSensorHelper = sensorHelper;
-        mSensorAction = action;
-
-        mSensor = sensorHelper.getProximitySensor();
-    }
-
-    @Override
-    public void screenTurnedOn() {
-        if (mEnabled) {
-            Log.d(TAG, "Disabling");
-            mSensorHelper.unregisterListener(this);
-            mEnabled = false;
-        }
-    }
-
-    @Override
-    public void screenTurnedOff() {
-        if (mMotoActionsSettings.isIrWakeupEnabled() && !mEnabled) {
-            Log.d(TAG, "Enabling");
-            mSensorHelper.registerListener(mSensor, this);
-            mEnabled = true;
-        }
-    }
-
-    @Override
-    public void onSensorChanged(SensorEvent event) {
-        boolean isNear = event.values[0] < mSensor.getMaximumRange();
-        if (mSawNear && !isNear) {
-            Log.d(TAG, "wave triggered");
-            mSensorAction.action();
-        }
-        mSawNear = isNear;
-    }
-
-    @Override
-    public void onAccuracyChanged(Sensor mSensor, int accuracy) {
-    }
-}
index f1d7b348bf00a7524f304625554675cf20c6d91d..1da40f8d77d153fc33b5a7c666057016183c49dc 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Copyright (c) 2015 The CyanogenMod Project
- * Copyright (c) 2017 The LineageOS Project
+ * Copyright (c) 2017-2022 The LineageOS Project
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -22,15 +22,18 @@ import android.hardware.SensorEvent;
 import android.hardware.SensorEventListener;
 import android.util.Log;
 
-import java.lang.System;
-
 import org.lineageos.settings.device.MotoActionsSettings;
 import org.lineageos.settings.device.SensorAction;
 import org.lineageos.settings.device.SensorHelper;
 
 public class StowSensor implements ScreenStateNotifier, SensorEventListener {
     private static final String TAG = "MotoActions-StowSensor";
-    private static final int IN_POCKET_MIN_TIME = 5000;
+
+    // Maximum time for the hand to cover the sensor: 1s
+    private static final long HANDWAVE_MAX_DELTA_NS = 1000L * 1000 * 1000;
+
+    // Minimum time until the device is considered to have been in the pocket: 5s
+    private static final long POCKET_MIN_DELTA_NS = 5000L * 1000 * 1000;
 
     private final MotoActionsSettings mMotoActionsSettings;
     private final SensorHelper mSensorHelper;
@@ -39,10 +42,10 @@ public class StowSensor implements ScreenStateNotifier, SensorEventListener {
 
     private boolean mEnabled;
     private boolean mLastStowed;
-    private long isStowedTime;
+    private long mLastStowedTime;
 
     public StowSensor(MotoActionsSettings MotoActionsSettings, SensorHelper sensorHelper,
-                SensorAction action) {
+                      SensorAction action) {
         mMotoActionsSettings = MotoActionsSettings;
         mSensorHelper = sensorHelper;
         mSensorAction = action;
@@ -61,7 +64,9 @@ public class StowSensor implements ScreenStateNotifier, SensorEventListener {
 
     @Override
     public void screenTurnedOff() {
-        if (mMotoActionsSettings.isPocketGestureEnabled() && !mEnabled) {
+        if ((mMotoActionsSettings.isPocketGestureEnabled()
+                || mMotoActionsSettings.isIrWakeupEnabled())
+                && !mEnabled) {
             Log.d(TAG, "Enabling");
             mSensorHelper.registerListener(mSensor, this);
             mEnabled = true;
@@ -71,12 +76,10 @@ public class StowSensor implements ScreenStateNotifier, SensorEventListener {
     @Override
     public void onSensorChanged(SensorEvent event) {
         boolean thisStowed = (event.values[0] != 0);
-        if(thisStowed){
-            isStowedTime = System.currentTimeMillis();
-        } else if (mLastStowed && !thisStowed) {
-            long inPocketTime = System.currentTimeMillis() - isStowedTime;
-            if(inPocketTime >= IN_POCKET_MIN_TIME){
-                Log.d(TAG, "Triggered after " + inPocketTime / 1000 + " seconds");
+        if (thisStowed) {
+            mLastStowedTime = event.timestamp;
+        } else if (mLastStowed) {
+            if (shouldPulse(event.timestamp)) {
                 mSensorAction.action();
             }
         }
@@ -84,6 +87,22 @@ public class StowSensor implements ScreenStateNotifier, SensorEventListener {
         Log.d(TAG, "event: " + thisStowed);
     }
 
+    private boolean shouldPulse(long timestamp) {
+        long delta = timestamp - mLastStowedTime;
+
+        boolean irWakeupEnabled = mMotoActionsSettings.isIrWakeupEnabled();
+        boolean pocketGestureEnabled = mMotoActionsSettings.isPocketGestureEnabled();
+
+        if (irWakeupEnabled && pocketGestureEnabled) {
+            return true;
+        } else if (irWakeupEnabled) {
+            return delta < HANDWAVE_MAX_DELTA_NS;
+        } else if (pocketGestureEnabled) {
+            return delta >= POCKET_MIN_DELTA_NS;
+        }
+        return false;
+    }
+
     @Override
     public void onAccuracyChanged(Sensor sensor, int accuracy) {
     }