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 DEBUG = false;
     26     private static final String TAG = "SipWakeLock";
     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         if (DEBUG) Log.v(TAG, "reset count=" + mHolders.size());
     38         mHolders.clear();
     39         release(null);
     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 (DEBUG) Log.v(TAG, "acquire count=" + mHolders.size());
     59     }
     60 
     61     synchronized void release(Object holder) {
     62         mHolders.remove(holder);
     63         if ((mWakeLock != null) && mHolders.isEmpty()
     64                 && mWakeLock.isHeld()) {
     65             mWakeLock.release();
     66         }
     67         if (DEBUG) Log.v(TAG, "release count=" + mHolders.size());
     68     }
     69 }
     70