1 /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
5  * This Source Code Form is subject to the terms of the Mozilla Public
6  * License, v. 2.0. If a copy of the MPL was not distributed with this
7  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8  */
9 
10 package org.libreoffice.kit;
11 
12 import android.app.Activity;
13 import android.content.pm.ApplicationInfo;
14 import android.content.res.AssetManager;
15 import android.util.Log;
16 
17 import java.io.InputStream;
18 import java.nio.ByteBuffer;
19 
20 // Native methods in this class are all implemented in
21 // sal/android/lo-bootstrap.c as the lo-bootstrap library is loaded with
22 // System.loadLibrary() and Android's JNI works only to such libraries, it
23 // seems.
24 public final class LibreOfficeKit
25 {
26     private static String LOGTAG = LibreOfficeKit.class.getSimpleName();
27     private static AssetManager mgr;
28 
29     // private constructor because instantiating would be meaningless
LibreOfficeKit()30     private LibreOfficeKit() {
31     }
32 
33     // Trigger library initialization - as this is done automatically by executing the "static" block, this method remains empty. However we need this to manually (at the right time) can force library initialization.
initializeLibrary()34     public static void initializeLibrary() {
35     }
36 
37     // Trigger initialization on the JNI - LOKit side.
initializeNative(String dataDir, String cacheDir, String apkFile, AssetManager mgr)38     private static native boolean initializeNative(String dataDir, String cacheDir, String apkFile, AssetManager mgr);
39 
getLibreOfficeKitHandle()40     public static native ByteBuffer getLibreOfficeKitHandle();
41 
42     // Wrapper for putenv()
putenv(String string)43     public static native void putenv(String string);
44 
45     // A method that starts a thread to redirect stdout and stderr writes to
46     // the Android logging mechanism, or stops the redirection.
redirectStdio(boolean state)47     public static native void redirectStdio(boolean state);
48 
49     static boolean initializeDone = false;
50 
51     // This init() method should be called from the upper Java level of
52     // LO-based apps.
init(Activity activity)53     public static synchronized void init(Activity activity)
54     {
55         if (initializeDone) {
56             return;
57         }
58 
59         mgr = activity.getResources().getAssets();
60 
61         ApplicationInfo applicationInfo = activity.getApplicationInfo();
62         String dataDir = applicationInfo.dataDir;
63         Log.i(LOGTAG, String.format("Initializing LibreOfficeKit, dataDir=%s\n", dataDir));
64 
65         redirectStdio(true);
66 
67         String cacheDir = activity.getApplication().getCacheDir().getAbsolutePath();
68         String apkFile = activity.getApplication().getPackageResourcePath();
69 
70         if (!initializeNative(dataDir, cacheDir, apkFile, mgr)) {
71             Log.e(LOGTAG, "Initialize native failed!");
72             return;
73         }
74         initializeDone = true;
75     }
76 
77     // Now with static loading we always have all native code in one native
78     // library which we always call liblo-native-code.so, regardless of the
79     // app. The library has already been unpacked into /data/data/<app
80     // name>/lib at installation time by the package manager.
81     static {
NativeLibLoader.load()82         NativeLibLoader.load();
83     }
84 }
85 
86 class NativeLibLoader {
87         private static boolean done = false;
88 
load()89         protected static synchronized void load() {
90             if (done)
91                 return;
92             System.loadLibrary("nspr4");
93             System.loadLibrary("plds4");
94             System.loadLibrary("plc4");
95             System.loadLibrary("nssutil3");
96             System.loadLibrary("freebl3");
97             System.loadLibrary("sqlite3");
98             System.loadLibrary("softokn3");
99             System.loadLibrary("nss3");
100             System.loadLibrary("nssckbi");
101             System.loadLibrary("nssdbm3");
102             System.loadLibrary("smime3");
103             System.loadLibrary("ssl3");
104 
105             System.loadLibrary("lo-native-code");
106             done = true;
107         }
108 }
109 
110 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
111