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

vibrator for android v < 4.0 #129

Merged
merged 25 commits into from
May 3, 2015
Merged
Show file tree
Hide file tree
Changes from 5 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 README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ GPS X X X
Notifications X X X X X
Text to speech X X X X X X
Email (open mail client) X X X X X
Vibrator X
Vibrator X X
Sms (send messages) X X
Compass X X X
Unique ID (IMEI or SN) X X X X X X
Expand Down
9 changes: 3 additions & 6 deletions examples/vibrator/main.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.lang import Builder
from kivy.properties import StringProperty, BooleanProperty

from plyer import vibrator
from kivy.uix.boxlayout import BoxLayout

Builder.load_string('''
#:import vibrator plyer.vibrator
Expand Down Expand Up @@ -39,10 +34,12 @@


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


class VibrationApp(App):

def build(self):
return VibrationInterface()

Expand Down
2 changes: 1 addition & 1 deletion plyer/facades.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ def vibrate(self, time=1):
def _vibrate(self, **kwargs):
raise NotImplementedError()

def pattern(self, pattern=[0, 1], repeat=-1):
def pattern(self, pattern=(0, 1), repeat=-1):
'''Ask the vibrator to vibrate with the given pattern, with an
optional repeat.

Expand Down
39 changes: 29 additions & 10 deletions plyer/platforms/android/vibrator.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,53 @@
from jnius import autoclass, cast
"""Implementation Vibrator for Android."""

from jnius import autoclass
from plyer.facades import Vibrator
from plyer.platforms.android import activity
from plyer.platforms.android import SDK_INT

Intent = autoclass('android.content.Intent')
Context = autoclass('android.content.Context')
vibrator = activity.getSystemService(Context.VIBRATOR_SERVICE)


class AndroidVibrator(Vibrator):
def _vibrate(self, **kwargs):
time = kwargs.get('time')
"""Android Vibrator class.

Supported features:
* vibrate for some period of time.
* vibrate from given pattern.
* cancel vibration.
* check whether Vibrator exists.

.. warning::
Feature check if Vibrator exists works only for
Android ver. 3.0.x (SDK >= 11) and above. For android with SDK < 11
it just returns `None`.

"""

def _vibrate(self, time=1):
if vibrator:
vibrator.vibrate(int(1000 * time))

def _pattern(self, **kwargs):
pattern = kwargs.get('pattern')
repeat = kwargs.get('repeat')

def _pattern(self, pattern=(0, 1), repeat=-1):
pattern = [int(1000 * time) for time in pattern]

if vibrator:
vibrator.vibrate(pattern, repeat)

def _exists(self, **kwargs):
return vibrator.hasVibrator()
def _exists(self):
if SDK_INT >= 11:
return vibrator.hasVibrator()
return NotImplementedError()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please review this line.


def _cancel(self, **kwargs):
def _cancel(self):
vibrator.cancel()


def instance():
"""Returns Vibrator with android features.

:return: instance of class AndroidVibrator
"""
return AndroidVibrator()