am 14b6d062: Merge changes Ib70e0cf2,I0691cd70 into gingerbread

* commit '14b6d0620b42d1bb3a55778ba452d838a0d89223':
  SipService: registers broadcast receivers on demand.
  SipService: release wake lock for cancelled tasks.
This commit is contained in:
Hung-ying Tyan
2011-01-24 19:44:56 -08:00
committed by Android Git Automerger

View File

@@ -105,7 +105,7 @@ public final class SipService extends ISipService.Stub {
if (SipManager.isApiSupported(context)) { if (SipManager.isApiSupported(context)) {
ServiceManager.addService("sip", new SipService(context)); ServiceManager.addService("sip", new SipService(context));
context.sendBroadcast(new Intent(SipManager.ACTION_SIP_SERVICE_UP)); context.sendBroadcast(new Intent(SipManager.ACTION_SIP_SERVICE_UP));
if (DEBUG) Log.i(TAG, "SIP service started"); if (DEBUG) Log.d(TAG, "SIP service started");
} }
} }
@@ -113,10 +113,6 @@ public final class SipService extends ISipService.Stub {
if (DEBUG) Log.d(TAG, " service started!"); if (DEBUG) Log.d(TAG, " service started!");
mContext = context; mContext = context;
mConnectivityReceiver = new ConnectivityReceiver(); mConnectivityReceiver = new ConnectivityReceiver();
context.registerReceiver(mConnectivityReceiver,
new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
context.registerReceiver(mWifiStateReceiver,
new IntentFilter(WifiManager.WIFI_STATE_CHANGED_ACTION));
mMyWakeLock = new SipWakeLock((PowerManager) mMyWakeLock = new SipWakeLock((PowerManager)
context.getSystemService(Context.POWER_SERVICE)); context.getSystemService(Context.POWER_SERVICE));
@@ -124,7 +120,7 @@ public final class SipService extends ISipService.Stub {
mWifiOnly = SipManager.isSipWifiOnly(context); mWifiOnly = SipManager.isSipWifiOnly(context);
} }
BroadcastReceiver mWifiStateReceiver = new BroadcastReceiver() { private 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();
@@ -147,6 +143,20 @@ public final class SipService extends ISipService.Stub {
} }
}; };
private void registerReceivers() {
mContext.registerReceiver(mConnectivityReceiver,
new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
mContext.registerReceiver(mWifiStateReceiver,
new IntentFilter(WifiManager.WIFI_STATE_CHANGED_ACTION));
if (DEBUG) Log.d(TAG, " +++ register receivers");
}
private void unregisterReceivers() {
mContext.unregisterReceiver(mConnectivityReceiver);
mContext.unregisterReceiver(mWifiStateReceiver);
if (DEBUG) Log.d(TAG, " --- unregister receivers");
}
private MyExecutor getExecutor() { private MyExecutor getExecutor() {
// create mExecutor lazily // create mExecutor lazily
if (mExecutor == null) mExecutor = new MyExecutor(); if (mExecutor == null) mExecutor = new MyExecutor();
@@ -166,12 +176,14 @@ public final class SipService extends ISipService.Stub {
return profiles.toArray(new SipProfile[profiles.size()]); return profiles.toArray(new SipProfile[profiles.size()]);
} }
public void open(SipProfile localProfile) { public synchronized void open(SipProfile localProfile) {
mContext.enforceCallingOrSelfPermission( mContext.enforceCallingOrSelfPermission(
android.Manifest.permission.USE_SIP, null); android.Manifest.permission.USE_SIP, null);
localProfile.setCallingUid(Binder.getCallingUid()); localProfile.setCallingUid(Binder.getCallingUid());
try { try {
boolean addingFirstProfile = mSipGroups.isEmpty();
createGroup(localProfile); createGroup(localProfile);
if (addingFirstProfile && !mSipGroups.isEmpty()) registerReceivers();
} catch (SipException e) { } catch (SipException e) {
Log.e(TAG, "openToMakeCalls()", e); Log.e(TAG, "openToMakeCalls()", e);
// TODO: how to send the exception back // TODO: how to send the exception back
@@ -192,8 +204,10 @@ public final class SipService extends ISipService.Stub {
if (DEBUG) Log.d(TAG, "open3: " + localProfile.getUriString() + ": " if (DEBUG) Log.d(TAG, "open3: " + localProfile.getUriString() + ": "
+ incomingCallPendingIntent + ": " + listener); + incomingCallPendingIntent + ": " + listener);
try { try {
boolean addingFirstProfile = mSipGroups.isEmpty();
SipSessionGroupExt group = createGroup(localProfile, SipSessionGroupExt group = createGroup(localProfile,
incomingCallPendingIntent, listener); incomingCallPendingIntent, listener);
if (addingFirstProfile && !mSipGroups.isEmpty()) registerReceivers();
if (localProfile.getAutoRegistration()) { if (localProfile.getAutoRegistration()) {
group.openToReceiveCalls(); group.openToReceiveCalls();
if (mWifiEnabled) grabWifiLock(); if (mWifiEnabled) grabWifiLock();
@@ -235,6 +249,7 @@ public final class SipService extends ISipService.Stub {
releaseWifiLock(); releaseWifiLock();
mMyWakeLock.reset(); // in case there's leak mMyWakeLock.reset(); // in case there's leak
} }
if (mSipGroups.isEmpty()) unregisterReceivers();
} }
public synchronized boolean isOpened(String localProfileUri) { public synchronized boolean isOpened(String localProfileUri) {
@@ -1055,7 +1070,10 @@ public final class SipService extends ISipService.Stub {
// we want to skip the interim ones) but deliver bad news // we want to skip the interim ones) but deliver bad news
// immediately // immediately
if (connected) { if (connected) {
if (mTask != null) mTask.cancel(); if (mTask != null) {
mTask.cancel();
mMyWakeLock.release(mTask);
}
mTask = new MyTimerTask(type, connected); mTask = new MyTimerTask(type, connected);
mTimer.schedule(mTask, 2 * 1000L); mTimer.schedule(mTask, 2 * 1000L);
// hold wakup lock so that we can finish changes before the // hold wakup lock so that we can finish changes before the
@@ -1096,6 +1114,7 @@ public final class SipService extends ISipService.Stub {
if (mTask != this) { if (mTask != this) {
Log.w(TAG, " unexpected task: " + mNetworkType Log.w(TAG, " unexpected task: " + mNetworkType
+ (mConnected ? " CONNECTED" : "DISCONNECTED")); + (mConnected ? " CONNECTED" : "DISCONNECTED"));
mMyWakeLock.release(this);
return; return;
} }
mTask = null; mTask = null;