0

I'm using this class to have an Encrypted EditText, it's working fine on Android < 4.2. On Android 4.2+ it's not working anymore, it's like if it's not saving the text anymore, but I'm not getting anything. Any ideas?

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 SimpleCrypto.decrypt(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(text));
    } catch (Exception e) {
        Log.e(mytag, e.getMessage());
    }
}
}
MoreOver
  • 381
  • 1
  • 5
  • 17
  • You might need to add debug message in `onSetInitialValue` to print the values of `restoreValue` and `getPersistedString(null)`. Also where do you persist the value i.e. call `persistString()`? Finally check the value of `shouldPersist()` – iTech Feb 19 '13 at 23:02
  • restoreValue is true, getPersistedString returns null and shouldPersist() is true. It is working on Android < 4.2, but not on Android 4.2+. Any ideas? – MoreOver Feb 21 '13 at 22:26
  • So where do you call `persistString()`? it seems the string is not stored in the `SharedPreferences` – iTech Feb 21 '13 at 22:34
  • The other thing I can suggest is to look at the relevant android source code and compare `4.2` with previous versions. – iTech Feb 21 '13 at 22:35
  • I'm not calling persistString(), I'm extending EditTextPreference, aren't these string automatically saved by Android? – MoreOver Feb 22 '13 at 01:30
  • It should be, but there is might be something changed in 4.2. So consider saving it manually and see if it will work. – iTech Feb 22 '13 at 01:56
  • I've tried adding persistString call under super.setText(SimpleCrypto.encrypt(text)); but it's the same. :( – MoreOver Feb 22 '13 at 23:39

0 Answers0