Skip to content

Commit

Permalink
Merge pull request #3999 from TeamAmaze/bugfix/3982
Browse files Browse the repository at this point in the history
Fix NetworkOnMainThreadException on HybridFile.getUsableSpace() and HybridFileParcelable.isDirectory()
  • Loading branch information
VishalNehra authored Dec 6, 2023
2 parents 922ecbd + 1153416 commit 4020096
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,11 @@
import android.os.Bundle;
import android.provider.MediaStore;
import android.text.format.Formatter;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.core.util.Pair;

import jcifs.smb.SmbAuthException;
Expand All @@ -85,7 +87,7 @@
import kotlin.collections.CollectionsKt;

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

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

Expand Down Expand Up @@ -203,6 +205,41 @@ protected void onCancelled() {
listener.onAsyncTaskFinished(null);
}

@Override
protected void onProgressUpdate(Throwable... values) {
for (Throwable exception : values) {
if (exception instanceof SmbException) {
if ("/".equals(Uri.parse(path).getPath())) {
new AlertDialog.Builder(context.get())
.setTitle(R.string.error_listfile_smb_title)
.setMessage(
context
.get()
.getString(
R.string.error_listfile_smb_noipcshare,
HybridFile.parseAndFormatUriForDisplay(path)))
.setPositiveButton(
android.R.string.ok,
(dialog, which) -> {
dialog.dismiss();
})
.show();
} else {
Toast.makeText(
context.get(),
context
.get()
.getString(
R.string.error_listfile_smb,
HybridFile.parseAndFormatUriForDisplay(path),
exception.getMessage()),
Toast.LENGTH_LONG)
.show();
}
}
}
}

@Override
protected void onPostExecute(@Nullable Pair<OpenMode, List<LayoutElementParcelable>> list) {
listener.onAsyncTaskFinished(list);
Expand Down Expand Up @@ -615,7 +652,8 @@ private List<LayoutElementParcelable> listSmb(
if (!e.getMessage().toLowerCase().contains("denied")) {
mainFragment.reauthenticateSmb();
}
LOG.warn("failed to load smb list, authentication issue", e);
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
32 changes: 16 additions & 16 deletions app/src/main/java/com/amaze/filemanager/filesystem/HybridFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import java.util.EnumSet;
import java.util.List;
import java.util.Locale;
import java.util.concurrent.Callable;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicReference;

Expand Down Expand Up @@ -591,16 +592,8 @@ public boolean isDirectory() {
switch (mode) {
case SFTP:
case FTP:
return isDirectory(AppConfig.getInstance());
case SMB:
SmbFile smbFile = getSmbFile();
try {
isDirectory = smbFile != null && smbFile.isDirectory();
} catch (SmbException e) {
LOG.warn("failed to get isDirectory for smb file", e);
isDirectory = false;
}
break;
return isDirectory(AppConfig.getInstance());
case ROOT:
isDirectory = NativeOperations.isDirectory(path);
break;
Expand Down Expand Up @@ -779,13 +772,20 @@ public long getUsableSpace() {
long size = 0L;
switch (mode) {
case SMB:
try {
SmbFile smbFile = getSmbFile();
size = smbFile != null ? smbFile.getDiskFreeSpace() : 0L;
} catch (SmbException e) {
size = 0L;
LOG.warn("failed to get usage space for smb file", e);
}
size =
Single.fromCallable(
(Callable<Long>)
() -> {
try {
SmbFile smbFile = getSmbFile();
return smbFile != null ? smbFile.getDiskFreeSpace() : 0L;
} catch (SmbException e) {
LOG.warn("failed to get usage space for smb file", e);
return 0L;
}
})
.subscribeOn(Schedulers.io())
.blockingGet();
break;
case FILE:
case ROOT:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,12 @@ public boolean isDirectory() {
return isDirectory;
}

@Override
public boolean isDirectory(Context context) {
if (isSmb() || isSftp()) return isDirectory;
else return super.isDirectory(context);
}

public boolean isHidden() {
return name.startsWith(".");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,8 @@ public void afterTextChanged(@NonNull Editable s) {
}
SmbFile smbFile;
String domaind = domain.getText().toString();
if (chkSmbAnonymous.isChecked())
if (chkSmbAnonymous.isChecked()
|| (TextUtils.isEmpty(user.getText()) && TextUtils.isEmpty(pass.getText())))
smbFile = createSMBPath(new String[] {ipa, "", "", domaind, sShare}, true, false);
else {
String useraw = user.getText().toString();
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -825,5 +825,8 @@ You only need to do this once, until the next time you select a new location for
<string name="preference_language_title">App Language</string>
<string name="preference_language_dialog_title">Select Language</string>
<string name="preference_language_system_default">System Default</string>
<string name="error.listfile.smb.title">Unable to load SMB file list</string>
<string name="error.listfile.smb">Error listing files at %s: %s</string>
<string name="error.listfile.smb.noipcshare">Error listing files at %s - probably the SMB server does not allow anonymous access to the IPC$ share. You may try specifying the share name directly in connection settings.</string>
</resources>

2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ buildscript {
robolectricVersion = '4.9'
glideVersion = '4.11.0'
sshjVersion = '0.35.0'
jcifsVersion = '2.1.6'
jcifsVersion = '2.1.9'
fabSpeedDialVersion = '3.1.1'
roomVersion = '2.5.0'
bouncyCastleVersion = '1.70'
Expand Down

0 comments on commit 4020096

Please sign in to comment.