From 10d1390980f986afef25f715ce41f02b3c586bf1 Mon Sep 17 00:00:00 2001 From: Michael Weghorn Date: Thu, 15 Apr 2021 16:50:16 +0200 Subject: tdf#95615 android: Don't offer "Save" after opening template 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 (cherry picked from commit 01521db61eb41447113c4bb671ac828a583c0cd1) --- .../org/libreoffice/LibreOfficeMainActivity.java | 45 +++++++++++++++------- .../src/java/org/libreoffice/ui/FileUtilities.java | 8 ++++ 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 mDocumentPartView = new ArrayList(); 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 @@ -156,6 +156,14 @@ public class FileUtilities { return isHidden(file) && file.getName().endsWith(".png"); } + /** + * 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. -- cgit