MotoActions: Relocate to AOSP gestures category
[GitHub/LineageOS/android_hardware_motorola.git] / MotoActions / src / org / lineageos / settings / device / doze / GlanceSensor.java
CommitLineData
b71d9a4e
JA
1/*
2 * Copyright (c) 2017 The LineageOS Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package org.lineageos.settings.device.doze;
18
19import android.hardware.Sensor;
20import android.hardware.SensorEvent;
21import android.hardware.SensorEventListener;
22import android.util.Log;
23
d2314727 24import org.lineageos.settings.device.MotoActionsSettings;
b71d9a4e
JA
25import org.lineageos.settings.device.SensorAction;
26import org.lineageos.settings.device.SensorHelper;
27
28public class GlanceSensor implements ScreenStateNotifier {
d2314727 29 private static final String TAG = "MotoActions-GlanceSensor";
b71d9a4e 30
d2314727 31 private final MotoActionsSettings mMotoActionsSettings;
b71d9a4e
JA
32 private final SensorHelper mSensorHelper;
33 private final SensorAction mSensorAction;
34
35 private final Sensor mSensor;
36 private final Sensor mApproachSensor;
37
38 private boolean mEnabled;
39
d2314727 40 public GlanceSensor(MotoActionsSettings MotoActionsSettings, SensorHelper sensorHelper,
b71d9a4e 41 SensorAction action) {
d2314727 42 mMotoActionsSettings = MotoActionsSettings;
b71d9a4e
JA
43 mSensorHelper = sensorHelper;
44 mSensorAction = action;
45
46 mSensor = sensorHelper.getGlanceSensor();
47 mApproachSensor = sensorHelper.getApproachGlanceSensor();
48 }
49
50 @Override
51 public void screenTurnedOn() {
52 if (mEnabled) {
53 Log.d(TAG, "Disabling");
54 mSensorHelper.unregisterListener(mGlanceListener);
55 mSensorHelper.unregisterListener(mApproachGlanceListener);
56 mEnabled = false;
57 }
58 }
59
60 @Override
61 public void screenTurnedOff() {
d2314727 62 if (mMotoActionsSettings.isPickUpEnabled() && !mEnabled) {
b71d9a4e
JA
63 Log.d(TAG, "Enabling");
64 mSensorHelper.registerListener(mSensor, mGlanceListener);
bcfceddb 65 mSensorHelper.registerListener(mApproachSensor, mApproachGlanceListener);
b71d9a4e
JA
66 mEnabled = true;
67 }
68 }
69
70 private SensorEventListener mGlanceListener = new SensorEventListener() {
71 @Override
72 public synchronized void onSensorChanged(SensorEvent event) {
73 Log.d(TAG, "Changed");
74 mSensorAction.action();
75 }
76
77 @Override
78 public void onAccuracyChanged(Sensor mSensor, int accuracy) {
79 }
80 };
81
82 private SensorEventListener mApproachGlanceListener = new SensorEventListener() {
83 @Override
84 public synchronized void onSensorChanged(SensorEvent event) {
85 Log.d(TAG, "Approach: Changed");
86 mSensorAction.action();
87 }
88
89 @Override
90 public void onAccuracyChanged(Sensor mSensor, int accuracy) {
91 }
92 };
93}