blob: 9f0ebd0193039c03a9c243ff3d6a4c1d2fd066c9 [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
28import com.android.phone.PhoneGlobals;
29import 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 {
Tyler Gunn9c1071f2014-12-09 10:07:54 -080040 public static final int NO_SUB_ID = -1;
Andrew Lee5ed870c2014-10-29 11:47:49 -070041
42 // Extra on intent containing the id of a subscription.
Tyler Gunn9c1071f2014-12-09 10:07:54 -080043 public static final String SUB_ID_EXTRA =
Andrew Lee5ed870c2014-10-29 11:47:49 -070044 "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
Andrew Leedd4f6df2014-12-09 19:13:51 -080049 private static Context mContext;
50
Andrew Lee5ed870c2014-10-29 11:47:49 -070051 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 */
Andrew Leedd4f6df2014-12-09 19:13:51 -080057 public SubscriptionInfoHelper(Context context, Intent intent) {
58 mContext = context;
Andrew Lee5ed870c2014-10-29 11:47:49 -070059 mSubId = intent.getIntExtra(SUB_ID_EXTRA, NO_SUB_ID);
60 mSubLabel = intent.getStringExtra(SUB_LABEL_EXTRA);
61 }
62
63 /**
Andrew Lee5ed870c2014-10-29 11:47:49 -070064 * @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 */
Andrew Leedd4f6df2014-12-09 19:13:51 -080067 public Intent getIntent(Class newActivityClass) {
68 Intent intent = new Intent(mContext, newActivityClass);
Andrew Lee5ed870c2014-10-29 11:47:49 -070069
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
Wink Saville0f3b5fc2014-11-11 08:40:49 -080081 public static void addExtrasToIntent(Intent intent, SubscriptionInfo subscription) {
Andrew Lee2fcb6c32014-12-04 14:52:35 -080082 if (subscription == null) {
83 return;
84 }
85
Andrew Lee5ed870c2014-10-29 11:47:49 -070086 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
Andrew Leedd4f6df2014-12-09 19:13:51 -0800112 if (!TelephonyManager.from(mContext).isMultiSimEnabled()) {
113 return;
114 }
115
Andrew Lee5ed870c2014-10-29 11:47:49 -0700116 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}