Open ccrazycoder opened 7 years ago
You have to create your custom classes that extend FinestWebView classes. You're welcome to use mine:
package com.peykfood.android.util;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.webkit.JavascriptInterface;
import androidx.annotation.NonNull;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import com.peykfood.android.WebActivity;
import com.thefinestartist.finestwebview.FinestWebView;
import com.thefinestartist.finestwebview.FinestWebViewActivity;
import com.thefinestartist.finestwebview.listeners.BroadCastManager;
import java.io.Serializable;
public class MyFinestWebView extends FinestWebView {
public static class Builder extends FinestWebView.Builder{
public static final String INTENT_NEW_JS_INTERFACE = "NEW_JS_INTERFACE";
public Builder(@NonNull Activity activity) {
super(activity);
}
public Builder(@NonNull Context context) {
super(context);
}
protected void show(String url, String data) {
this.url = url;
this.data = data;
this.key = System.identityHashCode(this);
if (!listeners.isEmpty()) new BroadCastManager(context, key, listeners);
Intent intent = new Intent(context, WebActivity.class);
intent.putExtra("builder", this);
this.context.startActivity(intent);
if (context instanceof Activity)
((Activity) context).overridePendingTransition(animationOpenEnter, animationOpenExit);
}
public void addJSInterface(JavascriptInterface javascriptInterface){
Intent intent = new Intent(INTENT_NEW_JS_INTERFACE);
intent.putExtra("interface", (Serializable)javascriptInterface);
LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
}
}
}
public class WebActivity extends com.thefinestartist.finestwebview.FinestWebViewActivity {
public WebView getWebView() {
return this.webView;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState, @Nullable PersistableBundle persistentState) {
super.onCreate(savedInstanceState, persistentState);
BroadcastReceiver receiver = new BroadcastReceiver() {
@SuppressLint("JavascriptInterface")
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(MyFinestWebView.Builder.INTENT_NEW_JS_INTERFACE)) {
webView.addJavascriptInterface((JavascriptInterface) intent.getSerializableExtra("interface"), "interface");
}
}
};
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(MyFinestWebView.Builder.INTENT_NEW_JS_INTERFACE);
LocalBroadcastManager.getInstance(getBaseContext()).registerReceiver(receiver, intentFilter);
}
}
Usage:
MyFinestWebView.Builder builder = new MyFinestWebView.Builder(mActivity);
builder.addJSInterface(new ApadanaInterface(mContext));
builder
.toolbarScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL | AppBarLayout.LayoutParams.SCROLL_FLAG_ENTER_ALWAYS)
.gradientDivider(true)
.dividerHeight(100)
.backPressToClose(false)
.setCustomAnimations(R.anim.activity_open_enter, R.anim.activity_open_exit, R.anim.activity_close_enter, R.anim.activity_close_exit);
builder.show("https://google.com");
It would be better if you add support to add JSInterface callback. As in my requirement, I need that to get back values from Webpage.