Search in sources :

Example 6 with EncryptedEntry

use of com.amaze.filemanager.database.models.EncryptedEntry in project AmazeFileManager by TeamAmaze.

the class CryptHandler method getAllEntries.

public List<EncryptedEntry> getAllEntries() throws GeneralSecurityException, IOException {
    List<EncryptedEntry> entryList = new ArrayList<EncryptedEntry>();
    // Select all query
    String query = "Select * FROM " + TABLE_ENCRYPTED;
    SQLiteDatabase sqLiteDatabase = getReadableDatabase();
    Cursor cursor = null;
    try {
        cursor = sqLiteDatabase.rawQuery(query, null);
        // Looping through all rows and adding them to list
        boolean hasNext = cursor.moveToFirst();
        while (hasNext) {
            EncryptedEntry encryptedEntry = new EncryptedEntry();
            encryptedEntry.setId((cursor.getInt(0)));
            encryptedEntry.setPath(cursor.getString(1));
            encryptedEntry.setPassword(CryptUtil.decryptPassword(context, cursor.getString(2)));
            entryList.add(encryptedEntry);
            hasNext = cursor.moveToNext();
        }
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
    return entryList;
}
Also used : SQLiteDatabase(android.database.sqlite.SQLiteDatabase) EncryptedEntry(com.amaze.filemanager.database.models.EncryptedEntry) ArrayList(java.util.ArrayList) Cursor(android.database.Cursor)

Example 7 with EncryptedEntry

use of com.amaze.filemanager.database.models.EncryptedEntry in project AmazeFileManager by TeamAmaze.

the class EncryptDecryptUtils method decryptFile.

public static void decryptFile(Context c, final MainActivity mainActivity, final MainFragment main, OpenMode openMode, HybridFileParcelable sourceFile, String decryptPath, UtilitiesProvider utilsProvider, boolean broadcastResult) {
    Intent decryptIntent = new Intent(main.getContext(), DecryptService.class);
    decryptIntent.putExtra(EncryptService.TAG_OPEN_MODE, openMode.ordinal());
    decryptIntent.putExtra(EncryptService.TAG_SOURCE, sourceFile);
    decryptIntent.putExtra(EncryptService.TAG_DECRYPT_PATH, decryptPath);
    SharedPreferences preferences1 = PreferenceManager.getDefaultSharedPreferences(main.getContext());
    EncryptedEntry encryptedEntry;
    try {
        encryptedEntry = findEncryptedEntry(main.getContext(), sourceFile.getPath());
    } catch (GeneralSecurityException | IOException e) {
        e.printStackTrace();
        // we couldn't find any entry in database or lost the key to decipher
        Toast.makeText(main.getContext(), main.getActivity().getResources().getString(R.string.crypt_decryption_fail), Toast.LENGTH_LONG).show();
        return;
    }
    DecryptButtonCallbackInterface decryptButtonCallbackInterface = new DecryptButtonCallbackInterface() {

        @Override
        public void confirm(Intent intent) {
            ServiceWatcherUtil.runService(main.getContext(), intent);
        }

        @Override
        public void failed() {
            Toast.makeText(main.getContext(), main.getActivity().getResources().getString(R.string.crypt_decryption_fail_password), Toast.LENGTH_LONG).show();
        }
    };
    if (encryptedEntry == null) {
        // couldn't find the matching path in database, we lost the password
        Toast.makeText(main.getContext(), main.getActivity().getResources().getString(R.string.crypt_decryption_fail), Toast.LENGTH_LONG).show();
        return;
    }
    switch(encryptedEntry.getPassword()) {
        case PreferencesConstants.ENCRYPT_PASSWORD_FINGERPRINT:
            try {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    GeneralDialogCreation.showDecryptFingerprintDialog(c, mainActivity, decryptIntent, utilsProvider.getAppTheme(), decryptButtonCallbackInterface);
                } else
                    throw new IllegalStateException("API < M!");
            } catch (GeneralSecurityException | IOException | IllegalStateException e) {
                e.printStackTrace();
                Toast.makeText(main.getContext(), main.getResources().getString(R.string.crypt_decryption_fail), Toast.LENGTH_LONG).show();
            }
            break;
        case PreferencesConstants.ENCRYPT_PASSWORD_MASTER:
            try {
                GeneralDialogCreation.showDecryptDialog(c, mainActivity, decryptIntent, utilsProvider.getAppTheme(), CryptUtil.decryptPassword(c, preferences1.getString(PreferencesConstants.PREFERENCE_CRYPT_MASTER_PASSWORD, PreferencesConstants.PREFERENCE_CRYPT_MASTER_PASSWORD_DEFAULT)), decryptButtonCallbackInterface);
            } catch (GeneralSecurityException | IOException e) {
                e.printStackTrace();
                Toast.makeText(main.getContext(), main.getResources().getString(R.string.crypt_decryption_fail), Toast.LENGTH_LONG).show();
            }
            break;
        default:
            GeneralDialogCreation.showDecryptDialog(c, mainActivity, decryptIntent, utilsProvider.getAppTheme(), encryptedEntry.getPassword(), decryptButtonCallbackInterface);
            break;
    }
}
Also used : SharedPreferences(android.content.SharedPreferences) EncryptedEntry(com.amaze.filemanager.database.models.EncryptedEntry) GeneralSecurityException(java.security.GeneralSecurityException) Intent(android.content.Intent) IOException(java.io.IOException)

Example 8 with EncryptedEntry

use of com.amaze.filemanager.database.models.EncryptedEntry in project AmazeFileManager by TeamAmaze.

the class MainActivityHelper method rename.

public void rename(OpenMode mode, final String oldPath, final String newPath, final Activity context, boolean rootmode) {
    final Toast toast = Toast.makeText(context, context.getString(R.string.renaming), Toast.LENGTH_SHORT);
    toast.show();
    Operations.rename(new HybridFile(mode, oldPath), new HybridFile(mode, newPath), rootmode, context, new Operations.ErrorCallBack() {

        @Override
        public void exists(HybridFile file) {
            context.runOnUiThread(() -> {
                if (toast != null)
                    toast.cancel();
                Toast.makeText(mainActivity, context.getString(R.string.fileexist), Toast.LENGTH_SHORT).show();
            });
        }

        @Override
        public void launchSAF(HybridFile file) {
        }

        @Override
        public void launchSAF(final HybridFile file, final HybridFile file1) {
            context.runOnUiThread(() -> {
                if (toast != null)
                    toast.cancel();
                mainActivity.oppathe = file.getPath();
                mainActivity.oppathe1 = file1.getPath();
                mainActivity.operation = DataUtils.RENAME;
                guideDialogForLEXA(mainActivity.oppathe1);
            });
        }

        @Override
        public void done(final HybridFile hFile, final boolean b) {
            context.runOnUiThread(() -> {
                if (b) {
                    Intent intent = new Intent(MainActivity.KEY_INTENT_LOAD_LIST);
                    intent.putExtra(MainActivity.KEY_INTENT_LOAD_LIST_FILE, hFile.getParent(context));
                    mainActivity.sendBroadcast(intent);
                    // update the database entry to reflect rename for encrypted file
                    if (oldPath.endsWith(CryptUtil.CRYPT_EXTENSION)) {
                        try {
                            CryptHandler cryptHandler = new CryptHandler(context);
                            EncryptedEntry oldEntry = cryptHandler.findEntry(oldPath);
                            EncryptedEntry newEntry = new EncryptedEntry();
                            newEntry.setId(oldEntry.getId());
                            newEntry.setPassword(oldEntry.getPassword());
                            newEntry.setPath(newPath);
                            cryptHandler.updateEntry(oldEntry, newEntry);
                        } catch (Exception e) {
                            e.printStackTrace();
                        // couldn't change the entry, leave it alone
                        }
                    }
                } else
                    Toast.makeText(context, context.getString(R.string.operationunsuccesful), Toast.LENGTH_SHORT).show();
            });
        }

        @Override
        public void invalidName(final HybridFile file) {
            context.runOnUiThread(() -> {
                if (toast != null)
                    toast.cancel();
                Toast.makeText(context, context.getString(R.string.invalid_name) + ": " + file.getName(), Toast.LENGTH_LONG).show();
            });
        }
    });
}
Also used : HybridFile(com.amaze.filemanager.filesystem.HybridFile) Toast(android.widget.Toast) CryptHandler(com.amaze.filemanager.database.CryptHandler) EncryptedEntry(com.amaze.filemanager.database.models.EncryptedEntry) Intent(android.content.Intent) Operations(com.amaze.filemanager.filesystem.Operations)

Aggregations

EncryptedEntry (com.amaze.filemanager.database.models.EncryptedEntry)8 CryptHandler (com.amaze.filemanager.database.CryptHandler)5 Intent (android.content.Intent)3 Cursor (android.database.Cursor)2 SQLiteDatabase (android.database.sqlite.SQLiteDatabase)2 SharedPreferences (android.content.SharedPreferences)1 Toast (android.widget.Toast)1 ShellNotRunningException (com.amaze.filemanager.exceptions.ShellNotRunningException)1 HybridFile (com.amaze.filemanager.filesystem.HybridFile)1 HybridFileParcelable (com.amaze.filemanager.filesystem.HybridFileParcelable)1 Operations (com.amaze.filemanager.filesystem.Operations)1 IOException (java.io.IOException)1 MalformedURLException (java.net.MalformedURLException)1 GeneralSecurityException (java.security.GeneralSecurityException)1 ArrayList (java.util.ArrayList)1 SmbException (jcifs.smb.SmbException)1