Kaydet (Commit) 768dea15 authored tarafından Tomaž Vajngerl's avatar Tomaž Vajngerl Kaydeden (comit) Miklos Vajna

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
üst 24b28151
......@@ -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() {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment