1 /*
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 
17 package com.android.server.telecom;
18 
19 import android.content.Context;
20 import android.os.PowerManager;
21 import android.telecom.Log;
22 
23 import com.android.internal.annotations.VisibleForTesting;
24 
25 /**
26  * This class manages the proximity sensor and allows callers to turn it on and off.
27  */
28 public class ProximitySensorManager extends CallsManagerListenerBase {
29 
30     private final CallsManager mCallsManager;
31     private final TelecomWakeLock mTelecomWakeLock;
32 
ProximitySensorManager(TelecomWakeLock telecomWakeLock, CallsManager callsManager)33     public ProximitySensorManager(TelecomWakeLock telecomWakeLock, CallsManager callsManager) {
34 
35         mTelecomWakeLock = telecomWakeLock;
36         mCallsManager = callsManager;
37         Log.d(this, "onCreate: mProximityWakeLock: ", mTelecomWakeLock);
38     }
39 
40     @Override
onCallRemoved(Call call)41     public void onCallRemoved(Call call) {
42         if (call.isExternalCall()) {
43             return;
44         }
45         if (mCallsManager.getCalls().isEmpty()) {
46             Log.i(this, "All calls removed, resetting proximity sensor to default state");
47             turnOff(true);
48         }
49         super.onCallRemoved(call);
50     }
51 
52     /**
53      * Turn the proximity sensor on.
54      */
55     @VisibleForTesting
turnOn()56     public void turnOn() {
57         if (mCallsManager.getCalls().isEmpty()) {
58             Log.w(this, "Asking to turn on prox sensor without a call? I don't think so.");
59             return;
60         }
61 
62         mTelecomWakeLock.acquire();
63     }
64 
65     /**
66      * Turn the proximity sensor off.
67      * @param screenOnImmediately
68      */
69     @VisibleForTesting
turnOff(boolean screenOnImmediately)70     public void turnOff(boolean screenOnImmediately) {
71         int flags = (screenOnImmediately ? 0 : PowerManager.RELEASE_FLAG_WAIT_FOR_NO_PROXIMITY);
72         mTelecomWakeLock.release(flags);
73     }
74 }
75