blob: f964f820379510a151eac7e4db2aae5850c5e6e8 [file] [log] [blame]
Andrew Lee5ed870c2014-10-29 11:47:49 -07001/**
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 com.android.phone;
18
19import android.app.ActionBar;
20import android.content.Context;
21import android.content.Intent;
22import android.content.res.Resources;
Ta-wei Yena1390d42017-12-04 15:11:33 -080023import android.telecom.PhoneAccountHandle;
Wink Saville0f3b5fc2014-11-11 08:40:49 -080024import android.telephony.SubscriptionInfo;
Andrew Lee5ed870c2014-10-29 11:47:49 -070025import android.telephony.SubscriptionManager;
Andrew Leedd4f6df2014-12-09 19:13:51 -080026import android.telephony.TelephonyManager;
Andrew Lee5ed870c2014-10-29 11:47:49 -070027import android.text.TextUtils;
28
Andrew Lee5ed870c2014-10-29 11:47:49 -070029import com.android.internal.telephony.Phone;
30import 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 */
39public class SubscriptionInfoHelper {
Andrew Lee5ed870c2014-10-29 11:47:49 -070040
41 // Extra on intent containing the id of a subscription.
Tyler Gunn9c1071f2014-12-09 10:07:54 -080042 public static final String SUB_ID_EXTRA =
Andrew Lee5ed870c2014-10-29 11:47:49 -070043 "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 Yen94574902017-12-11 11:13:07 -080048 private Context mContext;
Andrew Leedd4f6df2014-12-09 19:13:51 -080049
Ta-wei Yena1390d42017-12-04 15:11:33 -080050 private int mSubId = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
Ta-wei Yen94574902017-12-11 11:13:07 -080051 private String mSubLabel;
Andrew Lee5ed870c2014-10-29 11:47:49 -070052
53 /**
54 * Instantiates the helper, by extracting the subscription id and label from the intent.
55 */
Andrew Leedd4f6df2014-12-09 19:13:51 -080056 public SubscriptionInfoHelper(Context context, Intent intent) {
57 mContext = context;
arunvoddu09e1dc22022-07-28 07:45:16 +000058 PhoneAccountHandle phoneAccountHandle = intent.getParcelableExtra(
59 TelephonyManager.EXTRA_PHONE_ACCOUNT_HANDLE, PhoneAccountHandle.class);
Ta-wei Yena1390d42017-12-04 15:11:33 -080060 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 Lee5ed870c2014-10-29 11:47:49 -070066 mSubLabel = intent.getStringExtra(SUB_LABEL_EXTRA);
67 }
68
69 /**
Andrew Lee5ed870c2014-10-29 11:47:49 -070070 * @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 Leedd4f6df2014-12-09 19:13:51 -080073 public Intent getIntent(Class newActivityClass) {
74 Intent intent = new Intent(mContext, newActivityClass);
Andrew Lee5ed870c2014-10-29 11:47:49 -070075
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 Saville0f3b5fc2014-11-11 08:40:49 -080087 public static void addExtrasToIntent(Intent intent, SubscriptionInfo subscription) {
Andrew Lee2fcb6c32014-12-04 14:52:35 -080088 if (subscription == null) {
89 return;
90 }
91
Andrew Lee5ed870c2014-10-29 11:47:49 -070092 intent.putExtra(SubscriptionInfoHelper.SUB_ID_EXTRA, subscription.getSubscriptionId());
93 intent.putExtra(
Tyler Gunne37d04b2021-09-13 16:56:37 -070094 SubscriptionInfoHelper.SUB_LABEL_EXTRA, subscription.getDisplayName() == null ? null
95 : subscription.getDisplayName().toString());
Andrew Lee5ed870c2014-10-29 11:47:49 -070096 }
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 Leedd4f6df2014-12-09 19:13:51 -0800119 if (!TelephonyManager.from(mContext).isMultiSimEnabled()) {
120 return;
121 }
122
Andrew Lee5ed870c2014-10-29 11:47:49 -0700123 String title = String.format(res.getString(resId), mSubLabel);
124 actionBar.setTitle(title);
125 }
126
127 public boolean hasSubId() {
Ta-wei Yena1390d42017-12-04 15:11:33 -0800128 return mSubId != SubscriptionManager.INVALID_SUBSCRIPTION_ID;
Andrew Lee5ed870c2014-10-29 11:47:49 -0700129 }
130
131 public int getSubId() {
132 return mSubId;
133 }
134}