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