Skip to content

Commit 76b947c

Browse files
committed
fix: Call request_permissions callback immediately for SDK_INT < 23
1 parent 3e26bee commit 76b947c

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

pythonforandroid/recipes/android/src/android/permissions.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -457,11 +457,8 @@ class _RequestPermissionsManager:
457457
The callback supplied must accept two arguments: 'permissions' and
458458
'grantResults' (as supplied to onPermissionsCallbackResult).
459459
460-
Note that calling request_permission on SDK_INT < 23 will return
461-
immediately (as run-time permissions are not required), and so the callback
462-
will never happen. Therefore, request_permissions should only be called
463-
with a callback if calling check_permission has indicated that it is
464-
necessary.
460+
Note that for SDK_INT < 23, run-time permissions are not required, and so
461+
the callback will be called immediately.
465462
466463
The attribute '_java_callback' is initially None, but is set when the first
467464
permissions request is made. It is set to an instance of
@@ -475,6 +472,7 @@ class _RequestPermissionsManager:
475472
the Java call, and used to identify (via the _callbacks dictionary)
476473
the matching call.
477474
"""
475+
_SDK_INT = None
478476
_java_callback = None
479477
_callbacks = {1: None}
480478
_callback_id = 1
@@ -497,6 +495,16 @@ def request_permissions(cls, permissions, callback=None):
497495
with the matching requestCode is received, callback will be called
498496
with arguments of 'permissions' and 'grant_results'.
499497
"""
498+
if not cls._SDK_INT:
499+
# Get the Android build version and store it
500+
VERSION = autoclass('android.os.Build$VERSION')
501+
cls.SDK_INT = VERSION.SDK_INT
502+
if cls.SDK_INT < 23:
503+
# No run-time permissions needed, return immediately.
504+
if callback:
505+
callback(permissions, [True for x in permissions])
506+
return
507+
# Request permissions
500508
with cls._lock:
501509
if not cls._java_callback:
502510
cls.register_callback()
@@ -555,11 +563,9 @@ def request_permissions(permissions, callback=None):
555563
list of permissions, without permissions being granted; the App should
556564
check that each permission requested has been granted.
557565
558-
Also note that calling request_permission on SDK_INT < 23 will return
559-
immediately (as run-time permissions are not required), and so the callback
560-
will never happen. Therefore, request_permissions should only be called
561-
with a callback if calling check_permission has indicated that it is
562-
necessary.
566+
Also note that when calling request_permission on SDK_INT < 23, the
567+
callback will be returned immediately as requesting permissions is not
568+
required.
563569
"""
564570
_RequestPermissionsManager.request_permissions(permissions, callback)
565571

0 commit comments

Comments
 (0)