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

Fix vibrator, which was not working on Android devices. #523

Merged
merged 1 commit into from Nov 30, 2019
Merged
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
37 changes: 26 additions & 11 deletions plyer/platforms/android/vibrator.py
Original file line number Diff line number Diff line change
@@ -1,38 +1,53 @@
'''Implementation Vibrator for Android.'''
"""Implementation Vibrator for Android."""

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

Context = autoclass('android.content.Context')
vibrator = activity.getSystemService(Context.VIBRATOR_SERVICE)
Context = autoclass("android.content.Context")
vibrator_service = activity.getSystemService(Context.VIBRATOR_SERVICE)
vibrator = cast("android.os.Vibrator", vibrator_service)
if SDK_INT >= 26:
VibrationEffect = autoclass("android.os.VibrationEffect")


class AndroidVibrator(Vibrator):
'''Android Vibrator class.
"""Android Vibrator class.

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

def _vibrate(self, time=None, **kwargs):
if vibrator:
vibrator.vibrate(int(1000 * time))
if SDK_INT >= 26:
vibrator.vibrate(
VibrationEffect.createOneShot(
int(1000 * time), VibrationEffect.DEFAULT_AMPLITUDE
)
)
else:
vibrator.vibrate(int(1000 * time))

def _pattern(self, pattern=None, repeat=None, **kwargs):
pattern = [int(1000 * time) for time in pattern]

if vibrator:
vibrator.vibrate(pattern, repeat)
if SDK_INT >= 26:
vibrator.vibrate(
VibrationEffect.createWaveform(pattern, repeat)
)
else:
vibrator.vibrate(pattern, repeat)

def _exists(self, **kwargs):
if SDK_INT >= 11:
return vibrator.hasVibrator()
elif activity.getSystemService(Context.VIBRATOR_SERVICE) is None:
elif vibrator_service is None:
raise NotImplementedError()
return True

Expand All @@ -41,8 +56,8 @@ def _cancel(self, **kwargs):


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

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