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
17package com.android.server.telecom;
18
19import com.android.internal.annotations.VisibleForTesting;
20
21/**
22 * Handles acquisition and release of wake locks relating to call state.
23 */
24@VisibleForTesting
25public class InCallWakeLockController extends CallsManagerListenerBase {
26
27    private final TelecomWakeLock mTelecomWakeLock;
28    private final CallsManager mCallsManager;
29
30    @VisibleForTesting
31    public InCallWakeLockController(TelecomWakeLock telecomWakeLock, CallsManager callsManager) {
32        mCallsManager = callsManager;
33
34        mTelecomWakeLock = telecomWakeLock;
35        mTelecomWakeLock.setReferenceCounted(false);
36    }
37
38    @Override
39    public void onCallAdded(Call call) {
40        if (call.isExternalCall()) {
41            return;
42        }
43        handleWakeLock();
44    }
45
46    @Override
47    public void onCallRemoved(Call call) {
48        if (call.isExternalCall()) {
49            return;
50        }
51        handleWakeLock();
52    }
53
54    @Override
55    public void onCallStateChanged(Call call, int oldState, int newState) {
56        if (call.isExternalCall()) {
57            return;
58        }
59        handleWakeLock();
60    }
61
62    private void handleWakeLock() {
63        // We grab a full lock as long as there exists a ringing call.
64        Call ringingCall = mCallsManager.getRingingCall();
65        if (ringingCall != null) {
66            mTelecomWakeLock.acquire();
67            Log.i(this, "Acquiring full wake lock");
68        } else {
69            mTelecomWakeLock.release(0);
70            Log.i(this, "Releasing full wake lock");
71        }
72    }
73}
74