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.AlertDialog; 25 import android.app.Dialog; 26 import android.content.DialogInterface; 27 import android.content.Intent; 28 import android.content.SharedPreferences; 29 import android.os.Bundle; 30 import android.preference.ListPreference; 31 import android.preference.Preference; 32 import android.preference.PreferenceActivity; 33 import android.preference.PreferenceCategory; 34 import android.preference.PreferenceManager; 35 import android.preference.PreferenceScreen; 36 import android.provider.SearchRecentSuggestions; 37 import android.telephony.TelephonyManager; 38 import android.view.Menu; 39 import android.view.MenuItem; 40 41 import com.android.mms.util.Recycler; 42 43 /** 44 * With this activity, users can set preferences for MMS and SMS and 45 * can access and manipulate SMS messages stored on the SIM. 46 */ 47 public class MessagingPreferenceActivity extends PreferenceActivity { 48 // Symbolic names for the keys used for preference lookup 49 public static final String MMS_DELIVERY_REPORT_MODE = "pref_key_mms_delivery_reports"; 50 public static final String EXPIRY_TIME = "pref_key_mms_expiry"; 51 public static final String PRIORITY = "pref_key_mms_priority"; 52 public static final String READ_REPORT_MODE = "pref_key_mms_read_reports"; 53 public static final String SMS_DELIVERY_REPORT_MODE = "pref_key_sms_delivery_reports"; 54 public static final String NOTIFICATION_ENABLED = "pref_key_enable_notifications"; 55 public static final String NOTIFICATION_VIBRATE = "pref_key_vibrate"; 56 public static final String NOTIFICATION_VIBRATE_WHEN= "pref_key_vibrateWhen"; 57 public static final String NOTIFICATION_RINGTONE = "pref_key_ringtone"; 58 public static final String AUTO_RETRIEVAL = "pref_key_mms_auto_retrieval"; 59 public static final String RETRIEVAL_DURING_ROAMING = "pref_key_mms_retrieval_during_roaming"; 60 public static final String AUTO_DELETE = "pref_key_auto_delete"; 61 62 // Menu entries 63 private static final int MENU_RESTORE_DEFAULTS = 1; 64 65 private Preference mSmsLimitPref; 66 private Preference mMmsLimitPref; 67 private Preference mManageSimPref; 68 private Preference mClearHistoryPref; 69 private ListPreference mVibrateWhenPref; 70 private Recycler mSmsRecycler; 71 private Recycler mMmsRecycler; 72 private static final int CONFIRM_CLEAR_SEARCH_HISTORY_DIALOG = 3; 73 74 @Override 75 protected void onCreate(Bundle icicle) { 76 super.onCreate(icicle); 77 addPreferencesFromResource(R.xml.preferences); 78 79 mManageSimPref = findPreference("pref_key_manage_sim_messages"); 80 mSmsLimitPref = findPreference("pref_key_sms_delete_limit"); 81 mMmsLimitPref = findPreference("pref_key_mms_delete_limit"); 82 mClearHistoryPref = findPreference("pref_key_mms_clear_history"); 83 mVibrateWhenPref = (ListPreference) findPreference(NOTIFICATION_VIBRATE_WHEN); 84 85 if (!MmsApp.getApplication().getTelephonyManager().hasIccCard()) { 86 // No SIM card, remove the SIM-related prefs 87 PreferenceCategory smsCategory = 88 (PreferenceCategory)findPreference("pref_key_sms_settings"); 89 smsCategory.removePreference(mManageSimPref); 90 } 91 if (!MmsConfig.getMmsEnabled()) { 92 // No Mms, remove all the mms-related preferences 93 PreferenceCategory mmsOptions = 94 (PreferenceCategory)findPreference("pref_key_mms_settings"); 95 getPreferenceScreen().removePreference(mmsOptions); 96 97 PreferenceCategory storageOptions = 98 (PreferenceCategory)findPreference("pref_key_storage_settings"); 99 storageOptions.removePreference(findPreference("pref_key_mms_delete_limit")); 100 } 101 102 // If needed, migrate vibration setting from a previous version 103 SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); 104 if (!sharedPreferences.contains(NOTIFICATION_VIBRATE_WHEN) && 105 sharedPreferences.contains(NOTIFICATION_VIBRATE)) { 106 int stringId = sharedPreferences.getBoolean(NOTIFICATION_VIBRATE, false) ? 107 R.string.prefDefault_vibrate_true : 108 R.string.prefDefault_vibrate_false; 109 mVibrateWhenPref.setValue(getString(stringId)); 110 } 111 112 mSmsRecycler = Recycler.getSmsRecycler(); 113 mMmsRecycler = Recycler.getMmsRecycler(); 114 115 // Fix up the recycler's summary with the correct values 116 setSmsDisplayLimit(); 117 setMmsDisplayLimit(); 118 } 119 120 private void setSmsDisplayLimit() { 121 mSmsLimitPref.setSummary( 122 getString(R.string.pref_summary_delete_limit, 123 mSmsRecycler.getMessageLimit(this))); 124 } 125 126 private void setMmsDisplayLimit() { 127 mMmsLimitPref.setSummary( 128 getString(R.string.pref_summary_delete_limit, 129 mMmsRecycler.getMessageLimit(this))); 130 } 131 132 public boolean onCreateOptionsMenu(Menu menu) { 133 super.onCreateOptionsMenu(menu); 134 menu.clear(); 135 menu.add(0, MENU_RESTORE_DEFAULTS, 0, R.string.restore_default); 136 return true; 137 } 138 139 @Override 140 public boolean onOptionsItemSelected(MenuItem item) { 141 switch (item.getItemId()) { 142 case MENU_RESTORE_DEFAULTS: 143 restoreDefaultPreferences(); 144 return true; 145 } 146 return false; 147 } 148 149 @Override 150 public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, 151 Preference preference) { 152 if (preference == mSmsLimitPref) { 153 new NumberPickerDialog(this, 154 mSmsLimitListener, 155 mSmsRecycler.getMessageLimit(this), 156 mSmsRecycler.getMessageMinLimit(), 157 mSmsRecycler.getMessageMaxLimit(), 158 R.string.pref_title_sms_delete).show(); 159 } else if (preference == mMmsLimitPref) { 160 new NumberPickerDialog(this, 161 mMmsLimitListener, 162 mMmsRecycler.getMessageLimit(this), 163 mMmsRecycler.getMessageMinLimit(), 164 mMmsRecycler.getMessageMaxLimit(), 165 R.string.pref_title_mms_delete).show(); 166 } else if (preference == mManageSimPref) { 167 startActivity(new Intent(this, ManageSimMessages.class)); 168 } else if (preference == mClearHistoryPref) { 169 showDialog(CONFIRM_CLEAR_SEARCH_HISTORY_DIALOG); 170 return true; 171 } 172 173 return super.onPreferenceTreeClick(preferenceScreen, preference); 174 } 175 176 177 private void restoreDefaultPreferences() { 178 PreferenceManager.getDefaultSharedPreferences(this) 179 .edit().clear().commit(); 180 setPreferenceScreen(null); 181 addPreferencesFromResource(R.xml.preferences); 182 } 183 184 NumberPickerDialog.OnNumberSetListener mSmsLimitListener = 185 new NumberPickerDialog.OnNumberSetListener() { 186 public void onNumberSet(int limit) { 187 mSmsRecycler.setMessageLimit(MessagingPreferenceActivity.this, limit); 188 setSmsDisplayLimit(); 189 } 190 }; 191 192 NumberPickerDialog.OnNumberSetListener mMmsLimitListener = 193 new NumberPickerDialog.OnNumberSetListener() { 194 public void onNumberSet(int limit) { 195 mMmsRecycler.setMessageLimit(MessagingPreferenceActivity.this, limit); 196 setMmsDisplayLimit(); 197 } 198 }; 199 200 @Override 201 protected Dialog onCreateDialog(int id) { 202 switch (id) { 203 case CONFIRM_CLEAR_SEARCH_HISTORY_DIALOG: 204 return new AlertDialog.Builder(MessagingPreferenceActivity.this) 205 .setTitle(R.string.confirm_clear_search_title) 206 .setMessage(R.string.confirm_clear_search_text) 207 .setPositiveButton(android.R.string.ok, new AlertDialog.OnClickListener() { 208 public void onClick(DialogInterface dialog, int which) { 209 SearchRecentSuggestions recent = 210 ((MmsApp)getApplication()).getRecentSuggestions(); 211 if (recent != null) { 212 recent.clearHistory(); 213 } 214 } 215 }) 216 .setNegativeButton(android.R.string.cancel, null) 217 .setIcon(android.R.drawable.ic_dialog_alert) 218 .create(); 219 } 220 return super.onCreateDialog(id); 221 } 222 223 } 224