Skip to content

Commit

Permalink
add permission test sample
Browse files Browse the repository at this point in the history
  • Loading branch information
JoeSteven committed Aug 21, 2018
1 parent df4f6fd commit 3bd58d9
Show file tree
Hide file tree
Showing 6 changed files with 151 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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
Expand All @@ -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
Expand Down
16 changes: 16 additions & 0 deletions app/src/main/res/layout/activity_sample.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,22 @@
android:layout_height="wrap_content" />
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/btn_whitelist"
android:text="Add whitelist"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content" />
<Button
android:layout_weight="1"
android:id="@+id/btn_authorized"
android:text="authorize"
android:layout_width="0dp"
android:layout_height="wrap_content" />
</LinearLayout>
<com.joey.xwebview.ui.XWebProgressBar
android:id="@+id/progress_bar"
android:visibility="gone"
Expand Down

0 comments on commit 3bd58d9

Please sign in to comment.