1 /* 2 * Copyright (C) 2010 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.contacts; 18 19 import android.content.Context; 20 import android.content.Intent; 21 import android.content.SharedPreferences; 22 import android.provider.ContactsContract.QuickContact; 23 import android.util.Log; 24 25 /** 26 * Utility class to annotate Intents with extra data required for the Sticky-Tab behavior, which 27 * allows storing the app to go to the last tab that was used to make a call. Also handles saving 28 * and restoring the tab index 29 */ 30 public final class StickyTabs { 31 private static final String TAG = "StickyTabs"; 32 private static final boolean LOGV = false; 33 34 private static final String EXTRA_TAB_INDEX = 35 QuickContact.EXTRA_SELECTED_CONTACTS_APP_TAB_INDEX; 36 37 /** 38 * Name of the shared setting. We are using the same name as in FroYo to prevent 39 * having an orphan here 40 */ 41 public static final String PREFERENCES_NAME = "dialtacts"; 42 43 /** 44 * Name of the shared setting. We are using the same name as in FroYo to prevent 45 * having an orphan there 46 */ 47 private static final String PREF_LAST_PHONECALL_TAB = "last_manually_selected_tab"; 48 49 /** 50 * Writes the selected tab to the passed intent 51 * @param intent The intent to modify. 52 * @param tabIndex The tab index to write to the intent 53 * @return Returns the modified intent. Notice that this is not a new instance (the passed-in 54 * intent is modified) 55 */ setTab(Intent intent, int tabIndex)56 public static Intent setTab(Intent intent, int tabIndex) { 57 if (LOGV) Log.v(TAG, "*********** Setting tab index of intent to " + tabIndex); 58 59 if (tabIndex == -1) { 60 intent.removeExtra(EXTRA_TAB_INDEX); 61 } else { 62 intent.putExtra(EXTRA_TAB_INDEX, tabIndex); 63 } 64 return intent; 65 } 66 67 /** 68 * Writes the selected tab to the passed intent by retrieving it from the originalIntent that 69 * was passed in 70 * @param intent The intent to modify. 71 * @param originalIntent The intent where the tab index should be read from 72 * @return Returns the modified intent. Notice that this is not a new instance (the passed-in 73 * intent is modified) 74 */ setTab(Intent intent, Intent originalIntent)75 public static Intent setTab(Intent intent, Intent originalIntent) { 76 return setTab(intent, getTab(originalIntent)); 77 } 78 79 /** 80 * Returns the selected tab or -1 if no tab is stored 81 */ getTab(Intent intent)82 public static int getTab(Intent intent) { 83 if (intent.getExtras() == null) return -1; 84 return intent.getExtras().getInt(EXTRA_TAB_INDEX, -1); 85 } 86 87 /** 88 * Persists the given tabIndex. If the value is -1, the previously persisted value is not 89 * overriden 90 */ saveTab(Context context, int tabIndex)91 public static void saveTab(Context context, int tabIndex) { 92 if (LOGV) Log.v(TAG, "*********** Persisting tab index " + tabIndex); 93 if (tabIndex == -1) return; 94 95 final SharedPreferences.Editor editor = 96 context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE).edit(); 97 editor.putInt(PREF_LAST_PHONECALL_TAB, tabIndex); 98 editor.apply(); 99 } 100 101 /** 102 * Persists the tab as it is stored in the Intent. If the intent does not have a tab index, 103 * the persisted value is not overriden 104 */ saveTab(Context context, Intent intent)105 public static void saveTab(Context context, Intent intent) { 106 saveTab(context, getTab(intent)); 107 } 108 109 /** 110 * Returns the previously persisted tab or defaultValue if nothing is saved 111 */ loadTab(Context context, int defaultValue)112 public static int loadTab(Context context, int defaultValue) { 113 final SharedPreferences prefs = context.getSharedPreferences(PREFERENCES_NAME, 114 Context.MODE_PRIVATE); 115 final int result = prefs.getInt(PREF_LAST_PHONECALL_TAB, defaultValue); 116 if (LOGV) Log.v(TAG, "*********** Loaded tab index: " + result); 117 return result; 118 } 119 } 120