This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var AndroidPromise = { | |
callbacks: {}, | |
requestId: 0, | |
request: function (work, params={}) { | |
return new Promise((resolve, reject) => { | |
let requestId = this.requestId++ | |
this.callbacks[requestId] = { | |
'resolve': resolve, | |
'reject': reject | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Allows to call a Android java function asynchronously | |
// spawn long running computations/io on the Java/Android without blocking the JS/Website running inside the WebView | |
// Eg. const result = await callAndroidAsync('javaFunction', { param1: 'value1', param2: 'value2' }) | |
// Please give a star if you find this useful | |
export async function callAndroidAsync(javaFuncName, params) { | |
const rand = 'asyncJava_' + Math.floor(Math.random() * 1000000) | |
window[rand] = {} | |
// func called from android |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.os.Build; | |
import android.webkit.JavascriptInterface; | |
import android.webkit.WebView; | |
import com.google.gson.Gson; | |
import java.util.concurrent.Callable; | |
import java.util.concurrent.LinkedBlockingDeque; | |
import java.util.concurrent.ThreadPoolExecutor; | |
import java.util.concurrent.TimeUnit; |