2 * Copyright (c) 2017 The LineageOS Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org
.lineageos
.settings
.device
.doze
;
19 import android
.hardware
.Sensor
;
20 import android
.hardware
.SensorEvent
;
21 import android
.hardware
.SensorEventListener
;
22 import android
.util
.Log
;
24 import org
.lineageos
.settings
.device
.LineageActionsSettings
;
25 import org
.lineageos
.settings
.device
.SensorAction
;
26 import org
.lineageos
.settings
.device
.SensorHelper
;
28 public class GlanceSensor
implements ScreenStateNotifier
{
29 private static final String TAG
= "LineageActions-GlanceSensor";
31 private final LineageActionsSettings mLineageActionsSettings
;
32 private final SensorHelper mSensorHelper
;
33 private final SensorAction mSensorAction
;
35 private final Sensor mSensor
;
36 private final Sensor mApproachSensor
;
38 private boolean mEnabled
;
40 public GlanceSensor(LineageActionsSettings lineageActionsSettings
, SensorHelper sensorHelper
,
41 SensorAction action
) {
42 mLineageActionsSettings
= lineageActionsSettings
;
43 mSensorHelper
= sensorHelper
;
44 mSensorAction
= action
;
46 mSensor
= sensorHelper
.getGlanceSensor();
47 mApproachSensor
= sensorHelper
.getApproachGlanceSensor();
51 public void screenTurnedOn() {
53 Log
.d(TAG
, "Disabling");
54 mSensorHelper
.unregisterListener(mGlanceListener
);
55 mSensorHelper
.unregisterListener(mApproachGlanceListener
);
61 public void screenTurnedOff() {
62 if (mLineageActionsSettings
.isPickUpEnabled() && !mEnabled
) {
63 Log
.d(TAG
, "Enabling");
64 mSensorHelper
.registerListener(mSensor
, mGlanceListener
);
65 mSensorHelper
.registerListener(mSensor
, mApproachGlanceListener
);
70 private SensorEventListener mGlanceListener
= new SensorEventListener() {
72 public synchronized void onSensorChanged(SensorEvent event
) {
73 Log
.d(TAG
, "Changed");
74 mSensorAction
.action();
78 public void onAccuracyChanged(Sensor mSensor
, int accuracy
) {
82 private SensorEventListener mApproachGlanceListener
= new SensorEventListener() {
84 public synchronized void onSensorChanged(SensorEvent event
) {
85 Log
.d(TAG
, "Approach: Changed");
86 mSensorAction
.action();
90 public void onAccuracyChanged(Sensor mSensor
, int accuracy
) {