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 
handleWakeLock()63     private void handleWakeLock() {
64         // We grab a full lock as long as there exists a ringing call.
65         Call ringingCall = mCallsManager.getRingingOrSimulatedRingingCall();
66         if (ringingCall != null) {
67             mTelecomWakeLock.acquire();
68             Log.i(this, "Acquiring full wake lock");
69         } else {
70             mTelecomWakeLock.release(0);
71             Log.i(this, "Releasing full wake lock");
72         }
73     }
74 }
75