diff options
author | Michael Weghorn <m.weghorn@posteo.de> | 2021-04-15 08:38:46 +0200 |
---|---|---|
committer | Michael Weghorn <m.weghorn@posteo.de> | 2021-04-16 08:47:38 +0200 |
commit | a2b4564d719e6efeb614052b9c833991e447c68c (patch) | |
tree | 6633748784cd13ee7173f4360b6392a7851725fa /android/source/src | |
parent | deb892628a1501527c8c41b85a65282df95b81b1 (diff) |
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.weghorn@posteo.de>
Diffstat (limited to 'android/source/src')
-rw-r--r-- | android/source/src/java/org/libreoffice/LibreOfficeMainActivity.java | 69 |
1 files changed, 38 insertions, 31 deletions
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(); } |