blob: 8c9edf3ba4db0ef791b9ef95777074688e4f2cec [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;
Wink Saville0f3b5fc2014-11-11 08:40:49 -080023import android.telephony.SubscriptionInfo;
Andrew Lee5ed870c2014-10-29 11:47:49 -070024import android.telephony.SubscriptionManager;
Andrew Leedd4f6df2014-12-09 19:13:51 -080025import android.telephony.TelephonyManager;
Andrew Lee5ed870c2014-10-29 11:47:49 -070026import android.text.TextUtils;
27
Andrew Lee5ed870c2014-10-29 11:47:49 -070028import com.android.internal.telephony.Phone;
29import com.android.internal.telephony.PhoneFactory;
30
31/**
32 * Helper for manipulating intents or components with subscription-related information.
33 *
34 * In settings, subscription ids and labels are passed along to indicate that settings
35 * are being changed for particular subscriptions. This helper provides functions for
36 * helping extract this info and perform common operations using this info.
37 */
38public class SubscriptionInfoHelper {
Tyler Gunn9c1071f2014-12-09 10:07:54 -080039 public static final int NO_SUB_ID = -1;
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 Yen94574902017-12-11 11:13:07 -080050 private int mSubId = NO_SUB_ID;
51 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;
Andrew Lee5ed870c2014-10-29 11:47:49 -070058 mSubId = intent.getIntExtra(SUB_ID_EXTRA, NO_SUB_ID);
59 mSubLabel = intent.getStringExtra(SUB_LABEL_EXTRA);
60 }
61
62 /**
Andrew Lee5ed870c2014-10-29 11:47:49 -070063 * @param newActivityClass The class of the activity for the intent to start.
64 * @return Intent containing extras for the subscription id and label if they exist.
65 */
Andrew Leedd4f6df2014-12-09 19:13:51 -080066 public Intent getIntent(Class newActivityClass) {
67 Intent intent = new Intent(mContext, newActivityClass);
Andrew Lee5ed870c2014-10-29 11:47:49 -070068
69 if (hasSubId()) {
70 intent.putExtra(SUB_ID_EXTRA, mSubId);
71 }
72
73 if (!TextUtils.isEmpty(mSubLabel)) {
74 intent.putExtra(SUB_LABEL_EXTRA, mSubLabel);
75 }
76
77 return intent;
78 }
79
Wink Saville0f3b5fc2014-11-11 08:40:49 -080080 public static void addExtrasToIntent(Intent intent, SubscriptionInfo subscription) {
Andrew Lee2fcb6c32014-12-04 14:52:35 -080081 if (subscription == null) {
82 return;
83 }
84
Andrew Lee5ed870c2014-10-29 11:47:49 -070085 intent.putExtra(SubscriptionInfoHelper.SUB_ID_EXTRA, subscription.getSubscriptionId());
86 intent.putExtra(
87 SubscriptionInfoHelper.SUB_LABEL_EXTRA, subscription.getDisplayName().toString());
88 }
89
90 /**
91 * @return Phone object. If a subscription id exists, it returns the phone for the id.
92 */
93 public Phone getPhone() {
94 return hasSubId()
95 ? PhoneFactory.getPhone(SubscriptionManager.getPhoneId(mSubId))
96 : PhoneGlobals.getPhone();
97 }
98
99 /**
100 * Sets the action bar title to the string specified by the given resource id, formatting
101 * it with the subscription label. This assumes the resource string is formattable with a
102 * string-type specifier.
103 *
104 * If the subscription label does not exists, leave the existing title.
105 */
106 public void setActionBarTitle(ActionBar actionBar, Resources res, int resId) {
107 if (actionBar == null || TextUtils.isEmpty(mSubLabel)) {
108 return;
109 }
110
Andrew Leedd4f6df2014-12-09 19:13:51 -0800111 if (!TelephonyManager.from(mContext).isMultiSimEnabled()) {
112 return;
113 }
114
Andrew Lee5ed870c2014-10-29 11:47:49 -0700115 String title = String.format(res.getString(resId), mSubLabel);
116 actionBar.setTitle(title);
117 }
118
119 public boolean hasSubId() {
120 return mSubId != NO_SUB_ID;
121 }
122
123 public int getSubId() {
124 return mSubId;
125 }
126}