1 /*
2  * Copyright (C) 2014 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.telephony;
18 
19 import android.content.Context;
20 import android.graphics.Bitmap;
21 import android.graphics.Canvas;
22 import android.graphics.Color;
23 import android.graphics.Paint;
24 import android.graphics.PorterDuff;
25 import android.graphics.PorterDuffColorFilter;
26 import android.graphics.Rect;
27 import android.graphics.Typeface;
28 import android.os.Parcel;
29 import android.os.Parcelable;
30 import android.util.DisplayMetrics;
31 
32 /**
33  * A Parcelable class for Subscription Information.
34  */
35 public class SubscriptionInfo implements Parcelable {
36 
37     /**
38      * Size of text to render on the icon.
39      */
40     private static final int TEXT_SIZE = 16;
41 
42     /**
43      * Subscription Identifier, this is a device unique number
44      * and not an index into an array
45      */
46     private int mId;
47 
48     /**
49      * The GID for a SIM that maybe associated with this subscription, empty if unknown
50      */
51     private String mIccId;
52 
53     /**
54      * The index of the slot that currently contains the subscription
55      * and not necessarily unique and maybe INVALID_SLOT_ID if unknown
56      */
57     private int mSimSlotIndex;
58 
59     /**
60      * The name displayed to the user that identifies this subscription
61      */
62     private CharSequence mDisplayName;
63 
64     /**
65      * String that identifies SPN/PLMN
66      * TODO : Add a new field that identifies only SPN for a sim
67      */
68     private CharSequence mCarrierName;
69 
70     /**
71      * The source of the name, NAME_SOURCE_UNDEFINED, NAME_SOURCE_DEFAULT_SOURCE,
72      * NAME_SOURCE_SIM_SOURCE or NAME_SOURCE_USER_INPUT.
73      */
74     private int mNameSource;
75 
76     /**
77      * The color to be used for tinting the icon when displaying to the user
78      */
79     private int mIconTint;
80 
81     /**
82      * A number presented to the user identify this subscription
83      */
84     private String mNumber;
85 
86     /**
87      * Data roaming state, DATA_RAOMING_ENABLE, DATA_RAOMING_DISABLE
88      */
89     private int mDataRoaming;
90 
91     /**
92      * SIM Icon bitmap
93      */
94     private Bitmap mIconBitmap;
95 
96     /**
97      * Mobile Country Code
98      */
99     private int mMcc;
100 
101     /**
102      * Mobile Network Code
103      */
104     private int mMnc;
105 
106     /**
107      * ISO Country code for the subscription's provider
108      */
109     private String mCountryIso;
110 
111     /**
112      * @hide
113      */
SubscriptionInfo(int id, String iccId, int simSlotIndex, CharSequence displayName, CharSequence carrierName, int nameSource, int iconTint, String number, int roaming, Bitmap icon, int mcc, int mnc, String countryIso)114     public SubscriptionInfo(int id, String iccId, int simSlotIndex, CharSequence displayName,
115             CharSequence carrierName, int nameSource, int iconTint, String number, int roaming,
116             Bitmap icon, int mcc, int mnc, String countryIso) {
117         this.mId = id;
118         this.mIccId = iccId;
119         this.mSimSlotIndex = simSlotIndex;
120         this.mDisplayName = displayName;
121         this.mCarrierName = carrierName;
122         this.mNameSource = nameSource;
123         this.mIconTint = iconTint;
124         this.mNumber = number;
125         this.mDataRoaming = roaming;
126         this.mIconBitmap = icon;
127         this.mMcc = mcc;
128         this.mMnc = mnc;
129         this.mCountryIso = countryIso;
130     }
131 
132     /**
133      * @return the subscription ID.
134      */
getSubscriptionId()135     public int getSubscriptionId() {
136         return this.mId;
137     }
138 
139     /**
140      * @return the ICC ID.
141      */
getIccId()142     public String getIccId() {
143         return this.mIccId;
144     }
145 
146     /**
147      * @return the slot index of this Subscription's SIM card.
148      */
getSimSlotIndex()149     public int getSimSlotIndex() {
150         return this.mSimSlotIndex;
151     }
152 
153     /**
154      * @return the name displayed to the user that identifies this subscription
155      */
getDisplayName()156     public CharSequence getDisplayName() {
157         return this.mDisplayName;
158     }
159 
160     /**
161      * Sets the name displayed to the user that identifies this subscription
162      * @hide
163      */
setDisplayName(CharSequence name)164     public void setDisplayName(CharSequence name) {
165         this.mDisplayName = name;
166     }
167 
168     /**
169      * @return the name displayed to the user that identifies Subscription provider name
170      */
getCarrierName()171     public CharSequence getCarrierName() {
172         return this.mCarrierName;
173     }
174 
175     /**
176      * Sets the name displayed to the user that identifies Subscription provider name
177      * @hide
178      */
setCarrierName(CharSequence name)179     public void setCarrierName(CharSequence name) {
180         this.mCarrierName = name;
181     }
182 
183     /**
184      * @return the source of the name, eg NAME_SOURCE_UNDEFINED, NAME_SOURCE_DEFAULT_SOURCE,
185      * NAME_SOURCE_SIM_SOURCE or NAME_SOURCE_USER_INPUT.
186      * @hide
187      */
getNameSource()188     public int getNameSource() {
189         return this.mNameSource;
190     }
191 
192     /**
193      * Creates and returns an icon {@code Bitmap} to represent this {@code SubscriptionInfo} in a user
194      * interface.
195      *
196      * @param context A {@code Context} to get the {@code DisplayMetrics}s from.
197      *
198      * @return A bitmap icon for this {@code SubscriptionInfo}.
199      */
createIconBitmap(Context context)200     public Bitmap createIconBitmap(Context context) {
201         int width = mIconBitmap.getWidth();
202         int height = mIconBitmap.getHeight();
203         DisplayMetrics metrics = context.getResources().getDisplayMetrics();
204 
205         // Create a new bitmap of the same size because it will be modified.
206         Bitmap workingBitmap = Bitmap.createBitmap(metrics, width, height, mIconBitmap.getConfig());
207 
208         Canvas canvas = new Canvas(workingBitmap);
209         Paint paint = new Paint();
210 
211         // Tint the icon with the color.
212         paint.setColorFilter(new PorterDuffColorFilter(mIconTint, PorterDuff.Mode.SRC_ATOP));
213         canvas.drawBitmap(mIconBitmap, 0, 0, paint);
214         paint.setColorFilter(null);
215 
216         // Write the sim slot index.
217         paint.setAntiAlias(true);
218         paint.setTypeface(Typeface.create("sans-serif", Typeface.NORMAL));
219         paint.setColor(Color.WHITE);
220         // Set text size scaled by density
221         paint.setTextSize(TEXT_SIZE * metrics.density);
222         // Convert sim slot index to localized string
223         final String index = String.format("%d", mSimSlotIndex + 1);
224         final Rect textBound = new Rect();
225         paint.getTextBounds(index, 0, 1, textBound);
226         final float xOffset = (width / 2.f) - textBound.centerX();
227         final float yOffset = (height / 2.f) - textBound.centerY();
228         canvas.drawText(index, xOffset, yOffset, paint);
229 
230         return workingBitmap;
231     }
232 
233     /**
234      * A highlight color to use in displaying information about this {@code PhoneAccount}.
235      *
236      * @return A hexadecimal color value.
237      */
getIconTint()238     public int getIconTint() {
239         return mIconTint;
240     }
241 
242     /**
243      * Sets the color displayed to the user that identifies this subscription
244      * @hide
245      */
setIconTint(int iconTint)246     public void setIconTint(int iconTint) {
247         this.mIconTint = iconTint;
248     }
249 
250     /**
251      * @return the number of this subscription.
252      */
getNumber()253     public String getNumber() {
254         return mNumber;
255     }
256 
257     /**
258      * @return the data roaming state for this subscription, either
259      * {@link SubscriptionManager#DATA_ROAMING_ENABLE} or {@link SubscriptionManager#DATA_ROAMING_DISABLE}.
260      */
getDataRoaming()261     public int getDataRoaming() {
262         return this.mDataRoaming;
263     }
264 
265     /**
266      * @return the MCC.
267      */
getMcc()268     public int getMcc() {
269         return this.mMcc;
270     }
271 
272     /**
273      * @return the MNC.
274      */
getMnc()275     public int getMnc() {
276         return this.mMnc;
277     }
278 
279     /**
280      * @return the ISO country code
281      */
getCountryIso()282     public String getCountryIso() {
283         return this.mCountryIso;
284     }
285 
286     public static final Parcelable.Creator<SubscriptionInfo> CREATOR = new Parcelable.Creator<SubscriptionInfo>() {
287         @Override
288         public SubscriptionInfo createFromParcel(Parcel source) {
289             int id = source.readInt();
290             String iccId = source.readString();
291             int simSlotIndex = source.readInt();
292             CharSequence displayName = source.readCharSequence();
293             CharSequence carrierName = source.readCharSequence();
294             int nameSource = source.readInt();
295             int iconTint = source.readInt();
296             String number = source.readString();
297             int dataRoaming = source.readInt();
298             int mcc = source.readInt();
299             int mnc = source.readInt();
300             String countryIso = source.readString();
301             Bitmap iconBitmap = Bitmap.CREATOR.createFromParcel(source);
302 
303             return new SubscriptionInfo(id, iccId, simSlotIndex, displayName, carrierName,
304                     nameSource, iconTint, number, dataRoaming, iconBitmap, mcc, mnc, countryIso);
305         }
306 
307         @Override
308         public SubscriptionInfo[] newArray(int size) {
309             return new SubscriptionInfo[size];
310         }
311     };
312 
313     @Override
writeToParcel(Parcel dest, int flags)314     public void writeToParcel(Parcel dest, int flags) {
315         dest.writeInt(mId);
316         dest.writeString(mIccId);
317         dest.writeInt(mSimSlotIndex);
318         dest.writeCharSequence(mDisplayName);
319         dest.writeCharSequence(mCarrierName);
320         dest.writeInt(mNameSource);
321         dest.writeInt(mIconTint);
322         dest.writeString(mNumber);
323         dest.writeInt(mDataRoaming);
324         dest.writeInt(mMcc);
325         dest.writeInt(mMnc);
326         dest.writeString(mCountryIso);
327         mIconBitmap.writeToParcel(dest, flags);
328     }
329 
330     @Override
describeContents()331     public int describeContents() {
332         return 0;
333     }
334 
335     @Override
toString()336     public String toString() {
337         return "{id=" + mId + ", iccId=" + mIccId + " simSlotIndex=" + mSimSlotIndex
338                 + " displayName=" + mDisplayName + " carrierName=" + mCarrierName
339                 + " nameSource=" + mNameSource + " iconTint=" + mIconTint
340                 + " dataRoaming=" + mDataRoaming + " iconBitmap=" + mIconBitmap + " mcc " + mMcc
341                 + " mnc " + mMnc + "}";
342     }
343 }
344