Wifiソース学習のwifiリスト


wifiシリーズブログアドレス:
Wifiソース学習(Android 5.1)のwifiスイッチWifiソース学習(Android 5.1)のwifi optionItem Wifiソース学習(Android 5.1)のwifiリスト
Wifiリスト:
Settings\src\com\android\settings\wifi\WifiSettings.java
まずこの類を見てみましょう.
public class WifiSettings extends RestrictedSettingsFragment
        implements DialogInterface.OnClickListener, Indexable  {

Fragmentを受け継いで、それはFragmentのライフサイクル思想に従って勉強すればいいのです.
@Override
public void onResume() {
    final Activity activity = getActivity();
    super.onResume();
    if (mWifiEnabler != null) {
        mWifiEnabler.resume(activity);
    }
    //         ,       ?   
    activity.registerReceiver(mReceiver, mFilter);
    //    
    updateAccessPoints();
}

//登録されたブロードキャスト、ソースコードでこれらのブロードキャストに基づいて、インタフェースの表示を動的に変更
mFilter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
mFilter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
mFilter.addAction(WifiManager.NETWORK_IDS_CHANGED_ACTION);
mFilter.addAction(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION);
mFilter.addAction(WifiManager.CONFIGURED_NETWORKS_CHANGED_ACTION);
mFilter.addAction(WifiManager.LINK_CONFIGURATION_CHANGED_ACTION);
mFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
mFilter.addAction(WifiManager.RSSI_CHANGED_ACTION);

現在のwifiの状態に基づいてインタフェースを更新//単独で抽出して単純にwifiの状態に基づいてインタフェースを更新する方法を書くと、多重化が容易になります.
/**
 * Shows the latest access points available with supplemental information like
 * the strength of network and the security for it.
 */
private void updateAccessPoints() {
    // Safeguard from some delayed event handling
    if (getActivity() == null) return;
    if (isUiRestricted()) {
        addMessagePreference(R.string.wifi_empty_list_user_restricted);
        return;
    }
    //   wifi  
    final int wifiState = mWifiManager.getWifiState();
    //when we update the screen, check if verbose logging has been turned on or off
    mVerboseLogging = mWifiManager.getVerboseLoggingLevel();
    switch (wifiState) {
        // wifi      
        case WifiManager.WIFI_STATE_ENABLED:
            // AccessPoints are automatically sorted with TreeSet.
            //     
            final Collection accessPoints =
                    constructAccessPoints(getActivity(), mWifiManager, mLastInfo,
                            mLastNetworkInfo);
            //     
            getPreferenceScreen().removeAll();
            if (accessPoints.size() == 0) {
                //            WiFi   
                addMessagePreference(R.string.wifi_empty_list_wifi_on);
            }
            //       
            for (AccessPoint accessPoint : accessPoints) {
                // Ignore access points that are out of range.
                if (accessPoint.getLevel() != -1) {
                    getPreferenceScreen().addPreference(accessPoint);
                }
            }
            break;
        // wifi     ...
        case WifiManager.WIFI_STATE_ENABLING:
            getPreferenceScreen().removeAll();
            break;
        // wifi     ...
        case WifiManager.WIFI_STATE_DISABLING:
            addMessagePreference(R.string.wifi_stopping);
            break;
        // wifi      
        case WifiManager.WIFI_STATE_DISABLED:
            setOffMessage();
            break;
    }
}

//wifi情報の入手方法
/** Returns sorted list of access points */
private static List constructAccessPoints(Context context,
        WifiManager wifiManager, WifiInfo lastInfo, NetworkInfo lastNetworkInfo) {
    ArrayList accessPoints = new ArrayList();
    /** Lookup table to more quickly update AccessPoints by only considering objects with the
     * correct SSID.  Maps SSID -> List of AccessPoints with the given SSID.  */
    Multimap apMap = new Multimap();
      //           wifi
    final List configs = wifiManager.getConfiguredNetworks();
    if (configs != null) {
        // Update "Saved Networks" menu option.
        if (savedNetworksExist != (configs.size() > 0)) {
            savedNetworksExist = !savedNetworksExist;
            if (context instanceof Activity) {
                ((Activity) context).invalidateOptionsMenu();
            }
        }
        for (WifiConfiguration config : configs) {
            if (config.selfAdded && config.numAssociation == 0) {
                continue;
            }
            AccessPoint accessPoint = new AccessPoint(context, config);
            if (lastInfo != null && lastNetworkInfo != null) {
                accessPoint.update(lastInfo, lastNetworkInfo);
            }
            accessPoints.add(accessPoint);
            apMap.put(accessPoint.ssid, accessPoint);
        }
    }
    //        wifi   
    final List results = wifiManager.getScanResults();
    if (results != null) {
        for (ScanResult result : results) {
            // Ignore hidden and ad-hoc networks.
            if (result.SSID == null || result.SSID.length() == 0 ||
                    result.capabilities.contains("[IBSS]")) {
                continue;
            }
            boolean found = false;
            //       ,     ,        ,    apMap      
            for (AccessPoint accessPoint : apMap.getAll(result.SSID)) {
                if (accessPoint.update(result))
                    found = true;
            }
            if (!found) {
                //     ,        ,    
                AccessPoint accessPoint = new AccessPoint(context, result);
                if (lastInfo != null && lastNetworkInfo != null) {
                    accessPoint.update(lastInfo, lastNetworkInfo);
                }
                accessPoints.add(accessPoint);
                //     apMap  
                apMap.put(accessPoint.ssid, accessPoint);
            }
        }
    }
    // Pre-sort accessPoints to speed preference insertion
    //  wifi    (        ,    )
    Collections.sort(accessPoints);
    return accessPoints;
}

wifiリストの生成ソース分析はしばらくここまでです.