Android中Wifi网络配置信息的保存加载与更改—WifiConfigStore.java解析

我们先看源码中对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();
        
    }

关注公众号,获取更多开发必备知识
在这里插入图片描述

  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值