Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add permission request to plyer GPS example #529

Merged
merged 5 commits into from
Dec 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/gps/buildozer.spec
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ source.include_exts = py,png,jpg,kv,atlas
version = 1.0

# (list) Application requirements
requirements = plyer,kivy
requirements = python3, kivy, plyer, android

# (str) Presplash of the application
#presplash.filename = %(source.dir)s/data/presplash.png
Expand Down
34 changes: 34 additions & 0 deletions examples/gps/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from kivy.app import App
from kivy.properties import StringProperty
from kivy.clock import Clock, mainthread
from kivy.utils import platform

kv = '''
BoxLayout:
Expand Down Expand Up @@ -32,6 +33,35 @@ class GpsTest(App):
gps_location = StringProperty()
gps_status = StringProperty('Click Start to get GPS location updates')


def request_android_permissions(self):
"""
Since API 23, Android requires permission to be requested at runtime.
This function requests permission and handles the response via a
callback.

The request will produce a popup if permissions have not already been
been granted, otherwise it will do nothing.
"""
from android.permissions import request_permissions, Permission

def callback(permissions, results):
"""
Defines the callback to be fired when runtime permission
has been granted or denied. This is not strictly required,
but added for the sake of completeness.
"""
if all([res for res in results]):
print("callback. All permissions granted.")
else:
print("callback. Some permissions refused.")

request_permissions([Permission.ACCESS_COARSE_LOCATION,
Permission.ACCESS_FINE_LOCATION], callback)
# # To request permissions without a callback, do:
# request_permissions([Permission.ACCESS_COARSE_LOCATION,
# Permission.ACCESS_FINE_LOCATION])

def build(self):
try:
gps.configure(on_location=self.on_location,
Expand All @@ -41,6 +71,10 @@ def build(self):
traceback.print_exc()
self.gps_status = 'GPS is not implemented for your platform'

if platform == "android":
print("gps.py: Android detected. Requesting permissions")
self.request_android_permissions()

return Builder.load_string(kv)

def start(self, minTime, minDistance):
Expand Down