diff options
author | Joe LaPenna <jlapenna@google.com> | 2017-03-20 11:26:53 -0700 |
---|---|---|
committer | Joe LaPenna <jlapenna@google.com> | 2017-03-20 11:49:58 -0700 |
commit | 013ba587a539e3ea2f872e3f7b8c981805a6ad93 (patch) | |
tree | bc98f3f8665fa8ed28bb56c18eb26bc46fda1a7f | |
parent | 2567b5b61baa47324b6d520c4fa1b2a0ac7d59d5 (diff) | |
download | NetworkRecommendation-013ba587a539e3ea2f872e3f7b8c981805a6ad93.tar.gz |
Move WideAreaNetworks.java to config
Test: mma NetworkRecommendation RunNetworkRecommendationRoboTests
Bug: 34944625
Change-Id: Ia62ea173455f3516723a42b109edfc825aa57c84
6 files changed, 175 insertions, 40 deletions
diff --git a/robotests/src/com/android/networkrecommendation/wakeup/WifiWakeupControllerTest.java b/robotests/src/com/android/networkrecommendation/wakeup/WifiWakeupControllerTest.java index d11cc43..ac30e8e 100644 --- a/robotests/src/com/android/networkrecommendation/wakeup/WifiWakeupControllerTest.java +++ b/robotests/src/com/android/networkrecommendation/wakeup/WifiWakeupControllerTest.java @@ -36,7 +36,7 @@ import android.os.PowerManager; import android.provider.Settings; import com.android.networkrecommendation.BroadcastIntentTestHelper; import com.android.networkrecommendation.config.Flag; -import com.android.networkrecommendation.debug.WideAreaNetworks; +import com.android.networkrecommendation.config.WideAreaNetworks; import com.android.networkrecommendation.util.RoboCompatUtil; import com.google.common.collect.Lists; import java.io.FileDescriptor; diff --git a/src/com/android/networkrecommendation/config/Csv.java b/src/com/android/networkrecommendation/config/Csv.java new file mode 100644 index 0000000..35b6182 --- /dev/null +++ b/src/com/android/networkrecommendation/config/Csv.java @@ -0,0 +1,109 @@ +/* + * Copyright (C) 2009 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.networkrecommendation.config; + +import java.io.BufferedReader; +import java.io.IOException; +import java.util.List; + +/** + * Utilities for parsing and serializing Comma-Separated Value data. + * See http://en.wikipedia.org/wiki/Comma-separated_values and + * http://tools.ietf.org/html/rfc4180 for details of the format. + */ +public class Csv { + /** Field delimiter. The C in CSV. */ + public static final String COMMA = ","; + + /** Record delimiter. "Proper" CSV uses CR-LF, but that would be annoying. */ + public static final String NEWLINE = "\n"; + + /** + * Appends a single value to an output string. If the value + * contains quotes, commas, newlines, or leading or trailing whitespace, + * the value will be written in quotes (with internal quotes doubled). + * Newlines are converted to standard CR-LF form. This function does not + * write field delimiters -- append {@link COMMA} yourself between values. + * + * @param value to write, quoted as necessary; must be non-null. + * @param output to append (possibly quoted) value to + * @throws java.io.IOException if writing to 'output' fails + */ + public static void writeValue(String value, Appendable output) throws IOException { + int len = value.length(); + if (len == 0) return; + + char first = value.charAt(0); + char last = value.charAt(len - 1); + if (first != ' ' && first != '\t' && last != ' ' && last != '\t' && + value.indexOf('"') < 0 && value.indexOf(',') < 0 && + value.indexOf('\r') < 0 && value.indexOf('\n') < 0) { + // No quoting needed. + output.append(value); + return; + } + + output.append('"').append(value.replace("\"", "\"\"")).append('"'); + } + + /** + * Parse a record of comma separated values from an input file. + * May read multiple physical lines if values contain embedded newlines. + * + * @param reader to read one or more physical lines from + * @param out array to append unquoted CSV values to + * @return true if values were read, false on EOF + * @throws java.io.IOException if reading from 'reader' fails + */ + public static boolean parseLine(BufferedReader reader, List<String> out) throws IOException { + String text = reader.readLine(); + if (text == null) return false; + + int pos = 0; + do { + StringBuilder buf = new StringBuilder(); + int comma; + for (;;) { + comma = text.indexOf(',', pos); + int quote = text.indexOf('"', pos); + if (quote == -1 || (comma != -1 && comma < quote)) break; + + if (pos > 0 && text.charAt(pos - 1) == '"') buf.append('"'); + buf.append(text, pos, quote); + while ((quote = text.indexOf('"', (pos = quote + 1))) == -1) { + buf.append(text, pos, text.length()).append('\n'); + text = reader.readLine(); + if (text == null) { + out.add(buf.toString()); + return true; + } + quote = -1; + } + + buf.append(text, pos, quote); + pos = quote + 1; + } + + buf.append(text, pos, comma == -1 ? text.length() : comma); + out.add(buf.toString()); + pos = comma + 1; + } while (pos > 0); + return true; + } + + private Csv() {} // Do not instantiate +} diff --git a/src/com/android/networkrecommendation/config/G.java b/src/com/android/networkrecommendation/config/G.java index 45a9de3..b256f25 100644 --- a/src/com/android/networkrecommendation/config/G.java +++ b/src/com/android/networkrecommendation/config/G.java @@ -24,6 +24,7 @@ public final class G { */ public interface Netrec { Flag<Boolean> enableSensitiveLogging = new Flag(false); + Flag<String> wideAreaNetworks = new Flag("xfinitywifi,XFINITY"); } private G() {} diff --git a/src/com/android/networkrecommendation/config/WideAreaNetworks.java b/src/com/android/networkrecommendation/config/WideAreaNetworks.java new file mode 100644 index 0000000..1a33559 --- /dev/null +++ b/src/com/android/networkrecommendation/config/WideAreaNetworks.java @@ -0,0 +1,63 @@ +/* + * Copyright (C) 2017 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.android.networkrecommendation.config; + +import static com.android.networkrecommendation.Constants.TAG; + +import android.support.annotation.VisibleForTesting; +import com.android.networkrecommendation.config.G.Netrec; +import com.android.networkrecommendation.util.Blog; +import com.google.common.collect.ImmutableSet; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.StringReader; +import java.util.ArrayList; +import java.util.List; + +/** Provides a list of known wide area networks. */ +public class WideAreaNetworks { + private WideAreaNetworks() {} + + private static ImmutableSet<String> sWideAreaNetworks; + + /** Initialize the list of wide area networks from the phenotype flag. */ + public static void init() { + sWideAreaNetworks = parseFlag(); + } + + /** + * @param ssid canonical SSID for a network (with quotes removed) + * @return {@code true} if {@code ssid} is in the set of wide area networks. + */ + public static boolean contains(String ssid) { + if (sWideAreaNetworks == null) { + init(); + } + return sWideAreaNetworks.contains(ssid); + } + + @VisibleForTesting + static ImmutableSet<String> parseFlag() { + List<String> parts = new ArrayList<>(); + BufferedReader reader = new BufferedReader(new StringReader(Netrec.wideAreaNetworks.get())); + try { + Csv.parseLine(reader, parts); + } catch (IOException ex) { + Blog.e(TAG, ex, "Error parsing flag"); + } + return ImmutableSet.copyOf(parts); + } +} diff --git a/src/com/android/networkrecommendation/debug/WideAreaNetworks.java b/src/com/android/networkrecommendation/debug/WideAreaNetworks.java deleted file mode 100644 index 75330f3..0000000 --- a/src/com/android/networkrecommendation/debug/WideAreaNetworks.java +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Copyright (C) 2017 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.android.networkrecommendation.debug; - -import com.google.common.collect.ImmutableSet; - -/** - * This class contains a list of known wide area netowrks. TODO(netrec): replace this with a flag - * value or a flag controlled bloom filter. - */ -public class WideAreaNetworks { - private WideAreaNetworks() {} - - /** - * @param ssid canonical SSID for a network (with quotes removed) - * @return {@code true} if {@code ssid} is in the set of wide area networks. - */ - public static final boolean contains(String ssid) { - return WIDE_AREA_NETWORK_SSIDS.contains(ssid); - } - - /** List of wide area networks. */ - private static final ImmutableSet<String> WIDE_AREA_NETWORK_SSIDS = - ImmutableSet.of("xfinitywifi"); -} diff --git a/src/com/android/networkrecommendation/wakeup/WifiWakeupController.java b/src/com/android/networkrecommendation/wakeup/WifiWakeupController.java index ecd47bf..100e652 100644 --- a/src/com/android/networkrecommendation/wakeup/WifiWakeupController.java +++ b/src/com/android/networkrecommendation/wakeup/WifiWakeupController.java @@ -34,7 +34,7 @@ import android.text.TextUtils; import android.util.ArrayMap; import android.util.ArraySet; import com.android.networkrecommendation.config.G; -import com.android.networkrecommendation.debug.WideAreaNetworks; +import com.android.networkrecommendation.config.WideAreaNetworks; import com.android.networkrecommendation.util.Blog; import com.android.networkrecommendation.util.RoboCompatUtil; import com.android.networkrecommendation.util.WifiConfigurationUtil; |