diff --git a/engine/src/flutter/shell/platform/android/io/flutter/plugin/platform/PlatformPlugin.java b/engine/src/flutter/shell/platform/android/io/flutter/plugin/platform/PlatformPlugin.java index 43dd9c1cc4..de4208e546 100644 --- a/engine/src/flutter/shell/platform/android/io/flutter/plugin/platform/PlatformPlugin.java +++ b/engine/src/flutter/shell/platform/android/io/flutter/plugin/platform/PlatformPlugin.java @@ -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 { diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterPlatformPlugin.mm b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterPlatformPlugin.mm index c9b3ff9433..c312cac28b 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterPlatformPlugin.mm +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterPlatformPlugin.mm @@ -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 {