|
|
@@ -1,272 +0,0 @@
|
|
|
-package com.usai.redant.rautils.preview;
|
|
|
-
|
|
|
-import android.app.AlertDialog;
|
|
|
-import android.app.Application;
|
|
|
-import android.content.Context;
|
|
|
-import android.content.DialogInterface;
|
|
|
-import android.content.Intent;
|
|
|
-import android.content.pm.PackageManager;
|
|
|
-import android.graphics.Color;
|
|
|
-import android.net.Uri;
|
|
|
-import android.os.Build;
|
|
|
-import android.os.Bundle;
|
|
|
-import androidx.core.content.FileProvider;
|
|
|
-import androidx.appcompat.app.ActionBar;
|
|
|
-import androidx.appcompat.app.AppCompatActivity;
|
|
|
-import android.util.Log;
|
|
|
-import android.view.Gravity;
|
|
|
-import android.view.LayoutInflater;
|
|
|
-import android.view.Menu;
|
|
|
-import android.view.MenuItem;
|
|
|
-import android.view.View;
|
|
|
-import android.widget.TextView;
|
|
|
-
|
|
|
-import com.github.barteksc.pdfviewer.PDFView;
|
|
|
-import com.github.barteksc.pdfviewer.listener.OnLoadCompleteListener;
|
|
|
-import com.github.barteksc.pdfviewer.listener.OnPageChangeListener;
|
|
|
-import com.github.barteksc.pdfviewer.listener.OnPageErrorListener;
|
|
|
-import com.github.barteksc.pdfviewer.util.FitPolicy;
|
|
|
-import com.shockwave.pdfium.PdfDocument;
|
|
|
-import com.usai.redant.rautils.R;
|
|
|
-import com.usai.redant.rautils.utils.FileManager;
|
|
|
-import com.usai.redant.rautils.utils.RAProviderHelper;
|
|
|
-
|
|
|
-import java.io.File;
|
|
|
-import java.util.List;
|
|
|
-
|
|
|
-public class RAPDFPreviewActivity extends AppCompatActivity implements OnPageChangeListener, OnLoadCompleteListener,OnPageErrorListener {
|
|
|
-
|
|
|
- private static final String FilePathKey = "file";
|
|
|
-
|
|
|
- public static void test() {
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- public static void startPreviewActivity(Context context, String filePath) {
|
|
|
- if (context == null || filePath == null) {
|
|
|
- return;
|
|
|
- }
|
|
|
- Intent intent = new Intent(context, RAPDFPreviewActivity.class);
|
|
|
- intent.putExtra(FilePathKey, filePath);
|
|
|
- context.startActivity(intent);
|
|
|
- }
|
|
|
-
|
|
|
- private static final String TAG = RAPDFPreviewActivity.class.getSimpleName();
|
|
|
-
|
|
|
- private PDFView pdfView;
|
|
|
- private Integer pageNumber = 0;
|
|
|
-
|
|
|
- protected void openFile() {
|
|
|
-
|
|
|
- File file = new File(mFilePath);
|
|
|
- if (file == null || !file.exists()) {
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- Uri uri = null;
|
|
|
- String type = FileManager.getMimeType(file.getPath());
|
|
|
-
|
|
|
- // type "application/pdf"
|
|
|
- Intent intent = new Intent(Intent.ACTION_VIEW);
|
|
|
-
|
|
|
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
|
|
|
-
|
|
|
- // "com.usai.apex.fileprovider"即是在Manifest文件中配置的authorities
|
|
|
-
|
|
|
- Application application = getApplication();
|
|
|
- if (application != null && application instanceof RAProviderHelper.ProviderHelperDelegate) {
|
|
|
-
|
|
|
- String authority = ((RAProviderHelper.ProviderHelperDelegate) application).getProviderAuthorities();
|
|
|
-
|
|
|
- uri = FileProvider.getUriForFile(this, authority, file);
|
|
|
- // 给目标应用一个临时授权
|
|
|
- intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- } else {
|
|
|
- uri = Uri.fromFile(file);
|
|
|
- }
|
|
|
-
|
|
|
- intent.setDataAndType(uri, type);
|
|
|
-
|
|
|
- if (getPackageManager().resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY) != null) {
|
|
|
- // someone knows how to handle this mime type with this scheme, don't download.
|
|
|
- try {
|
|
|
- startActivity(intent);
|
|
|
- return;
|
|
|
- } catch (Exception ex) {
|
|
|
- Log.d("Open File", "activity not found for " + type + " over " + uri, ex);
|
|
|
- }
|
|
|
-
|
|
|
- } else {
|
|
|
- Log.d("Open File", "openFileAtPath: " + "No App " + uri);
|
|
|
-
|
|
|
-
|
|
|
- AlertDialog.Builder builder = new AlertDialog.Builder(RAPDFPreviewActivity.this);
|
|
|
- builder.setMessage("Can not open PDF file, no external App found." );
|
|
|
-
|
|
|
- builder.setTitle("Open PDF");
|
|
|
-
|
|
|
- builder.setPositiveButton(getString(android.R.string.ok), new DialogInterface.OnClickListener()
|
|
|
- {
|
|
|
-
|
|
|
- @Override
|
|
|
- public void onClick(DialogInterface dialog, int which)
|
|
|
- {
|
|
|
- dialog.dismiss();
|
|
|
-
|
|
|
-
|
|
|
- }
|
|
|
- });
|
|
|
-
|
|
|
- builder.create().show();
|
|
|
- }
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- protected void shareFile() {
|
|
|
-
|
|
|
- File file = new File(mFilePath);
|
|
|
- if (file == null || !file.exists()) {
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- Uri uri = null;
|
|
|
- String type = FileManager.getMimeType(file.getPath());
|
|
|
-
|
|
|
-
|
|
|
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
|
|
|
-
|
|
|
- Application application = getApplication();
|
|
|
- if (application != null && application instanceof RAProviderHelper.ProviderHelperDelegate) {
|
|
|
-
|
|
|
- String authority = ((RAProviderHelper.ProviderHelperDelegate) application).getProviderAuthorities();
|
|
|
-
|
|
|
- // "com.usai.apex.fileprovider"即是在Manifest文件中配置的authorities
|
|
|
- uri = FileProvider.getUriForFile(this, authority, file);
|
|
|
- // 给目标应用一个临时授权
|
|
|
- }
|
|
|
-
|
|
|
- } else {
|
|
|
- uri = Uri.fromFile(file);
|
|
|
- }
|
|
|
-
|
|
|
- Intent share = new Intent();
|
|
|
- share.setAction(Intent.ACTION_SEND);
|
|
|
- share.putExtra(Intent.EXTRA_STREAM, uri);
|
|
|
- share.setType(type);
|
|
|
-
|
|
|
- startActivity(Intent.createChooser(share, "Share"));
|
|
|
- }
|
|
|
-
|
|
|
- private String mFilePath;
|
|
|
- private void setCustomActionBar() {
|
|
|
-
|
|
|
- ActionBar.LayoutParams lp =new ActionBar.LayoutParams(ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.MATCH_PARENT, Gravity.CENTER);
|
|
|
- View mActionBarView = LayoutInflater.from(this).inflate(R.layout.actionbar_customtitle, null);
|
|
|
-
|
|
|
- String title=new File(mFilePath).getName();
|
|
|
- TextView mTitleView= mActionBarView.findViewById(R.id.title);
|
|
|
-
|
|
|
- mTitleView.setText(title);
|
|
|
- setTitle(title);
|
|
|
-
|
|
|
- ActionBar actionBar = getSupportActionBar();
|
|
|
- if (actionBar != null) {
|
|
|
- actionBar.setCustomView(mActionBarView, lp);
|
|
|
- actionBar.setDisplayHomeAsUpEnabled(true);
|
|
|
- actionBar.setDisplayShowTitleEnabled(true);
|
|
|
- }
|
|
|
-
|
|
|
- }
|
|
|
- @Override
|
|
|
- public boolean onOptionsItemSelected(MenuItem item) {
|
|
|
-
|
|
|
- int id = item.getItemId();
|
|
|
-
|
|
|
- if (id == android.R.id.home) {
|
|
|
- finish();
|
|
|
- } else if (id == R.id.pdf_preview_action_open) {
|
|
|
- openFile();
|
|
|
- } else if (id == R.id.pdf_preview_action_send) {
|
|
|
- shareFile();
|
|
|
- }
|
|
|
-
|
|
|
- return super.onOptionsItemSelected(item);
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public boolean onCreateOptionsMenu(Menu menu)
|
|
|
- {
|
|
|
- // Inflate the menu; this adds items to the action bar if it is present.
|
|
|
- getMenuInflater().inflate(R.menu.pdf_preview_menu, menu);
|
|
|
- return true;
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public void onDestroy()
|
|
|
- {
|
|
|
- super.onDestroy();
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public void onCreate(Bundle savedInstanceState) {
|
|
|
- super.onCreate(savedInstanceState);
|
|
|
- setContentView(R.layout.pdf_preview_activity);
|
|
|
-
|
|
|
- mFilePath = getIntent().getStringExtra(FilePathKey);
|
|
|
-
|
|
|
- setCustomActionBar();
|
|
|
-
|
|
|
- pdfView = findViewById(R.id.pdfView);
|
|
|
- pdfView.setBackgroundColor(Color.LTGRAY);
|
|
|
- pdfView.setPadding(10,10,10,10);
|
|
|
- pdfView.fromFile(new File(mFilePath))
|
|
|
- .defaultPage(pageNumber)
|
|
|
- .onPageChange(this)
|
|
|
- .enableAnnotationRendering(true)
|
|
|
- .onLoad(this)
|
|
|
- .spacing(10) // in dp
|
|
|
- .onPageError(this)
|
|
|
- .pageFitPolicy(FitPolicy.BOTH)
|
|
|
- .load();
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public void loadComplete(int nbPages) {
|
|
|
- PdfDocument.Meta meta = pdfView.getDocumentMeta();
|
|
|
- Log.e(TAG, "title = " + meta.getTitle());
|
|
|
- Log.e(TAG, "author = " + meta.getAuthor());
|
|
|
- Log.e(TAG, "subject = " + meta.getSubject());
|
|
|
- Log.e(TAG, "keywords = " + meta.getKeywords());
|
|
|
- Log.e(TAG, "creator = " + meta.getCreator());
|
|
|
- Log.e(TAG, "producer = " + meta.getProducer());
|
|
|
- Log.e(TAG, "creationDate = " + meta.getCreationDate());
|
|
|
- Log.e(TAG, "modDate = " + meta.getModDate());
|
|
|
-
|
|
|
- printBookmarksTree(pdfView.getTableOfContents(), "-");
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public void onPageChanged(int page, int pageCount) {
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public void onPageError(int page, Throwable t) {
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- public void printBookmarksTree(List<PdfDocument.Bookmark> tree, String sep) {
|
|
|
- for (PdfDocument.Bookmark b : tree) {
|
|
|
-
|
|
|
- Log.e(TAG, String.format("%s %s, p %d", sep, b.getTitle(), b.getPageIdx()));
|
|
|
-
|
|
|
- if (b.hasChildren()) {
|
|
|
- printBookmarksTree(b.getChildren(), sep + "-");
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-}
|