Home | History | Annotate | Download | only in wifi
      1 /*
      2  * Copyright (C) 2017 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.text.TextUtils;
     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.IOException;
     28 import java.util.HashSet;
     29 import java.util.Set;
     30 
     31 /**
     32  * Store data for network notifiers.
     33  *
     34  * Below are the current configuration data for each respective store file:
     35  *
     36  * Share Store (system wide configurations)
     37  * - No data
     38  *
     39  * User Store (user specific configurations)
     40  * - Set of blacklisted SSIDs
     41  */
     42 public class SsidSetStoreData implements WifiConfigStore.StoreData {
     43     private static final String XML_TAG_SECTION_HEADER_SUFFIX = "ConfigData";
     44     private static final String XML_TAG_SSID_SET = "SSIDSet";
     45 
     46     private final String mTagName;
     47     private final DataSource mDataSource;
     48 
     49     /**
     50      * Interface define the data source for the notifier store data.
     51      */
     52     public interface DataSource {
     53         /**
     54          * Retrieve the SSID set from the data source.
     55          *
     56          * @return Set of SSIDs
     57          */
     58         Set<String> getSsids();
     59 
     60         /**
     61          * Update the SSID set in the data source.
     62          *
     63          * @param ssidSet The set of SSIDs
     64          */
     65         void setSsids(Set<String> ssidSet);
     66     }
     67 
     68     /**
     69      * Creates the SSID Set store data.
     70      *
     71      * @param name Identifier of the SSID set.
     72      * @param dataSource The DataSource that implements the update and retrieval of the SSID set.
     73      */
     74     SsidSetStoreData(String name, DataSource dataSource) {
     75         mTagName = name + XML_TAG_SECTION_HEADER_SUFFIX;
     76         mDataSource = dataSource;
     77     }
     78 
     79     @Override
     80     public void serializeData(XmlSerializer out, boolean shared)
     81             throws XmlPullParserException, IOException {
     82         if (shared) {
     83             throw new XmlPullParserException("Share data not supported");
     84         }
     85         Set<String> ssidSet = mDataSource.getSsids();
     86         if (ssidSet != null && !ssidSet.isEmpty()) {
     87             XmlUtil.writeNextValue(out, XML_TAG_SSID_SET, mDataSource.getSsids());
     88         }
     89     }
     90 
     91     @Override
     92     public void deserializeData(XmlPullParser in, int outerTagDepth, boolean shared)
     93             throws XmlPullParserException, IOException {
     94         // Ignore empty reads.
     95         if (in == null) {
     96             return;
     97         }
     98         if (shared) {
     99             throw new XmlPullParserException("Share data not supported");
    100         }
    101 
    102         while (!XmlUtil.isNextSectionEnd(in, outerTagDepth)) {
    103             String[] valueName = new String[1];
    104             Object value = XmlUtil.readCurrentValue(in, valueName);
    105             if (TextUtils.isEmpty(valueName[0])) {
    106                 throw new XmlPullParserException("Missing value name");
    107             }
    108             switch (valueName[0]) {
    109                 case XML_TAG_SSID_SET:
    110                     mDataSource.setSsids((Set<String>) value);
    111                     break;
    112                 default:
    113                     throw new XmlPullParserException("Unknown tag under "
    114                             + mTagName + ": " + valueName[0]);
    115             }
    116         }
    117     }
    118 
    119     @Override
    120     public void resetData(boolean shared) {
    121         if (!shared) {
    122             mDataSource.setSsids(new HashSet<>());
    123         }
    124     }
    125 
    126     @Override
    127     public String getName() {
    128         return mTagName;
    129     }
    130 
    131     @Override
    132     public boolean supportShareData() {
    133         return false;
    134     }
    135 }
    136