Thanks to dmon and the example here Process the value of preference before save in Android?
I was able to get the basic code down. But my value isn't being stored encrypted in preferences.xml on the device and I know this is a simple mistake on my part (java novice).
My encryption and decryption class is working outside of the EditTextPreference code.
Kind Regards,
Mike
My preferences.xml
<ping.test.com.EncryptedEditTextpreference
android:key="key"
android:summary="Enter Your Public Key"
android:title="Public Key"
android:inputType="textPassword"/>
</PreferenceCategory>
My class to extend EditTextPreference
package ping.test.com;
import android.content.Context;
import android.preference.EditTextPreference;
import android.util.AttributeSet;
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();
try {
return SimpleCrypto.decrypt("BiteMe", value);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return value;
}
@Override
protected void onSetInitialValue(boolean restoreValue, Object defaultValue) {
super.setText(restoreValue ? getPersistedString(null) : (String) defaultValue);
}
@Override
public void setText(String text) {
try {
super.setText(SimpleCrypto.encrypt("BiteMe", text ));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}