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 @@ -26,11 +26,13 @@
import java.io.File;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collections;
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 +60,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 +73,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,76 +88,78 @@
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 String[] POST_PROCESS_EXCLUDED_PATHS = {"5", "6", "7"};
private static final int RECENT_FILES_LIMIT = 20;
private static final int RECENT_FILES_DAYS = 2;

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 {

@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();
if (hasMissingLoadRequirements(mainFragment, context)) {
throw new CancellationException();
}
MainFragmentViewModel mainFragmentViewModel = mainFragment.getMainFragmentViewModel();
MainActivityViewModel mainActivityViewModel = mainFragment.getMainActivityViewModel();
HybridFile hFile = resolveOpenMode(mainFragment, mainFragmentViewModel);

mainFragmentViewModel.setFolderCount(0);
mainFragmentViewModel.setFileCount(0);
final List<LayoutElementParcelable> list =
loadOpenModeElements(hFile, mainActivityViewModel, mainFragment);
if (isPostProcessingRequired(list)) {
postListCustomPathProcess(list, mainFragmentViewModel);
}
return new Pair<>(openmode, list);
}

if (mainFragment == null
private boolean hasMissingLoadRequirements(
@Nullable MainFragment mainFragment, @Nullable Context context) {
return mainFragment == null
|| context == null
|| mainFragment.getMainFragmentViewModel() == null
|| mainFragment.getMainActivityViewModel() == null
|| path == null) {
cancel(true);
return null;
}

HybridFile hFile = null;
MainFragmentViewModel mainFragmentViewModel = mainFragment.getMainFragmentViewModel();
MainActivityViewModel mainActivityViewModel = mainFragment.getMainActivityViewModel();
|| path == null;
}

if (OpenMode.UNKNOWN.equals(openmode)
private @Nullable HybridFile resolveOpenMode(
@NonNull MainFragment mainFragment, @NonNull MainFragmentViewModel mainFragmentViewModel) {
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);
}
|| OpenMode.TRASH_BIN.equals(openmode))) {
return null;
}
HybridFile hFile = new HybridFile(openmode, path);
hFile.generateMode(mainFragment.getActivity());
openmode = hFile.getMode();
if (hFile.isSmb()) {
mainFragmentViewModel.setSmbPath(path);
}
return hFile;
}

if (isCancelled()) return null;
private boolean isPostProcessingRequired(@Nullable List<LayoutElementParcelable> list) {
return list != null
&& !(openmode == OpenMode.CUSTOM
&& Arrays.asList(POST_PROCESS_EXCLUDED_PATHS).contains(path));
}

mainFragmentViewModel.setFolderCount(0);
mainFragmentViewModel.setFileCount(0);
private @Nullable List<LayoutElementParcelable> loadOpenModeElements(
@Nullable HybridFile hFile,
@NonNull MainActivityViewModel mainActivityViewModel,
@NonNull MainFragment mainFragment)
throws CloudPluginException {
final List<LayoutElementParcelable> list;

switch (openmode) {
case SMB:
list = listSmb(hFile, mainActivityViewModel, mainFragment);
Expand All @@ -182,39 +184,36 @@ public LoadFilesListTask(
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)))) {
postListCustomPathProcess(list, mainFragmentViewModel);
}

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

@Override
protected void onCancelled() {
listener.onAsyncTaskFinished(null);
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 +247,6 @@ protected void onProgressUpdate(Throwable... values) {
}
}

@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 +333,7 @@ private void postListCustomPathProcess(
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 +403,7 @@ private List<LayoutElementParcelable> listaudio() {
final Context context = this.context.get();

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

Cursor cursor =
Expand All @@ -437,8 +429,7 @@ else if (cursor.getCount() > 0 && cursor.moveToFirst()) {
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 +490,7 @@ else if (cursor.getCount() > 0 && cursor.moveToFirst()) {
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 +527,7 @@ else if (cursor.getCount() > 0 && cursor.moveToFirst()) {
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,24 +555,23 @@ else if (cursor.getCount() > 0 && cursor.moveToFirst()) {
final Context context = this.context.get();

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

List<LayoutElementParcelable> recentFiles = new ArrayList<>(20);
List<LayoutElementParcelable> recentFiles = new ArrayList<>(RECENT_FILES_LIMIT);
final String[] projection = {
MediaStore.Files.FileColumns.DATA, MediaStore.Files.FileColumns.DATE_MODIFIED
};
final String selection = MediaStore.Files.FileColumns.DATA + " NOT LIKE ?";
final String[] selectionArgs = new String[] {AppConfig.getTrashBinBasePath() + "%"};
Calendar c = Calendar.getInstance();
c.set(Calendar.DAY_OF_YEAR, c.get(Calendar.DAY_OF_YEAR) - 2);
c.set(Calendar.DAY_OF_YEAR, c.get(Calendar.DAY_OF_YEAR) - RECENT_FILES_DAYS);
Date d = c.getTime();
Cursor cursor;
try {
if (SDK_INT >= Q) {
Bundle queryArgs = new Bundle();
queryArgs.putInt(ContentResolver.QUERY_ARG_LIMIT, 20);
queryArgs.putInt(ContentResolver.QUERY_ARG_LIMIT, RECENT_FILES_LIMIT);
queryArgs.putStringArray(
ContentResolver.QUERY_ARG_SORT_COLUMNS,
new String[] {MediaStore.Files.FileColumns.DATE_MODIFIED});
Expand All @@ -605,7 +593,9 @@ else if (cursor.getCount() > 0 && cursor.moveToFirst()) {
projection,
selection,
selectionArgs,
MediaStore.Files.FileColumns.DATE_MODIFIED + " DESC LIMIT 20");
MediaStore.Files.FileColumns.DATE_MODIFIED
+ " DESC LIMIT "
+ RECENT_FILES_LIMIT);
}
} catch (SecurityException e) {
// Storage permission denied; treat as no recent files instead of crashing.
Expand Down Expand Up @@ -634,8 +624,7 @@ else if (cursor.getCount() > 0 && cursor.moveToFirst()) {
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 +718,6 @@ private List<LayoutElementParcelable> listSmb(
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 +851,7 @@ private void listOtgInternal(String path, OnFileFound fileFound) {
final Context context = this.context.get();

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

OTGUtil.getDocumentFiles(path, context, fileFound);
Expand All @@ -874,8 +861,7 @@ private void listDocumentFilesInternal(OnFileFound fileFound) {
final Context context = this.context.get();

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

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

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

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