Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.CancellationException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -58,7 +59,6 @@
import com.amaze.filemanager.utils.DataUtils;
import com.amaze.filemanager.utils.GenericExtKt;
import com.amaze.filemanager.utils.OTGUtil;
import com.amaze.filemanager.utils.OnAsyncTaskFinished;
import com.amaze.filemanager.utils.OnFileFound;
import com.amaze.filemanager.utils.Utils;
import com.amaze.trashbin.TrashBin;
Expand All @@ -72,7 +72,6 @@
import android.content.pm.ResolveInfo;
import android.database.Cursor;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.provider.MediaStore;
import android.text.format.Formatter;
Expand All @@ -88,42 +87,12 @@
import jcifs.smb.SmbFile;
import kotlin.collections.CollectionsKt;

public class LoadFilesListTask
extends AsyncTask<Void, Throwable, Pair<OpenMode, List<LayoutElementParcelable>>> {
public class LoadFilesListTask {

private static final Logger LOG = LoggerFactory.getLogger(LoadFilesListTask.class);

private String path;
private WeakReference<MainFragment> mainFragmentReference;
private WeakReference<Context> context;
private OpenMode openmode;
private boolean showHiddenFiles, showThumbs;
private DataUtils dataUtils = DataUtils.getInstance();
private OnAsyncTaskFinished<Pair<OpenMode, List<LayoutElementParcelable>>> listener;
private boolean forceReload;

public LoadFilesListTask(
Context context,
String path,
MainFragment mainFragment,
OpenMode openmode,
boolean showThumbs,
boolean showHiddenFiles,
boolean forceReload,
OnAsyncTaskFinished<Pair<OpenMode, List<LayoutElementParcelable>>> l) {
this.path = path;
this.mainFragmentReference = new WeakReference<>(mainFragment);
this.openmode = openmode;
this.context = new WeakReference<>(context);
this.showThumbs = showThumbs;
this.showHiddenFiles = showHiddenFiles;
this.listener = l;
this.forceReload = forceReload;
}
public Pair<OpenMode, List<LayoutElementParcelable>> load() throws Exception {

Check warning on line 94 in app/src/main/java/com/amaze/filemanager/asynchronous/asynctasks/LoadFilesListTask.java

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

app/src/main/java/com/amaze/filemanager/asynchronous/asynctasks/LoadFilesListTask.java#L94

The method 'load()' has an NPath complexity of 2340, current threshold is 200

@Override
@SuppressWarnings({"PMD.NPathComplexity", "ComplexMethod", "LongMethod"})
protected @Nullable Pair<OpenMode, List<LayoutElementParcelable>> doInBackground(Void... p) {
final MainFragment mainFragment = this.mainFragmentReference.get();
final Context context = this.context.get();

Expand All @@ -132,89 +101,113 @@
|| mainFragment.getMainFragmentViewModel() == null
|| mainFragment.getMainActivityViewModel() == null
|| path == null) {
cancel(true);
return null;

throw new CancellationException();
}

HybridFile hFile = null;

MainFragmentViewModel mainFragmentViewModel = mainFragment.getMainFragmentViewModel();

MainActivityViewModel mainActivityViewModel = mainFragment.getMainActivityViewModel();

if (OpenMode.UNKNOWN.equals(openmode)
|| OpenMode.CUSTOM.equals(openmode)
|| OpenMode.TRASH_BIN.equals(openmode)) {

hFile = new HybridFile(openmode, path);

hFile.generateMode(mainFragment.getActivity());

openmode = hFile.getMode();

if (hFile.isSmb()) {
mainFragmentViewModel.setSmbPath(path);
}
}

if (isCancelled()) return null;

mainFragmentViewModel.setFolderCount(0);
mainFragmentViewModel.setFileCount(0);

final List<LayoutElementParcelable> list;

switch (openmode) {
case SMB:
list = listSmb(hFile, mainActivityViewModel, mainFragment);
break;

case FTP:
case SFTP:
list = listSftp(mainActivityViewModel);
break;

case CUSTOM:
case TRASH_BIN:
list = getCachedMediaList(mainActivityViewModel);
break;

case OTG:
list = listOtg();
openmode = OpenMode.OTG;
break;

case DOCUMENT_FILE:
list = listDocumentFiles(mainActivityViewModel);
openmode = OpenMode.DOCUMENT_FILE;
break;

case DROPBOX:
case BOX:
case GDRIVE:
case ONEDRIVE:
try {
list = listCloud(mainActivityViewModel);
} catch (CloudPluginException e) {
LOG.warn("failed to load cloud files", e);
AppConfig.toast(context, context.getResources().getString(R.string.failed_no_connection));
return new Pair<>(openmode, Collections.emptyList());
}
list = listCloud(mainActivityViewModel);
break;

case ANDROID_DATA:
list = listAppDataDirectories(path);
break;

default:
// we're neither in OTG not in SMB, load the list based on root/general filesystem
list = listDefault(mainActivityViewModel, mainFragment);
break;
}

if (list != null
&& !(openmode == OpenMode.CUSTOM
&& (("5").equals(path) || ("6").equals(path) || ("7").equals(path)))) {
&& ("5".equals(path) || "6".equals(path) || "7".equals(path)))) {

postListCustomPathProcess(list, mainFragmentViewModel);
}

return new Pair<>(openmode, list);
}

@Override
protected void onCancelled() {
listener.onAsyncTaskFinished(null);
private String path;

Check notice on line 185 in app/src/main/java/com/amaze/filemanager/asynchronous/asynctasks/LoadFilesListTask.java

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

app/src/main/java/com/amaze/filemanager/asynchronous/asynctasks/LoadFilesListTask.java#L185

Fields should be declared at the top of the class, before any method declarations, constructors, initializers or inner classes.
private WeakReference<MainFragment> mainFragmentReference;

Check notice on line 186 in app/src/main/java/com/amaze/filemanager/asynchronous/asynctasks/LoadFilesListTask.java

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

app/src/main/java/com/amaze/filemanager/asynchronous/asynctasks/LoadFilesListTask.java#L186

Fields should be declared at the top of the class, before any method declarations, constructors, initializers or inner classes.
private WeakReference<Context> context;

Check notice on line 187 in app/src/main/java/com/amaze/filemanager/asynchronous/asynctasks/LoadFilesListTask.java

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

app/src/main/java/com/amaze/filemanager/asynchronous/asynctasks/LoadFilesListTask.java#L187

Fields should be declared at the top of the class, before any method declarations, constructors, initializers or inner classes.
private OpenMode openmode;

Check notice on line 188 in app/src/main/java/com/amaze/filemanager/asynchronous/asynctasks/LoadFilesListTask.java

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

app/src/main/java/com/amaze/filemanager/asynchronous/asynctasks/LoadFilesListTask.java#L188

Fields should be declared at the top of the class, before any method declarations, constructors, initializers or inner classes.
private boolean showHiddenFiles, showThumbs;

Check notice on line 189 in app/src/main/java/com/amaze/filemanager/asynchronous/asynctasks/LoadFilesListTask.java

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

app/src/main/java/com/amaze/filemanager/asynchronous/asynctasks/LoadFilesListTask.java#L189

Fields should be declared at the top of the class, before any method declarations, constructors, initializers or inner classes.
private DataUtils dataUtils = DataUtils.getInstance();

Check notice on line 190 in app/src/main/java/com/amaze/filemanager/asynchronous/asynctasks/LoadFilesListTask.java

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

app/src/main/java/com/amaze/filemanager/asynchronous/asynctasks/LoadFilesListTask.java#L190

Fields should be declared at the top of the class, before any method declarations, constructors, initializers or inner classes.
private boolean forceReload;

Check notice on line 191 in app/src/main/java/com/amaze/filemanager/asynchronous/asynctasks/LoadFilesListTask.java

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

app/src/main/java/com/amaze/filemanager/asynchronous/asynctasks/LoadFilesListTask.java#L191

Fields should be declared at the top of the class, before any method declarations, constructors, initializers or inner classes.

public LoadFilesListTask(
Context context,
String path,
MainFragment mainFragment,
OpenMode openmode,
boolean showThumbs,
boolean showHiddenFiles,
boolean forceReload) {
this.path = path;
this.mainFragmentReference = new WeakReference<>(mainFragment);
this.openmode = openmode;
this.context = new WeakReference<>(context);
this.showThumbs = showThumbs;
this.showHiddenFiles = showHiddenFiles;
this.forceReload = forceReload;
}

@Override
protected void onProgressUpdate(Throwable... values) {
public void onProgressUpdate(Throwable... values) {
for (Throwable exception : values) {
if (exception instanceof SmbException) {
if ("/".equals(Uri.parse(path).getPath())) {
Expand Down Expand Up @@ -248,11 +241,6 @@
}
}

@Override
protected void onPostExecute(@Nullable Pair<OpenMode, List<LayoutElementParcelable>> list) {
listener.onAsyncTaskFinished(list);
}

private List<LayoutElementParcelable> getCachedMediaList(
MainActivityViewModel mainActivityViewModel) throws IllegalStateException {
List<LayoutElementParcelable> list;
Expand Down Expand Up @@ -339,8 +327,7 @@
final Context context = this.context.get();

if (mainFragment == null || context == null) {
cancel(true);
return null;
throw new CancellationException();
}

String size = "";
Expand Down Expand Up @@ -410,8 +397,7 @@
final Context context = this.context.get();

if (context == null) {
cancel(true);
return null;
throw new CancellationException();
}

Cursor cursor =
Expand All @@ -437,8 +423,7 @@
final Context context = this.context.get();

if (context == null) {
cancel(true);
return null;
throw new CancellationException();
}

ArrayList<LayoutElementParcelable> docs = new ArrayList<>();
Expand Down Expand Up @@ -499,8 +484,7 @@
final Context context = this.context.get();

if (context == null) {
cancel(true);
return null;
throw new CancellationException();
}

ArrayList<LayoutElementParcelable> apks = new ArrayList<>();
Expand Down Expand Up @@ -537,8 +521,7 @@
private @Nullable List<LayoutElementParcelable> listRecent() {
final MainFragment mainFragment = this.mainFragmentReference.get();
if (mainFragment == null) {
cancel(true);
return null;
throw new CancellationException();
}

UtilsHandler utilsHandler = AppConfig.getInstance().getUtilsHandler();
Expand Down Expand Up @@ -566,8 +549,7 @@
final Context context = this.context.get();

if (context == null) {
cancel(true);
return null;
throw new CancellationException();
}

List<LayoutElementParcelable> recentFiles = new ArrayList<>(20);
Expand Down Expand Up @@ -634,8 +616,7 @@
final Context context = this.context.get();

if (context == null) {
cancel(true);
return null;
throw new CancellationException();
}

TrashBin trashBin = AppConfig.getInstance().getTrashBinInstance();
Expand Down Expand Up @@ -729,7 +710,6 @@
mainFragment.reauthenticateSmb();
}
LOG.warn("failed to load smb list, authentication issue: ", e);
publishProgress(e);
return null;
} catch (SmbException | NullPointerException e) {
LOG.warn("Failed to load smb files for path: " + path, e);
Expand Down Expand Up @@ -863,8 +843,7 @@
final Context context = this.context.get();

if (context == null) {
cancel(true);
return;
throw new CancellationException();
}

OTGUtil.getDocumentFiles(path, context, fileFound);
Expand All @@ -874,8 +853,7 @@
final Context context = this.context.get();

if (context == null) {
cancel(true);
return;
throw new CancellationException();
}

OTGUtil.getDocumentFiles(
Expand All @@ -888,8 +866,7 @@
final Context context = this.context.get();

if (context == null) {
cancel(true);
return;
throw new CancellationException();
}

if (!CloudSheetFragment.isCloudProviderAvailable(context)) {
Expand Down
Loading