diff options
author | Jason Monk <jmonk@google.com> | 2015-01-28 10:40:41 -0500 |
---|---|---|
committer | Jason Monk <jmonk@google.com> | 2015-02-04 15:08:55 -0500 |
commit | d52356aa5e82c7c5db61672bbe8d0f44861f3e59 (patch) | |
tree | b2ccbf3e88c9bacba4921e81b037514156f64c4c /packages/SystemUI | |
parent | 1a81b83e1ef3ccf13cf32bb621537a6bda5b33f7 (diff) | |
download | frameworks_base-d52356aa5e82c7c5db61672bbe8d0f44861f3e59.zip frameworks_base-d52356aa5e82c7c5db61672bbe8d0f44861f3e59.tar.gz frameworks_base-d52356aa5e82c7c5db61672bbe8d0f44861f3e59.tar.bz2 |
Move Settings wifi tracking to SettingsLib
Make SettingsLib capable of tracking which wifi networks are
available/saved and their state.
Also modify Quick Settings to use this code rather than having
its own logic.
Bug: 19180466
Change-Id: Iff9f9aed240d79323dba41496496e8076b9fa6f2
Diffstat (limited to 'packages/SystemUI')
5 files changed, 59 insertions, 179 deletions
diff --git a/packages/SystemUI/res/values/strings.xml b/packages/SystemUI/res/values/strings.xml index 40bf13f..6afca8a 100644 --- a/packages/SystemUI/res/values/strings.xml +++ b/packages/SystemUI/res/values/strings.xml @@ -630,10 +630,6 @@ <string name="quick_settings_done">Done</string> <!-- QuickSettings: Control panel: Label for connected device. [CHAR LIMIT=NONE] --> <string name="quick_settings_connected">Connected</string> - <!-- QuickSettings: Control panel: Label for a connected Wi-Fi access point when the connection is established by a Wi-Fi assistant application. [CHAR LIMIT=NONE] --> - <string name="quick_settings_connected_via_wfa">Connected via Wi\u2011Fi assistant</string> - <!-- QuickSettings: Control panel: Label for saved device or connection. [CHAR LIMIT=NONE] --> - <string name="quick_settings_saved">Saved</string> <!-- QuickSettings: Control panel: Label for connecting device. [CHAR LIMIT=NONE] --> <string name="quick_settings_connecting">Connecting...</string> <!-- QuickSettings: Tethering. [CHAR LIMIT=NONE] --> diff --git a/packages/SystemUI/src/com/android/systemui/qs/tiles/WifiTile.java b/packages/SystemUI/src/com/android/systemui/qs/tiles/WifiTile.java index bd4dbe6..70746c7 100644 --- a/packages/SystemUI/src/com/android/systemui/qs/tiles/WifiTile.java +++ b/packages/SystemUI/src/com/android/systemui/qs/tiles/WifiTile.java @@ -16,6 +16,8 @@ package com.android.systemui.qs.tiles; +import java.util.List; + import android.content.Context; import android.content.Intent; import android.content.res.Resources; @@ -24,16 +26,15 @@ import android.util.Log; import android.view.View; import android.view.ViewGroup; +import com.android.settingslib.wifi.AccessPoint; import com.android.systemui.R; import com.android.systemui.qs.QSDetailItems; import com.android.systemui.qs.QSDetailItems.Item; import com.android.systemui.qs.QSTile; import com.android.systemui.qs.QSTileView; import com.android.systemui.qs.SignalTileView; -import com.android.systemui.statusbar.phone.QSTileHost; import com.android.systemui.statusbar.policy.NetworkController; import com.android.systemui.statusbar.policy.NetworkController.AccessPointController; -import com.android.systemui.statusbar.policy.NetworkController.AccessPointController.AccessPoint; import com.android.systemui.statusbar.policy.NetworkController.NetworkSignalChangedCallback; /** Quick settings tile: Wifi **/ @@ -282,10 +283,10 @@ public class WifiTile extends QSTile<QSTile.SignalState> { } @Override - public void onAccessPointsChanged(final AccessPoint[] accessPoints) { - mAccessPoints = accessPoints; + public void onAccessPointsChanged(final List<AccessPoint> accessPoints) { + mAccessPoints = accessPoints.toArray(new AccessPoint[accessPoints.size()]); updateItems(); - if (accessPoints != null && accessPoints.length > 0) { + if (accessPoints != null && accessPoints.size() > 0) { fireScanStateChanged(false); } } @@ -299,7 +300,7 @@ public class WifiTile extends QSTile<QSTile.SignalState> { public void onDetailItemClick(Item item) { if (item == null || item.tag == null) return; final AccessPoint ap = (AccessPoint) item.tag; - if (!ap.isConnected) { + if (!ap.isActive()) { if (mWifiController.connect(ap)) { mHost.collapsePanels(); } @@ -326,16 +327,10 @@ public class WifiTile extends QSTile<QSTile.SignalState> { final AccessPoint ap = mAccessPoints[i]; final Item item = new Item(); item.tag = ap; - item.icon = ap.iconId; - item.line1 = ap.ssid; - if (ap.isConnected) { - item.line2 = mContext.getString(ap.isConfigured ? - R.string.quick_settings_connected : - R.string.quick_settings_connected_via_wfa); - } else if (ap.networkId >= 0) { - item.line2 = mContext.getString(R.string.quick_settings_saved); - } - item.overlay = ap.hasSecurity + item.icon = mWifiController.getIcon(ap); + item.line1 = ap.getSsid(); + item.line2 = ap.getSummary(); + item.overlay = ap.getSecurity() != AccessPoint.SECURITY_NONE ? mContext.getDrawable(R.drawable.qs_ic_wifi_lock) : null; items[i] = item; diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/policy/AccessPointControllerImpl.java b/packages/SystemUI/src/com/android/systemui/statusbar/policy/AccessPointControllerImpl.java index ad4c211..18983ff 100644 --- a/packages/SystemUI/src/com/android/systemui/statusbar/policy/AccessPointControllerImpl.java +++ b/packages/SystemUI/src/com/android/systemui/statusbar/policy/AccessPointControllerImpl.java @@ -17,35 +17,25 @@ package com.android.systemui.statusbar.policy; import android.app.ActivityManager; -import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; -import android.content.IntentFilter; -import android.net.wifi.ScanResult; -import android.net.wifi.WifiConfiguration; -import android.net.wifi.WifiConfiguration.KeyMgmt; -import android.net.wifi.WifiInfo; -import android.net.wifi.WifiManager; import android.net.wifi.WifiManager.ActionListener; import android.os.UserHandle; import android.os.UserManager; import android.provider.Settings; -import android.text.TextUtils; -import android.util.ArrayMap; -import android.util.ArraySet; import android.util.Log; +import com.android.settingslib.wifi.AccessPoint; +import com.android.settingslib.wifi.WifiTracker; +import com.android.settingslib.wifi.WifiTracker.WifiListener; import com.android.systemui.R; +import java.io.PrintWriter; import java.util.ArrayList; -import java.util.Collections; -import java.util.Comparator; import java.util.List; - -// TODO: Unify this logic with platform settings (see WifiSettings and AccessPoint). There is a -// fair amount of complexity here in statuses and logic beyond just connected/disconnected. -public class AccessPointControllerImpl implements NetworkController.AccessPointController { +public class AccessPointControllerImpl + implements NetworkController.AccessPointController, WifiListener { private static final String TAG = "AccessPointController"; private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG); @@ -63,25 +53,18 @@ public class AccessPointControllerImpl implements NetworkController.AccessPointC private final Context mContext; private final ArrayList<AccessPointCallback> mCallbacks = new ArrayList<AccessPointCallback>(); - private final WifiManager mWifiManager; + private final WifiTracker mWifiTracker; private final UserManager mUserManager; - private final Receiver mReceiver = new Receiver(); - private NetworkControllerImpl mNetworkController; - private boolean mScanning; private int mCurrentUser; public AccessPointControllerImpl(Context context) { mContext = context; - mWifiManager = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE); mUserManager = (UserManager) mContext.getSystemService(Context.USER_SERVICE); + mWifiTracker = new WifiTracker(context, this, false, true); mCurrentUser = ActivityManager.getCurrentUser(); } - void setNetworkController(NetworkControllerImpl networkController) { - mNetworkController = networkController; - } - public boolean canConfigWifi() { return !mUserManager.hasUserRestriction(UserManager.DISALLOW_CONFIG_WIFI, new UserHandle(mCurrentUser)); @@ -96,7 +79,9 @@ public class AccessPointControllerImpl implements NetworkController.AccessPointC if (callback == null || mCallbacks.contains(callback)) return; if (DEBUG) Log.d(TAG, "addCallback " + callback); mCallbacks.add(callback); - mReceiver.setListening(!mCallbacks.isEmpty()); + if (mCallbacks.size() == 1) { + mWifiTracker.startTracking(); + } } @Override @@ -104,37 +89,40 @@ public class AccessPointControllerImpl implements NetworkController.AccessPointC if (callback == null) return; if (DEBUG) Log.d(TAG, "removeCallback " + callback); mCallbacks.remove(callback); - mReceiver.setListening(!mCallbacks.isEmpty()); + if (mCallbacks.isEmpty()) { + mWifiTracker.stopTracking(); + } } @Override public void scanForAccessPoints() { - if (mScanning) return; if (DEBUG) Log.d(TAG, "scan!"); - mScanning = mWifiManager.startScan(); - // Grab current networks immediately while we wait for scan. - updateAccessPoints(); + mWifiTracker.forceScan(); + } + + @Override + public int getIcon(AccessPoint ap) { + int level = ap.getLevel(); + return ICONS[level >= 0 ? level : 0]; } public boolean connect(AccessPoint ap) { if (ap == null) return false; - if (DEBUG) Log.d(TAG, "connect networkId=" + ap.networkId); - if (ap.networkId < 0) { + if (DEBUG) Log.d(TAG, "connect networkId=" + ap.getConfig().networkId); + if (ap.isSaved()) { + mWifiTracker.getManager().connect(ap.getConfig().networkId, mConnectListener); + } else { // Unknown network, need to add it. - if (ap.hasSecurity) { + if (ap.getSecurity() != AccessPoint.SECURITY_NONE) { Intent intent = new Intent(Settings.ACTION_WIFI_SETTINGS); - intent.putExtra(EXTRA_START_CONNECT_SSID, ap.ssid); + intent.putExtra(EXTRA_START_CONNECT_SSID, ap.getSsid()); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); fireSettingsIntentCallback(intent); return true; } else { - WifiConfiguration config = new WifiConfiguration(); - config.SSID = "\"" + ap.ssid + "\""; - config.allowedKeyManagement.set(KeyMgmt.NONE); - mWifiManager.connect(config, mConnectListener); + ap.generateOpenNetworkConfig(); + mWifiTracker.getManager().connect(ap.getConfig(), mConnectListener); } - } else { - mWifiManager.connect(ap.networkId, mConnectListener); } return false; } @@ -145,76 +133,28 @@ public class AccessPointControllerImpl implements NetworkController.AccessPointC } } - private void fireAcccessPointsCallback(AccessPoint[] aps) { + private void fireAcccessPointsCallback(List<AccessPoint> aps) { for (AccessPointCallback callback : mCallbacks) { callback.onAccessPointsChanged(aps); } } - private static String trimDoubleQuotes(String v) { - return v != null && v.length() >= 2 && v.charAt(0) == '\"' - && v.charAt(v.length() - 1) == '\"' ? v.substring(1, v.length() - 1) : v; + public void dump(PrintWriter pw) { + mWifiTracker.dump(pw); } - private int getConnectedNetworkId(WifiInfo wifiInfo) { - return wifiInfo != null ? wifiInfo.getNetworkId() : AccessPoint.NO_NETWORK; + @Override + public void onWifiStateChanged(int state) { } - private ArrayMap<String, WifiConfiguration> getConfiguredNetworksBySsid() { - final List<WifiConfiguration> configs = mWifiManager.getConfiguredNetworks(); - if (configs == null || configs.size() == 0) return ArrayMap.EMPTY; - final ArrayMap<String, WifiConfiguration> rt = new ArrayMap<String, WifiConfiguration>(); - for (WifiConfiguration config : configs) { - rt.put(trimDoubleQuotes(config.SSID), config); - } - return rt; + @Override + public void onConnectedChanged() { + fireAcccessPointsCallback(mWifiTracker.getAccessPoints()); } - private void updateAccessPoints() { - final WifiInfo wifiInfo = mWifiManager.getConnectionInfo(); - final int connectedNetworkId = getConnectedNetworkId(wifiInfo); - if (DEBUG) Log.d(TAG, "connectedNetworkId: " + connectedNetworkId); - final List<ScanResult> scanResults = mWifiManager.getScanResults(); - final ArrayMap<String, WifiConfiguration> configured = getConfiguredNetworksBySsid(); - if (DEBUG) Log.d(TAG, "scanResults: " + scanResults); - final List<AccessPoint> aps = new ArrayList<AccessPoint>(scanResults.size()); - final ArraySet<String> ssids = new ArraySet<String>(); - for (ScanResult scanResult : scanResults) { - if (scanResult == null) { - continue; - } - final String ssid = scanResult.SSID; - if (TextUtils.isEmpty(ssid) || ssids.contains(ssid)) continue; - ssids.add(ssid); - final WifiConfiguration config = configured.get(ssid); - final int level = WifiManager.calculateSignalLevel(scanResult.level, ICONS.length); - final AccessPoint ap = new AccessPoint(); - ap.isConfigured = config != null; - ap.networkId = config != null ? config.networkId : AccessPoint.NO_NETWORK; - ap.ssid = ssid; - // Connected if either: - // -The network ID in the active WifiInfo matches this network's ID. - // -The network is ephemeral (no configuration) but the SSID matches. - ap.isConnected = (ap.networkId != AccessPoint.NO_NETWORK - && ap.networkId == connectedNetworkId) || - (ap.networkId == WifiConfiguration.INVALID_NETWORK_ID && wifiInfo != null && - ap.ssid.equals(trimDoubleQuotes(wifiInfo.getSSID()))); - if (ap.isConnected && mNetworkController != null) { - // Ensure we have the connected network's RSSI. - ap.level = mNetworkController.getConnectedWifiLevel(); - } else { - ap.level = level; - } - ap.iconId = ICONS[ap.level]; - // Based on Settings AccessPoint#getSecurity, keep up to date - // with better methods of determining no security or not. - ap.hasSecurity = scanResult.capabilities.contains("WEP") - || scanResult.capabilities.contains("PSK") - || scanResult.capabilities.contains("EAP"); - aps.add(ap); - } - Collections.sort(aps, mByStrength); - fireAcccessPointsCallback(aps.toArray(new AccessPoint[aps.size()])); + @Override + public void onAccessPointsChanged() { + fireAcccessPointsCallback(mWifiTracker.getAccessPoints()); } private final ActionListener mConnectListener = new ActionListener() { @@ -228,49 +168,4 @@ public class AccessPointControllerImpl implements NetworkController.AccessPointC if (DEBUG) Log.d(TAG, "connect failure reason=" + reason); } }; - - private final Comparator<AccessPoint> mByStrength = new Comparator<AccessPoint> () { - @Override - public int compare(AccessPoint lhs, AccessPoint rhs) { - return -Integer.compare(score(lhs), score(rhs)); - } - - private int score(AccessPoint ap) { - return ap.level + (ap.isConnected ? 20 : 0) + (ap.isConfigured ? 10 : 0); - } - }; - - private final class Receiver extends BroadcastReceiver { - private boolean mRegistered; - - public void setListening(boolean listening) { - if (listening && !mRegistered) { - if (DEBUG) Log.d(TAG, "Registering receiver"); - final IntentFilter filter = new IntentFilter(); - filter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION); - filter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION); - filter.addAction(WifiManager.NETWORK_IDS_CHANGED_ACTION); - filter.addAction(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION); - filter.addAction(WifiManager.CONFIGURED_NETWORKS_CHANGED_ACTION); - filter.addAction(WifiManager.LINK_CONFIGURATION_CHANGED_ACTION); - filter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION); - filter.addAction(WifiManager.RSSI_CHANGED_ACTION); - mContext.registerReceiver(this, filter); - mRegistered = true; - } else if (!listening && mRegistered) { - if (DEBUG) Log.d(TAG, "Unregistering receiver"); - mContext.unregisterReceiver(this); - mRegistered = false; - } - } - - @Override - public void onReceive(Context context, Intent intent) { - if (DEBUG) Log.d(TAG, "onReceive " + intent.getAction()); - if (WifiManager.SCAN_RESULTS_AVAILABLE_ACTION.equals(intent.getAction())) { - updateAccessPoints(); - mScanning = false; - } - } - } } diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/policy/NetworkController.java b/packages/SystemUI/src/com/android/systemui/statusbar/policy/NetworkController.java index 3cffc85..9212837 100644 --- a/packages/SystemUI/src/com/android/systemui/statusbar/policy/NetworkController.java +++ b/packages/SystemUI/src/com/android/systemui/statusbar/policy/NetworkController.java @@ -18,6 +18,10 @@ package com.android.systemui.statusbar.policy; import android.content.Intent; +import com.android.settingslib.wifi.AccessPoint; + +import java.util.List; + public interface NetworkController { boolean hasMobileDataFeature(); @@ -50,25 +54,14 @@ public interface NetworkController { void addAccessPointCallback(AccessPointCallback callback); void removeAccessPointCallback(AccessPointCallback callback); void scanForAccessPoints(); + int getIcon(AccessPoint ap); boolean connect(AccessPoint ap); boolean canConfigWifi(); public interface AccessPointCallback { - void onAccessPointsChanged(AccessPoint[] accessPoints); + void onAccessPointsChanged(List<AccessPoint> accessPoints); void onSettingsActivityTriggered(Intent settingsIntent); } - - public static class AccessPoint { - public static final int NO_NETWORK = -1; // see WifiManager - - public int networkId; - public int iconId; - public String ssid; - public boolean isConnected; - public boolean isConfigured; - public boolean hasSecurity; - public int level; // 0 - 5 - } } /** diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/policy/NetworkControllerImpl.java b/packages/SystemUI/src/com/android/systemui/statusbar/policy/NetworkControllerImpl.java index 1d37ee3..af6757c 100644 --- a/packages/SystemUI/src/com/android/systemui/statusbar/policy/NetworkControllerImpl.java +++ b/packages/SystemUI/src/com/android/systemui/statusbar/policy/NetworkControllerImpl.java @@ -166,7 +166,6 @@ public class NetworkControllerImpl extends BroadcastReceiver // AIRPLANE_MODE_CHANGED is sent at boot; we've probably already missed it updateAirplaneMode(true /* force callback */); - mAccessPoints.setNetworkController(this); } private void registerListeners() { @@ -669,6 +668,8 @@ public class NetworkControllerImpl extends BroadcastReceiver mobileSignalController.dump(pw); } mWifiSignalController.dump(pw); + + mAccessPoints.dump(pw); } private boolean mDemoMode; |