diff options
author | kaishu-sahu <kaishusahu101@gmail.com> | 2019-04-01 04:53:41 +0530 |
---|---|---|
committer | Jan Holesovsky <kendy@collabora.com> | 2019-04-10 18:00:20 +0200 |
commit | c74a47bc7eeb58b51175f2e711d202b006a3ff96 (patch) | |
tree | 030523b6c4d889e30858d41a3114ba11ea72635f /android | |
parent | 75949f6fd2638458075e3b34f31d43815368c19a (diff) |
android: add view document from other file-managers functionality.
Change-Id: I2325110a18c0acd93ec235e1a2e0dcc55affa6f5
Reviewed-on: https://gerrit.libreoffice.org/70025
Reviewed-by: Jan Holesovsky <kendy@collabora.com>
Tested-by: Jan Holesovsky <kendy@collabora.com>
Diffstat (limited to 'android')
4 files changed, 133 insertions, 22 deletions
diff --git a/android/app/src/main/AndroidManifest.xml b/android/app/src/main/AndroidManifest.xml index 044055d01..d13a27cd7 100644 --- a/android/app/src/main/AndroidManifest.xml +++ b/android/app/src/main/AndroidManifest.xml @@ -101,11 +101,12 @@ <data android:mimeType="image/x-wmf" /> <data android:mimeType="image/svg+xml" /> </intent-filter> - </activity> <!-- Document Provider Settings Activity --> - <activity - android:name=".storage.DocumentProviderSettingsActivity" - android:label="@string/storage_provider_settings" - android:theme="@style/Theme.AppCompat.Light.DarkActionBar"> + </activity> + + <!-- Document Provider Settings Activity --> + <activity android:name=".storage.DocumentProviderSettingsActivity" + android:theme="@style/Theme.AppCompat.Light.DarkActionBar" + android:label="@string/storage_provider_settings"> <intent-filter> <action android:name="android.intent.action.MAIN" /> </intent-filter> diff --git a/android/app/src/main/java/org/libreoffice/androidapp/MainActivity.java b/android/app/src/main/java/org/libreoffice/androidapp/MainActivity.java index de45ef978..fd1431939 100644 --- a/android/app/src/main/java/org/libreoffice/androidapp/MainActivity.java +++ b/android/app/src/main/java/org/libreoffice/androidapp/MainActivity.java @@ -9,8 +9,10 @@ package org.libreoffice.androidapp; +import android.content.ContentResolver; import android.content.SharedPreferences; import android.content.pm.ApplicationInfo; +import android.content.res.AssetFileDescriptor; import android.content.res.AssetManager; import android.os.Build; import android.os.Bundle; @@ -22,24 +24,39 @@ import android.webkit.WebSettings; import android.webkit.WebView; import android.webkit.WebViewClient; import android.widget.Button; +import android.widget.Toast; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; +import java.net.URI; import java.nio.ByteBuffer; import java.nio.channels.Channels; import java.nio.channels.FileChannel; import java.nio.channels.ReadableByteChannel; +import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; +import androidx.core.app.ActivityCompat; +import androidx.core.content.ContextCompat; public class MainActivity extends AppCompatActivity { final static String TAG = "MainActivity"; private static final String ASSETS_EXTRACTED_PREFS_KEY = "ASSETS_EXTRACTED"; + private static final int PERMISSION_WRITE_EXTERNAL_STORAGE = 777; - WebView mWebView; + private File mTempFile = null; + + private int providerId; + private URI documentUri; + + private String urlToLoad; + private WebView mWebView; + + private boolean isDocEditable = false; + private boolean isDocDebuggable = true; private static boolean copyFromAssets(AssetManager assetManager, String fromAssetPath, String targetDir) { @@ -49,7 +66,7 @@ public class MainActivity extends AppCompatActivity { boolean res = true; for (String file : files) { String[] dirOrFile = assetManager.list(fromAssetPath + "/" + file); - if ( dirOrFile.length == 0) { + if (dirOrFile.length == 0) { // noinspection ResultOfMethodCallIgnored new File(targetDir).mkdirs(); res &= copyAsset(assetManager, @@ -127,8 +144,29 @@ public class MainActivity extends AppCompatActivity { String cacheDir = getApplication().getCacheDir().getAbsolutePath(); String apkFile = getApplication().getPackageResourcePath(); -// String urlToLoad = "file://" + dataDir + "/hello-world.odt"; - String urlToLoad=getIntent().getStringExtra("URI"); + if (getIntent().getData() != null) { + + if (getIntent().getData().getScheme().equals(ContentResolver.SCHEME_CONTENT)) { + isDocEditable = false; + Toast.makeText(this, getResources().getString(R.string.temp_file_saving_disabled), Toast.LENGTH_SHORT).show(); + if (copyFileToTemp() && mTempFile != null) { + urlToLoad = mTempFile.toURI().toString(); + Log.d(TAG, "SCHEME_CONTENT: getPath(): " + getIntent().getData().getPath()); + } else { + // TODO: can't open the file + Log.e(TAG, "couldn't create temporary file from " + getIntent().getData()); + } + } else if (getIntent().getData().getScheme().equals(ContentResolver.SCHEME_FILE)) { + isDocEditable = true; + urlToLoad = getIntent().getData().getPath(); + Log.d(TAG, "SCHEME_FILE: getPath(): " + getIntent().getData().getPath()); + // Gather data to rebuild IFile object later + providerId = getIntent().getIntExtra( + "org.libreoffice.document_provider_id", 0); + documentUri = (URI) getIntent().getSerializableExtra( + "org.libreoffice.document_uri"); + } + } createLOOLWSD(dataDir, cacheDir, apkFile, assetManager, urlToLoad); @@ -147,11 +185,48 @@ public class MainActivity extends AppCompatActivity { } } - mWebView.loadUrl("file:///android_asset/dist/loleaflet.html?file_path=" + - urlToLoad + - "&closebutton=1&permission=edit" + - "&debug=true"); // TODO remove later? + loadDocument(); + } + + private boolean copyFileToTemp() { + ContentResolver contentResolver = getContentResolver(); + FileChannel inputChannel = null; + FileChannel outputChannel = null; + // CSV files need a .csv suffix to be opened in Calc. + String suffix = null; + String intentType = getIntent().getType(); + // K-9 mail uses the first, GMail uses the second variant. + if ("text/comma-separated-values".equals(intentType) || "text/csv".equals(intentType)) + suffix = ".csv"; + + try { + try { + AssetFileDescriptor assetFD = contentResolver.openAssetFileDescriptor(getIntent().getData(), "r"); + if (assetFD == null) { + Log.e(TAG, "couldn't create assetfiledescriptor from " + getIntent().getDataString()); + return false; + } + inputChannel = assetFD.createInputStream().getChannel(); + mTempFile = File.createTempFile("LibreOffice", suffix, this.getCacheDir()); + + outputChannel = new FileOutputStream(mTempFile).getChannel(); + long bytesTransferred = 0; + // might not copy all at once, so make sure everything gets copied.... + while (bytesTransferred < inputChannel.size()) { + bytesTransferred += outputChannel.transferFrom(inputChannel, bytesTransferred, inputChannel.size()); + } + Log.e(TAG, "Success copying " + bytesTransferred + " bytes"); + return true; + } finally { + if (inputChannel != null) inputChannel.close(); + if (outputChannel != null) outputChannel.close(); + } + } catch (FileNotFoundException e) { + return false; + } catch (IOException e) { + return false; } + } @Override protected void onResume() { @@ -162,37 +237,61 @@ public class MainActivity extends AppCompatActivity { updatePreferences(); } + private void loadDocument() { + String finalUrlToLoad = "file:///android_asset/dist/loleaflet.html?file_path=" + + urlToLoad+"&closebutton=1"; + if (isDocEditable) { + finalUrlToLoad += "&permission=edit"; + } else { + finalUrlToLoad += "&permission=readonly"; + } + if (isDocDebuggable) { + finalUrlToLoad += "&debug=true"; + } + mWebView.loadUrl(finalUrlToLoad); + } + static { System.loadLibrary("androidapp"); } - /** Initialize the LOOLWSD to load 'loadFileURL'. */ + /** + * Initialize the LOOLWSD to load 'loadFileURL'. + */ public native void createLOOLWSD(String dataDir, String cacheDir, String apkFile, AssetManager assetManager, String loadFileURL); - /** Passing messages from JS (instead of the websocket communication). */ + /** + * Passing messages from JS (instead of the websocket communication). + */ @JavascriptInterface public native void postMobileMessage(String message); - /** Passing messages from JS (instead of the websocket communication). */ + /** + * Passing messages from JS (instead of the websocket communication). + */ @JavascriptInterface public void postMobileError(String message) { // TODO handle this Log.d(TAG, "postMobileError: " + message); } - /** Passing messages from JS (instead of the websocket communication). */ + /** + * Passing messages from JS (instead of the websocket communication). + */ @JavascriptInterface public void postMobileDebug(String message) { // TODO handle this Log.d(TAG, "postMobileDebug: " + message); } - /** Passing message the other way around - from Java to the FakeWebSocket in JS. */ + /** + * Passing message the other way around - from Java to the FakeWebSocket in JS. + */ void callFakeWebsocketOnMessage(final String message) { // call from the UI thread mWebView.post(new Runnable() { public void run() { - Log.i(TAG,"Forwarding to the WebView: " + message); + Log.i(TAG, "Forwarding to the WebView: " + message); mWebView.loadUrl("javascript:window.TheFakeWebSocket.onmessage({'data':" + message + "});"); } }); diff --git a/android/app/src/main/java/org/libreoffice/androidapp/ui/LibreOfficeUIActivity.java b/android/app/src/main/java/org/libreoffice/androidapp/ui/LibreOfficeUIActivity.java index 8f003b6c0..1e7bb63b2 100644 --- a/android/app/src/main/java/org/libreoffice/androidapp/ui/LibreOfficeUIActivity.java +++ b/android/app/src/main/java/org/libreoffice/androidapp/ui/LibreOfficeUIActivity.java @@ -580,9 +580,19 @@ public class LibreOfficeUIActivity extends AppCompatActivity implements /*Settin @Override protected void onPostExecute(File file) { if (file != null) { - Intent intent = new Intent(getBaseContext(), MainActivity.class); - intent.putExtra("URI", document.getUri().toString()); - startActivity(intent); + Intent i = new Intent(Intent.ACTION_VIEW, Uri.fromFile(file)); + String packageName = getApplicationContext().getPackageName(); + ComponentName componentName = new ComponentName(packageName, + MainActivity.class.getName()); + i.setComponent(componentName); + + // these extras allow to rebuild the IFile object in LOMainActivity + i.putExtra("org.libreoffice.document_provider_id", + documentProvider.getId()); + i.putExtra("org.libreoffice.document_uri", + document.getUri()); + + startActivity(i); } } }.execute(document); diff --git a/android/app/src/main/res/values/strings.xml b/android/app/src/main/res/values/strings.xml index 69f49d340..5b0c83ea9 100644 --- a/android/app/src/main/res/values/strings.xml +++ b/android/app/src/main/res/values/strings.xml @@ -7,6 +7,7 @@ <string name="app_vendor">This release was supplied by $VENDOR.</string> <string name="no_recent_items">No recent items</string> <string name="no_items">No items</string> + <string name="temp_file_saving_disabled">This file is read-only, saving is disabled.</string> <string name="about_license">Show License</string> <string name="about_notice">Show Notice</string> |