10
10
import android .annotation .SuppressLint ;
11
11
import android .app .ProgressDialog ;
12
12
import android .content .Intent ;
13
+ import android .content .pm .PackageManager ;
13
14
import android .location .Location ;
14
15
import android .location .LocationManager ;
15
16
import android .os .Build ;
17
+ import android .os .Build .VERSION ;
18
+ import android .os .Build .VERSION_CODES ;
16
19
import android .os .Bundle ;
17
20
import android .provider .Settings ;
18
21
import android .util .DisplayMetrics ;
21
24
import android .widget .LinearLayout ;
22
25
import android .widget .RelativeLayout ;
23
26
import android .widget .TextView ;
27
+ import androidx .annotation .NonNull ;
24
28
import androidx .cardview .widget .CardView ;
25
29
import androidx .fragment .app .Fragment ;
26
30
import androidx .fragment .app .FragmentManager ;
38
42
import fr .free .nrw .commons .auth .LoginActivity ;
39
43
import fr .free .nrw .commons .auth .SessionManager ;
40
44
import fr .free .nrw .commons .contributions .ContributionController ;
45
+ import fr .free .nrw .commons .filepicker .Constants .RequestCodes ;
41
46
import fr .free .nrw .commons .filepicker .UploadableFile ;
42
47
import fr .free .nrw .commons .kvstore .JsonKvStore ;
43
48
import fr .free .nrw .commons .location .LatLng ;
59
64
import io .reactivex .android .schedulers .AndroidSchedulers ;
60
65
import io .reactivex .disposables .CompositeDisposable ;
61
66
import io .reactivex .schedulers .Schedulers ;
62
- import java .security .Permission ;
63
67
import java .util .ArrayList ;
64
68
import java .util .Collections ;
65
69
import java .util .HashMap ;
@@ -150,11 +154,6 @@ protected void onCreate(Bundle savedInstanceState) {
150
154
compositeDisposable = new CompositeDisposable ();
151
155
init ();
152
156
nearbyPopupAnswers = new HashMap <>();
153
- PermissionUtils .checkPermissionsAndPerformAction (this ,
154
- this ::receiveSharedItems ,
155
- R .string .storage_permission_title ,
156
- R .string .write_storage_permission_rationale_for_image_share ,
157
- PERMISSIONS_STORAGE );
158
157
//getting the current dpi of the device and if it is less than 320dp i.e. overlapping
159
158
//threshold, thumbnails automatically minimizes
160
159
DisplayMetrics metrics = getResources ().getDisplayMetrics ();
@@ -167,6 +166,7 @@ protected void onCreate(Bundle savedInstanceState) {
167
166
}
168
167
locationManager .requestLocationUpdatesFromProvider (LocationManager .GPS_PROVIDER );
169
168
locationManager .requestLocationUpdatesFromProvider (LocationManager .NETWORK_PROVIDER );
169
+ checkStoragePermissions ();
170
170
}
171
171
172
172
private void init () {
@@ -232,7 +232,6 @@ protected void onResume() {
232
232
askUserToLogIn ();
233
233
}
234
234
checkBlockStatus ();
235
- checkStoragePermissions ();
236
235
}
237
236
238
237
/**
@@ -255,14 +254,10 @@ protected void checkBlockStatus() {
255
254
256
255
private void checkStoragePermissions () {
257
256
final boolean hasAllPermissions = PermissionUtils .hasPermission (this , PERMISSIONS_STORAGE );
258
- if (!hasAllPermissions ) {
259
- PermissionUtils .checkPermissionsAndPerformAction (this ,
260
- () -> {
261
- //TODO handle this
262
- },
263
- R .string .storage_permission_title ,
264
- R .string .write_storage_permission_rationale_for_image_share ,
265
- PERMISSIONS_STORAGE );
257
+ if (hasAllPermissions ) {
258
+ receiveSharedItems ();
259
+ } else if (VERSION .SDK_INT >= VERSION_CODES .M ) {
260
+ requestPermissions (PERMISSIONS_STORAGE , RequestCodes .STORAGE );
266
261
}
267
262
}
268
263
@@ -350,7 +345,44 @@ public void askUserToLogIn() {
350
345
startActivity (loginIntent );
351
346
}
352
347
348
+ @ Override
349
+ public void onRequestPermissionsResult (final int requestCode ,
350
+ @ NonNull final String [] permissions ,
351
+ @ NonNull final int [] grantResults ) {
352
+ boolean areAllGranted = false ;
353
+ if (requestCode == RequestCodes .STORAGE ) {
354
+ if (VERSION .SDK_INT >= VERSION_CODES .M ) {
355
+ for (int i = 0 ; i < grantResults .length ; i ++) {
356
+ String permission = permissions [i ];
357
+ areAllGranted = grantResults [i ] == PackageManager .PERMISSION_GRANTED ;
358
+ if (grantResults [i ] == PackageManager .PERMISSION_DENIED ) {
359
+ boolean showRationale = shouldShowRequestPermissionRationale (permission );
360
+ if (!showRationale ) {
361
+ DialogUtil .showAlertDialog (this ,
362
+ getString (R .string .storage_permissions_denied ),
363
+ getString (R .string .unable_to_share_upload_item ),
364
+ getString (android .R .string .ok ),
365
+ this ::finish ,
366
+ false );
367
+ } else {
368
+ DialogUtil .showAlertDialog (this ,
369
+ getString (R .string .storage_permission_title ),
370
+ getString (
371
+ R .string .write_storage_permission_rationale_for_image_share ),
372
+ getString (android .R .string .ok ),
373
+ this ::checkStoragePermissions ,
374
+ false );
375
+ }
376
+ }
377
+ }
353
378
379
+ if (areAllGranted ) {
380
+ receiveSharedItems ();
381
+ }
382
+ }
383
+ }
384
+ super .onRequestPermissionsResult (requestCode , permissions , grantResults );
385
+ }
354
386
355
387
@ Override
356
388
protected void onActivityResult (int requestCode , int resultCode , Intent data ) {
0 commit comments