blob: 1255452fe3566c81e2a286d9369e23b405be9ac1 [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 {
39 private static final int NO_SUB_ID = -1;
40
41 // Extra on intent containing the id of a subscription.
42 private static final String SUB_ID_EXTRA =
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
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 Lee5ed870c2014-10-29 11:47:49 -070079 intent.putExtra(SubscriptionInfoHelper.SUB_ID_EXTRA, subscription.getSubscriptionId());
80 intent.putExtra(
81 SubscriptionInfoHelper.SUB_LABEL_EXTRA, subscription.getDisplayName().toString());
82 }
83
84 /**
85 * @return Phone object. If a subscription id exists, it returns the phone for the id.
86 */
87 public Phone getPhone() {
88 return hasSubId()
89 ? PhoneFactory.getPhone(SubscriptionManager.getPhoneId(mSubId))
90 : PhoneGlobals.getPhone();
91 }
92
93 /**
94 * Sets the action bar title to the string specified by the given resource id, formatting
95 * it with the subscription label. This assumes the resource string is formattable with a
96 * string-type specifier.
97 *
98 * If the subscription label does not exists, leave the existing title.
99 */
100 public void setActionBarTitle(ActionBar actionBar, Resources res, int resId) {
101 if (actionBar == null || TextUtils.isEmpty(mSubLabel)) {
102 return;
103 }
104
105 String title = String.format(res.getString(resId), mSubLabel);
106 actionBar.setTitle(title);
107 }
108
109 public boolean hasSubId() {
110 return mSubId != NO_SUB_ID;
111 }
112
113 public int getSubId() {
114 return mSubId;
115 }
116}