-
Notifications
You must be signed in to change notification settings - Fork 119
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
b/372558900
- Loading branch information
Colin Liang
committed
Oct 29, 2024
1 parent
e16ed1e
commit 6663610
Showing
8 changed files
with
165 additions
and
0 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
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 @@ | ||
AndroidExample.testJavaScriptMethod(); |
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
25 changes: 25 additions & 0 deletions
25
...droid/apk/app/src/main/java/dev/cobalt/coat/javabridge/CobaltJavaScriptAndroidObject.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,25 @@ | ||
package dev.cobalt.coat.javabridge; | ||
|
||
import androidx.annotation.Nullable; | ||
|
||
/** | ||
* Interface for Android objects that are exposed to JavaScript. | ||
*/ | ||
public interface CobaltJavaScriptAndroidObject { | ||
|
||
/** | ||
* Gets the name used to expose this object to JavaScript. | ||
* This name is used in the `addJavascriptInterface` method of the WebView. | ||
* | ||
* @return The JavaScript interface name. | ||
*/ | ||
public String getJavaScriptInterfaceName(); | ||
|
||
/** | ||
* Gets the name of the JavaScript asset file that uses this interface. | ||
* This allows the JavaScript code to be loaded and interact with this object. | ||
* | ||
* @return The name of the JavaScript asset file, or null if not applicable. | ||
*/ | ||
public @Nullable String getJavaScriptAssetName(); | ||
} |
26 changes: 26 additions & 0 deletions
26
...pk/app/src/main/java/dev/cobalt/coat/javabridge/CobaltJavaScriptAndroidObjectExample.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,26 @@ | ||
package dev.cobalt.coat.javabridge; | ||
|
||
import static dev.cobalt.util.Log.TAG; | ||
|
||
import android.util.Log; | ||
|
||
/** | ||
* A simple example of implement CobaltJavaScriptAndroidObject. | ||
*/ | ||
public class CobaltJavaScriptAndroidObjectExample implements CobaltJavaScriptAndroidObject { | ||
|
||
@Override | ||
public String getJavaScriptInterfaceName() { | ||
return "AndroidExample"; | ||
} | ||
|
||
@Override | ||
public String getJavaScriptAssetName() { | ||
return "example.js"; | ||
} | ||
|
||
@CobaltJavaScriptInterface | ||
public void testJavaScriptMethod() { | ||
Log.w(TAG, "Hello world"); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
...t/android/apk/app/src/main/java/dev/cobalt/coat/javabridge/CobaltJavaScriptInterface.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,17 @@ | ||
package dev.cobalt.coat.javabridge; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* Annotation that allows exposing methods to JavaScript. Starting from API level | ||
* {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR1} and above, methods explicitly | ||
* marked with this annotation are available to the Javascript code. | ||
*/ | ||
@SuppressWarnings("javadoc") | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target({ElementType.METHOD}) | ||
public @interface CobaltJavaScriptInterface { | ||
} |
29 changes: 29 additions & 0 deletions
29
cobalt/android/apk/app/src/main/java/dev/cobalt/util/AssetLoader.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,29 @@ | ||
package dev.cobalt.util; | ||
|
||
import static dev.cobalt.util.Log.TAG; | ||
|
||
import android.content.Context; | ||
import java.io.InputStream; | ||
import java.nio.charset.StandardCharsets; | ||
import java.io.IOException; | ||
|
||
/** Utility functions for read asset. */ | ||
public class AssetLoader { | ||
|
||
private AssetLoader() {} | ||
|
||
public static String loadJavaScriptFromAssets(Context context, String filename) { | ||
try { | ||
InputStream is = context.getAssets().open(filename); | ||
int size = is.available(); | ||
byte[] buffer = new byte[size]; | ||
is.read(buffer); | ||
is.close(); | ||
return new String(buffer, StandardCharsets.UTF_8); | ||
} catch (IOException ex) { | ||
String error = "asset " + filename + " failed to load"; | ||
Log.e(TAG, error); | ||
return String.format("console.error('%s');", error); | ||
} | ||
} | ||
} |
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