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.Activity; 26 import android.app.AlertDialog; 27 import android.app.Dialog; 28 import android.content.Context; 29 import android.content.DialogInterface; 30 import android.content.Intent; 31 import android.content.SharedPreferences; 32 import android.os.Bundle; 33 import android.preference.CheckBoxPreference; 34 import android.preference.ListPreference; 35 import android.preference.Preference; 36 import android.preference.PreferenceActivity; 37 import android.preference.PreferenceCategory; 38 import android.preference.PreferenceManager; 39 import android.preference.PreferenceScreen; 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 // Symbolic names for the keys used for preference lookup 52 public static final String MMS_DELIVERY_REPORT_MODE = "pref_key_mms_delivery_reports"; 53 public static final String EXPIRY_TIME = "pref_key_mms_expiry"; 54 public static final String PRIORITY = "pref_key_mms_priority"; 55 public static final String READ_REPORT_MODE = "pref_key_mms_read_reports"; 56 public static final String SMS_DELIVERY_REPORT_MODE = "pref_key_sms_delivery_reports"; 57 public static final String NOTIFICATION_ENABLED = "pref_key_enable_notifications"; 58 public static final String NOTIFICATION_VIBRATE = "pref_key_vibrate"; 59 public static final String NOTIFICATION_VIBRATE_WHEN= "pref_key_vibrateWhen"; 60 public static final String NOTIFICATION_RINGTONE = "pref_key_ringtone"; 61 public static final String AUTO_RETRIEVAL = "pref_key_mms_auto_retrieval"; 62 public static final String RETRIEVAL_DURING_ROAMING = "pref_key_mms_retrieval_during_roaming"; 63 public static final String AUTO_DELETE = "pref_key_auto_delete"; 64 65 // Menu entries 66 private static final int MENU_RESTORE_DEFAULTS = 1; 67 68 private Preference mSmsLimitPref; 69 private Preference mSmsDeliveryReportPref; 70 private Preference mMmsLimitPref; 71 private Preference mMmsDeliveryReportPref; 72 private Preference mMmsReadReportPref; 73 private Preference mManageSimPref; 74 private Preference mClearHistoryPref; 75 private ListPreference mVibrateWhenPref; 76 private CheckBoxPreference mEnableNotificationsPref; 77 private Recycler mSmsRecycler; 78 private Recycler mMmsRecycler; 79 private static final int CONFIRM_CLEAR_SEARCH_HISTORY_DIALOG = 3; 80 81 @Override 82 protected void onCreate(Bundle icicle) { 83 super.onCreate(icicle); 84 addPreferencesFromResource(R.xml.preferences); 85 86 mManageSimPref = findPreference("pref_key_manage_sim_messages"); 87 mSmsLimitPref = findPreference("pref_key_sms_delete_limit"); 88 mSmsDeliveryReportPref = findPreference("pref_key_sms_delivery_reports"); 89 mMmsDeliveryReportPref = findPreference("pref_key_mms_delivery_reports"); 90 mMmsReadReportPref = findPreference("pref_key_mms_read_reports"); 91 mMmsLimitPref = findPreference("pref_key_mms_delete_limit"); 92 mClearHistoryPref = findPreference("pref_key_mms_clear_history"); 93 mEnableNotificationsPref = (CheckBoxPreference) findPreference(NOTIFICATION_ENABLED); 94 mVibrateWhenPref = (ListPreference) findPreference(NOTIFICATION_VIBRATE_WHEN); 95 96 ActionBar actionBar = getActionBar(); 97 actionBar.setDisplayHomeAsUpEnabled(true); 98 setMessagePreferences(); 99 } 100 101 @Override 102 protected void onResume() { 103 super.onResume(); 104 105 // Since the enabled notifications pref can be changed outside of this activity, 106 // we have to reload it whenever we resume. 107 setEnabledNotificationsPref(); 108 } 109 110 private void setMessagePreferences() { 111 if (!MmsApp.getApplication().getTelephonyManager().hasIccCard()) { 112 // No SIM card, remove the SIM-related prefs 113 PreferenceCategory smsCategory = 114 (PreferenceCategory)findPreference("pref_key_sms_settings"); 115 smsCategory.removePreference(mManageSimPref); 116 } 117 118 if (!MmsConfig.getSMSDeliveryReportsEnabled()) { 119 PreferenceCategory smsCategory = 120 (PreferenceCategory)findPreference("pref_key_sms_settings"); 121 smsCategory.removePreference(mSmsDeliveryReportPref); 122 if (!MmsApp.getApplication().getTelephonyManager().hasIccCard()) { 123 getPreferenceScreen().removePreference(smsCategory); 124 } 125 } 126 127 if (!MmsConfig.getMmsEnabled()) { 128 // No Mms, remove all the mms-related preferences 129 PreferenceCategory mmsOptions = 130 (PreferenceCategory)findPreference("pref_key_mms_settings"); 131 getPreferenceScreen().removePreference(mmsOptions); 132 133 PreferenceCategory storageOptions = 134 (PreferenceCategory)findPreference("pref_key_storage_settings"); 135 storageOptions.removePreference(findPreference("pref_key_mms_delete_limit")); 136 } else { 137 if (!MmsConfig.getMMSDeliveryReportsEnabled()) { 138 PreferenceCategory mmsOptions = 139 (PreferenceCategory)findPreference("pref_key_mms_settings"); 140 mmsOptions.removePreference(mMmsDeliveryReportPref); 141 } 142 if (!MmsConfig.getMMSReadReportsEnabled()) { 143 PreferenceCategory mmsOptions = 144 (PreferenceCategory)findPreference("pref_key_mms_settings"); 145 mmsOptions.removePreference(mMmsReadReportPref); 146 } 147 } 148 149 setEnabledNotificationsPref(); 150 151 // If needed, migrate vibration setting from a previous version 152 SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); 153 if (!sharedPreferences.contains(NOTIFICATION_VIBRATE_WHEN) && 154 sharedPreferences.contains(NOTIFICATION_VIBRATE)) { 155 int stringId = sharedPreferences.getBoolean(NOTIFICATION_VIBRATE, false) ? 156 R.string.prefDefault_vibrate_true : 157 R.string.prefDefault_vibrate_false; 158 mVibrateWhenPref.setValue(getString(stringId)); 159 } 160 161 mSmsRecycler = Recycler.getSmsRecycler(); 162 mMmsRecycler = Recycler.getMmsRecycler(); 163 164 // Fix up the recycler's summary with the correct values 165 setSmsDisplayLimit(); 166 setMmsDisplayLimit(); 167 } 168 169 private void setEnabledNotificationsPref() { 170 // The "enable notifications" setting is really stored in our own prefs. Read the 171 // current value and set the checkbox to match. 172 mEnableNotificationsPref.setChecked(getNotificationEnabled(this)); 173 } 174 175 private void setSmsDisplayLimit() { 176 mSmsLimitPref.setSummary( 177 getString(R.string.pref_summary_delete_limit, 178 mSmsRecycler.getMessageLimit(this))); 179 } 180 181 private void setMmsDisplayLimit() { 182 mMmsLimitPref.setSummary( 183 getString(R.string.pref_summary_delete_limit, 184 mMmsRecycler.getMessageLimit(this))); 185 } 186 187 public boolean onCreateOptionsMenu(Menu menu) { 188 super.onCreateOptionsMenu(menu); 189 menu.clear(); 190 menu.add(0, MENU_RESTORE_DEFAULTS, 0, R.string.restore_default); 191 return true; 192 } 193 194 @Override 195 public boolean onOptionsItemSelected(MenuItem item) { 196 switch (item.getItemId()) { 197 case MENU_RESTORE_DEFAULTS: 198 restoreDefaultPreferences(); 199 return true; 200 201 case android.R.id.home: 202 // The user clicked on the Messaging icon in the action bar. Take them back from 203 // wherever they came from 204 finish(); 205 return true; 206 } 207 return false; 208 } 209 210 @Override 211 public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, 212 Preference preference) { 213 if (preference == mSmsLimitPref) { 214 new NumberPickerDialog(this, 215 mSmsLimitListener, 216 mSmsRecycler.getMessageLimit(this), 217 mSmsRecycler.getMessageMinLimit(), 218 mSmsRecycler.getMessageMaxLimit(), 219 R.string.pref_title_sms_delete).show(); 220 } else if (preference == mMmsLimitPref) { 221 new NumberPickerDialog(this, 222 mMmsLimitListener, 223 mMmsRecycler.getMessageLimit(this), 224 mMmsRecycler.getMessageMinLimit(), 225 mMmsRecycler.getMessageMaxLimit(), 226 R.string.pref_title_mms_delete).show(); 227 } else if (preference == mManageSimPref) { 228 startActivity(new Intent(this, ManageSimMessages.class)); 229 } else if (preference == mClearHistoryPref) { 230 showDialog(CONFIRM_CLEAR_SEARCH_HISTORY_DIALOG); 231 return true; 232 } else if (preference == mEnableNotificationsPref) { 233 // Update the actual "enable notifications" value that is stored in secure settings. 234 enableNotifications(mEnableNotificationsPref.isChecked(), this); 235 } 236 237 return super.onPreferenceTreeClick(preferenceScreen, preference); 238 } 239 240 241 private void restoreDefaultPreferences() { 242 PreferenceManager.getDefaultSharedPreferences(this) 243 .edit().clear().apply(); 244 setPreferenceScreen(null); 245 addPreferencesFromResource(R.xml.preferences); 246 setMessagePreferences(); 247 } 248 249 NumberPickerDialog.OnNumberSetListener mSmsLimitListener = 250 new NumberPickerDialog.OnNumberSetListener() { 251 public void onNumberSet(int limit) { 252 mSmsRecycler.setMessageLimit(MessagingPreferenceActivity.this, limit); 253 setSmsDisplayLimit(); 254 } 255 }; 256 257 NumberPickerDialog.OnNumberSetListener mMmsLimitListener = 258 new NumberPickerDialog.OnNumberSetListener() { 259 public void onNumberSet(int limit) { 260 mMmsRecycler.setMessageLimit(MessagingPreferenceActivity.this, limit); 261 setMmsDisplayLimit(); 262 } 263 }; 264 265 @Override 266 protected Dialog onCreateDialog(int id) { 267 switch (id) { 268 case CONFIRM_CLEAR_SEARCH_HISTORY_DIALOG: 269 return new AlertDialog.Builder(MessagingPreferenceActivity.this) 270 .setTitle(R.string.confirm_clear_search_title) 271 .setMessage(R.string.confirm_clear_search_text) 272 .setPositiveButton(android.R.string.ok, new AlertDialog.OnClickListener() { 273 public void onClick(DialogInterface dialog, int which) { 274 SearchRecentSuggestions recent = 275 ((MmsApp)getApplication()).getRecentSuggestions(); 276 if (recent != null) { 277 recent.clearHistory(); 278 } 279 dialog.dismiss(); 280 } 281 }) 282 .setNegativeButton(android.R.string.cancel, null) 283 .setIcon(android.R.drawable.ic_dialog_alert) 284 .create(); 285 } 286 return super.onCreateDialog(id); 287 } 288 289 public static boolean getNotificationEnabled(Context context) { 290 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); 291 boolean notificationsEnabled = 292 prefs.getBoolean(MessagingPreferenceActivity.NOTIFICATION_ENABLED, true); 293 return notificationsEnabled; 294 } 295 296 public static void enableNotifications(boolean enabled, Context context) { 297 // Store the value of notifications in SharedPreferences 298 SharedPreferences.Editor editor = 299 PreferenceManager.getDefaultSharedPreferences(context).edit(); 300 301 editor.putBoolean(MessagingPreferenceActivity.NOTIFICATION_ENABLED, enabled); 302 303 editor.apply(); 304 } 305 } 306