Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/remove main thread queries #3827

Open
wants to merge 9 commits into
base: release/4.0
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@

import java.lang.ref.WeakReference;

import com.amaze.filemanager.database.CloudHandler;
import com.amaze.filemanager.ui.activities.MainActivity;

import android.database.Cursor;
Expand All @@ -34,7 +33,7 @@ public class CloudLoaderAsyncTask extends AsyncTask<Void, Void, Boolean> {

private final WeakReference<MainActivity> mainActivity;

public CloudLoaderAsyncTask(MainActivity mainActivity, CloudHandler unused1, Cursor unused2) {
public CloudLoaderAsyncTask(MainActivity mainActivity, Cursor unused2) {
this.mainActivity = new WeakReference<>(mainActivity);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,7 @@ protected final AsyncTaskResult<Boolean> doInBackground(

// delete file entry from encrypted database
if (file.getName(applicationContext).endsWith(CryptUtil.CRYPT_EXTENSION)) {
CryptHandler handler = CryptHandler.INSTANCE;
handler.clear(file.getPath());
CryptHandler.clear(file.getPath());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ class MoveFilesTask(
newEntry.id = oldEntry.id
newEntry.password = oldEntry.password
newEntry.path = paths[i] + "/" + file.getName(applicationContext)
CryptHandler.updateEntry(oldEntry, newEntry)
CryptHandler.updateEntry(newEntry)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -323,8 +323,7 @@ private void findAndReplaceEncryptedEntry(HybridFileParcelable sourceFile) {
if (sourceFile.getName(c).endsWith(CryptUtil.CRYPT_EXTENSION)) {
try {

CryptHandler cryptHandler = CryptHandler.INSTANCE;
EncryptedEntry oldEntry = cryptHandler.findEntry(sourceFile.getPath());
EncryptedEntry oldEntry = CryptHandler.findEntry(sourceFile.getPath());
EncryptedEntry newEntry = new EncryptedEntry();

newEntry.setPassword(oldEntry.getPassword());
Expand All @@ -334,10 +333,10 @@ private void findAndReplaceEncryptedEntry(HybridFileParcelable sourceFile) {

// file was been moved, update the existing entry
newEntry.setId(oldEntry.getId());
cryptHandler.updateEntry(oldEntry, newEntry);
CryptHandler.updateEntry(newEntry);
} else {
// file was copied, create a new entry with same data
cryptHandler.addEntry(newEntry);
CryptHandler.addEntry(newEntry);
}
} catch (Exception e) {
LOG.warn("failed to find and replace encrypted entry after copy", e);
Expand Down
117 changes: 0 additions & 117 deletions app/src/main/java/com/amaze/filemanager/database/CloudHandler.java

This file was deleted.

119 changes: 119 additions & 0 deletions app/src/main/java/com/amaze/filemanager/database/CloudHandler.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/*
* Copyright (C) 2014-2023 Arpit Khurana <[email protected]>, Vishal Nehra <[email protected]>,
* Emmanuel Messulam<[email protected]>, Raymond Lai <airwave209gt at gmail.com> and Contributors.
*
* This file is part of Amaze File Manager.
*
* Amaze File Manager is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package com.amaze.filemanager.database

import android.annotation.SuppressLint
import android.content.Context
import com.amaze.filemanager.application.AppConfig
import com.amaze.filemanager.database.daos.CloudEntryDao
import com.amaze.filemanager.database.models.explorer.CloudEntry
import com.amaze.filemanager.fileoperations.exceptions.CloudPluginException
import com.amaze.filemanager.fileoperations.filesystem.OpenMode
import com.amaze.filemanager.ui.fragments.CloudSheetFragment
import io.reactivex.schedulers.Schedulers
import org.slf4j.LoggerFactory

/** Created by vishal on 18/4/17. */
@SuppressLint("CheckResult")
object CloudHandler {

private val LOG = LoggerFactory.getLogger(CloudHandler::class.java)

private val cloudEntryDao: CloudEntryDao by lazy {
AppConfig.getInstance().explorerDatabase.cloudEntryDao()
}

@JvmStatic
@Throws(CloudPluginException::class)
fun addEntry(context: Context, cloudEntry: CloudEntry?) {
if (!CloudSheetFragment.isCloudProviderAvailable(context)) throw CloudPluginException()

cloudEntryDao.insert(cloudEntry).subscribeOn(Schedulers.io()).subscribe()
}

@JvmStatic
fun clear(serviceType: OpenMode) {

Check warning on line 53 in app/src/main/java/com/amaze/filemanager/database/CloudHandler.kt

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

app/src/main/java/com/amaze/filemanager/database/CloudHandler.kt#L53

The function clear is missing documentation.
cloudEntryDao
.findByServiceType(serviceType.ordinal)
.subscribeOn(Schedulers.io())
.subscribe(
::deleteCloudEntry
) { throwable ->
LOG.warn("failed to delete cloud connection", throwable)
}
}

@JvmStatic
@Throws(CloudPluginException::class)
fun updateEntry(context: Context, serviceType: OpenMode?, newCloudEntry: CloudEntry) {

Check warning on line 66 in app/src/main/java/com/amaze/filemanager/database/CloudHandler.kt

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

app/src/main/java/com/amaze/filemanager/database/CloudHandler.kt#L66

The function updateEntry is missing documentation.
if (!CloudSheetFragment.isCloudProviderAvailable(context)) throw CloudPluginException()

cloudEntryDao.update(newCloudEntry).subscribeOn(Schedulers.io()).subscribe()
}

@JvmStatic
private fun deleteCloudEntry(cloudEntry: CloudEntry) {
cloudEntryDao
.delete(cloudEntry)
.subscribeOn(Schedulers.io())
.subscribe()
}

@JvmStatic
fun clearAllCloudConnections() {
cloudEntryDao.clear().subscribeOn(Schedulers.io()).blockingGet()
}

@JvmStatic
@Throws(CloudPluginException::class)
fun findEntry(context: Context, serviceType: OpenMode): CloudEntry? {
if (!CloudSheetFragment.isCloudProviderAvailable(context)) throw CloudPluginException()

return try {
cloudEntryDao
.findByServiceType(serviceType.ordinal)
.subscribeOn(Schedulers.io())
.blockingGet()
} catch (e: Exception) {
// catch error to handle Single#onError for blockingGet
LOG.error(javaClass.simpleName, e)
null
}
}

@JvmStatic
@Throws(CloudPluginException::class)
fun getAllEntries(context: Context): List<CloudEntry> {
if (!CloudSheetFragment.isCloudProviderAvailable(context)) throw CloudPluginException()

return cloudEntryDao.list().subscribeOn(Schedulers.io()).blockingGet()
}

const val CLOUD_PREFIX_BOX = "box:/"
const val CLOUD_PREFIX_DROPBOX = "dropbox:/"
const val CLOUD_PREFIX_GOOGLE_DRIVE = "gdrive:/"
const val CLOUD_PREFIX_ONE_DRIVE = "onedrive:/"

const val CLOUD_NAME_GOOGLE_DRIVE = "Google Drive™"
const val CLOUD_NAME_DROPBOX = "Dropbox"
const val CLOUD_NAME_ONE_DRIVE = "One Drive"
const val CLOUD_NAME_BOX = "Box"
}
30 changes: 17 additions & 13 deletions app/src/main/java/com/amaze/filemanager/database/CryptHandler.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2014-2020 Arpit Khurana <[email protected]>, Vishal Nehra <[email protected]>,
* Copyright (C) 2014-2023 Arpit Khurana <[email protected]>, Vishal Nehra <[email protected]>,
* Emmanuel Messulam<[email protected]>, Raymond Lai <airwave209gt at gmail.com> and Contributors.
*
* This file is part of Amaze File Manager.
Expand All @@ -21,6 +21,7 @@
package com.amaze.filemanager.database

import com.amaze.filemanager.application.AppConfig
import com.amaze.filemanager.database.daos.EncryptedEntryDao
import com.amaze.filemanager.database.models.explorer.EncryptedEntry
import io.reactivex.schedulers.Schedulers
import org.slf4j.Logger
Expand All @@ -30,45 +31,48 @@ import org.slf4j.LoggerFactory
object CryptHandler {

private val log: Logger = LoggerFactory.getLogger(CryptHandler::class.java)
private val database: ExplorerDatabase = AppConfig.getInstance().explorerDatabase

private val encryptedEntryDao: EncryptedEntryDao by lazy {
AppConfig.getInstance().explorerDatabase.encryptedEntryDao()
}

/**
* Add [EncryptedEntry] to database.
*/
@JvmStatic
fun addEntry(encryptedEntry: EncryptedEntry) {
database.encryptedEntryDao().insert(encryptedEntry).subscribeOn(Schedulers.io()).subscribe()
encryptedEntryDao.insert(encryptedEntry).subscribeOn(Schedulers.io()).subscribe()
}

/**
* Remove [EncryptedEntry] of specified path.
*/
@JvmStatic
fun clear(path: String) {
database.encryptedEntryDao().delete(path).subscribeOn(Schedulers.io()).subscribe()
encryptedEntryDao.delete(path).subscribeOn(Schedulers.io()).subscribe()
}

/**
* Update specified new [EncryptedEntry] in database.
*/
fun updateEntry(oldEncryptedEntry: EncryptedEntry, newEncryptedEntry: EncryptedEntry) {
database.encryptedEntryDao().update(newEncryptedEntry).subscribeOn(Schedulers.io())
.subscribe()
@JvmStatic
fun updateEntry(newEncryptedEntry: EncryptedEntry) {
encryptedEntryDao.update(newEncryptedEntry).subscribeOn(Schedulers.io()).subscribe()
}

/**
* Find [EncryptedEntry] of specified path. Returns null if not exist.
*/
@JvmStatic
fun findEntry(path: String): EncryptedEntry? {
return runCatching {
database.encryptedEntryDao().select(path).subscribeOn(Schedulers.io()).blockingGet()
encryptedEntryDao.select(path).subscribeOn(Schedulers.io()).blockingGet()
}.onFailure {
log.error(it.message!!)
}.getOrNull()
}

@JvmStatic
val allEntries: Array<EncryptedEntry>
get() {
val encryptedEntryList =
database.encryptedEntryDao().list().subscribeOn(Schedulers.io()).blockingGet()
return encryptedEntryList.toTypedArray()
}
get() = encryptedEntryDao.list().subscribeOn(Schedulers.io()).blockingGet().toTypedArray()
}