Selaa lähdekoodia

Fix #9842: Run DocumentProvider.openDocument network operations in async task

Signed-off-by: Álvaro Brey Vilas <alvaro.brey@nextcloud.com>
akallabeth 3 vuotta sitten
vanhempi
commit
a8c85a376d

+ 28 - 11
src/main/java/com/owncloud/android/providers/DocumentsStorageProvider.java

@@ -86,6 +86,8 @@ import java.util.List;
 import java.util.Objects;
 import java.util.concurrent.Executor;
 import java.util.concurrent.Executors;
+import java.util.concurrent.Future;
+import java.util.concurrent.FutureTask;
 import java.util.concurrent.TimeUnit;
 
 import javax.inject.Inject;
@@ -208,20 +210,35 @@ public class DocumentsStorageProvider extends DocumentsProvider {
                 // TODO show a conflict notification with a pending intent that shows a ConflictResolveDialog
                 Log_OC.w(TAG, "Conflict found!");
             } else {
-                DownloadFileOperation downloadFileOperation = new DownloadFileOperation(user, ocFile, context);
-                RemoteOperationResult result = downloadFileOperation.execute(document.getClient());
-                if (!result.isSuccess()) {
-                    if (ocFile.isDown()) {
-                        Handler handler = new Handler(Looper.getMainLooper());
-                        handler.post(() -> Toast.makeText(MainApp.getAppContext(),
-                                                          R.string.file_not_synced,
-                                                          Toast.LENGTH_SHORT).show());
+                final boolean[] rval = new boolean[1];
+                Runnable task = () -> {
+                    DownloadFileOperation downloadFileOperation = new DownloadFileOperation(user, ocFile, context);
+                    RemoteOperationResult result = downloadFileOperation.execute(document.getClient());
+                    if (!result.isSuccess()) {
+                        if (ocFile.isDown()) {
+                            Handler handler = new Handler(Looper.getMainLooper());
+                            handler.post(() -> Toast.makeText(MainApp.getAppContext(),
+                                                              R.string.file_not_synced,
+                                                              Toast.LENGTH_SHORT).show());
+                            rval[0] = true;
+                        } else {
+                            Log_OC.e(TAG, result.toString());
+                        }
                     } else {
-                        Log_OC.e(TAG, result.toString());
+                        saveDownloadedFile(document.getStorageManager(), downloadFileOperation, ocFile);
+                        rval[0] = true;
+                    }
+                };
+
+                try {
+                    Future future = new FutureTask(task, null);
+                    AsyncTask.execute(task);
+                    future.wait();
+                    if (!rval[0]) {
                         throw new FileNotFoundException("Error downloading file: " + ocFile.getFileName());
                     }
-                } else {
-                    saveDownloadedFile(document.getStorageManager(), downloadFileOperation, ocFile);
+                } catch (InterruptedException e) {
+                    throw new FileNotFoundException("Error downloading file: " + ocFile.getFileName());
                 }
             }
         }