Home | History | Annotate | Download | only in telecom
      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 
     21 import android.content.Context;
     22 import android.os.PowerManager;
     23 
     24 /**
     25  * Handles acquisition and release of wake locks relating to call state.
     26  */
     27 @VisibleForTesting
     28 public class InCallWakeLockController extends CallsManagerListenerBase {
     29 
     30     private static final String TAG = "InCallWakeLockContoller";
     31 
     32     private final Context mContext;
     33     private final PowerManager.WakeLock mFullWakeLock;
     34     private final CallsManager mCallsManager;
     35 
     36     @VisibleForTesting
     37     public InCallWakeLockController(Context context, CallsManager callsManager) {
     38         mContext = context;
     39         mCallsManager = callsManager;
     40 
     41         PowerManager powerManager = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
     42         mFullWakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, TAG);
     43 
     44         callsManager.addListener(this);
     45     }
     46 
     47     @Override
     48     public void onCallAdded(Call call) {
     49         handleWakeLock();
     50     }
     51 
     52     @Override
     53     public void onCallRemoved(Call call) {
     54         handleWakeLock();
     55     }
     56 
     57     @Override
     58     public void onCallStateChanged(Call call, int oldState, int newState) {
     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             mFullWakeLock.acquire();
     67             Log.i(this, "Acquiring full wake lock");
     68         } else if (mFullWakeLock.isHeld()) {
     69             mFullWakeLock.release();
     70             Log.i(this, "Releasing full wake lock");
     71         }
     72     }
     73 }
     74