package csdk.glucentralservices.webview; import android.R; import android.app.Activity; import android.app.Dialog; import android.content.DialogInterface; import android.graphics.drawable.ColorDrawable; import android.text.TextUtils; import android.webkit.WebResourceError; import android.webkit.WebResourceRequest; import androidx.annotation.RequiresApi; import com.google.firebase.perf.network.FirebasePerfUrlConnection; import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.net.URL; import java.util.concurrent.atomic.AtomicReference; import org.json.JSONObject; /* loaded from: classes4.dex */ public class WebView { private final Activity mActivity; private JSONObject mConfig; private Dialog mDialog; private AtomicReference mListener = new AtomicReference<>(NullWebViewListener.INSTANCE); private android.webkit.WebView mWebView; public class WebViewClient extends android.webkit.WebViewClient { private WebViewClient() { } @Override // android.webkit.WebViewClient public void onLoadResource(android.webkit.WebView webView, String str) { super.onLoadResource(webView, str); } @Override // android.webkit.WebViewClient @RequiresApi(api = 24) public boolean shouldOverrideUrlLoading(android.webkit.WebView webView, WebResourceRequest webResourceRequest) { return shouldOverrideUrlLoading(webView, webResourceRequest.getUrl().toString()); } @Override // android.webkit.WebViewClient public boolean shouldOverrideUrlLoading(android.webkit.WebView webView, String str) { String optString = WebView.this.mConfig.optString("scheme", ""); if (optString.length() <= 0 || !str.startsWith(optString)) { return false; } WebView.this.onDismissed(str); return true; } @Override // android.webkit.WebViewClient public void onPageFinished(android.webkit.WebView webView, String str) { super.onPageFinished(webView, str); WebView.this.onLoaded(null); } @Override // android.webkit.WebViewClient @RequiresApi(api = 23) public void onReceivedError(android.webkit.WebView webView, WebResourceRequest webResourceRequest, WebResourceError webResourceError) { onReceivedError(webView, webResourceError.getErrorCode(), webResourceError.getDescription().toString(), webResourceRequest.getUrl().toString()); } @Override // android.webkit.WebViewClient public void onReceivedError(android.webkit.WebView webView, int i, String str, String str2) { String optString = WebView.this.mConfig.optString("largeFileFilter", ""); if (optString == null || optString.length() == 0 || !str2.contains(optString)) { System.out.println("WebViewClient::onReceivedError: " + str2); WebView.this.onLoaded(new Exception(str)); } } } public WebView(Activity activity) { this.mActivity = activity; } public void show(String str, String str2, IWebViewListener iWebViewListener) { AtomicReference atomicReference = this.mListener; if (iWebViewListener == null) { iWebViewListener = NullWebViewListener.INSTANCE; } atomicReference.set(iWebViewListener); showDialog(); load(str, str2); } private void load(String str, String str2) { InputStream openStream; try { this.mConfig = new JSONObject(str2); if (TextUtils.isEmpty(str)) { onLoaded(new Exception("url is not set")); return; } if (str.startsWith("http")) { this.mWebView.loadUrl(str); return; } InputStream inputStream = null; try { if (str.startsWith("file:///android_asset")) { openStream = this.mActivity.getAssets().open(str.substring(str.lastIndexOf("/") + 1)); } else { openStream = FirebasePerfUrlConnection.openStream(new URL(str)); } inputStream = openStream; ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); byte[] bArr = new byte[1024]; while (true) { int read = inputStream.read(bArr); if (read != -1) { byteArrayOutputStream.write(bArr, 0, read); } else { String byteArrayOutputStream2 = byteArrayOutputStream.toString("UTF-8"); inputStream.close(); this.mWebView.loadDataWithBaseURL("", byteArrayOutputStream2, "text/html", "UTF-8", null); return; } } } catch (Throwable th) { inputStream.close(); throw th; } } catch (Exception e) { onLoaded(e); } } private void showDialog() { createWebView(); Dialog dialog = new Dialog(this.mActivity, R.style.Theme.Translucent.NoTitleBar); this.mDialog = dialog; dialog.setContentView(this.mWebView); this.mDialog.setCancelable(true); this.mDialog.getWindow().setLayout(-1, -1); this.mDialog.getWindow().setFlags(8, 8); this.mDialog.getWindow().getDecorView().setSystemUiVisibility(4870); this.mDialog.getWindow().setBackgroundDrawable(new ColorDrawable(0)); this.mDialog.setOnShowListener(new DialogInterface.OnShowListener() { // from class: csdk.glucentralservices.webview.WebView.1 @Override // android.content.DialogInterface.OnShowListener public void onShow(DialogInterface dialogInterface) { } }); this.mDialog.setOnCancelListener(new DialogInterface.OnCancelListener() { // from class: csdk.glucentralservices.webview.WebView.2 @Override // android.content.DialogInterface.OnCancelListener public void onCancel(DialogInterface dialogInterface) { WebView.this.onStatusReceived("gluact:dismiss", null); } }); this.mDialog.show(); if (!this.mDialog.isShowing()) { onLoaded(new Exception("Dialog of WebView could not be displayed.")); } this.mDialog.getWindow().clearFlags(8); } private void createWebView() { android.webkit.WebView webView = new android.webkit.WebView(this.mActivity); this.mWebView = webView; webView.setBackgroundColor(0); this.mWebView.getSettings().setJavaScriptEnabled(true); this.mWebView.setWebViewClient(new WebViewClient()); } /* JADX INFO: Access modifiers changed from: private */ public void onDismissed(String str) { this.mDialog.dismiss(); onStatusReceived(str, null); } /* JADX INFO: Access modifiers changed from: private */ public void onLoaded(Throwable th) { if (th == null) { runScript(); } else { onStatusReceived(null, th); } } /* JADX INFO: Access modifiers changed from: private */ public void onStatusReceived(String str, Throwable th) { this.mListener.get().onStatusReceived(str, th); this.mListener.set(NullWebViewListener.INSTANCE); } private void runScript() { String optString = this.mConfig.optString("script", ""); if (optString == null || optString.length() == 0) { return; } this.mWebView.evaluateJavascript(String.format("setTimeout(function(){%s;}, 1);", optString), null); } }