导航菜单

页面标题

页面副标题

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

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

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


package com.mcb.stjohnsemschool.activity;

import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import androidx.appcompat.app.AppCompatActivity;
import com.mcb.stjohnsemschool.utils.Constants;
import com.mcb.stjohnsemschool.utils.Filename;
import com.mcb.stjohnsemschool.utils.TransparentProgressDialog;
import com.mcb.stjohnsemschool.utils.Utility;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.Arrays;

public class ViewFileFromUrlActivity extends AppCompatActivity {
    private static final String TAG = "com.mcb.stjohnsemschool.activity.ViewFileFromUrlActivity";
    private boolean allowDownload;
    private int chapterId;
    private MenuItem downloadItem;
    private boolean isLearning;
    private TransparentProgressDialog mProgressbar;
    private int resourceId;
    private int topicId;
    private WebView webView;
    private String subjectGUID = "";
    private String url = "";

    public boolean share(MenuItem menuItem) {
        return true;
    }

    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.activity_webview);
        String stringExtra = getIntent().getStringExtra("Title");
        this.url = getIntent().getStringExtra("URL");
        Bundle extras = getIntent().getExtras();
        this.isLearning = extras.getBoolean("IsLearning", false);
        this.allowDownload = extras.getBoolean("AllowDownload", false);
        if (this.isLearning) {
            this.subjectGUID = extras.getString("SubjectGUID", "");
            this.chapterId = extras.getInt("ChapterId", 0);
            this.topicId = extras.getInt("TopicId", 0);
            this.resourceId = extras.getInt("ResourceId", 0);
        }
        this.mProgressbar = new TransparentProgressDialog(this, R.drawable.spinner_loading_imag);
        getSupportActionBar().setTitle(stringExtra);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        WebView webView = (WebView) findViewById(2131364517);
        this.webView = webView;
        WebSettings settings = webView.getSettings();
        settings.setLoadsImagesAutomatically(true);
        settings.setJavaScriptCanOpenWindowsAutomatically(true);
        settings.setBuiltInZoomControls(true);
        settings.setDomStorageEnabled(true);
        settings.setJavaScriptEnabled(true);
        settings.setLoadWithOverviewMode(true);
        settings.setUseWideViewPort(true);
        this.webView.setInitialScale(1);
        this.webView.setWebViewClient(new WebViewClient());
        this.webView.setWebChromeClient(new WebChromeClient());
        this.webView.setScrollBarStyle(0);
        if (Arrays.asList(Constants.imageExtensions).contains(new Filename(this.url, '/', '.').extension().toLowerCase())) {
            this.webView.loadUrl(this.url);
            return;
        }
        this.webView.loadUrl("https://docs.google.com/gview?embedded=true&url=" + this.url);
    }

    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 onDestroy() {
        if (this.isLearning) {
            Utility.saveFileViewLog(getApplicationContext(), this.subjectGUID, this.chapterId, this.topicId, this.resourceId, 1);
        }
        super.onDestroy();
    }

    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.download_share_main, menu);
        this.downloadItem = menu.findItem(R.id.action_download);
        menu.findItem(R.id.action_share).setVisible(false);
        MenuItem menuItem = this.downloadItem;
        if (menuItem != null) {
            if (this.allowDownload) {
                String str = this.url;
                if (new File(Utility.getDownloadStorageDir(getApplicationContext()) + "/MyClassBoard/" + str.substring(str.lastIndexOf("/") + 1)).exists()) {
                    this.downloadItem.setVisible(false);
                } else {
                    this.downloadItem.setVisible(true);
                }
            } else {
                menuItem.setVisible(false);
            }
        }
        return true;
    }

    public boolean download(MenuItem menuItem) {
        downloadFile();
        return true;
    }

    public void download(View view) {
        downloadFile();
    }

    public void downloadFile() {
        if (Utility.hasNetworkConnection(getApplicationContext())) {
            new DownloadFileAsync(this.url).execute(new String[0]);
        } else {
            Utility.showInfoDialog(this, "Please check your internet connection");
        }
    }

    class DownloadFileAsync extends AsyncTask<String, String, String> {
        private String imageUrl;

        public DownloadFileAsync(String str) {
            this.imageUrl = str;
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            if (ViewFileFromUrlActivity.this.mProgressbar == null || ViewFileFromUrlActivity.this.mProgressbar.isShowing()) {
                return;
            }
            ViewFileFromUrlActivity.this.mProgressbar.show();
        }

        @Override
        public String doInBackground(String... strArr) {
            try {
                try {
                    URL url = new URL(this.imageUrl);
                    URLConnection openConnection = url.openConnection();
                    openConnection.connect();
                    int contentLength = openConnection.getContentLength();
                    File file = new File(Utility.getDownloadStorageDir(ViewFileFromUrlActivity.this.getApplicationContext()) + "/MyClassBoard/");
                    if (!file.exists()) {
                        file.mkdirs();
                    }
                    String str = this.imageUrl;
                    String substring = str.substring(str.lastIndexOf("/") + 1);
                    BufferedInputStream bufferedInputStream = new BufferedInputStream(url.openStream());
                    FileOutputStream fileOutputStream = new FileOutputStream(file + "/" + substring);
                    byte[] bArr = new byte[1024];
                    long j = 0;
                    while (true) {
                        int read = bufferedInputStream.read(bArr);
                        if (read != -1) {
                            j += read;
                            long j2 = (100 * j) / contentLength;
                            fileOutputStream.write(bArr, 0, read);
                        } else {
                            fileOutputStream.flush();
                            fileOutputStream.close();
                            bufferedInputStream.close();
                            return null;
                        }
                    }
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                    return null;
                }
            } catch (Exception e2) {
                e2.printStackTrace();
                return null;
            }
        }

        @Override
        public void onPostExecute(String str) {
            if (ViewFileFromUrlActivity.this.mProgressbar != null && ViewFileFromUrlActivity.this.mProgressbar.isShowing()) {
                ViewFileFromUrlActivity.this.mProgressbar.dismiss();
            }
            String str2 = this.imageUrl;
            File file = new File(Utility.getDownloadStorageDir(ViewFileFromUrlActivity.this.getApplicationContext()) + "/MyClassBoard/" + str2.substring(str2.lastIndexOf("/") + 1));
            if (file.exists()) {
                ViewFileFromUrlActivity.this.downloadItem.setVisible(false);
            } else {
                ViewFileFromUrlActivity.this.downloadItem.setVisible(true);
            }
            Utility.showInfoDialog(ViewFileFromUrlActivity.this, "Downloaded to " + file.getAbsolutePath());
        }
    }
}