1 /*
2  * Copyright (C) 2017 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 
17 package com.android.server.wifi.hotspot2.anqp;
18 
19 import android.text.TextUtils;
20 
21 import com.android.internal.annotations.VisibleForTesting;
22 import com.android.server.wifi.ByteBufferReader;
23 
24 import java.nio.BufferUnderflowException;
25 import java.nio.ByteBuffer;
26 import java.nio.ByteOrder;
27 import java.nio.charset.StandardCharsets;
28 import java.util.Objects;
29 
30 /**
31  * The Icons available OSU Providers sub field, as specified in
32  * Wi-Fi Alliance Hotspot 2.0 (Release 2) Technical Specification - Version 5.00,
33  * section 4.8.1.4
34  *
35  * Format:
36  *
37  * | Width | Height | Language | Type Length | Type | Filename Length | Filename |
38  *     2       2         3           1       variable        1          variable
39  */
40 public class IconInfo {
41     private static final int LANGUAGE_CODE_LENGTH = 3;
42 
43     private final int mWidth;
44     private final int mHeight;
45     private final String mLanguage;
46     private final String mIconType;
47     private final String mFileName;
48 
49     @VisibleForTesting
IconInfo(int width, int height, String language, String iconType, String fileName)50     public IconInfo(int width, int height, String language, String iconType, String fileName) {
51         mWidth = width;
52         mHeight = height;
53         mLanguage = language;
54         mIconType = iconType;
55         mFileName = fileName;
56     }
57 
58     /**
59      * Parse a IconInfo from the given buffer.
60      *
61      * @param payload The buffer to read from
62      * @return {@link IconInfo}
63      * @throws BufferUnderflowException
64      */
parse(ByteBuffer payload)65     public static IconInfo parse(ByteBuffer payload) {
66         int width = (int) ByteBufferReader.readInteger(payload, ByteOrder.LITTLE_ENDIAN, 2)
67                 & 0xFFFF;
68         int height = (int) ByteBufferReader.readInteger(payload, ByteOrder.LITTLE_ENDIAN, 2)
69                 & 0xFFFF;
70 
71         // Read the language string.
72         String language = ByteBufferReader.readString(
73                 payload, LANGUAGE_CODE_LENGTH, StandardCharsets.US_ASCII).trim();
74 
75         String iconType =
76                 ByteBufferReader.readStringWithByteLength(payload, StandardCharsets.US_ASCII);
77         String fileName =
78                 ByteBufferReader.readStringWithByteLength(payload, StandardCharsets.UTF_8);
79 
80         return new IconInfo(width, height, language, iconType, fileName);
81     }
82 
getWidth()83     public int getWidth() {
84         return mWidth;
85     }
86 
getHeight()87     public int getHeight() {
88         return mHeight;
89     }
90 
getLanguage()91     public String getLanguage() {
92         return mLanguage;
93     }
94 
getIconType()95     public String getIconType() {
96         return mIconType;
97     }
98 
getFileName()99     public String getFileName() {
100         return mFileName;
101     }
102 
103     @Override
equals(Object thatObject)104     public boolean equals(Object thatObject) {
105         if (this == thatObject) {
106             return true;
107         }
108         if (!(thatObject instanceof IconInfo)) {
109             return false;
110         }
111 
112         IconInfo that = (IconInfo) thatObject;
113         return mWidth == that.mWidth
114                 && mHeight == that.mHeight
115                 && TextUtils.equals(mLanguage, that.mLanguage)
116                 && TextUtils.equals(mIconType, that.mIconType)
117                 && TextUtils.equals(mFileName, that.mFileName);
118     }
119 
120     @Override
hashCode()121     public int hashCode() {
122         return Objects.hash(mWidth, mHeight, mLanguage, mIconType, mFileName);
123     }
124 
125     @Override
toString()126     public String toString() {
127         return "IconInfo{"
128                 + "Width=" + mWidth
129                 + ", Height=" + mHeight
130                 + ", Language=" + mLanguage
131                 + ", IconType='" + mIconType + "\'"
132                 + ", FileName='" + mFileName + "\'"
133                 + "}";
134     }
135 }
136