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.email.activity.setup; 18 19 import android.content.ContentResolver; 20 import android.os.Bundle; 21 import android.preference.CheckBoxPreference; 22 import android.preference.ListPreference; 23 import android.preference.Preference; 24 import android.preference.Preference.OnPreferenceChangeListener; 25 import android.preference.PreferenceFragment; 26 import android.preference.PreferenceScreen; 27 import android.text.TextUtils; 28 import android.view.Menu; 29 import android.view.MenuInflater; 30 import android.view.MenuItem; 31 32 import com.android.email.Preferences; 33 import com.android.email.R; 34 import com.android.email.provider.EmailProvider; 35 import com.android.mail.preferences.MailPrefs; 36 import com.android.mail.ui.settings.ClearPictureApprovalsDialogFragment; 37 38 public class GeneralPreferences extends PreferenceFragment implements 39 OnPreferenceChangeListener { 40 41 private static final String PREFERENCE_KEY_AUTO_ADVANCE = "auto_advance"; 42 private static final String PREFERENCE_KEY_TEXT_ZOOM = "text_zoom"; 43 private static final String PREFERENCE_KEY_CONFIRM_DELETE = "confirm_delete"; 44 private static final String PREFERENCE_KEY_CONFIRM_SEND = "confirm_send"; 45 private static final String PREFERENCE_KEY_CONV_LIST_ICON = "conversation_list_icon"; 46 47 private MailPrefs mMailPrefs; 48 private Preferences mPreferences; 49 private ListPreference mAutoAdvance; 50 /** 51 * TODO: remove this when we've decided for certain that an app setting is unnecessary 52 * (b/5287963) 53 */ 54 @Deprecated 55 private ListPreference mTextZoom; 56 private CheckBoxPreference mConfirmDelete; 57 private CheckBoxPreference mConfirmSend; 58 //private CheckBoxPreference mConvListAttachmentPreviews; 59 private CheckBoxPreference mSwipeDelete; 60 61 private boolean mSettingsChanged = false; 62 63 CharSequence[] mSizeSummaries; 64 65 @Override onCreate(Bundle savedInstanceState)66 public void onCreate(Bundle savedInstanceState) { 67 super.onCreate(savedInstanceState); 68 setHasOptionsMenu(true); 69 70 mMailPrefs = MailPrefs.get(getActivity()); 71 getPreferenceManager().setSharedPreferencesName(Preferences.PREFERENCES_FILE); 72 73 // Load the preferences from an XML resource 74 addPreferencesFromResource(R.xml.general_preferences); 75 76 final PreferenceScreen ps = getPreferenceScreen(); 77 // Merely hide app pref for font size until we're sure it's unnecessary (b/5287963) 78 ps.removePreference(findPreference(PREFERENCE_KEY_TEXT_ZOOM)); 79 } 80 81 @Override onResume()82 public void onResume() { 83 loadSettings(); 84 mSettingsChanged = false; 85 super.onResume(); 86 } 87 88 @Override onPause()89 public void onPause() { 90 super.onPause(); 91 if (mSettingsChanged) { 92 // Notify the account list that we have changes 93 ContentResolver resolver = getActivity().getContentResolver(); 94 resolver.notifyChange(EmailProvider.UIPROVIDER_ALL_ACCOUNTS_NOTIFIER, null); 95 } 96 } 97 98 @Override onPreferenceChange(Preference preference, Object newValue)99 public boolean onPreferenceChange(Preference preference, Object newValue) { 100 String key = preference.getKey(); 101 // Indicate we need to send notifications to UI 102 mSettingsChanged = true; 103 if (PREFERENCE_KEY_AUTO_ADVANCE.equals(key)) { 104 mPreferences.setAutoAdvanceDirection(mAutoAdvance.findIndexOfValue((String) newValue)); 105 return true; 106 } else if (PREFERENCE_KEY_TEXT_ZOOM.equals(key)) { 107 mPreferences.setTextZoom(mTextZoom.findIndexOfValue((String) newValue)); 108 reloadDynamicSummaries(); 109 return true; 110 } else if (MailPrefs.PreferenceKeys.DEFAULT_REPLY_ALL.equals(key)) { 111 mMailPrefs.setDefaultReplyAll((Boolean) newValue); 112 return true; 113 } else if (PREFERENCE_KEY_CONV_LIST_ICON.equals(key)) { 114 mMailPrefs.setShowSenderImages((Boolean) newValue); 115 return true; 116 } 117 return false; 118 } 119 120 @Override onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference)121 public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) { 122 if (getActivity() == null) { 123 // Guard against monkeys. 124 return false; 125 } 126 mSettingsChanged = true; 127 String key = preference.getKey(); 128 if (PREFERENCE_KEY_CONFIRM_DELETE.equals(key)) { 129 mPreferences.setConfirmDelete(mConfirmDelete.isChecked()); 130 return true; 131 } else if (PREFERENCE_KEY_CONFIRM_SEND.equals(key)) { 132 mPreferences.setConfirmSend(mConfirmSend.isChecked()); 133 return true; 134 } else if (MailPrefs.PreferenceKeys.CONVERSATION_LIST_SWIPE.equals(key)) { 135 mMailPrefs.setConversationListSwipeEnabled(mSwipeDelete.isChecked()); 136 return true; 137 } 138 return false; 139 } 140 loadSettings()141 private void loadSettings() { 142 mPreferences = Preferences.getPreferences(getActivity()); 143 mAutoAdvance = (ListPreference) findPreference(PREFERENCE_KEY_AUTO_ADVANCE); 144 mAutoAdvance.setValueIndex(mPreferences.getAutoAdvanceDirection()); 145 mAutoAdvance.setOnPreferenceChangeListener(this); 146 147 mTextZoom = (ListPreference) findPreference(PREFERENCE_KEY_TEXT_ZOOM); 148 if (mTextZoom != null) { 149 mTextZoom.setValueIndex(mPreferences.getTextZoom()); 150 mTextZoom.setOnPreferenceChangeListener(this); 151 } 152 153 final CheckBoxPreference convListIcon = 154 (CheckBoxPreference) findPreference(PREFERENCE_KEY_CONV_LIST_ICON); 155 if (convListIcon != null) { 156 final boolean showSenderImage = mMailPrefs.getShowSenderImages(); 157 convListIcon.setChecked(showSenderImage); 158 convListIcon.setOnPreferenceChangeListener(this); 159 } 160 161 mConfirmDelete = (CheckBoxPreference) findPreference(PREFERENCE_KEY_CONFIRM_DELETE); 162 mConfirmSend = (CheckBoxPreference) findPreference(PREFERENCE_KEY_CONFIRM_SEND); 163 mSwipeDelete = (CheckBoxPreference) 164 findPreference(MailPrefs.PreferenceKeys.CONVERSATION_LIST_SWIPE); 165 mSwipeDelete.setChecked(mMailPrefs.getIsConversationListSwipeEnabled()); 166 167 final CheckBoxPreference replyAllPreference = 168 (CheckBoxPreference) findPreference(MailPrefs.PreferenceKeys.DEFAULT_REPLY_ALL); 169 replyAllPreference.setChecked(mMailPrefs.getDefaultReplyAll()); 170 replyAllPreference.setOnPreferenceChangeListener(this); 171 172 reloadDynamicSummaries(); 173 } 174 175 /** 176 * Reload any preference summaries that are updated dynamically 177 */ reloadDynamicSummaries()178 private void reloadDynamicSummaries() { 179 if (mTextZoom != null) { 180 int textZoomIndex = mPreferences.getTextZoom(); 181 // Update summary - but only load the array once 182 if (mSizeSummaries == null) { 183 mSizeSummaries = getActivity().getResources() 184 .getTextArray(R.array.general_preference_text_zoom_summary_array); 185 } 186 CharSequence summary = null; 187 if (textZoomIndex >= 0 && textZoomIndex < mSizeSummaries.length) { 188 summary = mSizeSummaries[textZoomIndex]; 189 } 190 mTextZoom.setSummary(summary); 191 } 192 } 193 194 @Override onCreateOptionsMenu(Menu menu, MenuInflater inflater)195 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { 196 menu.clear(); 197 inflater.inflate(R.menu.general_prefs_fragment_menu, menu); 198 } 199 200 @Override onOptionsItemSelected(MenuItem item)201 public boolean onOptionsItemSelected(MenuItem item) { 202 switch (item.getItemId()) { 203 case R.id.clear_picture_approvals_menu_item: 204 clearDisplayImages(); 205 return true; 206 } 207 208 return super.onOptionsItemSelected(item); 209 } 210 clearDisplayImages()211 private void clearDisplayImages() { 212 final ClearPictureApprovalsDialogFragment fragment = 213 ClearPictureApprovalsDialogFragment.newInstance(); 214 fragment.show(getActivity().getFragmentManager(), 215 ClearPictureApprovalsDialogFragment.FRAGMENT_TAG); 216 } 217 218 } 219