blob: f325b1a186c24f3fd510620b4b6b7269fe748747 [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;
25import android.text.TextUtils;
26
27import com.android.phone.PhoneGlobals;
28import 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
48 private static int mSubId = NO_SUB_ID;
49 private static String mSubLabel;
50
51 /**
52 * Instantiates the helper, by extracting the subscription id and label from the intent.
53 */
54 public SubscriptionInfoHelper(Intent intent) {
55 mSubId = intent.getIntExtra(SUB_ID_EXTRA, NO_SUB_ID);
56 mSubLabel = intent.getStringExtra(SUB_LABEL_EXTRA);
57 }
58
59 /**
60 * @param context The context.
61 * @param newActivityClass The class of the activity for the intent to start.
62 * @return Intent containing extras for the subscription id and label if they exist.
63 */
64 public Intent getIntent(Context context, Class newActivityClass) {
65 Intent intent = new Intent(context, newActivityClass);
66
67 if (hasSubId()) {
68 intent.putExtra(SUB_ID_EXTRA, mSubId);
69 }
70
71 if (!TextUtils.isEmpty(mSubLabel)) {
72 intent.putExtra(SUB_LABEL_EXTRA, mSubLabel);
73 }
74
75 return intent;
76 }
77
Wink Saville0f3b5fc2014-11-11 08:40:49 -080078 public static void addExtrasToIntent(Intent intent, SubscriptionInfo subscription) {
Andrew Lee2fcb6c32014-12-04 14:52:35 -080079 if (subscription == null) {
80 return;
81 }
82
Andrew Lee5ed870c2014-10-29 11:47:49 -070083 intent.putExtra(SubscriptionInfoHelper.SUB_ID_EXTRA, subscription.getSubscriptionId());
84 intent.putExtra(
85 SubscriptionInfoHelper.SUB_LABEL_EXTRA, subscription.getDisplayName().toString());
86 }
87
88 /**
89 * @return Phone object. If a subscription id exists, it returns the phone for the id.
90 */
91 public Phone getPhone() {
92 return hasSubId()
93 ? PhoneFactory.getPhone(SubscriptionManager.getPhoneId(mSubId))
94 : PhoneGlobals.getPhone();
95 }
96
97 /**
98 * Sets the action bar title to the string specified by the given resource id, formatting
99 * it with the subscription label. This assumes the resource string is formattable with a
100 * string-type specifier.
101 *
102 * If the subscription label does not exists, leave the existing title.
103 */
104 public void setActionBarTitle(ActionBar actionBar, Resources res, int resId) {
105 if (actionBar == null || TextUtils.isEmpty(mSubLabel)) {
106 return;
107 }
108
109 String title = String.format(res.getString(resId), mSubLabel);
110 actionBar.setTitle(title);
111 }
112
113 public boolean hasSubId() {
114 return mSubId != NO_SUB_ID;
115 }
116
117 public int getSubId() {
118 return mSubId;
119 }
120}