1package com.android.anqp;
2
3import android.os.Parcel;
4
5import java.net.ProtocolException;
6import java.nio.ByteBuffer;
7import java.nio.charset.StandardCharsets;
8
9import static com.android.anqp.Constants.SHORT_MASK;
10
11/**
12 * The Icons available OSU Providers sub field, as specified in
13 * Wi-Fi Alliance Hotspot 2.0 (Release 2) Technical Specification - Version 5.00,
14 * section 4.8.1.4
15 */
16public class IconInfo {
17    private final int mWidth;
18    private final int mHeight;
19    private final String mLanguage;
20    private final String mIconType;
21    private final String mFileName;
22
23    public IconInfo(ByteBuffer payload) throws ProtocolException {
24        if (payload.remaining() < 9) {
25            throw new ProtocolException("Truncated icon meta data");
26        }
27
28        mWidth = payload.getShort() & SHORT_MASK;
29        mHeight = payload.getShort() & SHORT_MASK;
30        mLanguage = Constants.getTrimmedString(payload,
31                Constants.LANG_CODE_LENGTH, StandardCharsets.US_ASCII);
32        mIconType = Constants.getPrefixedString(payload, 1, StandardCharsets.US_ASCII);
33        mFileName = Constants.getPrefixedString(payload, 1, StandardCharsets.UTF_8);
34    }
35
36    public int getWidth() {
37        return mWidth;
38    }
39
40    public int getHeight() {
41        return mHeight;
42    }
43
44    public String getLanguage() {
45        return mLanguage;
46    }
47
48    public String getIconType() {
49        return mIconType;
50    }
51
52    public String getFileName() {
53        return mFileName;
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        IconInfo that = (IconInfo) thatObject;
66        return mHeight == that.mHeight &&
67                mWidth == that.mWidth &&
68                mFileName.equals(that.mFileName) &&
69                mIconType.equals(that.mIconType) &&
70                mLanguage.equals(that.mLanguage);
71    }
72
73    @Override
74    public int hashCode() {
75        int result = mWidth;
76        result = 31 * result + mHeight;
77        result = 31 * result + mLanguage.hashCode();
78        result = 31 * result + mIconType.hashCode();
79        result = 31 * result + mFileName.hashCode();
80        return result;
81    }
82
83    @Override
84    public String toString() {
85        return "IconInfo{" +
86                "Width=" + mWidth +
87                ", Height=" + mHeight +
88                ", Language=" + mLanguage +
89                ", IconType='" + mIconType + '\'' +
90                ", FileName='" + mFileName + '\'' +
91                '}';
92    }
93
94    public IconInfo(Parcel in) {
95        mWidth = in.readInt();
96        mHeight = in.readInt();
97        mLanguage = in.readString();
98        mIconType = in.readString();
99        mFileName = in.readString();
100    }
101
102    public void writeParcel(Parcel out) {
103        out.writeInt(mWidth);
104        out.writeInt(mHeight);
105        out.writeString(mLanguage);
106        out.writeString(mIconType);
107        out.writeString(mFileName);
108    }
109}
110