觅伊 v4.1.7版本的 MD5 值为:2a9d9e87c53524c098d85b78a90aa398
以下内容为反编译后的 KitWebViewClient.java 源代码,内容仅作参考
package com.imacapp.home.web;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.net.Uri;
import android.net.http.SslError;
import android.os.Build;
import android.text.TextUtils;
import android.util.Log;
import android.webkit.CookieManager;
import android.webkit.CookieSyncManager;
import android.webkit.SslErrorHandler;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebResourceResponse;
import android.webkit.WebView;
import com.bumptech.glide.Glide;
import com.google.android.exoplayer2.text.ttml.TtmlNode;
import com.huawei.hms.framework.common.ContainerUtils;
import com.just.agentweb.DefaultWebClient;
import com.just.agentweb.WebViewClient;
import com.tencent.mmkv.MMKV;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.Map;
public class KitWebViewClient extends WebViewClient {
public static final String KEY_WEB_COOKIE = "key_web_cookie";
Context mContext;
WebView webView;
public KitWebViewClient(WebView webView, Context mContext) {
this.webView = webView;
this.mContext = mContext;
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
injectCookies(url);
}
public static void synCookies(Context context, String url, Map<String, String> map) {
CookieSyncManager.createInstance(context);
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.setAcceptCookie(true);
for (String str : map.keySet()) {
cookieManager.setCookie(url, str + ContainerUtils.KEY_VALUE_DELIMITER + map.get(str));
}
CookieSyncManager.getInstance().sync();
}
private void injectCookies(String url) {
String domain = getDomain(url);
MMKV defaultMMKV = MMKV.defaultMMKV();
HashMap hashMap = new HashMap();
String decodeString = defaultMMKV.decodeString(KEY_WEB_COOKIE + domain);
if (!TextUtils.isEmpty(decodeString)) {
String[] split = decodeString.split(";");
for (int i = 0; i < split.length; i++) {
int indexOf = split[i].indexOf(ContainerUtils.KEY_VALUE_DELIMITER);
if (indexOf != -1) {
hashMap.put(split[i].substring(0, indexOf), split[i].substring(indexOf + 1));
}
}
}
synCookies(this.mContext, domain, hashMap);
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
saveCookies(url);
}
private void saveCookies(String url) {
CookieManager cookieManager = CookieManager.getInstance();
String domain = getDomain(url);
String cookie = cookieManager.getCookie(domain);
MMKV defaultMMKV = MMKV.defaultMMKV();
defaultMMKV.encode(KEY_WEB_COOKIE + domain, cookie);
defaultMMKV.commit();
}
private String getDomain(String url) {
String replace = url.replace(DefaultWebClient.HTTP_SCHEME, "").replace(DefaultWebClient.HTTPS_SCHEME, "");
return replace.contains("/") ? replace.substring(0, replace.indexOf(47)) : replace;
}
@Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
if (request.isForMainFrame()) {
view.loadUrl("javascript:document.body.innerHTML=\"" + ("Page NO FOUND!errorCode :" + error.getErrorCode() + "-----errorData:" + error.getDescription().toString()) + "\"");
}
}
@Override
public void onReceivedHttpError(WebView view, WebResourceRequest request, WebResourceResponse errorResponse) {
if (request.isForMainFrame()) {
view.loadUrl("javascript:document.body.innerHTML=\"" + ("Page NO FOUND!errorCode :" + errorResponse.getStatusCode()) + "\"");
}
}
@Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
handler.proceed();
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
return super.shouldOverrideUrlLoading(view, request);
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
WebView.HitTestResult hitTestResult = view.getHitTestResult();
if (!TextUtils.isEmpty(url) && hitTestResult == null) {
view.loadUrl(url);
return true;
} else if (!url.startsWith("http") || url.contains(".apk")) {
handleThirdApp(this.mContext, url);
return true;
} else {
return false;
}
}
@Override
public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
String guessContentTypeFromName;
try {
guessContentTypeFromName = URLConnection.guessContentTypeFromName(request.getUrl().toString());
} catch (Exception e) {
e.printStackTrace();
}
if (guessContentTypeFromName != null && guessContentTypeFromName.startsWith(TtmlNode.TAG_IMAGE)) {
if (guessContentTypeFromName.contains("jpeg") || guessContentTypeFromName.contains("png") || guessContentTypeFromName.contains("webp")) {
Bitmap bitmap = Glide.with(this.mContext).asBitmap().load(request.getUrl().toString()).submit().get();
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
if (guessContentTypeFromName.endsWith("jpeg")) {
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream);
} else if (guessContentTypeFromName.endsWith("png")) {
bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
} else if (guessContentTypeFromName.endsWith("webp")) {
bitmap.compress(Bitmap.CompressFormat.WEBP, 100, byteArrayOutputStream);
} else {
return super.shouldInterceptRequest(view, request);
}
return new WebResourceResponse(guessContentTypeFromName, "UTF-8", new ByteArrayInputStream(byteArrayOutputStream.toByteArray()));
}
return super.shouldInterceptRequest(view, request);
}
return super.shouldInterceptRequest(view, request);
}
@Override
public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
return super.shouldInterceptRequest(view, url);
}
public boolean parseScheme(String url) {
if (url.toLowerCase().contains("platformapi/startapp")) {
return true;
}
return Build.VERSION.SDK_INT > 23 && url.toLowerCase().contains("platformapi") && url.toLowerCase().contains("startapp");
}
public static boolean hasPackage(Context context, String packageName) {
if (context != null && !TextUtils.isEmpty(packageName)) {
try {
context.getPackageManager().getPackageInfo(packageName, 256);
return true;
} catch (PackageManager.NameNotFoundException unused) {
}
}
return false;
}
public static void handleThirdApp(Context context, String url) {
String str;
if (url.contains("alipays")) {
str = "com.eg.android.AlipayGphone";
} else if (url.contains("weixin://wap/pay")) {
str = "com.tencent.mm";
} else if (url.contains("openapp.jdmobile")) {
str = "com.jingdong.app.mall";
} else {
startActivity(context, url);
str = "";
}
if (hasPackage(context, str)) {
startActivity(context, url);
}
}
private static void startActivity(Context context, String url) {
try {
if (url.startsWith("will://")) {
Uri parse = Uri.parse(url);
Log.e("---------scheme", parse.getScheme() + ";host: " + parse.getHost() + ";Id: " + parse.getPathSegments().get(0));
}
Intent intent = new Intent();
intent.setAction("android.intent.action.VIEW");
intent.setData(Uri.parse(url));
intent.addFlags(268435456);
context.startActivity(intent);
} catch (Exception e) {
e.printStackTrace();
}
}
}