John Spurlock | 7f8f22a | 2014-07-02 18:54:17 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.systemui.statusbar.policy; |
| 18 | |
Jason Monk | 5d32507 | 2014-10-27 11:38:47 -0400 | [diff] [blame^] | 19 | import android.app.ActivityManager; |
John Spurlock | 7f8f22a | 2014-07-02 18:54:17 -0400 | [diff] [blame] | 20 | import android.content.BroadcastReceiver; |
| 21 | import android.content.Context; |
| 22 | import android.content.Intent; |
| 23 | import android.content.IntentFilter; |
| 24 | import android.net.wifi.ScanResult; |
| 25 | import android.net.wifi.WifiConfiguration; |
Jason Monk | 5d32507 | 2014-10-27 11:38:47 -0400 | [diff] [blame^] | 26 | import android.net.wifi.WifiConfiguration.KeyMgmt; |
John Spurlock | 7f8f22a | 2014-07-02 18:54:17 -0400 | [diff] [blame] | 27 | import android.net.wifi.WifiInfo; |
| 28 | import android.net.wifi.WifiManager; |
| 29 | import android.net.wifi.WifiManager.ActionListener; |
Jason Monk | 5d32507 | 2014-10-27 11:38:47 -0400 | [diff] [blame^] | 30 | import android.os.UserHandle; |
| 31 | import android.os.UserManager; |
| 32 | import android.provider.Settings; |
John Spurlock | 7f8f22a | 2014-07-02 18:54:17 -0400 | [diff] [blame] | 33 | import android.text.TextUtils; |
| 34 | import android.util.ArrayMap; |
| 35 | import android.util.ArraySet; |
| 36 | import android.util.Log; |
| 37 | |
| 38 | import com.android.systemui.R; |
| 39 | import com.android.systemui.statusbar.policy.NetworkController.AccessPoint; |
| 40 | import com.android.systemui.statusbar.policy.NetworkController.AccessPointCallback; |
| 41 | |
| 42 | import java.util.ArrayList; |
| 43 | import java.util.Collections; |
| 44 | import java.util.Comparator; |
| 45 | import java.util.List; |
| 46 | |
Jason Monk | 5d32507 | 2014-10-27 11:38:47 -0400 | [diff] [blame^] | 47 | public class AccessPointController { |
| 48 | private static final String TAG = "AccessPointController"; |
| 49 | private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG); |
| 50 | |
| 51 | // This string extra specifies a network to open the connect dialog on, so the user can enter |
| 52 | // network credentials. This is used by quick settings for secured networks. |
| 53 | private static final String EXTRA_START_CONNECT_SSID = "wifi_start_connect_ssid"; |
John Spurlock | 7f8f22a | 2014-07-02 18:54:17 -0400 | [diff] [blame] | 54 | |
| 55 | private static final int[] ICONS = { |
| 56 | R.drawable.ic_qs_wifi_0, |
| 57 | R.drawable.ic_qs_wifi_full_1, |
| 58 | R.drawable.ic_qs_wifi_full_2, |
| 59 | R.drawable.ic_qs_wifi_full_3, |
| 60 | R.drawable.ic_qs_wifi_full_4, |
| 61 | }; |
| 62 | |
| 63 | private final Context mContext; |
| 64 | private final ArrayList<AccessPointCallback> mCallbacks = new ArrayList<AccessPointCallback>(); |
| 65 | private final WifiManager mWifiManager; |
Jason Monk | 5d32507 | 2014-10-27 11:38:47 -0400 | [diff] [blame^] | 66 | private final UserManager mUserManager; |
John Spurlock | 7f8f22a | 2014-07-02 18:54:17 -0400 | [diff] [blame] | 67 | private final Receiver mReceiver = new Receiver(); |
| 68 | |
| 69 | private boolean mScanning; |
Jason Monk | 5d32507 | 2014-10-27 11:38:47 -0400 | [diff] [blame^] | 70 | private int mCurrentUser; |
John Spurlock | 7f8f22a | 2014-07-02 18:54:17 -0400 | [diff] [blame] | 71 | |
Jason Monk | 5d32507 | 2014-10-27 11:38:47 -0400 | [diff] [blame^] | 72 | public AccessPointController(Context context) { |
John Spurlock | 7f8f22a | 2014-07-02 18:54:17 -0400 | [diff] [blame] | 73 | mContext = context; |
| 74 | mWifiManager = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE); |
Jason Monk | 5d32507 | 2014-10-27 11:38:47 -0400 | [diff] [blame^] | 75 | mUserManager = (UserManager) mContext.getSystemService(Context.USER_SERVICE); |
| 76 | mCurrentUser = ActivityManager.getCurrentUser(); |
| 77 | } |
| 78 | |
| 79 | public boolean canConfigWifi() { |
| 80 | return !mUserManager.hasUserRestriction(UserManager.DISALLOW_CONFIG_WIFI, |
| 81 | new UserHandle(mCurrentUser)); |
| 82 | } |
| 83 | |
| 84 | void onUserSwitched(int newUserId) { |
| 85 | mCurrentUser = newUserId; |
John Spurlock | 7f8f22a | 2014-07-02 18:54:17 -0400 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | public void addCallback(AccessPointCallback callback) { |
| 89 | if (callback == null || mCallbacks.contains(callback)) return; |
| 90 | if (DEBUG) Log.d(TAG, "addCallback " + callback); |
| 91 | mCallbacks.add(callback); |
| 92 | mReceiver.setListening(!mCallbacks.isEmpty()); |
| 93 | } |
| 94 | |
| 95 | public void removeCallback(AccessPointCallback callback) { |
| 96 | if (callback == null) return; |
| 97 | if (DEBUG) Log.d(TAG, "removeCallback " + callback); |
| 98 | mCallbacks.remove(callback); |
| 99 | mReceiver.setListening(!mCallbacks.isEmpty()); |
| 100 | } |
| 101 | |
| 102 | public void scan() { |
| 103 | if (mScanning) return; |
| 104 | if (DEBUG) Log.d(TAG, "scan!"); |
| 105 | mScanning = mWifiManager.startScan(); |
Jason Monk | 5d32507 | 2014-10-27 11:38:47 -0400 | [diff] [blame^] | 106 | // Grab current networks immediately while we wait for scan. |
| 107 | updateAccessPoints(); |
John Spurlock | 7f8f22a | 2014-07-02 18:54:17 -0400 | [diff] [blame] | 108 | } |
| 109 | |
Jason Monk | 5d32507 | 2014-10-27 11:38:47 -0400 | [diff] [blame^] | 110 | public boolean connect(AccessPoint ap) { |
| 111 | if (ap == null) return false; |
John Spurlock | 7f8f22a | 2014-07-02 18:54:17 -0400 | [diff] [blame] | 112 | if (DEBUG) Log.d(TAG, "connect networkId=" + ap.networkId); |
Jason Monk | 5d32507 | 2014-10-27 11:38:47 -0400 | [diff] [blame^] | 113 | if (ap.networkId < 0) { |
| 114 | // Unknown network, need to add it. |
| 115 | if (ap.hasSecurity) { |
| 116 | Intent intent = new Intent(Settings.ACTION_WIFI_SETTINGS); |
| 117 | intent.putExtra(EXTRA_START_CONNECT_SSID, ap.ssid); |
| 118 | intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 119 | mContext.startActivity(intent); |
| 120 | return true; |
| 121 | } else { |
| 122 | WifiConfiguration config = new WifiConfiguration(); |
| 123 | config.SSID = "\"" + ap.ssid + "\""; |
| 124 | config.allowedKeyManagement.set(KeyMgmt.NONE); |
| 125 | mWifiManager.connect(config, mConnectListener); |
John Spurlock | 7f8f22a | 2014-07-02 18:54:17 -0400 | [diff] [blame] | 126 | } |
Jason Monk | 5d32507 | 2014-10-27 11:38:47 -0400 | [diff] [blame^] | 127 | } else { |
| 128 | mWifiManager.connect(ap.networkId, mConnectListener); |
| 129 | } |
| 130 | return false; |
John Spurlock | 7f8f22a | 2014-07-02 18:54:17 -0400 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | private void fireCallback(AccessPoint[] aps) { |
| 134 | for (AccessPointCallback callback : mCallbacks) { |
| 135 | callback.onAccessPointsChanged(aps); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | private static String trimDoubleQuotes(String v) { |
| 140 | return v != null && v.length() >= 2 && v.charAt(0) == '\"' |
| 141 | && v.charAt(v.length() - 1) == '\"' ? v.substring(1, v.length() - 1) : v; |
| 142 | } |
| 143 | |
| 144 | private int getConnectedNetworkId() { |
| 145 | final WifiInfo wifiInfo = mWifiManager.getConnectionInfo(); |
| 146 | return wifiInfo != null ? wifiInfo.getNetworkId() : AccessPoint.NO_NETWORK; |
| 147 | } |
| 148 | |
| 149 | private ArrayMap<String, WifiConfiguration> getConfiguredNetworksBySsid() { |
| 150 | final List<WifiConfiguration> configs = mWifiManager.getConfiguredNetworks(); |
| 151 | if (configs == null || configs.size() == 0) return ArrayMap.EMPTY; |
| 152 | final ArrayMap<String, WifiConfiguration> rt = new ArrayMap<String, WifiConfiguration>(); |
| 153 | for (WifiConfiguration config : configs) { |
| 154 | rt.put(trimDoubleQuotes(config.SSID), config); |
| 155 | } |
| 156 | return rt; |
| 157 | } |
| 158 | |
| 159 | private void updateAccessPoints() { |
| 160 | final int connectedNetworkId = getConnectedNetworkId(); |
| 161 | if (DEBUG) Log.d(TAG, "connectedNetworkId: " + connectedNetworkId); |
| 162 | final List<ScanResult> scanResults = mWifiManager.getScanResults(); |
| 163 | final ArrayMap<String, WifiConfiguration> configured = getConfiguredNetworksBySsid(); |
| 164 | if (DEBUG) Log.d(TAG, "scanResults: " + scanResults); |
| 165 | final List<AccessPoint> aps = new ArrayList<AccessPoint>(scanResults.size()); |
| 166 | final ArraySet<String> ssids = new ArraySet<String>(); |
| 167 | for (ScanResult scanResult : scanResults) { |
Craig Mautner | 99b6928 | 2014-08-26 17:03:39 -0700 | [diff] [blame] | 168 | if (scanResult == null) { |
| 169 | continue; |
| 170 | } |
John Spurlock | 7f8f22a | 2014-07-02 18:54:17 -0400 | [diff] [blame] | 171 | final String ssid = scanResult.SSID; |
| 172 | if (TextUtils.isEmpty(ssid) || ssids.contains(ssid)) continue; |
John Spurlock | 7f8f22a | 2014-07-02 18:54:17 -0400 | [diff] [blame] | 173 | ssids.add(ssid); |
| 174 | final WifiConfiguration config = configured.get(ssid); |
| 175 | final int level = WifiManager.calculateSignalLevel(scanResult.level, ICONS.length); |
| 176 | final AccessPoint ap = new AccessPoint(); |
Jason Monk | 5d32507 | 2014-10-27 11:38:47 -0400 | [diff] [blame^] | 177 | ap.isConfigured = config != null; |
John Spurlock | 7f8f22a | 2014-07-02 18:54:17 -0400 | [diff] [blame] | 178 | ap.networkId = config != null ? config.networkId : AccessPoint.NO_NETWORK; |
| 179 | ap.ssid = ssid; |
| 180 | ap.iconId = ICONS[level]; |
| 181 | ap.isConnected = ap.networkId != AccessPoint.NO_NETWORK |
| 182 | && ap.networkId == connectedNetworkId; |
| 183 | ap.level = level; |
Jason Monk | 5d32507 | 2014-10-27 11:38:47 -0400 | [diff] [blame^] | 184 | // Based on Settings AccessPoint#getSecurity, keep up to date |
| 185 | // with better methods of determining no security or not. |
| 186 | ap.hasSecurity = scanResult.capabilities.contains("WEP") |
| 187 | || scanResult.capabilities.contains("PSK") |
| 188 | || scanResult.capabilities.contains("EAP"); |
John Spurlock | 7f8f22a | 2014-07-02 18:54:17 -0400 | [diff] [blame] | 189 | aps.add(ap); |
| 190 | } |
| 191 | Collections.sort(aps, mByStrength); |
| 192 | fireCallback(aps.toArray(new AccessPoint[aps.size()])); |
| 193 | } |
| 194 | |
Jason Monk | 5d32507 | 2014-10-27 11:38:47 -0400 | [diff] [blame^] | 195 | private final ActionListener mConnectListener = new ActionListener() { |
| 196 | @Override |
| 197 | public void onSuccess() { |
| 198 | if (DEBUG) Log.d(TAG, "connect success"); |
| 199 | } |
| 200 | |
| 201 | @Override |
| 202 | public void onFailure(int reason) { |
| 203 | if (DEBUG) Log.d(TAG, "connect failure reason=" + reason); |
| 204 | } |
| 205 | }; |
| 206 | |
John Spurlock | 7f8f22a | 2014-07-02 18:54:17 -0400 | [diff] [blame] | 207 | private final Comparator<AccessPoint> mByStrength = new Comparator<AccessPoint> () { |
| 208 | @Override |
| 209 | public int compare(AccessPoint lhs, AccessPoint rhs) { |
| 210 | return -Integer.compare(score(lhs), score(rhs)); |
| 211 | } |
| 212 | |
| 213 | private int score(AccessPoint ap) { |
Jason Monk | 5d32507 | 2014-10-27 11:38:47 -0400 | [diff] [blame^] | 214 | return ap.level + (ap.isConnected ? 20 : 0) + (ap.isConfigured ? 10 : 0); |
John Spurlock | 7f8f22a | 2014-07-02 18:54:17 -0400 | [diff] [blame] | 215 | } |
| 216 | }; |
| 217 | |
| 218 | private final class Receiver extends BroadcastReceiver { |
| 219 | private boolean mRegistered; |
| 220 | |
| 221 | public void setListening(boolean listening) { |
| 222 | if (listening && !mRegistered) { |
| 223 | if (DEBUG) Log.d(TAG, "Registering receiver"); |
| 224 | final IntentFilter filter = new IntentFilter(); |
| 225 | filter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION); |
| 226 | filter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION); |
| 227 | filter.addAction(WifiManager.NETWORK_IDS_CHANGED_ACTION); |
| 228 | filter.addAction(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION); |
| 229 | filter.addAction(WifiManager.CONFIGURED_NETWORKS_CHANGED_ACTION); |
| 230 | filter.addAction(WifiManager.LINK_CONFIGURATION_CHANGED_ACTION); |
| 231 | filter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION); |
| 232 | filter.addAction(WifiManager.RSSI_CHANGED_ACTION); |
| 233 | mContext.registerReceiver(this, filter); |
| 234 | mRegistered = true; |
| 235 | } else if (!listening && mRegistered) { |
| 236 | if (DEBUG) Log.d(TAG, "Unregistering receiver"); |
| 237 | mContext.unregisterReceiver(this); |
| 238 | mRegistered = false; |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | @Override |
| 243 | public void onReceive(Context context, Intent intent) { |
| 244 | if (DEBUG) Log.d(TAG, "onReceive " + intent.getAction()); |
| 245 | if (WifiManager.SCAN_RESULTS_AVAILABLE_ACTION.equals(intent.getAction())) { |
| 246 | updateAccessPoints(); |
| 247 | mScanning = false; |
| 248 | } |
| 249 | } |
| 250 | } |
| 251 | } |