aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Weghorn <m.weghorn@posteo.de>2021-04-15 16:50:16 +0200
committerMichael Weghorn <m.weghorn@posteo.de>2021-04-16 10:17:51 +0200
commit10d1390980f986afef25f715ce41f02b3c586bf1 (patch)
treef5d8df06f9732a40e9f0922d875c96bbf01297a1
parentb26b05746f8d9ce27347d01e601b61a2a97bbbe7 (diff)
tdf#95615 android: Don't offer "Save" after opening template distro/lhm/libreoffice-7-1+backports
When the input document in Android Viewer is a template, a new doc is created and a plain '.uno:Save' (which 'LibreOfficeMainActivity#saveDocument' triggers when the "Save" menu entry is selected) will therefore fail. A proper URI to save to (rather than overwriting the template itself) is only known after a "Save As" anyway, so don't set the 'mDocument' member until then, which leads to the "Save" menu entry becoming disabled, just as is the case when explicitly choosing to create a new document in the start activity. For now, the check whether the document is a template checks whether the MIME type detected for the URI ends with "template", which is the case for ODF and OOXML types (like "application/vnd.oasis.opendocument.text-template" or "application/vnd.openxmlformats-officedocument.wordprocessingml.template"). This can be refined further as needed, e.g. by explicitly adding more MIME types to check. (Editing the actual template instead of creating a new doc from it would be a different use case that remains unsupported also with this change in place.) Change-Id: I81ff957de27f620a026dbc01097b8061886293a1 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/114157 Tested-by: Jenkins Reviewed-by: Michael Weghorn <m.weghorn@posteo.de> (cherry picked from commit 01521db61eb41447113c4bb671ac828a583c0cd1)
-rw-r--r--android/source/src/java/org/libreoffice/LibreOfficeMainActivity.java45
-rw-r--r--android/source/src/java/org/libreoffice/ui/FileUtilities.java8
2 files changed, 39 insertions, 14 deletions
diff --git a/android/source/src/java/org/libreoffice/LibreOfficeMainActivity.java b/android/source/src/java/org/libreoffice/LibreOfficeMainActivity.java
index 066c05dc9662..f7f5f6ae0ed6 100644
--- a/android/source/src/java/org/libreoffice/LibreOfficeMainActivity.java
+++ b/android/source/src/java/org/libreoffice/LibreOfficeMainActivity.java
@@ -82,7 +82,7 @@ public class LibreOfficeMainActivity extends AppCompatActivity implements Settin
private List<DocumentPartView> mDocumentPartView = new ArrayList<DocumentPartView>();
private DocumentPartViewListAdapter mDocumentPartViewListAdapter;
private DocumentOverlay mDocumentOverlay;
- /** URI of the actual document. */
+ /** URI to save the document to. */
private Uri mDocumentUri;
/** Temporary local copy of the document. */
private File mTempFile = null;
@@ -170,29 +170,38 @@ public class LibreOfficeMainActivity extends AppCompatActivity implements Settin
mbISReadOnlyMode = !isExperimentalMode();
- mDocumentUri = getIntent().getData();
- if (mDocumentUri != null) {
- if (mDocumentUri.getScheme().equals(ContentResolver.SCHEME_CONTENT)
- || mDocumentUri.getScheme().equals(ContentResolver.SCHEME_ANDROID_RESOURCE)) {
+ final Uri docUri = getIntent().getData();
+ if (docUri != null) {
+ if (docUri.getScheme().equals(ContentResolver.SCHEME_CONTENT)
+ || docUri.getScheme().equals(ContentResolver.SCHEME_ANDROID_RESOURCE)) {
final boolean isReadOnlyDoc = (getIntent().getFlags() & Intent.FLAG_GRANT_WRITE_URI_PERMISSION) == 0;
mbISReadOnlyMode = !isExperimentalMode() || isReadOnlyDoc;
- Log.d(LOGTAG, "SCHEME_CONTENT: getPath(): " + mDocumentUri.getPath());
+ Log.d(LOGTAG, "SCHEME_CONTENT: getPath(): " + docUri.getPath());
- String displayName = FileUtilities.retrieveDisplayNameForDocumentUri(getContentResolver(), mDocumentUri);
+ String displayName = FileUtilities.retrieveDisplayNameForDocumentUri(getContentResolver(), docUri);
toolbarTop.setTitle(displayName);
- } else if (mDocumentUri.getScheme().equals(ContentResolver.SCHEME_FILE)) {
+ } else if (docUri.getScheme().equals(ContentResolver.SCHEME_FILE)) {
mbISReadOnlyMode = true;
- Log.d(LOGTAG, "SCHEME_FILE: getPath(): " + mDocumentUri.getPath());
- toolbarTop.setTitle(mDocumentUri.getLastPathSegment());
+ Log.d(LOGTAG, "SCHEME_FILE: getPath(): " + docUri.getPath());
+ toolbarTop.setTitle(docUri.getLastPathSegment());
}
// create a temporary local copy to work with
- boolean copyOK = copyFileToTemp() && mTempFile != null;
+ boolean copyOK = copyFileToTemp(docUri) && mTempFile != null;
if (!copyOK) {
// TODO: can't open the file
- Log.e(LOGTAG, "couldn't create temporary file from " + mDocumentUri);
+ Log.e(LOGTAG, "couldn't create temporary file from " + docUri);
return;
}
+
+ // if input doc is a template, a new doc is created and a proper URI to save to
+ // will only be available after a "Save As"
+ if (isTemplate(docUri)) {
+ toolbarTop.setTitle(R.string.default_document_name);
+ } else {
+ mDocumentUri = docUri;
+ }
+
LOKitShell.sendLoadEvent(mTempFile.getPath());
} else if (getIntent().getStringExtra(LibreOfficeUIActivity.NEW_DOC_TYPE_KEY) != null) {
// New document type string is not null, meaning we want to open a new document
@@ -275,7 +284,7 @@ public class LibreOfficeMainActivity extends AppCompatActivity implements Settin
return mDocumentOverlay.getCurrentCursorPosition();
}
- private boolean copyFileToTemp() {
+ private boolean copyFileToTemp(Uri documentUri) {
// CSV files need a .csv suffix to be opened in Calc.
String suffix = null;
String intentType = getIntent().getType();
@@ -286,7 +295,7 @@ public class LibreOfficeMainActivity extends AppCompatActivity implements Settin
try {
mTempFile = File.createTempFile("LibreOffice", suffix, this.getCacheDir());
final FileOutputStream outputStream = new FileOutputStream(mTempFile);
- return copyUriToStream(mDocumentUri, outputStream);
+ return copyUriToStream(documentUri, outputStream);
} catch (FileNotFoundException e) {
return false;
} catch (IOException e) {
@@ -395,6 +404,14 @@ public class LibreOfficeMainActivity extends AppCompatActivity implements Settin
}
}
+ /**
+ * Returns whether the MIME type for the URI is considered one for a document template.
+ */
+ private boolean isTemplate(final Uri documentUri) {
+ final String mimeType = getContentResolver().getType(documentUri);
+ return FileUtilities.isTemplateMimeType(mimeType);
+ }
+
public void saveFileToOriginalSource() {
if (isReadOnlyMode() || mTempFile == null || mDocumentUri == null || !mDocumentUri.getScheme().equals(ContentResolver.SCHEME_CONTENT))
return;
diff --git a/android/source/src/java/org/libreoffice/ui/FileUtilities.java b/android/source/src/java/org/libreoffice/ui/FileUtilities.java
index 0d51dd55e1e5..aed671205bef 100644
--- a/android/source/src/java/org/libreoffice/ui/FileUtilities.java
+++ b/android/source/src/java/org/libreoffice/ui/FileUtilities.java
@@ -157,6 +157,14 @@ public class FileUtilities {
}
/**
+ * Returns whether the passed MIME type is one for a document template.
+ */
+ public static boolean isTemplateMimeType(final String mimeType) {
+ // this works for ODF and OOXML template MIME types
+ return mimeType.endsWith("template");
+ }
+
+ /**
* Tries to retrieve the display (which should be the document name)
* for the given URI using the given resolver.
*/