• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 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.util.Log;
20 
21 import com.android.server.wifi.util.XmlUtil;
22 
23 import org.xmlpull.v1.XmlPullParser;
24 import org.xmlpull.v1.XmlPullParserException;
25 import org.xmlpull.v1.XmlSerializer;
26 
27 import java.io.ByteArrayOutputStream;
28 import java.io.IOException;
29 import java.nio.charset.StandardCharsets;
30 import java.util.HashMap;
31 import java.util.Map;
32 
33 /**
34  * Class used to backup/restore data using the SettingsBackupAgent.
35  * There are 2 symmetric API's exposed here:
36  * 1. retrieveBackupDataFromSettingsConfigStore: Retrieve the configuration data to be backed up.
37  * 2. retrieveSettingsFromBackupData: Restore the configuration using the provided data.
38  * The byte stream to be backed up is XML encoded and versioned to migrate the data easily across
39  * revisions.
40  */
41 public class WifiSettingsBackupRestore {
42     private static final String TAG = "WifiSettingsBackupRestore";
43 
44     public static final String XML_TAG_SECTION_HEADER_WIFI_SETTINGS_DATA =
45             "WifiSettingsSection";
46 
47     private WifiSettingsConfigStore mWifiSettingsConfigStore;
48     private Map<String, WifiSettingsConfigStore.Key> mRestoreSettingsMap = new HashMap<>();
49     /**
50      * Verbose logging flag.
51      */
52     private boolean mVerboseLoggingEnabled = false;
53 
WifiSettingsBackupRestore(WifiSettingsConfigStore settingsConfigStore)54     public WifiSettingsBackupRestore(WifiSettingsConfigStore settingsConfigStore) {
55         mWifiSettingsConfigStore = settingsConfigStore;
56         for (WifiSettingsConfigStore.Key key : mWifiSettingsConfigStore.getAllKeys()) {
57             mRestoreSettingsMap.put(key.key, key);
58         }
59     }
60 
61     /**
62      * Retrieve a byte stream representing the data that needs to be backed up from the
63      * provided WifiSettingsConfigStore.
64      */
retrieveBackupDataFromSettingsConfigStore(XmlSerializer out, ByteArrayOutputStream outputStream)65     public void retrieveBackupDataFromSettingsConfigStore(XmlSerializer out,
66             ByteArrayOutputStream outputStream) {
67         Map<String, Object> backupSettingsMap = new HashMap<>();
68         try {
69             out.setOutput(outputStream, StandardCharsets.UTF_8.name());
70             // Start writing the XML stream.
71             XmlUtil.writeNextSectionStart(out, XML_TAG_SECTION_HEADER_WIFI_SETTINGS_DATA);
72             XmlUtil.writeNextSectionStart(out,
73                     WifiSettingsConfigStore.StoreData.XML_TAG_SECTION_HEADER);
74             // Prepare the setting's map.
75             for (WifiSettingsConfigStore.Key key
76                     : mWifiSettingsConfigStore.getAllBackupRestoreKeys()) {
77                 backupSettingsMap.put(key.key, mWifiSettingsConfigStore.get(key));
78             }
79             XmlUtil.writeNextValue(out, WifiSettingsConfigStore.StoreData.XML_TAG_VALUES,
80                     backupSettingsMap);
81 
82             XmlUtil.writeNextSectionEnd(out,
83                     WifiSettingsConfigStore.StoreData.XML_TAG_SECTION_HEADER);
84             XmlUtil.writeNextSectionEnd(out, XML_TAG_SECTION_HEADER_WIFI_SETTINGS_DATA);
85         } catch (XmlPullParserException | IOException e) {
86             Log.e(TAG, "Error retrieving the backup data: " + e);
87         }
88     }
89 
90     /**
91      * Parse out the wifi settings from the back up data and restore it.
92      *
93      * @param in the xml parser.
94      * @param depth the current depth which the tag XML_TAG_SECTION_HEADER_WIFI_SETTINGS_DATA.
95      */
restoreSettingsFromBackupData(XmlPullParser in, int depth)96     public void restoreSettingsFromBackupData(XmlPullParser in, int depth) {
97         if (in == null) {
98             Log.e(TAG, "Invalid backup data received");
99             return;
100         }
101         try {
102             XmlUtil.gotoNextSectionWithName(in,
103                     WifiSettingsConfigStore.StoreData.XML_TAG_SECTION_HEADER, depth + 1);
104             Map<String, Object> values =
105                     WifiSettingsConfigStore.StoreData.deserializeSettingsData(in, depth + 2);
106             if (values != null) {
107                 for (String keyString : values.keySet()) {
108                     if (mRestoreSettingsMap.containsKey(keyString)) {
109                         if (mVerboseLoggingEnabled) {
110                             Log.i(TAG, "Restored Settings: " + keyString
111                                     + " with value: " + values.get(keyString));
112                         }
113                         mWifiSettingsConfigStore.put(mRestoreSettingsMap.get(keyString),
114                                 values.get(keyString));
115                     } else {
116                         Log.e(TAG, "Unexpcected Settings found: " + keyString
117                                 + " with value: " + values.get(keyString));
118                     }
119                 }
120             }
121         } catch (XmlPullParserException | IOException | ClassCastException
122                 | IllegalArgumentException e) {
123             Log.e(TAG, "Error parsing the backup data: " + e);
124         }
125     }
126 
127     /**
128      * Enable verbose logging.
129      *
130      * @param verboseEnabled whether or not verbosity log level is enabled.
131      */
enableVerboseLogging(boolean verboseEnabled)132     public void enableVerboseLogging(boolean verboseEnabled) {
133         mVerboseLoggingEnabled = verboseEnabled;
134     }
135 }
136