use of org.shadowice.flocke.andotp.Database.Entry in project andOTP by andOTP.
the class BackupActivity method backupEncryptedWithPGP.
private void backupEncryptedWithPGP(Uri uri, Intent encryptIntent) {
ArrayList<Entry> entries = DatabaseHelper.loadDatabase(this, encryptionKey);
String plainJSON = DatabaseHelper.entriesToString(entries);
if (encryptIntent == null) {
encryptIntent = new Intent();
if (settings.getOpenPGPSign()) {
encryptIntent.setAction(OpenPgpApi.ACTION_SIGN_AND_ENCRYPT);
encryptIntent.putExtra(OpenPgpApi.EXTRA_SIGN_KEY_ID, pgpKeyId);
} else {
encryptIntent.setAction(OpenPgpApi.ACTION_ENCRYPT);
}
encryptIntent.putExtra(OpenPgpApi.EXTRA_KEY_IDS, new long[] { pgpKeyId });
encryptIntent.putExtra(OpenPgpApi.EXTRA_REQUEST_ASCII_ARMOR, true);
}
InputStream is = new ByteArrayInputStream(plainJSON.getBytes(StandardCharsets.UTF_8));
ByteArrayOutputStream os = new ByteArrayOutputStream();
OpenPgpApi api = new OpenPgpApi(this, pgpServiceConnection.getService());
Intent result = api.executeApi(encryptIntent, is, os);
handleOpenPGPResult(result, os, uri, Constants.INTENT_BACKUP_ENCRYPT_PGP);
}
use of org.shadowice.flocke.andotp.Database.Entry in project andOTP by andOTP.
the class BackupActivity method doBackupCrypt.
private void doBackupCrypt(Uri uri) {
String password = settings.getBackupPasswordEnc();
if (!password.isEmpty()) {
if (Tools.isExternalStorageWritable()) {
ArrayList<Entry> entries = DatabaseHelper.loadDatabase(this, encryptionKey);
String plain = DatabaseHelper.entriesToString(entries);
boolean success = true;
try {
SecretKey key = EncryptionHelper.generateSymmetricKeyFromPassword(password);
byte[] encrypted = EncryptionHelper.encrypt(key, plain.getBytes(StandardCharsets.UTF_8));
FileHelper.writeBytesToFile(this, uri, encrypted);
} catch (Exception e) {
e.printStackTrace();
success = false;
}
if (success) {
Toast.makeText(this, R.string.backup_toast_export_success, Toast.LENGTH_LONG).show();
} else {
Toast.makeText(this, R.string.backup_toast_export_failed, Toast.LENGTH_LONG).show();
}
} else {
Toast.makeText(this, R.string.backup_toast_storage_not_accessible, Toast.LENGTH_LONG).show();
}
} else {
Toast.makeText(this, R.string.backup_toast_crypt_password_not_set, Toast.LENGTH_LONG).show();
}
finishWithResult();
}
use of org.shadowice.flocke.andotp.Database.Entry in project andOTP by andOTP.
the class MainActivity method onActivityResult.
// Activity results
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
if (result != null) {
if (result.getContents() != null) {
try {
Entry e = new Entry(result.getContents());
e.updateOTP();
adapter.addEntry(e);
adapter.saveEntries();
refreshTags();
} catch (Exception e) {
Toast.makeText(this, R.string.toast_invalid_qr_code, Toast.LENGTH_LONG).show();
}
}
} else if (requestCode == Constants.INTENT_MAIN_BACKUP && resultCode == RESULT_OK) {
if (intent.getBooleanExtra("reload", false)) {
adapter.loadEntries();
refreshTags();
}
} else if (requestCode == Constants.INTENT_MAIN_SETTINGS && resultCode == RESULT_OK) {
boolean encryptionChanged = intent.getBooleanExtra(Constants.EXTRA_SETTINGS_ENCRYPTION_CHANGED, false);
byte[] newKey = intent.getByteArrayExtra(Constants.EXTRA_SETTINGS_ENCRYPTION_KEY);
if (encryptionChanged)
updateEncryption(newKey);
} else if (requestCode == Constants.INTENT_MAIN_AUTHENTICATE) {
if (resultCode != RESULT_OK) {
Toast.makeText(getBaseContext(), R.string.toast_auth_failed_fatal, Toast.LENGTH_LONG).show();
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
finishAndRemoveTask();
} else {
finish();
}
} else {
requireAuthentication = false;
byte[] authKey = null;
if (intent != null)
authKey = intent.getByteArrayExtra(Constants.EXTRA_AUTH_PASSWORD_KEY);
updateEncryption(authKey);
}
}
}
use of org.shadowice.flocke.andotp.Database.Entry in project andOTP by andOTP.
the class SettingsActivity method tryEncryptionChange.
private boolean tryEncryptionChange(EncryptionType newEnc, byte[] newKey) {
Toast upgrading = Toast.makeText(this, R.string.settings_toast_encryption_changing, Toast.LENGTH_LONG);
upgrading.show();
if (DatabaseHelper.backupDatabase(this)) {
ArrayList<Entry> entries;
if (encryptionKey != null)
entries = DatabaseHelper.loadDatabase(this, encryptionKey);
else
entries = new ArrayList<>();
SecretKey newEncryptionKey;
if (newEnc == EncryptionType.KEYSTORE) {
newEncryptionKey = KeyStoreHelper.loadEncryptionKeyFromKeyStore(this, true);
} else if (newKey != null && newKey.length > 0) {
newEncryptionKey = EncryptionHelper.generateSymmetricKey(newKey);
} else {
upgrading.cancel();
DatabaseHelper.restoreDatabaseBackup(this);
return false;
}
if (DatabaseHelper.saveDatabase(this, entries, newEncryptionKey)) {
encryptionKey = newEncryptionKey;
encryptionChanged = true;
fragment.encryption.setValue(newEnc.name().toLowerCase());
upgrading.cancel();
Toast.makeText(this, R.string.settings_toast_encryption_change_success, Toast.LENGTH_LONG).show();
return true;
}
DatabaseHelper.restoreDatabaseBackup(this);
upgrading.cancel();
Toast.makeText(this, R.string.settings_toast_encryption_change_failed, Toast.LENGTH_LONG).show();
} else {
upgrading.cancel();
Toast.makeText(this, R.string.settings_toast_encryption_backup_failed, Toast.LENGTH_LONG).show();
}
return false;
}
use of org.shadowice.flocke.andotp.Database.Entry in project andOTP by andOTP.
the class EntriesCardAdapter method changeThumbnail.
public void changeThumbnail(final int pos) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
int marginSmall = context.getResources().getDimensionPixelSize(R.dimen.activity_margin_small);
int marginMedium = context.getResources().getDimensionPixelSize(R.dimen.activity_margin_medium);
int realIndex = getRealIndex(pos);
final ThumbnailSelectionAdapter thumbnailAdapter = new ThumbnailSelectionAdapter(context, entries.get(realIndex).getLabel());
final EditText input = new EditText(context);
input.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
input.setSingleLine();
input.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable editable) {
thumbnailAdapter.filter(editable.toString());
}
});
int gridPadding = context.getResources().getDimensionPixelSize(R.dimen.activity_margin_small);
int gridBackground = Tools.getThemeColor(context, R.attr.thumbnailBackground);
GridView grid = new GridView(context);
grid.setAdapter(thumbnailAdapter);
grid.setBackgroundColor(gridBackground);
grid.setPadding(gridPadding, gridPadding, gridPadding, gridPadding);
grid.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
int thumbnailSize = settings.getThumbnailSize();
grid.setColumnWidth(thumbnailSize);
grid.setNumColumns(GridView.AUTO_FIT);
grid.setVerticalSpacing(context.getResources().getDimensionPixelSize(R.dimen.activity_margin_medium));
grid.setHorizontalSpacing(context.getResources().getDimensionPixelSize(R.dimen.activity_margin_medium));
grid.setStretchMode(GridView.STRETCH_COLUMN_WIDTH);
LinearLayout layout = new LinearLayout(context);
layout.setOrientation(LinearLayout.VERTICAL);
layout.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
layout.addView(input);
layout.addView(grid);
FrameLayout container = new FrameLayout(context);
container.setPaddingRelative(marginMedium, marginSmall, marginMedium, 0);
container.addView(layout);
final AlertDialog alert = builder.setTitle(R.string.menu_popup_change_image).setView(container).setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
}).create();
grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
int realIndex = getRealIndex(pos);
EntryThumbnail.EntryThumbnails thumbnail = EntryThumbnail.EntryThumbnails.Default;
try {
int realPos = thumbnailAdapter.getRealIndex(position);
thumbnail = EntryThumbnail.EntryThumbnails.values()[realPos];
} catch (Exception e) {
e.printStackTrace();
}
Entry e = entries.get(realIndex);
e.setThumbnail(thumbnail);
DatabaseHelper.saveDatabase(context, entries, encryptionKey);
notifyItemChanged(pos);
alert.cancel();
}
});
alert.show();
}
Aggregations