blob: 69950640337f1706dd7f1b7be536753373880b60 [file] [log] [blame]
Blazej Magnowski68d0c9b2015-09-18 17:25:55 -07001/*
2 * Copyright (C) 2015 The Android Open Source 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 com.android.systemui.classifier;
18
19import android.hardware.Sensor;
20import android.hardware.SensorEvent;
21import android.view.MotionEvent;
22
23/**
24 * A classifier which looks at the proximity sensor during the gesture. It calculates the percentage
25 * the proximity sensor showing the near state during the whole gesture
26 */
27public class ProximityClassifier extends GestureClassifier {
28 private long mGestureStartTimeNano;
29 private long mNearStartTimeNano;
30 private long mNearDuration;
31 private boolean mNear;
32 private float mAverageNear;
33
34 public ProximityClassifier(ClassifierData classifierData) {
35 }
36
37 @Override
38 public void onSensorChanged(SensorEvent event) {
39 if (event.sensor.getType() == Sensor.TYPE_PROXIMITY) {
40 update(event.values[0] < event.sensor.getMaximumRange(), event.timestamp);
41 }
42 }
43
44 @Override
45 public void onTouchEvent(MotionEvent event) {
46 int action = event.getActionMasked();
47
48 if (action == MotionEvent.ACTION_DOWN) {
49 mGestureStartTimeNano = event.getEventTimeNano();
50 mNearStartTimeNano = event.getEventTimeNano();
51 mNearDuration = 0;
52 }
53
54 if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_CANCEL) {
55 update(mNear, event.getEventTimeNano());
56 long duration = event.getEventTimeNano() - mGestureStartTimeNano;
57
58 if (duration == 0) {
59 mAverageNear = mNear ? 1.0f : 0.0f;
60 } else {
61 mAverageNear = (float) mNearDuration / (float) duration;
62 }
63 }
64 }
65
66
67 /**
68 * @param near is the sensor showing the near state right now
69 * @param timestampNano time of this event in nanoseconds
70 */
71 private void update(boolean near, long timestampNano) {
72 // This if is necessary because MotionEvents and SensorEvents do not come in
73 // chronological order
74 if (timestampNano > mNearStartTimeNano) {
75 // if the state before was near then add the difference of the current time and
76 // mNearStartTimeNano to mNearDuration.
77 if (mNear) {
78 mNearDuration += timestampNano - mNearStartTimeNano;
79 }
80
81 // if the new state is near, set mNearStartTimeNano equal to this moment.
82 if (near) {
83 mNearStartTimeNano = timestampNano;
84 }
85 }
86 mNear = near;
87 }
88
89 @Override
90 public float getFalseTouchEvaluation(int type) {
91 return ProximityEvaluator.evaluate(mAverageNear, type);
92 }
93}