• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package com.android.anqp;
2 
3 import java.io.IOException;
4 import java.net.ProtocolException;
5 import java.nio.ByteBuffer;
6 import java.nio.charset.StandardCharsets;
7 import java.util.Locale;
8 
9 import static com.android.anqp.Constants.BYTE_MASK;
10 
11 /**
12  * A generic Internationalized name used in ANQP elements as specified in 802.11-2012 and
13  * "Wi-Fi Alliance Hotspot 2.0 (Release 2) Technical Specification - Version 5.00"
14  */
15 public class I18Name {
16     private final String mLanguage;
17     private final Locale mLocale;
18     private final String mText;
19 
I18Name(ByteBuffer payload)20     public I18Name(ByteBuffer payload) throws ProtocolException {
21         if (payload.remaining() < Constants.LANG_CODE_LENGTH + 1) {
22             throw new ProtocolException("Truncated I18Name: " + payload.remaining());
23         }
24         int nameLength = payload.get() & BYTE_MASK;
25         if (nameLength < Constants.LANG_CODE_LENGTH) {
26             throw new ProtocolException("Runt I18Name: " + nameLength);
27         }
28         mLanguage = Constants.getTrimmedString(payload,
29                 Constants.LANG_CODE_LENGTH, StandardCharsets.US_ASCII);
30         mLocale = Locale.forLanguageTag(mLanguage);
31         mText = Constants.getString(payload, nameLength -
32                 Constants.LANG_CODE_LENGTH, StandardCharsets.UTF_8);
33     }
34 
I18Name(String compoundString)35     public I18Name(String compoundString) throws IOException {
36         if (compoundString.length() < Constants.LANG_CODE_LENGTH) {
37             throw new IOException("I18String too short: '" + compoundString + "'");
38         }
39         mLanguage = compoundString.substring(0, Constants.LANG_CODE_LENGTH);
40         mText = compoundString.substring(Constants.LANG_CODE_LENGTH);
41         mLocale = Locale.forLanguageTag(mLanguage);
42     }
43 
getLanguage()44     public String getLanguage() {
45         return mLanguage;
46     }
47 
getLocale()48     public Locale getLocale() {
49         return mLocale;
50     }
51 
getText()52     public String getText() {
53         return mText;
54     }
55 
56     @Override
equals(Object thatObject)57     public boolean equals(Object thatObject) {
58         if (this == thatObject) {
59             return true;
60         }
61         if (thatObject == null || getClass() != thatObject.getClass()) {
62             return false;
63         }
64 
65         I18Name that = (I18Name) thatObject;
66         return mLanguage.equals(that.mLanguage) && mText.equals(that.mText);
67     }
68 
69     @Override
hashCode()70     public int hashCode() {
71         int result = mLanguage.hashCode();
72         result = 31 * result + mText.hashCode();
73         return result;
74     }
75 
76     @Override
toString()77     public String toString() {
78         return mText + ':' + mLocale.getLanguage();
79     }
80 }
81