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

iOS Device Haptic Feedback #71

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
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,33 @@
*/
public interface VibrationService {


/**
* Returns an instance of {@link VibrationService}.
*
* @return An instance of {@link VibrationService}.
*/
static Optional<VibrationService> create() {
return Services.get(VibrationService.class);
}

/**
* Haptic Feedback Styles: Light, Medium, Heavy.
*/
enum HapticFeedbackStyle {
LIGHT(1), MEDIUM(2), HEAVY(3);

private int style;

HapticFeedbackStyle(int style) {
this.style = style;
}

public int getStyle() {
return this.style;
}
}

/**
* Vibrates the device with the default pattern and duration
*/
Expand All @@ -88,11 +107,18 @@ static Optional<VibrationService> create() {
* <li>Wait for 2 seconds</li>
* <li>Vibrate for 3 seconds</li>
* </ul>
*
* Note: the availability of this functionality is platform-restricted, and at present only Android supports it.
* <p>
* Note: the availability of this functionality is platform-restricted, and at present only Android supports it.
* Calling this method on iOS will result in the same vibration as calling {@link #vibrate()}
*
* @param pattern The pattern of durations to play the vibration for (with wait periods in between).
*/
void vibrate(long... pattern);

/**
* Call Haptic Feedback.
*
* @param hapticFeedbackStyle Feedback Style {@link HapticFeedbackStyle}
*/
void hapticFeedback(HapticFeedbackStyle hapticFeedbackStyle);
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,24 @@ public class IOSVibrationService implements VibrationService {
static {
System.loadLibrary("Vibration");
}

@Override
public void vibrate() {
doVibrate();
}

@Override
public void vibrate(long... pattern) {
// pattern not supported on iOS
vibrate();
vibrate();
}

@Override
public void hapticFeedback(HapticFeedbackStyle hapticFeedbackStyle) {
doHapticFeedback(hapticFeedbackStyle.getStyle());
}

private native static void doVibrate();
}

private native static void doHapticFeedback(int style);
}
23 changes: 23 additions & 0 deletions modules/vibration/src/main/native/ios/Vibration.m
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,26 @@
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
}

JNIEXPORT void JNICALL Java_com_gluonhq_attach_vibration_impl_IOSVibrationService_doHapticFeedback
(JNIEnv *env, jclass jClass,jint style)
{
UIImpactFeedbackStyle feedbackStyle;

switch (style) {
case 1:
feedbackStyle = UIImpactFeedbackStyleLight;
break;
case 2:
feedbackStyle = UIImpactFeedbackStyleMedium;
break;
case 3:
feedbackStyle = UIImpactFeedbackStyleHeavy;
break;
default:
feedbackStyle = UIImpactFeedbackStyleMedium;
break;
}

UIImpactFeedbackGenerator *feedback = [[UIImpactFeedbackGenerator alloc]initWithStyle:UIImpactFeedbackStyleMedium];
[feedback impactOccurred];
}