blob: a79c0edf8a405157415da69bf98c484a471e63a5 [file] [log] [blame]
Yorke Leed1346872014-07-28 14:38:45 -07001/*
2 * Copyright (C) 2014 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
Tyler Gunn7cc70b42014-09-12 22:17:27 -070017package com.android.server.telecom;
Yorke Leed1346872014-07-28 14:38:45 -070018
19import android.content.Context;
20import android.os.PowerManager;
Brad Ebingera3eccfe2016-10-05 15:45:22 -070021import android.telecom.Log;
Yorke Leed1346872014-07-28 14:38:45 -070022
Brad Ebinger63a89f22015-12-08 17:57:59 -080023import com.android.internal.annotations.VisibleForTesting;
24
Yorke Leed1346872014-07-28 14:38:45 -070025/**
26 * This class manages the proximity sensor and allows callers to turn it on and off.
27 */
28public class ProximitySensorManager extends CallsManagerListenerBase {
Yorke Leed1346872014-07-28 14:38:45 -070029
Ihab Awad8de76912015-02-17 12:25:52 -080030 private final CallsManager mCallsManager;
Brad Ebinger63a89f22015-12-08 17:57:59 -080031 private final TelecomWakeLock mTelecomWakeLock;
Yorke Leed1346872014-07-28 14:38:45 -070032
Brad Ebinger63a89f22015-12-08 17:57:59 -080033 public ProximitySensorManager(TelecomWakeLock telecomWakeLock, CallsManager callsManager) {
Yorke Leed1346872014-07-28 14:38:45 -070034
Brad Ebinger63a89f22015-12-08 17:57:59 -080035 mTelecomWakeLock = telecomWakeLock;
Ihab Awad8de76912015-02-17 12:25:52 -080036 mCallsManager = callsManager;
Brad Ebinger63a89f22015-12-08 17:57:59 -080037 Log.d(this, "onCreate: mProximityWakeLock: ", mTelecomWakeLock);
Yorke Leed1346872014-07-28 14:38:45 -070038 }
39
40 @Override
41 public void onCallRemoved(Call call) {
Tyler Gunnf15dc332016-06-07 16:01:41 -070042 if (call.isExternalCall()) {
43 return;
44 }
Ihab Awad8de76912015-02-17 12:25:52 -080045 if (mCallsManager.getCalls().isEmpty()) {
Yorke Leecce72fc2014-10-20 10:07:14 -070046 Log.i(this, "All calls removed, resetting proximity sensor to default state");
Yorke Leed1346872014-07-28 14:38:45 -070047 turnOff(true);
48 }
49 super.onCallRemoved(call);
50 }
51
52 /**
53 * Turn the proximity sensor on.
54 */
Brad Ebinger63a89f22015-12-08 17:57:59 -080055 @VisibleForTesting
56 public void turnOn() {
Ihab Awad8de76912015-02-17 12:25:52 -080057 if (mCallsManager.getCalls().isEmpty()) {
Santos Cordonb6a108f2014-09-02 16:53:53 -070058 Log.w(this, "Asking to turn on prox sensor without a call? I don't think so.");
59 return;
60 }
61
Brad Ebinger63a89f22015-12-08 17:57:59 -080062 mTelecomWakeLock.acquire();
Yorke Leed1346872014-07-28 14:38:45 -070063 }
64
65 /**
66 * Turn the proximity sensor off.
67 * @param screenOnImmediately
68 */
Brad Ebinger63a89f22015-12-08 17:57:59 -080069 @VisibleForTesting
70 public void turnOff(boolean screenOnImmediately) {
71 int flags = (screenOnImmediately ? 0 : PowerManager.RELEASE_FLAG_WAIT_FOR_NO_PROXIMITY);
72 mTelecomWakeLock.release(flags);
Yorke Leed1346872014-07-28 14:38:45 -070073 }
74}