am 3bb10442: Merge "Make SipService listen to WIFI state change events." into gingerbread

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

* commit '3bb1044243ad38da35ccddc3e0d6d92d64fefe3e':
  Make SipService listen to WIFI state change events.
This commit is contained in:
Hung-ying Tyan
2010-10-13 22:54:20 -07:00
committed by Android Git Automerger

View File

@ -92,7 +92,7 @@ public final class SipService extends ISipService.Stub {
new HashMap<String, ISipSession>(); new HashMap<String, ISipSession>();
private ConnectivityReceiver mConnectivityReceiver; private ConnectivityReceiver mConnectivityReceiver;
private boolean mScreenOn; private boolean mWifiEnabled;
/** /**
* Starts the SIP service. Do nothing if the SIP API is not supported on the * Starts the SIP service. Do nothing if the SIP API is not supported on the
@ -112,23 +112,32 @@ public final class SipService extends ISipService.Stub {
mConnectivityReceiver = new ConnectivityReceiver(); mConnectivityReceiver = new ConnectivityReceiver();
context.registerReceiver(mConnectivityReceiver, context.registerReceiver(mConnectivityReceiver,
new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION)); new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
context.registerReceiver(mScreenOnOffReceiver, context.registerReceiver(mWifiStateReceiver,
new IntentFilter(Intent.ACTION_SCREEN_ON)); new IntentFilter(WifiManager.WIFI_STATE_CHANGED_ACTION));
context.registerReceiver(mScreenOnOffReceiver,
new IntentFilter(Intent.ACTION_SCREEN_OFF));
mTimer = new WakeupTimer(context); mTimer = new WakeupTimer(context);
mWifiOnly = SipManager.isSipWifiOnly(context); mWifiOnly = SipManager.isSipWifiOnly(context);
} }
BroadcastReceiver mScreenOnOffReceiver = new BroadcastReceiver() { BroadcastReceiver mWifiStateReceiver = new BroadcastReceiver() {
@Override @Override
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {
String action = intent.getAction(); String action = intent.getAction();
if (Intent.ACTION_SCREEN_OFF.equals(action)) { if (WifiManager.WIFI_STATE_CHANGED_ACTION.equals(action)) {
mScreenOn = false; int state = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE,
} else if (Intent.ACTION_SCREEN_ON.equals(action)) { WifiManager.WIFI_STATE_UNKNOWN);
mScreenOn = true; synchronized (SipService.this) {
switch (state) {
case WifiManager.WIFI_STATE_ENABLED:
mWifiEnabled = true;
if (anyOpened()) grabWifiLock();
break;
case WifiManager.WIFI_STATE_DISABLED:
mWifiEnabled = false;
releaseWifiLock();
break;
}
}
} }
} }
}; };
@ -182,7 +191,7 @@ public final class SipService extends ISipService.Stub {
incomingCallPendingIntent, listener); incomingCallPendingIntent, listener);
if (localProfile.getAutoRegistration()) { if (localProfile.getAutoRegistration()) {
group.openToReceiveCalls(); group.openToReceiveCalls();
if (isWifiActive()) grabWifiLock(); if (mWifiEnabled) grabWifiLock();
} }
} catch (SipException e) { } catch (SipException e) {
Log.e(TAG, "openToReceiveCalls()", e); Log.e(TAG, "openToReceiveCalls()", e);
@ -216,7 +225,7 @@ public final class SipService extends ISipService.Stub {
group = mSipGroups.remove(localProfileUri); group = mSipGroups.remove(localProfileUri);
notifyProfileRemoved(group.getLocalProfile()); notifyProfileRemoved(group.getLocalProfile());
group.close(); group.close();
if (isWifiActive() && !anyOpened()) releaseWifiLock(); if (!anyOpened()) releaseWifiLock();
} }
public synchronized boolean isOpened(String localProfileUri) { public synchronized boolean isOpened(String localProfileUri) {
@ -349,7 +358,7 @@ public final class SipService extends ISipService.Stub {
private void grabWifiLock() { private void grabWifiLock() {
if (mWifiLock == null) { if (mWifiLock == null) {
if (DEBUG) Log.d(TAG, "acquire wifi lock"); if (DEBUG) Log.d(TAG, "~~~~~~~~~~~~~~~~~~~~~ acquire wifi lock");
mWifiLock = ((WifiManager) mWifiLock = ((WifiManager)
mContext.getSystemService(Context.WIFI_SERVICE)) mContext.getSystemService(Context.WIFI_SERVICE))
.createWifiLock(WifiManager.WIFI_MODE_FULL, TAG); .createWifiLock(WifiManager.WIFI_MODE_FULL, TAG);
@ -359,16 +368,12 @@ public final class SipService extends ISipService.Stub {
private void releaseWifiLock() { private void releaseWifiLock() {
if (mWifiLock != null) { if (mWifiLock != null) {
if (DEBUG) Log.d(TAG, "release wifi lock"); if (DEBUG) Log.d(TAG, "~~~~~~~~~~~~~~~~~~~~~ release wifi lock");
mWifiLock.release(); mWifiLock.release();
mWifiLock = null; mWifiLock = null;
} }
} }
private boolean isWifiActive() {
return "WIFI".equalsIgnoreCase(mNetworkType);
}
private synchronized void onConnectivityChanged( private synchronized void onConnectivityChanged(
String type, boolean connected) { String type, boolean connected) {
if (DEBUG) Log.d(TAG, "onConnectivityChanged(): " if (DEBUG) Log.d(TAG, "onConnectivityChanged(): "
@ -382,14 +387,6 @@ public final class SipService extends ISipService.Stub {
boolean isWifi = "WIFI".equalsIgnoreCase(type); boolean isWifi = "WIFI".equalsIgnoreCase(type);
boolean wifiOff = (isWifi && !connected) || (wasWifi && !sameType); boolean wifiOff = (isWifi && !connected) || (wasWifi && !sameType);
boolean wifiOn = isWifi && connected; boolean wifiOn = isWifi && connected;
if (wifiOff) {
if (mScreenOn) releaseWifiLock();
// If the screen is off, we still keep the wifi lock in order
// to be able to reassociate with any available AP. Otherwise,
// the wifi driver could be stopped after 15 mins of idle time.
} else if (wifiOn) {
if (anyOpened()) grabWifiLock();
}
try { try {
boolean wasConnected = mConnected; boolean wasConnected = mConnected;
@ -409,7 +406,6 @@ public final class SipService extends ISipService.Stub {
group.onConnectivityChanged(true); group.onConnectivityChanged(true);
} }
} }
} catch (SipException e) { } catch (SipException e) {
Log.e(TAG, "onConnectivityChanged()", e); Log.e(TAG, "onConnectivityChanged()", e);
} }