package com.apkstudio.appd77d4cbcc9e24c16a7eed07b0ac8781f;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ActivityNotFoundException;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.WindowInsets;
import android.webkit.CookieManager;
import android.webkit.DownloadListener;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.Toast;

/* JADX INFO: loaded from: classes2.dex */
public class MainActivity extends Activity {
    private final String home = "https://www.crunchyroll.com/zh-tw";
    private boolean returningFromPicker;
    private ValueCallback<Uri[]> upload;
    private WebView web;

    @Override // android.app.Activity
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        LinearLayout linearLayout = new LinearLayout(this);
        linearLayout.setOrientation(1);
        linearLayout.setBackgroundColor(-1);
        linearLayout.setOnApplyWindowInsetsListener(new View.OnApplyWindowInsetsListener() { // from class: com.apkstudio.appd77d4cbcc9e24c16a7eed07b0ac8781f.MainActivity$$ExternalSyntheticLambda0
            @Override // android.view.View.OnApplyWindowInsetsListener
            public final WindowInsets onApplyWindowInsets(View view, WindowInsets windowInsets) {
                return MainActivity.lambda$onCreate$0(view, windowInsets);
            }
        });
        final ProgressBar progressBar = new ProgressBar(this, null, android.R.attr.progressBarStyleHorizontal);
        progressBar.setMax(100);
        linearLayout.addView(progressBar, new LinearLayout.LayoutParams(-1, 6));
        this.web = new WebView(this);
        linearLayout.addView(this.web, new LinearLayout.LayoutParams(-1, 0, 1.0f));
        setContentView(linearLayout);
        WebSettings settings = this.web.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setDomStorageEnabled(true);
        settings.setAllowFileAccess(false);
        settings.setAllowContentAccess(false);
        settings.setMixedContentMode(1);
        settings.setUseWideViewPort(true);
        settings.setLoadWithOverviewMode(true);
        CookieManager.getInstance().setAcceptCookie(true);
        this.web.setWebViewClient(new AnonymousClass1());
        this.web.setWebChromeClient(new WebChromeClient() { // from class: com.apkstudio.appd77d4cbcc9e24c16a7eed07b0ac8781f.MainActivity.2
            @Override // android.webkit.WebChromeClient
            public void onProgressChanged(WebView webView, int i) {
                progressBar.setProgress(i);
                progressBar.setVisibility(i == 100 ? 8 : 0);
            }

            @Override // android.webkit.WebChromeClient
            public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> valueCallback, WebChromeClient.FileChooserParams fileChooserParams) {
                if (MainActivity.this.upload != null) {
                    MainActivity.this.upload.onReceiveValue(null);
                }
                MainActivity.this.upload = valueCallback;
                try {
                    MainActivity.this.startActivityForResult(fileChooserParams.createIntent(), 100);
                    return true;
                } catch (ActivityNotFoundException e) {
                    MainActivity.this.upload.onReceiveValue(null);
                    MainActivity.this.upload = null;
                    return true;
                }
            }
        });
        this.web.setDownloadListener(new DownloadListener() { // from class: com.apkstudio.appd77d4cbcc9e24c16a7eed07b0ac8781f.MainActivity$$ExternalSyntheticLambda1
            @Override // android.webkit.DownloadListener
            public final void onDownloadStart(String str, String str2, String str3, String str4, long j) {
                this.f$0.m12xed3a4f28(str, str2, str3, str4, j);
            }
        });
        if (bundle == null) {
            this.web.loadUrl("https://www.crunchyroll.com/zh-tw");
        } else {
            this.web.restoreState(bundle);
        }
    }

    static /* synthetic */ WindowInsets lambda$onCreate$0(View view, WindowInsets windowInsets) {
        view.setPadding(windowInsets.getSystemWindowInsetLeft(), windowInsets.getSystemWindowInsetTop(), windowInsets.getSystemWindowInsetRight(), windowInsets.getSystemWindowInsetBottom());
        return windowInsets;
    }

    private /* synthetic */ void lambda$onCreate$1(View view) {
        onBackPressed();
    }

    private /* synthetic */ void lambda$onCreate$2(View view) {
        this.web.reload();
    }

    /* JADX INFO: renamed from: com.apkstudio.appd77d4cbcc9e24c16a7eed07b0ac8781f.MainActivity$1, reason: invalid class name */
    class AnonymousClass1 extends WebViewClient {
        AnonymousClass1() {
        }

        @Override // android.webkit.WebViewClient
        public boolean shouldOverrideUrlLoading(WebView webView, WebResourceRequest webResourceRequest) {
            String scheme = webResourceRequest.getUrl().getScheme();
            if ("https".equals(scheme) || "http".equals(scheme)) {
                return false;
            }
            if (!webResourceRequest.isForMainFrame() || !webResourceRequest.hasGesture()) {
                return true;
            }
            if (!"tel".equals(scheme) && !"mailto".equals(scheme)) {
                return true;
            }
            MainActivity.this.openExternal(webResourceRequest.getUrl());
            return true;
        }

        /* JADX INFO: renamed from: lambda$onReceivedError$0$com-apkstudio-appd77d4cbcc9e24c16a7eed07b0ac8781f-MainActivity$1, reason: not valid java name */
        /* synthetic */ void m13x63b7ae45(DialogInterface dialogInterface, int i) {
            MainActivity.this.web.loadUrl("https://www.crunchyroll.com/zh-tw");
        }

        @Override // android.webkit.WebViewClient
        public void onReceivedError(WebView webView, WebResourceRequest webResourceRequest, WebResourceError webResourceError) {
            if (webResourceRequest.isForMainFrame()) {
                new AlertDialog.Builder(MainActivity.this).setTitle("网页暂时无法打开").setMessage("请检查网络连接，然后重试。").setPositiveButton("重试", new DialogInterface.OnClickListener() { // from class: com.apkstudio.appd77d4cbcc9e24c16a7eed07b0ac8781f.MainActivity$1$$ExternalSyntheticLambda0
                    @Override // android.content.DialogInterface.OnClickListener
                    public final void onClick(DialogInterface dialogInterface, int i) {
                        this.f$0.m13x63b7ae45(dialogInterface, i);
                    }
                }).setNegativeButton("取消", (DialogInterface.OnClickListener) null).show();
            }
        }
    }

    /* JADX INFO: renamed from: lambda$onCreate$3$com-apkstudio-appd77d4cbcc9e24c16a7eed07b0ac8781f-MainActivity, reason: not valid java name */
    /* synthetic */ void m12xed3a4f28(String str, String str2, String str3, String str4, long j) {
        Uri uri = Uri.parse(str);
        if ("https".equals(uri.getScheme()) || "http".equals(uri.getScheme())) {
            openExternal(uri);
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void openExternal(Uri uri) {
        try {
            startActivity(new Intent("android.intent.action.VIEW", uri));
        } catch (ActivityNotFoundException e) {
            Toast.makeText(this, "没有可打开此链接的应用", 0).show();
        }
    }

    @Override // android.app.Activity
    protected void onActivityResult(int i, int i2, Intent intent) {
        super.onActivityResult(i, i2, intent);
        if (i != 100 || this.upload == null) {
            return;
        }
        this.upload.onReceiveValue(WebChromeClient.FileChooserParams.parseResult(i2, intent));
        this.upload = null;
    }

    @Override // android.app.Activity
    protected void onSaveInstanceState(Bundle bundle) {
        super.onSaveInstanceState(bundle);
        this.web.saveState(bundle);
    }

    @Override // android.app.Activity
    public void onBackPressed() {
        if (this.web.canGoBack()) {
            this.web.goBack();
        } else {
            super.onBackPressed();
        }
    }

    @Override // android.app.Activity
    protected void onPause() {
        this.web.onPause();
        CookieManager.getInstance().flush();
        super.onPause();
    }

    @Override // android.app.Activity
    protected void onStart() {
        super.onStart();
        OpenStats.started(this, this.returningFromPicker);
        this.returningFromPicker = false;
    }

    @Override // android.app.Activity
    protected void onStop() {
        this.returningFromPicker = this.upload != null;
        OpenStats.stopped(isChangingConfigurations());
        super.onStop();
    }

    @Override // android.app.Activity
    protected void onResume() {
        super.onResume();
        if (this.web != null) {
            this.web.onResume();
        }
    }

    @Override // android.app.Activity
    protected void onDestroy() {
        if (this.upload != null) {
            this.upload.onReceiveValue(null);
        }
        this.web.destroy();
        super.onDestroy();
    }
}
