Skip to content

Commit 0b87d79

Browse files
committed
[Android] Simple haptic feedback
HapticFeedbackConstants.KEYBOARD_TAP on all keypress and touchpad down events triggered by QML. Fixes #115
1 parent b36f20d commit 0b87d79

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

core/os/os-android.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,3 +155,33 @@ char *android_basename(const char *path)
155155

156156
return nullptr;
157157
}
158+
159+
void androidVibrate()
160+
{
161+
QAndroidJniEnvironment env;
162+
163+
// Call activity.getWindow().getDecorView().perforHapticFeedback(KEYBOARD_TAP);
164+
QAndroidJniObject window = QtAndroid::androidActivity()
165+
.callObjectMethod("getWindow", "()Landroid/view/Window;");
166+
167+
QAndroidJniObject decorView = window.callObjectMethod("getDecorView", "()Landroid/view/View;");
168+
169+
if (env->ExceptionCheck())
170+
{
171+
env->ExceptionDescribe();
172+
env->ExceptionClear();
173+
return;
174+
}
175+
176+
bool success = decorView.callMethod<jboolean>("performHapticFeedback", "(I)Z", 3); // HapticFeedbackConstants.KEYBOARD_TAP
177+
178+
if (env->ExceptionCheck())
179+
{
180+
env->ExceptionDescribe();
181+
env->ExceptionClear();
182+
return;
183+
}
184+
185+
if (!success)
186+
qInfo() << "Haptic feedback failed";
187+
}

core/os/os.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ FILE *fopen_utf8(const char *filename, const char *mode);
2525
#if defined(__ANDROID__)
2626
/* Returns an allocated string or NULL on failure. */
2727
char *android_basename(const char *path);
28+
/* KEYBOARD_TAP vibration for the current View. */
29+
void androidVibrate();
2830
#endif
2931

3032
void *os_reserve(size_t size);

qmlbridge.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,13 +256,23 @@ void QMLBridge::setButtonState(int id, bool state)
256256
{
257257
int col = id % KEYPAD_COLS, row = id / KEYPAD_COLS;
258258

259+
#ifdef Q_OS_ANDROID
260+
if (state)
261+
androidVibrate();
262+
#endif
263+
259264
::keypad_set_key(row, col, state);
260265
}
261266

262267
void QMLBridge::setTouchpadState(qreal x, qreal y, bool contact, bool down)
263268
{
264269
::touchpad_set_state(x, y, contact, down);
265270

271+
#ifdef Q_OS_ANDROID
272+
if (down)
273+
androidVibrate();
274+
#endif
275+
266276
touchpadStateChanged();
267277
}
268278

0 commit comments

Comments
 (0)