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

updated examples for vibrator #706

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
28 changes: 22 additions & 6 deletions examples/vibrator/main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,27 @@
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
from kivy.utils import platform

if 'android' == platform:
# ------------------------------------------- #
# for permissions
from android.permissions import Permission, request_permissions, check_permission

# check permissions
def check_permissions(perms):
for perm in perms:
if check_permission(perm) != True:
return False
return True

# list perms
perms = [Permission.VIBRATE]

# get perms
if check_permissions(perms)!= True:
request_permissions(perms)
# ------------------------------------------- #

Builder.load_string('''
#:import vibrator plyer.vibrator
Expand Down Expand Up @@ -29,23 +50,18 @@
text: 'vibrate pattern'
on_release:
vibrator.pattern([float(n) for n in ti.text.split(',')])

''')


class VibrationInterface(BoxLayout):
'''Root Widget.'''
pass


class VibrationApp(App):

def build(self):
return VibrationInterface()

def on_pause(self):
return True


if __name__ == "__main__":
VibrationApp().run()
VibrationApp().run()
26 changes: 26 additions & 0 deletions examples/vibrator/vibrator.kv
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#:import vibrator plyer.vibrator
<VibrationInterface>:
orientation: 'vertical'
Label:
size_hint_y: None
height: sp(40)
text: 'vibrator exists: ' + str(vibrator.exists())
Button:
text: 'vibrate 10s'
on_release: vibrator.vibrate(10)
Button:
text: 'vibrate 1s'
on_release: vibrator.vibrate(1)
Button:
text: 'vibrate 0.1s'
on_release: vibrator.vibrate(0.1)
Button:
text: 'cancel vibration'
on_release: vibrator.cancel()
TextInput:
id: ti
text: '0.5,0.5,1,2,0.1,0.1,0.1,0.1,0.1,0.1'
Button:
text: 'vibrate pattern'
on_release:
vibrator.pattern([float(n) for n in ti.text.split(',')])
25 changes: 25 additions & 0 deletions examples/vibrator/vibrator2.kv
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<VibrationInterface>:
orientation: 'vertical'
Label:
size_hint_y: None
height: sp(40)
text: 'root exists: ' + str(root.exists())
Button:
text: 'vibrate 10s'
on_release: root.vibro(10)
Button:
text: 'vibrate 1s'
on_release: root.vibro(1)
Button:
text: 'vibrate 0.1s'
on_release: root.vibro(0.1)
Button:
text: 'cancel vibration'
on_release: root.cancel()
TextInput:
id: ti
text: '0.5,0.5,1,2,0.1,0.1,0.1,0.1,0.1,0.1'
Button:
text: 'vibrate pattern'
on_release:
root.pattern([float(n) for n in ti.text.split(',')])
52 changes: 52 additions & 0 deletions examples/vibrator/vibrator_v2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout

import os.path
from os.path import dirname
from kivy.lang import Builder
from kivy.utils import platform

if 'android' == platform:
# ------------------------------------------- #
# for permissions
from android.permissions import Permission, request_permissions, check_permission

# check permissions
def check_permissions(perms):
for perm in perms:
if check_permission(perm) != True:
return False
return True

# list perms
perms = [Permission.VIBRATE]

# get perms
if check_permissions(perms)!= True:
request_permissions(perms)
# ------------------------------------------- #

# Builder.load_file('vibrator.kv') # not work
Builder.load_file(os.path.join(dirname(__file__), 'vibrator.kv'))

class VibrationInterface(BoxLayout):
'''Root Widget.'''

def is_android(self):
if 'android' == platform:
return True
else:
return False

def do_nothing(self):
pass

class VibrationApp(App):
def build(self):
return VibrationInterface()

def on_pause(self):
return True

if __name__ == "__main__":
VibrationApp().run()
77 changes: 77 additions & 0 deletions examples/vibrator/vibrator_v3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout

import os.path
from os.path import dirname
from kivy.lang import Builder
from kivy.utils import platform

if 'android' == platform:
from plyer import vibrator
# ------------------------------------------- #
# for permissions
from android.permissions import Permission, request_permissions, check_permission

# check permissions
def check_permissions(perms):
for perm in perms:
if check_permission(perm) != True:
return False
return True

# list perms
perms = [Permission.VIBRATE]

# get perms
if check_permissions(perms)!= True:
request_permissions(perms)
# ------------------------------------------- #

# Builder.load_file('vibrator2.kv') # not work
Builder.load_file(os.path.join(dirname(__file__), 'vibrator2.kv'))

class VibrationInterface(BoxLayout):
'''Root Widget.'''

def vibro(self, n):
if self.__is_android():
vibrator.vibrate(n)
else:
self.__do_nothing()

def exists(self):
if self.__is_android():
return vibrator.exists()
else:
self.__do_nothing()

def cancel(self):
if self.__is_android():
vibrator.cancel()
else:
self.__do_nothing()

def pattern(self, arr):
if self.__is_android():
vibrator.pattern(arr)
else:
self.__do_nothing()

def __is_android(self):
if 'android' == platform:
return True
else:
return False

def __do_nothing(self):
pass

class VibrationApp(App):
def build(self):
return VibrationInterface()

def on_pause(self):
return True

if __name__ == "__main__":
VibrationApp().run()