-
-
Notifications
You must be signed in to change notification settings - Fork 66
/
Copy pathPdfViewerExampleActivity.java
82 lines (68 loc) · 3.21 KB
/
PdfViewerExampleActivity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package com.tejpratapsingh.pdfcreatorandroid;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.print.PrintAttributes;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.core.content.FileProvider;
import com.tejpratapsingh.pdfcreator.activity.PDFViewerActivity;
import com.tejpratapsingh.pdfcreator.utils.PDFUtil;
import java.io.File;
import java.net.URLConnection;
public class PdfViewerExampleActivity extends PDFViewerActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getSupportActionBar() != null) {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setTitle("Pdf Viewer");
getSupportActionBar().setBackgroundDrawable(new ColorDrawable(getResources()
.getColor(R.color.colorTransparentBlack)));
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_pdf_viewer, menu);
// return true so that the menu pop up is opened
return true;
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
if (item.getItemId() == android.R.id.home) {
finish();
} else if (item.getItemId() == R.id.menuPrintPdf) {
File fileToPrint = getPdfFile();
if (fileToPrint == null || !fileToPrint.exists()) {
Toast.makeText(this, R.string.text_generated_file_error, Toast.LENGTH_SHORT).show();
return super.onOptionsItemSelected(item);
}
PrintAttributes.Builder printAttributeBuilder = new PrintAttributes.Builder();
printAttributeBuilder.setMediaSize(PrintAttributes.MediaSize.ISO_A4);
printAttributeBuilder.setMinMargins(PrintAttributes.Margins.NO_MARGINS);
PDFUtil.printPdf(PdfViewerExampleActivity.this, fileToPrint, printAttributeBuilder.build());
} else if (item.getItemId() == R.id.menuSharePdf) {
File fileToShare = getPdfFile();
if (fileToShare == null || !fileToShare.exists()) {
Toast.makeText(this, R.string.text_generated_file_error, Toast.LENGTH_SHORT).show();
return super.onOptionsItemSelected(item);
}
Intent intentShareFile = new Intent(Intent.ACTION_SEND);
Uri apkURI = FileProvider.getUriForFile(
getApplicationContext(),
getApplicationContext()
.getPackageName() + ".provider", fileToShare);
intentShareFile.setDataAndType(apkURI, URLConnection.guessContentTypeFromName(fileToShare.getName()));
intentShareFile.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intentShareFile.putExtra(Intent.EXTRA_STREAM,
apkURI);
startActivity(Intent.createChooser(intentShareFile, "Share File"));
}
return super.onOptionsItemSelected(item);
}
}