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 * 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 */ 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)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 */ getSubscriptionId()134 public int getSubscriptionId() { 135 return this.mId; 136 } 137 138 /** 139 * @return the ICC ID. 140 */ getIccId()141 public String getIccId() { 142 return this.mIccId; 143 } 144 145 /** 146 * @return the slot index of this Subscription's SIM card. 147 */ getSimSlotIndex()148 public int getSimSlotIndex() { 149 return this.mSimSlotIndex; 150 } 151 152 /** 153 * @return the name displayed to the user that identifies this subscription 154 */ getDisplayName()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 */ setDisplayName(CharSequence name)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 */ getCarrierName()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 */ setCarrierName(CharSequence name)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 */ getNameSource()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 */ createIconBitmap(Context context)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 */ getIconTint()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 */ setIconTint(int iconTint)245 public void setIconTint(int iconTint) { 246 this.mIconTint = iconTint; 247 } 248 249 /** 250 * @return the number of this subscription. 251 */ getNumber()252 public String getNumber() { 253 return mNumber; 254 } 255 256 /** 257 * @return the data roaming state for this subscription, either 258 * {@link SubscriptionManager#DATA_ROAMING_ENABLE} or {@link SubscriptionManager#DATA_ROAMING_DISABLE}. 259 */ getDataRoaming()260 public int getDataRoaming() { 261 return this.mDataRoaming; 262 } 263 264 /** 265 * @return the MCC. 266 */ getMcc()267 public int getMcc() { 268 return this.mMcc; 269 } 270 271 /** 272 * @return the MNC. 273 */ getMnc()274 public int getMnc() { 275 return this.mMnc; 276 } 277 278 /** 279 * @return the ISO country code 280 */ getCountryIso()281 public String getCountryIso() { 282 return this.mCountryIso; 283 } 284 285 public static final Parcelable.Creator<SubscriptionInfo> CREATOR = new Parcelable.Creator<SubscriptionInfo>() { 286 @Override 287 public SubscriptionInfo createFromParcel(Parcel source) { 288 int id = source.readInt(); 289 String iccId = source.readString(); 290 int simSlotIndex = source.readInt(); 291 CharSequence displayName = source.readCharSequence(); 292 CharSequence carrierName = source.readCharSequence(); 293 int nameSource = source.readInt(); 294 int iconTint = source.readInt(); 295 String number = source.readString(); 296 int dataRoaming = source.readInt(); 297 int mcc = source.readInt(); 298 int mnc = source.readInt(); 299 String countryIso = source.readString(); 300 Bitmap iconBitmap = Bitmap.CREATOR.createFromParcel(source); 301 302 return new SubscriptionInfo(id, iccId, simSlotIndex, displayName, carrierName, 303 nameSource, iconTint, number, dataRoaming, iconBitmap, mcc, mnc, countryIso); 304 } 305 306 @Override 307 public SubscriptionInfo[] newArray(int size) { 308 return new SubscriptionInfo[size]; 309 } 310 }; 311 312 @Override writeToParcel(Parcel dest, int flags)313 public void writeToParcel(Parcel dest, int flags) { 314 dest.writeInt(mId); 315 dest.writeString(mIccId); 316 dest.writeInt(mSimSlotIndex); 317 dest.writeCharSequence(mDisplayName); 318 dest.writeCharSequence(mCarrierName); 319 dest.writeInt(mNameSource); 320 dest.writeInt(mIconTint); 321 dest.writeString(mNumber); 322 dest.writeInt(mDataRoaming); 323 dest.writeInt(mMcc); 324 dest.writeInt(mMnc); 325 dest.writeString(mCountryIso); 326 mIconBitmap.writeToParcel(dest, flags); 327 } 328 329 @Override describeContents()330 public int describeContents() { 331 return 0; 332 } 333 334 @Override toString()335 public String toString() { 336 return "{id=" + mId + ", iccId=" + mIccId + " simSlotIndex=" + mSimSlotIndex 337 + " displayName=" + mDisplayName + " carrierName=" + mCarrierName 338 + " nameSource=" + mNameSource + " iconTint=" + mIconTint 339 + " dataRoaming=" + mDataRoaming + " iconBitmap=" + mIconBitmap + " mcc " + mMcc 340 + " mnc " + mMnc + "}"; 341 } 342 } 343