Skip to content

Commit

Permalink
update JS API method 'startIntent': only use ES6 in AP1 21+
Browse files Browse the repository at this point in the history
  • Loading branch information
warren-bank committed Sep 3, 2020
1 parent b29d825 commit b5ac378
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.util.Log;
import android.webkit.JavascriptInterface;
import android.widget.Toast;
Expand All @@ -11,12 +12,14 @@ public class WmJsApi {

public static final String TAG = "WebViewGmApi";

public String secret;
public String secret;
public Activity activity;
public boolean useES6;

public WmJsApi(String secret, Activity activity) {
this.secret = secret;
this.secret = secret;
this.activity = activity;
this.useES6 = (Build.VERSION.SDK_INT >= 21); // use ES5 in Android <= 4.4 because WebView is outdated and cannot be updated
}

public static final String GlobalJsApiNamespace = "WebViewWM";
Expand Down Expand Up @@ -110,10 +113,13 @@ public String getWrappedJsApi() {
String defaultSignature = "\"" + WmJsApi.this.secret + "\"";
String jsApi = "";

jsApi += "var GM_toastLong" + " = function(message) { " + jsBridgeName + ".toast(" + defaultSignature + ", " + Toast.LENGTH_LONG + ", message);" + " };\n";
jsApi += "var GM_toastShort" + " = function(message) { " + jsBridgeName + ".toast(" + defaultSignature + ", " + Toast.LENGTH_SHORT + ", message);" + " };\n";
jsApi += "var GM_startIntent" + " = function(action, data, type, ...extras) { " + jsBridgeName + ".startIntent(" + defaultSignature + ", action, data, type, extras);" + " };\n";
jsApi += "var GM_exit" + " = function() { " + jsBridgeName + ".exit(" + defaultSignature + ");" + " };\n";
jsApi += "var GM_toastLong" + " = function(message) { " + jsBridgeName + ".toast(" + defaultSignature + ", " + Toast.LENGTH_LONG + ", message);" + " };\n";
jsApi += "var GM_toastShort" + " = function(message) { " + jsBridgeName + ".toast(" + defaultSignature + ", " + Toast.LENGTH_SHORT + ", message);" + " };\n";
jsApi += (useES6)
? ("var GM_startIntent" + " = function(action, data, type, ...extras) { " + jsBridgeName + ".startIntent(" + defaultSignature + ", action, data, type, extras);" + " };\n")
: ("var GM_startIntent" + " = function(action, data, type) { " + jsBridgeName + ".startIntent(" + defaultSignature + ", action, data, type, Array.prototype.slice.call(arguments, 3));" + " };\n")
;
jsApi += "var GM_exit" + " = function() { " + jsBridgeName + ".exit(" + defaultSignature + ");" + " };\n";

return jsApi;
}
Expand Down
4 changes: 2 additions & 2 deletions android-studio-project/constants.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
project.ext {
releaseVersionCode = Integer.parseInt("001000308", 10)
releaseVersion = '001.00.03-08API'
releaseVersionCode = Integer.parseInt("001000408", 10)
releaseVersion = '001.00.04-08API'
javaVersion = JavaVersion.VERSION_1_8
minSdkVersion = 8
targetSdkVersion = 28
Expand Down

0 comments on commit b5ac378

Please sign in to comment.