This repository has been archived by the owner on Nov 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathandroid_permissions.py
81 lines (73 loc) · 2.9 KB
/
android_permissions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
from kivy.utils import platform
from kivy.clock import mainthread
if platform == 'android':
from kivy.uix.button import Button
from kivy.uix.modalview import ModalView
from kivy.clock import Clock
from android import api_version, mActivity
from android.permissions import request_permissions, check_permission, \
Permission
#########################################################################
#
# The start_app callback may occur up to two timesteps after this class
# is instantiated. So the class must exist for two time steps, if not the
# callback will not be called.
#
# To defer garbage collection, instantiate this class with a class variable:
#
# def on_start(self):
# self.dont_gc = AndroidPermissions(self.start_app)
#
# def start_app(self):
# self.dont_gc = None
#
###########################################################################
#
# Android Behavior:
#
# If the user selects "Don't Allow", the ONLY way to enable
# the disallowed permission is with App Settings.
# This class give the user an additional chance if "Don't Allow" is
# selected once.
#
###########################################################################
class AndroidPermissions:
def __init__(self, start_app = None):
self.permission_dialog_count = 0
self.start_app = start_app
if platform == 'android':
#################################################
# Customize run time permissions for the app here
#################################################
self.permissions = [Permission.CAMERA, Permission.RECORD_AUDIO]
if api_version < 29:
self.permissions.append(Permission.WRITE_EXTERNAL_STORAGE)
#################################################
self.permission_status([],[])
elif self.start_app:
self.start_app()
def permission_status(self, permissions, grants):
granted = True
for p in self.permissions:
granted = granted and check_permission(p)
if granted:
if self.start_app:
self.start_app()
elif self.permission_dialog_count < 2:
Clock.schedule_once(self.permission_dialog)
else:
self.no_permission_view()
def permission_dialog(self, dt):
self.permission_dialog_count += 1
request_permissions(self.permissions, self.permission_status)
@mainthread
def no_permission_view(self):
view = ModalView()
view.add_widget(Button(text='Permission NOT granted.\n\n' +\
'Tap to quit app.\n\n\n' +\
'If you selected "Don\'t Allow",\n' +\
'enable permission with App Settings.',
on_press=self.bye))
view.open()
def bye(self, instance):
mActivity.finishAndRemoveTask()