2121import android .database .Cursor ;
2222import android .graphics .Color ;
2323import android .net .Uri ;
24+ import android .os .Bundle ;
2425import android .provider .OpenableColumns ;
2526import androidx .annotation .NonNull ;
27+ import androidx .annotation .Nullable ;
2628import androidx .core .app .ActivityCompat ;
2729import androidx .core .content .ContextCompat ;
2830import androidx .appcompat .app .AppCompatActivity ;
2931import android .util .Log ;
32+ import android .view .Menu ;
33+ import android .view .MenuInflater ;
34+ import android .view .MenuItem ;
3035import android .widget .Toast ;
3136
3237import com .github .barteksc .pdfviewer .PDFView ;
3742import com .github .barteksc .pdfviewer .util .FitPolicy ;
3843import com .shockwave .pdfium .PdfDocument ;
3944
40- import org .androidannotations .annotations .AfterViews ;
41- import org .androidannotations .annotations .EActivity ;
42- import org .androidannotations .annotations .NonConfigurationInstance ;
43- import org .androidannotations .annotations .OnActivityResult ;
44- import org .androidannotations .annotations .OptionsItem ;
45- import org .androidannotations .annotations .OptionsMenu ;
46- import org .androidannotations .annotations .ViewById ;
47-
4845import java .util .List ;
4946
50- @ EActivity (R .layout .activity_main )
51- @ OptionsMenu (R .menu .options )
52- public class PDFViewActivity extends AppCompatActivity implements OnPageChangeListener , OnLoadCompleteListener ,
47+ import butterknife .BindView ;
48+ import butterknife .ButterKnife ;
49+
50+ public class PDFViewActivity extends AppCompatActivity implements
51+ OnPageChangeListener ,
52+ OnLoadCompleteListener ,
5353 OnPageErrorListener {
5454
5555 private static final String TAG = PDFViewActivity .class .getSimpleName ();
@@ -60,48 +60,63 @@ public class PDFViewActivity extends AppCompatActivity implements OnPageChangeLi
6060 public static final String SAMPLE_FILE = "sample.pdf" ;
6161 public static final String READ_EXTERNAL_STORAGE = "android.permission.READ_EXTERNAL_STORAGE" ;
6262
63- @ ViewById
63+ @ BindView ( R . id . pdfView )
6464 PDFView pdfView ;
6565
66- @ NonConfigurationInstance
6766 Uri uri ;
6867
69- @ NonConfigurationInstance
7068 Integer pageNumber = 0 ;
7169
7270 String pdfFileName ;
7371
74- @ OptionsItem (R .id .pickFile )
75- void pickFile () {
76- int permissionCheck = ContextCompat .checkSelfPermission (this ,
77- READ_EXTERNAL_STORAGE );
72+ @ Override
73+ protected void onCreate (@ Nullable Bundle savedInstanceState ) {
74+ super .onCreate (savedInstanceState );
75+ setContentView (R .layout .activity_main );
76+ ButterKnife .bind (this );
77+ }
7878
79- if ( permissionCheck != PackageManager . PERMISSION_GRANTED ) {
80- ActivityCompat . requestPermissions (
81- this ,
82- new String []{ READ_EXTERNAL_STORAGE },
83- PERMISSION_CODE
84- );
79+ @ Override
80+ public boolean onCreateOptionsMenu ( Menu menu ) {
81+ MenuInflater inflater = getMenuInflater ();
82+ inflater . inflate ( R . menu . options , menu );
83+ return super . onCreateOptionsMenu ( menu );
84+ }
8585
86- return ;
86+ @ Override
87+ public boolean onOptionsItemSelected (MenuItem item ) {
88+ switch (item .getItemId ()) {
89+ case R .id .pickFile :
90+ int permissionCheck = ContextCompat .checkSelfPermission (this ,
91+ READ_EXTERNAL_STORAGE );
92+ if (permissionCheck != PackageManager .PERMISSION_GRANTED ) {
93+ ActivityCompat .requestPermissions (
94+ this ,
95+ new String []{READ_EXTERNAL_STORAGE },
96+ PERMISSION_CODE
97+ );
98+
99+ return true ;
100+ }
101+ launchPicker ();
102+ return true ;
103+ default :
104+ return super .onOptionsItemSelected (item );
87105 }
88-
89- launchPicker ();
90106 }
91107
92- void launchPicker () {
93- Intent intent = new Intent (Intent .ACTION_GET_CONTENT );
94- intent .setType ("application/pdf" );
95- try {
96- startActivityForResult (intent , REQUEST_CODE );
97- } catch (ActivityNotFoundException e ) {
98- //alert user that file manager not working
99- Toast .makeText (this , R .string .toast_pick_file_error , Toast .LENGTH_SHORT ).show ();
108+ @ Override
109+ protected void onActivityResult (int requestCode , int resultCode , @ Nullable Intent data ) {
110+ super .onActivityResult (requestCode , resultCode , data );
111+ if (resultCode == RESULT_OK ) {
112+ uri = data .getData ();
113+ displayFromUri (uri );
100114 }
101115 }
102116
103- @ AfterViews
104- void afterViews () {
117+ @ Override
118+ protected void onPostCreate (@ Nullable Bundle savedInstanceState ) {
119+ super .onPostCreate (savedInstanceState );
105120 pdfView .setBackgroundColor (Color .LTGRAY );
106121 if (uri != null ) {
107122 displayFromUri (uri );
@@ -111,6 +126,17 @@ void afterViews() {
111126 setTitle (pdfFileName );
112127 }
113128
129+ void launchPicker () {
130+ Intent intent = new Intent (Intent .ACTION_GET_CONTENT );
131+ intent .setType ("application/pdf" );
132+ try {
133+ startActivityForResult (intent , REQUEST_CODE );
134+ } catch (ActivityNotFoundException e ) {
135+ //alert user that file manager not working
136+ Toast .makeText (this , R .string .toast_pick_file_error , Toast .LENGTH_SHORT ).show ();
137+ }
138+ }
139+
114140 private void displayFromAsset (String assetFileName ) {
115141 pdfFileName = assetFileName ;
116142
@@ -140,14 +166,6 @@ private void displayFromUri(Uri uri) {
140166 .load ();
141167 }
142168
143- @ OnActivityResult (REQUEST_CODE )
144- public void onResult (int resultCode , Intent intent ) {
145- if (resultCode == RESULT_OK ) {
146- uri = intent .getData ();
147- displayFromUri (uri );
148- }
149- }
150-
151169 @ Override
152170 public void onPageChanged (int page , int pageCount ) {
153171 pageNumber = page ;
@@ -211,6 +229,7 @@ public void printBookmarksTree(List<PdfDocument.Bookmark> tree, String sep) {
211229 @ Override
212230 public void onRequestPermissionsResult (int requestCode , @ NonNull String permissions [],
213231 @ NonNull int [] grantResults ) {
232+ super .onRequestPermissionsResult (requestCode , permissions , grantResults );
214233 if (requestCode == PERMISSION_CODE ) {
215234 if (grantResults .length > 0
216235 && grantResults [0 ] == PackageManager .PERMISSION_GRANTED ) {
0 commit comments