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