diff --git a/app/src/main/java/com/joey/xwebassistant/sample/JavaMethod/JSToast.java b/app/src/main/java/com/joey/xwebassistant/sample/JavaMethod/JSToast.java index b5523d1..5b653d3 100644 --- a/app/src/main/java/com/joey/xwebassistant/sample/JavaMethod/JSToast.java +++ b/app/src/main/java/com/joey/xwebassistant/sample/JavaMethod/JSToast.java @@ -1,35 +1,19 @@ package com.joey.xwebassistant.sample.JavaMethod; import android.content.Context; -import android.widget.Toast; -import com.joey.xwebview.jsbridge.method.JSMessage; import com.joey.xwebview.jsbridge.method.XJavaMethod; /** * Description: * author:Joey - * date:2018/8/20 + * date:2018/8/21 */ -public class JSToast extends XJavaMethod{ - private Context context; +public abstract class JSToast extends XJavaMethod{ + protected Context context; public void setContext(Context context) { this.context = context; } - @Override - public void call(JSMessage message) { - if (context != null){ - Toast.makeText(context, message.params.optString("message"), Toast.LENGTH_SHORT).show(); - callback(message.callback, "call JSToast success"); - } else { - callError(message.errorCallback, "call JSToast failed! context is null!"); - } - } - - @Override - public Permission permission() { - return Permission.PUBLIC; - } } diff --git a/app/src/main/java/com/joey/xwebassistant/sample/JavaMethod/JSToastAuthorized.java b/app/src/main/java/com/joey/xwebassistant/sample/JavaMethod/JSToastAuthorized.java new file mode 100644 index 0000000..b3d6c3c --- /dev/null +++ b/app/src/main/java/com/joey/xwebassistant/sample/JavaMethod/JSToastAuthorized.java @@ -0,0 +1,28 @@ +package com.joey.xwebassistant.sample.JavaMethod; + +import android.widget.Toast; + +import com.joey.xwebview.jsbridge.method.JSMessage; + +/** + * Description: + * author:Joey + * date:2018/8/20 + */ +public class JSToastAuthorized extends JSToast{ + + @Override + public void call(JSMessage message) { + if (context != null){ + Toast.makeText(context, message.params.optString("message"), Toast.LENGTH_SHORT).show(); + callback(message.callback, "call JSToastAuthorized success"); + } else { + callError(message.errorCallback, "call JSToastAuthorized failed! context is null!"); + } + } + + @Override + public Permission permission() { + return Permission.PUBLIC; + } +} diff --git a/app/src/main/java/com/joey/xwebassistant/sample/JavaMethod/JSToastPrivate.java b/app/src/main/java/com/joey/xwebassistant/sample/JavaMethod/JSToastPrivate.java new file mode 100644 index 0000000..86d2efb --- /dev/null +++ b/app/src/main/java/com/joey/xwebassistant/sample/JavaMethod/JSToastPrivate.java @@ -0,0 +1,27 @@ +package com.joey.xwebassistant.sample.JavaMethod; + +import android.widget.Toast; + +import com.joey.xwebview.jsbridge.method.JSMessage; + +/** + * Description: + * author:Joey + * date:2018/8/20 + */ +public class JSToastPrivate extends JSToast{ + @Override + public void call(JSMessage message) { + if (context != null){ + Toast.makeText(context, message.params.optString("message"), Toast.LENGTH_SHORT).show(); + callback(message.callback, "call JSToastPrivate success"); + } else { + callError(message.errorCallback, "call JSToastPrivate failed! context is null!"); + } + } + + @Override + public Permission permission() { + return Permission.PRIVATE; + } +} diff --git a/app/src/main/java/com/joey/xwebassistant/sample/JavaMethod/JSToastPublic.java b/app/src/main/java/com/joey/xwebassistant/sample/JavaMethod/JSToastPublic.java new file mode 100644 index 0000000..6fed93e --- /dev/null +++ b/app/src/main/java/com/joey/xwebassistant/sample/JavaMethod/JSToastPublic.java @@ -0,0 +1,27 @@ +package com.joey.xwebassistant.sample.JavaMethod; + +import android.widget.Toast; + +import com.joey.xwebview.jsbridge.method.JSMessage; + +/** + * Description: + * author:Joey + * date:2018/8/20 + */ +public class JSToastPublic extends JSToast{ + @Override + public void call(JSMessage message) { + if (context != null){ + Toast.makeText(context, message.params.optString("message"), Toast.LENGTH_SHORT).show(); + callback(message.callback, "call JSToastPublic success"); + } else { + callError(message.errorCallback, "call JSToastPublic failed! context is null!"); + } + } + + @Override + public Permission permission() { + return Permission.PUBLIC; + } +} diff --git a/app/src/main/java/com/joey/xwebassistant/sample/SampleActivity.java b/app/src/main/java/com/joey/xwebassistant/sample/SampleActivity.java index 0d9b194..3e4a101 100644 --- a/app/src/main/java/com/joey/xwebassistant/sample/SampleActivity.java +++ b/app/src/main/java/com/joey/xwebassistant/sample/SampleActivity.java @@ -1,14 +1,21 @@ package com.joey.xwebassistant.sample; import android.annotation.TargetApi; +import android.net.Uri; import android.os.Build; import android.os.Bundle; import android.support.v7.app.ActionBar; import android.support.v7.app.AppCompatActivity; +import android.text.TextUtils; +import android.view.View; import android.webkit.WebSettings; +import android.widget.Button; import android.widget.EditText; import com.joey.xwebassistant.sample.JavaMethod.JSToast; +import com.joey.xwebassistant.sample.JavaMethod.JSToastAuthorized; +import com.joey.xwebassistant.sample.JavaMethod.JSToastPrivate; +import com.joey.xwebassistant.sample.JavaMethod.JSToastPublic; import com.joey.xwebview.XWebView; import com.joey.xwebview.jsbridge.JSBridgeRegister; import com.joey.xwebview.ui.IWebTitle; @@ -18,6 +25,10 @@ public class SampleActivity extends AppCompatActivity implements IWebTitle { private XWebView webView; private EditText etUrl; private EditText etJs; + private String authorized; + private String whiteList; + private Button btnWhiteList; + private Button btnAuthorized; @TargetApi(Build.VERSION_CODES.LOLLIPOP) @Override @@ -31,19 +42,55 @@ protected void onCreate(Bundle savedInstanceState) { .setWebTitleEnable(this) .setCacheMode(WebSettings.LOAD_NO_CACHE) .setProgressEnable(findViewById(R.id.progress_bar)) - .setJSBridgeUrlEnabled(register(), new JSBridgeUrlParser()); + .setJSBridgeUrlEnabled(register(), new JSBridgeUrlParser()) + .setJSBridgeAuthorizedChecker(this::isAuthorized); findViewById(R.id.btn_load).setOnClickListener(v -> webView.loadUrl(etUrl.getText().toString())); findViewById(R.id.btn_input).setOnClickListener(v-> webView.invokeJavaScript("msg", etJs.getText().toString())); + btnWhiteList = findViewById(R.id.btn_whitelist); + btnWhiteList.setOnClickListener(this::addWhiteList); + btnAuthorized = findViewById(R.id.btn_authorized); + btnAuthorized.setOnClickListener(this::authorized); + } + + private void authorized(View view) { + if (TextUtils.isEmpty(authorized)) { + authorized = Uri.parse(etUrl.getText().toString()).getHost(); + btnAuthorized.setText("UnAuthorize"); + } else { + authorized = null; + btnAuthorized.setText("Authorize"); + } + } + + private void addWhiteList(View view) { + if (TextUtils.isEmpty(whiteList)) { + whiteList = Uri.parse(etUrl.getText().toString()).getHost(); + btnWhiteList.setText("remove whitelist"); + } else { + whiteList = null; + btnWhiteList.setText("add whitelist"); + } + webView.setJSBridgeUrlEnabled(register(), new JSBridgeUrlParser()); + } + + private boolean isAuthorized(String javafunc, String url) { + return TextUtils.equals(authorized, Uri.parse(url).getHost()); } private JSBridgeRegister register() { - return JSBridgeRegister.create() - .register("toast", JSToast.class) + JSBridgeRegister register = JSBridgeRegister.create() + .register("toast_public", JSToastPublic.class) + .register("toast_private", JSToastPrivate.class) + .register("toast_authorized", JSToastAuthorized.class) .setMethodInitializer((func, method) -> { if (method instanceof JSToast) ((JSToast) method).setContext(SampleActivity.this); }); + if (!TextUtils.isEmpty(whiteList)) { + register.whiteList(whiteList); + } + return register; } @Override diff --git a/app/src/main/res/layout/activity_sample.xml b/app/src/main/res/layout/activity_sample.xml index d47e5ea..f1bd045 100644 --- a/app/src/main/res/layout/activity_sample.xml +++ b/app/src/main/res/layout/activity_sample.xml @@ -21,6 +21,22 @@ android:layout_height="wrap_content" /> + +