Skip to content

Commit

Permalink
fix: 🐛 merge forked branch 'Ugur-Atakan/main'
Browse files Browse the repository at this point in the history
solved "Type mismatch: inferred type is Sensor? but Sensor was expected"

#40
  • Loading branch information
AndrewDongminYoo committed Mar 10, 2024
1 parent fa0b47b commit 952c196
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 81 deletions.
2 changes: 1 addition & 1 deletion android/src/main/java/com/stepcounter/StepCounterModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class StepCounterModule internal constructor(context: ReactApplicationContext) :
}

private val appContext: ReactApplicationContext = context
private val sensorManager: SensorManager
private val lateinit sensorManager: SensorManager
private val stepsOK: Boolean
get() = checkSelfPermission(appContext, STEP_COUNTER) == PERMISSION_GRANTED
private val accelOK: Boolean
Expand Down
24 changes: 14 additions & 10 deletions android/src/main/java/com/stepcounter/StepCounterPackage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ class StepCounterPackage : TurboReactPackage() {
* @see StepCounterModule
* @see StepCounterModule.NAME
*/
override fun getModule(name: String, reactContext: ReactApplicationContext): NativeModule? {
override fun getModule(
name: String,
reactContext: ReactApplicationContext
): NativeModule? {
return if (name == StepCounterModule.NAME) StepCounterModule(reactContext) else null
}

Expand All @@ -46,15 +49,16 @@ class StepCounterPackage : TurboReactPackage() {
return ReactModuleInfoProvider {
val moduleInfo: MutableMap<String, ReactModuleInfo> = HashMap()
val isTurboModule: Boolean = BuildConfig.IS_NEW_ARCHITECTURE_ENABLED
moduleInfo[StepCounterModule.NAME] = ReactModuleInfo(
StepCounterModule.NAME,
StepCounterModule.NAME,
false, // canOverrideExistingModule
false, // needsEagerInit
true, // hasConstants
false, // isCxxModule
isTurboModule // isTurboModule
)
moduleInfo[StepCounterModule.NAME] =
ReactModuleInfo(
StepCounterModule.NAME,
StepCounterModule.NAME,
false, // canOverrideExistingModule
false, // needsEagerInit
true, // hasConstants
false, // isCxxModule
isTurboModule // isTurboModule
)
moduleInfo
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class AccelerometerService(
) : SensorListenService(counterModule, sensorManager) {
override val sensorTypeString = "Accelerometer"
override val sensorType = Sensor.TYPE_ACCELEROMETER
override val detectedSensor: Sensor = sensorManager.getDefaultSensor(sensorType)
override val detectedSensor: Sensor? = sensorManager?.getDefaultSensor(sensorType)
override var currentSteps: Double = 0.0
private var velocityRingCounter: Int = 0
private var accelRingCounter: Int = 0
Expand Down Expand Up @@ -81,11 +81,12 @@ class AccelerometerService(
accelRingY[accelRingCounter % ACCEL_RING_SIZE] = eventData[1]
accelRingZ[accelRingCounter % ACCEL_RING_SIZE] = eventData[2]
// Next we'll calculate the average of the last 50 vectors in the ring
val gravity: FloatArray = floatArrayOf(
sum(accelRingX) / min(accelRingCounter, ACCEL_RING_SIZE),
sum(accelRingY) / min(accelRingCounter, ACCEL_RING_SIZE),
sum(accelRingZ) / min(accelRingCounter, ACCEL_RING_SIZE)
)
val gravity: FloatArray =
floatArrayOf(
sum(accelRingX) / min(accelRingCounter, ACCEL_RING_SIZE),
sum(accelRingY) / min(accelRingCounter, ACCEL_RING_SIZE),
sum(accelRingZ) / min(accelRingCounter, ACCEL_RING_SIZE)
)
// Next step is to figure out the component of the current acceleration
// in the direction of world_z and subtract gravity's contribution
val currentZ: Float = dot(normalize(gravity), eventData) - norm(gravity)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import com.stepcounter.StepCounterModule
*/
abstract class SensorListenService(
private val counterModule: StepCounterModule,
private val sensorManager: SensorManager
private val sensorManager: android.hardware.SensorManager
) : SensorEventListener, LifecycleEventListener {
/**
* the accelerometer sensor type
Expand Down Expand Up @@ -62,11 +62,12 @@ abstract class SensorListenService(
* </pre>
*/
private val samplingPeriodUs
get() = when (sensorType) {
Sensor.TYPE_ACCELEROMETER -> SensorManager.SENSOR_DELAY_GAME
Sensor.TYPE_STEP_COUNTER -> SensorManager.SENSOR_DELAY_NORMAL
else -> SensorManager.SENSOR_DELAY_UI
}
get() =
when (sensorType) {
Sensor.TYPE_ACCELEROMETER -> SensorManager.SENSOR_DELAY_GAME
Sensor.TYPE_STEP_COUNTER -> SensorManager.SENSOR_DELAY_NORMAL
else -> SensorManager.SENSOR_DELAY_UI
}

/**
* @return if the [sensor][detectedSensor] is
Expand All @@ -84,7 +85,7 @@ abstract class SensorListenService(
* @see Sensor.TYPE_ACCELEROMETER
* @see Sensor.TYPE_STEP_COUNTER
*/
abstract val detectedSensor: Sensor
abstract val detectedSensor: Sensor?

/**
* the current steps data of the user
Expand All @@ -97,26 +98,28 @@ abstract class SensorListenService(
* @see Arguments.createMap
*/
val stepsParamsMap: WritableMap
get() = Arguments.createMap().apply {
putNumber("steps", currentSteps)
putNumber("distance", distance)
putNumber("startDate", startDate)
putNumber("endDate", endDate)
putString("counterType", sensorTypeString)
putNumber("calories", calories)
}
get() =
Arguments.createMap().apply {
putNumber("steps", currentSteps)
putNumber("distance", distance)
putNumber("startDate", startDate)
putNumber("endDate", endDate)
putString("counterType", sensorTypeString)
putNumber("calories", calories)
}

val stepsSensorInfo: WritableMap
get() = Arguments.createMap().apply {
putNumber("minDelay", detectedSensor.minDelay)
putNumber("maxDelay", detectedSensor.maxDelay)
putString("name", detectedSensor.name)
putString("vendor", detectedSensor.vendor)
putNumber("power", detectedSensor.power)
putNumber("resolution", detectedSensor.resolution)
putBoolean("wakeUpSensor", detectedSensor.isWakeUpSensor)
putBoolean("additionalInfoSupported", detectedSensor.isAdditionalInfoSupported)
}
get() =
Arguments.createMap().apply {
putNumber("minDelay", detectedSensor!!.minDelay)
putNumber("maxDelay", detectedSensor!!.maxDelay)
putString("name", detectedSensor!!.name)
putString("vendor", detectedSensor!!.vendor)
putNumber("power", detectedSensor!!.power)
putNumber("resolution", detectedSensor!!.resolution)
putBoolean("wakeUpSensor", detectedSensor!!.isWakeUpSensor)
putBoolean("additionalInfoSupported", detectedSensor!!.isAdditionalInfoSupported)
}

/**
* Number of in-database-saved calories.
Expand Down Expand Up @@ -151,13 +154,14 @@ abstract class SensorListenService(
get() = System.currentTimeMillis()

private val sensorDelay: Int
get() = when (samplingPeriodUs) {
SensorManager.SENSOR_DELAY_FASTEST -> 0
SensorManager.SENSOR_DELAY_GAME -> 20000
SensorManager.SENSOR_DELAY_UI -> 66667
SensorManager.SENSOR_DELAY_NORMAL -> 200000
else -> samplingPeriodUs
}
get() =
when (samplingPeriodUs) {
SensorManager.SENSOR_DELAY_FASTEST -> 0
SensorManager.SENSOR_DELAY_GAME -> 20000
SensorManager.SENSOR_DELAY_UI -> 66667
SensorManager.SENSOR_DELAY_NORMAL -> 200000
else -> samplingPeriodUs
}

/**
* this class is not implemented Service class now, but made it work so
Expand Down Expand Up @@ -211,7 +215,7 @@ abstract class SensorListenService(
if (event?.sensor == null ||
event.sensor != detectedSensor ||
event.sensor.type != sensorType ||
event.sensor.type != detectedSensor.type
detectedSensor?.type != event.sensor.type
) {
return
}
Expand Down Expand Up @@ -240,7 +244,10 @@ abstract class SensorListenService(
* @param accuracy The new accuracy of this sensor, one of
* `SensorManager.SENSOR_STATUS_*`
*/
override fun onAccuracyChanged(sensor: Sensor?, accuracy: Int) {
override fun onAccuracyChanged(
sensor: Sensor?,
accuracy: Int
) {
Log.d(TAG_NAME, "onAccuracyChanged.accuracy $accuracy")
Log.d(TAG_NAME, "onAccuracyChanged.sensor: $sensor")
}
Expand Down Expand Up @@ -276,7 +283,10 @@ abstract class SensorListenService(
}
}

private fun WritableMap.putNumber(key: String, number: Number) {
private fun WritableMap.putNumber(
key: String,
number: Number
) {
when (number) {
is Double -> putDouble(key, number)
is Int -> putInt(key, number)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class StepCounterService(
) : SensorListenService(counterModule, sensorManager) {
override val sensorTypeString = "Step Counter"
override val sensorType = Sensor.TYPE_STEP_COUNTER
override val detectedSensor: Sensor = sensorManager.getDefaultSensor(sensorType)
override val detectedSensor: Sensor? = sensorManager?.getDefaultSensor(sensorType)
private var previousSteps: Double = 0.0
set(value) {
if (field < value) {
Expand Down
10 changes: 8 additions & 2 deletions android/src/main/java/com/stepcounter/utils/SensorFusionMath.kt
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ object SensorFusionMath {
* @param vectorB The second array.
* @see <a href="https://en.wikipedia.org/wiki/Cross_product">Cross product</a>
*/
fun cross(vectorA: FloatArray, vectorB: FloatArray): FloatArray {
fun cross(
vectorA: FloatArray,
vectorB: FloatArray
): FloatArray {
val outVector = FloatArray(3)
outVector[0] = vectorA[1] * vectorB[2] - vectorA[2] * vectorB[1]
outVector[1] = vectorA[2] * vectorB[0] - vectorA[0] * vectorB[2]
Expand Down Expand Up @@ -74,7 +77,10 @@ object SensorFusionMath {
* @returns The dot product(single number) of the two sequences of numbers.
* @see <a href="https://en.wikipedia.org/wiki/Dot_product">Dot product</a>
*/
fun dot(a: FloatArray, b: FloatArray): Float {
fun dot(
a: FloatArray,
b: FloatArray
): Float {
return a[0] * b[0] + a[1] * b[1] + a[2] * b[2]
}

Expand Down
18 changes: 9 additions & 9 deletions android/src/newarch/StepCounterSpec.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@ import com.facebook.react.bridge.ReactApplicationContext

abstract class StepCounterSpec internal constructor(context: ReactApplicationContext) :
NativeStepCounterSpec(context) {
override fun getName(): String = "StepCounter"
override fun getName(): String = "StepCounter"

abstract override fun isStepCountingSupported(promise: Promise)
abstract override fun isStepCountingSupported(promise: Promise)

abstract override fun startStepCounterUpdate(from: Double)
abstract override fun startStepCounterUpdate(from: Double)

abstract override fun stopStepCounterUpdate()
abstract override fun stopStepCounterUpdate()

override fun addListener(eventName: String) {
}
override fun addListener(eventName: String) {
}

override fun removeListeners(count: Double) {
}
}
override fun removeListeners(count: Double) {
}
}
33 changes: 16 additions & 17 deletions android/src/oldarch/StepCounterSpec.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,23 @@ import com.facebook.react.module.annotations.ReactModule
@ReactModule(name = "StepCounter")
abstract class StepCounterSpec internal constructor(context: ReactApplicationContext) :
ReactContextBaseJavaModule(context) {
@ReactMethod
@DoNotStrip
abstract fun isStepCountingSupported(promise: Promise)

@ReactMethod
@DoNotStrip
abstract fun isStepCountingSupported(promise: Promise)
@ReactMethod
@DoNotStrip
abstract fun startStepCounterUpdate(from: Double)

@ReactMethod
@DoNotStrip
abstract fun startStepCounterUpdate(from: Double)
@ReactMethod
@DoNotStrip
abstract fun stopStepCounterUpdate()

@ReactMethod
@DoNotStrip
abstract fun stopStepCounterUpdate()
@ReactMethod
@DoNotStrip
abstract fun addListener(eventName: String)

@ReactMethod
@DoNotStrip
abstract fun addListener(eventName: String)

@ReactMethod
@DoNotStrip
abstract fun removeListeners(count: Double)
}
@ReactMethod
@DoNotStrip
abstract fun removeListeners(count: Double)
}

0 comments on commit 952c196

Please sign in to comment.