Skip to content

Commit

Permalink
Add more haptic feedback varieties (flutter#4699)
Browse files Browse the repository at this point in the history
* Add more haptic feedback varieties

* Make the specific feedback types do nothing on <iOS 10
  • Loading branch information
xster authored Feb 27, 2018
1 parent 41a3747 commit ba98f26
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public void onMethodCall(MethodCall call, Result result) {
playSystemSound((String) arguments);
result.success(null);
} else if (method.equals("HapticFeedback.vibrate")) {
vibrateHapticFeedback();
vibrateHapticFeedback((String) arguments);
result.success(null);
} else if (method.equals("SystemChrome.setPreferredOrientations")) {
setSystemChromePreferredOrientations((JSONArray) arguments);
Expand Down Expand Up @@ -85,9 +85,20 @@ private void playSystemSound(String soundType) {
}
}

private void vibrateHapticFeedback() {
private void vibrateHapticFeedback(String feedbackType) {
View view = mActivity.getWindow().getDecorView();
view.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
if (feedbackType == null) {
view.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
} else if (feedbackType.equals("HapticFeedbackType.lightImpact")) {
view.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
} else if (feedbackType.equals("HapticFeedbackType.mediumImpact")) {
view.performHapticFeedback(HapticFeedbackConstants.KEYBOARD_TAP);
} else if (feedbackType.equals("HapticFeedbackType.heavyImpact")) {
// HapticFeedbackConstants.CONTEXT_CLICK from API level 23.
view.performHapticFeedback(6);
} else if (feedbackType.equals("HapticFeedbackType.selectionClick")) {
view.performHapticFeedback(HapticFeedbackConstants.CLOCK_TICK);
}
}

private void setSystemChromePreferredOrientations(JSONArray orientations) throws JSONException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
[self playSystemSound:args];
result(nil);
} else if ([method isEqualToString:@"HapticFeedback.vibrate"]) {
[self vibrateHapticFeedback];
[self vibrateHapticFeedback:args];
result(nil);
} else if ([method isEqualToString:@"SystemChrome.setPreferredOrientations"]) {
[self setSystemChromePreferredOrientations:args];
Expand Down Expand Up @@ -76,8 +76,23 @@ - (void)playSystemSound:(NSString*)soundType {
}
}

- (void)vibrateHapticFeedback {
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
- (void)vibrateHapticFeedback:(NSString*)feedbackType {
if (!feedbackType) {
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
return;
}

if (@available(iOS 10, *)) {
if ([@"HapticFeedbackType.lightImpact" isEqualToString: feedbackType]) {
[[[UIImpactFeedbackGenerator alloc] initWithStyle:UIImpactFeedbackStyleLight] impactOccurred];
} else if ([@"HapticFeedbackType.mediumImpact" isEqualToString: feedbackType]) {
[[[UIImpactFeedbackGenerator alloc] initWithStyle:UIImpactFeedbackStyleMedium] impactOccurred];
} else if ([@"HapticFeedbackType.heavyImpact" isEqualToString: feedbackType]) {
[[[UIImpactFeedbackGenerator alloc] initWithStyle:UIImpactFeedbackStyleHeavy] impactOccurred];
} else if ([@"HapticFeedbackType.selectionClick" isEqualToString: feedbackType]) {
[[[UISelectionFeedbackGenerator alloc] init] selectionChanged];
}
}
}

- (void)setSystemChromePreferredOrientations:(NSArray*)orientations {
Expand Down

0 comments on commit ba98f26

Please sign in to comment.