I need to crypt my password before save it to local android database. Everything work fine without encryption, I have preferences.xml and so. How can I call a function after I change value of preference (for example, password) ? Here is my code:
public class Preferences extends PreferenceActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
// Get the custom preference
Preference customPref = (Preference) findPreference("pass");
customPref.setOnPreferenceChangeListener(new OnPreferenceChangeListener(){
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
String crypto = SimpleCrypto.encrypt("MYSECRETKEY", newValue.toString()); // encrypt
// Here is where I'm wrong, I guess.
SharedPreferences settings = getSharedPreferences("preferences", MODE_PRIVATE);
SharedPreferences.Editor editor = settings.edit();
editor.putString("pass", crypto);
editor.commit();
});
}
}
P.S: Like this, when I change password, it stores password without encryption.
Android provides the SharedPreferences object to help you save simple application data. Using the SharedPreferences object, you can save the data you want through the use of key/value pairs — specify a key for the data you want to save, and then both it and its value will be saved automatically to an XML file.
To get access to the preferences, we have three APIs to choose from: getPreferences() : used from within your Activity, to access activity-specific preferences. getSharedPreferences() : used from within your Activity (or other application Context), to access application-level preferences.
Preferences in Android are used to keep track of application and user preferences. In any application, there are default preferences that can accessed through the PreferenceManager instance and its related method getDefaultSharedPreferences(Context)
Android supports the following ways of storing data in the local file system: Files - You can create and update files. Preferences - Android allows you to save and retrieve persistent key-value pairs of primitive data type. SQLite database - instances of SQLite databases are also stored on the local file system.
I did this by extending the base EditTextPreference and encrypting/decrypting the password there:
public class EncryptedEditTextPreference extends EditTextPreference {
public EncryptedEditTextPreference(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public EncryptedEditTextPreference(Context context, AttributeSet attrs) {
super(context, attrs);
}
public EncryptedEditTextPreference(Context context) {
super(context);
}
@Override
public String getText() {
String value = super.getText();
return SecurityUtils.decrypt(value);
}
@Override
protected void onSetInitialValue(boolean restoreValue, Object defaultValue) {
super.setText(restoreValue ? getPersistedString(null) : (String) defaultValue);
}
@Override
public void setText(String text) {
if (Utils.isStringBlank(text)) {
super.setText(null);
return;
}
super.setText(SecurityUtils.encrypt(text));
}
}
There are some calls to my personal utilities, but I think the code is pretty clear in what you need to do.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With