diff --git a/shaky/src/main/java/com/linkedin/android/shaky/ShakeDelegate.java b/shaky/src/main/java/com/linkedin/android/shaky/ShakeDelegate.java index 9053370..791ff2e 100644 --- a/shaky/src/main/java/com/linkedin/android/shaky/ShakeDelegate.java +++ b/shaky/src/main/java/com/linkedin/android/shaky/ShakeDelegate.java @@ -52,6 +52,11 @@ public abstract class ShakeDelegate { */ @MenuRes protected int resMenu = FormFragment.DEFAULT_MENU; + /** + * Allows user to enable performing a custom action on detecting shake. + */ + private boolean enableCustomHandlingOfShake = false; + /** * @return true if shake detection should be enabled, false otherwise */ @@ -147,6 +152,30 @@ public DialogFragment getCustomDialog() { return null; } + /** + * Returns whether custom handling of shake events is enabled. + * + * @return true if custom handling of shake is enabled, false otherwise + */ + public boolean isCustomHandlingOfShakeEnabled() { + return enableCustomHandlingOfShake; + } + + /** + * Enables or disables custom handling of shake events. + * + * @param enableCustomHandlingOfShake true to enable custom handling, false to disable + */ + public void enableCustomHandlingOfShake(boolean enableCustomHandlingOfShake) { + this.enableCustomHandlingOfShake = enableCustomHandlingOfShake; + } + + /** + * This method can be overridden to provide custom action to be performed on shake when + * {@link #enableCustomHandlingOfShake} is enabled. + */ + public void performCustomActionOnShake(@NonNull Activity activity){} + /** * @return if the dialog should be shown on shake or the shake-to-feedback bottom sheet. */ diff --git a/shaky/src/main/java/com/linkedin/android/shaky/Shaky.java b/shaky/src/main/java/com/linkedin/android/shaky/Shaky.java index 9567101..33db569 100644 --- a/shaky/src/main/java/com/linkedin/android/shaky/Shaky.java +++ b/shaky/src/main/java/com/linkedin/android/shaky/Shaky.java @@ -227,7 +227,23 @@ private boolean isValidStartAction(String action) { @Override public void hearShake() { - launchShakeBottomSheet(ShakyFlowCallback.SHAKY_STARTED_BY_SHAKE); + if(delegate.isCustomHandlingOfShakeEnabled()) { + if (shakyFlowCallback != null) { + shakyFlowCallback.onShakyStarted(ShakyFlowCallback.SHAKY_STARTED_BY_SHAKE); + } + + if (shouldIgnoreShake() || !canStartFeedbackFlow() || activity==null) { + return; + } + + delegate.performCustomActionOnShake(activity); + + if (shakyFlowCallback != null) { + shakyFlowCallback.onUserPromptShown(); + } + } else { + launchShakeBottomSheet(ShakyFlowCallback.SHAKY_STARTED_BY_SHAKE); + } } private void launchShakeBottomSheet(@ShakyFlowCallback.ShakyStartedReason int reason) {