summaryrefslogtreecommitdiff
path: root/android/source/src/java/org/libreoffice/LibreOfficeMainActivity.java
diff options
context:
space:
mode:
authorMichael Weghorn <m.weghorn@posteo.de>2021-03-31 10:58:01 +0200
committerMichael Weghorn <m.weghorn@posteo.de>2021-04-01 07:18:07 +0200
commite60c9c62b7df7b15006658546e9a533d1be61175 (patch)
tree196279b7f174e8488b29c595769484f2660bf519 /android/source/src/java/org/libreoffice/LibreOfficeMainActivity.java
parent05b7f6f4e85c44997d3b5603d5a61f64eed2c88c (diff)
android: Use system file picker to create new docs
Similar to the way that existing documents can be opened from within the Android Viewer app using the system file picker by using 'Intent.ACTION_OPEN_DOCUMENT', use the system file picker via 'Intent.ACTION_CREATE_DOCUMENT' to create new docs as well, instead of providing a custom dialog to insert a file name. As described at [1], this allows to save files in locations supported by any existing DocumentsProvider (e.g. a Nextcloud share, if the Nextcloud app is installed and set up), not just locally. This also allows to further unify the handling in LOMainActivity. Just like for the cases where an existing document is opened using the system file picker or a document is passed from a third-party app, the document URI is now set in the Intent, a temporary file is used and writing back to the actual URI happens on save. Drop LibreOfficeMainActivity's method 'showSaveStatusMessage', which was only meant to be used when a new file was created, but was called from the more generic 'LOKitTileProvider::saveDocumentAs'. Change that to show a more general error message when saving fails. Since the actual file is now created by the DocumentsProvider, LOKitTileProvider only operates on the temporary copy anyway. Side note: With this change in place, overwriting existing files also no longer just happens silently, as used to be the case when typing the name of an existing file in the custom dialog for creating new files earlier. Since 'Intent.ACTION_OPEN_DOCUMENT' was introduced in SDK version 19 (Android 4.4), restrict creating new docs to corresponding devices. [1] https://developer.android.com/training/data-storage/shared/documents-files Change-Id: I8932cb892ca8ac97a04d15cbd1540d0ee68350da Reviewed-on: https://gerrit.libreoffice.org/c/core/+/113408 Tested-by: Jenkins Reviewed-by: Michael Weghorn <m.weghorn@posteo.de> (cherry picked from commit 4b8540911bc8fabee0729b31780a1ba8b4663121)
Diffstat (limited to 'android/source/src/java/org/libreoffice/LibreOfficeMainActivity.java')
-rw-r--r--android/source/src/java/org/libreoffice/LibreOfficeMainActivity.java75
1 files changed, 31 insertions, 44 deletions
diff --git a/android/source/src/java/org/libreoffice/LibreOfficeMainActivity.java b/android/source/src/java/org/libreoffice/LibreOfficeMainActivity.java
index 6db06d155322..a070c292009c 100644
--- a/android/source/src/java/org/libreoffice/LibreOfficeMainActivity.java
+++ b/android/source/src/java/org/libreoffice/LibreOfficeMainActivity.java
@@ -58,6 +58,7 @@ import java.nio.channels.FileChannel;
import java.nio.channels.ReadableByteChannel;
import java.util.ArrayList;
import java.util.List;
+import java.util.UUID;
/**
* Main activity of the LibreOffice App. It is started in the UI thread.
@@ -127,7 +128,7 @@ public class LibreOfficeMainActivity extends AppCompatActivity implements Settin
private boolean isSearchToolbarOpen = false;
private static boolean isDocumentChanged = false;
private boolean isUNOCommandsToolbarOpen = false;
- public boolean isNewDocument = false;
+ private boolean isNewDocument = false;
private long lastModified = 0;
@Override
@@ -180,31 +181,35 @@ public class LibreOfficeMainActivity extends AppCompatActivity implements Settin
mbISReadOnlyMode = !isExperimentalMode();
- if (getIntent().getStringExtra(LibreOfficeUIActivity.NEW_DOC_TYPE_KEY) != null) {
- // New document type string is not null, meaning we want to open a new document
- String newDocumentType = getIntent().getStringExtra(LibreOfficeUIActivity.NEW_DOC_TYPE_KEY);
- String newFilePath = getIntent().getStringExtra(LibreOfficeUIActivity.NEW_FILE_PATH_KEY);
-
- // Load the new document
- loadNewDocument(newFilePath, newDocumentType);
- } else if (getIntent().getData() != null) {
+ if (getIntent().getData() != null) {
if (getIntent().getData().getScheme().equals(ContentResolver.SCHEME_CONTENT)) {
- if (copyFileToTemp() && mTempFile != null) {
+ final boolean isReadOnlyDoc;
+ if (getIntent().getStringExtra(LibreOfficeUIActivity.NEW_DOC_TYPE_KEY) != null) {
+ // New document type string is not null, meaning we want to open a new document
+ String newDocumentType = getIntent().getStringExtra(LibreOfficeUIActivity.NEW_DOC_TYPE_KEY);
+ // create a temporary local file, will be copied to the actual URI when saving
+ loadNewDocument(newDocumentType);
mInputFile = mTempFile;
- boolean isReadOnlyDoc = (getIntent().getFlags() & Intent.FLAG_GRANT_WRITE_URI_PERMISSION) == 0;
- mbISReadOnlyMode = !isExperimentalMode() || isReadOnlyDoc;
- Log.d(LOGTAG, "SCHEME_CONTENT: getPath(): " + getIntent().getData().getPath());
-
- String displayName = extractDisplayNameFromIntent();
- if (displayName.isEmpty()) {
- // fall back to using temp file name
- displayName = mInputFile.getName();
- }
- toolbarTop.setTitle(displayName);
+ isReadOnlyDoc = false;
+ } else if (copyFileToTemp() && mTempFile != null) {
+ mInputFile = mTempFile;
+ isReadOnlyDoc = (getIntent().getFlags() & Intent.FLAG_GRANT_WRITE_URI_PERMISSION) == 0;
} else {
// TODO: can't open the file
Log.e(LOGTAG, "couldn't create temporary file from " + getIntent().getData());
+ return;
+ }
+
+ mbISReadOnlyMode = !isExperimentalMode() || isReadOnlyDoc;
+ Log.d(LOGTAG, "SCHEME_CONTENT: getPath(): " + getIntent().getData().getPath());
+
+ String displayName = extractDisplayNameFromIntent();
+ if (displayName.isEmpty()) {
+ // fall back to using temp file name
+ displayName = mInputFile.getName();
}
+ toolbarTop.setTitle(displayName);
+
} else if (getIntent().getData().getScheme().equals(ContentResolver.SCHEME_FILE)) {
mInputFile = new File(getIntent().getData().getPath());
Log.d(LOGTAG, "SCHEME_FILE: getPath(): " + getIntent().getData().getPath());
@@ -280,12 +285,12 @@ public class LibreOfficeMainActivity extends AppCompatActivity implements Settin
}
}
- // Loads a new Document
- private void loadNewDocument(String newFilePath, String newDocumentType) {
- mInputFile = new File(newFilePath);
- LOKitShell.sendNewDocumentLoadEvent(newFilePath, newDocumentType);
+ // Loads a new Document and saves it to a temporary file
+ private void loadNewDocument(String newDocumentType) {
+ String tempFileName = "LibreOffice_" + UUID.randomUUID().toString();
+ mTempFile = new File(this.getCacheDir(), tempFileName);
+ LOKitShell.sendNewDocumentLoadEvent(mTempFile.getPath(), newDocumentType);
isNewDocument = true;
- toolbarTop.setTitle(mInputFile.getName());
}
public RectF getCurrentCursorPosition() {
@@ -376,19 +381,8 @@ public class LibreOfficeMainActivity extends AppCompatActivity implements Settin
if (documentUri != null) {
// case where file was opened using IDocumentProvider from within LO app
saveFilesToCloud();
- } else if (isNewDocument) {
- // nothing to do for actual save, the actual (local) file is already handled
- // by LOKitTileProvider
- runOnUiThread(new Runnable() {
- @Override
- public void run() {
- Toast.makeText(LibreOfficeMainActivity.this, R.string.message_saved,
- Toast.LENGTH_SHORT).show();
- }
- });
- setDocumentChanged(false);
} else {
- // case where file was passed via Intent
+ // case where file URI was passed via Intent
if (isReadOnlyMode() || mInputFile == null || getIntent().getData() == null || !getIntent().getData().getScheme().equals(ContentResolver.SCHEME_CONTENT))
return;
@@ -1035,13 +1029,6 @@ public class LibreOfficeMainActivity extends AppCompatActivity implements Settin
}
}
- // This method is used in LOKitTileProvider.java to show status of new file creation.
- public void showSaveStatusMessage(boolean error) {
- if (!error)
- Snackbar.make(mDrawerLayout, getString(R.string.create_new_file_success) + mInputFile.getName(), Snackbar.LENGTH_LONG).show();
- else
- Snackbar.make(mDrawerLayout, getString(R.string.create_new_file_error) + mInputFile.getName(), Snackbar.LENGTH_LONG).show(); }
-
public void showCustomStatusMessage(String message){
Snackbar.make(mDrawerLayout, message, Snackbar.LENGTH_LONG).show();
}