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