导航菜单

页面标题

页面副标题

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

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

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


package com.mcb.stjohnsemschool.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import com.downloader.Error;
import com.downloader.OnDownloadListener;
import com.downloader.PRDownloader;
import com.github.barteksc.pdfviewer.PDFView;
import com.github.barteksc.pdfviewer.listener.OnPageErrorListener;
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;

public class PdfViewActivity extends AppCompatActivity {
    private Activity activity;
    private boolean allowDownload;
    private int chapterId;
    private Context context;
    private MenuItem downloadItem;
    private boolean isLearning;
    private PDFView mPDfView;
    private ProgressBar mProgressBar;
    private TransparentProgressDialog mProgressbar;
    private int resourceId;
    private int topicId;
    private final int PDF_SELECTION_CODE = 99;
    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_pdf_view);
        this.context = getApplicationContext();
        this.activity = this;
        this.mProgressbar = new TransparentProgressDialog(this, R.drawable.spinner_loading_imag);
        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);
        }
        getSupportActionBar().setTitle(stringExtra);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        initializations();
    }

    private void initializations() {
        this.mProgressBar = (ProgressBar) findViewById(R.id.progressBar);
        this.mPDfView = findViewById(R.id.pdfView);
        PRDownloader.initialize(this.context);
        checkPdfAction();
    }

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

    private void checkPdfAction() {
        this.mProgressBar.setVisibility(0);
        downloadPdfFromInternet(this.url, getRootDirPath(), "myFile.pdf");
    }

    private void downloadPdfFromInternet(String str, final String str2, final String str3) {
        PRDownloader.download(str, str2, str3).build().start(new OnDownloadListener() {
            public void onDownloadComplete() {
                File file = new File(str2, str3);
                PdfViewActivity.this.mProgressBar.setVisibility(8);
                PdfViewActivity.this.showPdfFromFile(file);
            }

            public void onError(Error error) {
                Toast.makeText(PdfViewActivity.this.context, "Error in viewing file", 0).show();
            }
        });
    }

    private String getRootDirPath() {
        if ("mounted" == Environment.getExternalStorageState()) {
            return ContextCompat.getExternalFilesDirs(this.context, (String) null)[0].getAbsolutePath();
        }
        return this.context.getFilesDir().getAbsolutePath();
    }

    private void showPdfFromUri(Uri uri) {
        this.mPDfView.fromUri(uri).defaultPage(0).spacing(10).enableAnnotationRendering(true).load();
    }

    public void showPdfFromFile(File file) {
        this.mPDfView.fromFile(file).password((String) null).defaultPage(0).enableSwipe(true).swipeHorizontal(false).enableDoubletap(true).enableAnnotationRendering(true).onPageError(new OnPageErrorListener() {
            public void onPageError(int i, Throwable th) {
                Toast.makeText(PdfViewActivity.this.context, "Error at page " + i, 0).show();
            }
        }).load();
    }

    protected void onActivityResult(int i, int i2, Intent intent) {
        super.onActivityResult(i, i2, intent);
        if (i == 99 && i2 == -1 && intent != null) {
            showPdfFromUri(intent.getData());
        }
    }

    protected void onDestroy() {
        if (this.isLearning) {
            Utility.saveFileViewLog(this.context, 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 (PdfViewActivity.this.mProgressbar == null || PdfViewActivity.this.mProgressbar.isShowing()) {
                return;
            }
            PdfViewActivity.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(PdfViewActivity.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 (PdfViewActivity.this.mProgressbar != null && PdfViewActivity.this.mProgressbar.isShowing()) {
                PdfViewActivity.this.mProgressbar.dismiss();
            }
            String str2 = this.imageUrl;
            File file = new File(Utility.getDownloadStorageDir(PdfViewActivity.this.getApplicationContext()) + "/MyClassBoard/" + str2.substring(str2.lastIndexOf("/") + 1));
            if (file.exists()) {
                PdfViewActivity.this.downloadItem.setVisible(false);
            } else {
                PdfViewActivity.this.downloadItem.setVisible(true);
            }
            Utility.showInfoDialog(PdfViewActivity.this, "Downloaded to " + file.getAbsolutePath());
        }
    }
}