• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
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 
17 package com.android.phone;
18 
19 import android.app.ActionBar;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.content.res.Resources;
23 import android.telecom.PhoneAccountHandle;
24 import android.telephony.SubscriptionInfo;
25 import android.telephony.SubscriptionManager;
26 import android.telephony.TelephonyManager;
27 import android.text.TextUtils;
28 
29 import com.android.internal.telephony.Phone;
30 import 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  */
39 public class SubscriptionInfoHelper {
40 
41     // Extra on intent containing the id of a subscription.
42     public 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 Context mContext;
49 
50     private int mSubId = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
51     private String mSubLabel;
52 
53     /**
54      * Instantiates the helper, by extracting the subscription id and label from the intent.
55      */
SubscriptionInfoHelper(Context context, Intent intent)56     public SubscriptionInfoHelper(Context context, Intent intent) {
57         mContext = context;
58         PhoneAccountHandle phoneAccountHandle =
59                 intent.getParcelableExtra(TelephonyManager.EXTRA_PHONE_ACCOUNT_HANDLE);
60         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         }
66         mSubLabel = intent.getStringExtra(SUB_LABEL_EXTRA);
67     }
68 
69     /**
70      * @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      */
getIntent(Class newActivityClass)73     public Intent getIntent(Class newActivityClass) {
74         Intent intent = new Intent(mContext, newActivityClass);
75 
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 
addExtrasToIntent(Intent intent, SubscriptionInfo subscription)87     public static void addExtrasToIntent(Intent intent, SubscriptionInfo subscription) {
88         if (subscription == null) {
89             return;
90         }
91 
92         intent.putExtra(SubscriptionInfoHelper.SUB_ID_EXTRA, subscription.getSubscriptionId());
93         intent.putExtra(
94                 SubscriptionInfoHelper.SUB_LABEL_EXTRA, subscription.getDisplayName() == null ? null
95                         : subscription.getDisplayName().toString());
96     }
97 
98     /**
99      * @return Phone object. If a subscription id exists, it returns the phone for the id.
100      */
getPhone()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      */
setActionBarTitle(ActionBar actionBar, Resources res, int resId)114     public void setActionBarTitle(ActionBar actionBar, Resources res, int resId) {
115         if (actionBar == null || TextUtils.isEmpty(mSubLabel)) {
116             return;
117         }
118 
119         if (!TelephonyManager.from(mContext).isMultiSimEnabled()) {
120             return;
121         }
122 
123         String title = String.format(res.getString(resId), mSubLabel);
124         actionBar.setTitle(title);
125     }
126 
hasSubId()127     public boolean hasSubId() {
128         return mSubId != SubscriptionManager.INVALID_SUBSCRIPTION_ID;
129     }
130 
getSubId()131     public int getSubId() {
132         return mSubId;
133     }
134 }
135