2 * Copyright (C) 2014 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package android.telephony;
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;
33 * A Parcelable class for Subscription Information.
35 public class SubscriptionInfo implements Parcelable {
38 * Size of text to render on the icon.
40 private static final int TEXT_SIZE = 16;
43 * Subscription Identifier, this is a device unique number
44 * and not an index into an array
49 * The GID for a SIM that maybe associated with this subscription, empty if unknown
51 private String mIccId;
54 * The index of the slot that currently contains the subscription
55 * and not necessarily unique and maybe INVALID_SLOT_ID if unknown
57 private int mSimSlotIndex;
60 * The name displayed to the user that identifies this subscription
62 private CharSequence mDisplayName;
65 * String that identifies SPN/PLMN
66 * TODO : Add a new field that identifies only SPN for a sim
68 private CharSequence mCarrierName;
71 * The source of the name, NAME_SOURCE_UNDEFINED, NAME_SOURCE_DEFAULT_SOURCE,
72 * NAME_SOURCE_SIM_SOURCE or NAME_SOURCE_USER_INPUT.
74 private int mNameSource;
77 * The color to be used for tinting the icon when displaying to the user
79 private int mIconTint;
82 * A number presented to the user identify this subscription
84 private String mNumber;
87 * Data roaming state, DATA_RAOMING_ENABLE, DATA_RAOMING_DISABLE
89 private int mDataRoaming;
94 private Bitmap mIconBitmap;
102 * Mobile Network Code
107 * ISO Country code for the subscription's provider
109 private String mCountryIso;
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) {
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;
129 this.mCountryIso = countryIso;
133 * @return the subscription ID.
135 public int getSubscriptionId() {
140 * @return the ICC ID.
142 public String getIccId() {
147 * @return the slot index of this Subscription's SIM card.
149 public int getSimSlotIndex() {
150 return this.mSimSlotIndex;
154 * @return the name displayed to the user that identifies this subscription
156 public CharSequence getDisplayName() {
157 return this.mDisplayName;
161 * Sets the name displayed to the user that identifies this subscription
164 public void setDisplayName(CharSequence name) {
165 this.mDisplayName = name;
169 * @return the name displayed to the user that identifies Subscription provider name
171 public CharSequence getCarrierName() {
172 return this.mCarrierName;
176 * Sets the name displayed to the user that identifies Subscription provider name
179 public void setCarrierName(CharSequence name) {
180 this.mCarrierName = name;
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.
188 public int getNameSource() {
189 return this.mNameSource;
193 * Creates and returns an icon {@code Bitmap} to represent this {@code SubscriptionInfo} in a user
196 * @param context A {@code Context} to get the {@code DisplayMetrics}s from.
198 * @return A bitmap icon for this {@code SubscriptionInfo}.
200 public Bitmap createIconBitmap(Context context) {
201 int width = mIconBitmap.getWidth();
202 int height = mIconBitmap.getHeight();
203 DisplayMetrics metrics = context.getResources().getDisplayMetrics();
205 // Create a new bitmap of the same size because it will be modified.
206 Bitmap workingBitmap = Bitmap.createBitmap(metrics, width, height, mIconBitmap.getConfig());
208 Canvas canvas = new Canvas(workingBitmap);
209 Paint paint = new Paint();
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);
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);
230 return workingBitmap;
234 * A highlight color to use in displaying information about this {@code PhoneAccount}.
236 * @return A hexadecimal color value.
238 public int getIconTint() {
243 * Sets the color displayed to the user that identifies this subscription
246 public void setIconTint(int iconTint) {
247 this.mIconTint = iconTint;
251 * @return the number of this subscription.
253 public String getNumber() {
258 * @return the data roaming state for this subscription, either
259 * {@link SubscriptionManager#DATA_ROAMING_ENABLE} or {@link SubscriptionManager#DATA_ROAMING_DISABLE}.
261 public int getDataRoaming() {
262 return this.mDataRoaming;
268 public int getMcc() {
275 public int getMnc() {
280 * @return the ISO country code
282 public String getCountryIso() {
283 return this.mCountryIso;
286 public static final Parcelable.Creator<SubscriptionInfo> CREATOR = new Parcelable.Creator<SubscriptionInfo>() {
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);
303 return new SubscriptionInfo(id, iccId, simSlotIndex, displayName, carrierName,
304 nameSource, iconTint, number, dataRoaming, iconBitmap, mcc, mnc, countryIso);
308 public SubscriptionInfo[] newArray(int size) {
309 return new SubscriptionInfo[size];
314 public void writeToParcel(Parcel dest, int flags) {
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);
326 dest.writeString(mCountryIso);
327 mIconBitmap.writeToParcel(dest, flags);
331 public int describeContents() {
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 + "}";