package com.weikou.beibeivideo.util.browser; import android.app.Activity; import android.content.Intent; import android.content.pm.PackageManager; import android.support.v4.content.ContextCompat; import android.webkit.JavascriptInterface; import android.widget.ImageView; import android.widget.TextView; import com.lcjian.library.util.common.StringUtils; import com.tencent.smtt.sdk.WebView; import com.weikou.beibeivideo.util.AlibcTradeUtil; import com.weikou.beibeivideo.util.JumpActivityUtil; import org.json.JSONException; import org.json.JSONObject; import java.util.Iterator; public class BWJavaInterface extends BaseBWJavaInterface { Activity mContext; TextView tv_top_bar_middle, tv_top_bar_left2, tv_top_bar_right; ImageView iv_right; WebView webview; private boolean boo = false; public BWJavaInterface(Activity activity, WebView webview) { super(activity); mContext = activity; this.webview = webview; boo = true; } public BWJavaInterface(Activity activity, TextView tv_top_bar_middle , TextView tv_top_bar_left2, TextView tv_top_bar_right , ImageView iv_right, WebView webview) { super(activity); mContext = activity; this.tv_top_bar_middle = tv_top_bar_middle; this.tv_top_bar_left2 = tv_top_bar_left2; this.tv_top_bar_right = tv_top_bar_right; this.iv_right = iv_right; this.webview = webview; } @JavascriptInterface public void setTitle(final String title) { // tv_top_bar_middle.setText(title); if (!boo) tv_top_bar_middle.post(new Runnable() { @Override public void run() { if (null != title && !StringUtils.isEmpty(title.trim())) tv_top_bar_middle.setText(title); } }); } @JavascriptInterface public void jumpPageWithFinishCurrentPage(String pageClassName, String paramJson) { Intent intent = null; JSONObject param = null; try { if (StringUtils.isEmpty(paramJson)) { param = null; } else { param = new JSONObject(paramJson); } if (StringUtils.isEmpty(pageClassName)) { return; } else { intent = new Intent(mContext, Class.forName(JumpActivityUtil.filterActivityName(pageClassName))); } } catch (JSONException e) { param = null; e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } if (null != param) { @SuppressWarnings("unchecked") Iterator its = param.keys(); while (its.hasNext()) { String key = its.next(); String value = param.optString(key); intent.putExtra(key, value); } } mContext.startActivity(intent); if (!boo) mContext.finish(); } @JavascriptInterface public void finishPage() { if (!boo) iv_right.post(new Runnable() { @Override public void run() { mContext.finish(); } }); } /** * 淘宝授权 * * @param url */ @JavascriptInterface public void tbAuth(String url) { if (StringUtils.isEmpty(url)) return; AlibcTradeUtil.openAuthLink(mContext, url); } // 判断权限集合 是否授权 false授权 true未授权 public boolean lacksPermissions(String... permissions) { for (String permission : permissions) { if (lacksPermission(permission)) { return true; } } return false; } // 判断是否缺少权限 private boolean lacksPermission(String permission) { //权限未授权 return ContextCompat.checkSelfPermission(mContext, permission) == PackageManager.PERMISSION_DENIED; } }