99import fr .free .nrw .commons .R ;
1010import fr .free .nrw .commons .contributions .ContributionController ;
1111import fr .free .nrw .commons .utils .PermissionUtils ;
12- import timber .log .Timber ;
1312
1413import static android .Manifest .permission .READ_EXTERNAL_STORAGE ;
1514import static android .Manifest .permission .WRITE_EXTERNAL_STORAGE ;
@@ -34,31 +33,39 @@ class DirectUpload {
3433 * Do not use requestCode 1 as it will conflict with NearbyFragment's requestCodes.
3534 */
3635 void initiateGalleryUpload () {
37- if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .M ) {
38- Activity parentActivity = fragment .getActivity ();
39- if (parentActivity != null ) {
40- if (ContextCompat .checkSelfPermission (parentActivity , READ_EXTERNAL_STORAGE ) != PERMISSION_GRANTED ) {
41- if (fragment .shouldShowRequestPermissionRationale (READ_EXTERNAL_STORAGE )) {
42- new AlertDialog .Builder (parentActivity )
43- .setMessage (parentActivity .getString (R .string .read_storage_permission_rationale ))
44- .setPositiveButton (android .R .string .ok , (dialog , which ) -> {
45- Timber .d ("Requesting permissions for read external storage" );
46- parentActivity .requestPermissions (new String []{READ_EXTERNAL_STORAGE }, PermissionUtils .GALLERY_PERMISSION_FROM_NEARBY_MAP );
47- dialog .dismiss ();
48- })
49- .setNegativeButton (android .R .string .cancel , null )
50- .create ()
51- .show ();
52- } else {
36+ // Only need to handle permissions for Marshmallow and above
37+ if (Build .VERSION .SDK_INT < Build .VERSION_CODES .M ) {
38+ return ;
39+ }
40+
41+ Activity parentActivity = fragment .getActivity ();
42+ if (parentActivity == null ) {
43+ controller .startSingleGalleryPick ();
44+ return ;
45+ }
46+
47+ // If we have permission, go ahead
48+ if (ContextCompat .checkSelfPermission (parentActivity , READ_EXTERNAL_STORAGE ) == PERMISSION_GRANTED ) {
49+ controller .startSingleGalleryPick ();
50+ return ;
51+ }
52+
53+ // If we don't have permission, and we need to show the rationale, show the rationale
54+ if (fragment .shouldShowRequestPermissionRationale (READ_EXTERNAL_STORAGE )) {
55+ new AlertDialog .Builder (parentActivity )
56+ .setMessage (parentActivity .getString (R .string .read_storage_permission_rationale ))
57+ .setPositiveButton (android .R .string .ok , (dialog , which ) -> {
5358 parentActivity .requestPermissions (new String []{READ_EXTERNAL_STORAGE }, PermissionUtils .GALLERY_PERMISSION_FROM_NEARBY_MAP );
54- }
55- } else {
56- controller .startSingleGalleryPick ();
57- }
58- } else {
59- controller .startSingleGalleryPick ();
60- }
59+ dialog .dismiss ();
60+ })
61+ .setNegativeButton (android .R .string .cancel , null )
62+ .create ()
63+ .show ();
64+ return ;
6165 }
66+
67+ // If we don't have permission, and we don't need to show rationale just request permission
68+ parentActivity .requestPermissions (new String []{READ_EXTERNAL_STORAGE }, PermissionUtils .GALLERY_PERMISSION_FROM_NEARBY_MAP );
6269 }
6370
6471 /**
@@ -67,29 +74,38 @@ void initiateGalleryUpload() {
6774 * Do not use requestCode 1 as it will conflict with NearbyFragment's requestCodes.
6875 */
6976 void initiateCameraUpload () {
70- if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .M ) {
71- Activity parentActivity = fragment .getActivity ();
72- if (parentActivity != null ) {
73- if (ContextCompat .checkSelfPermission (parentActivity , WRITE_EXTERNAL_STORAGE ) != PERMISSION_GRANTED ) {
74- if (fragment .shouldShowRequestPermissionRationale (WRITE_EXTERNAL_STORAGE )) {
75- new AlertDialog .Builder (parentActivity )
76- .setMessage (parentActivity .getString (R .string .write_storage_permission_rationale ))
77- .setPositiveButton (android .R .string .ok , (dialog , which ) -> {
78- parentActivity .requestPermissions (new String []{WRITE_EXTERNAL_STORAGE }, PermissionUtils .CAMERA_PERMISSION_FROM_NEARBY_MAP );
79- dialog .dismiss ();
80- })
81- .setNegativeButton (android .R .string .cancel , null )
82- .create ()
83- .show ();
84- } else {
77+ // Only need to handle permissions for Marshmallow and above
78+ if (Build .VERSION .SDK_INT < Build .VERSION_CODES .M ) {
79+ return ;
80+ }
81+
82+ Activity parentActivity = fragment .getActivity ();
83+ if (parentActivity == null ) {
84+ controller .startCameraCapture ();
85+ return ;
86+ }
87+
88+ // If we have permission, go ahead
89+ if (ContextCompat .checkSelfPermission (parentActivity , WRITE_EXTERNAL_STORAGE ) == PERMISSION_GRANTED ) {
90+ controller .startCameraCapture ();
91+ return ;
92+ }
93+
94+ // If we don't have permission, and we need to show the rationale, show the rationale
95+ if (fragment .shouldShowRequestPermissionRationale (WRITE_EXTERNAL_STORAGE )) {
96+ new AlertDialog .Builder (parentActivity )
97+ .setMessage (parentActivity .getString (R .string .write_storage_permission_rationale ))
98+ .setPositiveButton (android .R .string .ok , (dialog , which ) -> {
8599 parentActivity .requestPermissions (new String []{WRITE_EXTERNAL_STORAGE }, PermissionUtils .CAMERA_PERMISSION_FROM_NEARBY_MAP );
86- }
87- } else {
88- controller .startCameraCapture ();
89- }
90- } else {
91- controller .startCameraCapture ();
92- }
100+ dialog .dismiss ();
101+ })
102+ .setNegativeButton (android .R .string .cancel , null )
103+ .create ()
104+ .show ();
105+ return ;
93106 }
107+
108+ // If we don't have permission, and we don't need to show rationale just request permission
109+ parentActivity .requestPermissions (new String []{WRITE_EXTERNAL_STORAGE }, PermissionUtils .CAMERA_PERMISSION_FROM_NEARBY_MAP );
94110 }
95111}
0 commit comments