-
Notifications
You must be signed in to change notification settings - Fork 121
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split javascript interface methods into domain objects.
Change-Id: I8c9af486c107377d16316361b12be4db3d47dd17
- Loading branch information
Colin Liang
committed
Oct 21, 2024
1 parent
8667efb
commit 1fa1b2a
Showing
10 changed files
with
328 additions
and
213 deletions.
There are no files selected for viewing
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
70 changes: 70 additions & 0 deletions
70
starboard/android/apk/app/src/main/assets/platform_services.js
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
function arrayBufferToBase64(buffer) { | ||
const bytes = new Uint8Array(buffer); | ||
let binary = ''; | ||
for (let i = 0; i < bytes.byteLength; i++) { | ||
binary += String.fromCharCode(bytes[i]); | ||
} | ||
return window.btoa(binary); // Encode binary string to Base64 | ||
} | ||
|
||
var platform_services = { | ||
callbacks: { | ||
}, | ||
callback_from_android: (serviceID, dataFromJava) => { | ||
console.log("Wrapper callback received:", name, dataFromJava); | ||
const binaryString = window.atob(dataFromJava); | ||
console.log("message:" + binaryString); | ||
const len = binaryString.length; | ||
const bytes = new Uint8Array(len); | ||
for (let i = 0; i < len; i++) { | ||
bytes[i] = binaryString.charCodeAt(i); | ||
} | ||
const arrayBuffer = bytes.buffer; | ||
window.H5vccPlatformService.callbacks[serviceID].callback(arrayBuffer); | ||
}, | ||
has: (name) => { | ||
console.log('platformService.has(' + name + ')'); | ||
return Android_H5vccPlatformService.has_platform_service(name); | ||
}, | ||
open: function(name, callback) { | ||
console.log('platformService.open(' + name + ',' + | ||
JSON.stringify(callback) + ')'); | ||
if (typeof callback !== 'function') { | ||
console.log("THROWING Missing or invalid callback function.") | ||
throw new Error("Missing or invalid callback function."); | ||
} else { | ||
console.log("callback was function!!!"); | ||
} | ||
|
||
const serviceId = Object.keys(this.callbacks).length + 1; | ||
// Store the callback with the service ID, name, and callback | ||
window.H5vccPlatformService.callbacks[serviceId] = { | ||
name: name, | ||
callback: callback | ||
}; | ||
Android_H5vccPlatformService.open_platform_service(serviceId, name); | ||
return { | ||
'name': name, | ||
'send': function (data) { | ||
const decoder = new TextDecoder('utf-8'); | ||
const text = decoder.decode(data); | ||
console.log('1 platformService.send(' + text + ')'); | ||
var convert_to_b64 = arrayBufferToBase64(data); | ||
console.log('sending as b64:' + convert_to_b64); | ||
Android_H5vccPlatformService.platform_service_send(name, convert_to_b64); | ||
}, | ||
close: () => { | ||
console.log('1 platformService.close()'); | ||
Android_H5vccPlatformService.close_platform_service(name); | ||
}, | ||
} | ||
}, | ||
send: (data) => { | ||
console.log('platformService.send(' + JSON.stringify(data) + ')'); | ||
}, | ||
close: () => { | ||
console.log('platformService.close()'); | ||
}, | ||
} | ||
|
||
window.H5vccPlatformService = platform_services; |
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
64 changes: 64 additions & 0 deletions
64
starboard/android/apk/app/src/main/java/dev/cobalt/coat/ChrobaltWebViewClient.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package dev.cobalt.coat; | ||
|
||
import static dev.cobalt.util.Log.TAG; | ||
|
||
import android.app.Activity; | ||
import android.graphics.Bitmap; | ||
import android.webkit.WebView; | ||
import android.webkit.WebViewClient; | ||
|
||
import java.util.List; | ||
|
||
import dev.cobalt.coat.android_webview.JavaScriptAndroidObject; | ||
import dev.cobalt.util.Log; | ||
|
||
public class ChrobaltWebViewClient extends WebViewClient { | ||
private final List<JavaScriptAndroidObject> javaScriptAndroidObjectList; | ||
|
||
public ChrobaltWebViewClient(List<JavaScriptAndroidObject> javaScriptAndroidObjectList) { | ||
super(); | ||
this.javaScriptAndroidObjectList = javaScriptAndroidObjectList; | ||
} | ||
|
||
@Override | ||
public void onPageStarted(WebView view, String url, Bitmap favicon) { | ||
Log.i(TAG, "Page started loading: " + url); | ||
super.onPageStarted(view, url, favicon); | ||
|
||
for (JavaScriptAndroidObject javaScriptAndroidObject : javaScriptAndroidObjectList) { | ||
String jsCode = Helpers.loadJavaScriptFromAsset(view.getContext(), javaScriptAndroidObject.getJavaScriptAssets()); | ||
view.evaluateJavascript(jsCode, null); | ||
} | ||
|
||
// Load from a bundled asset | ||
String jsCode = Helpers.loadJavaScriptFromAsset(view.getContext(), "injected_script.js"); | ||
view.evaluateJavascript(jsCode, null); | ||
|
||
// Load over network from development host | ||
Helpers.loadJavaScriptFromURL("http://" + Helpers.getDevelopmentHostSetting(view.getContext()) + ":8000/dyn_script.js") | ||
.thenAccept(jsCode2 -> { | ||
|
||
if ( view.getContext() instanceof Activity) { | ||
((Activity) view.getContext()).runOnUiThread(() -> { | ||
// Perform UI operations here | ||
Log.i(TAG, "Got JS2, injecting"); | ||
view.evaluateJavascript(jsCode2, null); | ||
}); | ||
} | ||
|
||
}).exceptionally(e -> { | ||
// Handle any exceptions here | ||
Log.e(TAG, "Error message: " + e.getMessage(), e); | ||
return null; | ||
}); | ||
|
||
Log.i(TAG, "JavaScript injected"); | ||
} | ||
|
||
@Override | ||
public void onPageFinished(WebView view, String url) { | ||
Log.i(TAG, "Page finished loading: " + url); | ||
super.onPageFinished(view, url); | ||
} | ||
|
||
} |
Oops, something went wrong.