InCallWakeLockController.java revision d35be75e088fd4f9073678d89186c959f79001f3
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 android.content.Context;
20import android.os.PowerManager;
21import android.telecom.CallState;
22
23/**
24 * Handles acquisition and release of wake locks relating to call state.
25 */
26public class InCallWakeLockController extends CallsManagerListenerBase {
27
28    private static final String TAG = "InCallWakeLockContoller";
29
30    private final Context mContext;
31    private final PowerManager.WakeLock mFullWakeLock;
32    private final CallsManager mCallsManager;
33
34    public InCallWakeLockController(Context context, CallsManager callsManager) {
35        mContext = context;
36        mCallsManager = callsManager;
37
38        PowerManager powerManager = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
39        mFullWakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, TAG);
40
41        callsManager.addListener(this);
42    }
43
44    @Override
45    public void onCallAdded(Call call) {
46        handleWakeLock();
47    }
48
49    @Override
50    public void onCallRemoved(Call call) {
51        handleWakeLock();
52    }
53
54    @Override
55    public void onCallStateChanged(Call call, int oldState, int newState) {
56        handleWakeLock();
57    }
58
59    private void handleWakeLock() {
60        // We grab a full lock as long as there exists a ringing call.
61        Call ringingCall = mCallsManager.getRingingCall();
62        if (ringingCall != null) {
63            mFullWakeLock.acquire();
64            Log.i(this, "Acquiring full wake lock");
65        } else if (mFullWakeLock.isHeld()) {
66            mFullWakeLock.release();
67            Log.i(this, "Releasing full wake lock");
68        }
69    }
70}
71