Andrew Lee | 5ed870c | 2014-10-29 11:47:49 -0700 | [diff] [blame] | 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; |
Ta-wei Yen | a1390d4 | 2017-12-04 15:11:33 -0800 | [diff] [blame] | 23 | import android.telecom.PhoneAccountHandle; |
Wink Saville | 0f3b5fc | 2014-11-11 08:40:49 -0800 | [diff] [blame] | 24 | import android.telephony.SubscriptionInfo; |
Andrew Lee | 5ed870c | 2014-10-29 11:47:49 -0700 | [diff] [blame] | 25 | import android.telephony.SubscriptionManager; |
Andrew Lee | dd4f6df | 2014-12-09 19:13:51 -0800 | [diff] [blame] | 26 | import android.telephony.TelephonyManager; |
Andrew Lee | 5ed870c | 2014-10-29 11:47:49 -0700 | [diff] [blame] | 27 | import android.text.TextUtils; |
| 28 | |
Andrew Lee | 5ed870c | 2014-10-29 11:47:49 -0700 | [diff] [blame] | 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 { |
Andrew Lee | 5ed870c | 2014-10-29 11:47:49 -0700 | [diff] [blame] | 40 | |
| 41 | // Extra on intent containing the id of a subscription. |
Tyler Gunn | 9c1071f | 2014-12-09 10:07:54 -0800 | [diff] [blame] | 42 | public static final String SUB_ID_EXTRA = |
Andrew Lee | 5ed870c | 2014-10-29 11:47:49 -0700 | [diff] [blame] | 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 | |
Ta-wei Yen | 9457490 | 2017-12-11 11:13:07 -0800 | [diff] [blame] | 48 | private Context mContext; |
Andrew Lee | dd4f6df | 2014-12-09 19:13:51 -0800 | [diff] [blame] | 49 | |
Ta-wei Yen | a1390d4 | 2017-12-04 15:11:33 -0800 | [diff] [blame] | 50 | private int mSubId = SubscriptionManager.INVALID_SUBSCRIPTION_ID; |
Ta-wei Yen | 9457490 | 2017-12-11 11:13:07 -0800 | [diff] [blame] | 51 | private String mSubLabel; |
Andrew Lee | 5ed870c | 2014-10-29 11:47:49 -0700 | [diff] [blame] | 52 | |
| 53 | /** |
| 54 | * Instantiates the helper, by extracting the subscription id and label from the intent. |
| 55 | */ |
Andrew Lee | dd4f6df | 2014-12-09 19:13:51 -0800 | [diff] [blame] | 56 | public SubscriptionInfoHelper(Context context, Intent intent) { |
| 57 | mContext = context; |
arunvoddu | 09e1dc2 | 2022-07-28 07:45:16 +0000 | [diff] [blame] | 58 | PhoneAccountHandle phoneAccountHandle = intent.getParcelableExtra( |
| 59 | TelephonyManager.EXTRA_PHONE_ACCOUNT_HANDLE, PhoneAccountHandle.class); |
Ta-wei Yen | a1390d4 | 2017-12-04 15:11:33 -0800 | [diff] [blame] | 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 | } |
Andrew Lee | 5ed870c | 2014-10-29 11:47:49 -0700 | [diff] [blame] | 66 | mSubLabel = intent.getStringExtra(SUB_LABEL_EXTRA); |
| 67 | } |
| 68 | |
| 69 | /** |
Andrew Lee | 5ed870c | 2014-10-29 11:47:49 -0700 | [diff] [blame] | 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 | */ |
Andrew Lee | dd4f6df | 2014-12-09 19:13:51 -0800 | [diff] [blame] | 73 | public Intent getIntent(Class newActivityClass) { |
| 74 | Intent intent = new Intent(mContext, newActivityClass); |
Andrew Lee | 5ed870c | 2014-10-29 11:47:49 -0700 | [diff] [blame] | 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 | |
Wink Saville | 0f3b5fc | 2014-11-11 08:40:49 -0800 | [diff] [blame] | 87 | public static void addExtrasToIntent(Intent intent, SubscriptionInfo subscription) { |
Andrew Lee | 2fcb6c3 | 2014-12-04 14:52:35 -0800 | [diff] [blame] | 88 | if (subscription == null) { |
| 89 | return; |
| 90 | } |
| 91 | |
Andrew Lee | 5ed870c | 2014-10-29 11:47:49 -0700 | [diff] [blame] | 92 | intent.putExtra(SubscriptionInfoHelper.SUB_ID_EXTRA, subscription.getSubscriptionId()); |
| 93 | intent.putExtra( |
Tyler Gunn | e37d04b | 2021-09-13 16:56:37 -0700 | [diff] [blame] | 94 | SubscriptionInfoHelper.SUB_LABEL_EXTRA, subscription.getDisplayName() == null ? null |
| 95 | : subscription.getDisplayName().toString()); |
Andrew Lee | 5ed870c | 2014-10-29 11:47:49 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | /** |
| 99 | * @return Phone object. If a subscription id exists, it returns the phone for the id. |
| 100 | */ |
| 101 | public Phone getPhone() { |
| 102 | return hasSubId() |
| 103 | ? PhoneFactory.getPhone(SubscriptionManager.getPhoneId(mSubId)) |
| 104 | : PhoneGlobals.getPhone(); |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Sets the action bar title to the string specified by the given resource id, formatting |
| 109 | * it with the subscription label. This assumes the resource string is formattable with a |
| 110 | * string-type specifier. |
| 111 | * |
| 112 | * If the subscription label does not exists, leave the existing title. |
| 113 | */ |
| 114 | public void setActionBarTitle(ActionBar actionBar, Resources res, int resId) { |
| 115 | if (actionBar == null || TextUtils.isEmpty(mSubLabel)) { |
| 116 | return; |
| 117 | } |
| 118 | |
Andrew Lee | dd4f6df | 2014-12-09 19:13:51 -0800 | [diff] [blame] | 119 | if (!TelephonyManager.from(mContext).isMultiSimEnabled()) { |
| 120 | return; |
| 121 | } |
| 122 | |
Andrew Lee | 5ed870c | 2014-10-29 11:47:49 -0700 | [diff] [blame] | 123 | String title = String.format(res.getString(resId), mSubLabel); |
| 124 | actionBar.setTitle(title); |
| 125 | } |
| 126 | |
| 127 | public boolean hasSubId() { |
Ta-wei Yen | a1390d4 | 2017-12-04 15:11:33 -0800 | [diff] [blame] | 128 | return mSubId != SubscriptionManager.INVALID_SUBSCRIPTION_ID; |
Andrew Lee | 5ed870c | 2014-10-29 11:47:49 -0700 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | public int getSubId() { |
| 132 | return mSubId; |
| 133 | } |
| 134 | } |