From 768dea15b4c5c50367e40af45fc2265478ec154b Mon Sep 17 00:00:00 2001 From: Tomaž Vajngerl <tomaz.vajngerl@collabora.co.uk> Date: Thu, 5 Feb 2015 19:05:05 +0900 Subject: android: copy document to temp file when using content scheme We get the data from Intent, which has data identified by an uri. An uri can use many schemes but we support file (loading directly from a file) or content (used by GMail App). When loading from content, the document is available through a stream and has to be stored into a temporary file locally first, and then that file is should be used as input for loading the document. Change-Id: Ia4ffa8ff02b9737b91a41c03c2eb335d28fe1d61 --- .../org/libreoffice/LibreOfficeMainActivity.java | 54 +++++++++++++++++++++- 1 file changed, 53 insertions(+), 1 deletion(-) (limited to 'android') diff --git a/android/experimental/LOAndroid3/src/java/org/libreoffice/LibreOfficeMainActivity.java b/android/experimental/LOAndroid3/src/java/org/libreoffice/LibreOfficeMainActivity.java index 56e388968e77..17fa86761dc5 100644 --- a/android/experimental/LOAndroid3/src/java/org/libreoffice/LibreOfficeMainActivity.java +++ b/android/experimental/LOAndroid3/src/java/org/libreoffice/LibreOfficeMainActivity.java @@ -2,6 +2,7 @@ package org.libreoffice; import android.app.Activity; import android.app.AlertDialog; +import android.content.ContentResolver; import android.content.Context; import android.content.DialogInterface; import android.os.Bundle; @@ -26,6 +27,13 @@ import org.mozilla.gecko.ZoomConstraints; import org.mozilla.gecko.gfx.GeckoLayerClient; import org.mozilla.gecko.gfx.LayerView; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; import java.util.ArrayList; import java.util.List; @@ -49,6 +57,7 @@ public class LibreOfficeMainActivity extends LOAbout { private String mInputFile; private TextSelection mTextSelection; private TextCursorLayer mTextCursorLayer; + private File mTempFile = null; public LibreOfficeMainActivity() { super(/*newActivity=*/false); @@ -98,7 +107,15 @@ public class LibreOfficeMainActivity extends LOAbout { mMainHandler = new Handler(); if (getIntent().getData() != null) { - mInputFile = getIntent().getData().getPath(); + if (getIntent().getData().getScheme().equals(ContentResolver.SCHEME_CONTENT)) { + if (copyFileToTemp() && mTempFile != null) { + mInputFile = mTempFile.getPath(); + } else { + // TODO: can't open the file + } + } else if (getIntent().getData().getScheme().equals(ContentResolver.SCHEME_FILE)) { + mInputFile = getIntent().getData().getPath(); + } } else { mInputFile = DEFAULT_DOC_PATH; } @@ -137,6 +154,35 @@ public class LibreOfficeMainActivity extends LOAbout { mLayerClient.notifyReady(); } + private boolean copyFileToTemp() { + ContentResolver contentResolver = getContentResolver(); + InputStream inputStream = null; + try { + inputStream = contentResolver.openInputStream(getIntent().getData()); + mTempFile = File.createTempFile("LibreOffice", null, this.getCacheDir()); + + OutputStream outputStream = new FileOutputStream(mTempFile); + byte[] buffer = new byte[4096]; + int len = 0; + while ((len = inputStream.read(buffer)) != -1) { + outputStream.write(buffer, 0, len); + } + inputStream.close(); + outputStream.close(); + return true; + } catch (FileNotFoundException e) { + } catch (IOException e) { + } finally { + if (inputStream != null) { + try { + inputStream.close(); + } catch (IOException e) { + } + } + } + return false; + } + @Override protected void onResume() { super.onResume(); @@ -168,6 +214,12 @@ public class LibreOfficeMainActivity extends LOAbout { Log.i(LOGTAG, "onDestroy.."); mLayerClient.destroy(); super.onDestroy(); + + if (isFinishing()) { // Not an orientation change + if (mTempFile != null) { + mTempFile.delete(); + } + } } public LOKitThread getLOKitThread() { -- cgit