SubscriptionInfo.java revision 4abdd079498801edf6d144ba6d6965d4cb00e91e
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
17package android.telephony;
18
19import android.content.Context;
20import android.graphics.Bitmap;
21import android.graphics.Canvas;
22import android.graphics.Color;
23import android.graphics.Paint;
24import android.graphics.PorterDuff;
25import android.graphics.PorterDuffColorFilter;
26import android.graphics.Rect;
27import android.graphics.Typeface;
28import android.os.Parcel;
29import android.os.Parcelable;
30import android.util.DisplayMetrics;
31
32/**
33 * A Parcelable class for Subscription Information.
34 */
35public 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     * The string displayed to the user that identifies Subscription Provider Name
66     */
67    private CharSequence mCarrierName;
68
69    /**
70     * The source of the name, NAME_SOURCE_UNDEFINED, NAME_SOURCE_DEFAULT_SOURCE,
71     * NAME_SOURCE_SIM_SOURCE or NAME_SOURCE_USER_INPUT.
72     */
73    private int mNameSource;
74
75    /**
76     * The color to be used for tinting the icon when displaying to the user
77     */
78    private int mIconTint;
79
80    /**
81     * A number presented to the user identify this subscription
82     */
83    private String mNumber;
84
85    /**
86     * Data roaming state, DATA_RAOMING_ENABLE, DATA_RAOMING_DISABLE
87     */
88    private int mDataRoaming;
89
90    /**
91     * SIM Icon bitmap
92     */
93    private Bitmap mIconBitmap;
94
95    /**
96     * Mobile Country Code
97     */
98    private int mMcc;
99
100    /**
101     * Mobile Network Code
102     */
103    private int mMnc;
104
105    /**
106     * ISO Country code for the subscription's provider
107     */
108    private String mCountryIso;
109
110    /**
111     * @hide
112     */
113    public SubscriptionInfo(int id, String iccId, int simSlotIndex, CharSequence displayName,
114            CharSequence carrierName, int nameSource, int iconTint, String number, int roaming,
115            Bitmap icon, int mcc, int mnc, String countryIso) {
116        this.mId = id;
117        this.mIccId = iccId;
118        this.mSimSlotIndex = simSlotIndex;
119        this.mDisplayName = displayName;
120        this.mCarrierName = carrierName;
121        this.mNameSource = nameSource;
122        this.mIconTint = iconTint;
123        this.mNumber = number;
124        this.mDataRoaming = roaming;
125        this.mIconBitmap = icon;
126        this.mMcc = mcc;
127        this.mMnc = mnc;
128        this.mCountryIso = countryIso;
129    }
130
131    /**
132     * @return the subscription ID.
133     */
134    public int getSubscriptionId() {
135        return this.mId;
136    }
137
138    /**
139     * @return the ICC ID.
140     */
141    public String getIccId() {
142        return this.mIccId;
143    }
144
145    /**
146     * @return the slot index of this Subscription's SIM card.
147     */
148    public int getSimSlotIndex() {
149        return this.mSimSlotIndex;
150    }
151
152    /**
153     * @return the name displayed to the user that identifies this subscription
154     */
155    public CharSequence getDisplayName() {
156        return this.mDisplayName;
157    }
158
159    /**
160     * Sets the name displayed to the user that identifies this subscription
161     * @hide
162     */
163    public void setDisplayName(CharSequence name) {
164        this.mDisplayName = name;
165    }
166
167    /**
168     * @return the name displayed to the user that identifies Subscription provider name
169     */
170    public CharSequence getCarrierName() {
171        return this.mCarrierName;
172    }
173
174    /**
175     * Sets the name displayed to the user that identifies Subscription provider name
176     * @hide
177     */
178    public void setCarrierName(CharSequence name) {
179        this.mCarrierName = name;
180    }
181
182    /**
183     * @return the source of the name, eg NAME_SOURCE_UNDEFINED, NAME_SOURCE_DEFAULT_SOURCE,
184     * NAME_SOURCE_SIM_SOURCE or NAME_SOURCE_USER_INPUT.
185     * @hide
186     */
187    public int getNameSource() {
188        return this.mNameSource;
189    }
190
191    /**
192     * Creates and returns an icon {@code Bitmap} to represent this {@code SubscriptionInfo} in a user
193     * interface.
194     *
195     * @param context A {@code Context} to get the {@code DisplayMetrics}s from.
196     *
197     * @return A bitmap icon for this {@code SubscriptionInfo}.
198     */
199    public Bitmap createIconBitmap(Context context) {
200        int width = mIconBitmap.getWidth();
201        int height = mIconBitmap.getHeight();
202        DisplayMetrics metrics = context.getResources().getDisplayMetrics();
203
204        // Create a new bitmap of the same size because it will be modified.
205        Bitmap workingBitmap = Bitmap.createBitmap(metrics, width, height, mIconBitmap.getConfig());
206
207        Canvas canvas = new Canvas(workingBitmap);
208        Paint paint = new Paint();
209
210        // Tint the icon with the color.
211        paint.setColorFilter(new PorterDuffColorFilter(mIconTint, PorterDuff.Mode.SRC_ATOP));
212        canvas.drawBitmap(mIconBitmap, 0, 0, paint);
213        paint.setColorFilter(null);
214
215        // Write the sim slot index.
216        paint.setAntiAlias(true);
217        paint.setTypeface(Typeface.create("sans-serif", Typeface.NORMAL));
218        paint.setColor(Color.WHITE);
219        // Set text size scaled by density
220        paint.setTextSize(TEXT_SIZE * metrics.density);
221        // Convert sim slot index to localized string
222        final String index = String.format("%d", mSimSlotIndex + 1);
223        final Rect textBound = new Rect();
224        paint.getTextBounds(index, 0, 1, textBound);
225        final float xOffset = (width / 2.f) - textBound.centerX();
226        final float yOffset = (height / 2.f) - textBound.centerY();
227        canvas.drawText(index, xOffset, yOffset, paint);
228
229        return workingBitmap;
230    }
231
232    /**
233     * A highlight color to use in displaying information about this {@code PhoneAccount}.
234     *
235     * @return A hexadecimal color value.
236     */
237    public int getIconTint() {
238        return mIconTint;
239    }
240
241    /**
242     * Sets the color displayed to the user that identifies this subscription
243     * @hide
244     */
245    public void setIconTint(int iconTint) {
246        this.mIconTint = iconTint;
247    }
248
249    /**
250     * @return the number of this subscription.
251     */
252    public String getNumber() {
253        return mNumber;
254    }
255
256    /**
257     * @return the data roaming state for this subscription, either DATA_ROAMING_ENABLE or
258     * DATA_ROAMING_DISABLE.
259     * @hide
260     */
261    public int getDataRoaming() {
262        return this.mDataRoaming;
263    }
264
265    /**
266     * @return the MCC.
267     */
268    public int getMcc() {
269        return this.mMcc;
270    }
271
272    /**
273     * @return the MNC.
274     */
275    public int getMnc() {
276        return this.mMnc;
277    }
278
279    /**
280     * @return the ISO country code
281     */
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
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
331    public int describeContents() {
332        return 0;
333    }
334
335    @Override
336    public String toString() {
337        return "{id=" + mId + ", iccId=" + mIccId + " simSlotIndex=" + mSimSlotIndex
338                + " displayName=" + mDisplayName + " carrierName=" + mCarrierName
339                + " nameSource=" + mNameSource + " iconTint=" + mIconTint + " number=" + mNumber
340                + " dataRoaming=" + mDataRoaming + " iconBitmap=" + mIconBitmap + " mcc " + mMcc
341                + " mnc " + mMnc + "}";
342    }
343}
344