FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
pythonnative/src/pythonnative/native_modules/haptics.py at main · andevsr/pythonnative · GitHub
andevsr
pythonnative
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
pythonnative
/
src
/
pythonnative
/
native_modules
/
haptics.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
181 lines (140 loc) · 5.83 KB
Breadcrumbs
pythonnative
/
src
/
pythonnative
/
native_modules
/
haptics.py
Copy path
File metadata and controls
181 lines (140 loc) · 5.83 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
"""Haptic feedback and raw vibration.
Two interfaces live here:
- [`Haptics`][pythonnative.Haptics]: semantic, iOS-style feedback
(impact / notification / selection) backed by
``UIFeedbackGenerator`` on iOS and ``VibrationEffect`` patterns on
Android.
- [`Vibration`][pythonnative.Vibration]: a blunt "buzz for N
milliseconds" interface for cases where you want an explicit
duration.
Every method is synchronous and best-effort: on a device that lacks a
Taptic Engine / vibrator, or on desktop, calls are silent no-ops rather
than errors.
"""
from
__future__
import
annotations
from
typing
import
Any
,
Optional
from
..
import
diagnostics
from
..
utils
import
IS_ANDROID
,
IS_IOS
ImpactStyle
=
str
# "light" | "medium" | "heavy" | "soft" | "rigid"
NotificationType
=
str
# "success" | "warning" | "error"
_IOS_IMPACT_STYLE
=
{
"light"
:
0
,
"medium"
:
1
,
"heavy"
:
2
,
"soft"
:
4
,
"rigid"
:
5
}
_IOS_NOTIFICATION_TYPE
=
{
"success"
:
0
,
"warning"
:
1
,
"error"
:
2
}
# Approximate Android fallback durations (ms) per semantic feedback.
_ANDROID_IMPACT_MS
=
{
"light"
:
10
,
"medium"
:
20
,
"heavy"
:
40
,
"soft"
:
15
,
"rigid"
:
30
}
_ANDROID_NOTIFICATION_MS
=
{
"success"
:
30
,
"warning"
:
50
,
"error"
:
70
}
class
Haptics
:
"""Semantic haptic feedback (synchronous, best-effort)."""
@
staticmethod
def
impact
(
style
:
ImpactStyle
=
"medium"
)
->
None
:
"""Play a physical "impact" tap of the given ``style``."""
if
IS_IOS
:
_ios_impact
(
style
)
elif
IS_ANDROID
:
_android_buzz
(
_ANDROID_IMPACT_MS
.
get
(
style
,
20
))
@
staticmethod
def
notification
(
type_
:
NotificationType
=
"success"
)
->
None
:
"""Play a success / warning / error notification pattern."""
if
IS_IOS
:
_ios_notification
(
type_
)
elif
IS_ANDROID
:
_android_buzz
(
_ANDROID_NOTIFICATION_MS
.
get
(
type_
,
30
))
@
staticmethod
def
selection
()
->
None
:
"""Play the light "selection changed" tick."""
if
IS_IOS
:
_ios_selection
()
elif
IS_ANDROID
:
_android_buzz
(
8
)
class
Vibration
:
"""Raw vibration control (synchronous, best-effort)."""
@
staticmethod
def
vibrate
(
duration_ms
:
int
=
400
)
->
None
:
"""Vibrate for ``duration_ms`` milliseconds."""
if
IS_ANDROID
:
_android_buzz
(
int
(
duration_ms
))
elif
IS_IOS
:
# iOS has no arbitrary-duration API; approximate with a
# heavy impact for short buzzes and the legacy AudioServices
# vibration for longer ones.
if
duration_ms
>=
200
:
_ios_legacy_vibrate
()
else
:
_ios_impact
(
"heavy"
)
@
staticmethod
def
cancel
()
->
None
:
"""Cancel an in-progress vibration (Android only)."""
if
IS_ANDROID
:
vibrator
=
_android_vibrator
()
if
vibrator
is
not
None
:
try
:
vibrator
.
cancel
()
except
Exception
:
diagnostics
.
swallowed
(
"haptics.Vibration.cancel"
)
# ======================================================================
# iOS
# ======================================================================
def
_ios_impact
(
style
:
str
)
->
None
:
try
:
from
rubicon
.
objc
import
ObjCClass
generator
=
ObjCClass
(
"UIImpactFeedbackGenerator"
).
alloc
().
initWithStyle_
(
_IOS_IMPACT_STYLE
.
get
(
style
,
1
))
generator
.
prepare
()
generator
.
impactOccurred
()
except
Exception
:
diagnostics
.
swallowed
(
"haptics._ios_impact"
)
def
_ios_notification
(
type_
:
str
)
->
None
:
try
:
from
rubicon
.
objc
import
ObjCClass
generator
=
ObjCClass
(
"UINotificationFeedbackGenerator"
).
alloc
().
init
()
generator
.
prepare
()
generator
.
notificationOccurred_
(
_IOS_NOTIFICATION_TYPE
.
get
(
type_
,
0
))
except
Exception
:
diagnostics
.
swallowed
(
"haptics._ios_notification"
)
def
_ios_selection
()
->
None
:
try
:
from
rubicon
.
objc
import
ObjCClass
generator
=
ObjCClass
(
"UISelectionFeedbackGenerator"
).
alloc
().
init
()
generator
.
prepare
()
generator
.
selectionChanged
()
except
Exception
:
diagnostics
.
swallowed
(
"haptics._ios_selection"
)
def
_ios_legacy_vibrate
()
->
None
:
try
:
from
ctypes
import
CDLL
,
util
audio
=
CDLL
(
util
.
find_library
(
"AudioToolbox"
))
audio
.
AudioServicesPlaySystemSound
(
4095
)
# kSystemSoundID_Vibrate
except
Exception
:
diagnostics
.
swallowed
(
"haptics._ios_legacy_vibrate"
)
# ======================================================================
# Android
# ======================================================================
def
_android_vibrator
()
->
Optional
[
Any
]:
try
:
from
java
import
jclass
from
..
utils
import
get_android_context
ctx
=
get_android_context
()
Build
=
jclass
(
"android.os.Build"
)
if
Build
.
VERSION
.
SDK_INT
>=
31
:
VibratorManager
=
jclass
(
"android.os.VibratorManager"
)
Context
=
jclass
(
"android.content.Context"
)
manager
=
ctx
.
getSystemService
(
Context
.
VIBRATOR_MANAGER_SERVICE
)
VibratorManager
# referenced for clarity
return
manager
.
getDefaultVibrator
()
Context
=
jclass
(
"android.content.Context"
)
return
ctx
.
getSystemService
(
Context
.
VIBRATOR_SERVICE
)
except
Exception
:
return
None
def
_android_buzz
(
duration_ms
:
int
)
->
None
:
vibrator
=
_android_vibrator
()
if
vibrator
is
None
:
return
try
:
from
java
import
jclass
Build
=
jclass
(
"android.os.Build"
)
if
Build
.
VERSION
.
SDK_INT
>=
26
:
VibrationEffect
=
jclass
(
"android.os.VibrationEffect"
)
effect
=
VibrationEffect
.
createOneShot
(
int
(
duration_ms
),
VibrationEffect
.
DEFAULT_AMPLITUDE
)
vibrator
.
vibrate
(
effect
)
else
:
vibrator
.
vibrate
(
int
(
duration_ms
))
except
Exception
:
diagnostics
.
swallowed
(
"haptics._android_buzz"
)
Back
|
FazBrowse Home
|
New Git URL