Andrew Lee | 5ed870c | 2014-10-29 11:47:49 -0700 | [diff] [blame] | 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; |
Wink Saville | 0f3b5fc | 2014-11-11 08:40:49 -0800 | [diff] [blame] | 23 | import android.telephony.SubscriptionInfo; |
Andrew Lee | 5ed870c | 2014-10-29 11:47:49 -0700 | [diff] [blame] | 24 | import android.telephony.SubscriptionManager; |
| 25 | import android.text.TextUtils; |
| 26 | |
| 27 | import com.android.phone.PhoneGlobals; |
| 28 | import com.android.internal.telephony.Phone; |
| 29 | import 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 | */ |
| 38 | public 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 Saville | 0f3b5fc | 2014-11-11 08:40:49 -0800 | [diff] [blame] | 78 | public static void addExtrasToIntent(Intent intent, SubscriptionInfo subscription) { |
Andrew Lee | 5ed870c | 2014-10-29 11:47:49 -0700 | [diff] [blame] | 79 | 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 | } |