Skip to content

Commit

Permalink
Merge pull request #1914 from wolfsolver/nextcloud
Browse files Browse the repository at this point in the history
Handle NetworkOnMainThreadException for NextCloud Client
  • Loading branch information
guanlisheng authored Nov 24, 2024
2 parents 345b604 + 1ee9226 commit abe3ede
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 17 deletions.
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

0 comments on commit abe3ede

Please sign in to comment.