Yorke Lee | d134687 | 2014-07-28 14:38:45 -0700 | [diff] [blame] | 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 | |
Tyler Gunn | 7cc70b4 | 2014-09-12 22:17:27 -0700 | [diff] [blame] | 17 | package com.android.server.telecom; |
Yorke Lee | d134687 | 2014-07-28 14:38:45 -0700 | [diff] [blame] | 18 | |
| 19 | import android.content.Context; |
| 20 | import android.os.PowerManager; |
Brad Ebinger | a3eccfe | 2016-10-05 15:45:22 -0700 | [diff] [blame] | 21 | import android.telecom.Log; |
Yorke Lee | d134687 | 2014-07-28 14:38:45 -0700 | [diff] [blame] | 22 | |
Brad Ebinger | 63a89f2 | 2015-12-08 17:57:59 -0800 | [diff] [blame] | 23 | import com.android.internal.annotations.VisibleForTesting; |
| 24 | |
Yorke Lee | d134687 | 2014-07-28 14:38:45 -0700 | [diff] [blame] | 25 | /** |
| 26 | * This class manages the proximity sensor and allows callers to turn it on and off. |
| 27 | */ |
| 28 | public class ProximitySensorManager extends CallsManagerListenerBase { |
Yorke Lee | d134687 | 2014-07-28 14:38:45 -0700 | [diff] [blame] | 29 | |
Ihab Awad | 8de7691 | 2015-02-17 12:25:52 -0800 | [diff] [blame] | 30 | private final CallsManager mCallsManager; |
Brad Ebinger | 63a89f2 | 2015-12-08 17:57:59 -0800 | [diff] [blame] | 31 | private final TelecomWakeLock mTelecomWakeLock; |
Yorke Lee | d134687 | 2014-07-28 14:38:45 -0700 | [diff] [blame] | 32 | |
Brad Ebinger | 63a89f2 | 2015-12-08 17:57:59 -0800 | [diff] [blame] | 33 | public ProximitySensorManager(TelecomWakeLock telecomWakeLock, CallsManager callsManager) { |
Yorke Lee | d134687 | 2014-07-28 14:38:45 -0700 | [diff] [blame] | 34 | |
Brad Ebinger | 63a89f2 | 2015-12-08 17:57:59 -0800 | [diff] [blame] | 35 | mTelecomWakeLock = telecomWakeLock; |
Ihab Awad | 8de7691 | 2015-02-17 12:25:52 -0800 | [diff] [blame] | 36 | mCallsManager = callsManager; |
Brad Ebinger | 63a89f2 | 2015-12-08 17:57:59 -0800 | [diff] [blame] | 37 | Log.d(this, "onCreate: mProximityWakeLock: ", mTelecomWakeLock); |
Yorke Lee | d134687 | 2014-07-28 14:38:45 -0700 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | @Override |
| 41 | public void onCallRemoved(Call call) { |
Tyler Gunn | f15dc33 | 2016-06-07 16:01:41 -0700 | [diff] [blame] | 42 | if (call.isExternalCall()) { |
| 43 | return; |
| 44 | } |
Ihab Awad | 8de7691 | 2015-02-17 12:25:52 -0800 | [diff] [blame] | 45 | if (mCallsManager.getCalls().isEmpty()) { |
Yorke Lee | cce72fc | 2014-10-20 10:07:14 -0700 | [diff] [blame] | 46 | Log.i(this, "All calls removed, resetting proximity sensor to default state"); |
Yorke Lee | d134687 | 2014-07-28 14:38:45 -0700 | [diff] [blame] | 47 | turnOff(true); |
| 48 | } |
| 49 | super.onCallRemoved(call); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Turn the proximity sensor on. |
| 54 | */ |
Brad Ebinger | 63a89f2 | 2015-12-08 17:57:59 -0800 | [diff] [blame] | 55 | @VisibleForTesting |
| 56 | public void turnOn() { |
Ihab Awad | 8de7691 | 2015-02-17 12:25:52 -0800 | [diff] [blame] | 57 | if (mCallsManager.getCalls().isEmpty()) { |
Santos Cordon | b6a108f | 2014-09-02 16:53:53 -0700 | [diff] [blame] | 58 | Log.w(this, "Asking to turn on prox sensor without a call? I don't think so."); |
| 59 | return; |
| 60 | } |
| 61 | |
Brad Ebinger | 63a89f2 | 2015-12-08 17:57:59 -0800 | [diff] [blame] | 62 | mTelecomWakeLock.acquire(); |
Yorke Lee | d134687 | 2014-07-28 14:38:45 -0700 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Turn the proximity sensor off. |
| 67 | * @param screenOnImmediately |
| 68 | */ |
Brad Ebinger | 63a89f2 | 2015-12-08 17:57:59 -0800 | [diff] [blame] | 69 | @VisibleForTesting |
| 70 | public void turnOff(boolean screenOnImmediately) { |
| 71 | int flags = (screenOnImmediately ? 0 : PowerManager.RELEASE_FLAG_WAIT_FOR_NO_PROXIMITY); |
| 72 | mTelecomWakeLock.release(flags); |
Yorke Lee | d134687 | 2014-07-28 14:38:45 -0700 | [diff] [blame] | 73 | } |
| 74 | } |