1 /*
2  * Copyright (C) 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.server.wifi;
18 
19 import android.annotation.Nullable;
20 import android.content.Context;
21 import android.net.wifi.SoftApConfiguration;
22 
23 import com.android.server.wifi.util.SettingsMigrationDataHolder;
24 import com.android.server.wifi.util.WifiConfigStoreEncryptionUtil;
25 import com.android.server.wifi.util.XmlUtil;
26 
27 import org.xmlpull.v1.XmlPullParser;
28 import org.xmlpull.v1.XmlPullParserException;
29 import org.xmlpull.v1.XmlSerializer;
30 
31 import java.io.IOException;
32 
33 /**
34  * Store data for SoftAp
35  */
36 public class SoftApStoreData implements WifiConfigStore.StoreData {
37     private static final String TAG = "SoftApStoreData";
38     private static final String XML_TAG_SECTION_HEADER_SOFTAP = "SoftAp";
39 
40     private final Context mContext;
41     private final SettingsMigrationDataHolder mSettingsMigrationDataHolder;
42     private final DataSource mDataSource;
43 
44     /**
45      * Interface define the data source for the notifier store data.
46      */
47     public interface DataSource {
48         /**
49          * Retrieve the SoftAp configuration from the data source to serialize them to disk.
50          *
51          * @return {@link SoftApConfiguration} Instance of SoftApConfiguration.
52          */
toSerialize()53         SoftApConfiguration toSerialize();
54 
55         /**
56          * Set the SoftAp configuration in the data source after serializing them from disk.
57          *
58          * @param config {@link SoftApConfiguration} Instance of SoftApConfiguration.
59          */
fromDeserialized(SoftApConfiguration config)60         void fromDeserialized(SoftApConfiguration config);
61 
62         /**
63          * Clear internal data structure in preparation for user switch or initial store read.
64          */
reset()65         void reset();
66 
67         /**
68          * Indicates whether there is new data to serialize.
69          */
hasNewDataToSerialize()70         boolean hasNewDataToSerialize();
71     }
72 
73     /**
74      * Creates the SSID Set store data.
75      *
76      * @param dataSource The DataSource that implements the update and retrieval of the SSID set.
77      */
SoftApStoreData(Context context, SettingsMigrationDataHolder settingsMigrationDataHolder, DataSource dataSource)78     SoftApStoreData(Context context, SettingsMigrationDataHolder settingsMigrationDataHolder,
79             DataSource dataSource) {
80         mContext = context;
81         mSettingsMigrationDataHolder = settingsMigrationDataHolder;
82         mDataSource = dataSource;
83     }
84 
85     @Override
serializeData(XmlSerializer out, @Nullable WifiConfigStoreEncryptionUtil encryptionUtil)86     public void serializeData(XmlSerializer out,
87             @Nullable WifiConfigStoreEncryptionUtil encryptionUtil)
88             throws XmlPullParserException, IOException {
89         SoftApConfiguration softApConfig = mDataSource.toSerialize();
90         if (softApConfig != null) {
91             XmlUtil.SoftApConfigurationXmlUtil.writeSoftApConfigurationToXml(out, softApConfig,
92                     encryptionUtil);
93         }
94     }
95 
96     @Override
deserializeData(XmlPullParser in, int outerTagDepth, @WifiConfigStore.Version int version, @Nullable WifiConfigStoreEncryptionUtil encryptionUtil)97     public void deserializeData(XmlPullParser in, int outerTagDepth,
98             @WifiConfigStore.Version int version,
99             @Nullable WifiConfigStoreEncryptionUtil encryptionUtil)
100             throws XmlPullParserException, IOException {
101         // Ignore empty reads.
102         if (in == null) {
103             return;
104         }
105 
106         SoftApConfiguration softApConfig = XmlUtil.SoftApConfigurationXmlUtil.parseFromXml(
107                 in, outerTagDepth, mSettingsMigrationDataHolder,
108                 version >= WifiConfigStore.ENCRYPT_CREDENTIALS_CONFIG_STORE_DATA_VERSION,
109                 encryptionUtil);
110         if (softApConfig != null) {
111             mDataSource.fromDeserialized(softApConfig);
112         }
113     }
114 
115     @Override
resetData()116     public void resetData() {
117         mDataSource.reset();
118     }
119 
120     @Override
hasNewDataToSerialize()121     public boolean hasNewDataToSerialize() {
122         return mDataSource.hasNewDataToSerialize();
123     }
124 
125     @Override
getName()126     public String getName() {
127         return XML_TAG_SECTION_HEADER_SOFTAP;
128     }
129 
130     @Override
getStoreFileId()131     public @WifiConfigStore.StoreFileId int getStoreFileId() {
132         return WifiConfigStore.STORE_FILE_SHARED_SOFTAP; // Shared softap store.
133     }
134 }
135