1package com.android.anqp; 2 3import java.io.IOException; 4import java.net.ProtocolException; 5import java.nio.ByteBuffer; 6import java.nio.charset.StandardCharsets; 7import java.util.Locale; 8 9import 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 */ 15public class I18Name { 16 private final String mLanguage; 17 private final Locale mLocale; 18 private final String mText; 19 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 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 44 public String getLanguage() { 45 return mLanguage; 46 } 47 48 public Locale getLocale() { 49 return mLocale; 50 } 51 52 public String getText() { 53 return mText; 54 } 55 56 @Override 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 70 public int hashCode() { 71 int result = mLanguage.hashCode(); 72 result = 31 * result + mText.hashCode(); 73 return result; 74 } 75 76 @Override 77 public String toString() { 78 return mText + ':' + mLocale.getLanguage(); 79 } 80} 81