@Override public void onSharedPreferenceChanged( final SharedPreferences sharedPreferences, final String key) { PieTalkLogger.d(TAG, "Preference changed for key: " + key); if (mIsResettingValue) { mIsResettingValue = false; return; } final Preference myPref = findPreference(key); final Resources resources = getActivity().getResources(); final UserPreferences localPreferences = mPieTalkService.getLocalPreferences(); // final String oldValue= ""; String oldValue = ""; String newValue = ""; int preferenceId = 0; if (key == resources.getString(R.string.email_address)) { preferenceId = R.string.email_address; oldValue = localPreferences.getEmail(); newValue = sharedPreferences.getString(key, ""); if (!android.util.Patterns.EMAIL_ADDRESS.matcher(newValue).matches()) { SharedPreferences.Editor editor = sharedPreferences.edit(); editor.putString(key, oldValue); PieTalkAlert.showToast(getActivity(), "That email address is invalid!"); mIsResettingValue = true; editor.commit(); EditTextPreference editPref = (EditTextPreference) myPref; editPref.setText(oldValue); return; } } else if (key == resources.getString(R.string.receive_pies_from)) { preferenceId = R.string.receive_pies_from; oldValue = localPreferences.getReceiveFrom(); newValue = sharedPreferences.getString(key, ""); } else if (key == resources.getString(R.string.share_stories_to)) { preferenceId = R.string.share_stories_to; oldValue = localPreferences.getShareTo(); newValue = sharedPreferences.getString(key, ""); } // localPreferences.setEmail(newValue); localPreferences.setValueForPreference(preferenceId, newValue); myPref.setSummary(sharedPreferences.getString(key, "")); mPieTalkService.updatePreferences( localPreferences, new TableOperationCallback<UserPreferences>() { @Override public void onCompleted( UserPreferences resultsPreferences, Exception ex, ServiceFilterResponse serviceFilterResponse) { // reset preferences if needed, otherwise say they've been saved // if (key == resources.getString(R.string.email_address)) { // PieTalkAlert.showToast(getActivity(), "OH NO EMAIL ERROR"); // } // Display error PieTalkLogger.e(TAG, "Callback!"); if (ex != null) { PieTalkLogger.i(TAG, "ERrror in callback"); if (NoNetworkConnectivityException.class.isInstance(ex)) return; mIsResettingValue = true; UserPreferences backupPrefs = mPieTalkService.getBackupPreferences(); // PieTalkAlert.showSimpleErrorDialog(getActivity(), // ex.getCause().getMessage().replace("\"", "")); String error = ex.getCause().getMessage(); // Check for unexpected 500 errors if (error.contains("500")) { // PieTalkAlert.showToast(getActivity(), "There was an error. Please try again // later."); PieTalkAlert.showToast( mPieTalkService.getActivityContext(), "There was an error. Please try again later."); } else { // PieTalkAlert.showToast(getActivity(), // ex.getCause().getMessage().replace("\"", "")); PieTalkAlert.showToast( mPieTalkService.getActivityContext(), ex.getCause().getMessage().replace("\"", "")); } SharedPreferences.Editor editor = sharedPreferences.edit(); String oldValue = ""; if (key == resources.getString(R.string.email_address)) oldValue = backupPrefs.getEmail(); editor.putString(key, oldValue); editor.commit(); myPref.setSummary(oldValue); if (EditTextPreference.class.isInstance(myPref)) { EditTextPreference editPref = (EditTextPreference) myPref; editPref.setText(oldValue); } } else { if (getActivity() != null) PieTalkAlert.showToast(getActivity(), "Setting updated"); } } }); }
@Override public void onResume() { super.onResume(); PieTalkLogger.d(TAG, "settings fragment on resume"); getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this); }