Wifi源码学习(Android5.1)之wifi开关
Wifi源码学习(Android5.1)之wifi optionItem
Wifi源码学习(Android5.1)之wifi列表
Wifi 列表:
首先看一下这个类。
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 列表的生成源码分析暂时就到这儿了。