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.telecom.PhoneAccountHandle; 24 import android.telephony.SubscriptionInfo; 25 import android.telephony.SubscriptionManager; 26 import android.telephony.TelephonyManager; 27 import android.text.TextUtils; 28 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 41 // Extra on intent containing the id of a subscription. 42 public static final String SUB_ID_EXTRA = 43 "com.android.phone.settings.SubscriptionInfoHelper.SubscriptionId"; 44 // Extra on intent containing the label of a subscription. 45 private static final String SUB_LABEL_EXTRA = 46 "com.android.phone.settings.SubscriptionInfoHelper.SubscriptionLabel"; 47 48 private Context mContext; 49 50 private int mSubId = SubscriptionManager.INVALID_SUBSCRIPTION_ID; 51 private String mSubLabel; 52 53 /** 54 * Instantiates the helper, by extracting the subscription id and label from the intent. 55 */ SubscriptionInfoHelper(Context context, Intent intent)56 public SubscriptionInfoHelper(Context context, Intent intent) { 57 mContext = context; 58 PhoneAccountHandle phoneAccountHandle = 59 intent.getParcelableExtra(TelephonyManager.EXTRA_PHONE_ACCOUNT_HANDLE); 60 if (phoneAccountHandle != null) { 61 mSubId = PhoneUtils.getSubIdForPhoneAccountHandle(phoneAccountHandle); 62 } 63 if (mSubId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) { 64 mSubId = intent.getIntExtra(SUB_ID_EXTRA, SubscriptionManager.INVALID_SUBSCRIPTION_ID); 65 } 66 mSubLabel = intent.getStringExtra(SUB_LABEL_EXTRA); 67 } 68 69 /** 70 * @param newActivityClass The class of the activity for the intent to start. 71 * @return Intent containing extras for the subscription id and label if they exist. 72 */ getIntent(Class newActivityClass)73 public Intent getIntent(Class newActivityClass) { 74 Intent intent = new Intent(mContext, newActivityClass); 75 76 if (hasSubId()) { 77 intent.putExtra(SUB_ID_EXTRA, mSubId); 78 } 79 80 if (!TextUtils.isEmpty(mSubLabel)) { 81 intent.putExtra(SUB_LABEL_EXTRA, mSubLabel); 82 } 83 84 return intent; 85 } 86 addExtrasToIntent(Intent intent, SubscriptionInfo subscription)87 public static void addExtrasToIntent(Intent intent, SubscriptionInfo subscription) { 88 if (subscription == null) { 89 return; 90 } 91 92 intent.putExtra(SubscriptionInfoHelper.SUB_ID_EXTRA, subscription.getSubscriptionId()); 93 intent.putExtra( 94 SubscriptionInfoHelper.SUB_LABEL_EXTRA, subscription.getDisplayName().toString()); 95 } 96 97 /** 98 * @return Phone object. If a subscription id exists, it returns the phone for the id. 99 */ getPhone()100 public Phone getPhone() { 101 return hasSubId() 102 ? PhoneFactory.getPhone(SubscriptionManager.getPhoneId(mSubId)) 103 : PhoneGlobals.getPhone(); 104 } 105 106 /** 107 * Sets the action bar title to the string specified by the given resource id, formatting 108 * it with the subscription label. This assumes the resource string is formattable with a 109 * string-type specifier. 110 * 111 * If the subscription label does not exists, leave the existing title. 112 */ setActionBarTitle(ActionBar actionBar, Resources res, int resId)113 public void setActionBarTitle(ActionBar actionBar, Resources res, int resId) { 114 if (actionBar == null || TextUtils.isEmpty(mSubLabel)) { 115 return; 116 } 117 118 if (!TelephonyManager.from(mContext).isMultiSimEnabled()) { 119 return; 120 } 121 122 String title = String.format(res.getString(resId), mSubLabel); 123 actionBar.setTitle(title); 124 } 125 hasSubId()126 public boolean hasSubId() { 127 return mSubId != SubscriptionManager.INVALID_SUBSCRIPTION_ID; 128 } 129 getSubId()130 public int getSubId() { 131 return mSubId; 132 } 133 } 134