diff --git a/app/src/main/java/com/money/manager/ex/core/docstorage/FileStorageHelper.java b/app/src/main/java/com/money/manager/ex/core/docstorage/FileStorageHelper.java index 442f62db8..138e2bf07 100644 --- a/app/src/main/java/com/money/manager/ex/core/docstorage/FileStorageHelper.java +++ b/app/src/main/java/com/money/manager/ex/core/docstorage/FileStorageHelper.java @@ -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); diff --git a/app/src/main/java/com/money/manager/ex/sync/SyncManager.java b/app/src/main/java/com/money/manager/ex/sync/SyncManager.java index da681d4a9..75437c450 100644 --- a/app/src/main/java/com/money/manager/ex/sync/SyncManager.java +++ b/app/src/main/java/com/money/manager/ex/sync/SyncManager.java @@ -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 dateTimeUtilsLazy; @@ -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.");