diff options
author | Mert Tumer <merttumer@outlook.com> | 2018-08-03 06:55:42 -0700 |
---|---|---|
committer | Tomaž Vajngerl <quikee@gmail.com> | 2018-08-07 14:29:02 +0200 |
commit | 2fa47f8c0f9e4a7a61f29b4bb8acaf10c861fda1 (patch) | |
tree | 64bab084cb4156f4e1edb10db8824e0288732c72 /android | |
parent | e4c9d68b23a9a58ee040bf748bb3d492350b0ee0 (diff) |
tdf#106648 save current work when switching to other apps
Change-Id: Ieecfad2d0127fec850f7a4f3576a77eec94926b3
Signed-off-by: Mert Tumer <merttumer@outlook.com>
Reviewed-on: https://gerrit.libreoffice.org/58562
Tested-by: Jenkins
Reviewed-by: Tomaž Vajngerl <quikee@gmail.com>
Diffstat (limited to 'android')
3 files changed, 67 insertions, 4 deletions
diff --git a/android/source/src/java/org/libreoffice/LOKitTileProvider.java b/android/source/src/java/org/libreoffice/LOKitTileProvider.java index 3672cdeb0bc2..3fcaf6ce0939 100644 --- a/android/source/src/java/org/libreoffice/LOKitTileProvider.java +++ b/android/source/src/java/org/libreoffice/LOKitTileProvider.java @@ -20,6 +20,7 @@ import org.libreoffice.kit.DirectBufferAllocator; import org.libreoffice.kit.Document; import org.libreoffice.kit.LibreOfficeKit; import org.libreoffice.kit.Office; +import org.libreoffice.ui.FileUtilities; import org.mozilla.gecko.gfx.BufferedCairoImage; import org.mozilla.gecko.gfx.CairoImage; import org.mozilla.gecko.gfx.IntSize; @@ -65,17 +66,33 @@ class LOKitTileProvider implements TileProvider { mOffice.setMessageCallback(messageCallback); mOffice.setOptionalFeatures(Document.LOK_FEATURE_DOCUMENT_PASSWORD); mContext.setTileProvider(this); - mInputFile = input; + File f = new File(mInputFile); + final String cacheFile = mContext.getExternalCacheDir().getAbsolutePath() + "/lo_cached_" + f.getName(); + + if(mContext.firstStart){ + File cacheFileObj = new File(cacheFile); + if(cacheFileObj.exists()) { + cacheFileObj.delete(); + } + mContext.firstStart=false; + } Log.i(LOGTAG, "====> Loading file '" + input + "'"); - File fileToBeEncoded = new File(input); + File fileToBeEncoded; + if(isDocumentCached()){ + fileToBeEncoded = new File(cacheFile); + }else{ + fileToBeEncoded = new File(input); + + } String encodedFileName = android.net.Uri.encode(fileToBeEncoded.getName()); mDocument = mOffice.documentLoad( (new File(fileToBeEncoded.getParent(),encodedFileName)).getPath() ); + if (mDocument == null && !mContext.isPasswordProtected()) { Log.i(LOGTAG, "====> mOffice.documentLoad() returned null, trying to restart 'Office' and loading again"); mOffice.destroy(); @@ -325,6 +342,43 @@ class LOKitTileProvider implements TileProvider { LOKitShell.hideProgressSpinner(mContext); } + public boolean isDocumentCached(){ + File input = new File(mInputFile); + final String cacheFile = mContext.getExternalCacheDir().getAbsolutePath() + "/lo_cached_" + input.getName(); + File cacheFileObj = new File(cacheFile); + if(cacheFileObj.exists()) + return true; + + return false; + } + + public void cacheDocument() { + String cacheDir = mContext.getExternalCacheDir().getAbsolutePath(); + File input = new File(mInputFile); + final String cacheFile = cacheDir + "/lo_cached_" + input.getName(); + Log.i(LOGTAG, "cacheDocument: " + cacheFile); + if(isDocumentCached()){ + LOKitShell.sendEvent(new LOEvent(LOEvent.UNO_COMMAND, ".uno:Save")); + }else{ + mDocument.saveAs("file://"+cacheFile, FileUtilities.getExtension(input.getPath()).substring(1),""); + } + } + + public void saveDocument(){ + if(isDocumentCached()){ + String format = FileUtilities.getExtension(mInputFile).substring(1); + String cacheDir = mContext.getExternalCacheDir().getAbsolutePath(); + File input = new File(mInputFile); + final String cacheFile = cacheDir + "/lo_cached_" + input.getName(); + String path = input.getAbsolutePath(); + saveDocumentAs(path, format); + (new File(cacheFile)).delete(); + }else{ + mContext.saveDocument(); + } + } + + private void setupDocumentFonts() { String values = mDocument.getCommandValues(".uno:CharFontName"); if (values == null || values.isEmpty()) diff --git a/android/source/src/java/org/libreoffice/LibreOfficeMainActivity.java b/android/source/src/java/org/libreoffice/LibreOfficeMainActivity.java index f92c8bcf977d..544fab1b6f50 100644 --- a/android/source/src/java/org/libreoffice/LibreOfficeMainActivity.java +++ b/android/source/src/java/org/libreoffice/LibreOfficeMainActivity.java @@ -89,6 +89,7 @@ public class LibreOfficeMainActivity extends AppCompatActivity implements Settin private File mTempFile = null; private File mTempSlideShowFile = null; private String newDocumentType = null; + public boolean firstStart = true; BottomSheetBehavior bottomToolbarSheetBehavior; BottomSheetBehavior toolbarColorPickerBottomSheetBehavior; @@ -460,7 +461,7 @@ public class LibreOfficeMainActivity extends AppCompatActivity implements Settin if (isNewDocument) { saveAs(); } else { - saveDocument(); + mTileProvider.saveDocument(); } isDocumentChanged=false; onBackPressed(); @@ -961,6 +962,14 @@ public class LibreOfficeMainActivity extends AppCompatActivity implements Settin mFormattingController.handleActivityResult(requestCode, resultCode, data); hideBottomToolbar(); } + + @Override + protected void onSaveInstanceState(Bundle outState) { + super.onSaveInstanceState(outState); + //save document to cache + mTileProvider.cacheDocument(); + } + } /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/android/source/src/java/org/libreoffice/ToolbarController.java b/android/source/src/java/org/libreoffice/ToolbarController.java index d34a3447ec1c..ffd0afddc9cd 100644 --- a/android/source/src/java/org/libreoffice/ToolbarController.java +++ b/android/source/src/java/org/libreoffice/ToolbarController.java @@ -165,7 +165,7 @@ public class ToolbarController implements Toolbar.OnMenuItemClickListener { if (mContext.isNewDocument) { mContext.saveAs(); } else { - mContext.saveDocument(); + mContext.getTileProvider().saveDocument(); } return true; case R.id.action_parts: |