Search in sources :

Example 1 with Entry

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);
}
Also used : Entry(org.shadowice.flocke.andotp.Database.Entry) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) OpenPgpApi(org.openintents.openpgp.util.OpenPgpApi) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 2 with Entry

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();
}
Also used : Entry(org.shadowice.flocke.andotp.Database.Entry) SecretKey(javax.crypto.SecretKey)

Example 3 with Entry

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);
        }
    }
}
Also used : Entry(org.shadowice.flocke.andotp.Database.Entry) IntentResult(com.google.zxing.integration.android.IntentResult)

Example 4 with Entry

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;
}
Also used : Entry(org.shadowice.flocke.andotp.Database.Entry) SecretKey(javax.crypto.SecretKey) Toast(android.widget.Toast) ArrayList(java.util.ArrayList)

Example 5 with Entry

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();
}
Also used : AlertDialog(android.app.AlertDialog) DialogInterface(android.content.DialogInterface) Entry(org.shadowice.flocke.andotp.Database.Entry) TextWatcher(android.text.TextWatcher) Editable(android.text.Editable) EditText(android.widget.EditText) ViewGroup(android.view.ViewGroup) GridView(android.widget.GridView) View(android.view.View) AdapterView(android.widget.AdapterView) RecyclerView(android.support.v7.widget.RecyclerView) FrameLayout(android.widget.FrameLayout) AdapterView(android.widget.AdapterView) GridView(android.widget.GridView) LinearLayout(android.widget.LinearLayout)

Aggregations

Entry (org.shadowice.flocke.andotp.Database.Entry)19 ArrayList (java.util.ArrayList)5 SecretKey (javax.crypto.SecretKey)5 AlertDialog (android.app.AlertDialog)4 DialogInterface (android.content.DialogInterface)4 EditText (android.widget.EditText)4 View (android.view.View)3 AdapterView (android.widget.AdapterView)3 FrameLayout (android.widget.FrameLayout)3 IOException (java.io.IOException)3 Uri (android.net.Uri)2 RecyclerView (android.support.v7.widget.RecyclerView)2 Editable (android.text.Editable)2 TextWatcher (android.text.TextWatcher)2 ViewGroup (android.view.ViewGroup)2 GridView (android.widget.GridView)2 LinearLayout (android.widget.LinearLayout)2 File (java.io.File)2 HashMap (java.util.HashMap)2 Callable (java.util.concurrent.Callable)2