161e6f352c9c1fe8dc346ccf8d3560f7060ec663aSantos Cordon/*
261e6f352c9c1fe8dc346ccf8d3560f7060ec663aSantos Cordon * Copyright (C) 2014 The Android Open Source Project
361e6f352c9c1fe8dc346ccf8d3560f7060ec663aSantos Cordon *
461e6f352c9c1fe8dc346ccf8d3560f7060ec663aSantos Cordon * Licensed under the Apache License, Version 2.0 (the "License");
561e6f352c9c1fe8dc346ccf8d3560f7060ec663aSantos Cordon * you may not use this file except in compliance with the License.
661e6f352c9c1fe8dc346ccf8d3560f7060ec663aSantos Cordon * You may obtain a copy of the License at
761e6f352c9c1fe8dc346ccf8d3560f7060ec663aSantos Cordon *
861e6f352c9c1fe8dc346ccf8d3560f7060ec663aSantos Cordon *      http://www.apache.org/licenses/LICENSE-2.0
961e6f352c9c1fe8dc346ccf8d3560f7060ec663aSantos Cordon *
1061e6f352c9c1fe8dc346ccf8d3560f7060ec663aSantos Cordon * Unless required by applicable law or agreed to in writing, software
1161e6f352c9c1fe8dc346ccf8d3560f7060ec663aSantos Cordon * distributed under the License is distributed on an "AS IS" BASIS,
1261e6f352c9c1fe8dc346ccf8d3560f7060ec663aSantos Cordon * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1361e6f352c9c1fe8dc346ccf8d3560f7060ec663aSantos Cordon * See the License for the specific language governing permissions and
1461e6f352c9c1fe8dc346ccf8d3560f7060ec663aSantos Cordon * limitations under the License.
1561e6f352c9c1fe8dc346ccf8d3560f7060ec663aSantos Cordon */
1661e6f352c9c1fe8dc346ccf8d3560f7060ec663aSantos Cordon
1761e6f352c9c1fe8dc346ccf8d3560f7060ec663aSantos Cordonpackage com.android.server.telecom;
1861e6f352c9c1fe8dc346ccf8d3560f7060ec663aSantos Cordon
19a365390b0d157fbe1655cdc41ea5d13fa13fdbdfIhab Awadimport com.android.internal.annotations.VisibleForTesting;
20a365390b0d157fbe1655cdc41ea5d13fa13fdbdfIhab Awad
2161e6f352c9c1fe8dc346ccf8d3560f7060ec663aSantos Cordon/**
2261e6f352c9c1fe8dc346ccf8d3560f7060ec663aSantos Cordon * Handles acquisition and release of wake locks relating to call state.
2361e6f352c9c1fe8dc346ccf8d3560f7060ec663aSantos Cordon */
24a365390b0d157fbe1655cdc41ea5d13fa13fdbdfIhab Awad@VisibleForTesting
25a365390b0d157fbe1655cdc41ea5d13fa13fdbdfIhab Awadpublic class InCallWakeLockController extends CallsManagerListenerBase {
2661e6f352c9c1fe8dc346ccf8d3560f7060ec663aSantos Cordon
2763a89f28d6e86844eea242a0175db21df1624db1Brad Ebinger    private final TelecomWakeLock mTelecomWakeLock;
2861e6f352c9c1fe8dc346ccf8d3560f7060ec663aSantos Cordon    private final CallsManager mCallsManager;
2961e6f352c9c1fe8dc346ccf8d3560f7060ec663aSantos Cordon
30a365390b0d157fbe1655cdc41ea5d13fa13fdbdfIhab Awad    @VisibleForTesting
3163a89f28d6e86844eea242a0175db21df1624db1Brad Ebinger    public InCallWakeLockController(TelecomWakeLock telecomWakeLock, CallsManager callsManager) {
3261e6f352c9c1fe8dc346ccf8d3560f7060ec663aSantos Cordon        mCallsManager = callsManager;
3361e6f352c9c1fe8dc346ccf8d3560f7060ec663aSantos Cordon
3463a89f28d6e86844eea242a0175db21df1624db1Brad Ebinger        mTelecomWakeLock = telecomWakeLock;
3563a89f28d6e86844eea242a0175db21df1624db1Brad Ebinger        mTelecomWakeLock.setReferenceCounted(false);
3661e6f352c9c1fe8dc346ccf8d3560f7060ec663aSantos Cordon    }
3761e6f352c9c1fe8dc346ccf8d3560f7060ec663aSantos Cordon
3861e6f352c9c1fe8dc346ccf8d3560f7060ec663aSantos Cordon    @Override
3961e6f352c9c1fe8dc346ccf8d3560f7060ec663aSantos Cordon    public void onCallAdded(Call call) {
40f15dc33f87f88e21ef745952a68af65c86e1bf1eTyler Gunn        if (call.isExternalCall()) {
41f15dc33f87f88e21ef745952a68af65c86e1bf1eTyler Gunn            return;
42f15dc33f87f88e21ef745952a68af65c86e1bf1eTyler Gunn        }
4361e6f352c9c1fe8dc346ccf8d3560f7060ec663aSantos Cordon        handleWakeLock();
4461e6f352c9c1fe8dc346ccf8d3560f7060ec663aSantos Cordon    }
4561e6f352c9c1fe8dc346ccf8d3560f7060ec663aSantos Cordon
4661e6f352c9c1fe8dc346ccf8d3560f7060ec663aSantos Cordon    @Override
4761e6f352c9c1fe8dc346ccf8d3560f7060ec663aSantos Cordon    public void onCallRemoved(Call call) {
48f15dc33f87f88e21ef745952a68af65c86e1bf1eTyler Gunn        if (call.isExternalCall()) {
49f15dc33f87f88e21ef745952a68af65c86e1bf1eTyler Gunn            return;
50f15dc33f87f88e21ef745952a68af65c86e1bf1eTyler Gunn        }
5161e6f352c9c1fe8dc346ccf8d3560f7060ec663aSantos Cordon        handleWakeLock();
5261e6f352c9c1fe8dc346ccf8d3560f7060ec663aSantos Cordon    }
5361e6f352c9c1fe8dc346ccf8d3560f7060ec663aSantos Cordon
5461e6f352c9c1fe8dc346ccf8d3560f7060ec663aSantos Cordon    @Override
5561e6f352c9c1fe8dc346ccf8d3560f7060ec663aSantos Cordon    public void onCallStateChanged(Call call, int oldState, int newState) {
56f15dc33f87f88e21ef745952a68af65c86e1bf1eTyler Gunn        if (call.isExternalCall()) {
57f15dc33f87f88e21ef745952a68af65c86e1bf1eTyler Gunn            return;
58f15dc33f87f88e21ef745952a68af65c86e1bf1eTyler Gunn        }
5961e6f352c9c1fe8dc346ccf8d3560f7060ec663aSantos Cordon        handleWakeLock();
6061e6f352c9c1fe8dc346ccf8d3560f7060ec663aSantos Cordon    }
6161e6f352c9c1fe8dc346ccf8d3560f7060ec663aSantos Cordon
6261e6f352c9c1fe8dc346ccf8d3560f7060ec663aSantos Cordon    private void handleWakeLock() {
6361e6f352c9c1fe8dc346ccf8d3560f7060ec663aSantos Cordon        // We grab a full lock as long as there exists a ringing call.
6461e6f352c9c1fe8dc346ccf8d3560f7060ec663aSantos Cordon        Call ringingCall = mCallsManager.getRingingCall();
6561e6f352c9c1fe8dc346ccf8d3560f7060ec663aSantos Cordon        if (ringingCall != null) {
6663a89f28d6e86844eea242a0175db21df1624db1Brad Ebinger            mTelecomWakeLock.acquire();
6761e6f352c9c1fe8dc346ccf8d3560f7060ec663aSantos Cordon            Log.i(this, "Acquiring full wake lock");
6863a89f28d6e86844eea242a0175db21df1624db1Brad Ebinger        } else {
6963a89f28d6e86844eea242a0175db21df1624db1Brad Ebinger            mTelecomWakeLock.release(0);
7061e6f352c9c1fe8dc346ccf8d3560f7060ec663aSantos Cordon            Log.i(this, "Releasing full wake lock");
7161e6f352c9c1fe8dc346ccf8d3560f7060ec663aSantos Cordon        }
7261e6f352c9c1fe8dc346ccf8d3560f7060ec663aSantos Cordon    }
7361e6f352c9c1fe8dc346ccf8d3560f7060ec663aSantos Cordon}
74