OSDN Git Service

Make SipService listen to WIFI state change events.
authorHung-ying Tyan <tyanh@google.com>
Wed, 13 Oct 2010 09:05:59 +0000 (17:05 +0800)
committerHung-ying Tyan <tyanh@google.com>
Wed, 13 Oct 2010 09:11:58 +0000 (17:11 +0800)
+ Grab a WIFI lock if any account is opened to receive calls and WIFI is enabled
+ Release the WIFI lock if no account is opened to receive calls or WIFI is
  disabled
+ Remove screen on/off event receiver

http://b/issue?id=3077454

Change-Id: Ifdf60a850bcf4106c75ec1e7563b26d8b33d7e92

voip/java/com/android/server/sip/SipService.java

index 42b4e7c..1a17d38 100644 (file)
@@ -92,7 +92,7 @@ public final class SipService extends ISipService.Stub {
             new HashMap<String, ISipSession>();
 
     private ConnectivityReceiver mConnectivityReceiver;
-    private boolean mScreenOn;
+    private boolean mWifiEnabled;
 
     /**
      * 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();
         context.registerReceiver(mConnectivityReceiver,
                 new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
-        context.registerReceiver(mScreenOnOffReceiver,
-                new IntentFilter(Intent.ACTION_SCREEN_ON));
-        context.registerReceiver(mScreenOnOffReceiver,
-                new IntentFilter(Intent.ACTION_SCREEN_OFF));
+        context.registerReceiver(mWifiStateReceiver,
+                new IntentFilter(WifiManager.WIFI_STATE_CHANGED_ACTION));
 
         mTimer = new WakeupTimer(context);
         mWifiOnly = SipManager.isSipWifiOnly(context);
     }
 
-    BroadcastReceiver mScreenOnOffReceiver = new BroadcastReceiver() {
+    BroadcastReceiver mWifiStateReceiver = new BroadcastReceiver() {
         @Override
         public void onReceive(Context context, Intent intent) {
             String action = intent.getAction();
-            if (Intent.ACTION_SCREEN_OFF.equals(action)) {
-                mScreenOn = false;
-            } else if (Intent.ACTION_SCREEN_ON.equals(action)) {
-                mScreenOn = true;
+            if (WifiManager.WIFI_STATE_CHANGED_ACTION.equals(action)) {
+                int state = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE,
+                        WifiManager.WIFI_STATE_UNKNOWN);
+                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);
             if (localProfile.getAutoRegistration()) {
                 group.openToReceiveCalls();
-                if (isWifiActive()) grabWifiLock();
+                if (mWifiEnabled) grabWifiLock();
             }
         } catch (SipException e) {
             Log.e(TAG, "openToReceiveCalls()", e);
@@ -216,7 +225,7 @@ public final class SipService extends ISipService.Stub {
         group = mSipGroups.remove(localProfileUri);
         notifyProfileRemoved(group.getLocalProfile());
         group.close();
-        if (isWifiActive() && !anyOpened()) releaseWifiLock();
+        if (!anyOpened()) releaseWifiLock();
     }
 
     public synchronized boolean isOpened(String localProfileUri) {
@@ -349,7 +358,7 @@ public final class SipService extends ISipService.Stub {
 
     private void grabWifiLock() {
         if (mWifiLock == null) {
-            if (DEBUG) Log.d(TAG, "acquire wifi lock");
+            if (DEBUG) Log.d(TAG, "~~~~~~~~~~~~~~~~~~~~~ acquire wifi lock");
             mWifiLock = ((WifiManager)
                     mContext.getSystemService(Context.WIFI_SERVICE))
                     .createWifiLock(WifiManager.WIFI_MODE_FULL, TAG);
@@ -359,16 +368,12 @@ public final class SipService extends ISipService.Stub {
 
     private void releaseWifiLock() {
         if (mWifiLock != null) {
-            if (DEBUG) Log.d(TAG, "release wifi lock");
+            if (DEBUG) Log.d(TAG, "~~~~~~~~~~~~~~~~~~~~~ release wifi lock");
             mWifiLock.release();
             mWifiLock = null;
         }
     }
 
-    private boolean isWifiActive() {
-        return "WIFI".equalsIgnoreCase(mNetworkType);
-    }
-
     private synchronized void onConnectivityChanged(
             String type, boolean connected) {
         if (DEBUG) Log.d(TAG, "onConnectivityChanged(): "
@@ -382,14 +387,6 @@ public final class SipService extends ISipService.Stub {
         boolean isWifi = "WIFI".equalsIgnoreCase(type);
         boolean wifiOff = (isWifi && !connected) || (wasWifi && !sameType);
         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 {
             boolean wasConnected = mConnected;
@@ -409,7 +406,6 @@ public final class SipService extends ISipService.Stub {
                     group.onConnectivityChanged(true);
                 }
             }
-
         } catch (SipException e) {
             Log.e(TAG, "onConnectivityChanged()", e);
         }