diff --git a/doze/res/xml/doze_settings.xml b/doze/res/xml/doze_settings.xml index 88727aa..5738316 100644 --- a/doze/res/xml/doze_settings.xml +++ b/doze/res/xml/doze_settings.xml @@ -28,4 +28,16 @@ + + + + + + diff --git a/doze/src/org/lineageos/settings/doze/DozeService.java b/doze/src/org/lineageos/settings/doze/DozeService.java index 423d810..3cfae43 100644 --- a/doze/src/org/lineageos/settings/doze/DozeService.java +++ b/doze/src/org/lineageos/settings/doze/DozeService.java @@ -30,11 +30,13 @@ public class DozeService extends Service { private static final boolean DEBUG = false; private PickupSensor mPickupSensor; + private PocketSensor mPocketSensor; @Override public void onCreate() { if (DEBUG) Log.d(TAG, "Creating service"); mPickupSensor = new PickupSensor(this); + mPocketSensor = new PocketSensor(this); IntentFilter screenStateFilter = new IntentFilter(); screenStateFilter.addAction(Intent.ACTION_SCREEN_ON); @@ -54,6 +56,7 @@ public class DozeService extends Service { super.onDestroy(); this.unregisterReceiver(mScreenStateReceiver); mPickupSensor.disable(); + mPocketSensor.disable(); } @Override @@ -66,6 +69,9 @@ public class DozeService extends Service { if (Utils.isPickUpEnabled(this)) { mPickupSensor.disable(); } + if (Utils.isPocketEnabled(this)) { + mPocketSensor.disable(); + } } private void onDisplayOff() { @@ -73,6 +79,9 @@ public class DozeService extends Service { if (Utils.isPickUpEnabled(this)) { mPickupSensor.enable(); } + if (Utils.isPocketEnabled(this)) { + mPocketSensor.enable(); + } } private BroadcastReceiver mScreenStateReceiver = new BroadcastReceiver() { diff --git a/doze/src/org/lineageos/settings/doze/PocketSensor.java b/doze/src/org/lineageos/settings/doze/PocketSensor.java new file mode 100644 index 0000000..5aca39e --- /dev/null +++ b/doze/src/org/lineageos/settings/doze/PocketSensor.java @@ -0,0 +1,93 @@ +/* + * Copyright (c) 2016 The CyanogenMod Project + * Copyright (c) 2018 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.doze; + +import android.content.Context; +import android.hardware.Sensor; +import android.hardware.SensorEvent; +import android.hardware.SensorEventListener; +import android.hardware.SensorManager; +import android.os.SystemClock; +import android.util.Log; + +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; + +public class PocketSensor implements SensorEventListener { + + private static final boolean DEBUG = false; + private static final String TAG = "PocketSensor"; + + private static final int MIN_PULSE_INTERVAL_MS = 2500; + + private SensorManager mSensorManager; + private Sensor mSensor; + private Context mContext; + private ExecutorService mExecutorService; + + private long mEntryTimestamp; + + public PocketSensor(Context context) { + mContext = context; + mSensorManager = mContext.getSystemService(SensorManager.class); + mSensor = Utils.getSensor(mSensorManager, "oneplus.sensor.pocket"); + mExecutorService = Executors.newSingleThreadExecutor(); + } + + private Future submit(Runnable runnable) { + return mExecutorService.submit(runnable); + } + + @Override + public void onSensorChanged(SensorEvent event) { + if (DEBUG) Log.d(TAG, "Got sensor event: " + event.values[0]); + + long delta = SystemClock.elapsedRealtime() - mEntryTimestamp; + if (delta < MIN_PULSE_INTERVAL_MS) { + return; + } + + mEntryTimestamp = SystemClock.elapsedRealtime(); + + if (event.values[0] == 0.0) { + Utils.launchDozePulse(mContext); + } + } + + @Override + public void onAccuracyChanged(Sensor sensor, int accuracy) { + /* Empty */ + } + + protected void enable() { + if (DEBUG) Log.d(TAG, "Enabling"); + submit(() -> { + mSensorManager.registerListener(this, mSensor, + SensorManager.SENSOR_DELAY_NORMAL); + }); + } + + protected void disable() { + if (DEBUG) Log.d(TAG, "Disabling"); + submit(() -> { + mSensorManager.unregisterListener(this, mSensor); + mEntryTimestamp = SystemClock.elapsedRealtime(); + }); + } +} diff --git a/doze/src/org/lineageos/settings/doze/Utils.java b/doze/src/org/lineageos/settings/doze/Utils.java index 32c859d..cbafe8c 100644 --- a/doze/src/org/lineageos/settings/doze/Utils.java +++ b/doze/src/org/lineageos/settings/doze/Utils.java @@ -36,6 +36,7 @@ public final class Utils { private static final String DOZE_INTENT = "com.android.systemui.doze.pulse"; protected static final String GESTURE_PICK_UP_KEY = "gesture_pick_up"; + protected static final String GESTURE_POCKET_KEY = "gesture_pocket"; protected static void startService(Context context) { if (DEBUG) Log.d(TAG, "Starting service"); @@ -50,7 +51,8 @@ public final class Utils { } protected static void checkDozeService(Context context) { - if (isDozeEnabled(context) && isPickUpEnabled(context)) { + if (isDozeEnabled(context) && + (isPickUpEnabled(context) || isPocketEnabled(context))) { startService(context); } else { stopService(context); @@ -83,6 +85,16 @@ public final class Utils { .getBoolean(GESTURE_PICK_UP_KEY, false); } + protected static void enablePocket(Context context, boolean enable) { + PreferenceManager.getDefaultSharedPreferences(context).edit() + .putBoolean(GESTURE_POCKET_KEY, enable).apply(); + } + + protected static boolean isPocketEnabled(Context context) { + return PreferenceManager.getDefaultSharedPreferences(context) + .getBoolean(GESTURE_POCKET_KEY, false); + } + protected static Sensor getSensor(SensorManager sm, String type) { for (Sensor sensor : sm.getSensorList(Sensor.TYPE_ALL)) { if (type.equals(sensor.getStringType())) {