-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
improve android haptics, offer toggle for haptics
- Loading branch information
Showing
14 changed files
with
252 additions
and
168 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# Expo Haptics Patch | ||
|
||
Whenever we migrated to Expo Haptics, there was a difference between how the previous and new libraries handled the | ||
Android implementation of an iOS "light" haptic. The previous library used the `Vibration` API solely, which does not | ||
have any configuration for intensity of vibration. The `Vibration` API has also been deprecated since SDK 26. See: | ||
https://github.com/mkuczera/react-native-haptic-feedback/blob/master/android/src/main/java/com/mkuczera/vibrateFactory/VibrateWithDuration.java | ||
|
||
Expo Haptics is using `VibrationManager` API on SDK >= 31. See: https://github.com/expo/expo/blob/main/packages/expo-haptics/android/src/main/java/expo/modules/haptics/HapticsModule.kt#L19 | ||
The timing and intensity of their haptic configurations though differs greatly from the original implementation. This | ||
patch uses the new `VibrationManager` API to create the same vibration that would have been seen in the deprecated | ||
`Vibration` API. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
diff --git a/node_modules/expo-haptics/android/src/main/java/expo/modules/haptics/HapticsModule.kt b/node_modules/expo-haptics/android/src/main/java/expo/modules/haptics/HapticsModule.kt | ||
index 26c52af..b949a4c 100644 | ||
--- a/node_modules/expo-haptics/android/src/main/java/expo/modules/haptics/HapticsModule.kt | ||
+++ b/node_modules/expo-haptics/android/src/main/java/expo/modules/haptics/HapticsModule.kt | ||
@@ -42,7 +42,7 @@ class HapticsModule : Module() { | ||
|
||
private fun vibrate(type: HapticsVibrationType) { | ||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { | ||
- vibrator.vibrate(VibrationEffect.createWaveform(type.timings, type.amplitudes, -1)) | ||
+ vibrator.vibrate(VibrationEffect.createWaveform(type.oldSDKPattern, intArrayOf(0, 100), -1)) | ||
} else { | ||
@Suppress("DEPRECATION") | ||
vibrator.vibrate(type.oldSDKPattern, -1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import React from 'react' | ||
|
||
import * as persisted from '#/state/persisted' | ||
|
||
type StateContext = boolean | ||
type SetContext = (v: boolean) => void | ||
|
||
const stateContext = React.createContext<StateContext>( | ||
Boolean(persisted.defaults.disableHaptics), | ||
) | ||
const setContext = React.createContext<SetContext>((_: boolean) => {}) | ||
|
||
export function Provider({children}: {children: React.ReactNode}) { | ||
const [state, setState] = React.useState( | ||
Boolean(persisted.get('disableHaptics')), | ||
) | ||
|
||
const setStateWrapped = React.useCallback( | ||
(hapticsEnabled: persisted.Schema['disableHaptics']) => { | ||
setState(Boolean(hapticsEnabled)) | ||
persisted.write('disableHaptics', hapticsEnabled) | ||
}, | ||
[setState], | ||
) | ||
|
||
React.useEffect(() => { | ||
return persisted.onUpdate(() => { | ||
setState(Boolean(persisted.get('disableHaptics'))) | ||
}) | ||
}, [setStateWrapped]) | ||
|
||
return ( | ||
<stateContext.Provider value={state}> | ||
<setContext.Provider value={setStateWrapped}> | ||
{children} | ||
</setContext.Provider> | ||
</stateContext.Provider> | ||
) | ||
} | ||
|
||
export const useHapticsDisabled = () => React.useContext(stateContext) | ||
export const useSetHapticsDisabled = () => React.useContext(setContext) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.