samsung: doze: add standard pickup sensor support
authorMichael Bestas <mkbestas@lineageos.org>
Mon, 20 Jul 2020 20:16:26 +0000 (23:16 +0300)
committerTim Zimmermann <tim@linux4.de>
Sun, 3 Apr 2022 04:13:51 +0000 (06:13 +0200)
fcuzzocrea:

  * adapt original change to be suitable for SamsungDoze

Change-Id: Ie2572fec6d4307ce0b555f6ee4dbf92c9cf7bd87

doze/res/xml/gesture_panel.xml
doze/src/org/lineageos/settings/doze/SamsungDozeService.java
doze/src/org/lineageos/settings/doze/SamsungDozeSettings.java
doze/src/org/lineageos/settings/doze/Utils.java

index e747d99606ad6a2b58fde7d05dc5d77d52fc8a61..21c7eeb8d6258c6a425eef55ceefbdd405831eff 100644 (file)
             android:summary="@string/pocket_gesture_summary" />
     </PreferenceCategory>
 
+    <PreferenceCategory
+        android:key="pickup_sensor"
+        android:title="@string/pickup_sensor_title">
+        <SwitchPreference
+            android:key="gesture_pick_up"
+            android:defaultValue="false"
+            android:title="@string/pick_up_gesture_title"
+            android:summary="@string/pick_up_gesture_summary" />
+    </PreferenceCategory>
+
 </PreferenceScreen>
index 418197bcb645361243dbe36d4d95082bf6da172e..2ca12a9167c84435e04bf1a3b319b9f58798590b 100644 (file)
@@ -25,6 +25,8 @@ import android.hardware.Sensor;
 import android.hardware.SensorEvent;
 import android.hardware.SensorEventListener;
 import android.hardware.SensorManager;
+import android.hardware.TriggerEvent;
+import android.hardware.TriggerEventListener;
 import android.os.IBinder;
 import android.os.PowerManager;
 import android.os.SystemClock;
@@ -45,12 +47,52 @@ public class SamsungDozeService extends Service {
 
     private Context mContext;
     private ExecutorService mExecutorService;
-    private SamsungProximitySensor mSensor;
+    private SamsungProximitySensor mProximitySensor;
+    private SamsungPickUpSensor mPickUpSensor;
     private PowerManager mPowerManager;
 
     private boolean mHandwaveGestureEnabled = false;
+    private boolean mPickUpGestureEnabled = false;
     private boolean mPocketGestureEnabled = false;
 
+    class SamsungPickUpSensor {
+        private SensorManager mSensorManager;
+        private Sensor mSensor;
+
+        private long mEntryTimestamp;
+
+        public SamsungPickUpSensor(Context context) {
+            mSensorManager = mContext.getSystemService(SensorManager.class);
+            mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_PICK_UP_GESTURE);
+            mExecutorService = Executors.newSingleThreadExecutor();
+        }
+
+        private Future<?> submit(Runnable runnable) {
+            return mExecutorService.submit(runnable);
+        }
+
+        protected void enable() {
+            submit(() -> {
+                mSensorManager.requestTriggerSensor(mPickupListener, mSensor);
+            });
+        }
+
+        protected void disable() {
+            submit(() -> {
+                mSensorManager.cancelTriggerSensor(mPickupListener, mSensor);
+            });
+        }
+
+        private TriggerEventListener mPickupListener = new TriggerEventListener() {
+            @Override
+            public void onTrigger(TriggerEvent event) {
+                if (DEBUG) Log.d(TAG, "Triggered");
+                wakeOrLaunchDozePulse();
+                enable();
+            }
+        };
+    }
+
     class SamsungProximitySensor implements SensorEventListener {
         private SensorManager mSensorManager;
         private Sensor mSensor;
@@ -121,9 +163,11 @@ public class SamsungDozeService extends Service {
         if (DEBUG) Log.d(TAG, "SamsungDozeService Started");
         mContext = this;
         mPowerManager = getSystemService(PowerManager.class);
-        mSensor = new SamsungProximitySensor(mContext);
+        mProximitySensor = new SamsungProximitySensor(mContext);
+        mPickUpSensor = new SamsungPickUpSensor(mContext);
         if (!isInteractive()) {
-            mSensor.enable();
+            mProximitySensor.enable();
+            mPickUpSensor.enable();
         }
     }
 
@@ -159,12 +203,24 @@ public class SamsungDozeService extends Service {
 
     private void onDisplayOn() {
         if (DEBUG) Log.d(TAG, "Display on");
-        mSensor.disable();
+        if (Utils.isPickUpGestureEnabled(this)) {
+            mPickUpSensor.disable();
+        }
+        if (Utils.isHandwaveGestureEnabled(this) ||
+                Utils.isPocketGestureEnabled(this)) {
+            mProximitySensor.disable();
+        }
     }
 
     private void onDisplayOff() {
         if (DEBUG) Log.d(TAG, "Display off");
-        mSensor.enable();
+        if (Utils.isPickUpGestureEnabled(this)) {
+            mPickUpSensor.enable();
+        }
+        if (Utils.isHandwaveGestureEnabled(this) ||
+                Utils.isPocketGestureEnabled(this)) {
+            mProximitySensor.enable();
+        }
     }
 
     private BroadcastReceiver mScreenStateReceiver = new BroadcastReceiver() {
index d452464595c804a336693c9435f8872ce98fd4b6..f0f9b52525390bf3a1f3e1da4bf47e85d8ccdc45 100644 (file)
@@ -38,6 +38,7 @@ public class SamsungDozeSettings extends PreferenceFragment
 
     private SwitchPreference mAlwaysOnDisplayPreference;
     private SwitchPreference mHandwavePreference;
+    private SwitchPreference mPickUpPreference;
     private SwitchPreference mPocketPreference;
     private SwitchPreference mWakeOnGesturePreference;
 
@@ -62,6 +63,10 @@ public class SamsungDozeSettings extends PreferenceFragment
         mHandwavePreference.setEnabled(dozeEnabled);
         mHandwavePreference.setOnPreferenceChangeListener(this);
 
+        mPickUpPreference = findPreference(Utils.GESTURE_PICK_UP_KEY);
+        mPickUpPreference.setEnabled(dozeEnabled);
+        mPickUpPreference.setOnPreferenceChangeListener(this);
+
         mPocketPreference = findPreference(Utils.GESTURE_POCKET_KEY);
         mPocketPreference.setEnabled(dozeEnabled);
         mPocketPreference.setOnPreferenceChangeListener(this);
@@ -75,6 +80,7 @@ public class SamsungDozeSettings extends PreferenceFragment
             getPreferenceScreen().removePreference(mAlwaysOnDisplayPreference);
         } else {
             mHandwavePreference.setDependency(Utils.ALWAYS_ON_DISPLAY);
+            mPickUpPreference.setDependency(Utils.ALWAYS_ON_DISPLAY);
             mPocketPreference.setDependency(Utils.ALWAYS_ON_DISPLAY);
             mWakeOnGesturePreference.setDependency(Utils.ALWAYS_ON_DISPLAY);
         }
@@ -115,6 +121,7 @@ public class SamsungDozeSettings extends PreferenceFragment
         mAlwaysOnDisplayPreference.setEnabled(isChecked);
 
         mHandwavePreference.setEnabled(isChecked);
+        mPickUpPreference.setEnabled(isChecked);
         mPocketPreference.setEnabled(isChecked);
         mWakeOnGesturePreference.setEnabled(isChecked);
     }
index 1b78bed8d69a9bc4ef605f6f119fe7c7cd7dd59b..f6fec2bc5c7f34eecd20614d0ed0a77bccecdc20 100644 (file)
@@ -37,6 +37,7 @@ public final class Utils {
     protected static final String ALWAYS_ON_DISPLAY = "always_on_display";
     protected static final String DOZE_ENABLE = "doze_enable";
     protected static final String GESTURE_HAND_WAVE_KEY = "gesture_hand_wave";
+    protected static final String GESTURE_PICK_UP_KEY = "gesture_pick_up";
     protected static final String GESTURE_POCKET_KEY = "gesture_pocket";
     protected static final String WAKE_ON_GESTURE_KEY = "wake_on_gesture";
 
@@ -90,7 +91,8 @@ public final class Utils {
     }
 
     protected static boolean isAnyGestureEnabled(Context context) {
-        return isHandwaveGestureEnabled(context) || isPocketGestureEnabled(context);
+        return isHandwaveGestureEnabled(context) || isPickUpGestureEnabled(context) ||
+                isPocketGestureEnabled(context);
     }
 
     protected static boolean isHandwaveGestureEnabled(Context context) {
@@ -98,6 +100,11 @@ public final class Utils {
                 .getBoolean(GESTURE_HAND_WAVE_KEY, false);
     }
 
+    protected static boolean isPickUpGestureEnabled(Context context) {
+        return PreferenceManager.getDefaultSharedPreferences(context)
+                .getBoolean(GESTURE_PICK_UP_KEY, false);
+    }
+
     protected static boolean isPocketGestureEnabled(Context context) {
         return PreferenceManager.getDefaultSharedPreferences(context)
                 .getBoolean(GESTURE_POCKET_KEY, false);