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 com.android.phone; 18 19 import android.app.ActionBar; 20 import android.content.Context; 21 import android.content.Intent; 22 import android.content.res.Resources; 23 import android.telephony.SubscriptionInfo; 24 import android.telephony.SubscriptionManager; 25 import android.telephony.TelephonyManager; 26 import android.text.TextUtils; 27 28 import com.android.phone.PhoneGlobals; 29 import com.android.internal.telephony.Phone; 30 import com.android.internal.telephony.PhoneFactory; 31 32 /** 33 * Helper for manipulating intents or components with subscription-related information. 34 * 35 * In settings, subscription ids and labels are passed along to indicate that settings 36 * are being changed for particular subscriptions. This helper provides functions for 37 * helping extract this info and perform common operations using this info. 38 */ 39 public class SubscriptionInfoHelper { 40 public static final int NO_SUB_ID = -1; 41 42 // Extra on intent containing the id of a subscription. 43 public static final String SUB_ID_EXTRA = 44 "com.android.phone.settings.SubscriptionInfoHelper.SubscriptionId"; 45 // Extra on intent containing the label of a subscription. 46 private static final String SUB_LABEL_EXTRA = 47 "com.android.phone.settings.SubscriptionInfoHelper.SubscriptionLabel"; 48 49 private static Context mContext; 50 51 private static int mSubId = NO_SUB_ID; 52 private static String mSubLabel; 53 54 /** 55 * Instantiates the helper, by extracting the subscription id and label from the intent. 56 */ 57 public SubscriptionInfoHelper(Context context, Intent intent) { 58 mContext = context; 59 mSubId = intent.getIntExtra(SUB_ID_EXTRA, NO_SUB_ID); 60 mSubLabel = intent.getStringExtra(SUB_LABEL_EXTRA); 61 } 62 63 /** 64 * @param newActivityClass The class of the activity for the intent to start. 65 * @return Intent containing extras for the subscription id and label if they exist. 66 */ 67 public Intent getIntent(Class newActivityClass) { 68 Intent intent = new Intent(mContext, newActivityClass); 69 70 if (hasSubId()) { 71 intent.putExtra(SUB_ID_EXTRA, mSubId); 72 } 73 74 if (!TextUtils.isEmpty(mSubLabel)) { 75 intent.putExtra(SUB_LABEL_EXTRA, mSubLabel); 76 } 77 78 return intent; 79 } 80 81 public static void addExtrasToIntent(Intent intent, SubscriptionInfo subscription) { 82 if (subscription == null) { 83 return; 84 } 85 86 intent.putExtra(SubscriptionInfoHelper.SUB_ID_EXTRA, subscription.getSubscriptionId()); 87 intent.putExtra( 88 SubscriptionInfoHelper.SUB_LABEL_EXTRA, subscription.getDisplayName().toString()); 89 } 90 91 /** 92 * @return Phone object. If a subscription id exists, it returns the phone for the id. 93 */ 94 public Phone getPhone() { 95 return hasSubId() 96 ? PhoneFactory.getPhone(SubscriptionManager.getPhoneId(mSubId)) 97 : PhoneGlobals.getPhone(); 98 } 99 100 /** 101 * Sets the action bar title to the string specified by the given resource id, formatting 102 * it with the subscription label. This assumes the resource string is formattable with a 103 * string-type specifier. 104 * 105 * If the subscription label does not exists, leave the existing title. 106 */ 107 public void setActionBarTitle(ActionBar actionBar, Resources res, int resId) { 108 if (actionBar == null || TextUtils.isEmpty(mSubLabel)) { 109 return; 110 } 111 112 if (!TelephonyManager.from(mContext).isMultiSimEnabled()) { 113 return; 114 } 115 116 String title = String.format(res.getString(resId), mSubLabel); 117 actionBar.setTitle(title); 118 } 119 120 public boolean hasSubId() { 121 return mSubId != NO_SUB_ID; 122 } 123 124 public int getSubId() { 125 return mSubId; 126 } 127 } 128