1/* 2 * Copyright (C) 2016 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package com.android.server.wifi.hotspot2.anqp; 18 19import android.text.TextUtils; 20 21import com.android.internal.annotations.VisibleForTesting; 22import com.android.server.wifi.ByteBufferReader; 23 24import java.net.ProtocolException; 25import java.nio.BufferUnderflowException; 26import java.nio.ByteBuffer; 27import java.nio.charset.StandardCharsets; 28import java.util.Locale; 29 30/** 31 * A generic Internationalized name field used in the Operator Friendly Name ANQP element 32 * (see HS2.0 R2 Spec 4.2) and the Venue Name ANQP element (see 802.11-2012 8.4.4.4). 33 * 34 * Format: 35 * 36 * | Length | Language Code | Name | 37 * 1 3 variable 38 */ 39public class I18Name { 40 @VisibleForTesting 41 public static final int LANGUAGE_CODE_LENGTH = 3; 42 43 @VisibleForTesting 44 public static final int MINIMUM_LENGTH = LANGUAGE_CODE_LENGTH; 45 46 private final String mLanguage; 47 private final Locale mLocale; 48 private final String mText; 49 50 @VisibleForTesting 51 public I18Name(String language, Locale locale, String text) { 52 mLanguage = language; 53 mLocale = locale; 54 mText = text; 55 } 56 57 /** 58 * Parse a I18Name from the given buffer. 59 * 60 * @param payload The byte buffer to read from 61 * @return {@link I18Name} 62 * @throws BufferUnderflowException 63 * @throws ProtocolException 64 */ 65 public static I18Name parse(ByteBuffer payload) throws ProtocolException { 66 // Retrieve the length field. 67 int length = payload.get() & 0xFF; 68 69 // Check for the minimum required length. 70 if (length < MINIMUM_LENGTH) { 71 throw new ProtocolException("Invalid length: " + length); 72 } 73 74 // Read the language string. 75 String language = ByteBufferReader.readString( 76 payload, LANGUAGE_CODE_LENGTH, StandardCharsets.US_ASCII).trim(); 77 Locale locale = Locale.forLanguageTag(language); 78 79 // Read the text string. 80 String text = ByteBufferReader.readString(payload, length - LANGUAGE_CODE_LENGTH, 81 StandardCharsets.UTF_8); 82 return new I18Name(language, locale, text); 83 } 84 85 public String getLanguage() { 86 return mLanguage; 87 } 88 89 public Locale getLocale() { 90 return mLocale; 91 } 92 93 public String getText() { 94 return mText; 95 } 96 97 @Override 98 public boolean equals(Object thatObject) { 99 if (this == thatObject) { 100 return true; 101 } 102 if (!(thatObject instanceof I18Name)) { 103 return false; 104 } 105 106 I18Name that = (I18Name) thatObject; 107 return TextUtils.equals(mLanguage, that.mLanguage) 108 && TextUtils.equals(mText, that.mText); 109 } 110 111 @Override 112 public int hashCode() { 113 int result = mLanguage.hashCode(); 114 result = 31 * result + mText.hashCode(); 115 return result; 116 } 117 118 @Override 119 public String toString() { 120 return mText + ':' + mLocale.getLanguage(); 121 } 122} 123