Add more haptic feedback varieties (flutter/engine#4699)

* Add more haptic feedback varieties

* Make the specific feedback types do nothing on <iOS 10
This commit is contained in:
xster
2018-02-27 14:55:18 -08:00
committed by GitHub
parent e6ca9221d5
commit 5809f804f4
2 changed files with 32 additions and 6 deletions

View File

@@ -48,7 +48,7 @@ public class PlatformPlugin implements MethodCallHandler, ActivityLifecycleListe
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);
@@ -85,9 +85,20 @@ public class PlatformPlugin implements MethodCallHandler, ActivityLifecycleListe
}
}
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 {

View File

@@ -40,7 +40,7 @@ using namespace shell;
[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];
@@ -76,8 +76,23 @@ using namespace shell;
}
}
- (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 {