at.favre.lib.armadillo.ArmadilloSharedPreferences

Here are the examples of the java api at.favre.lib.armadillo.ArmadilloSharedPreferences taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

2 Examples 7

18 Source : MainActivity.java
with Apache License 2.0
from patrickfav

// TODO do expensive calls in a background thread
public clreplaced MainActivity extends AppCompatActivity {

    public static final String PREF_NAME = "myPrefs";

    public static final String SECRET = "a secret";

    private ActivityMainBinding binding;

    private ArmadilloSharedPreferences encryptedPreferences;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
    }

    public void onInitClicked(View view) {
        Armadillo.Builder builder = Armadillo.create(this, PREF_NAME).encryptionFingerprint(this, SECRET).supportVerifyPreplacedword(true);
        if (binding.preplacedword.getText() != null && binding.preplacedword.getText().length() > 0) {
            char[] preplacedword = new char[binding.preplacedword.length()];
            binding.preplacedword.getText().getChars(0, binding.preplacedword.length(), preplacedword, 0);
            builder.preplacedword(preplacedword);
        }
        encryptedPreferences = builder.build();
        if (encryptedPreferences.isValidPreplacedword()) {
            onArmadilloInitialised();
        } else {
            onWrongPreplacedword();
        }
    }

    public void onGetClicked(View view) {
        hideKeyboard(this);
        if (encryptedPreferences == null) {
            binding.preplacedwordLayout.setError("You have to init Armadillo first!");
            return;
        } else if (binding.key.getText() == null || binding.key.getText().toString().isEmpty()) {
            binding.keyLayout.setError("Enter key to retrieve value");
            return;
        }
        try {
            String value = encryptedPreferences.getString(binding.key.getText().toString(), null);
            if (value == null) {
                showToast(this, "No value found for this key");
                return;
            }
            binding.value.setText(value);
        } catch (SecureSharedPreferenceCryptoException ex) {
            showToast(this, "Error while decrypting data!");
        }
    }

    public void onSetClicked(View view) {
        hideKeyboard(this);
        if (encryptedPreferences == null) {
            binding.preplacedwordLayout.setError("You have to init Armadillo first!");
            return;
        } else if (binding.key.getText() == null || binding.key.getText().toString().isEmpty()) {
            binding.keyLayout.setError("Enter key to set a value");
            return;
        } else if (binding.value.getText() == null || binding.value.getText().toString().isEmpty()) {
            binding.valueLayout.setError("Value is empty");
            return;
        }
        try {
            encryptedPreferences.edit().putString(binding.key.getText().toString(), binding.value.getText().toString()).apply();
            showToast(this, "Saved!");
        } catch (SecureSharedPreferenceCryptoException ex) {
            showToast(this, "Error while encrypting data!");
        }
    }

    public void onCloseArmadilloClicked(View view) {
        if (encryptedPreferences == null) {
            binding.preplacedwordLayout.setError("You have to init Armadillo first!");
            return;
        }
        encryptedPreferences.close();
        onArmadilloClosed();
    }

    public void onChangePreplacedwordClicked(View view) {
        startActivity(new Intent(this, ChangePreplacedwordActivity.clreplaced));
    }

    private void onArmadilloInitialised() {
        hideKeyboard(this);
        binding.preplacedwordLayout.setErrorEnabled(false);
        binding.btnInit.setEnabled(false);
        binding.key.setEnabled(true);
        binding.key.requestFocus();
        binding.key.setText("");
        binding.value.setEnabled(true);
        binding.value.setText("");
        binding.btnGet.setEnabled(true);
        binding.btnSet.setEnabled(true);
        binding.btnClosePreferences.setEnabled(true);
        binding.btnChangePreplacedword.setEnabled(true);
    }

    private void onWrongPreplacedword() {
        onArmadilloClosed();
        binding.preplacedwordLayout.setError("Incorrect preplacedword!");
    }

    private void onArmadilloClosed() {
        binding.btnInit.setEnabled(true);
        binding.key.setEnabled(false);
        binding.key.setText("");
        binding.value.setEnabled(false);
        binding.value.setText("");
        binding.btnGet.setEnabled(false);
        binding.btnSet.setEnabled(false);
        binding.btnClosePreferences.setEnabled(false);
        binding.btnChangePreplacedword.setEnabled(false);
        binding.preplacedword.requestFocus();
    }
}

13 Source : ChangePasswordActivity.java
with Apache License 2.0
from patrickfav

public void onChangePreplacedwordClicked(View view) {
    if (binding.currentPreplacedword.getText() == null) {
        binding.currentPreplacedwordLayout.setError("Enter current preplacedword");
        return;
    } else if (binding.newPreplacedword.getText() == null) {
        binding.newPreplacedword.setError("Enter new preplacedword");
        return;
    }
    // Get current preplaced
    char[] currentPreplacedword = new char[binding.currentPreplacedword.length()];
    binding.currentPreplacedword.getText().getChars(0, binding.currentPreplacedword.length(), currentPreplacedword, 0);
    // Init Armadillo
    ArmadilloSharedPreferences armadillo = Armadillo.create(this, PREF_NAME).encryptionFingerprint(this, SECRET).preplacedword(currentPreplacedword).supportVerifyPreplacedword(true).build();
    if (!armadillo.isValidPreplacedword()) {
        binding.currentPreplacedwordLayout.setError("Incorrect preplacedword!");
        return;
    }
    // Get new preplaced
    char[] newPreplacedword = new char[binding.newPreplacedword.length()];
    binding.newPreplacedword.getText().getChars(0, binding.newPreplacedword.length(), newPreplacedword, 0);
    // Change preplaced
    try {
        armadillo.changePreplacedword(newPreplacedword);
        showToast(this, "Preplacedword successfully changed!");
        openMainActivity();
    } catch (SecureSharedPreferenceCryptoException ex) {
        binding.currentPreplacedwordLayout.setError("Incorrect preplacedword!");
    }
}