[Libreoffice-commits] online.git: android/app loleaflet/src

kaishu-sahu (via logerrit) logerrit at kemper.freedesktop.org
Fri Jun 14 18:36:48 UTC 2019


 android/app/src/main/cpp/androidapp.cpp                                |   18 +
 android/app/src/main/java/org/libreoffice/androidapp/MainActivity.java |   37 +++
 android/app/src/main/java/org/libreoffice/androidapp/PrintAdapter.java |   96 ++++++++++
 loleaflet/src/control/Toolbar.js                                       |    2 
 4 files changed, 141 insertions(+), 12 deletions(-)

New commits:
commit 3209a8c36865e82b10bc87552ef079ad3c5f10aa
Author:     kaishu-sahu <kaishusahu101 at gmail.com>
AuthorDate: Fri May 31 18:16:44 2019 +0530
Commit:     Jan Holesovsky <kendy at collabora.com>
CommitDate: Fri Jun 14 20:35:50 2019 +0200

    android: add print feature to the document viewer.
    
    Change-Id: I9de03347bde73faac1e9285f775e9584bb48a08d
    Reviewed-on: https://gerrit.libreoffice.org/73268
    Reviewed-by: Jan Holesovsky <kendy at collabora.com>
    Tested-by: Jan Holesovsky <kendy at collabora.com>

diff --git a/android/app/src/main/cpp/androidapp.cpp b/android/app/src/main/cpp/androidapp.cpp
index 26828e6e1..e66a97b84 100644
--- a/android/app/src/main/cpp/androidapp.cpp
+++ b/android/app/src/main/cpp/androidapp.cpp
@@ -219,14 +219,6 @@ Java_org_libreoffice_androidapp_MainActivity_postMobileMessageNative(JNIEnv *env
 
             // ???
         }
-        else if (strcmp(string_value, "PRINT") == 0 && false /* FIXME disabled so far */)
-        {
-            // TODO this is just a sketch to check this compiles
-            std::string printFile = Util::createRandomTmpDir() + "/print.pdf";
-            getLOKDocument()->saveAs(printFile.c_str(), "pdf", nullptr);
-
-            // TODO more stuff here...
-        }
         else
         {
             // As above
@@ -283,4 +275,14 @@ Java_org_libreoffice_androidapp_MainActivity_createLOOLWSD(JNIEnv *env, jobject,
     fakeClientFd = fakeSocketSocket();
 }
 
+extern "C"
+JNIEXPORT void JNICALL
+Java_org_libreoffice_androidapp_MainActivity_saveAs(JNIEnv *env, jobject instance,
+                                                    jstring fileUri_) {
+    const char *fileUri = env->GetStringUTFChars(fileUri_, 0);
+
+    getLOKDocument()->saveAs(fileUri, "pdf", nullptr);
+
+    env->ReleaseStringUTFChars(fileUri_, fileUri);
+}
 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */
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 586a64ee5..a7e0ae366 100644
--- a/android/app/src/main/java/org/libreoffice/androidapp/MainActivity.java
+++ b/android/app/src/main/java/org/libreoffice/androidapp/MainActivity.java
@@ -20,14 +20,16 @@ import android.content.res.AssetManager;
 import android.net.Uri;
 import android.os.Build;
 import android.os.Bundle;
+import android.os.Handler;
 import android.preference.PreferenceManager;
+import android.print.PrintAttributes;
+import android.print.PrintDocumentAdapter;
+import android.print.PrintManager;
 import android.util.Log;
-import android.view.View;
 import android.webkit.JavascriptInterface;
 import android.webkit.WebSettings;
 import android.webkit.WebView;
 import android.webkit.WebViewClient;
-import android.widget.Button;
 import android.widget.Toast;
 
 import java.io.File;
@@ -69,6 +71,7 @@ public class MainActivity extends AppCompatActivity {
     private String urlToLoad;
     private WebView mWebView;
     private SharedPreferences sPrefs;
+    private Handler mainHandler;
 
     private boolean isDocEditable = false;
     private boolean isDocDebuggable = BuildConfig.DEBUG;
@@ -218,6 +221,7 @@ public class MainActivity extends AppCompatActivity {
                 WebView.setWebContentsDebuggingEnabled(true);
             }
         }
+        mainHandler = new Handler(getMainLooper());
     }
 
 
@@ -349,7 +353,9 @@ public class MainActivity extends AppCompatActivity {
     public void postMobileMessage(String message) {
         Log.d(TAG, "postMobileMessage: " + message);
 
-        postMobileMessageNative(message);
+        if (interceptMsgFromWebView(message)) {
+            postMobileMessageNative(message);
+        }
 
         // Going back to document browser on BYE (called when pressing the top left exit button)
         if (message.equals("BYE"))
@@ -389,6 +395,31 @@ public class MainActivity extends AppCompatActivity {
             }
         });
     }
+
+    /**
+     * return true to pass the message to the native part and false to block the message
+     */
+    boolean interceptMsgFromWebView(String message) {
+        if (message.equals("PRINT")) {
+            mainHandler.post(new Runnable() {
+                @Override
+                public void run() {
+                    initiatePrint();
+                }
+            });
+            return false;
+        }
+        return true;
+    }
+
+    private void initiatePrint() {
+        PrintManager printManager = (PrintManager) getSystemService(PRINT_SERVICE);
+        PrintDocumentAdapter printAdapter = new PrintAdapter(MainActivity.this);
+        printManager.print("Document", printAdapter, new PrintAttributes.Builder().build());
+    }
+
+    public native void saveAs(String fileUri);
+
 }
 
 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */
diff --git a/android/app/src/main/java/org/libreoffice/androidapp/PrintAdapter.java b/android/app/src/main/java/org/libreoffice/androidapp/PrintAdapter.java
new file mode 100644
index 000000000..e4c5bbbb5
--- /dev/null
+++ b/android/app/src/main/java/org/libreoffice/androidapp/PrintAdapter.java
@@ -0,0 +1,96 @@
+/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/*
+ * This file is part of the LibreOffice project.
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ */
+
+package org.libreoffice.androidapp;
+
+import android.os.Bundle;
+import android.os.CancellationSignal;
+import android.os.ParcelFileDescriptor;
+import android.print.PageRange;
+import android.print.PrintAttributes;
+import android.print.PrintDocumentAdapter;
+import android.print.PrintDocumentInfo;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.util.Objects;
+
+public class PrintAdapter extends PrintDocumentAdapter {
+
+    private File printDocFile;
+    private MainActivity mainActivity;
+
+    PrintAdapter(MainActivity mainActivity) {
+        this.mainActivity = mainActivity;
+    }
+
+    @Override
+    public void onStart() {
+        super.onStart();
+        //Will show its own progress bar for the below task
+        printDocFile = new File(mainActivity.getCacheDir(), "print.pdf");
+        mainActivity.saveAs(printDocFile.toURI().toString());
+    }
+
+    @Override
+    public void onLayout(PrintAttributes oldAttributes, PrintAttributes newAttributes,
+                         CancellationSignal cancellationSignal, LayoutResultCallback callback,
+                         Bundle extras) {
+        if (cancellationSignal.isCanceled()) {
+            callback.onLayoutCancelled();
+        } else {
+            PrintDocumentInfo.Builder builder =
+                    new PrintDocumentInfo.Builder("finalPrint.pdf");
+            builder.setContentType(PrintDocumentInfo.CONTENT_TYPE_DOCUMENT)
+                    .setPageCount(PrintDocumentInfo.PAGE_COUNT_UNKNOWN)
+                    .build();
+            callback.onLayoutFinished(builder.build(),
+                    !newAttributes.equals(oldAttributes));
+        }
+    }
+
+    @Override
+    public void onWrite(PageRange[] pages, ParcelFileDescriptor destination, CancellationSignal cancellationSignal,
+                        WriteResultCallback callback) {
+        InputStream in = null;
+        OutputStream out = null;
+        try {
+            in = new FileInputStream(printDocFile);
+            out = new FileOutputStream(destination.getFileDescriptor());
+
+            byte[] buf = new byte[16384];
+            int size;
+
+            while ((size = in.read(buf)) >= 0
+                    && !cancellationSignal.isCanceled()) {
+                out.write(buf, 0, size);
+            }
+
+            if (cancellationSignal.isCanceled()) {
+                callback.onWriteCancelled();
+            } else {
+                callback.onWriteFinished(new PageRange[]{PageRange.ALL_PAGES});
+            }
+        } catch (IOException e) {
+            e.printStackTrace();
+        } finally {
+            try {
+                Objects.requireNonNull(in).close();
+                Objects.requireNonNull(out).close();
+            } catch (IOException | NullPointerException e) {
+                e.printStackTrace();
+            }
+        }
+    }
+}
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/loleaflet/src/control/Toolbar.js b/loleaflet/src/control/Toolbar.js
index f75c0ae31..06563be19 100644
--- a/loleaflet/src/control/Toolbar.js
+++ b/loleaflet/src/control/Toolbar.js
@@ -81,7 +81,7 @@ L.Map.include({
 	},
 
 	print: function () {
-		if (window.ThisIsTheiOSApp) {
+		if (window.ThisIsTheiOSApp || window.ThisIsTheAndroidApp) {
 			window.postMobileMessage('PRINT');
 		} else {
 			this.showBusy(_('Downloading...'), false);


More information about the Libreoffice-commits mailing list