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 /**
23  * This class manages the proximity sensor and allows callers to turn it on and off.
24  */
25 public class ProximitySensorManager extends CallsManagerListenerBase {
26     private static final String TAG = ProximitySensorManager.class.getSimpleName();
27 
28     private final PowerManager.WakeLock mProximityWakeLock;
29 
ProximitySensorManager(Context context)30     public ProximitySensorManager(Context context) {
31         PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
32 
33         if (pm.isWakeLockLevelSupported(PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK)) {
34             mProximityWakeLock = pm.newWakeLock(
35                     PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK, TAG);
36         } else {
37             mProximityWakeLock = null;
38         }
39         Log.d(this, "onCreate: mProximityWakeLock: ", mProximityWakeLock);
40     }
41 
42     @Override
onCallRemoved(Call call)43     public void onCallRemoved(Call call) {
44         if (CallsManager.getInstance().getCalls().isEmpty()) {
45             Log.i(this, "All calls removed, resetting proximity sensor to default state");
46             turnOff(true);
47         }
48         super.onCallRemoved(call);
49     }
50 
51     /**
52      * Turn the proximity sensor on.
53      */
turnOn()54     void turnOn() {
55         if (CallsManager.getInstance().getCalls().isEmpty()) {
56             Log.w(this, "Asking to turn on prox sensor without a call? I don't think so.");
57             return;
58         }
59 
60         if (mProximityWakeLock == null) {
61             return;
62         }
63         if (!mProximityWakeLock.isHeld()) {
64             Log.i(this, "Acquiring proximity wake lock");
65             mProximityWakeLock.acquire();
66         } else {
67             Log.i(this, "Proximity wake lock already acquired");
68         }
69     }
70 
71     /**
72      * Turn the proximity sensor off.
73      * @param screenOnImmediately
74      */
turnOff(boolean screenOnImmediately)75     void turnOff(boolean screenOnImmediately) {
76         if (mProximityWakeLock == null) {
77             return;
78         }
79         if (mProximityWakeLock.isHeld()) {
80             Log.i(this, "Releasing proximity wake lock");
81             int flags =
82                 (screenOnImmediately ? 0 : PowerManager.RELEASE_FLAG_WAIT_FOR_NO_PROXIMITY);
83             mProximityWakeLock.release(flags);
84         } else {
85             Log.i(this, "Proximity wake lock already released");
86         }
87     }
88 }
89