github.com/cybriq/giocore@v0.0.7-0.20210703034601-cfb9cb5f3900/app/internal/wm/Gio.java (about)

     1  // SPDX-License-Identifier: Unlicense OR MIT
     2  
     3  package org.gioui;
     4  
     5  import android.content.ClipboardManager;
     6  import android.content.ClipData;
     7  import android.content.Context;
     8  import android.os.Handler;
     9  import android.os.Looper;
    10  
    11  import java.io.UnsupportedEncodingException;
    12  
    13  public final class Gio {
    14  	private static final Object initLock = new Object();
    15  	private static boolean jniLoaded;
    16  	private static final Handler handler = new Handler(Looper.getMainLooper());
    17  
    18  	/**
    19  	 * init loads and initializes the Go native library and runs
    20  	 * the Go main function.
    21  	 *
    22  	 * It is exported for use by Android apps that need to run Go code
    23  	 * outside the lifecycle of the Gio activity.
    24  	 */
    25  	public static synchronized void init(Context appCtx) {
    26  		synchronized (initLock) {
    27  			if (jniLoaded) {
    28  				return;
    29  			}
    30  			String dataDir = appCtx.getFilesDir().getAbsolutePath();
    31  			byte[] dataDirUTF8;
    32  			try {
    33  				dataDirUTF8 = dataDir.getBytes("UTF-8");
    34  			} catch (UnsupportedEncodingException e) {
    35  				throw new RuntimeException(e);
    36  			}
    37  			System.loadLibrary("gio");
    38  			runGoMain(dataDirUTF8, appCtx);
    39  			jniLoaded = true;
    40  		}
    41  	}
    42  
    43  	static private native void runGoMain(byte[] dataDir, Context context);
    44  
    45  	static void writeClipboard(Context ctx, String s) {
    46  		ClipboardManager m = (ClipboardManager)ctx.getSystemService(Context.CLIPBOARD_SERVICE);
    47  		m.setPrimaryClip(ClipData.newPlainText(null, s));
    48  	}
    49  
    50  	static String readClipboard(Context ctx) {
    51  		ClipboardManager m = (ClipboardManager)ctx.getSystemService(Context.CLIPBOARD_SERVICE);
    52  		ClipData c = m.getPrimaryClip();
    53  		if (c == null || c.getItemCount() < 1) {
    54  			return null;
    55  		}
    56  		return c.getItemAt(0).coerceToText(ctx).toString();
    57  	}
    58  
    59  	static void wakeupMainThread() {
    60  		handler.post(new Runnable() {
    61  			@Override public void run() {
    62  				scheduleMainFuncs();
    63  			}
    64  		});
    65  	}
    66  
    67  	static private native void scheduleMainFuncs();
    68  }