├── .gitignore ├── HOWTO.md ├── LICENSE ├── README.md ├── app ├── .gitignore ├── app.iml ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── coshx │ │ └── drekkartest │ │ └── ApplicationTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── coshx │ │ │ └── drekkartest │ │ │ ├── BasicTriggeringActivity.java │ │ │ ├── BenchmarkActivity.java │ │ │ ├── EventDataActivity.java │ │ │ ├── EventNameActivity.java │ │ │ ├── InitializationActivity.java │ │ │ ├── MainActivity.java │ │ │ ├── MultipleSubscriberActivity.java │ │ │ ├── ThreadingActivity.java │ │ │ ├── TwoBucketActivity.java │ │ │ ├── TwoBusActivity.java │ │ │ ├── TwoEventActivity.java │ │ │ ├── UnregistrationActivity.java │ │ │ └── WebActivity.java │ └── res │ │ ├── layout │ │ ├── activity_main.xml │ │ ├── two_buckets.xml │ │ └── webview_layout.xml │ │ ├── menu │ │ └── menu_main.xml │ │ ├── mipmap-hdpi │ │ └── ic_launcher.png │ │ ├── mipmap-mdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xhdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xxhdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xxxhdpi │ │ └── ic_launcher.png │ │ ├── raw │ │ ├── basic_triggering.js │ │ ├── basic_triggering_page.html │ │ ├── benchmark.js │ │ ├── benchmark_page.html │ │ ├── event_data.js │ │ ├── event_data_page.html │ │ ├── event_name.js │ │ ├── event_name_page.html │ │ ├── initialization.js │ │ ├── initialization_page.html │ │ ├── multiple_subscribers.js │ │ ├── multiple_subscribers_page.html │ │ ├── threading.js │ │ ├── threading_page.html │ │ ├── two_buckets.js │ │ ├── two_buckets_page.html │ │ ├── two_buses.js │ │ ├── two_buses_page.html │ │ ├── two_events.js │ │ ├── two_events_page.html │ │ ├── unregistration.js │ │ └── unregistration_page.html │ │ ├── values-v21 │ │ └── styles.xml │ │ ├── values-w820dp │ │ └── dimens.xml │ │ └── values │ │ ├── colors.xml │ │ ├── dimens.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── java │ └── com │ └── coshx │ └── drekkartest │ └── ExampleUnitTest.java ├── build.gradle ├── drekkar.iml ├── drekkar ├── .gitignore ├── build.gradle ├── drekkar-drekkar.iml ├── drekkar.iml ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── coshx │ │ └── drekkar │ │ └── ApplicationTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── coshx │ │ │ └── drekkar │ │ │ ├── Arguments.java │ │ │ ├── Callback.java │ │ │ ├── DataSerializer.java │ │ │ ├── Drekkar.java │ │ │ ├── DrekkarFactory.java │ │ │ ├── EventBus.java │ │ │ ├── EventSubscriber.java │ │ │ ├── IWebViewJSEndpoint.java │ │ │ ├── ThreadingHelper.java │ │ │ ├── WebViewJSEndpoint.java │ │ │ ├── WebViewJSEndpointMediator.java │ │ │ ├── WhenReady.java │ │ │ └── WhenReadyOnMain.java │ └── res │ │ ├── raw │ │ ├── drekkar.js │ │ └── drekkar_min.js │ │ └── values │ │ └── strings.xml │ └── test │ └── java │ └── com │ └── coshx │ └── drekkar │ └── ExampleUnitTest.java ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── js ├── Gruntfile.js ├── coffeelint-config.json ├── drekkar.coffee └── package.json ├── logo.png ├── original_logo.png ├── settings.gradle └── src.iml /.gitignore: -------------------------------------------------------------------------------- 1 | # Built application files 2 | *.apk 3 | *.ap_ 4 | 5 | # Files for the Dalvik VM 6 | *.dex 7 | 8 | # Java class files 9 | *.class 10 | 11 | # Generated files 12 | bin/ 13 | gen/ 14 | 15 | # Gradle files 16 | .gradle/ 17 | build/ 18 | /*/build/ 19 | 20 | # Local configuration file (sdk path, etc) 21 | local.properties 22 | 23 | # Proguard folder generated by Eclipse 24 | proguard/ 25 | 26 | # Log Files 27 | *.log 28 | 29 | # Tmp 30 | .DS_Store 31 | *~ 32 | *.map 33 | 34 | .idea 35 | js/node_modules 36 | javadoc/ -------------------------------------------------------------------------------- /HOWTO.md: -------------------------------------------------------------------------------- 1 | # HOW TO 2 | 3 | ## Publish a new release 4 | 5 | 1. Apply any needed change 6 | 1. Increment the version number into the Gruntfile 7 | 1. Run both `default` and `release` target to build the scripts 8 | 1. Increment the version number into the Readme, install section (gradle has to match the latest tag) 9 | 1. Push your changes and create a new tag. The new tag has to be created before pushing the lib. 10 | 1. From the root folder, run this command to update the lib (and pushing to JitPack): 11 | 12 | ```./gradlew install``` 13 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Coshx Labs 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ![Logo](https://raw.githubusercontent.com/coshx/drekkar/master/logo.png) 2 | 3 | [![Release](https://jitpack.io/v/coshx/drekkar.svg)](https://jitpack.io/#coshx/drekkar) 4 | 5 | [![Join the chat at https://gitter.im/coshx/drekkar](https://img.shields.io/gitter/room/nwjs/nw.js.svg)](https://gitter.im/coshx/drekkar?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) 6 | 7 | **An event bus for sending messages between WebView and embedded JS. [Alter ego of Caravel](https://github.com/coshx/caravel).** 8 | 9 | ## Features 10 | 11 | * Easy, fast and reliable event bus system 12 | * Multiple bus support 13 | * Multithreading support 14 | * WebView ~> JavaScript supported types: 15 | - `Bool` 16 | - `Int` 17 | - `Float` 18 | - `Double` 19 | - `String` 20 | - Any list (using types in this list, including maps) 21 | - Any map (using types in this list, including lists) 22 | * JavaScript ~> Android supported types: 23 | - `Boolean` 24 | - `Int` 25 | - `Float` (available as a `Double`) 26 | - `String` 27 | - `Array` (available as a `List`) 28 | - `Object` (available as a `Map`) 29 | 30 | ## Installation 31 | 32 | ### Using JitPack 33 | 34 | Merge this code into your root build.gradle file: 35 | 36 | ```groovy 37 | allprojects { 38 | repositories { 39 | maven { url "https://jitpack.io" } 40 | } 41 | } 42 | ``` 43 | 44 | Then, add this dependency to your module: 45 | 46 | ```groovy 47 | dependencies { 48 | compile 'com.github.coshx:drekkar:v0.1.1' 49 | } 50 | ``` 51 | 52 | Finally, you need to load the internal JS script Drekkar is using to make magic happen. You can either use `R.draw.drekkar_min` or add the minified JS script from [the latest release](https://github.com/coshx/drekkar/releases) to your `raw` resources. This script must be loaded in any webpage you are using Drekkar. 53 | 54 | ### Using as a submodule 55 | 56 | Clone this repo and add the `drekkar` module to your workspace. 57 | 58 | ## Get started 59 | 60 | Drekkar allows developers to communicate between their `WebView` and the embedded JS. You can send any kind of message between these two folks. 61 | 62 | Have a glance at this super simple sample. Let's start with the Android part: 63 | 64 | ```java 65 | class MyActivity extends Activity { 66 | WebView webView; 67 | 68 | void onCreate(Bundle savedInstanceState) { 69 | super.onCreate(savedInstanceState); 70 | 71 | webView = (WebView) findViewById(R.id.my_webview_id); 72 | 73 | // Prepare your bus before loading your web view's content 74 | Drekkar.getDefault(this, webView, new WhenReady() { 75 | @Override 76 | public void run(EventBus bus) { 77 | // In this scope, the JS endpoint is ready to handle any event. 78 | // Register and post your events here 79 | List list = new ArrayList<>(); 80 | list.add(1); 81 | list.add(2); 82 | list.add(3); 83 | 84 | bus.post("MyEvent", list); 85 | 86 | MyActivity.this.bus = bus; // You can save your bus for firing events later 87 | } 88 | }); 89 | 90 | // ... Load web view's content there 91 | } 92 | } 93 | ``` 94 | 95 | And now, in your JS: 96 | 97 | ```javascript 98 | var bus = Drekkar.getDefault(); 99 | 100 | bus.register("AnEventWithAString", function(name, data) { 101 | alert('I received this string: ' + data); 102 | bus.post("AnEventForAndroid"); 103 | }); 104 | ``` 105 | 106 | And voilà! 107 | 108 | ## Porting your app from Caravel to Drekkar 109 | 110 | Super duper easy. Just use the same codebase and use the JS script from Drekkar. Finally, add this after having loaded the Drekkar script: 111 | 112 | ```javascript 113 | var Caravel = Drekkar; 114 | ``` 115 | 116 | ## Troubleshooting 117 | 118 | ### 😕 Sometimes the bus is not working?! 119 | 120 | Firstly, ensure you are using the bus correctly. Check if you are unregistering the bus when exiting the controller owning your web component. Use the [unregister method for this](). 121 | 122 | Drekkar automatically cleans up any unused bus when you create a new one. However, this operation is run in the background to avoid any delay on your side. So, a thread collision might happen if you have not unsubscribed your bus properly. 123 | 124 | However, if you think everything is good with your codebase, feel free to open a ticket. 125 | 126 | ### What object should I use as a subscriber? 127 | 128 | A subscriber could be any object **except the watched target**. We recommend to use the activity/current context as a subscriber (it is a common pattern). 129 | 130 | ### Reserved names 131 | 132 | `DrekkarInit` is an internal event, sent by the JS part for running the `WhenReady` object. 133 | 134 | Also, the default bus is named `default`. If you use this name for a custom bus, Drekkar will automatically switch to the default one. 135 | 136 | Finally, Drekkar names its JS interface `DrekkarWebViewJSEndpoint`. 137 | 138 | ### Keep in mind event and bus names are case-sensitive. 139 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/app.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 8 | 9 | 10 | 11 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 22 5 | buildToolsVersion "22.0.1" 6 | 7 | defaultConfig { 8 | applicationId "com.coshx.drekkartest" 9 | minSdkVersion 15 10 | targetSdkVersion 22 11 | versionCode 1 12 | versionName "1.0" 13 | } 14 | buildTypes { 15 | release { 16 | minifyEnabled false 17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 18 | } 19 | } 20 | } 21 | 22 | dependencies { 23 | compile fileTree(dir: 'libs', include: ['*.jar']) 24 | testCompile 'junit:junit:4.12' 25 | compile 'com.android.support:appcompat-v7:22.2.1' 26 | compile 'com.android.support:design:22.2.1' 27 | compile project(':drekkar') 28 | } 29 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in /Users/acadet/android-sdks/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/coshx/drekkartest/ApplicationTest.java: -------------------------------------------------------------------------------- 1 | package com.coshx.drekkartest; 2 | 3 | import android.app.Application; 4 | import android.test.ApplicationTestCase; 5 | 6 | /** 7 | * Testing Fundamentals 8 | */ 9 | public class ApplicationTest extends ApplicationTestCase { 10 | public ApplicationTest() { 11 | super(Application.class); 12 | } 13 | } -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 8 | 9 | 16 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /app/src/main/java/com/coshx/drekkartest/BasicTriggeringActivity.java: -------------------------------------------------------------------------------- 1 | package com.coshx.drekkartest; 2 | 3 | import android.os.Bundle; 4 | 5 | import com.coshx.drekkar.Callback; 6 | import com.coshx.drekkar.Drekkar; 7 | import com.coshx.drekkar.EventBus; 8 | import com.coshx.drekkar.WhenReady; 9 | 10 | /** 11 | * @class BasicTriggeringActivity 12 | * @brief 13 | */ 14 | public class BasicTriggeringActivity extends WebActivity { 15 | 16 | @Override 17 | protected void onCreate(Bundle savedInstanceState) { 18 | super.onCreate(savedInstanceState); 19 | 20 | Drekkar.getDefault( 21 | this, webView, new WhenReady() { 22 | @Override 23 | public void run(final EventBus bus) { 24 | bus.register( 25 | "From JS", new Callback() { 26 | @Override 27 | public void run(String name, Object data) { 28 | bus.post("From Android"); 29 | } 30 | } 31 | ); 32 | } 33 | } 34 | ); 35 | 36 | loadURL("basic_triggering"); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /app/src/main/java/com/coshx/drekkartest/BenchmarkActivity.java: -------------------------------------------------------------------------------- 1 | package com.coshx.drekkartest; 2 | 3 | import android.os.Bundle; 4 | 5 | import com.coshx.drekkar.Callback; 6 | import com.coshx.drekkar.Drekkar; 7 | import com.coshx.drekkar.EventBus; 8 | import com.coshx.drekkar.WhenReady; 9 | 10 | import java.util.UUID; 11 | 12 | /** 13 | * @class BenchmarkActivity 14 | * @brief 15 | */ 16 | public class BenchmarkActivity extends WebActivity { 17 | 18 | private EventBus bus; 19 | 20 | private void startTesting() { 21 | String name = UUID.randomUUID().toString(); 22 | 23 | Drekkar.get( 24 | this, name, webView, new WhenReady() { 25 | @Override 26 | public void run(final EventBus bus) { 27 | for (int i = 0; i < 1000; i++) { 28 | bus.register( 29 | "Background-" + i, new Callback() { 30 | @Override 31 | public void run(String name, Object data) { 32 | bus.post(name + "-confirmation"); 33 | } 34 | } 35 | ); 36 | 37 | bus.registerOnMain( 38 | "Main-" + i, new Callback() { 39 | @Override 40 | public void run(String name, Object data) { 41 | bus.post(name + "-confirmation"); 42 | } 43 | } 44 | ); 45 | } 46 | 47 | bus.post("Ready"); 48 | } 49 | } 50 | ); 51 | 52 | bus.post("BusName", name); 53 | } 54 | 55 | @Override 56 | protected void onCreate(Bundle savedInstanceState) { 57 | super.onCreate(savedInstanceState); 58 | 59 | Drekkar.getDefault( 60 | this, webView, new WhenReady() { 61 | @Override 62 | public void run(EventBus bus) { 63 | BenchmarkActivity.this.bus = bus; 64 | bus.register( 65 | "Start", new Callback() { 66 | @Override 67 | public void run(String name, Object data) { 68 | startTesting(); 69 | } 70 | } 71 | ); 72 | } 73 | } 74 | ); 75 | 76 | loadURL("benchmark"); 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /app/src/main/java/com/coshx/drekkartest/EventDataActivity.java: -------------------------------------------------------------------------------- 1 | package com.coshx.drekkartest; 2 | 3 | import android.os.Bundle; 4 | import android.util.Log; 5 | 6 | import com.coshx.drekkar.Callback; 7 | import com.coshx.drekkar.Drekkar; 8 | import com.coshx.drekkar.EventBus; 9 | import com.coshx.drekkar.WhenReady; 10 | 11 | import java.util.ArrayList; 12 | import java.util.HashMap; 13 | import java.util.List; 14 | import java.util.Map; 15 | 16 | /** 17 | * @class EventDataActivity 18 | * @brief 19 | */ 20 | public class EventDataActivity extends WebActivity { 21 | 22 | private void raise(String msg) { 23 | Log.e(EventDataActivity.class.getName(), msg); 24 | } 25 | 26 | @Override 27 | protected void onCreate(Bundle savedInstanceState) { 28 | super.onCreate(savedInstanceState); 29 | 30 | Drekkar.getDefault( 31 | this, webView, new WhenReady() { 32 | @Override 33 | public void run(EventBus bus) { 34 | bus.post("Bool", true); 35 | bus.post("Int", 42); 36 | bus.post("Float", 19.92); 37 | bus.post("Double", 20.15); 38 | bus.post("String", "Churchill"); 39 | bus.post("HazardousString", "There is a \" and a '"); 40 | 41 | List list = new ArrayList<>(); 42 | list.add(1); 43 | list.add(2); 44 | list.add(3); 45 | list.add(5); 46 | bus.post("List", list); 47 | 48 | Map dictionary = new HashMap(); 49 | dictionary.put("foo", 45); 50 | dictionary.put("bar", 89); 51 | bus.post("Dictionary", dictionary); 52 | 53 | List complexList = new ArrayList(); 54 | Map d1 = new HashMap(), d2 = new HashMap(); 55 | d1.put("name", "Alice"); 56 | d1.put("age", 24); 57 | d2.put("name", "Bob"); 58 | d2.put("age", 23); 59 | complexList.add(d1); 60 | complexList.add(d2); 61 | bus.post("ComplexList", complexList); 62 | 63 | Map complexDictionary = new HashMap(); 64 | complexDictionary.put("name", "Paul"); 65 | Map d3 = new HashMap(); 66 | d3.put("street", "Hugo"); 67 | d3.put("city", "Bordeaux"); 68 | complexDictionary.put("address", d3); 69 | String sArray[] = { "Fifa", "Star Wars" }; 70 | complexDictionary.put("games", sArray); 71 | bus.post("ComplexDictionary", complexDictionary); 72 | 73 | 74 | bus.register( 75 | "True", new Callback() { 76 | @Override 77 | public void run(String name, Object data) { 78 | boolean b = (boolean) data; 79 | 80 | if (b != true) { 81 | raise("True - wrong value"); 82 | } 83 | } 84 | } 85 | ); 86 | 87 | bus.register( 88 | "False", new Callback() { 89 | @Override 90 | public void run(String name, Object data) { 91 | boolean b = (boolean) data; 92 | 93 | if (b != false) { 94 | raise("False - wrong value"); 95 | } 96 | } 97 | } 98 | ); 99 | 100 | bus.register( 101 | "Int", new Callback() { 102 | @Override 103 | public void run(String name, Object data) { 104 | int i = (int) data; 105 | 106 | if (i != 987) { 107 | raise("Int - wrong value"); 108 | } 109 | } 110 | } 111 | ); 112 | 113 | bus.register( 114 | "Double", new Callback() { 115 | @Override 116 | public void run(String name, Object data) { 117 | double d = (double) data; 118 | 119 | if (d != 15.15) { 120 | raise("Double - wrong value"); 121 | } 122 | } 123 | } 124 | ); 125 | 126 | bus.register( 127 | "String", new Callback() { 128 | @Override 129 | public void run(String name, Object data) { 130 | String s = (String) data; 131 | 132 | if (!s.equals("Napoleon")) { 133 | raise("String - wrong value"); 134 | } 135 | } 136 | } 137 | ); 138 | 139 | bus.register( 140 | "UUID", new Callback() { 141 | @Override 142 | public void run(String name, Object data) { 143 | String s = (String) data; 144 | 145 | if (!s.equals("9658ae60-9e0d-4da7-a63d-46fe75ff1db1")) { 146 | raise("UUID - wrong value"); 147 | } 148 | } 149 | } 150 | ); 151 | 152 | bus.register( 153 | "List", new Callback() { 154 | @Override 155 | public void run(String name, Object data) { 156 | List l = (List) data; 157 | 158 | if (l.size() != 3) { 159 | raise("List - wrong length"); 160 | } 161 | if (l.get(0) != 3) { 162 | raise("List - wrong first length"); 163 | } 164 | if (l.get(1) != 1) { 165 | raise("List - wrong second element"); 166 | } 167 | if (l.get(2) != 4) { 168 | raise("List - wrong third element"); 169 | } 170 | } 171 | } 172 | ); 173 | 174 | bus.register( 175 | "Dictionary", new Callback() { 176 | @Override 177 | public void run(String name, Object data) { 178 | Map dictionary = (Map) data; 179 | 180 | if (dictionary.size() != 2) { 181 | raise("Dictionary - wrong length"); 182 | } 183 | if (!((String) dictionary.get("movie")).equals( 184 | "Once upon a time " + 185 | "in the West" 186 | )) { 187 | raise("Dictionary - wrong first pair"); 188 | } 189 | if (!((String) dictionary.get("actor")).equals( 190 | "Charles Bronson" 191 | )) { 192 | raise("Dictionary - wrong second pair"); 193 | } 194 | } 195 | } 196 | ); 197 | 198 | bus.register( 199 | "ComplexArray", new Callback() { 200 | @Override 201 | public void run(String name, Object data) { 202 | List list = (List) data; 203 | 204 | if (list.size() != 3) { 205 | raise("ComplexArray - wrong length"); 206 | } 207 | if (((Integer) list.get(0)) != 87) { 208 | raise("ComplexArray - wrong first element"); 209 | } 210 | 211 | Map d = (Map) list.get(1); 212 | if (!((String) d.get("name")).equals("Bruce Willis")) { 213 | raise("ComplexArray - wrong second element"); 214 | } 215 | 216 | if (!((String) list.get(2)).equals("left-handed")) { 217 | raise("ComplexArray - wrong third element"); 218 | } 219 | } 220 | } 221 | ); 222 | 223 | bus.register( 224 | "ComplexDictionary", new Callback() { 225 | @Override 226 | public void run(String name, Object data) { 227 | Map dictionary = (Map) data; 228 | 229 | if (!((String) dictionary.get("name")).equals("John Malkovich")) { 230 | raise("ComplexDictionary - wrong first pair"); 231 | } 232 | List l = (List) dictionary.get("movies"); 233 | if (l.size() != 2) { 234 | raise("ComplexDictionary - wrong length"); 235 | } 236 | if (!l.get(0).equals("Dangerous Liaisons")) { 237 | raise("ComplexDictionary - wrong first element in array"); 238 | } 239 | if (!l.get(1).equals("Burn after reading")) { 240 | raise("ComplexDictionary - wrong second element in array"); 241 | } 242 | 243 | if (((int) dictionary.get("kids")) != 2) { 244 | raise("ComplexDictionary - wrong third pair"); 245 | } 246 | } 247 | } 248 | ); 249 | 250 | bus.post("Ready"); 251 | } 252 | } 253 | ); 254 | 255 | loadURL("event_data"); 256 | } 257 | } 258 | -------------------------------------------------------------------------------- /app/src/main/java/com/coshx/drekkartest/EventNameActivity.java: -------------------------------------------------------------------------------- 1 | package com.coshx.drekkartest; 2 | 3 | import android.os.Bundle; 4 | 5 | import com.coshx.drekkar.Callback; 6 | import com.coshx.drekkar.Drekkar; 7 | import com.coshx.drekkar.EventBus; 8 | import com.coshx.drekkar.WhenReady; 9 | 10 | public class EventNameActivity extends WebActivity { 11 | @Override 12 | protected void onCreate(Bundle savedInstanceState) { 13 | super.onCreate(savedInstanceState); 14 | 15 | Drekkar.getDefault( 16 | this, webView, new WhenReady() { 17 | @Override 18 | public void run(final EventBus bus) { 19 | bus.register( 20 | "Bar", new Callback() { 21 | @Override 22 | public void run(String name, Object data) { 23 | if (name.equals("Bar")) { 24 | bus.post("Foo"); 25 | } else { 26 | bus.post("Foobar"); 27 | } 28 | } 29 | } 30 | ); 31 | } 32 | } 33 | ); 34 | 35 | loadURL("event_name"); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /app/src/main/java/com/coshx/drekkartest/InitializationActivity.java: -------------------------------------------------------------------------------- 1 | package com.coshx.drekkartest; 2 | 3 | import android.os.Bundle; 4 | 5 | import com.coshx.drekkar.Drekkar; 6 | import com.coshx.drekkar.EventBus; 7 | import com.coshx.drekkar.WhenReady; 8 | 9 | /** 10 | * @class InitializationActivity 11 | * @brief 12 | */ 13 | public class InitializationActivity extends WebActivity { 14 | @Override 15 | protected void onCreate(Bundle savedInstanceState) { 16 | super.onCreate(savedInstanceState); 17 | 18 | Drekkar.getDefault( 19 | this, webView, new WhenReady() { 20 | @Override 21 | public void run(EventBus bus) { 22 | bus.post("Before"); 23 | } 24 | } 25 | ); 26 | 27 | loadURL("initialization"); 28 | 29 | Drekkar.getDefault( 30 | this, webView, new WhenReady() { 31 | @Override 32 | public void run(EventBus bus) { 33 | bus.post("After"); 34 | } 35 | } 36 | ); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /app/src/main/java/com/coshx/drekkartest/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.coshx.drekkartest; 2 | 3 | import android.app.Activity; 4 | import android.content.Intent; 5 | import android.os.Build; 6 | import android.os.Bundle; 7 | import android.view.View; 8 | import android.webkit.WebView; 9 | 10 | public class MainActivity extends Activity { 11 | 12 | @Override 13 | protected void onCreate(Bundle savedInstanceState) { 14 | super.onCreate(savedInstanceState); 15 | setContentView(R.layout.activity_main); 16 | 17 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { 18 | WebView.setWebContentsDebuggingEnabled(true); 19 | } 20 | 21 | findViewById(R.id.basic_triggering_trigger).setOnClickListener( 22 | new View.OnClickListener() { 23 | @Override 24 | public void onClick(View v) { 25 | startActivity( 26 | new Intent(getApplication(), BasicTriggeringActivity.class) 27 | ); 28 | } 29 | } 30 | ); 31 | 32 | findViewById(R.id.benchmark_trigger).setOnClickListener( 33 | new View.OnClickListener() { 34 | @Override 35 | public void onClick(View v) { 36 | startActivity( 37 | new Intent(getApplication(), BenchmarkActivity.class) 38 | ); 39 | } 40 | } 41 | ); 42 | 43 | findViewById(R.id.event_data_trigger).setOnClickListener( 44 | new View.OnClickListener() { 45 | @Override 46 | public void onClick(View v) { 47 | startActivity( 48 | new Intent(getApplication(), EventDataActivity.class) 49 | ); 50 | } 51 | } 52 | ); 53 | 54 | findViewById(R.id.event_name_trigger).setOnClickListener( 55 | new View.OnClickListener() { 56 | @Override 57 | public void onClick(View v) { 58 | startActivity( 59 | new Intent(getApplication(), EventNameActivity.class) 60 | ); 61 | } 62 | } 63 | ); 64 | 65 | findViewById(R.id.initialization_trigger).setOnClickListener( 66 | new View.OnClickListener() { 67 | @Override 68 | public void onClick(View v) { 69 | startActivity( 70 | new Intent(getApplication(), InitializationActivity.class) 71 | ); 72 | } 73 | } 74 | ); 75 | 76 | findViewById(R.id.multiple_subscriber_trigger).setOnClickListener( 77 | new View.OnClickListener() { 78 | @Override 79 | public void onClick(View v) { 80 | startActivity( 81 | new Intent(getApplication(), MultipleSubscriberActivity.class) 82 | ); 83 | } 84 | } 85 | ); 86 | 87 | findViewById(R.id.threading_trigger).setOnClickListener( 88 | new View.OnClickListener() { 89 | @Override 90 | public void onClick(View v) { 91 | startActivity( 92 | new Intent(getApplication(), ThreadingActivity.class) 93 | ); 94 | } 95 | } 96 | ); 97 | 98 | findViewById(R.id.two_bucket_trigger).setOnClickListener( 99 | new View.OnClickListener() { 100 | @Override 101 | public void onClick(View v) { 102 | startActivity( 103 | new Intent(getApplication(), TwoBucketActivity.class) 104 | ); 105 | } 106 | } 107 | ); 108 | 109 | findViewById(R.id.two_bus_trigger).setOnClickListener( 110 | new View.OnClickListener() { 111 | @Override 112 | public void onClick(View v) { 113 | startActivity( 114 | new Intent(getApplication(), TwoBusActivity.class) 115 | ); 116 | } 117 | } 118 | ); 119 | 120 | findViewById(R.id.two_event_trigger).setOnClickListener( 121 | new View.OnClickListener() { 122 | @Override 123 | public void onClick(View v) { 124 | startActivity( 125 | new Intent(getApplication(), TwoEventActivity.class) 126 | ); 127 | } 128 | } 129 | ); 130 | 131 | findViewById(R.id.unregistration_trigger).setOnClickListener( 132 | new View.OnClickListener() { 133 | @Override 134 | public void onClick(View v) { 135 | startActivity( 136 | new Intent(getApplication(), UnregistrationActivity.class) 137 | ); 138 | } 139 | } 140 | ); 141 | } 142 | } 143 | -------------------------------------------------------------------------------- /app/src/main/java/com/coshx/drekkartest/MultipleSubscriberActivity.java: -------------------------------------------------------------------------------- 1 | package com.coshx.drekkartest; 2 | 3 | import android.os.Bundle; 4 | 5 | import com.coshx.drekkar.Drekkar; 6 | import com.coshx.drekkar.EventBus; 7 | import com.coshx.drekkar.WhenReady; 8 | 9 | /** 10 | * @class MultipleSubscriberActivity 11 | * @brief 12 | */ 13 | public class MultipleSubscriberActivity extends WebActivity { 14 | @Override 15 | protected void onCreate(Bundle savedInstanceState) { 16 | super.onCreate(savedInstanceState); 17 | 18 | Drekkar.getDefault( 19 | this, webView, new WhenReady() { 20 | @Override 21 | public void run(EventBus bus) { 22 | bus.post("AnEvent"); 23 | } 24 | } 25 | ); 26 | 27 | loadURL("multiple_subscribers"); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /app/src/main/java/com/coshx/drekkartest/ThreadingActivity.java: -------------------------------------------------------------------------------- 1 | package com.coshx.drekkartest; 2 | 3 | import android.os.Bundle; 4 | import android.util.Log; 5 | 6 | import com.coshx.drekkar.Callback; 7 | import com.coshx.drekkar.Drekkar; 8 | import com.coshx.drekkar.EventBus; 9 | import com.coshx.drekkar.WhenReady; 10 | import com.coshx.drekkar.WhenReadyOnMain; 11 | 12 | /** 13 | * @class ThreadingActivity 14 | * @brief 15 | */ 16 | public class ThreadingActivity extends WebActivity { 17 | @Override 18 | protected void onCreate(Bundle savedInstanceState) { 19 | super.onCreate(savedInstanceState); 20 | 21 | Drekkar.get( 22 | this, "First", webView, new WhenReady() { 23 | @Override 24 | public void run(final EventBus bus) { 25 | bus.registerOnMain( 26 | "FromJSForBackground", new Callback() { 27 | @Override 28 | public void run(String name, Object data) { 29 | bus.post("FromBackgroundAfterFromJS"); 30 | } 31 | } 32 | ); 33 | 34 | try { 35 | Thread.sleep(1 * 1000); 36 | } catch (Exception e) { 37 | Log.e(ThreadingActivity.class.getName(), e.getMessage()); 38 | } 39 | bus.post("FromBackground"); 40 | } 41 | } 42 | ); 43 | 44 | Drekkar.get( 45 | this, "Second", webView, new WhenReadyOnMain() { 46 | @Override 47 | public void run(final EventBus bus) { 48 | bus.register( 49 | "FromJSForMain", new Callback() { 50 | @Override 51 | public void run(String name, Object data) { 52 | try { 53 | Thread.sleep(2 * 1000); 54 | } catch (Exception e) { 55 | Log.e(ThreadingActivity.class.getName(), e.getMessage()); 56 | } 57 | 58 | bus.post("FromMainAfterFromJS"); 59 | } 60 | } 61 | ); 62 | 63 | bus.post("FromMain"); 64 | } 65 | } 66 | ); 67 | 68 | loadURL("threading"); 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /app/src/main/java/com/coshx/drekkartest/TwoBucketActivity.java: -------------------------------------------------------------------------------- 1 | package com.coshx.drekkartest; 2 | 3 | import android.app.Activity; 4 | import android.os.Bundle; 5 | import android.util.Log; 6 | import android.webkit.WebView; 7 | 8 | import com.coshx.drekkar.Callback; 9 | import com.coshx.drekkar.Drekkar; 10 | import com.coshx.drekkar.EventBus; 11 | import com.coshx.drekkar.WhenReadyOnMain; 12 | 13 | /** 14 | * @class TwoBucketActivity 15 | * @brief 16 | */ 17 | public class TwoBucketActivity extends Activity { 18 | 19 | private WebView webView1; 20 | private WebView webView2; 21 | 22 | private boolean isOtherOneReady = false; 23 | 24 | @Override 25 | protected void onCreate(Bundle savedInstanceState) { 26 | super.onCreate(savedInstanceState); 27 | setContentView(R.layout.two_buckets); 28 | 29 | webView1 = (WebView) findViewById(R.id.two_bucket_webview_1); 30 | webView2 = (WebView) findViewById(R.id.two_bucket_webview_2); 31 | 32 | Drekkar.getDefault( 33 | this, webView1, new WhenReadyOnMain() { 34 | @Override 35 | public void run(EventBus bus) { 36 | bus.register( 37 | "Bar", new Callback() { 38 | @Override 39 | public void run(String name, Object data) { 40 | Log.d(TwoBucketActivity.class.getName(), "Bar 1"); 41 | } 42 | } 43 | ); 44 | 45 | if (isOtherOneReady) { 46 | bus.post("Foo"); 47 | } else { 48 | isOtherOneReady = true; 49 | } 50 | } 51 | } 52 | ); 53 | 54 | Drekkar.getDefault( 55 | this, webView2, new WhenReadyOnMain() { 56 | @Override 57 | public void run(EventBus bus) { 58 | bus.register( 59 | "Bar", new Callback() { 60 | @Override 61 | public void run(String name, Object data) { 62 | Log.d(TwoBucketActivity.class.getName(), "Bar 2"); 63 | } 64 | } 65 | ); 66 | 67 | if (isOtherOneReady) { 68 | bus.post("Foo"); 69 | } else { 70 | isOtherOneReady = true; 71 | } 72 | } 73 | } 74 | ); 75 | 76 | webView1.loadUrl("file:///android_res/raw/two_buckets_page.html"); 77 | webView1.getSettings().setJavaScriptEnabled(true); 78 | webView2.loadUrl("file:///android_res/raw/two_buckets_page.html"); 79 | webView2.getSettings().setJavaScriptEnabled(true); 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /app/src/main/java/com/coshx/drekkartest/TwoBusActivity.java: -------------------------------------------------------------------------------- 1 | package com.coshx.drekkartest; 2 | 3 | import android.os.Bundle; 4 | 5 | import com.coshx.drekkar.Drekkar; 6 | import com.coshx.drekkar.EventBus; 7 | import com.coshx.drekkar.WhenReady; 8 | 9 | /** 10 | * @class TwoBusActivity 11 | * @brief 12 | */ 13 | public class TwoBusActivity extends WebActivity { 14 | @Override 15 | protected void onCreate(Bundle savedInstanceState) { 16 | super.onCreate(savedInstanceState); 17 | 18 | Drekkar.get( 19 | this, "FooBus", webView, new WhenReady() { 20 | @Override 21 | public void run(EventBus bus) { 22 | bus.post("AnEvent"); 23 | } 24 | } 25 | ); 26 | 27 | Drekkar.get( 28 | this, "BarBus", webView, new WhenReady() { 29 | @Override 30 | public void run(EventBus bus) { 31 | bus.post("AnEvent"); 32 | } 33 | } 34 | ); 35 | 36 | loadURL("two_buses"); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /app/src/main/java/com/coshx/drekkartest/TwoEventActivity.java: -------------------------------------------------------------------------------- 1 | package com.coshx.drekkartest; 2 | 3 | import android.os.Bundle; 4 | 5 | import com.coshx.drekkar.Callback; 6 | import com.coshx.drekkar.Drekkar; 7 | import com.coshx.drekkar.EventBus; 8 | import com.coshx.drekkar.WhenReady; 9 | 10 | /** 11 | * @class TwoEventActivity 12 | * @brief 13 | */ 14 | public class TwoEventActivity extends WebActivity { 15 | @Override 16 | protected void onCreate(Bundle savedInstanceState) { 17 | super.onCreate(savedInstanceState); 18 | 19 | Drekkar.getDefault( 20 | this, webView, new WhenReady() { 21 | @Override 22 | public void run(final EventBus bus) { 23 | bus.register( 24 | "FirstEvent", new Callback() { 25 | @Override 26 | public void run(String name, Object data) { 27 | bus.post("ThirdEvent"); 28 | } 29 | } 30 | ); 31 | 32 | bus.register( 33 | "NeverTriggeredEvent", new Callback() { 34 | @Override 35 | public void run(String name, Object data) { 36 | bus.post("FourthEvent"); 37 | } 38 | } 39 | ); 40 | } 41 | } 42 | ); 43 | 44 | loadURL("two_events"); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /app/src/main/java/com/coshx/drekkartest/UnregistrationActivity.java: -------------------------------------------------------------------------------- 1 | package com.coshx.drekkartest; 2 | 3 | import android.os.Bundle; 4 | import android.util.Log; 5 | 6 | import com.coshx.drekkar.Callback; 7 | import com.coshx.drekkar.Drekkar; 8 | import com.coshx.drekkar.EventBus; 9 | import com.coshx.drekkar.WhenReady; 10 | 11 | import java.util.Timer; 12 | import java.util.TimerTask; 13 | 14 | /** 15 | * @class UnregistrationActivity 16 | * @brief 17 | */ 18 | public class UnregistrationActivity extends WebActivity { 19 | 20 | private EventBus bus; 21 | 22 | @Override 23 | protected void onCreate(Bundle savedInstanceState) { 24 | super.onCreate(savedInstanceState); 25 | 26 | Drekkar.getDefault( 27 | this, webView, new WhenReady() { 28 | @Override 29 | public void run(final EventBus bus) { 30 | UnregistrationActivity.this.bus = bus; 31 | 32 | bus.registerOnMain( 33 | "Whazup?", new Callback() { 34 | @Override 35 | public void run(String name, Object data) { 36 | bus.post("Bye"); 37 | Timer t = new Timer(); 38 | 39 | t.schedule( 40 | new TimerTask() { 41 | @Override 42 | public void run() { 43 | UnregistrationActivity.this.bus.unregister(); 44 | } 45 | }, 46 | 2 * 1000 47 | ); 48 | } 49 | } 50 | ); 51 | 52 | bus.register( 53 | "Still around?", new Callback() { 54 | @Override 55 | public void run(String name, Object data) { 56 | Log.e( 57 | UnregistrationActivity.class.getName(), "You should not see this " + 58 | "message" 59 | ); 60 | } 61 | } 62 | ); 63 | 64 | bus.post("Hello!"); 65 | } 66 | } 67 | ); 68 | 69 | loadURL("unregistration"); 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /app/src/main/java/com/coshx/drekkartest/WebActivity.java: -------------------------------------------------------------------------------- 1 | package com.coshx.drekkartest; 2 | 3 | import android.app.Activity; 4 | import android.os.Bundle; 5 | import android.webkit.WebView; 6 | 7 | /** 8 | * @class WebActivity 9 | * @brief 10 | */ 11 | public class WebActivity extends Activity { 12 | 13 | protected WebView webView; 14 | 15 | @Override 16 | protected void onCreate(Bundle savedInstanceState) { 17 | super.onCreate(savedInstanceState); 18 | 19 | setContentView(R.layout.webview_layout); 20 | webView = (WebView) findViewById(R.id.embedded_webview); 21 | } 22 | 23 | void loadURL(String filename) { 24 | webView.loadUrl("file:///android_res/raw/" + filename + "_page.html"); 25 | webView.getSettings().setJavaScriptEnabled(true); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 10 | 11 | 16 | 17 | 18 |