android/source/src/java/org/libreoffice/LibreOfficeMainActivity.java |   69 
+++++-----
 1 file changed, 38 insertions(+), 31 deletions(-)

New commits:
commit a2b4564d719e6efeb614052b9c833991e447c68c
Author:     Michael Weghorn <m.wegh...@posteo.de>
AuthorDate: Thu Apr 15 08:38:46 2021 +0200
Commit:     Michael Weghorn <m.wegh...@posteo.de>
CommitDate: Fri Apr 16 08:47:38 2021 +0200

    android: Extract copying Uri to stream in thread to separate method
    
    This essentially extracts what
    
        commit 7f838b73e85eb6f0a1dce4647650a5cf5f34ccd2
        Date:   Fri Mar 19 15:46:36 2021 +0100
    
            tdf#129833 android: Move reading file to separate thread
    
    introduced into a separate helper method.
    
    Change-Id: Ic70ba9f2e2bc125415ff1b3fa3375c3389181c43
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/114123
    Tested-by: Jenkins
    Reviewed-by: Michael Weghorn <m.wegh...@posteo.de>

diff --git 
a/android/source/src/java/org/libreoffice/LibreOfficeMainActivity.java 
b/android/source/src/java/org/libreoffice/LibreOfficeMainActivity.java
index a3f62601c1c4..d97719afb726 100644
--- a/android/source/src/java/org/libreoffice/LibreOfficeMainActivity.java
+++ b/android/source/src/java/org/libreoffice/LibreOfficeMainActivity.java
@@ -282,7 +282,6 @@ public class LibreOfficeMainActivity extends 
AppCompatActivity implements Settin
     }
 
     private boolean copyFileToTemp() {
-        final ContentResolver contentResolver = getContentResolver();
         // CSV files need a .csv suffix to be opened in Calc.
         String suffix = null;
         String intentType = getIntent().getType();
@@ -293,36 +292,7 @@ public class LibreOfficeMainActivity extends 
AppCompatActivity implements Settin
         try {
             mTempFile = File.createTempFile("LibreOffice", suffix, 
this.getCacheDir());
             final FileOutputStream outputStream = new 
FileOutputStream(mTempFile);
-            // need to run copy operation in a separate thread, since network 
access is not
-            // allowed from main thread, but that may happen here when 
underlying
-            // DocumentsProvider (like the NextCloud one) does that
-            class CopyThread extends Thread {
-                /** Whether copy operation was successful. */
-                private boolean result = false;
-
-                @Override
-                public void run() {
-                    result = false;
-                    try {
-                        InputStream inputStream = 
contentResolver.openInputStream(mDocumentUri);
-                        result = copyStream(inputStream, outputStream);
-                    } catch (IOException e) {
-                        e.printStackTrace();
-                        return;
-                    }
-                }
-            };
-            CopyThread copyThread = new CopyThread();
-            copyThread.start();
-            try {
-                // wait for copy operation to finish
-                // NOTE: might be useful to add some indicator in UI for long 
copy operations involving network...
-                copyThread.join();
-            } catch (InterruptedException e) {
-                e.printStackTrace();
-            }
-
-            return copyThread.result;
+            return copyUriToStream(mDocumentUri, outputStream);
         } catch (FileNotFoundException e) {
             return false;
         } catch (IOException e) {
@@ -915,6 +885,43 @@ public class LibreOfficeMainActivity extends 
AppCompatActivity implements Settin
         }
     }
 
+    /**
+     * Copies everything from the given Uri to the given OutputStream
+     * and closes the OutputStream in the end.
+     * The copy operation runs in a separate thread, but the method only 
returns
+     * after the thread has finished its execution.
+     * This can be used to copy in a blocking way when network access is 
involved,
+     * which is not allowed from the main thread, but that may happen when an 
underlying
+     * DocumentsProvider (like the NextCloud one) does network access.
+     */
+    private boolean copyUriToStream(final Uri inputUri, final OutputStream 
outputStream) {
+        class CopyThread extends Thread {
+            /** Whether copy operation was successful. */
+            private boolean result = false;
+
+            @Override
+            public void run() {
+                final ContentResolver contentResolver = getContentResolver();
+                try {
+                    InputStream inputStream = 
contentResolver.openInputStream(inputUri);
+                    result = copyStream(inputStream, outputStream);
+                } catch (FileNotFoundException e) {
+                    e.printStackTrace();
+                }
+            }
+        }
+        CopyThread copyThread = new CopyThread();
+        copyThread.start();
+        try {
+            // wait for copy operation to finish
+            // NOTE: might be useful to add some indicator in UI for long copy 
operations involving network...
+            copyThread.join();
+        } catch(InterruptedException e) {
+            e.printStackTrace();
+        }
+        return copyThread.result;
+    }
+
     public void showCustomStatusMessage(String message){
         Snackbar.make(mDrawerLayout, message, Snackbar.LENGTH_LONG).show();
     }
_______________________________________________
Libreoffice-commits mailing list
libreoffice-comm...@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/libreoffice-commits

Reply via email to