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