GoldenGate


Source link: https://github.com/Flipboard/GoldenGate

GoldenGate

GoldenGate is an Android annotation processor for generating type safe javascript bindings (Bridges). The library is very similar in usage to something like retrofit in that only an interface has to be declared and annotated (though retrofit does not do any compile time code generating). This annotated interface is at compile time used to generate an type safe wrapper around a webview for interfacing with the javascript.

Installation

buildscript {

  repositories {

jcenter()
  
}

  dependencies {

classpath 'com.neenbedankt.gradle.plugins:android-apt:1.4'
  
}
 
}
  apply plugin: 'com.neenbedankt.android-apt'  dependencies {

  apt 'com.flipboard:goldengate-compiler:1.4.2'
  compile 'com.flipboard:goldengate-api:1.4.2' 
}

Usage

Before starting you will need to configure how object should be serialized to json. By default GoldenGate will use gson but does not package this dependency in case you would like to use something else. If you want to use gson add it as a dependency to your build.gradle.

dependencies {

  compile 'com.google.code.gson:gson:x.x.x' 
}

If you would like to use some other library like jackson or maybe a custom json implementation you can register a JsonSerializer with JavascriptBridge.

JavaScriptBridge.setJsonSerializer(new JsonSerializer(){

  @Override
  <T> String toJson(T stuff) {

// do stuff
  
}

@Override
  <T> T fromJson(String json, Class<T> type) {

// do stuff
  
}
 
}
);

Start by creating an interface and annotate it with @Bridge and also add a method which you want to call in javascript.

@Bridge interface MyJavascript {
  void alert(String message);
 
}

This will automatically generate a class called MyJavascriptBridge which is the implementation which wraps a webview and implements the interface we just defined. Now we have a compile time checked type safe way of opening a javascript alert.

Webview webview = ...; MyJavascript bridge = new MyJavascriptBridge(webview);
 bridge.alert("Hi there!");

The above example is just a fire and forget example. We often want to get some result back. For this we have Callback<T>, because javascript runs asynchronously we can't just return this value and must therefor use a callback. The callback argument must allways be the argument specified last in the method decleration. Here is an example of using Callback<T>.

@Bridge interface MyJavascript {
  void calculateSomeValue(Callback<Integer> value);
 
}
  Webview webview = ...; MyJavascript bridge = new MyJavascriptBridge(webview);
 bridge.calculateSomeValue(new Callback<Integer>() {
  @Override  void onResult(Integer result) {

// do something with result  
}
 
}
);

That's it for simple usage! There are two other annotations for customized usage, @Method and @Property. @Method can be used to override the name of the method on the javascript side of the bridge (The java name of the method is automatically chosen if this annotation is not supplied).

@Bridge interface MyJavascript {
  @Method("console.Log")  void alert(String message);
 
}

The @Property annotation should be used for when setting or getting a property on the javascript side of things. In this case the method may only have one parameter (either a callback for result or a value which should be set). Just like with the @Method declaration a custom name can be chosen for the property. The default name for properties however is the name of the parameter to the method.

@Bridge interface MyJavascript {
  @Property("window.innerHeight")  void getWindowHeight(Callback<Integer> height);
 
}

The @JavascriptCallback annotation should be used on method parameters of type Callback which will be passed to javascript as a javascript callback. This allows javascript functions to call back into your java code with a result at a later time, perhaps after a network request has finished.

@Bridge interface MyJavascript {

  @Method("[1,2,3,4,5,6,7,8,9,10].forEach")
  void looptyLoop(@JavascriptCallback Callback<Integer> val);
 
}

And lastly if things aren't working as expected there is a @Debug annotation that can be added to your @Bridge annotated interface which will cause the javascript being executed to be logged to the console beforehand.

@Debug @Bridge interface MyJavascript {
  void alert(String message);
 
}

Proguard

If you use Proguard, you'll want to make sure you add the following to your config

# GoldenGate -keep class * extends com.flipboard.goldengate.JavaScriptBridge {
 *; 
}
 -keepattributes JavascriptInterface -keepclassmembers class ** {

  @android.webkit.JavascriptInterface public *; 
}
 

Contributing

We welcome pull requests for bug fixes, new features, and improvements to GoldenGate. Contributors to the main GoldenGate repository must accept Flipboard's Apache-style Individual Contributor License Agreement (CLA) before any changes can be merged.

Resources

An Android costomized View to display battery level with charging status.

Releasing versions in Gradle is very different from releasing in Maven. Maven came with maven-release-plugin which did all the dirty work. Gradle has no such tool and probably doesn't need it anyway. Evolution of software craft came to the point, when we start thinking about SCM as ultimate source of truth about project version. Version should not be hardcoded in pom.xml or build.gradle.

JPatterns is a collection of annotations that should make it easier to communicate the use of Design Patterns within your code to your fellow developers and your future self.

Design Patterns are typically encoded into Java code in an ad-hoc fashion. They are either embedded into the names of the classes or written into the Javadocs. Unfortunately it is impossible to accurately determine a pattern based solely on the class structure without knowing the intent of the code author.

The shorctus have a features of Android 7.1 Nougat, and available only for the launcher that implement, in this library, you can implement in your launcher shortcuts starting from API 14!

Library aiming to calculate prayer time with one line code , if you implement prayer time application , there is no need to do this headache again .

TrustKit Android is an open source library that makes it easy to deploy SSL public key pinning and reporting in any Android App.

Topics


2D Engines   3D Engines   9-Patch   Action Bars   Activities   ADB   Advertisements   Analytics   Animations   ANR   AOP   API   APK   APT   Architecture   Audio   Autocomplete   Background Processing   Backward Compatibility   Badges   Bar Codes   Benchmarking   Bitmaps   Bluetooth   Blur Effects   Bread Crumbs   BRMS   Browser Extensions   Build Systems   Bundles   Buttons   Caching   Camera   Canvas   Cards   Carousels   Changelog   Checkboxes   Cloud Storages   Color Analysis   Color Pickers   Colors   Comet/Push   Compass Sensors   Conferences   Content Providers   Continuous Integration   Crash Reports   Credit Cards   Credits   CSV   Curl/Flip   Data Binding   Data Generators   Data Structures   Database   Database Browsers   Date &   Debugging   Decompilers   Deep Links   Dependency Injections   Design   Design Patterns   Dex   Dialogs   Distributed Computing   Distribution Platforms   Download Managers   Drawables   Emoji   Emulators   EPUB   Equalizers &   Event Buses   Exception Handling   Face Recognition   Feedback &   File System   File/Directory   Fingerprint   Floating Action   Fonts   Forms   Fragments   FRP   FSM   Functional Programming   Gamepads   Games   Geocaching   Gestures   GIF   Glow Pad   Gradle Plugins   Graphics   Grid Views   Highlighting   HTML   HTTP Mocking   Icons   IDE   IDE Plugins   Image Croppers   Image Loaders   Image Pickers   Image Processing   Image Views   Instrumentation   Intents   Job Schedulers   JSON   Keyboard   Kotlin   Layouts   Library Demos   List View   List Views   Localization   Location   Lock Patterns   Logcat   Logging   Mails   Maps   Markdown   Mathematics   Maven Plugins   MBaaS   Media   Menus   Messaging   MIME   Mobile Web   Native Image   Navigation   NDK   Networking   NFC   NoSQL   Number Pickers   OAuth   Object Mocking   OCR Engines   OpenGL   ORM   Other Pickers   Parallax List   Parcelables   Particle Systems   Password Inputs   PDF   Permissions   Physics Engines   Platforms   Plugin Frameworks   Preferences   Progress Indicators   ProGuard   Properties   Protocol Buffer   Pull To   Purchases   Push/Pull   QR Codes   Quick Return   Radio Buttons   Range Bars   Ratings   Recycler Views   Resources   REST   Ripple Effects   RSS   Screenshots   Scripting   Scroll Views   SDK   Search Inputs   Security   Sensors   Services   Showcase Views   Signatures   Sliding Panels   Snackbars   SOAP   Social Networks   Spannable   Spinners   Splash Screens   SSH   Static Analysis   Status Bars   Styling   SVG   System   Tags   Task Managers   TDD &   Template Engines   Testing   Testing Tools   Text Formatting   Text Views   Text Watchers   Text-to   Toasts   Toolkits For   Tools   Tooltips   Trainings   TV   Twitter   Updaters   USB   User Stories   Utils   Validation   Video   View Adapters   View Pagers   Views   Watch Face   Wearable Data   Wearables   Weather   Web Tools   Web Views   WebRTC   WebSockets   Wheel Widgets   Wi-Fi   Widgets   Windows   Wizards   XML   XMPP   YAML   ZIP Codes