admin管理员组文章数量:1620395
我们先看源码中对WifiConfigStore这个类的解释:
* This class provides the API's to save/load/modify network configurations from a persistent
* store. Uses keystore for certificate/key management operations.
* NOTE: This class should only be used from WifiConfigManager and is not thread-safe!
此类提供API以从持久性保存/加载/修改网络配置商店。 使用密钥库进行证书/密钥管理操作。
注意:此类只能在WifiConfigManager中使用,并且不是线程安全的!
一般WifiConfigManager中才会调用WifiConfigStore的方法,比如要加载已保存过的网络时,要迁移保存过的网络数据时,都会调用WifiConfigStore的方法。
WifiConfigManager代码路径: /frameworks/opt/net/wifi/service/java/com/android/server/wifi/WifiConfigManager.java
下面我们看WifiConfigManager的代码:
loadFromStore函数就是从Store中加载列表,也就是加载已经保存过的热点信息。
如果发现没有相应的文件,则创建。(这里说明一下,wifi保存的热点信息是存储在一个文件中的,这个文件不是一开始就存在的,而是设备第一次保存网络信息的时候才开始创建的。)
public boolean loadFromStore() {
if (mDeferredUserUnlockRead) {
Log.i(TAG, "Handling user unlock before loading from store.");
mWifiConfigStore.setUserStore(WifiConfigStore.createUserFile(mCurrentUserId)); //如果没有存储已保存网络的文件,则创建。
mDeferredUserUnlockRead = false;
}
if (!mWifiConfigStore.areStoresPresent()) {
if (!mWifiConfigStoreLegacy.areStoresPresent()) {
mPendingStoreRead = false;
}
return true;
}
try {
mWifiConfigStore.read(); //读取信息
.................
loadInternalData(mNetworkListStoreData.getSharedConfigurations(),
mNetworkListStoreData.getUserConfigurations(),
mDeletedEphemeralSsidsStoreData.getSsidList());
return true;
}
下面我们来看WifiConfigStore:主要看存储热点信息的文件的创建。
public static StoreFile createUserFile(int userId) {
return createFile(Environment.getDataMiscCeDirectory(userId));
}
由下面函数可以看出,要创建的文件是data/misc/wifi目录下面的WifiConfigStore.xml文件。而我们保存过的wifi信息,正是保存在这个xml文件中,以前是保存在wpa_supplicant.conf文件中的。
private static StoreFile createFile(File storeBaseDir) {
File storeDir = new File(storeBaseDir, STORE_DIRECTORY_NAME); //文件路径
if (!storeDir.exists()) {
if (!storeDir.mkdir()) {
Log.w(TAG, "Could not create store directory " + storeDir);
}
}
return new StoreFile(new File(storeDir, STORE_FILE_NAME)); //文件名
}
private static final String STORE_FILE_NAME = "WifiConfigStore.xml";
private static final String STORE_DIRECTORY_NAME = "wifi";
下面是WIfiConfigStore中的其他几个重要函数:
read()函数主要是从store中读取数据,并对数据进行解析。
public void read() throws XmlPullParserException, IOException {
// Reset both share and user store data.
resetStoreData(true);
if (mUserStore != null) {
resetStoreData(false);
}
long readStartTime = mClock.getElapsedSinceBootMillis();
byte[] sharedDataBytes = mSharedStore.readRawData();
byte[] userDataBytes = null;
if (mUserStore != null) {
userDataBytes = mUserStore.readRawData();
}
long readTime = mClock.getElapsedSinceBootMillis() - readStartTime;
Log.d(TAG, "Reading from stores completed in " + readTime + " ms.");
deserializeData(sharedDataBytes, true);
if (mUserStore != null) {
deserializeData(userDataBytes, false);
}
}
write()函数主要是往store里写入数据,也就是你输入wifi密码以后点击保存时就会执行这一步。
public void write(boolean forceSync)
throws XmlPullParserException, IOException {
// Serialize the provided data and send it to the respective stores. The actual write will
// be performed later depending on the |forceSync| flag .
byte[] sharedDataBytes = serializeData(true);
mSharedStore.storeRawDataToWrite(sharedDataBytes);
if (mUserStore != null) {
byte[] userDataBytes = serializeData(false);
mUserStore.storeRawDataToWrite(userDataBytes);
}
// Every write provides a new snapshot to be persisted, so |forceSync| flag overrides any
// pending buffer writes.
if (forceSync) {
writeBufferedData();
} else {
startBufferedWriteAlarm();
}
}
在WifiConfigStore中还有一个重要的接口:StoreData
这个接口主要用来实现序列化和反序列化数据,在read和write的时候会用到。
public interface StoreData {
void serializeData(XmlSerializer out, boolean shared) throws XmlPullParserException, IOException;
void deserializeData(@Nullable XmlPullParser in, int outerTagDepth, boolean shared) throws XmlPullParserException, IOException;
void resetData(boolean shared);
String getName();
boolean supportShareData();
}
关注公众号,获取更多开发必备知识
版权声明:本文标题:Android中Wifi网络配置信息的保存加载与更改—WifiConfigStore.java解析 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://m.elefans.com/dianzi/1728817407a1175102.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论