AccessPoint.java revision f9bf6da6cd0666897f4b56cf0e6dee3c55243273
1/*
2 * Copyright (C) 2015 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
17package com.android.settingslib.wifi;
18
19import android.app.AppGlobals;
20import android.content.Context;
21import android.content.pm.ApplicationInfo;
22import android.content.pm.IPackageManager;
23import android.content.pm.PackageManager;
24import android.net.ConnectivityManager;
25import android.net.Network;
26import android.net.NetworkCapabilities;
27import android.net.NetworkInfo;
28import android.net.NetworkInfo.DetailedState;
29import android.net.NetworkInfo.State;
30import android.net.wifi.IWifiManager;
31import android.net.wifi.ScanResult;
32import android.net.wifi.WifiConfiguration;
33import android.net.wifi.WifiConfiguration.KeyMgmt;
34import android.net.wifi.WifiInfo;
35import android.net.wifi.WifiManager;
36import android.os.Bundle;
37import android.os.RemoteException;
38import android.os.ServiceManager;
39import android.os.UserHandle;
40import android.text.Spannable;
41import android.text.SpannableString;
42import android.text.TextUtils;
43import android.text.style.TtsSpan;
44import android.util.Log;
45import android.util.LruCache;
46
47import com.android.settingslib.R;
48
49import java.util.ArrayList;
50import java.util.Map;
51
52
53public class AccessPoint implements Comparable<AccessPoint> {
54    static final String TAG = "SettingsLib.AccessPoint";
55
56    /**
57     * Lower bound on the 2.4 GHz (802.11b/g/n) WLAN channels
58     */
59    public static final int LOWER_FREQ_24GHZ = 2400;
60
61    /**
62     * Upper bound on the 2.4 GHz (802.11b/g/n) WLAN channels
63     */
64    public static final int HIGHER_FREQ_24GHZ = 2500;
65
66    /**
67     * Lower bound on the 5.0 GHz (802.11a/h/j/n/ac) WLAN channels
68     */
69    public static final int LOWER_FREQ_5GHZ = 4900;
70
71    /**
72     * Upper bound on the 5.0 GHz (802.11a/h/j/n/ac) WLAN channels
73     */
74    public static final int HIGHER_FREQ_5GHZ = 5900;
75
76
77    /**
78     * Experimental: we should be able to show the user the list of BSSIDs and bands
79     *  for that SSID.
80     *  For now this data is used only with Verbose Logging so as to show the band and number
81     *  of BSSIDs on which that network is seen.
82     */
83    public LruCache<String, ScanResult> mScanResultCache = new LruCache<String, ScanResult>(32);
84
85    private static final String KEY_NETWORKINFO = "key_networkinfo";
86    private static final String KEY_WIFIINFO = "key_wifiinfo";
87    private static final String KEY_SCANRESULT = "key_scanresult";
88    private static final String KEY_SSID = "key_ssid";
89    private static final String KEY_SECURITY = "key_security";
90    private static final String KEY_PSKTYPE = "key_psktype";
91    private static final String KEY_SCANRESULTCACHE = "key_scanresultcache";
92    private static final String KEY_CONFIG = "key_config";
93
94    /**
95     * These values are matched in string arrays -- changes must be kept in sync
96     */
97    public static final int SECURITY_NONE = 0;
98    public static final int SECURITY_WEP = 1;
99    public static final int SECURITY_PSK = 2;
100    public static final int SECURITY_EAP = 3;
101
102    private static final int PSK_UNKNOWN = 0;
103    private static final int PSK_WPA = 1;
104    private static final int PSK_WPA2 = 2;
105    private static final int PSK_WPA_WPA2 = 3;
106
107    private static final int VISIBILITY_OUTDATED_AGE_IN_MILLI = 20000;
108    private final Context mContext;
109
110    private String ssid;
111    private int security;
112    private int networkId = WifiConfiguration.INVALID_NETWORK_ID;
113
114    private int pskType = PSK_UNKNOWN;
115
116    private WifiConfiguration mConfig;
117
118    private int mRssi = Integer.MAX_VALUE;
119    private long mSeen = 0;
120
121    private WifiInfo mInfo;
122    private NetworkInfo mNetworkInfo;
123    private AccessPointListener mAccessPointListener;
124
125    private Object mTag;
126
127    public AccessPoint(Context context, Bundle savedState) {
128        mContext = context;
129        mConfig = savedState.getParcelable(KEY_CONFIG);
130        if (mConfig != null) {
131            loadConfig(mConfig);
132        }
133        if (savedState.containsKey(KEY_SSID)) {
134            ssid = savedState.getString(KEY_SSID);
135        }
136        if (savedState.containsKey(KEY_SECURITY)) {
137            security = savedState.getInt(KEY_SECURITY);
138        }
139        if (savedState.containsKey(KEY_PSKTYPE)) {
140            pskType = savedState.getInt(KEY_PSKTYPE);
141        }
142        mInfo = (WifiInfo) savedState.getParcelable(KEY_WIFIINFO);
143        if (savedState.containsKey(KEY_NETWORKINFO)) {
144            mNetworkInfo = savedState.getParcelable(KEY_NETWORKINFO);
145        }
146        if (savedState.containsKey(KEY_SCANRESULTCACHE)) {
147            ArrayList<ScanResult> scanResultArrayList =
148                    savedState.getParcelableArrayList(KEY_SCANRESULTCACHE);
149            mScanResultCache.evictAll();
150            for (ScanResult result : scanResultArrayList) {
151                mScanResultCache.put(result.BSSID, result);
152            }
153        }
154        update(mConfig, mInfo, mNetworkInfo);
155        mRssi = getRssi();
156        mSeen = getSeen();
157    }
158
159    AccessPoint(Context context, ScanResult result) {
160        mContext = context;
161        initWithScanResult(result);
162    }
163
164    AccessPoint(Context context, WifiConfiguration config) {
165        mContext = context;
166        loadConfig(config);
167    }
168
169    @Override
170    public int compareTo(AccessPoint other) {
171        // Active one goes first.
172        if (isActive() && !other.isActive()) return -1;
173        if (!isActive() && other.isActive()) return 1;
174
175        // Reachable one goes before unreachable one.
176        if (mRssi != Integer.MAX_VALUE && other.mRssi == Integer.MAX_VALUE) return -1;
177        if (mRssi == Integer.MAX_VALUE && other.mRssi != Integer.MAX_VALUE) return 1;
178
179        // Configured one goes before unconfigured one.
180        if (networkId != WifiConfiguration.INVALID_NETWORK_ID
181                && other.networkId == WifiConfiguration.INVALID_NETWORK_ID) return -1;
182        if (networkId == WifiConfiguration.INVALID_NETWORK_ID
183                && other.networkId != WifiConfiguration.INVALID_NETWORK_ID) return 1;
184
185        // Sort by signal strength.
186        int difference = WifiManager.compareSignalLevel(other.mRssi, mRssi);
187        if (difference != 0) {
188            return difference;
189        }
190        // Sort by ssid.
191        return ssid.compareToIgnoreCase(other.ssid);
192    }
193
194    @Override
195    public boolean equals(Object other) {
196        if (!(other instanceof AccessPoint)) return false;
197        return (this.compareTo((AccessPoint) other) == 0);
198    }
199
200    @Override
201    public int hashCode() {
202        int result = 0;
203        if (mInfo != null) result += 13 * mInfo.hashCode();
204        result += 19 * mRssi;
205        result += 23 * networkId;
206        result += 29 * ssid.hashCode();
207        return result;
208    }
209
210    @Override
211    public String toString() {
212        StringBuilder builder = new StringBuilder().append("AccessPoint(")
213                .append(ssid);
214        if (isSaved()) {
215            builder.append(',').append("saved");
216        }
217        if (isActive()) {
218            builder.append(',').append("active");
219        }
220        if (isEphemeral()) {
221            builder.append(',').append("ephemeral");
222        }
223        if (isConnectable()) {
224            builder.append(',').append("connectable");
225        }
226        if (security != SECURITY_NONE) {
227            builder.append(',').append(securityToString(security, pskType));
228        }
229        return builder.append(')').toString();
230    }
231
232    public boolean matches(ScanResult result) {
233        return ssid.equals(result.SSID) && security == getSecurity(result);
234    }
235
236    public boolean matches(WifiConfiguration config) {
237        if (config.isPasspoint() && mConfig != null && mConfig.isPasspoint()) {
238            return config.FQDN.equals(mConfig.providerFriendlyName);
239        } else {
240            return ssid.equals(removeDoubleQuotes(config.SSID))
241                    && security == getSecurity(config)
242                    && (mConfig == null || mConfig.shared == config.shared);
243        }
244    }
245
246    public WifiConfiguration getConfig() {
247        return mConfig;
248    }
249
250    public void clearConfig() {
251        mConfig = null;
252        networkId = WifiConfiguration.INVALID_NETWORK_ID;
253    }
254
255    public WifiInfo getInfo() {
256        return mInfo;
257    }
258
259    public int getLevel() {
260        if (mRssi == Integer.MAX_VALUE) {
261            return -1;
262        }
263        return WifiManager.calculateSignalLevel(mRssi, 4);
264    }
265
266    public int getRssi() {
267        int rssi = Integer.MIN_VALUE;
268        for (ScanResult result : mScanResultCache.snapshot().values()) {
269            if (result.level > rssi) {
270                rssi = result.level;
271            }
272        }
273
274        return rssi;
275    }
276
277    public long getSeen() {
278        long seen = 0;
279        for (ScanResult result : mScanResultCache.snapshot().values()) {
280            if (result.timestamp > seen) {
281                seen = result.timestamp;
282            }
283        }
284
285        return seen;
286    }
287
288    public NetworkInfo getNetworkInfo() {
289        return mNetworkInfo;
290    }
291
292    public int getSecurity() {
293        return security;
294    }
295
296    public String getSecurityString(boolean concise) {
297        Context context = mContext;
298        if (mConfig != null && mConfig.isPasspoint()) {
299            return concise ? context.getString(R.string.wifi_security_short_eap) :
300                context.getString(R.string.wifi_security_eap);
301        }
302        switch(security) {
303            case SECURITY_EAP:
304                return concise ? context.getString(R.string.wifi_security_short_eap) :
305                    context.getString(R.string.wifi_security_eap);
306            case SECURITY_PSK:
307                switch (pskType) {
308                    case PSK_WPA:
309                        return concise ? context.getString(R.string.wifi_security_short_wpa) :
310                            context.getString(R.string.wifi_security_wpa);
311                    case PSK_WPA2:
312                        return concise ? context.getString(R.string.wifi_security_short_wpa2) :
313                            context.getString(R.string.wifi_security_wpa2);
314                    case PSK_WPA_WPA2:
315                        return concise ? context.getString(R.string.wifi_security_short_wpa_wpa2) :
316                            context.getString(R.string.wifi_security_wpa_wpa2);
317                    case PSK_UNKNOWN:
318                    default:
319                        return concise ? context.getString(R.string.wifi_security_short_psk_generic)
320                                : context.getString(R.string.wifi_security_psk_generic);
321                }
322            case SECURITY_WEP:
323                return concise ? context.getString(R.string.wifi_security_short_wep) :
324                    context.getString(R.string.wifi_security_wep);
325            case SECURITY_NONE:
326            default:
327                return concise ? "" : context.getString(R.string.wifi_security_none);
328        }
329    }
330
331    public String getSsidStr() {
332        return ssid;
333    }
334
335    public CharSequence getSsid() {
336        SpannableString str = new SpannableString(ssid);
337        str.setSpan(new TtsSpan.VerbatimBuilder(ssid).build(), 0, ssid.length(),
338                Spannable.SPAN_INCLUSIVE_INCLUSIVE);
339        return str;
340    }
341
342    public String getConfigName() {
343        if (mConfig != null && mConfig.isPasspoint()) {
344            return mConfig.providerFriendlyName;
345        } else {
346            return ssid;
347        }
348    }
349
350    public DetailedState getDetailedState() {
351        return mNetworkInfo != null ? mNetworkInfo.getDetailedState() : null;
352    }
353
354    public String getSavedNetworkSummary() {
355        if (mConfig != null) {
356            PackageManager pm = mContext.getPackageManager();
357            String systemName = pm.getNameForUid(android.os.Process.SYSTEM_UID);
358            int userId = UserHandle.getUserId(mConfig.creatorUid);
359            ApplicationInfo appInfo = null;
360            if (mConfig.creatorName != null && mConfig.creatorName.equals(systemName)) {
361                appInfo = mContext.getApplicationInfo();
362            } else {
363                try {
364                    IPackageManager ipm = AppGlobals.getPackageManager();
365                    appInfo = ipm.getApplicationInfo(mConfig.creatorName, 0 /* flags */, userId);
366                } catch (RemoteException rex) {
367                }
368            }
369            if (appInfo != null &&
370                    !appInfo.packageName.equals(mContext.getString(R.string.settings_package)) &&
371                    !appInfo.packageName.equals(
372                    mContext.getString(R.string.certinstaller_package))) {
373                return mContext.getString(R.string.saved_network, appInfo.loadLabel(pm));
374            }
375        }
376        return "";
377    }
378
379    public String getSummary() {
380        return getSettingsSummary();
381    }
382
383    public String getSettingsSummary() {
384        // Update to new summary
385        StringBuilder summary = new StringBuilder();
386
387        if (isActive() && mConfig != null && mConfig.isPasspoint()) {
388            // This is the active connection on passpoint
389            summary.append(getSummary(mContext, getDetailedState(),
390                    false, mConfig.providerFriendlyName));
391        } else if (isActive()) {
392            // This is the active connection on non-passpoint network
393            summary.append(getSummary(mContext, getDetailedState(),
394                    mInfo != null && mInfo.isEphemeral()));
395        } else if (mConfig != null && mConfig.isPasspoint()) {
396            String format = mContext.getString(R.string.available_via_passpoint);
397            summary.append(String.format(format, mConfig.providerFriendlyName));
398        } else if (mConfig != null && mConfig.hasNoInternetAccess()) {
399            summary.append(mContext.getString(R.string.wifi_no_internet));
400        } else if (mConfig != null && !mConfig.getNetworkSelectionStatus().isNetworkEnabled()) {
401            WifiConfiguration.NetworkSelectionStatus networkStatus =
402                    mConfig.getNetworkSelectionStatus();
403            switch (networkStatus.getNetworkSelectionDisableReason()) {
404                case WifiConfiguration.NetworkSelectionStatus.DISABLED_AUTHENTICATION_FAILURE:
405                    summary.append(mContext.getString(R.string.wifi_disabled_password_failure));
406                    break;
407                case WifiConfiguration.NetworkSelectionStatus.DISABLED_DHCP_FAILURE:
408                case WifiConfiguration.NetworkSelectionStatus.DISABLED_DNS_FAILURE:
409                    summary.append(mContext.getString(R.string.wifi_disabled_network_failure));
410                    break;
411                case WifiConfiguration.NetworkSelectionStatus.DISABLED_ASSOCIATION_REJECTION:
412                    summary.append(mContext.getString(R.string.wifi_disabled_generic));
413                    break;
414            }
415        } else if (mRssi == Integer.MAX_VALUE) { // Wifi out of range
416            summary.append(mContext.getString(R.string.wifi_not_in_range));
417        } else { // In range, not disabled.
418            if (mConfig != null) { // Is saved network
419                summary.append(mContext.getString(R.string.wifi_remembered));
420            }
421        }
422
423        if (WifiTracker.sVerboseLogging > 0) {
424            // Add RSSI/band information for this config, what was seen up to 6 seconds ago
425            // verbose WiFi Logging is only turned on thru developers settings
426            if (mInfo != null && mNetworkInfo != null) { // This is the active connection
427                summary.append(" f=" + Integer.toString(mInfo.getFrequency()));
428            }
429            summary.append(" " + getVisibilityStatus());
430            if (mConfig != null && !mConfig.getNetworkSelectionStatus().isNetworkEnabled()) {
431                summary.append(" (" + mConfig.getNetworkSelectionStatus().getNetworkStatusString());
432                if (mConfig.getNetworkSelectionStatus().getDisableTime() > 0) {
433                    long now = System.currentTimeMillis();
434                    long diff = (now - mConfig.getNetworkSelectionStatus().getDisableTime()) / 1000;
435                    long sec = diff%60; //seconds
436                    long min = (diff/60)%60; //minutes
437                    long hour = (min/60)%60; //hours
438                    summary.append(", ");
439                    if (hour > 0) summary.append(Long.toString(hour) + "h ");
440                    summary.append( Long.toString(min) + "m ");
441                    summary.append( Long.toString(sec) + "s ");
442                }
443                summary.append(")");
444            }
445
446            if (mConfig != null) {
447                WifiConfiguration.NetworkSelectionStatus networkStatus =
448                        mConfig.getNetworkSelectionStatus();
449                for (int index = WifiConfiguration.NetworkSelectionStatus.NETWORK_SELECTION_ENABLE;
450                        index < WifiConfiguration.NetworkSelectionStatus
451                        .NETWORK_SELECTION_DISABLED_MAX; index++) {
452                    if (networkStatus.getDisableReasonCounter(index) != 0) {
453                        summary.append(" " + WifiConfiguration.NetworkSelectionStatus
454                                .getNetworkDisableReasonString(index) + "="
455                                + networkStatus.getDisableReasonCounter(index));
456                    }
457                }
458            }
459        }
460        return summary.toString();
461    }
462
463    /**
464     * Returns the visibility status of the WifiConfiguration.
465     *
466     * @return autojoin debugging information
467     * TODO: use a string formatter
468     * ["rssi 5Ghz", "num results on 5GHz" / "rssi 5Ghz", "num results on 5GHz"]
469     * For instance [-40,5/-30,2]
470     */
471    private String getVisibilityStatus() {
472        StringBuilder visibility = new StringBuilder();
473        StringBuilder scans24GHz = null;
474        StringBuilder scans5GHz = null;
475        String bssid = null;
476
477        long now = System.currentTimeMillis();
478
479        if (mInfo != null) {
480            bssid = mInfo.getBSSID();
481            if (bssid != null) {
482                visibility.append(" ").append(bssid);
483            }
484            visibility.append(" rssi=").append(mInfo.getRssi());
485            visibility.append(" ");
486            visibility.append(" score=").append(mInfo.score);
487            visibility.append(String.format(" tx=%.1f,", mInfo.txSuccessRate));
488            visibility.append(String.format("%.1f,", mInfo.txRetriesRate));
489            visibility.append(String.format("%.1f ", mInfo.txBadRate));
490            visibility.append(String.format("rx=%.1f", mInfo.rxSuccessRate));
491        }
492
493        int rssi5 = WifiConfiguration.INVALID_RSSI;
494        int rssi24 = WifiConfiguration.INVALID_RSSI;
495        int num5 = 0;
496        int num24 = 0;
497        int numBlackListed = 0;
498        int n24 = 0; // Number scan results we included in the string
499        int n5 = 0; // Number scan results we included in the string
500        Map<String, ScanResult> list = mScanResultCache.snapshot();
501        // TODO: sort list by RSSI or age
502        for (ScanResult result : list.values()) {
503
504            if (result.frequency >= LOWER_FREQ_5GHZ
505                    && result.frequency <= HIGHER_FREQ_5GHZ) {
506                // Strictly speaking: [4915, 5825]
507                // number of known BSSID on 5GHz band
508                num5 = num5 + 1;
509            } else if (result.frequency >= LOWER_FREQ_24GHZ
510                    && result.frequency <= HIGHER_FREQ_24GHZ) {
511                // Strictly speaking: [2412, 2482]
512                // number of known BSSID on 2.4Ghz band
513                num24 = num24 + 1;
514            }
515
516
517            if (result.frequency >= LOWER_FREQ_5GHZ
518                    && result.frequency <= HIGHER_FREQ_5GHZ) {
519                if (result.level > rssi5) {
520                    rssi5 = result.level;
521                }
522                if (n5 < 4) {
523                    if (scans5GHz == null) scans5GHz = new StringBuilder();
524                    scans5GHz.append(" \n{").append(result.BSSID);
525                    if (bssid != null && result.BSSID.equals(bssid)) scans5GHz.append("*");
526                    scans5GHz.append("=").append(result.frequency);
527                    scans5GHz.append(",").append(result.level);
528                    scans5GHz.append("}");
529                    n5++;
530                }
531            } else if (result.frequency >= LOWER_FREQ_24GHZ
532                    && result.frequency <= HIGHER_FREQ_24GHZ) {
533                if (result.level > rssi24) {
534                    rssi24 = result.level;
535                }
536                if (n24 < 4) {
537                    if (scans24GHz == null) scans24GHz = new StringBuilder();
538                    scans24GHz.append(" \n{").append(result.BSSID);
539                    if (bssid != null && result.BSSID.equals(bssid)) scans24GHz.append("*");
540                    scans24GHz.append("=").append(result.frequency);
541                    scans24GHz.append(",").append(result.level);
542                    scans24GHz.append("}");
543                    n24++;
544                }
545            }
546        }
547        visibility.append(" [");
548        if (num24 > 0) {
549            visibility.append("(").append(num24).append(")");
550            if (n24 <= 4) {
551                if (scans24GHz != null) {
552                    visibility.append(scans24GHz.toString());
553                }
554            } else {
555                visibility.append("max=").append(rssi24);
556                if (scans24GHz != null) {
557                    visibility.append(",").append(scans24GHz.toString());
558                }
559            }
560        }
561        visibility.append(";");
562        if (num5 > 0) {
563            visibility.append("(").append(num5).append(")");
564            if (n5 <= 4) {
565                if (scans5GHz != null) {
566                    visibility.append(scans5GHz.toString());
567                }
568            } else {
569                visibility.append("max=").append(rssi5);
570                if (scans5GHz != null) {
571                    visibility.append(",").append(scans5GHz.toString());
572                }
573            }
574        }
575        if (numBlackListed > 0)
576            visibility.append("!").append(numBlackListed);
577        visibility.append("]");
578
579        return visibility.toString();
580    }
581
582    /**
583     * Return whether this is the active connection.
584     * For ephemeral connections (networkId is invalid), this returns false if the network is
585     * disconnected.
586     */
587    public boolean isActive() {
588        return mNetworkInfo != null &&
589                (networkId != WifiConfiguration.INVALID_NETWORK_ID ||
590                 mNetworkInfo.getState() != State.DISCONNECTED);
591    }
592
593    public boolean isConnectable() {
594        return getLevel() != -1 && getDetailedState() == null;
595    }
596
597    public boolean isEphemeral() {
598        return mInfo != null && mInfo.isEphemeral() &&
599                mNetworkInfo != null && mNetworkInfo.getState() != State.DISCONNECTED;
600    }
601
602    public boolean isPasspoint() {
603        return mConfig != null && mConfig.isPasspoint();
604    }
605
606    /**
607     * Return whether the given {@link WifiInfo} is for this access point.
608     * If the current AP does not have a network Id then the config is used to
609     * match based on SSID and security.
610     */
611    private boolean isInfoForThisAccessPoint(WifiConfiguration config, WifiInfo info) {
612        if (isPasspoint() == false && networkId != WifiConfiguration.INVALID_NETWORK_ID) {
613            return networkId == info.getNetworkId();
614        } else if (config != null) {
615            return matches(config);
616        }
617        else {
618            // Might be an ephemeral connection with no WifiConfiguration. Try matching on SSID.
619            // (Note that we only do this if the WifiConfiguration explicitly equals INVALID).
620            // TODO: Handle hex string SSIDs.
621            return ssid.equals(removeDoubleQuotes(info.getSSID()));
622        }
623    }
624
625    public boolean isSaved() {
626        return networkId != WifiConfiguration.INVALID_NETWORK_ID;
627    }
628
629    public Object getTag() {
630        return mTag;
631    }
632
633    public void setTag(Object tag) {
634        mTag = tag;
635    }
636
637    /**
638     * Generate and save a default wifiConfiguration with common values.
639     * Can only be called for unsecured networks.
640     */
641    public void generateOpenNetworkConfig() {
642        if (security != SECURITY_NONE)
643            throw new IllegalStateException();
644        if (mConfig != null)
645            return;
646        mConfig = new WifiConfiguration();
647        mConfig.SSID = AccessPoint.convertToQuotedString(ssid);
648        mConfig.allowedKeyManagement.set(KeyMgmt.NONE);
649    }
650
651    void loadConfig(WifiConfiguration config) {
652        if (config.isPasspoint())
653            ssid = config.providerFriendlyName;
654        else
655            ssid = (config.SSID == null ? "" : removeDoubleQuotes(config.SSID));
656
657        security = getSecurity(config);
658        networkId = config.networkId;
659        mConfig = config;
660    }
661
662    private void initWithScanResult(ScanResult result) {
663        ssid = result.SSID;
664        security = getSecurity(result);
665        if (security == SECURITY_PSK)
666            pskType = getPskType(result);
667        mRssi = result.level;
668        mSeen = result.timestamp;
669    }
670
671    public void saveWifiState(Bundle savedState) {
672        if (ssid != null) savedState.putString(KEY_SSID, getSsidStr());
673        savedState.putInt(KEY_SECURITY, security);
674        savedState.putInt(KEY_PSKTYPE, pskType);
675        if (mConfig != null) savedState.putParcelable(KEY_CONFIG, mConfig);
676        savedState.putParcelable(KEY_WIFIINFO, mInfo);
677        savedState.putParcelableArrayList(KEY_SCANRESULTCACHE,
678                new ArrayList<ScanResult>(mScanResultCache.snapshot().values()));
679        if (mNetworkInfo != null) {
680            savedState.putParcelable(KEY_NETWORKINFO, mNetworkInfo);
681        }
682    }
683
684    public void setListener(AccessPointListener listener) {
685        mAccessPointListener = listener;
686    }
687
688    boolean update(ScanResult result) {
689        if (matches(result)) {
690            /* Update the LRU timestamp, if BSSID exists */
691            mScanResultCache.get(result.BSSID);
692
693            /* Add or update the scan result for the BSSID */
694            mScanResultCache.put(result.BSSID, result);
695
696            int oldLevel = getLevel();
697            int oldRssi = getRssi();
698            mSeen = getSeen();
699            mRssi = (getRssi() + oldRssi)/2;
700            int newLevel = getLevel();
701
702            if (newLevel > 0 && newLevel != oldLevel && mAccessPointListener != null) {
703                mAccessPointListener.onLevelChanged(this);
704            }
705            // This flag only comes from scans, is not easily saved in config
706            if (security == SECURITY_PSK) {
707                pskType = getPskType(result);
708            }
709
710            if (mAccessPointListener != null) {
711                mAccessPointListener.onAccessPointChanged(this);
712            }
713
714            return true;
715        }
716        return false;
717    }
718
719    boolean update(WifiConfiguration config, WifiInfo info, NetworkInfo networkInfo) {
720        boolean reorder = false;
721        if (info != null && isInfoForThisAccessPoint(config, info)) {
722            reorder = (mInfo == null);
723            mRssi = info.getRssi();
724            mInfo = info;
725            mNetworkInfo = networkInfo;
726            if (mAccessPointListener != null) {
727                mAccessPointListener.onAccessPointChanged(this);
728            }
729        } else if (mInfo != null) {
730            reorder = true;
731            mInfo = null;
732            mNetworkInfo = null;
733            if (mAccessPointListener != null) {
734                mAccessPointListener.onAccessPointChanged(this);
735            }
736        }
737        return reorder;
738    }
739
740    void update(WifiConfiguration config) {
741        mConfig = config;
742        networkId = config.networkId;
743        if (mAccessPointListener != null) {
744            mAccessPointListener.onAccessPointChanged(this);
745        }
746    }
747
748    void setRssi(int rssi) {
749        mRssi = rssi;
750    }
751
752    public static String getSummary(Context context, String ssid, DetailedState state,
753            boolean isEphemeral, String passpointProvider) {
754        if (state == DetailedState.CONNECTED && ssid == null) {
755            if (TextUtils.isEmpty(passpointProvider) == false) {
756                // Special case for connected + passpoint networks.
757                String format = context.getString(R.string.connected_via_passpoint);
758                return String.format(format, passpointProvider);
759            } else if (isEphemeral) {
760                // Special case for connected + ephemeral networks.
761                return context.getString(R.string.connected_via_wfa);
762            }
763        }
764
765        // Case when there is wifi connected without internet connectivity.
766        final ConnectivityManager cm = (ConnectivityManager)
767                context.getSystemService(Context.CONNECTIVITY_SERVICE);
768        if (state == DetailedState.CONNECTED) {
769            IWifiManager wifiManager = IWifiManager.Stub.asInterface(
770                    ServiceManager.getService(Context.WIFI_SERVICE));
771            Network nw;
772
773            try {
774                nw = wifiManager.getCurrentNetwork();
775            } catch (RemoteException e) {
776                nw = null;
777            }
778            NetworkCapabilities nc = cm.getNetworkCapabilities(nw);
779            if (nc != null && !nc.hasCapability(nc.NET_CAPABILITY_VALIDATED)) {
780                return context.getString(R.string.wifi_connected_no_internet);
781            }
782        }
783
784        String[] formats = context.getResources().getStringArray((ssid == null)
785                ? R.array.wifi_status : R.array.wifi_status_with_ssid);
786        int index = state.ordinal();
787
788        if (index >= formats.length || formats[index].length() == 0) {
789            return "";
790        }
791        return String.format(formats[index], ssid);
792    }
793
794    public static String getSummary(Context context, DetailedState state, boolean isEphemeral) {
795        return getSummary(context, null, state, isEphemeral, null);
796    }
797
798    public static String getSummary(Context context, DetailedState state, boolean isEphemeral,
799            String passpointProvider) {
800        return getSummary(context, null, state, isEphemeral, passpointProvider);
801    }
802
803    public static String convertToQuotedString(String string) {
804        return "\"" + string + "\"";
805    }
806
807    private static int getPskType(ScanResult result) {
808        boolean wpa = result.capabilities.contains("WPA-PSK");
809        boolean wpa2 = result.capabilities.contains("WPA2-PSK");
810        if (wpa2 && wpa) {
811            return PSK_WPA_WPA2;
812        } else if (wpa2) {
813            return PSK_WPA2;
814        } else if (wpa) {
815            return PSK_WPA;
816        } else {
817            Log.w(TAG, "Received abnormal flag string: " + result.capabilities);
818            return PSK_UNKNOWN;
819        }
820    }
821
822    private static int getSecurity(ScanResult result) {
823        if (result.capabilities.contains("WEP")) {
824            return SECURITY_WEP;
825        } else if (result.capabilities.contains("PSK")) {
826            return SECURITY_PSK;
827        } else if (result.capabilities.contains("EAP")) {
828            return SECURITY_EAP;
829        }
830        return SECURITY_NONE;
831    }
832
833    static int getSecurity(WifiConfiguration config) {
834        if (config.allowedKeyManagement.get(KeyMgmt.WPA_PSK)) {
835            return SECURITY_PSK;
836        }
837        if (config.allowedKeyManagement.get(KeyMgmt.WPA_EAP) ||
838                config.allowedKeyManagement.get(KeyMgmt.IEEE8021X)) {
839            return SECURITY_EAP;
840        }
841        return (config.wepKeys[0] != null) ? SECURITY_WEP : SECURITY_NONE;
842    }
843
844    public static String securityToString(int security, int pskType) {
845        if (security == SECURITY_WEP) {
846            return "WEP";
847        } else if (security == SECURITY_PSK) {
848            if (pskType == PSK_WPA) {
849                return "WPA";
850            } else if (pskType == PSK_WPA2) {
851                return "WPA2";
852            } else if (pskType == PSK_WPA_WPA2) {
853                return "WPA_WPA2";
854            }
855            return "PSK";
856        } else if (security == SECURITY_EAP) {
857            return "EAP";
858        }
859        return "NONE";
860    }
861
862    static String removeDoubleQuotes(String string) {
863        if (TextUtils.isEmpty(string)) {
864            return "";
865        }
866        int length = string.length();
867        if ((length > 1) && (string.charAt(0) == '"')
868                && (string.charAt(length - 1) == '"')) {
869            return string.substring(1, length - 1);
870        }
871        return string;
872    }
873
874    public interface AccessPointListener {
875        void onAccessPointChanged(AccessPoint accessPoint);
876        void onLevelChanged(AccessPoint accessPoint);
877    }
878}
879