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 com.android.internal.annotations.VisibleForTesting;
20 import android.telecom.Log;
21 
22 /**
23  * Handles acquisition and release of wake locks relating to call state.
24  */
25 @VisibleForTesting
26 public class InCallWakeLockController extends CallsManagerListenerBase {
27 
28     private final TelecomWakeLock mTelecomWakeLock;
29     private final CallsManager mCallsManager;
30 
31     @VisibleForTesting
InCallWakeLockController(TelecomWakeLock telecomWakeLock, CallsManager callsManager)32     public InCallWakeLockController(TelecomWakeLock telecomWakeLock, CallsManager callsManager) {
33         mCallsManager = callsManager;
34 
35         mTelecomWakeLock = telecomWakeLock;
36         mTelecomWakeLock.setReferenceCounted(false);
37     }
38 
39     @Override
onCallAdded(Call call)40     public void onCallAdded(Call call) {
41         if (call.isExternalCall()) {
42             return;
43         }
44         handleWakeLock();
45     }
46 
47     @Override
onCallRemoved(Call call)48     public void onCallRemoved(Call call) {
49         if (call.isExternalCall()) {
50             return;
51         }
52         handleWakeLock();
53     }
54 
55     @Override
onCallStateChanged(Call call, int oldState, int newState)56     public void onCallStateChanged(Call call, int oldState, int newState) {
57         if (call.isExternalCall()) {
58             return;
59         }
60         handleWakeLock();
61     }
62 
63     @Override
onExternalCallChanged(Call call, boolean isExternalCall)64     public void onExternalCallChanged(Call call, boolean isExternalCall) {
65         // In case a call changes its external call state during ringing, we need to trigger
66         // the wake lock update correspondingly. External call is handled by another device
67         // and should not hold a wake lock on the local device.
68         handleWakeLock();
69     }
70 
handleWakeLock()71     private void handleWakeLock() {
72         // We grab a full lock as long as there exists a ringing call.
73         Call ringingCall = mCallsManager.getRingingOrSimulatedRingingCall();
74         if (ringingCall != null && !ringingCall.isExternalCall()) {
75             mTelecomWakeLock.acquire();
76             Log.i(this, "Acquiring full wake lock");
77         } else {
78             mTelecomWakeLock.release(0);
79             Log.i(this, "Releasing full wake lock");
80         }
81     }
82 }
83