summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPeter Qiu <zqiu@google.com>2017-06-06 13:12:11 -0700
committerPeter Qiu <zqiu@google.com>2017-06-13 15:54:30 +0000
commit2d511b3ad304301a3459f434b973525a23107ad1 (patch)
tree52b62c82d703a190bcd6ce45a06cc2fab5e88d4f
parent95571223c1eece592650e67c894e4a9f95c2d5ae (diff)
hotspot2: anqp: OsuProviderInfo: friendly name and service description selection
An OSU provider might contain multiple friendly names and service descriptions in different languages. In this case, the value that's in the default language will be preferred. Bug: 62235301 Test: frameworks/opt/net/wifi/tests/wifitests/runtests.sh Change-Id: Ie6065a494e6a5c9aa1cc05621ab9627628efb446 Merged-In: Ie6065a494e6a5c9aa1cc05621ab9627628efb446
-rw-r--r--service/java/com/android/server/wifi/hotspot2/anqp/OsuProviderInfo.java43
-rw-r--r--tests/wifitests/src/com/android/server/wifi/hotspot2/anqp/OsuProviderInfoTest.java124
2 files changed, 167 insertions, 0 deletions
diff --git a/service/java/com/android/server/wifi/hotspot2/anqp/OsuProviderInfo.java b/service/java/com/android/server/wifi/hotspot2/anqp/OsuProviderInfo.java
index 85b024e50..8952c5a51 100644
--- a/service/java/com/android/server/wifi/hotspot2/anqp/OsuProviderInfo.java
+++ b/service/java/com/android/server/wifi/hotspot2/anqp/OsuProviderInfo.java
@@ -30,6 +30,7 @@ import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
+import java.util.Locale;
import java.util.Objects;
/**
@@ -168,6 +169,28 @@ public class OsuProviderInfo {
return Collections.unmodifiableList(mServiceDescriptions);
}
+ /**
+ * Return the friendly name string from the friendly name list. The string matching
+ * the default locale will be returned if it is found, otherwise the first name in the list
+ * will be returned. A null will be returned if the list is empty.
+ *
+ * @return friendly name string
+ */
+ public String getFriendlyName() {
+ return getI18String(mFriendlyNames);
+ }
+
+ /**
+ * Return the service description string from the service description list. The string
+ * matching the default locale will be returned if it is found, otherwise the first element in
+ * the list will be returned. A null will be returned if the list is empty.
+ *
+ * @return service description string
+ */
+ public String getServiceDescription() {
+ return getI18String(mServiceDescriptions);
+ }
+
@Override
public boolean equals(Object thatObject) {
if (this == thatObject) {
@@ -255,4 +278,24 @@ public class OsuProviderInfo {
payload.position(payload.position() + length);
return subBuffer;
}
+
+ /**
+ * Return the appropriate I18 string value from the list of I18 string values.
+ * The string matching the default locale will be returned if it is found, otherwise the
+ * first string in the list will be returned. A null will be returned if the list is empty.
+ *
+ * @param i18Strings List of I18 string values
+ * @return String matching the default locale, null otherwise
+ */
+ private static String getI18String(List<I18Name> i18Strings) {
+ for (I18Name name : i18Strings) {
+ if (name.getLanguage().equals(Locale.getDefault().getLanguage())) {
+ return name.getText();
+ }
+ }
+ if (i18Strings.size() > 0) {
+ return i18Strings.get(0).getText();
+ }
+ return null;
+ }
}
diff --git a/tests/wifitests/src/com/android/server/wifi/hotspot2/anqp/OsuProviderInfoTest.java b/tests/wifitests/src/com/android/server/wifi/hotspot2/anqp/OsuProviderInfoTest.java
index e20d6d53e..8ef93f061 100644
--- a/tests/wifitests/src/com/android/server/wifi/hotspot2/anqp/OsuProviderInfoTest.java
+++ b/tests/wifitests/src/com/android/server/wifi/hotspot2/anqp/OsuProviderInfoTest.java
@@ -25,6 +25,9 @@ import org.junit.Test;
import java.net.ProtocolException;
import java.nio.BufferUnderflowException;
import java.nio.ByteBuffer;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Locale;
/**
* Unit tests for {@link com.android.server.wifi.hotspot2.anqp.OsuProviderInfo}.
@@ -80,4 +83,125 @@ public class OsuProviderInfoTest {
assertEquals(OsuProviderInfoTestUtil.TEST_OSU_PROVIDER_INFO,
OsuProviderInfo.parse(buffer));
}
+
+ /**
+ * Verify that when a provider contained multiple friendly names in different languages, the
+ * friendly name that's in default language is returned.
+ *
+ * @throws Exception
+ */
+ @Test
+ public void getFriendlyNameMatchingDefaultLocale() throws Exception {
+ List<I18Name> friendlyNames = new ArrayList<>();
+ Locale defaultLocale = Locale.getDefault();
+ Locale nonDefaultLocale = Locale.FRENCH;
+ if (defaultLocale.equals(nonDefaultLocale)) {
+ nonDefaultLocale = Locale.ENGLISH;
+ }
+ String nonDefaultString = "Non-default";
+ String defaultString = "Default";
+ friendlyNames.add(
+ new I18Name(nonDefaultLocale.getLanguage(), nonDefaultLocale, nonDefaultString));
+ friendlyNames.add(new I18Name(defaultLocale.getLanguage(), defaultLocale, defaultString));
+ OsuProviderInfo providerInfo =
+ new OsuProviderInfo(friendlyNames, null, null, null, null, null);
+ assertEquals(defaultString, providerInfo.getFriendlyName());
+ }
+
+ /**
+ * Verify that when a provider contained multiple friendly names where no friendly name
+ * is in default language, the first name in the list is returned.
+ *
+ * @throws Exception
+ */
+ @Test
+ public void getFriendlyNameNotMatchingDefaultLocale() throws Exception {
+ List<I18Name> friendlyNames = new ArrayList<>();
+ Locale nonDefaultLocale = Locale.FRENCH;
+ if (nonDefaultLocale.equals(Locale.getDefault())) {
+ nonDefaultLocale = Locale.ENGLISH;
+ }
+ String firstString = "First name";
+ String secondString = "Second name";
+ friendlyNames.add(
+ new I18Name(nonDefaultLocale.getLanguage(), nonDefaultLocale, firstString));
+ friendlyNames.add(
+ new I18Name(nonDefaultLocale.getLanguage(), nonDefaultLocale, secondString));
+ OsuProviderInfo providerInfo =
+ new OsuProviderInfo(friendlyNames, null, null, null, null, null);
+ assertEquals(firstString, providerInfo.getFriendlyName());
+ }
+
+ /**
+ * Verify that null will be returned for a provider containing empty friendly name list.
+ *
+ * @throws Exception
+ */
+ @Test
+ public void getFriendlyNameWithEmptyList() throws Exception {
+ OsuProviderInfo providerInfo =
+ new OsuProviderInfo(new ArrayList<I18Name>(), null, null, null, null, null);
+ assertEquals(null, providerInfo.getFriendlyName());
+ }
+
+ /**
+ * Verify that when a provider contained multiple service descriptions in different languages,
+ * the service description that's in default language is returned.
+ *
+ * @throws Exception
+ */
+ @Test
+ public void getServiceDescriptionMatchingDefaultLocale() throws Exception {
+ List<I18Name> serviceDescriptions = new ArrayList<>();
+ Locale defaultLocale = Locale.getDefault();
+ Locale nonDefaultLocale = Locale.FRENCH;
+ if (defaultLocale.equals(nonDefaultLocale)) {
+ nonDefaultLocale = Locale.ENGLISH;
+ }
+ String nonDefaultString = "Non-default";
+ String defaultString = "Default";
+ serviceDescriptions.add(
+ new I18Name(nonDefaultLocale.getLanguage(), nonDefaultLocale, nonDefaultString));
+ serviceDescriptions.add(
+ new I18Name(defaultLocale.getLanguage(), defaultLocale, defaultString));
+ OsuProviderInfo providerInfo =
+ new OsuProviderInfo(null, null, null, null, null, serviceDescriptions);
+ assertEquals(defaultString, providerInfo.getServiceDescription());
+ }
+
+ /**
+ * Verify that when a provider contained multiple service descriptions where none of them
+ * is in default language, the first element in the list is returned.
+ *
+ * @throws Exception
+ */
+ @Test
+ public void getServiceDescriptionNotMatchingDefaultLocale() throws Exception {
+ List<I18Name> serviceDescriptions = new ArrayList<>();
+ Locale nonDefaultLocale = Locale.FRENCH;
+ if (nonDefaultLocale.equals(Locale.getDefault())) {
+ nonDefaultLocale = Locale.ENGLISH;
+ }
+ String firstString = "First name";
+ String secondString = "Second name";
+ serviceDescriptions.add(
+ new I18Name(nonDefaultLocale.getLanguage(), nonDefaultLocale, firstString));
+ serviceDescriptions.add(
+ new I18Name(nonDefaultLocale.getLanguage(), nonDefaultLocale, secondString));
+ OsuProviderInfo providerInfo =
+ new OsuProviderInfo(null, null, null, null, null, serviceDescriptions);
+ assertEquals(firstString, providerInfo.getServiceDescription());
+ }
+
+ /**
+ * Verify that null will be returned for a provider containing empty friendly name list.
+ *
+ * @throws Exception
+ */
+ @Test
+ public void getServiceDescriptionWithEmptyList() throws Exception {
+ OsuProviderInfo providerInfo =
+ new OsuProviderInfo(null, null, null, null, null, new ArrayList<I18Name>());
+ assertEquals(null, providerInfo.getServiceDescription());
+ }
}