导航菜单

页面标题

页面副标题

St.John's v1.0.9 - WebViewActivity.java 源代码

正在查看: St.John's v1.0.9 应用的 WebViewActivity.java JAVA 源代码文件

本页面展示 JAVA 反编译生成的源代码文件,支持语法高亮显示。 仅供安全研究与技术分析使用,严禁用于任何非法用途。请遵守相关法律法规。


package com.mcb.stjohnsemschool.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.util.Base64;
import android.view.MenuItem;
import android.view.View;
import android.webkit.DownloadListener;
import android.webkit.JavascriptInterface;
import android.webkit.MimeTypeMap;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.FileProvider;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.firebase.analytics.FirebaseAnalytics;
import com.mcb.stjohnsemschool.utils.TransparentProgressDialog;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.DateFormat;
import java.util.Date;

public class WebViewActivity extends AppCompatActivity {
    private static final String TAG = "com.mcb.stjohnsemschool.activity.WebViewActivity";
    static String base64String = "";
    String downloadPath = "";
    boolean isDownloadVisible;
    Activity mActivity;
    Context mContext;
    FloatingActionButton mDownloadFab;
    private TransparentProgressDialog mProgressbar;
    String title;
    private WebView webView;

    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.activity_webview);
        this.mContext = getApplicationContext();
        this.mActivity = this;
        String stringExtra = getIntent().getStringExtra("URL");
        String stringExtra2 = getIntent().getStringExtra("Title");
        this.isDownloadVisible = false;
        if (getIntent().hasExtra("isDownload")) {
            this.isDownloadVisible = getIntent().getBooleanExtra("isDownload", false);
            this.downloadPath = getIntent().getStringExtra("download_path");
        }
        getSupportActionBar().setTitle(stringExtra2);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        this.mProgressbar = new TransparentProgressDialog(this, R.drawable.spinner_loading_imag);
        this.webView = (WebView) findViewById(2131364517);
        FloatingActionButton floatingActionButton = (FloatingActionButton) findViewById(R.id.download_fab);
        this.mDownloadFab = floatingActionButton;
        floatingActionButton.setVisibility(8);
        this.mDownloadFab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                try {
                    WebViewActivity.this.convertBase64StringToPdfAndStoreIt();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
        this.webView.getSettings().setLoadsImagesAutomatically(true);
        this.webView.getSettings().setJavaScriptEnabled(true);
        this.webView.getSettings().getAllowContentAccess();
        this.webView.getSettings().setDomStorageEnabled(true);
        this.webView.getSettings().setSupportMultipleWindows(true);
        this.webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
        this.webView.getSettings().setAllowFileAccess(false);
        this.webView.getSettings().setBuiltInZoomControls(true);
        this.webView.getSettings().setDisplayZoomControls(false);
        this.webView.getSettings().setLoadWithOverviewMode(true);
        this.webView.getSettings().setUseWideViewPort(true);
        this.webView.getSettings().setSupportZoom(true);
        this.webView.getSettings().setDefaultTextEncodingName("utf-8");
        this.webView.setWebChromeClient(new WebChromeClient());
        this.webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView webView, String str) {
                return false;
            }

            @Override
            public void onPageStarted(WebView webView, String str, Bitmap bitmap) {
                super.onPageStarted(webView, str, bitmap);
            }

            @Override
            public void onPageFinished(WebView webView, String str) {
                super.onPageFinished(webView, str);
                if (WebViewActivity.this.mProgressbar != null && WebViewActivity.this.mProgressbar.isShowing()) {
                    WebViewActivity.this.mProgressbar.dismiss();
                }
                if (WebViewActivity.this.isDownloadVisible) {
                    WebViewActivity.this.mDownloadFab.setVisibility(0);
                }
            }
        });
        this.webView.setScrollBarStyle(0);
        TransparentProgressDialog transparentProgressDialog = this.mProgressbar;
        if (transparentProgressDialog != null && !transparentProgressDialog.isShowing()) {
            this.mProgressbar.show();
        }
        this.webView.setDownloadListener(new DownloadListener() {
            @Override
            public void onDownloadStart(String str, String str2, String str3, String str4, long j) {
                WebViewActivity.this.webView.loadUrl(JavaScriptInterface.getBase64StringFromBlobUrl(str.replace("%3A", ":")));
            }
        });
        this.webView.loadUrl(stringExtra);
    }

    public boolean onOptionsItemSelected(MenuItem menuItem) {
        if (menuItem.getItemId() == 16908332) {
            onBackPressed();
            return true;
        }
        return super.onOptionsItemSelected(menuItem);
    }

    public void onBackPressed() {
        if (this.webView.canGoBack()) {
            this.webView.goBack();
        } else {
            super.onBackPressed();
        }
    }

    protected void onResume() {
        super.onResume();
        String string = getSharedPreferences("", 0).getString("usernamekey", "");
        Bundle bundle = new Bundle();
        bundle.putString("user_name", string);
        FirebaseAnalytics.getInstance(this).logEvent("PAGE_NOTIFICATION_HELP", bundle);
    }

    public static class JavaScriptInterface {
        @JavascriptInterface
        public void getBase64FromBlobData(String str) throws IOException {
            WebViewActivity.base64String = str.replaceFirst("^data:application/pdf;base64,", "");
        }

        public static String getBase64StringFromBlobUrl(String str) {
            if (str.startsWith("blob")) {
                return "javascript: var xhr = new XMLHttpRequest();xhr.open('GET', '" + str + "', true);xhr.setRequestHeader('Access-Control-Allow-Origin','*');xhr.setRequestHeader('Content-type','application/pdf');xhr.responseType = 'blob';xhr.onload = function(e) {    if (this.status == 200) {        var blobPdf = this.response;        var reader = new FileReader();        reader.readAsDataURL(blobPdf);        reader.onloadend = function() {            base64data = reader.result;            app.getBase64FromBlobData(base64data);        }    }};xhr.send();";
            }
            return "javascript: console.log('It is not a Blob URL');";
        }
    }

    public void convertBase64StringToPdfAndStoreIt() throws IOException {
        File file = new File(getApplicationContext().getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS) + "/" + DateFormat.getDateTimeInstance().format(new Date()) + ".pdf");
        byte[] decode = Base64.decode(base64String, 0);
        FileOutputStream fileOutputStream = new FileOutputStream(file, false);
        fileOutputStream.write(decode);
        fileOutputStream.flush();
        if (file.exists()) {
            showPdf(file.getAbsolutePath());
        }
    }

    public void showPdf(String str) {
        try {
            String lowerCase = str.substring(str.lastIndexOf(".") + 1).toLowerCase();
            File file = new File(str);
            MimeTypeMap singleton = MimeTypeMap.getSingleton();
            Intent intent = new Intent("android.intent.action.VIEW");
            String mimeTypeFromExtension = singleton.getMimeTypeFromExtension(lowerCase);
            Uri uriForFile = FileProvider.getUriForFile(this.mContext, this.mContext.getPackageName() + ".fileprovider", file);
            this.mContext.grantUriPermission(this.mContext.getPackageName() + ".fileprovider", uriForFile, 3);
            intent.setFlags(268435457);
            intent.setDataAndType(uriForFile, mimeTypeFromExtension);
            try {
                this.mContext.startActivity(intent);
            } catch (Exception unused) {
                Toast.makeText(this.mContext, "No handler for this type of file./ No file has found", 0).show();
                Toast.makeText(this.mContext, "Please download the Docs supported App from Play store to view the file", 0).show();
            }
        } catch (Exception unused2) {
            Toast.makeText(this.mContext, "Please download the Docs supported App from Play store to view the file./ No file has found", 0).show();
        }
    }
}