1
1
package com .devpaul .filepickerlibrary ;
2
2
3
+ import android .Manifest ;
3
4
import android .app .ProgressDialog ;
4
5
import android .content .Context ;
5
6
import android .content .Intent ;
7
+ import android .content .pm .PackageManager ;
6
8
import android .net .Uri ;
7
9
import android .os .AsyncTask ;
10
+ import android .os .Build ;
8
11
import android .os .Bundle ;
9
12
import android .os .Environment ;
13
+ import android .support .v4 .app .ActivityCompat ;
14
+ import android .support .v4 .content .ContextCompat ;
10
15
import android .support .v7 .app .AppCompatActivity ;
11
16
import android .support .v7 .widget .DefaultItemAnimator ;
12
17
import android .support .v7 .widget .LinearLayoutManager ;
13
18
import android .support .v7 .widget .RecyclerView ;
14
19
import android .support .v7 .widget .Toolbar ;
15
- import android .util .Log ;
16
20
import android .view .MenuItem ;
17
21
import android .view .View ;
18
22
import android .view .ViewTreeObserver ;
24
28
import android .widget .LinearLayout ;
25
29
import android .widget .RelativeLayout ;
26
30
31
+ import com .afollestad .materialdialogs .MaterialDialog ;
27
32
import com .devpaul .filepickerlibrary .adapter .FileRecyclerViewAdapter ;
28
33
import com .devpaul .filepickerlibrary .enums .FileScopeType ;
29
34
import com .devpaul .filepickerlibrary .enums .FileType ;
@@ -230,7 +235,14 @@ public class FilePicker extends AppCompatActivity implements NameFileDialogInter
230
235
*/
231
236
private Context mContext ;
232
237
238
+ /**
239
+ * Request code for app permissions.
240
+ */
241
+ private static final int REQUEST_FOR_READ_EXTERNAL_STORAGE = 101 ;
233
242
243
+ /**
244
+ * Layout manager for the Recycler View.
245
+ */
234
246
private LinearLayoutManager mLinearLayoutManager ;
235
247
236
248
@ Override
@@ -306,8 +318,7 @@ public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
306
318
if (Math .abs (dy ) >= 5 ) {
307
319
if (dy > 0 ) {
308
320
toggleButton (false );
309
- }
310
- else if (dy < 0 ) {
321
+ } else if (dy < 0 ) {
311
322
toggleButton (true );
312
323
}
313
324
if (areButtonsShowing ) {
@@ -318,8 +329,7 @@ else if(dy < 0) {
318
329
hideButtons ();
319
330
adapter .setSelectedPosition (-1 );
320
331
}
321
- }
322
- else {
332
+ } else {
323
333
mLastFirstVisibleItem = firstVisibleItem ;
324
334
}
325
335
super .onScrolled (recyclerView , dx , dy );
@@ -332,23 +342,83 @@ else if(dy < 0) {
332
342
//drawable has not been set so set the color.
333
343
setHeaderBackground (colorId , drawableId );
334
344
345
+ //check for proper permissions.
346
+ if (Build .VERSION .SDK_INT == Build .VERSION_CODES .M ) {
347
+ int permissionCheck = ContextCompat .checkSelfPermission (this ,
348
+ Manifest .permission .READ_EXTERNAL_STORAGE );
349
+ if (permissionCheck != PackageManager .PERMISSION_GRANTED ) {
350
+ if (ActivityCompat .shouldShowRequestPermissionRationale (this , Manifest .permission .READ_EXTERNAL_STORAGE )) {
351
+ //Show permission rationale.
352
+ new MaterialDialog .Builder (FilePicker .this )
353
+ .title (R .string .file_picker_permission_rationale_dialog_title )
354
+ .content (R .string .file_picker_permission_rationale_dialog_content )
355
+ .positiveText (R .string .file_picker_ok )
356
+ .negativeText (R .string .file_picker_cancel )
357
+ .callback (new MaterialDialog .ButtonCallback () {
358
+ @ Override
359
+ public void onPositive (MaterialDialog dialog ) {
360
+ ActivityCompat .requestPermissions (FilePicker .this ,
361
+ new String []{Manifest .permission .READ_EXTERNAL_STORAGE ,
362
+ Manifest .permission .WRITE_EXTERNAL_STORAGE },
363
+ REQUEST_FOR_READ_EXTERNAL_STORAGE );
364
+ }
365
+
366
+ @ Override
367
+ public void onNegative (MaterialDialog dialog ) {
368
+ setResult (RESULT_CANCELED );
369
+ finish ();
370
+ }
371
+ })
372
+ .show ();
373
+ }
374
+ else {
375
+ ActivityCompat .requestPermissions (this ,
376
+ new String []{Manifest .permission .READ_EXTERNAL_STORAGE ,
377
+ Manifest .permission .WRITE_EXTERNAL_STORAGE },
378
+ REQUEST_FOR_READ_EXTERNAL_STORAGE );
379
+ }
380
+ }
381
+ } else {
382
+ init ();
383
+ }
384
+ }
385
+
386
+ /**
387
+ * Initialize the current directory.
388
+ */
389
+ private void init () {
335
390
curDirectory = new File (Environment .getExternalStorageDirectory ().getPath ());
336
391
currentFile = new File (curDirectory .getPath ());
337
392
lastDirectory = curDirectory .getParentFile ();
338
393
339
394
if (curDirectory .isDirectory ()) {
340
- Log .d ("FILEPICKER" , "Is directory" );
341
395
new UpdateFilesTask (this ).execute (curDirectory );
342
396
} else {
343
- Log .d ("FILEPICKER" , "Is not directory" );
344
397
try {
345
- throw new Exception ("Initial file must be a directory." );
398
+ throw new Exception (getString ( R . string . file_picker_directory_error ) );
346
399
} catch (Exception e ) {
347
400
e .printStackTrace ();
348
401
}
349
402
}
350
403
}
351
404
405
+ @ Override
406
+ public void onRequestPermissionsResult (int requestCode , String [] permissions , int [] grantResults ) {
407
+ switch (requestCode ) {
408
+ //see if we got the permission.
409
+ case REQUEST_FOR_READ_EXTERNAL_STORAGE :
410
+ if (grantResults .length > 0 && grantResults [0 ] == PackageManager .PERMISSION_GRANTED
411
+ && grantResults [1 ] == PackageManager .PERMISSION_GRANTED ) {
412
+ init ();
413
+ }
414
+ else {
415
+ setResult (RESULT_CANCELED );
416
+ finish ();
417
+ }
418
+ return ;
419
+ }
420
+ }
421
+
352
422
private static OvershootInterpolator interpolator = new OvershootInterpolator ();
353
423
/**
354
424
* Toggles the material floating action button.
@@ -415,7 +485,7 @@ public void onClick(View view) {
415
485
finish ();
416
486
} else {
417
487
SnackbarManager .show (Snackbar .with (FilePicker .this )
418
- .text ("Please select a directory." )
488
+ .text (R . string . file_picker_snackbar_select_directory_message )
419
489
.duration (1500 ));
420
490
}
421
491
} else { //request code is for a file
@@ -433,7 +503,8 @@ public void onClick(View view) {
433
503
finish ();
434
504
} else {
435
505
SnackbarManager .show (Snackbar .with (FilePicker .this )
436
- .text ("Please select a " + requiredExtension + " file." )
506
+ .text (String .format (getString (R .string .file_picker_snackbar_select_file_ext_message ),
507
+ requiredExtension ))
437
508
.duration (1500 ));
438
509
}
439
510
} else {
@@ -465,11 +536,11 @@ public void onClick(View view) {
465
536
startActivity (newIntent );
466
537
} catch (android .content .ActivityNotFoundException e ) {
467
538
SnackbarManager .show (Snackbar .with (FilePicker .this )
468
- .text ("No handler for this type of file." ));
539
+ .text (R . string . file_picker_snackbar_no_file_type_handler ));
469
540
}
470
541
} else {
471
542
SnackbarManager .show (Snackbar .with (FilePicker .this )
472
- .text ("Couldn't get file type." ));
543
+ .text (R . string . file_picker_snackbar_no_read_type ));
473
544
}
474
545
475
546
}
@@ -556,7 +627,7 @@ public void onReturnFileName(String fileName) {
556
627
if (fileName .equalsIgnoreCase ("" ) || fileName .isEmpty ()) {
557
628
fileName = null ;
558
629
}
559
- if (fileName != null ) {
630
+ if (fileName != null && curDirectory != null ) {
560
631
File file = new File (curDirectory .getPath () + "//" + fileName );
561
632
boolean created = false ;
562
633
if (!file .exists ()) {
@@ -613,9 +684,8 @@ private UpdateFilesTask(Context context) {
613
684
614
685
@ Override
615
686
protected void onPreExecute () {
616
- Log .i ("FilePicker" , "AsyncCalled" );
617
687
dialog = new ProgressDialog (mContext );
618
- dialog .setMessage ("Loading..." );
688
+ dialog .setMessage (getString ( R . string . file_picker_progress_dialog_loading ) );
619
689
dialog .setCancelable (false );
620
690
dialog .show ();
621
691
hideButtons ();
@@ -635,7 +705,7 @@ protected void onPostExecute(File[] localFiles) {
635
705
files = localFiles ;
636
706
if (directory .getPath ().equalsIgnoreCase (Environment
637
707
.getExternalStorageDirectory ().getPath ())) {
638
- toolbar .setTitle ("Parent Directory" );
708
+ toolbar .setTitle (getString ( R . string . file_picker_default_directory_title ) );
639
709
640
710
} else {
641
711
toolbar .setTitle (directory .getName ());
@@ -647,9 +717,15 @@ protected void onPostExecute(File[] localFiles) {
647
717
// for(int i = 0; i < files.length; i++) {
648
718
// adapter.addFile(files[i]);
649
719
// }
650
- adapter = new FileRecyclerViewAdapter (FilePicker .this , files , scopeType , callback );
651
- //TODO: Fix this, figure out how to add and remove the header.
652
- recyclerView .setAdapter (adapter );
720
+ if (files != null ) {
721
+ adapter = new FileRecyclerViewAdapter (FilePicker .this , files , scopeType , callback );
722
+ //TODO: Fix this, figure out how to add and remove the header.
723
+ recyclerView .setAdapter (adapter );
724
+ }
725
+ //make sure the button is showing.
726
+ if (!isFabShowing ) {
727
+ toggleButton (true );
728
+ }
653
729
if (dialog .isShowing ()) {
654
730
dialog .dismiss ();
655
731
}
0 commit comments