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 
17 package android.service.carrier;
18 
19 import android.annotation.NonNull;
20 import android.annotation.Nullable;
21 import android.os.Parcel;
22 import android.os.Parcelable;
23 import android.telephony.TelephonyManager;
24 
25 import com.android.internal.telephony.uicc.IccUtils;
26 import com.android.telephony.Rlog;
27 
28 import java.util.Objects;
29 
30 /**
31  * Used to pass info to CarrierConfigService implementations so they can decide what values to
32  * return. Instead of passing mcc, mnc, gid1, gid2, spn, imsi to locate carrier information,
33  * CarrierIdentifier also include carrier id {@link TelephonyManager#getSimCarrierId()},
34  * a platform-wide unique identifier for each carrier. CarrierConfigService can directly use
35  * carrier id as the key to look up the carrier info.
36  */
37 public class CarrierIdentifier implements Parcelable {
38 
39     /** Used to create a {@link CarrierIdentifier} from a {@link Parcel}. */
40     public static final @android.annotation.NonNull Creator<CarrierIdentifier> CREATOR = new Creator<CarrierIdentifier>() {
41             @Override
42         public CarrierIdentifier createFromParcel(Parcel parcel) {
43             return new CarrierIdentifier(parcel);
44         }
45 
46             @Override
47         public CarrierIdentifier[] newArray(int i) {
48             return new CarrierIdentifier[i];
49         }
50     };
51 
52     private String mMcc;
53     private String mMnc;
54     private @Nullable String mSpn;
55     private @Nullable String mImsi;
56     private @Nullable String mGid1;
57     private @Nullable String mGid2;
58     private int mCarrierId = TelephonyManager.UNKNOWN_CARRIER_ID;
59     private int mSpecificCarrierId = TelephonyManager.UNKNOWN_CARRIER_ID;
60 
CarrierIdentifier(String mcc, String mnc, @Nullable String spn, @Nullable String imsi, @Nullable String gid1, @Nullable String gid2)61     public CarrierIdentifier(String mcc, String mnc, @Nullable String spn, @Nullable String imsi,
62             @Nullable String gid1, @Nullable String gid2) {
63         this(mcc, mnc, spn, imsi, gid1, gid2, TelephonyManager.UNKNOWN_CARRIER_ID,
64                 TelephonyManager.UNKNOWN_CARRIER_ID);
65     }
66 
67     /**
68      * @param mcc mobile country code
69      * @param mnc mobile network code
70      * @param spn service provider name
71      * @param imsi International Mobile Subscriber Identity {@link TelephonyManager#getSubscriberId()}
72      * @param gid1 group id level 1 {@link TelephonyManager#getGroupIdLevel1()}
73      * @param gid2 group id level 2
74      * @param carrierid carrier unique identifier {@link TelephonyManager#getSimCarrierId()}, used
75      *                  to uniquely identify the carrier and look up the carrier configurations.
76      * @param specificCarrierId specific carrier identifier
77      * {@link TelephonyManager#getSimSpecificCarrierId()}
78      */
CarrierIdentifier(@onNull String mcc, @NonNull String mnc, @Nullable String spn, @Nullable String imsi, @Nullable String gid1, @Nullable String gid2, int carrierid, int specificCarrierId)79     public CarrierIdentifier(@NonNull String mcc, @NonNull String mnc, @Nullable String spn,
80                              @Nullable String imsi, @Nullable String gid1, @Nullable String gid2,
81                              int carrierid, int specificCarrierId) {
82         mMcc = mcc;
83         mMnc = mnc;
84         mSpn = spn;
85         mImsi = imsi;
86         mGid1 = gid1;
87         mGid2 = gid2;
88         mCarrierId = carrierid;
89         mSpecificCarrierId = specificCarrierId;
90     }
91 
92     /**
93      * Creates a carrier identifier instance.
94      *
95      * @param mccMnc A 3-byte array as defined by 3GPP TS 24.008.
96      * @param gid1 The group identifier level 1.
97      * @param gid2 The group identifier level 2.
98      * @throws IllegalArgumentException If the length of {@code mccMnc} is not 3.
99      */
CarrierIdentifier(byte[] mccMnc, @Nullable String gid1, @Nullable String gid2)100     public CarrierIdentifier(byte[] mccMnc, @Nullable String gid1, @Nullable String gid2) {
101         if (mccMnc.length != 3) {
102             throw new IllegalArgumentException(
103                     "MCC & MNC must be set by a 3-byte array: byte[" + mccMnc.length + "]");
104         }
105         String hex = IccUtils.bytesToHexString(mccMnc);
106         mMcc = new String(new char[] {hex.charAt(1), hex.charAt(0), hex.charAt(3)});
107         if (hex.charAt(2) == 'F') {
108             mMnc = new String(new char[] {hex.charAt(5), hex.charAt(4)});
109         } else {
110             mMnc = new String(new char[] {hex.charAt(5), hex.charAt(4), hex.charAt(2)});
111         }
112         mGid1 = gid1;
113         mGid2 = gid2;
114         mSpn = null;
115         mImsi = null;
116     }
117 
118     /** @hide */
CarrierIdentifier(Parcel parcel)119     public CarrierIdentifier(Parcel parcel) {
120         readFromParcel(parcel);
121     }
122 
123     /** Get the mobile country code. */
getMcc()124     public String getMcc() {
125         return mMcc;
126     }
127 
128     /** Get the mobile network code. */
getMnc()129     public String getMnc() {
130         return mMnc;
131     }
132 
133     /** Get the service provider name. */
134     @Nullable
getSpn()135     public String getSpn() {
136         return mSpn;
137     }
138 
139     /** Get the international mobile subscriber identity. */
140     @Nullable
getImsi()141     public String getImsi() {
142         return mImsi;
143     }
144 
145     /** Get the group identifier level 1. */
146     @Nullable
getGid1()147     public String getGid1() {
148         return mGid1;
149     }
150 
151     /** Get the group identifier level 2. */
152     @Nullable
getGid2()153     public String getGid2() {
154         return mGid2;
155     }
156 
157     /**
158      * Returns the carrier id.
159      * @see TelephonyManager#getSimCarrierId()
160      */
getCarrierId()161     public int getCarrierId() {
162         return mCarrierId;
163     }
164 
165     /**
166      * A specific carrier ID returns the fine-grained carrier ID of the current subscription.
167      * It can represent the fact that a carrier may be in effect an aggregation of other carriers
168      * (ie in an MVNO type scenario) where each of these specific carriers which are used to make
169      * up the actual carrier service may have different carrier configurations.
170      * A specific carrier ID could also be used, for example, in a scenario where a carrier requires
171      * different carrier configuration for different service offering such as a prepaid plan.
172      *
173      * @see TelephonyManager#getSimSpecificCarrierId()
174      */
getSpecificCarrierId()175     public int getSpecificCarrierId() {
176         return mSpecificCarrierId;
177     }
178 
179     @Override
equals(Object obj)180     public boolean equals(Object obj) {
181         if (this == obj) {
182             return true;
183         }
184         if (obj == null || getClass() != obj.getClass()) {
185             return false;
186         }
187 
188         CarrierIdentifier that = (CarrierIdentifier) obj;
189         return Objects.equals(mMcc, that.mMcc)
190                 && Objects.equals(mMnc, that.mMnc)
191                 && Objects.equals(mSpn, that.mSpn)
192                 && Objects.equals(mImsi, that.mImsi)
193                 && Objects.equals(mGid1, that.mGid1)
194                 && Objects.equals(mGid2, that.mGid2)
195                 && Objects.equals(mCarrierId, that.mCarrierId)
196                 && Objects.equals(mSpecificCarrierId, that.mSpecificCarrierId);
197     }
198 
199     @Override
hashCode()200     public int hashCode(){
201         return Objects.hash(mMcc, mMnc, mSpn, mImsi, mGid1, mGid2, mCarrierId, mSpecificCarrierId);
202     }
203 
204     @Override
describeContents()205     public int describeContents() {
206         return 0;
207     }
208 
209     @Override
writeToParcel(Parcel out, int flags)210     public void writeToParcel(Parcel out, int flags) {
211         out.writeString(mMcc);
212         out.writeString(mMnc);
213         out.writeString(mSpn);
214         out.writeString(mImsi);
215         out.writeString(mGid1);
216         out.writeString(mGid2);
217         out.writeInt(mCarrierId);
218         out.writeInt(mSpecificCarrierId);
219     }
220 
221     @Override
toString()222     public String toString() {
223       return "CarrierIdentifier{"
224               + "mcc=" + mMcc
225               + ",mnc=" + mMnc
226               + ",spn=" + mSpn
227               + ",imsi=" + Rlog.pii(false, mImsi)
228               + ",gid1=" + mGid1
229               + ",gid2=" + mGid2
230               + ",carrierid=" + mCarrierId
231               + ",specificCarrierId=" + mSpecificCarrierId
232               + "}";
233     }
234 
235     /** @hide */
readFromParcel(Parcel in)236     public void readFromParcel(Parcel in) {
237         mMcc = in.readString();
238         mMnc = in.readString();
239         mSpn = in.readString();
240         mImsi = in.readString();
241         mGid1 = in.readString();
242         mGid2 = in.readString();
243         mCarrierId = in.readInt();
244         mSpecificCarrierId = in.readInt();
245     }
246 
247     /** @hide */
248     public interface MatchType {
249         int ALL = 0;
250         int SPN = 1;
251         int IMSI_PREFIX = 2;
252         int GID1 = 3;
253         int GID2 = 4;
254     }
255 }
256