blob: ac93bb5ece70230b058574a5c4cba93888cb3cab [file] [log] [blame]
Santos Cordon61e6f352014-12-03 02:53:34 -08001/*
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
17package com.android.server.telecom;
18
Ihab Awada3653902015-01-23 13:44:46 -080019import com.android.internal.annotations.VisibleForTesting;
Brad Ebingera3eccfe2016-10-05 15:45:22 -070020import android.telecom.Log;
Ihab Awada3653902015-01-23 13:44:46 -080021
Santos Cordon61e6f352014-12-03 02:53:34 -080022/**
23 * Handles acquisition and release of wake locks relating to call state.
24 */
Ihab Awada3653902015-01-23 13:44:46 -080025@VisibleForTesting
26public class InCallWakeLockController extends CallsManagerListenerBase {
Santos Cordon61e6f352014-12-03 02:53:34 -080027
Brad Ebinger63a89f22015-12-08 17:57:59 -080028 private final TelecomWakeLock mTelecomWakeLock;
Santos Cordon61e6f352014-12-03 02:53:34 -080029 private final CallsManager mCallsManager;
30
Ihab Awada3653902015-01-23 13:44:46 -080031 @VisibleForTesting
Brad Ebinger63a89f22015-12-08 17:57:59 -080032 public InCallWakeLockController(TelecomWakeLock telecomWakeLock, CallsManager callsManager) {
Santos Cordon61e6f352014-12-03 02:53:34 -080033 mCallsManager = callsManager;
34
Brad Ebinger63a89f22015-12-08 17:57:59 -080035 mTelecomWakeLock = telecomWakeLock;
36 mTelecomWakeLock.setReferenceCounted(false);
Santos Cordon61e6f352014-12-03 02:53:34 -080037 }
38
39 @Override
40 public void onCallAdded(Call call) {
Tyler Gunnf15dc332016-06-07 16:01:41 -070041 if (call.isExternalCall()) {
42 return;
43 }
Santos Cordon61e6f352014-12-03 02:53:34 -080044 handleWakeLock();
45 }
46
47 @Override
48 public void onCallRemoved(Call call) {
Tyler Gunnf15dc332016-06-07 16:01:41 -070049 if (call.isExternalCall()) {
50 return;
51 }
Santos Cordon61e6f352014-12-03 02:53:34 -080052 handleWakeLock();
53 }
54
55 @Override
56 public void onCallStateChanged(Call call, int oldState, int newState) {
Tyler Gunnf15dc332016-06-07 16:01:41 -070057 if (call.isExternalCall()) {
58 return;
59 }
Santos Cordon61e6f352014-12-03 02:53:34 -080060 handleWakeLock();
61 }
62
63 private void handleWakeLock() {
64 // We grab a full lock as long as there exists a ringing call.
65 Call ringingCall = mCallsManager.getRingingCall();
66 if (ringingCall != null) {
Brad Ebinger63a89f22015-12-08 17:57:59 -080067 mTelecomWakeLock.acquire();
Santos Cordon61e6f352014-12-03 02:53:34 -080068 Log.i(this, "Acquiring full wake lock");
Brad Ebinger63a89f22015-12-08 17:57:59 -080069 } else {
70 mTelecomWakeLock.release(0);
Santos Cordon61e6f352014-12-03 02:53:34 -080071 Log.i(this, "Releasing full wake lock");
72 }
73 }
74}