summaryrefslogtreecommitdiff
path: root/java/com/android/incallui/answerproximitysensor/AnswerProximitySensor.java
blob: 8aeb05fea66e59c14b30527b255c58669ee70b1b (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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
 * Copyright (C) 2016 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.answerproximitysensor;

import android.content.Context;
import android.hardware.display.DisplayManager;
import android.os.PowerManager;
import android.os.Trace;
import android.view.Display;
import com.android.dialer.common.LogUtil;
import com.android.dialer.configprovider.ConfigProviderComponent;
import com.android.incallui.call.DialerCall;
import com.android.incallui.call.DialerCallListener;
import com.android.incallui.call.state.DialerCallState;

/**
 * This class prevents users from accidentally answering calls by keeping the screen off until the
 * proximity sensor is unblocked. If the screen is already on or if this is a call waiting call then
 * nothing is done.
 */
public class AnswerProximitySensor
    implements DialerCallListener, AnswerProximityWakeLock.ScreenOnListener {

  private static final String CONFIG_ANSWER_PROXIMITY_SENSOR_ENABLED =
      "answer_proximity_sensor_enabled";
  private static final String CONFIG_ANSWER_PSEUDO_PROXIMITY_WAKE_LOCK_ENABLED =
      "answer_pseudo_proximity_wake_lock_enabled";

  private final DialerCall call;
  private final AnswerProximityWakeLock answerProximityWakeLock;

  public static boolean shouldUse(Context context, DialerCall call) {
    Trace.beginSection("AnswerProximitySensor.shouldUse");
    // Don't use the AnswerProximitySensor for call waiting and other states. Those states are
    // handled by the general ProximitySensor code.
    if (call.getState() != DialerCallState.INCOMING) {
      LogUtil.i("AnswerProximitySensor.shouldUse", "call state is not incoming");
      Trace.endSection();
      return false;
    }

    if (!ConfigProviderComponent.get(context)
        .getConfigProvider()
        .getBoolean(CONFIG_ANSWER_PROXIMITY_SENSOR_ENABLED, true)) {
      LogUtil.i("AnswerProximitySensor.shouldUse", "disabled by config");
      Trace.endSection();
      return false;
    }

    if (!context
        .getSystemService(PowerManager.class)
        .isWakeLockLevelSupported(PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK)) {
      LogUtil.i("AnswerProximitySensor.shouldUse", "wake lock level not supported");
      Trace.endSection();
      return false;
    }

    if (isDefaultDisplayOn(context)) {
      LogUtil.i("AnswerProximitySensor.shouldUse", "display is already on");
      Trace.endSection();
      return false;
    }

    Trace.endSection();
    return true;
  }

  public AnswerProximitySensor(
      Context context, DialerCall call, PseudoScreenState pseudoScreenState) {
    Trace.beginSection("AnswerProximitySensor Constructor");
    this.call = call;

    LogUtil.i("AnswerProximitySensor.constructor", "acquiring lock");
    if (ConfigProviderComponent.get(context)
        .getConfigProvider()
        .getBoolean(CONFIG_ANSWER_PSEUDO_PROXIMITY_WAKE_LOCK_ENABLED, true)) {
      answerProximityWakeLock = new PseudoProximityWakeLock(context, pseudoScreenState);
    } else {
      // TODO(twyen): choose a wake lock implementation base on framework/device.
      // These bugs requires the PseudoProximityWakeLock workaround:
      // a bug Proximity sensor not working on M
      // a bug fautly touch input when screen is off on marlin/sailfish
      answerProximityWakeLock = new SystemProximityWakeLock(context);
    }
    answerProximityWakeLock.setScreenOnListener(this);
    answerProximityWakeLock.acquire();

    call.addListener(this);
    Trace.endSection();
  }

  private void cleanup() {
    Trace.beginSection("AnswerProximitySensor.Cleanup");
    call.removeListener(this);
    releaseProximityWakeLock();
    Trace.endSection();
  }

  private void releaseProximityWakeLock() {
    if (answerProximityWakeLock.isHeld()) {
      LogUtil.i("AnswerProximitySensor.releaseProximityWakeLock", "releasing lock");
      answerProximityWakeLock.release();
    }
  }

  private static boolean isDefaultDisplayOn(Context context) {
    Display display =
        context.getSystemService(DisplayManager.class).getDisplay(Display.DEFAULT_DISPLAY);
    return display.getState() == Display.STATE_ON;
  }

  @Override
  public void onDialerCallDisconnect() {
    LogUtil.i("AnswerProximitySensor.onDialerCallDisconnect", null);
    cleanup();
  }

  @Override
  public void onDialerCallUpdate() {
    if (call.getState() != DialerCallState.INCOMING) {
      LogUtil.i("AnswerProximitySensor.onDialerCallUpdate", "no longer incoming, cleaning up");
      cleanup();
    }
  }

  @Override
  public void onDialerCallChildNumberChange() {}

  @Override
  public void onDialerCallLastForwardedNumberChange() {}

  @Override
  public void onDialerCallUpgradeToVideo() {}

  @Override
  public void onWiFiToLteHandover() {}

  @Override
  public void onHandoverToWifiFailure() {}

  @Override
  public void onInternationalCallOnWifi() {}

  @Override
  public void onEnrichedCallSessionUpdate() {}

  @Override
  public void onDialerCallSessionModificationStateChange() {}

  @Override
  public void onScreenOn() {
    cleanup();
  }
}