use of com.amaze.filemanager.database.models.explorer.EncryptedEntry in project AmazeFileManager by TeamAmaze.
the class EncryptDecryptUtils method startEncryption.
/**
* Queries database to map path and password. Starts the encryption process after database query
*
* @param path the path of file to encrypt
* @param password the password in plaintext
* @throws GeneralSecurityException Errors on encrypting file/folder
* @throws IOException I/O errors on encrypting file/folder
*/
public static void startEncryption(Context c, final String path, final String password, Intent intent) throws GeneralSecurityException, IOException {
CryptHandler cryptHandler = CryptHandler.getInstance();
String destPath = path.substring(0, path.lastIndexOf('/') + 1).concat(intent.getStringExtra(EncryptService.TAG_ENCRYPT_TARGET));
// EncryptService.TAG_ENCRYPT_TARGET already has the .aze extension, no need to append again
EncryptedEntry encryptedEntry = new EncryptedEntry(destPath, password);
cryptHandler.addEntry(encryptedEntry);
// start the encryption process
ServiceWatcherUtil.runService(c, intent);
}
use of com.amaze.filemanager.database.models.explorer.EncryptedEntry in project AmazeFileManager by TeamAmaze.
the class EncryptDecryptUtils method findEncryptedEntry.
/**
* Queries database to find entry for the specific path
*
* @param path the path to match with
* @return the entry
*/
private static EncryptedEntry findEncryptedEntry(String path) throws GeneralSecurityException, IOException {
CryptHandler handler = CryptHandler.getInstance();
EncryptedEntry matchedEntry = null;
// find closest path which matches with database entry
for (EncryptedEntry encryptedEntry : handler.getAllEntries()) {
if (path.contains(encryptedEntry.getPath())) {
if (matchedEntry == null || matchedEntry.getPath().length() < encryptedEntry.getPath().length()) {
matchedEntry = encryptedEntry;
}
}
}
return matchedEntry;
}
use of com.amaze.filemanager.database.models.explorer.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(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().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().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().getString(R.string.crypt_decryption_fail), Toast.LENGTH_LONG).show();
return;
}
switch(encryptedEntry.getPassword().value) {
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.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.getString(R.string.crypt_decryption_fail), Toast.LENGTH_LONG).show();
}
break;
default:
GeneralDialogCreation.showDecryptDialog(c, mainActivity, decryptIntent, utilsProvider.getAppTheme(), encryptedEntry.getPassword().value, decryptButtonCallbackInterface);
break;
}
}
use of com.amaze.filemanager.database.models.explorer.EncryptedEntry in project AmazeFileManager by TeamAmaze.
the class MoveFiles method onPostExecute.
@Override
public void onPostExecute(Boolean movedCorrectly) {
if (movedCorrectly) {
if (currentPath.equals(paths.get(0))) {
// mainFrag.updateList();
Intent intent = new Intent(MainActivity.KEY_INTENT_LOAD_LIST);
intent.putExtra(MainActivity.KEY_INTENT_LOAD_LIST_FILE, paths.get(0));
context.sendBroadcast(intent);
}
if (invalidOperation) {
Toast.makeText(context, R.string.some_files_failed_invalid_operation, Toast.LENGTH_LONG).show();
}
for (int i = 0; i < paths.size(); i++) {
List<HybridFile> targetFiles = new ArrayList<>();
List<HybridFileParcelable> sourcesFiles = new ArrayList<>();
for (HybridFileParcelable f : files.get(i)) {
targetFiles.add(new HybridFile(OpenMode.FILE, paths.get(i) + "/" + f.getName(context)));
}
for (List<HybridFileParcelable> hybridFileParcelables : files) {
sourcesFiles.addAll(hybridFileParcelables);
}
FileUtils.scanFile(context, sourcesFiles.toArray(new HybridFileParcelable[sourcesFiles.size()]));
FileUtils.scanFile(context, targetFiles.toArray(new HybridFile[targetFiles.size()]));
}
// updating encrypted db entry if any encrypted file was moved
AppConfig.getInstance().runInBackground(() -> {
for (int i = 0; i < paths.size(); i++) {
for (HybridFileParcelable file : files.get(i)) {
if (file.getName(context).endsWith(CryptUtil.CRYPT_EXTENSION)) {
try {
CryptHandler cryptHandler = CryptHandler.getInstance();
EncryptedEntry oldEntry = cryptHandler.findEntry(file.getPath());
EncryptedEntry newEntry = new EncryptedEntry();
newEntry.setId(oldEntry.getId());
newEntry.setPassword(oldEntry.getPassword());
newEntry.setPath(paths.get(i) + "/" + file.getName(context));
cryptHandler.updateEntry(oldEntry, newEntry);
} catch (Exception e) {
e.printStackTrace();
// couldn't change the entry, leave it alone
}
}
}
}
});
} else {
if (destinationSize < totalBytes) {
// destination don't have enough space; return
Toast.makeText(context, context.getResources().getString(R.string.in_safe), Toast.LENGTH_LONG).show();
return;
}
for (int i = 0; i < paths.size(); i++) {
Intent intent = new Intent(context, CopyService.class);
intent.putExtra(CopyService.TAG_COPY_SOURCES, files.get(i));
intent.putExtra(CopyService.TAG_COPY_TARGET, paths.get(i));
intent.putExtra(CopyService.TAG_COPY_MOVE, true);
intent.putExtra(CopyService.TAG_COPY_OPEN_MODE, mode.ordinal());
intent.putExtra(CopyService.TAG_IS_ROOT_EXPLORER, isRootExplorer);
ServiceWatcherUtil.runService(context, intent);
}
}
}
use of com.amaze.filemanager.database.models.explorer.EncryptedEntry in project AmazeFileManager by TeamAmaze.
the class MainActivityHelper method rename.
public void rename(OpenMode mode, final String oldPath, final String newPath, final String newName, final boolean isDirectory, final Activity context, boolean rootmode) {
final Toast toast = Toast.makeText(context, context.getString(R.string.renaming), Toast.LENGTH_SHORT);
toast.show();
HybridFile oldFile = new HybridFile(mode, oldPath);
HybridFile newFile;
if (Utils.isNullOrEmpty(newName)) {
newFile = new HybridFile(mode, newPath);
} else {
newFile = new HybridFile(mode, newPath, newName, isDirectory);
}
Operations.rename(oldFile, newFile, 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 = RENAME;
guideDialogForLEXA(mainActivity.oppathe1);
});
}
@Override
public void done(final HybridFile hFile, final boolean b) {
context.runOnUiThread(() -> {
/*
* DocumentFile.renameTo() may return false even when rename is successful. Hence we need an extra check
* instead of merely looking at the return value
*/
if (b || newFile.exists(context)) {
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 = CryptHandler.getInstance();
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.operation_unsuccesful), 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(context), Toast.LENGTH_LONG).show();
});
}
});
}
Aggregations