am 3fbfee2f: Merge "SipService: add wake lock for incoming INVITE packets." into gingerbread

Merge commit '3fbfee2febf13bcc46c389ebecbf91465ef211b7' into gingerbread-plus-aosp

* commit '3fbfee2febf13bcc46c389ebecbf91465ef211b7':
  SipService: add wake lock for incoming INVITE packets.
This commit is contained in:
Hung-ying Tyan
2010-10-14 20:52:11 -07:00
committed by Android Git Automerger
3 changed files with 92 additions and 46 deletions

View File

@ -55,7 +55,6 @@ import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Timer;
@ -67,8 +66,8 @@ import javax.sip.SipException;
* @hide
*/
public final class SipService extends ISipService.Stub {
private static final String TAG = "SipService";
private static final boolean DEBUGV = false;
static final String TAG = "SipService";
static final boolean DEBUGV = false;
private static final boolean DEBUG = true;
private static final boolean DEBUG_TIMER = DEBUG && false;
private static final int EXPIRY_TIME = 3600;
@ -95,7 +94,7 @@ public final class SipService extends ISipService.Stub {
private ConnectivityReceiver mConnectivityReceiver;
private boolean mWifiEnabled;
private MyWakeLock mMyWakeLock;
private SipWakeLock mMyWakeLock;
/**
* Starts the SIP service. Do nothing if the SIP API is not supported on the
@ -117,7 +116,7 @@ public final class SipService extends ISipService.Stub {
new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
context.registerReceiver(mWifiStateReceiver,
new IntentFilter(WifiManager.WIFI_STATE_CHANGED_ACTION));
mMyWakeLock = new MyWakeLock((PowerManager)
mMyWakeLock = new SipWakeLock((PowerManager)
context.getSystemService(Context.POWER_SERVICE));
mTimer = new WakeupTimer(context);
@ -459,7 +458,8 @@ public final class SipService extends ISipService.Stub {
private SipSessionGroup createSipSessionGroup(String localIp,
SipProfile localProfile, String password) throws SipException {
try {
return new SipSessionGroup(localIp, localProfile, password);
return new SipSessionGroup(localIp, localProfile, password,
mMyWakeLock);
} catch (IOException e) {
// network disconnected
Log.w(TAG, "createSipSessionGroup(): network disconnected?");
@ -546,6 +546,7 @@ public final class SipService extends ISipService.Stub {
@Override
public void onRinging(ISipSession s, SipProfile caller,
String sessionDescription) {
if (DEBUGV) Log.d(TAG, "<<<<< onRinging()");
SipSessionGroup.SipSessionImpl session =
(SipSessionGroup.SipSessionImpl) s;
synchronized (SipService.this) {
@ -1360,41 +1361,4 @@ public final class SipService extends ISipService.Stub {
}
}
}
private static class MyWakeLock {
private PowerManager mPowerManager;
private PowerManager.WakeLock mWakeLock;
private HashSet<Object> mHolders = new HashSet<Object>();
MyWakeLock(PowerManager powerManager) {
mPowerManager = powerManager;
}
synchronized void reset() {
mHolders.clear();
release(null);
if (DEBUGV) Log.v(TAG, "~~~ hard reset wakelock");
}
synchronized void acquire(Object holder) {
mHolders.add(holder);
if (mWakeLock == null) {
mWakeLock = mPowerManager.newWakeLock(
PowerManager.PARTIAL_WAKE_LOCK, "SipWakeLock");
}
if (!mWakeLock.isHeld()) mWakeLock.acquire();
if (DEBUGV) Log.v(TAG, "acquire wakelock: holder count="
+ mHolders.size());
}
synchronized void release(Object holder) {
mHolders.remove(holder);
if ((mWakeLock != null) && mHolders.isEmpty()
&& mWakeLock.isHeld()) {
mWakeLock.release();
}
if (DEBUGV) Log.v(TAG, "release wakelock: holder count="
+ mHolders.size());
}
}
}

View File

@ -84,6 +84,7 @@ class SipSessionGroup implements SipListener {
private static final String ANONYMOUS = "anonymous";
private static final int EXPIRY_TIME = 3600; // in seconds
private static final int CANCEL_CALL_TIMER = 3; // in seconds
private static final long WAKE_LOCK_HOLDING_TIME = 500; // in milliseconds
private static final EventObject DEREGISTER = new EventObject("Deregister");
private static final EventObject END_CALL = new EventObject("End call");
@ -101,6 +102,8 @@ class SipSessionGroup implements SipListener {
private SipSessionImpl mCallReceiverSession;
private String mLocalIp;
private SipWakeLock mWakeLock;
// call-id-to-SipSession map
private Map<String, SipSessionImpl> mSessionMap =
new HashMap<String, SipSessionImpl>();
@ -110,10 +113,11 @@ class SipSessionGroup implements SipListener {
* @param password the password of the profile
* @throws IOException if cannot assign requested address
*/
public SipSessionGroup(String localIp, SipProfile myself, String password)
throws SipException, IOException {
public SipSessionGroup(String localIp, SipProfile myself, String password,
SipWakeLock wakeLock) throws SipException, IOException {
mLocalProfile = myself;
mPassword = password;
mWakeLock = wakeLock;
reset(localIp);
}
@ -271,7 +275,14 @@ class SipSessionGroup implements SipListener {
}
}
public void processRequest(RequestEvent event) {
public void processRequest(final RequestEvent event) {
if (isRequestEvent(Request.INVITE, event)) {
if (DEBUG) Log.d(TAG, "<<<<< got INVITE, thread:"
+ Thread.currentThread());
// Acquire a wake lock and keep it for WAKE_LOCK_HOLDING_TIME;
// should be large enough to bring up the app.
mWakeLock.acquire(WAKE_LOCK_HOLDING_TIME);
}
process(event);
}

View File

@ -0,0 +1,71 @@
/*
* Copyright (C) 2010, The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.server.sip;
import android.os.PowerManager;
import android.util.Log;
import java.util.HashSet;
class SipWakeLock {
private static final boolean DEBUGV = SipService.DEBUGV;
private static final String TAG = SipService.TAG;
private PowerManager mPowerManager;
private PowerManager.WakeLock mWakeLock;
private PowerManager.WakeLock mTimerWakeLock;
private HashSet<Object> mHolders = new HashSet<Object>();
SipWakeLock(PowerManager powerManager) {
mPowerManager = powerManager;
}
synchronized void reset() {
mHolders.clear();
release(null);
if (DEBUGV) Log.v(TAG, "~~~ hard reset wakelock");
}
synchronized void acquire(long timeout) {
if (mTimerWakeLock == null) {
mTimerWakeLock = mPowerManager.newWakeLock(
PowerManager.PARTIAL_WAKE_LOCK, "SipWakeLock.timer");
mTimerWakeLock.setReferenceCounted(true);
}
mTimerWakeLock.acquire(timeout);
}
synchronized void acquire(Object holder) {
mHolders.add(holder);
if (mWakeLock == null) {
mWakeLock = mPowerManager.newWakeLock(
PowerManager.PARTIAL_WAKE_LOCK, "SipWakeLock");
}
if (!mWakeLock.isHeld()) mWakeLock.acquire();
if (DEBUGV) Log.v(TAG, "acquire wakelock: holder count="
+ mHolders.size());
}
synchronized void release(Object holder) {
mHolders.remove(holder);
if ((mWakeLock != null) && mHolders.isEmpty()
&& mWakeLock.isHeld()) {
mWakeLock.release();
}
if (DEBUGV) Log.v(TAG, "release wakelock: holder count="
+ mHolders.size());
}
}