Skip to content

Commit

Permalink
Merge pull request #1977 from TeamAmaze/bugfix/1942
Browse files Browse the repository at this point in the history
  • Loading branch information
EmmanuelMess authored Sep 17, 2020
2 parents 8492c27 + 82072e7 commit 1b8317c
Show file tree
Hide file tree
Showing 35 changed files with 1,024 additions and 677 deletions.
8 changes: 8 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ dependencies {
implementation "androidx.room:room-runtime:$roomVersion"
implementation "com.android.billingclient:billing:$androidBillingVersion"
annotationProcessor "androidx.room:room-compiler:$roomVersion"
// RxJava support for Room
implementation "androidx.room:room-rxjava2:$roomVersion"
annotationProcessor 'androidx.annotation:annotation:1.1.0'

//For tests
Expand Down Expand Up @@ -218,6 +220,12 @@ dependencies {

implementation 'org.tukaani:xz:1.8'

implementation 'io.reactivex.rxjava2:rxandroid:2.1.1'
// Because RxAndroid releases are few and far between, it is recommended you also
// explicitly depend on RxJava's latest version for bug fixes and new features.
// (see https://github.com/ReactiveX/RxJava/releases for latest 3.x.x version)
implementation group: 'io.reactivex.rxjava2', name: 'rxjava', version: '2.1.1'

implementation project(':commons_compress_7z')
}

Expand Down
70 changes: 9 additions & 61 deletions app/src/main/java/com/amaze/filemanager/application/AppConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,6 @@
import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.StrictMode;
import android.widget.Toast;

Expand All @@ -47,6 +44,11 @@
import androidx.annotation.StringRes;
import androidx.appcompat.app.AppCompatDelegate;

import io.reactivex.Completable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import jcifs.Config;

public class AppConfig extends GlideApplication {

public static final String TAG = AppConfig.class.getSimpleName();
Expand All @@ -56,9 +58,6 @@ public class AppConfig extends GlideApplication {
private ImageLoader imageLoader;
private UtilsHandler utilsHandler;

private static Handler applicationhandler = new Handler();
private HandlerThread backgroundHandlerThread;
private static Handler backgroundHandler;
private WeakReference<Context> mainActivityContext;
private static ScreenUtils screenUtils;

Expand All @@ -77,7 +76,6 @@ public void onCreate() {
super.onCreate();
AppCompatDelegate.setCompatVectorFromResourcesEnabled(
true); // selector in srcCompat isn't supported without this
backgroundHandlerThread = new HandlerThread("app_background");
instance = this;

CustomSshJConfig.init();
Expand All @@ -87,10 +85,7 @@ public void onCreate() {
utilsProvider = new UtilitiesProvider(this);
utilsHandler = new UtilsHandler(this, utilitiesDatabase);

backgroundHandlerThread.start();
backgroundHandler = new Handler(backgroundHandlerThread.getLooper());

runInBackground(jcifs.Config::registerSmbURLHandler);
runInBackground(Config::registerSmbURLHandler);

// disabling file exposure method check for api n+
StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
Expand All @@ -100,62 +95,15 @@ public void onCreate() {
@Override
public void onTerminate() {
super.onTerminate();
backgroundHandlerThread.quit();
}

/**
* Post a runnable to handler. Use this in case we don't have any restriction to execute after
* this runnable is executed, and {@link #runInBackground(Runnable)} in case we need to execute
* something after execution in background
*/
public static void runInBackground(Runnable runnable) {
synchronized (backgroundHandler) {
backgroundHandler.post(runnable);
}
}

/**
* A compact AsyncTask which runs which executes whatever is passed by callbacks. Supports any
* class that extends an object as param array, and result too.
*/
public static <Params, Result> void runInParallel(
final CustomAsyncCallbacks<Params, Result> customAsyncCallbacks) {

synchronized (customAsyncCallbacks) {
new AsyncTask<Params, Void, Result>() {
@Override
protected void onPreExecute() {
super.onPreExecute();
customAsyncCallbacks.onPreExecute();
}

@Override
protected Result doInBackground(Object... params) {
return customAsyncCallbacks.doInBackground();
}

@Override
protected void onPostExecute(Result aVoid) {
super.onPostExecute(aVoid);
customAsyncCallbacks.onPostExecute(aVoid);
}
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, customAsyncCallbacks.parameters);
}
}

/** Interface providing callbacks utilized by {@link #runInBackground(Runnable)} */
public abstract static class CustomAsyncCallbacks<Params, Result> {
public final @Nullable Params[] parameters;

public CustomAsyncCallbacks(@Nullable Params[] params) {
parameters = params;
}

public abstract Result doInBackground();

public void onPostExecute(Result result) {}

public void onPreExecute() {}
public void runInBackground(Runnable runnable) {
Completable.fromRunnable(runnable).subscribeOn(Schedulers.io()).subscribe();
}

/**
Expand Down Expand Up @@ -212,7 +160,7 @@ public static void toast(Context context, String message) {
* @param r Runnable to run
*/
public void runInApplicationThread(Runnable r) {
applicationhandler.post(r);
Completable.fromRunnable(r).subscribeOn(AndroidSchedulers.mainThread()).subscribe();
}

public static synchronized AppConfig getInstance() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,27 +194,28 @@ public void onPostExecute(Boolean movedCorrectly) {
}

// updating encrypted db entry if any encrypted file was moved
AppConfig.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
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 {

Expand Down
33 changes: 26 additions & 7 deletions app/src/main/java/com/amaze/filemanager/database/CloudHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

package com.amaze.filemanager.database;

import java.util.Arrays;
import java.util.List;

import com.amaze.filemanager.database.models.explorer.CloudEntry;
Expand All @@ -29,9 +28,12 @@
import com.amaze.filemanager.utils.OpenMode;

import android.content.Context;
import android.util.Log;

import androidx.annotation.NonNull;

import io.reactivex.schedulers.Schedulers;

/** Created by vishal on 18/4/17. */
public class CloudHandler {

Expand All @@ -57,34 +59,51 @@ public void addEntry(CloudEntry cloudEntry) throws CloudPluginException {

if (!CloudSheetFragment.isCloudProviderAvailable(context)) throw new CloudPluginException();

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

public void clear(OpenMode serviceType) {
database
.cloudEntryDao()
.delete(database.cloudEntryDao().findByServiceType(serviceType.ordinal()));
.findByServiceType(serviceType.ordinal())
.subscribeOn(Schedulers.io())
.subscribe(
cloudEntry ->
database
.cloudEntryDao()
.delete(cloudEntry)
.subscribeOn(Schedulers.io())
.subscribe());
}

public void updateEntry(OpenMode serviceType, CloudEntry newCloudEntry)
throws CloudPluginException {

if (!CloudSheetFragment.isCloudProviderAvailable(context)) throw new CloudPluginException();

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

public CloudEntry findEntry(OpenMode serviceType) throws CloudPluginException {

if (!CloudSheetFragment.isCloudProviderAvailable(context)) throw new CloudPluginException();

return database.cloudEntryDao().findByServiceType(serviceType.ordinal());
try {
return database
.cloudEntryDao()
.findByServiceType(serviceType.ordinal())
.subscribeOn(Schedulers.io())
.blockingGet();
} catch (Exception e) {
// catch error to handle Single#onError for blockingGet
Log.e(getClass().getSimpleName(), e.getMessage());
return null;
}
}

public List<CloudEntry> getAllEntries() throws CloudPluginException {

if (!CloudSheetFragment.isCloudProviderAvailable(context)) throw new CloudPluginException();

return Arrays.asList(database.cloudEntryDao().list());
return database.cloudEntryDao().list().subscribeOn(Schedulers.io()).blockingGet();
}
}
25 changes: 20 additions & 5 deletions app/src/main/java/com/amaze/filemanager/database/CryptHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,17 @@

package com.amaze.filemanager.database;

import java.util.List;

import com.amaze.filemanager.application.AppConfig;
import com.amaze.filemanager.database.models.explorer.EncryptedEntry;

import android.util.Log;

import androidx.annotation.NonNull;

import io.reactivex.schedulers.Schedulers;

/** Created by vishal on 15/4/17. */
public class CryptHandler {

Expand All @@ -44,22 +50,31 @@ public static CryptHandler getInstance() {
}

public void addEntry(EncryptedEntry encryptedEntry) {
AppConfig.runInBackground(() -> database.encryptedEntryDao().insert(encryptedEntry));
database.encryptedEntryDao().insert(encryptedEntry).subscribeOn(Schedulers.io()).subscribe();
}

public void clear(String path) {
AppConfig.runInBackground(() -> database.encryptedEntryDao().delete(path));
database.encryptedEntryDao().delete(path).subscribeOn(Schedulers.io()).subscribe();
}

public void updateEntry(EncryptedEntry oldEncryptedEntry, EncryptedEntry newEncryptedEntry) {
AppConfig.runInBackground(() -> database.encryptedEntryDao().update(newEncryptedEntry));
database.encryptedEntryDao().update(newEncryptedEntry).subscribeOn(Schedulers.io()).subscribe();
}

public EncryptedEntry findEntry(String path) {
return database.encryptedEntryDao().select(path);
try {
return database.encryptedEntryDao().select(path).subscribeOn(Schedulers.io()).blockingGet();
} catch (Exception e) {
// catch error to handle Single#onError for blockingGet
Log.e(getClass().getSimpleName(), e.getMessage());
return null;
}
}

public EncryptedEntry[] getAllEntries() {
return database.encryptedEntryDao().list();
List<EncryptedEntry> encryptedEntryList =
database.encryptedEntryDao().list().subscribeOn(Schedulers.io()).blockingGet();
EncryptedEntry[] encryptedEntries = new EncryptedEntry[encryptedEntryList.size()];
return encryptedEntryList.toArray(encryptedEntries);
}
}
17 changes: 13 additions & 4 deletions app/src/main/java/com/amaze/filemanager/database/SortHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,13 @@
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import io.reactivex.schedulers.Schedulers;

/** Created by Ning on 5/28/2018. */
public class SortHandler {

Expand Down Expand Up @@ -70,19 +73,25 @@ public static int getSortType(Context context, String path) {
}

public void addEntry(Sort sort) {
AppConfig.runInBackground(() -> database.sortDao().insert(sort));
database.sortDao().insert(sort).subscribeOn(Schedulers.io()).subscribe();
}

public void clear(String path) {
AppConfig.runInBackground(() -> database.sortDao().clear(path));
database.sortDao().clear(path).subscribeOn(Schedulers.io()).subscribe();
}

public void updateEntry(Sort oldSort, Sort newSort) {
AppConfig.runInBackground(() -> database.sortDao().update(newSort));
database.sortDao().update(newSort).subscribeOn(Schedulers.io()).subscribe();
}

@Nullable
public Sort findEntry(String path) {
return database.sortDao().find(path);
try {
return database.sortDao().find(path).subscribeOn(Schedulers.io()).blockingGet();
} catch (Exception e) {
// catch error to handle Single#onError for blockingGet
Log.e(getClass().getSimpleName(), e.getMessage());
return null;
}
}
}
Loading

0 comments on commit 1b8317c

Please sign in to comment.