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

Handle NetworkOnMainThreadException for NextCloud Client #1914

Merged
merged 1 commit into from
Nov 24, 2024
Merged
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 @@ -334,16 +334,19 @@ private void downloadDatabase(Uri uri, String localPath) throws Exception {

// Use try-with-resources to automatically close resources
File tempDatabaseFile = File.createTempFile("database", ".db", getContext().getFilesDir());
try (FileOutputStream outputStream = new FileOutputStream(tempDatabaseFile);
InputStream is = resolver.openInputStream(uri)) {

// Copy contents
long bytesCopied = ByteStreams.copy(is, outputStream);
Timber.i("copied %d bytes", bytesCopied);
} catch (Exception e) {
Timber.e(e);
return;
}
Thread thread = new Thread(() -> {
try {
FileOutputStream outputStream = new FileOutputStream(tempDatabaseFile);
InputStream is = resolver.openInputStream(uri);
long bytesCopied = ByteStreams.copy(is, outputStream);
Timber.i("copied %d bytes", bytesCopied);
} catch (Exception e) {
Timber.e(e);
}
});
thread.start();
thread.join();

// Replace local database with downloaded version
File localDatabaseFile = new File(localPath);
Expand Down
28 changes: 20 additions & 8 deletions app/src/main/java/com/money/manager/ex/sync/SyncManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@
*/
public class SyncManager {

public static long scheduledJobId = Constants.NOT_SET;
// public static long scheduledJobId = Constants.NOT_SET;

private boolean isRemoteFileAccessibleExist = false;

@Inject Lazy<MmxDateTimeUtils> dateTimeUtilsLazy;

Expand Down Expand Up @@ -126,16 +128,26 @@ public boolean canSync() {

public boolean isRemoteFileAccessible(boolean showAllert) {
// check if remote file is accessible
boolean exist = false;
isRemoteFileAccessibleExist = false;
String remotePath = getRemotePath();

Thread thread = new Thread(() -> {
try {
InputStream inputStream = getContext().getContentResolver().openInputStream(Uri.parse(remotePath));
inputStream.close();
isRemoteFileAccessibleExist = true;
} catch (Exception e) {
Timber.e(e);
}
});
thread.start();
try {
InputStream inputStream = getContext().getContentResolver().openInputStream(Uri.parse(remotePath));
inputStream.close();
exist = true;
} catch (Exception e) {
Timber.i("Remote Read got error: %s", e.getMessage());
thread.join();
} catch (InterruptedException e) {
isRemoteFileAccessibleExist = false;
}
if (!exist) {

if (!isRemoteFileAccessibleExist) {
if (showAllert) {
Toast.makeText(getContext(), R.string.remote_unavailable, Toast.LENGTH_SHORT).show();
Timber.i("Remote file is no longer available.");
Expand Down
Loading