package X; import android.net.Uri; import android.os.Build; import android.os.Handler; import android.os.Looper; import android.os.Message; import android.text.TextUtils; import android.webkit.WebChromeClient; import android.webkit.WebSettings; import android.webkit.WebView; import android.webkit.WebViewClient; import com.bytedance.covode.number.Covode; import java.util.HashMap; import java.util.List; import java.util.Map; import org.json.JSONArray; import org.json.JSONObject; /* renamed from: X.197 reason: invalid class name */ public class AnonymousClass197 implements AbstractC08670Wq { public String LIZ = "_fetchQueue"; public String LIZIZ = "_handleMessageFromToutiao"; public String LIZJ = "javascript:ToutiaoJSBridge"; public WebView LIZLLL; public String LJ; public List<String> LJFF; public List<String> LJI; public List<String> LJII; public Map<String, AbstractC08660Wp> LJIIIIZZ = new HashMap(); public E9A LJIIIZ; public boolean LJIIJ; public AbstractC48747JCe LJIIJJI; public Handler LJIIL = new HandlerC65922ij(this, Looper.getMainLooper()); static { Covode.recordClassIndex(25904); } public AnonymousClass197(WebView webView) { this.LIZLLL = webView; if (webView != null) { if ((webView instanceof EB1) && !((EB1) webView).LIZ) { this.LIZLLL.setWebChromeClient(new WebChromeClient()); } try { this.LIZLLL.getSettings().setJavaScriptEnabled(true); } catch (Exception unused) { } } } public static AnonymousClass197 LIZ(WebView webView) { return new AnonymousClass197(webView); } private void LIZ(JSONObject jSONObject) { WebView webView = this.LIZLLL; if (webView != null) { C37282EkZ.LIZ(webView, this.LIZJ + "." + this.LIZIZ + "(" + jSONObject.toString() + ")"); } } public final AnonymousClass197 LIZ(WebChromeClient webChromeClient) { WebView webView = this.LIZLLL; if (webView != null) { webView.setWebChromeClient(webChromeClient); } return this; } public final AnonymousClass197 LIZ(WebViewClient webViewClient) { if (this.LIZLLL == null) { return this; } if (webViewClient instanceof C48249Ix4) { ((C48249Ix4) webViewClient).LIZLLL = this; } WebView webView = this.LIZLLL; if (C21290sw.LIZIZ.LIZ() && webViewClient != null) { WebSettings settings = webView.getSettings(); String userAgentString = settings.getUserAgentString(); if (!userAgentString.contains("BytedanceWebview/d8a21c6")) { StringBuilder sb = new StringBuilder(userAgentString); if (!TextUtils.isEmpty(sb)) { sb.append(" "); } sb.append("BytedanceWebview/d8a21c6"); settings.setUserAgentString(sb.toString()); } } webView.setWebViewClient(C45324Hqz.LIZ(webViewClient)); return this; } public final AnonymousClass197 LIZ(String str) { this.LJ = str; return this; } public final AnonymousClass197 LIZ(String str, AbstractC08660Wp r3) { if (!TextUtils.isEmpty(str) && r3 != null) { this.LJIIIIZZ.put(str, r3); } return this; } public final AnonymousClass197 LIZ(List<String> list) { this.LJFF = list; return this; } public final void LIZ() { this.LIZLLL = null; Handler handler = this.LJIIL; if (handler != null) { handler.removeCallbacksAndMessages(null); } this.LJIIIZ = null; this.LJIIIIZZ = null; } public final void LIZ(String str, String str2, JSONObject jSONObject) { try { JSONObject jSONObject2 = new JSONObject(); jSONObject2.put("__msg_type", "callback"); jSONObject2.put("__callback_id", str2); if (jSONObject != null) { jSONObject2.put("__params", jSONObject); } WebView webView = this.LIZLLL; if (webView != null) { C37282EkZ.LIZ(webView, AnonymousClass0EJ.LIZ("javascript:(function(){ const iframe = document.querySelector('iframe[src=\"%s\"'); if (iframe && iframe.contentWindow) { iframe.contentWindow.postMessage(%s, '%s'); }})()", new Object[]{str, jSONObject2.toString(), str})); } } catch (Exception unused) { } } public final void LIZ(String str, JSONObject jSONObject) { AbstractC48747JCe jCe = this.LJIIJJI; if (jCe == null || jCe.LIZ(str, jSONObject, 1) == null) { try { JSONObject jSONObject2 = new JSONObject(); jSONObject2.put("__msg_type", "callback"); jSONObject2.put("__callback_id", str); if (jSONObject != null) { jSONObject2.put("__params", jSONObject); } LIZ(jSONObject2); } catch (Exception unused) { } } } @Override // X.AbstractC08670Wq public final void LIZ(List<String> list, C65932ik r4, JSONObject jSONObject) { this.LJII = list; if (!TextUtils.isEmpty(r4.LJI)) { LIZ(r4.LJI, r4.LIZIZ, jSONObject); } else { LIZ(r4.LIZIZ, jSONObject); } } public final AnonymousClass197 LIZIZ(List<String> list) { this.LJI = list; return this; } public final void LIZIZ(String str, JSONObject jSONObject) { AbstractC48747JCe jCe = this.LJIIJJI; if (jCe == null || jCe.LIZ(str, jSONObject, 2) == null) { try { if (!TextUtils.isEmpty(str)) { JSONObject jSONObject2 = new JSONObject(); jSONObject2.put("__msg_type", C59681Nby.LJIIIIZZ); jSONObject2.put("__event_id", str); if (jSONObject != null) { jSONObject2.put("__params", jSONObject); } LIZ(jSONObject2); } } catch (Exception unused) { } } } public final boolean LIZIZ(String str) { if (!TextUtils.isEmpty(str) && !TextUtils.isEmpty(this.LJ)) { return this.LJ.equals(Uri.parse(str).getScheme().toLowerCase()) && LIZJ(str); } } public final AnonymousClass197 LIZJ(List<String> list) { this.LJII = list; return this; } public final boolean LIZJ(String str) { if (str == null || !str.startsWith(this.LJ)) { return false; } String str2 = this.LJ + "://private/setresult/"; try { if (str.equals(this.LJ + "://dispatch_message/")) { WebView webView = this.LIZLLL; if (webView != null) { int i2 = Build.VERSION.SDK_INT; webView.evaluateJavascript(this.LIZJ + "." + this.LIZ + "()", new C69782ox(this)); } return true; } if (str.startsWith(str2)) { int i3 = Build.VERSION.SDK_INT; return true; } return false; } catch (Exception unused) { } } public final void LIZLLL(String str) { try { JSONArray jSONArray = new JSONArray(str); int length = jSONArray.length(); for (int i2 = 0; i2 < length; i2++) { JSONObject jSONObject = jSONArray.getJSONObject(i2); C65932ik r3 = new C65932ik(); r3.LIZ = jSONObject.getString("__msg_type"); String str2 = null; r3.LIZIZ = jSONObject.optString("__callback_id", null); r3.LIZJ = jSONObject.optString("func"); r3.LIZLLL = jSONObject.optJSONObject("params"); r3.LJ = jSONObject.optInt("JSSDK"); r3.LJFF = jSONObject.optString("namespace"); r3.LJI = jSONObject.optString("__iframe_url"); if (!TextUtils.isEmpty(r3.LIZ) && !TextUtils.isEmpty(r3.LIZJ)) { E9A e9a = this.LJIIIZ; if (e9a == null || !e9a.LIZ(r3)) { Message obtain = Message.obtain(); obtain.obj = r3; this.LJIIL.sendMessage(obtain); } else { JSONObject jSONObject2 = new JSONObject(); WebView webView = this.LIZLLL; if (webView != null) { str2 = webView.getUrl(); } this.LJIIIZ.LIZ(r3, jSONObject2, str2, this); return; } } } } catch (Exception unused) { } } public final boolean LJ(String str) { List<String> list; int indexOf; if (TextUtils.isEmpty(str) || (!str.startsWith("http://") && !str.startsWith("https://"))) { return false; } try { String host = (Build.VERSION.SDK_INT >= 27 || (indexOf = str.indexOf(92)) == -1) ? Uri.parse(str).getHost() : Uri.parse(str.substring(0, indexOf)).getHost(); if (!(host == null || (list = this.LJFF) == null)) { if (!list.isEmpty()) { for (int i2 = 0; i2 < this.LJFF.size(); i2++) { String str2 = this.LJFF.get(i2); if (!host.equals(str2)) { if (!host.endsWith(".".concat(String.valueOf(str2)))) { } } return true; } } } } catch (Exception unused) { } return false; } }