1/**
2 * Copyright (c) 2015, 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 android.service.carrier;
18
19import android.annotation.Nullable;
20import android.os.Parcel;
21import android.os.Parcelable;
22
23import com.android.internal.telephony.uicc.IccUtils;
24
25import java.util.Objects;
26
27/**
28 * Used to pass info to CarrierConfigService implementations so they can decide what values to
29 * return.
30 */
31public class CarrierIdentifier implements Parcelable {
32
33    /** Used to create a {@link CarrierIdentifier} from a {@link Parcel}. */
34    public static final Creator<CarrierIdentifier> CREATOR = new Creator<CarrierIdentifier>() {
35            @Override
36        public CarrierIdentifier createFromParcel(Parcel parcel) {
37            return new CarrierIdentifier(parcel);
38        }
39
40            @Override
41        public CarrierIdentifier[] newArray(int i) {
42            return new CarrierIdentifier[i];
43        }
44    };
45
46    private String mMcc;
47    private String mMnc;
48    private @Nullable String mSpn;
49    private @Nullable String mImsi;
50    private @Nullable String mGid1;
51    private @Nullable String mGid2;
52
53    public CarrierIdentifier(String mcc, String mnc, @Nullable String spn, @Nullable String imsi,
54            @Nullable String gid1, @Nullable String gid2) {
55        mMcc = mcc;
56        mMnc = mnc;
57        mSpn = spn;
58        mImsi = imsi;
59        mGid1 = gid1;
60        mGid2 = gid2;
61    }
62
63    /**
64     * Creates a carrier identifier instance.
65     *
66     * @param mccMnc A 3-byte array as defined by 3GPP TS 24.008.
67     * @param gid1 The group identifier level 1.
68     * @param gid2 The group identifier level 2.
69     * @throws IllegalArgumentException If the length of {@code mccMnc} is not 3.
70     */
71    public CarrierIdentifier(byte[] mccMnc, @Nullable String gid1, @Nullable String gid2) {
72        if (mccMnc.length != 3) {
73            throw new IllegalArgumentException(
74                    "MCC & MNC must be set by a 3-byte array: byte[" + mccMnc.length + "]");
75        }
76        String hex = IccUtils.bytesToHexString(mccMnc);
77        mMcc = new String(new char[] {hex.charAt(1), hex.charAt(0), hex.charAt(3)});
78        if (hex.charAt(2) == 'F') {
79            mMnc = new String(new char[] {hex.charAt(5), hex.charAt(4)});
80        } else {
81            mMnc = new String(new char[] {hex.charAt(5), hex.charAt(4), hex.charAt(2)});
82        }
83        mGid1 = gid1;
84        mGid2 = gid2;
85        mSpn = null;
86        mImsi = null;
87    }
88
89    /** @hide */
90    public CarrierIdentifier(Parcel parcel) {
91        readFromParcel(parcel);
92    }
93
94    /** Get the mobile country code. */
95    public String getMcc() {
96        return mMcc;
97    }
98
99    /** Get the mobile network code. */
100    public String getMnc() {
101        return mMnc;
102    }
103
104    /** Get the service provider name. */
105    @Nullable
106    public String getSpn() {
107        return mSpn;
108    }
109
110    /** Get the international mobile subscriber identity. */
111    @Nullable
112    public String getImsi() {
113        return mImsi;
114    }
115
116    /** Get the group identifier level 1. */
117    @Nullable
118    public String getGid1() {
119        return mGid1;
120    }
121
122    /** Get the group identifier level 2. */
123    @Nullable
124    public String getGid2() {
125        return mGid2;
126    }
127
128    @Override
129    public boolean equals(Object obj) {
130        if (this == obj) {
131            return true;
132        }
133        if (obj == null || getClass() != obj.getClass()) {
134            return false;
135        }
136
137        CarrierIdentifier that = (CarrierIdentifier) obj;
138        return Objects.equals(mMcc, that.mMcc)
139                && Objects.equals(mMnc, that.mMnc)
140                && Objects.equals(mSpn, that.mSpn)
141                && Objects.equals(mImsi, that.mImsi)
142                && Objects.equals(mGid1, that.mGid1)
143                && Objects.equals(mGid2, that.mGid2);
144    }
145
146    @Override
147    public int hashCode() {
148        int result = 1;
149        result = 31 * result + Objects.hashCode(mMcc);
150        result = 31 * result + Objects.hashCode(mMnc);
151        result = 31 * result + Objects.hashCode(mSpn);
152        result = 31 * result + Objects.hashCode(mImsi);
153        result = 31 * result + Objects.hashCode(mGid1);
154        result = 31 * result + Objects.hashCode(mGid2);
155        return result;
156    }
157
158    @Override
159    public int describeContents() {
160        return 0;
161    }
162
163    @Override
164    public void writeToParcel(Parcel out, int flags) {
165        out.writeString(mMcc);
166        out.writeString(mMnc);
167        out.writeString(mSpn);
168        out.writeString(mImsi);
169        out.writeString(mGid1);
170        out.writeString(mGid2);
171    }
172
173    @Override
174    public String toString() {
175      return "CarrierIdentifier{"
176          + "mcc=" + mMcc
177          + ",mnc=" + mMnc
178          + ",spn=" + mSpn
179          + ",imsi=" + mImsi
180          + ",gid1=" + mGid1
181          + ",gid2=" + mGid2
182          + "}";
183    }
184
185    /** @hide */
186    public void readFromParcel(Parcel in) {
187        mMcc = in.readString();
188        mMnc = in.readString();
189        mSpn = in.readString();
190        mImsi = in.readString();
191        mGid1 = in.readString();
192        mGid2 = in.readString();
193    }
194
195    /** @hide */
196    public interface MatchType {
197        int ALL = 0;
198        int SPN = 1;
199        int IMSI_PREFIX = 2;
200        int GID1 = 3;
201        int GID2 = 4;
202    }
203}
204