Skip to content

Commit

Permalink
fix: updates to add android
Browse files Browse the repository at this point in the history
  • Loading branch information
gtokman committed Apr 20, 2024
1 parent 4211e4a commit 52b9b53
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 17 deletions.
2 changes: 2 additions & 0 deletions android/src/main/AndroidManifestNew.xml
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android">

<uses-permission android:name="android.permission.VIBRATE" />
</manifest>
63 changes: 58 additions & 5 deletions android/src/main/java/com/candlefinance/haptics/HapticsModule.kt
Original file line number Diff line number Diff line change
@@ -1,22 +1,75 @@
package com.candlefinance.haptics

import android.os.Build
import android.os.VibrationEffect
import android.os.Vibrator
import android.os.VibratorManager
import com.facebook.react.bridge.Promise
import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.bridge.ReactContextBaseJavaModule
import com.facebook.react.bridge.ReactMethod
import com.facebook.react.bridge.Promise
import com.facebook.react.bridge.ReadableArray
import okhttp3.internal.platform.Platform


class HapticsModule(reactContext: ReactApplicationContext) :
ReactContextBaseJavaModule(reactContext) {

var vibrator: Vibrator? = null
private val ERROR_PATTERN = longArrayOf(0, 100, 50, 50, 50, 50, 100)
private val SUCCESS_PATTERN = longArrayOf(0, 25, 25, 25)
private val WARNING_PATTERN = longArrayOf(0, 100, 50, 100, 100, 50)

override fun getName(): String {
return NAME
}

// Example method
// See https://reactnative.dev/docs/native-modules-android
@ReactMethod
fun multiply(a: Double, b: Double, promise: Promise) {
promise.resolve(a * b)
fun haptic(type: String, promise: Promise) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
vibrator = reactApplicationContext.getSystemService(Vibrator::class.java)
val vibrationEffect = when (type) {
"light" -> VibrationEffect.createOneShot(20, VibrationEffect.DEFAULT_AMPLITUDE)
"medium" -> VibrationEffect.createOneShot(40, VibrationEffect.DEFAULT_AMPLITUDE)
"rigid" -> VibrationEffect.createOneShot(60, VibrationEffect.DEFAULT_AMPLITUDE)
"heavy" -> VibrationEffect.createOneShot(80, VibrationEffect.DEFAULT_AMPLITUDE)
"soft" -> VibrationEffect.createOneShot(100, VibrationEffect.DEFAULT_AMPLITUDE)
"selectionChanged" -> if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
VibrationEffect.createPredefined(VibrationEffect.EFFECT_TICK)
} else {
VibrationEffect.createOneShot(20, VibrationEffect.DEFAULT_AMPLITUDE)
}
"warning" -> VibrationEffect.createWaveform(WARNING_PATTERN, -1)
"error" -> VibrationEffect.createWaveform(ERROR_PATTERN, -1 )
"success" -> VibrationEffect.createWaveform(SUCCESS_PATTERN, -1)
else -> VibrationEffect.createOneShot(20, VibrationEffect.DEFAULT_AMPLITUDE)
}
vibrator?.vibrate(vibrationEffect)
} else {
promise.reject("ERR_VERSION", "Vibration is not supported on this device")
}
}

@ReactMethod
fun hapticWithPattern(pattern: ReadableArray, promise: Promise) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
vibrator = reactApplicationContext.getSystemService(Vibrator::class.java)
val patternLong = pattern.toArrayList().map {
when (it) {
"o" -> 40L
"O" -> 80L
"." -> 20L
":" -> 25L
"-" -> 0L
"=" -> 0L
else -> 20L
}
}.toLongArray()
val vibrationEffect = VibrationEffect.createWaveform(patternLong, -1)
vibrator?.vibrate(vibrationEffect)
} else {
promise.reject("ERR_VERSION", "Vibration is not supported on this device")
}
}

companion object {
Expand Down
2 changes: 2 additions & 0 deletions example/ios/HapticsExample.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,7 @@
"-ld_classic",
"-Wl",
"-ld_classic",
"-Wl -ld_classic ",
);
REACT_NATIVE_PATH = "${PODS_ROOT}/../../node_modules/react-native";
SDKROOT = iphoneos;
Expand Down Expand Up @@ -701,6 +702,7 @@
"-ld_classic",
"-Wl",
"-ld_classic",
"-Wl -ld_classic ",
);
REACT_NATIVE_PATH = "${PODS_ROOT}/../../node_modules/react-native";
SDKROOT = iphoneos;
Expand Down
4 changes: 2 additions & 2 deletions example/ios/Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
PODS:
- boost (1.76.0)
- candlefinance-haptics (0.2.3):
- candlefinance-haptics (0.3.2):
- RCT-Folly (= 2021.07.22.00)
- React-Core
- CocoaAsyncSocket (7.6.5)
Expand Down Expand Up @@ -619,7 +619,7 @@ EXTERNAL SOURCES:

SPEC CHECKSUMS:
boost: 57d2868c099736d80fcd648bf211b4431e51a558
candlefinance-haptics: 22e2a8560b86b9ef10f8677e6c5d8d14496a22a5
candlefinance-haptics: 1e86f183628ad882f0c3f54d0e1c4a1c39e7e1ce
CocoaAsyncSocket: 065fd1e645c7abab64f7a6a2007a48038fdc6a99
DoubleConversion: 5189b271737e1565bdce30deb4a08d647e3f5f54
FBLazyVector: 71803c074f6325f10b5ec891c443b6bbabef0ca7
Expand Down
10 changes: 0 additions & 10 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@ export type HapticType =
| 'selectionChanged';

export function haptic(type: HapticType = 'medium') {
if (Platform.OS === 'android') {
console.log('Haptics is not supported on Android');
return;
}
Haptics.haptic(type);
}

Expand All @@ -40,24 +36,18 @@ export type HapticPattern =
| '='; // Represents a wait of 1 second

export function hapticWithPattern(pattern: HapticPattern[]) {
if (Platform.OS === 'android') {
console.log('Haptics is not supported on Android');
return;
}
Haptics.hapticWithPattern(pattern);
}

export function play(fileName: string, loop: boolean = false) {
if (Platform.OS === 'android') {
console.log('Haptics is not supported on Android');
return;
}
Haptics.play(fileName, loop);
}

export function stop() {
if (Platform.OS === 'android') {
console.log('Haptics is not supported on Android');
return;
}
Haptics.stop();
Expand Down

0 comments on commit 52b9b53

Please sign in to comment.