Home | History | Annotate | Download | only in sip
      1 /*
      2  * Copyright (C) 2010, 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.sip;
     18 
     19 import android.os.PowerManager;
     20 import android.util.Log;
     21 
     22 import java.util.HashSet;
     23 
     24 class SipWakeLock {
     25     private static final boolean DEBUGV = SipService.DEBUGV;
     26     private static final String TAG = SipService.TAG;
     27     private PowerManager mPowerManager;
     28     private PowerManager.WakeLock mWakeLock;
     29     private PowerManager.WakeLock mTimerWakeLock;
     30     private HashSet<Object> mHolders = new HashSet<Object>();
     31 
     32     SipWakeLock(PowerManager powerManager) {
     33         mPowerManager = powerManager;
     34     }
     35 
     36     synchronized void reset() {
     37         mHolders.clear();
     38         release(null);
     39         if (DEBUGV) Log.v(TAG, "~~~ hard reset wakelock");
     40     }
     41 
     42     synchronized void acquire(long timeout) {
     43         if (mTimerWakeLock == null) {
     44             mTimerWakeLock = mPowerManager.newWakeLock(
     45                     PowerManager.PARTIAL_WAKE_LOCK, "SipWakeLock.timer");
     46             mTimerWakeLock.setReferenceCounted(true);
     47         }
     48         mTimerWakeLock.acquire(timeout);
     49     }
     50 
     51     synchronized void acquire(Object holder) {
     52         mHolders.add(holder);
     53         if (mWakeLock == null) {
     54             mWakeLock = mPowerManager.newWakeLock(
     55                     PowerManager.PARTIAL_WAKE_LOCK, "SipWakeLock");
     56         }
     57         if (!mWakeLock.isHeld()) mWakeLock.acquire();
     58         if (DEBUGV) Log.v(TAG, "acquire wakelock: holder count="
     59                 + mHolders.size());
     60     }
     61 
     62     synchronized void release(Object holder) {
     63         mHolders.remove(holder);
     64         if ((mWakeLock != null) && mHolders.isEmpty()
     65                 && mWakeLock.isHeld()) {
     66             mWakeLock.release();
     67         }
     68         if (DEBUGV) Log.v(TAG, "release wakelock: holder count="
     69                 + mHolders.size());
     70     }
     71 }
     72