1 /** 2 * Copyright (C) 2014 The Android Open Source Project 3 * 4 * <p>Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file 5 * except in compliance with the License. You may obtain a copy of the License at 6 * 7 * <p>http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * <p>Unless required by applicable law or agreed to in writing, software distributed under the 10 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either 11 * express or implied. See the License for the specific language governing permissions and 12 * limitations under the License 13 */ 14 package com.android.voicemail.impl; 15 16 import android.app.ActionBar; 17 import android.content.Context; 18 import android.content.Intent; 19 import android.content.res.Resources; 20 import android.text.TextUtils; 21 22 /** 23 * Helper for manipulating intents or components with subscription-related information. 24 * 25 * <p>In settings, subscription ids and labels are passed along to indicate that settings are being 26 * changed for particular subscriptions. This helper provides functions for helping extract this 27 * info and perform common operations using this info. 28 */ 29 public class SubscriptionInfoHelper { 30 public static final int NO_SUB_ID = -1; 31 32 // Extra on intent containing the id of a subscription. 33 public static final String SUB_ID_EXTRA = 34 "com.android.voicemailomtp.settings.SubscriptionInfoHelper.SubscriptionId"; 35 // Extra on intent containing the label of a subscription. 36 private static final String SUB_LABEL_EXTRA = 37 "com.android.voicemailomtp.settings.SubscriptionInfoHelper.SubscriptionLabel"; 38 39 private static Context mContext; 40 41 private static int mSubId = NO_SUB_ID; 42 private static String mSubLabel; 43 44 /** Instantiates the helper, by extracting the subscription id and label from the intent. */ SubscriptionInfoHelper(Context context, Intent intent)45 public SubscriptionInfoHelper(Context context, Intent intent) { 46 mContext = context; 47 mSubId = intent.getIntExtra(SUB_ID_EXTRA, NO_SUB_ID); 48 mSubLabel = intent.getStringExtra(SUB_LABEL_EXTRA); 49 } 50 51 /** 52 * Sets the action bar title to the string specified by the given resource id, formatting it with 53 * the subscription label. This assumes the resource string is formattable with a string-type 54 * specifier. 55 * 56 * <p>If the subscription label does not exists, leave the existing title. 57 */ setActionBarTitle(ActionBar actionBar, Resources res, int resId)58 public void setActionBarTitle(ActionBar actionBar, Resources res, int resId) { 59 if (actionBar == null || TextUtils.isEmpty(mSubLabel)) { 60 return; 61 } 62 63 String title = String.format(res.getString(resId), mSubLabel); 64 actionBar.setTitle(title); 65 } 66 getSubId()67 public int getSubId() { 68 return mSubId; 69 } 70 } 71