SubscriptionInfo.java revision 7151cde3ca742d24ea97f163526519b83431c87e
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 * Returns the subscription ID. 133 */ 134 public int getSubscriptionId() { 135 return this.mId; 136 } 137 138 /** 139 * Returns the ICC ID. 140 */ 141 public String getIccId() { 142 return this.mIccId; 143 } 144 145 /** 146 * Returns the slot index of this Subscription's SIM card. 147 */ 148 public int getSimSlotIndex() { 149 return this.mSimSlotIndex; 150 } 151 152 /** 153 * Returns 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 * Returns 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 */ 186 public int getNameSource() { 187 return this.mNameSource; 188 } 189 190 /** 191 * Creates and returns an icon {@code Bitmap} to represent this {@code SubscriptionInfo} in a user 192 * interface. 193 * 194 * @param context A {@code Context} to get the {@code DisplayMetrics}s from. 195 * 196 * @return A bitmap icon for this {@code SubscriptionInfo}. 197 */ 198 public Bitmap createIconBitmap(Context context) { 199 int width = mIconBitmap.getWidth(); 200 int height = mIconBitmap.getHeight(); 201 DisplayMetrics metrics = context.getResources().getDisplayMetrics(); 202 203 // Create a new bitmap of the same size because it will be modified. 204 Bitmap workingBitmap = Bitmap.createBitmap(metrics, width, height, mIconBitmap.getConfig()); 205 206 Canvas canvas = new Canvas(workingBitmap); 207 Paint paint = new Paint(); 208 209 // Tint the icon with the color. 210 paint.setColorFilter(new PorterDuffColorFilter(mIconTint, PorterDuff.Mode.SRC_ATOP)); 211 canvas.drawBitmap(mIconBitmap, 0, 0, paint); 212 paint.setColorFilter(null); 213 214 // Write the sim slot index. 215 paint.setAntiAlias(true); 216 paint.setTypeface(Typeface.create("sans-serif", Typeface.NORMAL)); 217 paint.setColor(Color.WHITE); 218 // Set text size scaled by density 219 paint.setTextSize(TEXT_SIZE * metrics.density); 220 // Convert sim slot index to localized string 221 final String index = String.format("%d", mSimSlotIndex + 1); 222 final Rect textBound = new Rect(); 223 paint.getTextBounds(index, 0, 1, textBound); 224 final float xOffset = (width / 2.f) - textBound.centerX(); 225 final float yOffset = (height / 2.f) - textBound.centerY(); 226 canvas.drawText(index, xOffset, yOffset, paint); 227 228 return workingBitmap; 229 } 230 231 /** 232 * A highlight color to use in displaying information about this {@code PhoneAccount}. 233 * 234 * @return A hexadecimal color value. 235 */ 236 public int getIconTint() { 237 return mIconTint; 238 } 239 240 /** 241 * Sets the color displayed to the user that identifies this subscription 242 * @hide 243 */ 244 public void setIconTint(int iconTint) { 245 this.mIconTint = iconTint; 246 } 247 248 /** 249 * Returns the number of this subscription. 250 */ 251 public String getNumber() { 252 return mNumber; 253 } 254 255 /** 256 * Return the data roaming value. 257 */ 258 public int getDataRoaming() { 259 return this.mDataRoaming; 260 } 261 262 /** 263 * Returns the MCC. 264 */ 265 public int getMcc() { 266 return this.mMcc; 267 } 268 269 /** 270 * Returns the MNC. 271 */ 272 public int getMnc() { 273 return this.mMnc; 274 } 275 276 /** 277 * Returns the ISO country code 278 */ 279 public String getCountryIso() { 280 return this.mCountryIso; 281 } 282 283 public static final Parcelable.Creator<SubscriptionInfo> CREATOR = new Parcelable.Creator<SubscriptionInfo>() { 284 @Override 285 public SubscriptionInfo createFromParcel(Parcel source) { 286 int id = source.readInt(); 287 String iccId = source.readString(); 288 int simSlotIndex = source.readInt(); 289 CharSequence displayName = source.readCharSequence(); 290 CharSequence carrierName = source.readCharSequence(); 291 int nameSource = source.readInt(); 292 int iconTint = source.readInt(); 293 String number = source.readString(); 294 int dataRoaming = source.readInt(); 295 int mcc = source.readInt(); 296 int mnc = source.readInt(); 297 String countryIso = source.readString(); 298 Bitmap iconBitmap = Bitmap.CREATOR.createFromParcel(source); 299 300 return new SubscriptionInfo(id, iccId, simSlotIndex, displayName, carrierName, 301 nameSource, iconTint, number, dataRoaming, iconBitmap, mcc, mnc, countryIso); 302 } 303 304 @Override 305 public SubscriptionInfo[] newArray(int size) { 306 return new SubscriptionInfo[size]; 307 } 308 }; 309 310 @Override 311 public void writeToParcel(Parcel dest, int flags) { 312 dest.writeInt(mId); 313 dest.writeString(mIccId); 314 dest.writeInt(mSimSlotIndex); 315 dest.writeCharSequence(mDisplayName); 316 dest.writeCharSequence(mCarrierName); 317 dest.writeInt(mNameSource); 318 dest.writeInt(mIconTint); 319 dest.writeString(mNumber); 320 dest.writeInt(mDataRoaming); 321 dest.writeInt(mMcc); 322 dest.writeInt(mMnc); 323 dest.writeString(mCountryIso); 324 mIconBitmap.writeToParcel(dest, flags); 325 } 326 327 @Override 328 public int describeContents() { 329 return 0; 330 } 331 332 @Override 333 public String toString() { 334 return "{id=" + mId + ", iccId=" + mIccId + " simSlotIndex=" + mSimSlotIndex 335 + " displayName=" + mDisplayName + " carrierName=" + mCarrierName 336 + " nameSource=" + mNameSource + " iconTint=" + mIconTint + " number=" + mNumber 337 + " dataRoaming=" + mDataRoaming + " iconBitmap=" + mIconBitmap + " mcc " + mMcc 338 + " mnc " + mMnc + "}"; 339 } 340} 341