OSDN Git Service

Initiate wake up scan only in a disconnected state
authorIrfan Sheriff <isheriff@google.com>
Wed, 3 Nov 2010 23:13:32 +0000 (16:13 -0700)
committerIrfan Sheriff <isheriff@google.com>
Thu, 4 Nov 2010 22:20:01 +0000 (15:20 -0700)
When wifi is connected to an access point, we should
not wake up the device to initiate scans.

Change-Id: I1a48387a0cac7b23a8d947d8409cafc63da18d38

services/java/com/android/server/WifiService.java
wifi/java/android/net/wifi/WifiStateMachine.java

index 9687aa7..853e46d 100644 (file)
@@ -89,10 +89,8 @@ public class WifiService extends IWifiManager.Stub {
 
     private AlarmManager mAlarmManager;
     private PendingIntent mIdleIntent;
-    private PendingIntent mScanIntent;
     private BluetoothA2dp mBluetoothA2dp;
     private static final int IDLE_REQUEST = 0;
-    private static final int SCAN_REQUEST = 0;
     private boolean mScreenOff;
     private boolean mDeviceIdle;
     private int mPluggedType;
@@ -128,18 +126,9 @@ public class WifiService extends IWifiManager.Stub {
      */
     private static final long DEFAULT_IDLE_MS = 15 * 60 * 1000; /* 15 minutes */
 
-    /**
-     * See {@link Settings.Secure#WIFI_SCAN_INTERVAL_MS}. This is the default value if a
-     * Settings.Secure value is not present.
-     */
-    private static final long DEFAULT_SCAN_INTERVAL_MS = 60 * 1000; /* 1 minute */
-
     private static final String ACTION_DEVICE_IDLE =
             "com.android.server.WifiManager.action.DEVICE_IDLE";
 
-    private static final String ACTION_START_SCAN =
-        "com.android.server.WifiManager.action.START_SCAN";
-
     private boolean mIsReceiverRegistered = false;
 
 
@@ -249,9 +238,6 @@ public class WifiService extends IWifiManager.Stub {
         Intent idleIntent = new Intent(ACTION_DEVICE_IDLE, null);
         mIdleIntent = PendingIntent.getBroadcast(mContext, IDLE_REQUEST, idleIntent, 0);
 
-        Intent scanIntent = new Intent(ACTION_START_SCAN, null);
-        mScanIntent = PendingIntent.getBroadcast(mContext, SCAN_REQUEST, scanIntent, 0);
-
         HandlerThread wifiThread = new HandlerThread("WifiService");
         wifiThread.start();
         mHandler = new WifiServiceHandler(wifiThread.getLooper(), context);
@@ -936,8 +922,6 @@ public class WifiService extends IWifiManager.Stub {
                 int state = intent.getIntExtra(BluetoothProfile.EXTRA_STATE,
                                                BluetoothA2dp.STATE_NOT_PLAYING);
                 mWifiStateMachine.setBluetoothScanMode(state == BluetoothA2dp.STATE_PLAYING);
-            } else if (action.equals(ACTION_START_SCAN)) {
-                mWifiStateMachine.startScan(true);
             }
         }
 
@@ -1010,10 +994,6 @@ public class WifiService extends IWifiManager.Stub {
             strongestLockMode = WifiManager.WIFI_MODE_FULL;
         }
 
-        /* Scan interval when driver is started */
-        long scanMs = Settings.Secure.getLong(mContext.getContentResolver(),
-                Settings.Secure.WIFI_SCAN_INTERVAL_MS, DEFAULT_SCAN_INTERVAL_MS);
-
         /* Disable tethering when airplane mode is enabled */
         if (airplaneMode) {
             mWifiStateMachine.setWifiApEnabled(null, false);
@@ -1026,14 +1006,11 @@ public class WifiService extends IWifiManager.Stub {
                 mWifiStateMachine.setScanOnlyMode(
                         strongestLockMode == WifiManager.WIFI_MODE_SCAN_ONLY);
                 mWifiStateMachine.setDriverStart(true);
-                mAlarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
-                        System.currentTimeMillis() + scanMs, scanMs, mScanIntent);
                 mWifiStateMachine.setHighPerfModeEnabled(strongestLockMode
                         == WifiManager.WIFI_MODE_FULL_HIGH_PERF);
             } else {
                 mWifiStateMachine.requestCmWakeLock();
                 mWifiStateMachine.setDriverStart(false);
-                mAlarmManager.cancel(mScanIntent);
             }
         } else {
             mWifiStateMachine.setWifiEnabled(false);
@@ -1046,7 +1023,6 @@ public class WifiService extends IWifiManager.Stub {
         intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
         intentFilter.addAction(Intent.ACTION_BATTERY_CHANGED);
         intentFilter.addAction(ACTION_DEVICE_IDLE);
-        intentFilter.addAction(ACTION_START_SCAN);
         intentFilter.addAction(BluetoothA2dp.ACTION_PLAYING_STATE_CHANGED);
         mContext.registerReceiver(mReceiver, intentFilter);
     }
index fdb50e2..93c38cd 100644 (file)
@@ -38,6 +38,8 @@ import static android.net.wifi.WifiManager.WIFI_AP_STATE_ENABLING;
 import static android.net.wifi.WifiManager.WIFI_AP_STATE_FAILED;
 
 import android.app.ActivityManagerNative;
+import android.app.AlarmManager;
+import android.app.PendingIntent;
 import android.net.LinkAddress;
 import android.net.NetworkInfo;
 import android.net.DhcpInfo;
@@ -67,8 +69,11 @@ import android.bluetooth.BluetoothAdapter;
 import android.bluetooth.BluetoothDevice;
 import android.bluetooth.BluetoothHeadset;
 import android.bluetooth.BluetoothProfile;
+import android.content.BroadcastReceiver;
 import android.content.Intent;
 import android.content.Context;
+import android.content.IntentFilter;
+
 import com.android.internal.app.IBatteryStats;
 import com.android.internal.util.AsyncChannel;
 import com.android.internal.util.HierarchicalState;
@@ -147,6 +152,9 @@ public class WifiStateMachine extends HierarchicalStateMachine {
      * and load configuration afterwards */
     private boolean mWpsStarted = false;
 
+    private AlarmManager mAlarmManager;
+    private PendingIntent mScanIntent;
+
     // Channel for sending replies.
     private AsyncChannel mReplyChannel = new AsyncChannel();
 
@@ -344,6 +352,12 @@ public class WifiStateMachine extends HierarchicalStateMachine {
     private static final int POWER_MODE_ACTIVE = 1;
     private static final int POWER_MODE_AUTO = 0;
 
+    /**
+     * See {@link Settings.Secure#WIFI_SCAN_INTERVAL_MS}. This is the default value if a
+     * Settings.Secure value is not present.
+     */
+    private static final long DEFAULT_SCAN_INTERVAL_MS = 60 * 1000; /* 1 minute */
+
     /* Default parent state */
     private HierarchicalState mDefaultState = new DefaultState();
     /* Temporary initial state */
@@ -411,6 +425,10 @@ public class WifiStateMachine extends HierarchicalStateMachine {
     private final AtomicInteger mLastEnableUid = new AtomicInteger(Process.myUid());
     private final AtomicInteger mLastApEnableUid = new AtomicInteger(Process.myUid());
 
+    private static final int SCAN_REQUEST = 0;
+    private static final String ACTION_START_SCAN =
+        "com.android.server.WifiManager.action.START_SCAN";
+
     /**
      * Keep track of whether WIFI is running.
      */
@@ -465,6 +483,19 @@ public class WifiStateMachine extends HierarchicalStateMachine {
         mLastNetworkId = -1;
         mLastSignalLevel = -1;
 
+        mAlarmManager = (AlarmManager)mContext.getSystemService(Context.ALARM_SERVICE);
+        Intent scanIntent = new Intent(ACTION_START_SCAN, null);
+        mScanIntent = PendingIntent.getBroadcast(mContext, SCAN_REQUEST, scanIntent, 0);
+
+        mContext.registerReceiver(
+                new BroadcastReceiver() {
+                    @Override
+                    public void onReceive(Context context, Intent intent) {
+                        startScan(false);
+                    }
+                },
+                new IntentFilter(ACTION_START_SCAN));
+
         mScanResultCache = new LinkedHashMap<String, ScanResult>(
             SCAN_RESULT_CACHE_SIZE, 0.75f, true) {
                 /*
@@ -1130,7 +1161,7 @@ public class WifiStateMachine extends HierarchicalStateMachine {
                     if (scanResult != null) {
                         scanList.add(scanResult);
                     } else {
-                        Log.w(TAG, "misformatted scan result for: " + line);
+                        //TODO: hidden network handling
                     }
                 }
                 lineBeg = lineEnd + 1;
@@ -2057,7 +2088,7 @@ public class WifiStateMachine extends HierarchicalStateMachine {
             } else {
                 WifiNative.setScanResultHandlingCommand(CONNECT_MODE);
                 WifiNative.reconnectCommand();
-                transitionTo(mConnectModeState);
+                transitionTo(mDisconnectedState);
             }
         }
         @Override
@@ -2631,6 +2662,17 @@ public class WifiStateMachine extends HierarchicalStateMachine {
         public void enter() {
             if (DBG) Log.d(TAG, getName() + "\n");
             EventLog.writeEvent(EVENTLOG_WIFI_STATE_CHANGED, getName());
+
+            /**
+             * In a disconnected state, an infrequent scan that wakes
+             * up the device is needed to ensure a user connects to
+             * an access point on the move
+             */
+            long scanMs = Settings.Secure.getLong(mContext.getContentResolver(),
+                    Settings.Secure.WIFI_SCAN_INTERVAL_MS, DEFAULT_SCAN_INTERVAL_MS);
+
+            mAlarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
+                    System.currentTimeMillis() + scanMs, scanMs, mScanIntent);
         }
         @Override
         public boolean processMessage(Message message) {
@@ -2654,6 +2696,11 @@ public class WifiStateMachine extends HierarchicalStateMachine {
             EventLog.writeEvent(EVENTLOG_WIFI_EVENT_HANDLED, message.what);
             return HANDLED;
         }
+
+        @Override
+        public void exit() {
+            mAlarmManager.cancel(mScanIntent);
+        }
     }
 
     class SoftApStartedState extends HierarchicalState {