summaryrefslogtreecommitdiff
path: root/java/com/android/incallui/answer/impl/classifier/ProximityClassifier.java
blob: 28701ea6d3d6e6c247379a219bd6c6f18a452ce3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*
 * Copyright (C) 2015 The Android Open Source 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 com.android.incallui.answer.impl.classifier;

import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.view.MotionEvent;
import java.util.concurrent.TimeUnit;

/**
 * A classifier which looks at the proximity sensor during the gesture. It calculates the percentage
 * the proximity sensor showing the near state during the whole gesture
 */
class ProximityClassifier extends GestureClassifier {
  private long mGestureStartTimeNano;
  private long mNearStartTimeNano;
  private long mNearDuration;
  private boolean mNear;
  private float mAverageNear;

  public ProximityClassifier(ClassifierData classifierData) {}

  @Override
  public String getTag() {
    return "PROX";
  }

  @Override
  public void onSensorChanged(SensorEvent event) {
    if (event.sensor.getType() == Sensor.TYPE_PROXIMITY) {
      update(event.values[0] < event.sensor.getMaximumRange(), event.timestamp);
    }
  }

  @Override
  public void onTouchEvent(MotionEvent event) {
    int action = event.getActionMasked();

    if (action == MotionEvent.ACTION_DOWN) {
      mGestureStartTimeNano = TimeUnit.MILLISECONDS.toNanos(event.getEventTime());
      mNearStartTimeNano = TimeUnit.MILLISECONDS.toNanos(event.getEventTime());
      mNearDuration = 0;
    }

    if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_CANCEL) {
      update(mNear, TimeUnit.MILLISECONDS.toNanos(event.getEventTime()));
      long duration = TimeUnit.MILLISECONDS.toNanos(event.getEventTime()) - mGestureStartTimeNano;

      if (duration == 0) {
        mAverageNear = mNear ? 1.0f : 0.0f;
      } else {
        mAverageNear = (float) mNearDuration / (float) duration;
      }
    }
  }

  /**
   * @param near is the sensor showing the near state right now
   * @param timestampNano time of this event in nanoseconds
   */
  private void update(boolean near, long timestampNano) {
    // This if is necessary because MotionEvents and SensorEvents do not come in
    // chronological order
    if (timestampNano > mNearStartTimeNano) {
      // if the state before was near then add the difference of the current time and
      // mNearStartTimeNano to mNearDuration.
      if (mNear) {
        mNearDuration += timestampNano - mNearStartTimeNano;
      }

      // if the new state is near, set mNearStartTimeNano equal to this moment.
      if (near) {
        mNearStartTimeNano = timestampNano;
      }
    }
    mNear = near;
  }

  @Override
  public float getFalseTouchEvaluation() {
    return ProximityEvaluator.evaluate(mAverageNear);
  }
}