summaryrefslogtreecommitdiffstats
path: root/src/com/android/settings/wifi/AccessPointState.java
blob: c22495434f431ec48f0e6b431b40f1343e23c2da (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
/*
 * Copyright (C) 2007 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.settings.wifi;

import com.android.settings.R;

import android.content.Context;
import android.net.NetworkInfo;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiConfiguration.AuthAlgorithm;
import android.net.wifi.WifiConfiguration.GroupCipher;
import android.net.wifi.WifiConfiguration.KeyMgmt;
import android.net.wifi.WifiConfiguration.PairwiseCipher;
import android.net.wifi.WifiConfiguration.Protocol;
import android.os.Parcel;
import android.os.Parcelable;
import android.text.TextUtils;
import android.util.Log;

public final class AccessPointState implements Comparable<AccessPointState>, Parcelable {

    private static final String TAG = "AccessPointState";
    
    // Constants used for different security types
    public static final String WPA2 = "WPA2";
    public static final String WPA = "WPA";
    public static final String WEP = "WEP";
    public static final String OPEN = "Open";

    /** String present in capabilities if the scan result is ad-hoc */
    private static final String ADHOC_CAPABILITY = "[IBSS]";
    /** String present in capabilities if the scan result is enterprise secured */
    private static final String ENTERPRISE_CAPABILITY = "-EAP-";
    
    // Localized strings for different security types
    private static String LOCALIZED_WPA2;
    private static String LOCALIZED_WPA;
    private static String LOCALIZED_WEP;
    private static String LOCALIZED_OPEN;
    private static String LOCALIZED_UNKNOWN;
    private static String LOCALIZED_VERBOSE_WPA2;
    private static String LOCALIZED_VERBOSE_WPA;
    private static String LOCALIZED_VERBOSE_WEP;
    private static String LOCALIZED_VERBOSE_OPEN;

    
    // Localized strings for various messages
    private static String SUMMARY_NOT_IN_RANGE;
    private static String SUMMARY_REMEMBERED;
    private static String SUMMARY_CONNECTION_FAILED;
    
    public static final String BSSID_ANY = "any";
    public static final int NETWORK_ID_NOT_SET = -1;
    /** This should be used with care! */
    static final int NETWORK_ID_ANY = -2;
    
    public static final int MATCH_NONE = 0;
    public static final int MATCH_WEAK = 1;
    public static final int MATCH_STRONG = 2;
    public static final int MATCH_EXACT = 3;
    
    // Don't set these directly, use the setters.
    public int networkId;
    public int priority;
    public boolean hiddenSsid;
    public int linkSpeed;
    public int ipAddress;
    public String bssid;
    public String ssid;
    public int signal;
    public boolean primary;
    public boolean seen;
    public boolean configured;
    public NetworkInfo.DetailedState status;
    public String security;
    public boolean disabled;
    
    /**
     * Use this for sorting based on signal strength. It is a heavily-damped
     * time-averaged weighted signal.
     */
    private float signalForSorting = Float.MIN_VALUE;
    
    private static final float DAMPING_FACTOR = 0.2f;
    
    /**
     * This will be a user entered password, and NOT taken from wpa_supplicant
     * (since it would give us *)
     */
    private String mPassword; 
    private boolean mConfigHadPassword;
    
    public static final int WEP_PASSWORD_AUTO = 0;
    public static final int WEP_PASSWORD_ASCII = 1;
    public static final int WEP_PASSWORD_HEX = 2;
    private int mWepPasswordType;
    
    private Context mContext;

    /**
     * If > 0, don't refresh (changes are being batched), use
     * {@link #blockRefresh()} and {@link #unblockRefresh()} only.
     */
    private int mBlockRefresh;
    /**
     * This will be set by {@link #requestRefresh} and shouldn't be written to
     * elsewhere.
     */
    private boolean mNeedsRefresh;    
    
    private AccessPointStateCallback mCallback;
    
    private StringBuilder mSummaryBuilder = new StringBuilder();
    
    interface AccessPointStateCallback {
        void refreshAccessPointState();
    }
    
    public AccessPointState(Context context) {
        this();
        
        setContext(context);
    }

    private AccessPointState() {
        bssid = BSSID_ANY;
        ssid = "";
        networkId = NETWORK_ID_NOT_SET;
        hiddenSsid = false;
    }

    void setContext(Context context) {
        mContext = context;
        setStrings();
    }

    private void setStrings() {
        final Context c = mContext;

        if (SUMMARY_NOT_IN_RANGE == null && c != null) {
            SUMMARY_NOT_IN_RANGE = c.getString(R.string.summary_not_in_range);
            SUMMARY_REMEMBERED = c.getString(R.string.summary_remembered);
            SUMMARY_CONNECTION_FAILED = c.getString(R.string.summary_connection_failed);
            
            LOCALIZED_OPEN = c.getString(R.string.wifi_security_open);
            LOCALIZED_WEP = c.getString(R.string.wifi_security_wep);
            LOCALIZED_WPA = c.getString(R.string.wifi_security_wpa);
            LOCALIZED_WPA2 = c.getString(R.string.wifi_security_wpa2);

            LOCALIZED_VERBOSE_OPEN = c.getString(R.string.wifi_security_verbose_open);
            LOCALIZED_VERBOSE_WEP = c.getString(R.string.wifi_security_verbose_wep);
            LOCALIZED_VERBOSE_WPA = c.getString(R.string.wifi_security_verbose_wpa);
            LOCALIZED_VERBOSE_WPA2 = c.getString(R.string.wifi_security_verbose_wpa2);

            LOCALIZED_UNKNOWN = c.getString(R.string.wifi_security_unknown);
        }
    }
    
    public void setNetworkId(int networkId) {
        this.networkId = networkId;
    }
    
    public void setBssid(String bssid) {
        if (bssid != null) {
            // If the BSSID is a wildcard, do NOT let a specific BSSID replace it
            if (!this.bssid.equals(BSSID_ANY)) {
                this.bssid = bssid;
            }
        }
    }

    private String getWpaSupplicantBssid() {
        return bssid.equals(BSSID_ANY) ? null : bssid;
    }
    
    public static String convertToQuotedString(String string) {
        if (TextUtils.isEmpty(string)) {
            return "";
        }
        
        final int lastPos = string.length() - 1;
        if (lastPos < 0 || (string.charAt(0) == '"' && string.charAt(lastPos) == '"')) {
            return string;
        }
        
        return "\"" + string + "\"";
    }
    
    public void setPrimary(boolean primary) {
        if (this.primary != primary) {
            this.primary = primary;
            requestRefresh();
        }
    }
    
    public void setSeen(boolean seen) {
        if (this.seen != seen) {
            this.seen = seen;
            requestRefresh();
        }
    }
    
    public void setDisabled(boolean disabled) {
        if (this.disabled != disabled) {
            this.disabled = disabled;
            requestRefresh();
        }
    }
    
    public void setSignal(int signal) {

        if (signalForSorting == Float.MIN_VALUE) {
            signalForSorting = signal;
        } else {
            signalForSorting = (DAMPING_FACTOR * signal) + ((1-DAMPING_FACTOR) * signalForSorting);
        }

        if (this.signal != signal) {
            this.signal = signal;
            requestRefresh();
        }
    }
    
    public String getHumanReadableSsid() {
        if (TextUtils.isEmpty(ssid)) {
            return "";
        }
        
        final int lastPos = ssid.length() - 1;
        if (ssid.charAt(0) == '"' && ssid.charAt(lastPos) == '"') {
            return ssid.substring(1, lastPos);
        }
        
        return ssid;
    }
    
    public void setSsid(String ssid) {
        if (ssid != null) {
            this.ssid = convertToQuotedString(ssid);
            requestRefresh();
        }
    }
    
    public void setPriority(int priority) {
        if (this.priority != priority) {
            this.priority = priority;
            requestRefresh();
        }
    }

    public void setHiddenSsid(boolean hiddenSsid) {
        if (this.hiddenSsid != hiddenSsid) {
            this.hiddenSsid = hiddenSsid;
            requestRefresh();
        }
    }

    public void setLinkSpeed(int linkSpeed) {
        if (this.linkSpeed != linkSpeed) {
            this.linkSpeed = linkSpeed;
            requestRefresh();
        }
    }
    
    public void setIpAddress(int address) {
        if (ipAddress != address) {
            ipAddress = address;
            requestRefresh();
        }
    }
    
    public void setConfigured(boolean configured) {
        if (this.configured != configured) {
            this.configured = configured;
            requestRefresh();
        }
    }
    
    public void setStatus(NetworkInfo.DetailedState status) {
        if (this.status != status) {
            this.status = status;
            requestRefresh();
        }
    }
    
    public void setSecurity(String security) {
        if (TextUtils.isEmpty(this.security) || !this.security.equals(security)) {
            this.security = security;
            requestRefresh();
        }
    }

    public boolean hasSecurity() {
        return security != null && !security.contains(OPEN);
    }
    
    public String getHumanReadableSecurity() {
        if (security.equals(OPEN)) return LOCALIZED_OPEN;
        else if (security.equals(WEP)) return LOCALIZED_WEP;
        else if (security.equals(WPA)) return LOCALIZED_WPA;
        else if (security.equals(WPA2)) return LOCALIZED_WPA2;
        
        return LOCALIZED_UNKNOWN;
    }
    
    public void updateFromScanResult(ScanResult scanResult) {
        blockRefresh();
        
        // We don't keep specific AP BSSIDs and instead leave that as wildcard
        
        setSeen(true);
        setSsid(scanResult.SSID);
        if (networkId == NETWORK_ID_NOT_SET) {
            // Since ScanResults don't cross-reference network ID, we set it as a wildcard
            setNetworkId(NETWORK_ID_ANY);
        }
        setSignal(scanResult.level);
        setSecurity(getScanResultSecurity(scanResult));
        unblockRefresh();
    }
    
    /**
     * @return The security of a given {@link ScanResult}.
     */
    public static String getScanResultSecurity(ScanResult scanResult) {
        final String cap = scanResult.capabilities;
        final String[] securityModes = { WEP, WPA, WPA2 }; 
        for (int i = securityModes.length - 1; i >= 0; i--) {
            if (cap.contains(securityModes[i])) {
                return securityModes[i];
            }
        }
        
        return OPEN;
    }
    
    /**
     * @return Whether the given ScanResult represents an adhoc network.
     */
    public static boolean isAdhoc(ScanResult scanResult) {
        return scanResult.capabilities.contains(ADHOC_CAPABILITY);
    }
    
    /**
     * @return Whether the given ScanResult has enterprise security.
     */
    public static boolean isEnterprise(ScanResult scanResult) {
        return scanResult.capabilities.contains(ENTERPRISE_CAPABILITY);
    }
    
    public void updateFromWifiConfiguration(WifiConfiguration wifiConfig) {
        if (wifiConfig != null) {
            blockRefresh();
            setBssid(wifiConfig.BSSID);
            setNetworkId(wifiConfig.networkId);
            setPriority(wifiConfig.priority);
            setHiddenSsid(wifiConfig.hiddenSSID);
            setSsid(wifiConfig.SSID);
            setConfigured(true);
            setDisabled(wifiConfig.status == WifiConfiguration.Status.DISABLED);
            parseWifiConfigurationSecurity(wifiConfig);
            unblockRefresh();
        }
    }
    
    public void setPassword(String password) {
        setPassword(password, WEP_PASSWORD_AUTO);
    }
    
    public void setPassword(String password, int wepPasswordType) {
        mPassword = password;
        mWepPasswordType = wepPasswordType;
    }
    
    public boolean hasPassword() {
        return !TextUtils.isEmpty(mPassword) || mConfigHadPassword; 
    }
    
    private static boolean hasPassword(WifiConfiguration wifiConfig) {
        return !TextUtils.isEmpty(wifiConfig.preSharedKey)
                || !TextUtils.isEmpty(wifiConfig.wepKeys[0])
                || !TextUtils.isEmpty(wifiConfig.wepKeys[1])
                || !TextUtils.isEmpty(wifiConfig.wepKeys[2])
                || !TextUtils.isEmpty(wifiConfig.wepKeys[3]);        
    }
    
    private void parseWifiConfigurationSecurity(WifiConfiguration wifiConfig) {
        setSecurity(getWifiConfigurationSecurity(wifiConfig));
        mConfigHadPassword = hasPassword(wifiConfig);
    }
    
    /**
     * @return The security of a given {@link WifiConfiguration}.
     */
    public static String getWifiConfigurationSecurity(WifiConfiguration wifiConfig) {

        if (wifiConfig.allowedKeyManagement.get(KeyMgmt.NONE)) {
            // If we never set group ciphers, wpa_supplicant puts all of them.
            // For open, we don't set group ciphers.
            // For WEP, we specifically only set WEP40 and WEP104, so CCMP
            // and TKIP should not be there.
            if (!wifiConfig.allowedGroupCiphers.get(GroupCipher.CCMP)
                    && (wifiConfig.allowedGroupCiphers.get(GroupCipher.WEP40)
                            || wifiConfig.allowedGroupCiphers.get(GroupCipher.WEP104))) {
                return WEP;
            } else {
                return OPEN;
            }
        } else if (wifiConfig.allowedProtocols.get(Protocol.RSN)) {
            return WPA2;
        } else if (wifiConfig.allowedProtocols.get(Protocol.WPA)) {
            return WPA;
        } else {
            Log.w(TAG, "Unknown security type from WifiConfiguration, falling back on open.");
            return OPEN;
        }
    }
    
    public void updateFromWifiInfo(WifiInfo wifiInfo, NetworkInfo.DetailedState state) {
        if (wifiInfo != null) {
            blockRefresh();
            setBssid(wifiInfo.getBSSID());
            setLinkSpeed(wifiInfo.getLinkSpeed());
            setNetworkId(wifiInfo.getNetworkId());
            setIpAddress(wifiInfo.getIpAddress());
            setSsid(wifiInfo.getSSID());
            if (state != null) {
                setStatus(state);
            }
            setHiddenSsid(wifiInfo.getHiddenSSID());
            unblockRefresh();
        }
    }
    
    /**
     * @return Whether this AP can be connected to at the moment.
     */
    public boolean isConnectable() {
        return !primary && seen;
    }

    /**
     * @return Whether this AP can be forgotten at the moment.
     */
    public boolean isForgetable() {
        return configured;
    }
    
    /**
     * Updates the state as if it were never configured.
     * <p>
     * Note: This will not pass the forget call to the Wi-Fi API.
     */
    public void forget() {
        blockRefresh();
        setConfigured(false);
        setNetworkId(NETWORK_ID_NOT_SET);
        setPrimary(false);
        setStatus(null);
        setDisabled(false);
        unblockRefresh();
    }
    
    public void updateWifiConfiguration(WifiConfiguration config) {
        config.BSSID = getWpaSupplicantBssid();
        config.priority = priority;
        config.hiddenSSID = hiddenSsid;
        config.SSID = convertToQuotedString(ssid);
        
        setupSecurity(config);
    }
    
    private void setupSecurity(WifiConfiguration config) {
        config.allowedAuthAlgorithms.clear();
        config.allowedGroupCiphers.clear();
        config.allowedKeyManagement.clear();
        config.allowedPairwiseCiphers.clear();
        config.allowedProtocols.clear();
        
        if (TextUtils.isEmpty(security)) {
            security = OPEN;
            Log.w(TAG, "Empty security, assuming open");
        }
        
        if (security.equals(WEP)) {
            
            // If password is empty, it should be left untouched
            if (!TextUtils.isEmpty(mPassword)) {
                if (mWepPasswordType == WEP_PASSWORD_AUTO) {
                    if (isHexWepKey(mPassword)) {
                        config.wepKeys[0] = mPassword;
                    } else {
                        config.wepKeys[0] = convertToQuotedString(mPassword);
                    }
                } else {
                    config.wepKeys[0] = mWepPasswordType == WEP_PASSWORD_ASCII
                            ? convertToQuotedString(mPassword)
                            : mPassword;
                }
            }
            
            config.wepTxKeyIndex = 0;
            
            config.allowedAuthAlgorithms.set(AuthAlgorithm.OPEN);
            config.allowedAuthAlgorithms.set(AuthAlgorithm.SHARED);

            config.allowedKeyManagement.set(KeyMgmt.NONE);
            
            config.allowedGroupCiphers.set(GroupCipher.WEP40);
            config.allowedGroupCiphers.set(GroupCipher.WEP104);
            
        } else if (security.equals(WPA) || security.equals(WPA2)){
            config.allowedGroupCiphers.set(GroupCipher.TKIP);
            config.allowedGroupCiphers.set(GroupCipher.CCMP);
            
            config.allowedKeyManagement.set(KeyMgmt.WPA_PSK);
            
            config.allowedPairwiseCiphers.set(PairwiseCipher.CCMP);
            config.allowedPairwiseCiphers.set(PairwiseCipher.TKIP);

            config.allowedProtocols.set(security.equals(WPA2) ? Protocol.RSN : Protocol.WPA);
            
            // If password is empty, it should be left untouched
            if (!TextUtils.isEmpty(mPassword)) {
                if (mPassword.length() == 64 && isHex(mPassword)) {
                    // Goes unquoted as hex
                    config.preSharedKey = mPassword;
                } else {
                    // Goes quoted as ASCII
                    config.preSharedKey = convertToQuotedString(mPassword);
                }
            }
            
        } else if (security.equals(OPEN)) {
            config.allowedKeyManagement.set(KeyMgmt.NONE);
        }
    }
    
    private static boolean isHexWepKey(String wepKey) {
        final int len = wepKey.length();
        
        // WEP-40, WEP-104, and some vendors using 256-bit WEP (WEP-232?)
        if (len != 10 && len != 26 && len != 58) {
            return false;
        }
        
        return isHex(wepKey);
    }
    
    private static boolean isHex(String key) {
        for (int i = key.length() - 1; i >= 0; i--) {
            final char c = key.charAt(i);
            if (!(c >= '0' && c <= '9' || c >= 'A' && c <= 'F' || c >= 'a' && c <= 'f')) {
                return false;
            }
        }
        
        return true;
    }
    
    public void setCallback(AccessPointStateCallback callback) {
        mCallback = callback;
    }
    
    void blockRefresh() {
        mBlockRefresh++;
    }
    
    void unblockRefresh() {
        if (--mBlockRefresh == 0 && mNeedsRefresh) {
            requestRefresh();
        }
    }
    
    private void requestRefresh() {
        if (mBlockRefresh > 0) {
            mNeedsRefresh = true;
            return;
        }
        
        if (mCallback != null) {
            mCallback.refreshAccessPointState();
        }
        
        mNeedsRefresh = false;
    }
    
    /**
     * {@inheritDoc}
     * @see #hashCode()
     * @see #equals(Object)
     */
    public int matches(int otherNetworkId, String otherBssid, String otherSsid,
            String otherSecurity) {
        
        // Whenever this method is touched, please ensure #equals and #hashCode
        // still work with the changes here!
        
        if (otherSsid == null) {
            if (WifiLayer.LOGV) {
                Log.w(TAG, "BSSID: " + otherBssid + ", SSID: " + otherSsid);
            }
            return MATCH_NONE;
        }

        /*
         * If we both have 'security' set, it must match (an open network still
         * has 'security' set to OPEN)
         */
        if (security != null && otherSecurity != null) {
            if (!security.equals(otherSecurity)) {
                return MATCH_NONE;
            }
        }
        
        // WifiConfiguration gives an empty bssid as a BSSID wildcard
        if (TextUtils.isEmpty(otherBssid)) {
            otherBssid = AccessPointState.BSSID_ANY;
        }

        final boolean networkIdMatches = networkId == otherNetworkId;
        if (!networkIdMatches && networkId != NETWORK_ID_ANY && otherNetworkId != NETWORK_ID_ANY) {
            // Network IDs don't match (e.g., 1 & 2 or unset & 1) and neither is a wildcard
            return MATCH_NONE;
        }
        
        if (networkIdMatches && otherNetworkId != NETWORK_ID_NOT_SET
                && otherNetworkId != NETWORK_ID_ANY) {
            // Network ID matches (they're set to the same ID)
            return MATCH_EXACT;
        }
        
        // So now, network IDs aren't set or at least one is a wildcard 
        
        final boolean bssidMatches = bssid.equals(otherBssid);
        final boolean otherBssidIsWildcard = otherBssid.equals(BSSID_ANY);
        if (bssidMatches && !otherBssidIsWildcard) {
            // BSSID matches (and neither is a wildcard)
            return MATCH_STRONG;
        }

        if (!bssidMatches && !bssid.equals(BSSID_ANY) && !otherBssidIsWildcard) {
            // BSSIDs don't match (e.g., 00:24:21:21:42:12 & 42:12:44:21:22:52)
            // and neither is a wildcard
            return MATCH_NONE;
        }
        
        // So now, BSSIDs are both wildcards
        
        final boolean ssidMatches = ssid.equals(otherSsid); 
        if (ssidMatches) {
            // SSID matches
            return MATCH_WEAK;
        }

        return MATCH_NONE;
    }
    
    /**
     * {@inheritDoc}
     * @see #matches(int, String, String)
     * @see #equals(Object)
     */
    @Override
    public int hashCode() {
        // Two equal() objects must have same hashCode.
        // With Wi-Fi, the broadest match is if two SSIDs are the same.  The finer-grained matches
        // imply this (for example, the same network IDs means the same WifiConfiguration which
        // means the same SSID).
        // See #matches for the exact matching algorithm we use.
        return ssid != null ? ssid.hashCode() : 0;
    }

    /**
     * {@inheritDoc}
     * @see #matches(int, String, String)
     * @see #hashCode()
     */
    @Override
    public boolean equals(Object o) {
        if (!o.getClass().equals(getClass())) {
            return false;
        }
        
        final AccessPointState other = (AccessPointState) o;

        // To see which conditions cause two AccessPointStates to be equal, see
        // where #matches returns MATCH_WEAK or greater.
        
        return matches(other.networkId, other.bssid, other.ssid, other.security) >= MATCH_WEAK;
    }

    public int matchesWifiConfiguration(WifiConfiguration wifiConfig) {
        String security = getWifiConfigurationSecurity(wifiConfig);
        return matches(wifiConfig.networkId, wifiConfig.BSSID, wifiConfig.SSID, security);
    }
    
    String getSummarizedStatus() {
        StringBuilder sb = mSummaryBuilder;
        sb.delete(0, sb.length());
        
        if (primary && status != null) {
            buildSummary(sb, WifiStatus.getPrintable(mContext, status), true);
            
        } else if (!seen) {
            buildSummary(sb, SUMMARY_NOT_IN_RANGE, true);

            // Remembered comes second in this case
            if (!primary && configured) {
                buildSummary(sb, SUMMARY_REMEMBERED, true);
            }
            
        } else {
            if (configured && disabled) {
                // The connection failure overrides all in this case
                return SUMMARY_CONNECTION_FAILED;
            }

            // Remembered comes first in this case
            if (!primary && configured) {
                buildSummary(sb, SUMMARY_REMEMBERED, true);
            }
            
            // If it is seen (and not the primary), show the security type
            String verboseSecurity = getVerboseSecurity();
            if (verboseSecurity != null) {
                buildSummary(sb, verboseSecurity, true);
            }
        }
        
        return sb.toString();
    }
    
    private String getVerboseSecurity() {
        if (WEP.equals(security)) {
            return LOCALIZED_VERBOSE_WEP;
        } else if (WPA.equals(security)) {
            return LOCALIZED_VERBOSE_WPA;
        } else if (WPA2.equals(security)) {
            return LOCALIZED_VERBOSE_WPA2;
        } else if (OPEN.equals(security)) {
            return LOCALIZED_VERBOSE_OPEN;
        } else {
            return null;
        }
    }
    
    private void buildSummary(StringBuilder sb, String string, boolean autoLowerCaseFirstLetter) {
        if (sb.length() == 0) {
            sb.append(string);
        } else {
            sb.append(", ");
            if (autoLowerCaseFirstLetter) {
                // Convert first letter to lowercase
                sb.append(Character.toLowerCase(string.charAt(0))).append(string, 1,
                        string.length());
            } else {
                sb.append(string);
            }
        }
    }
    
    public int compareTo(AccessPointState other) {
        // This ranks the states for displaying in the AP list, not for
        // connecting to (wpa_supplicant does that using the WifiConfiguration's
        // priority field).
        
        // Clarity > efficiency, of this logic:
        int comparison;
        
        // Primary
        comparison = (other.primary ? 1 : 0) - (primary ? 1 : 0);
        if (comparison != 0) return comparison;
        
        // Currently seen (similar to, but not always the same as within range)
        comparison = (other.seen ? 1 : 0) - (seen ? 1 : 0);
        if (comparison != 0) return comparison;

        // Configured
        comparison = (other.configured ? 1 : 0) - (configured ? 1 : 0);
        if (comparison != 0) return comparison;

        if (!configured) {
            // Neither are configured

            // Open network
            comparison = (hasSecurity() ? 1 : 0) - (other.hasSecurity() ? 1 : 0);
            if (comparison != 0) return comparison;
        }

        // Signal strength
        comparison = (int) (other.signalForSorting - signalForSorting);
        if (comparison != 0) return comparison;

        // Alphabetical
        return ssid.compareToIgnoreCase(other.ssid);
    }

    public String toString() {
        return ssid + " (" + bssid + ", " + networkId + ", " + super.toString() + ")";
    }
    
    /** Implement the Parcelable interface */
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(bssid);
        dest.writeInt(configured ? 1 : 0);
        dest.writeInt(ipAddress);
        dest.writeInt(linkSpeed);
        dest.writeInt(networkId);
        dest.writeInt(primary ? 1 : 0);
        dest.writeInt(priority);
        dest.writeInt(hiddenSsid ? 1 : 0);
        dest.writeString(security);
        dest.writeInt(seen ? 1 : 0);
        dest.writeInt(disabled ? 1 : 0);
        dest.writeInt(signal);
        dest.writeString(ssid);
        dest.writeString(status != null ? status.toString() : null);
        dest.writeString(mPassword);
        dest.writeInt(mConfigHadPassword ? 1 : 0);
        dest.writeInt(mWepPasswordType);
    }

    /** Implement the Parcelable interface */
    public int describeContents() {
        return 0;
    }

    /** Implement the Parcelable interface */
    public static final Creator<AccessPointState> CREATOR =
        new Creator<AccessPointState>() {
            public AccessPointState createFromParcel(Parcel in) {
                AccessPointState state = new AccessPointState();
                state.bssid = in.readString();
                state.configured = in.readInt() == 1;
                state.ipAddress = in.readInt();
                state.linkSpeed = in.readInt();
                state.networkId = in.readInt();
                state.primary = in.readInt() == 1;
                state.priority = in.readInt();
                state.hiddenSsid = in.readInt() == 1;
                state.security = in.readString();
                state.seen = in.readInt() == 1;
                state.disabled = in.readInt() == 1;
                state.signal = in.readInt();
                state.ssid = in.readString();
                String statusStr = in.readString();
                if (statusStr != null) {
                    state.status = NetworkInfo.DetailedState.valueOf(statusStr);
                }
                state.mPassword = in.readString();
                state.mConfigHadPassword = in.readInt() == 1;
                state.mWepPasswordType = in.readInt();
                return state;
            }

            public AccessPointState[] newArray(int size) {
                return new AccessPointState[size];
            }
        };

        
}