2018-07-13 08:38:39 -04:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2015 The CyanogenMod Project
|
|
|
|
* 2017-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 PickupSensor implements SensorEventListener {
|
|
|
|
|
|
|
|
private static final boolean DEBUG = false;
|
|
|
|
private static final String TAG = "PickupSensor";
|
|
|
|
|
|
|
|
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 PickupSensor(Context context) {
|
|
|
|
mContext = context;
|
|
|
|
mSensorManager = mContext.getSystemService(SensorManager.class);
|
2019-04-26 12:22:38 -04:00
|
|
|
mSensor = Utils.getSensor(mSensorManager, "oneplus.sensor.op_motion_detect");
|
2018-07-13 08:38:39 -04:00
|
|
|
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] == 1) {
|
|
|
|
Utils.launchDozePulse(mContext);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onAccuracyChanged(Sensor sensor, int accuracy) {
|
|
|
|
/* Empty */
|
|
|
|
}
|
|
|
|
|
|
|
|
protected void enable() {
|
|
|
|
if (DEBUG) Log.d(TAG, "Enabling");
|
|
|
|
submit(() -> {
|
2019-01-08 07:25:19 -05:00
|
|
|
mEntryTimestamp = SystemClock.elapsedRealtime();
|
2018-07-13 08:38:39 -04:00
|
|
|
mSensorManager.registerListener(this, mSensor,
|
|
|
|
SensorManager.SENSOR_DELAY_NORMAL);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
protected void disable() {
|
|
|
|
if (DEBUG) Log.d(TAG, "Disabling");
|
|
|
|
submit(() -> {
|
|
|
|
mSensorManager.unregisterListener(this, mSensor);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|