1 /* 2 * Copyright (C) 2007-2008 Esmertec AG. 3 * Copyright (C) 2007-2008 The Android Open Source Project 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package com.android.mms.ui; 19 20 import com.android.mms.MmsApp; 21 import com.android.mms.MmsConfig; 22 import com.android.mms.R; 23 24 import android.app.ActionBar; 25 import android.app.AlertDialog; 26 import android.app.Dialog; 27 import android.content.Context; 28 import android.content.DialogInterface; 29 import android.content.Intent; 30 import android.content.SharedPreferences; 31 import android.os.Bundle; 32 import android.preference.CheckBoxPreference; 33 import android.preference.ListPreference; 34 import android.preference.Preference; 35 import android.preference.PreferenceActivity; 36 import android.preference.PreferenceCategory; 37 import android.preference.PreferenceManager; 38 import android.preference.PreferenceScreen; 39 import android.preference.Preference.OnPreferenceChangeListener; 40 import android.provider.SearchRecentSuggestions; 41 import android.view.Menu; 42 import android.view.MenuItem; 43 44 import com.android.mms.util.Recycler; 45 46 /** 47 * With this activity, users can set preferences for MMS and SMS and 48 * can access and manipulate SMS messages stored on the SIM. 49 */ 50 public class MessagingPreferenceActivity extends PreferenceActivity 51 implements OnPreferenceChangeListener { 52 // Symbolic names for the keys used for preference lookup 53 public static final String MMS_DELIVERY_REPORT_MODE = "pref_key_mms_delivery_reports"; 54 public static final String EXPIRY_TIME = "pref_key_mms_expiry"; 55 public static final String PRIORITY = "pref_key_mms_priority"; 56 public static final String READ_REPORT_MODE = "pref_key_mms_read_reports"; 57 public static final String SMS_DELIVERY_REPORT_MODE = "pref_key_sms_delivery_reports"; 58 public static final String NOTIFICATION_ENABLED = "pref_key_enable_notifications"; 59 public static final String NOTIFICATION_VIBRATE = "pref_key_vibrate"; 60 public static final String NOTIFICATION_VIBRATE_WHEN= "pref_key_vibrateWhen"; 61 public static final String NOTIFICATION_RINGTONE = "pref_key_ringtone"; 62 public static final String AUTO_RETRIEVAL = "pref_key_mms_auto_retrieval"; 63 public static final String RETRIEVAL_DURING_ROAMING = "pref_key_mms_retrieval_during_roaming"; 64 public static final String AUTO_DELETE = "pref_key_auto_delete"; 65 66 // Menu entries 67 private static final int MENU_RESTORE_DEFAULTS = 1; 68 69 private Preference mSmsLimitPref; 70 private Preference mSmsDeliveryReportPref; 71 private Preference mMmsLimitPref; 72 private Preference mMmsDeliveryReportPref; 73 private Preference mMmsReadReportPref; 74 private Preference mManageSimPref; 75 private Preference mClearHistoryPref; 76 private ListPreference mVibrateWhenPref; 77 private CheckBoxPreference mEnableNotificationsPref; 78 private Recycler mSmsRecycler; 79 private Recycler mMmsRecycler; 80 private static final int CONFIRM_CLEAR_SEARCH_HISTORY_DIALOG = 3; 81 private CharSequence[] mVibrateEntries; 82 private CharSequence[] mVibrateValues; 83 84 @Override 85 protected void onCreate(Bundle icicle) { 86 super.onCreate(icicle); 87 88 loadPrefs(); 89 90 ActionBar actionBar = getActionBar(); 91 actionBar.setDisplayHomeAsUpEnabled(true); 92 } 93 94 @Override 95 protected void onResume() { 96 super.onResume(); 97 98 // Since the enabled notifications pref can be changed outside of this activity, 99 // we have to reload it whenever we resume. 100 setEnabledNotificationsPref(); 101 registerListeners(); 102 } 103 104 private void loadPrefs() { 105 addPreferencesFromResource(R.xml.preferences); 106 107 mManageSimPref = findPreference("pref_key_manage_sim_messages"); 108 mSmsLimitPref = findPreference("pref_key_sms_delete_limit"); 109 mSmsDeliveryReportPref = findPreference("pref_key_sms_delivery_reports"); 110 mMmsDeliveryReportPref = findPreference("pref_key_mms_delivery_reports"); 111 mMmsReadReportPref = findPreference("pref_key_mms_read_reports"); 112 mMmsLimitPref = findPreference("pref_key_mms_delete_limit"); 113 mClearHistoryPref = findPreference("pref_key_mms_clear_history"); 114 mEnableNotificationsPref = (CheckBoxPreference) findPreference(NOTIFICATION_ENABLED); 115 mVibrateWhenPref = (ListPreference) findPreference(NOTIFICATION_VIBRATE_WHEN); 116 117 mVibrateEntries = getResources().getTextArray(R.array.prefEntries_vibrateWhen); 118 mVibrateValues = getResources().getTextArray(R.array.prefValues_vibrateWhen); 119 120 setMessagePreferences(); 121 } 122 123 private void restoreDefaultPreferences() { 124 PreferenceManager.getDefaultSharedPreferences(this).edit().clear().apply(); 125 setPreferenceScreen(null); 126 loadPrefs(); 127 128 // NOTE: After restoring preferences, the auto delete function (i.e. message recycler) 129 // will be turned off by default. However, we really want the default to be turned on. 130 // Because all the prefs are cleared, that'll cause: 131 // ConversationList.runOneTimeStorageLimitCheckForLegacyMessages to get executed the 132 // next time the user runs the Messaging app and it will either turn on the setting 133 // by default, or if the user is over the limits, encourage them to turn on the setting 134 // manually. 135 } 136 137 private void setMessagePreferences() { 138 if (!MmsApp.getApplication().getTelephonyManager().hasIccCard()) { 139 // No SIM card, remove the SIM-related prefs 140 PreferenceCategory smsCategory = 141 (PreferenceCategory)findPreference("pref_key_sms_settings"); 142 smsCategory.removePreference(mManageSimPref); 143 } 144 145 if (!MmsConfig.getSMSDeliveryReportsEnabled()) { 146 PreferenceCategory smsCategory = 147 (PreferenceCategory)findPreference("pref_key_sms_settings"); 148 smsCategory.removePreference(mSmsDeliveryReportPref); 149 if (!MmsApp.getApplication().getTelephonyManager().hasIccCard()) { 150 getPreferenceScreen().removePreference(smsCategory); 151 } 152 } 153 154 if (!MmsConfig.getMmsEnabled()) { 155 // No Mms, remove all the mms-related preferences 156 PreferenceCategory mmsOptions = 157 (PreferenceCategory)findPreference("pref_key_mms_settings"); 158 getPreferenceScreen().removePreference(mmsOptions); 159 160 PreferenceCategory storageOptions = 161 (PreferenceCategory)findPreference("pref_key_storage_settings"); 162 storageOptions.removePreference(findPreference("pref_key_mms_delete_limit")); 163 } else { 164 if (!MmsConfig.getMMSDeliveryReportsEnabled()) { 165 PreferenceCategory mmsOptions = 166 (PreferenceCategory)findPreference("pref_key_mms_settings"); 167 mmsOptions.removePreference(mMmsDeliveryReportPref); 168 } 169 if (!MmsConfig.getMMSReadReportsEnabled()) { 170 PreferenceCategory mmsOptions = 171 (PreferenceCategory)findPreference("pref_key_mms_settings"); 172 mmsOptions.removePreference(mMmsReadReportPref); 173 } 174 } 175 176 setEnabledNotificationsPref(); 177 178 // If needed, migrate vibration setting from a previous version 179 SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); 180 if (!sharedPreferences.contains(NOTIFICATION_VIBRATE_WHEN) && 181 sharedPreferences.contains(NOTIFICATION_VIBRATE)) { 182 int stringId = sharedPreferences.getBoolean(NOTIFICATION_VIBRATE, false) ? 183 R.string.prefDefault_vibrate_true : 184 R.string.prefDefault_vibrate_false; 185 mVibrateWhenPref.setValue(getString(stringId)); 186 } 187 188 mSmsRecycler = Recycler.getSmsRecycler(); 189 mMmsRecycler = Recycler.getMmsRecycler(); 190 191 // Fix up the recycler's summary with the correct values 192 setSmsDisplayLimit(); 193 setMmsDisplayLimit(); 194 195 adjustVibrateSummary(mVibrateWhenPref.getValue()); 196 } 197 198 private void setEnabledNotificationsPref() { 199 // The "enable notifications" setting is really stored in our own prefs. Read the 200 // current value and set the checkbox to match. 201 mEnableNotificationsPref.setChecked(getNotificationEnabled(this)); 202 } 203 204 private void setSmsDisplayLimit() { 205 mSmsLimitPref.setSummary( 206 getString(R.string.pref_summary_delete_limit, 207 mSmsRecycler.getMessageLimit(this))); 208 } 209 210 private void setMmsDisplayLimit() { 211 mMmsLimitPref.setSummary( 212 getString(R.string.pref_summary_delete_limit, 213 mMmsRecycler.getMessageLimit(this))); 214 } 215 216 public boolean onCreateOptionsMenu(Menu menu) { 217 super.onCreateOptionsMenu(menu); 218 menu.clear(); 219 menu.add(0, MENU_RESTORE_DEFAULTS, 0, R.string.restore_default); 220 return true; 221 } 222 223 @Override 224 public boolean onOptionsItemSelected(MenuItem item) { 225 switch (item.getItemId()) { 226 case MENU_RESTORE_DEFAULTS: 227 restoreDefaultPreferences(); 228 return true; 229 230 case android.R.id.home: 231 // The user clicked on the Messaging icon in the action bar. Take them back from 232 // wherever they came from 233 finish(); 234 return true; 235 } 236 return false; 237 } 238 239 @Override 240 public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, 241 Preference preference) { 242 if (preference == mSmsLimitPref) { 243 new NumberPickerDialog(this, 244 mSmsLimitListener, 245 mSmsRecycler.getMessageLimit(this), 246 mSmsRecycler.getMessageMinLimit(), 247 mSmsRecycler.getMessageMaxLimit(), 248 R.string.pref_title_sms_delete).show(); 249 } else if (preference == mMmsLimitPref) { 250 new NumberPickerDialog(this, 251 mMmsLimitListener, 252 mMmsRecycler.getMessageLimit(this), 253 mMmsRecycler.getMessageMinLimit(), 254 mMmsRecycler.getMessageMaxLimit(), 255 R.string.pref_title_mms_delete).show(); 256 } else if (preference == mManageSimPref) { 257 startActivity(new Intent(this, ManageSimMessages.class)); 258 } else if (preference == mClearHistoryPref) { 259 showDialog(CONFIRM_CLEAR_SEARCH_HISTORY_DIALOG); 260 return true; 261 } else if (preference == mEnableNotificationsPref) { 262 // Update the actual "enable notifications" value that is stored in secure settings. 263 enableNotifications(mEnableNotificationsPref.isChecked(), this); 264 } 265 266 return super.onPreferenceTreeClick(preferenceScreen, preference); 267 } 268 269 270 NumberPickerDialog.OnNumberSetListener mSmsLimitListener = 271 new NumberPickerDialog.OnNumberSetListener() { 272 public void onNumberSet(int limit) { 273 mSmsRecycler.setMessageLimit(MessagingPreferenceActivity.this, limit); 274 setSmsDisplayLimit(); 275 } 276 }; 277 278 NumberPickerDialog.OnNumberSetListener mMmsLimitListener = 279 new NumberPickerDialog.OnNumberSetListener() { 280 public void onNumberSet(int limit) { 281 mMmsRecycler.setMessageLimit(MessagingPreferenceActivity.this, limit); 282 setMmsDisplayLimit(); 283 } 284 }; 285 286 @Override 287 protected Dialog onCreateDialog(int id) { 288 switch (id) { 289 case CONFIRM_CLEAR_SEARCH_HISTORY_DIALOG: 290 return new AlertDialog.Builder(MessagingPreferenceActivity.this) 291 .setTitle(R.string.confirm_clear_search_title) 292 .setMessage(R.string.confirm_clear_search_text) 293 .setPositiveButton(android.R.string.ok, new AlertDialog.OnClickListener() { 294 public void onClick(DialogInterface dialog, int which) { 295 SearchRecentSuggestions recent = 296 ((MmsApp)getApplication()).getRecentSuggestions(); 297 if (recent != null) { 298 recent.clearHistory(); 299 } 300 dialog.dismiss(); 301 } 302 }) 303 .setNegativeButton(android.R.string.cancel, null) 304 .setIcon(android.R.drawable.ic_dialog_alert) 305 .create(); 306 } 307 return super.onCreateDialog(id); 308 } 309 310 public static boolean getNotificationEnabled(Context context) { 311 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); 312 boolean notificationsEnabled = 313 prefs.getBoolean(MessagingPreferenceActivity.NOTIFICATION_ENABLED, true); 314 return notificationsEnabled; 315 } 316 317 public static void enableNotifications(boolean enabled, Context context) { 318 // Store the value of notifications in SharedPreferences 319 SharedPreferences.Editor editor = 320 PreferenceManager.getDefaultSharedPreferences(context).edit(); 321 322 editor.putBoolean(MessagingPreferenceActivity.NOTIFICATION_ENABLED, enabled); 323 324 editor.apply(); 325 } 326 327 private void registerListeners() { 328 mVibrateWhenPref.setOnPreferenceChangeListener(this); 329 } 330 331 public boolean onPreferenceChange(Preference preference, Object newValue) { 332 boolean result = false; 333 if (preference == mVibrateWhenPref) { 334 adjustVibrateSummary((String)newValue); 335 result = true; 336 } 337 return result; 338 } 339 340 private void adjustVibrateSummary(String value) { 341 int len = mVibrateValues.length; 342 for (int i = 0; i < len; i++) { 343 if (mVibrateValues[i].equals(value)) { 344 mVibrateWhenPref.setSummary(mVibrateEntries[i]); 345 return; 346 } 347 } 348 mVibrateWhenPref.setSummary(null); 349 } 350 } 351