Convert Rageshake to an interface for testing purpose.
This commit is contained in:
parent
67201a1af9
commit
4e25e97976
4 changed files with 87 additions and 45 deletions
|
|
@ -176,6 +176,7 @@ dependencies {
|
||||||
implementation(libs.androidx.startup)
|
implementation(libs.androidx.startup)
|
||||||
implementation(libs.coil)
|
implementation(libs.coil)
|
||||||
implementation(libs.datetime)
|
implementation(libs.datetime)
|
||||||
|
implementation(libs.squareup.seismic)
|
||||||
|
|
||||||
implementation(libs.dagger)
|
implementation(libs.dagger)
|
||||||
kapt(libs.dagger.compiler)
|
kapt(libs.dagger.compiler)
|
||||||
|
|
|
||||||
|
|
@ -95,12 +95,12 @@ class RageshakeDetectionPresenter @Inject constructor(
|
||||||
private fun handleRageShake(start: Boolean, state: RageshakeDetectionState, takeScreenshot: MutableState<Boolean>) {
|
private fun handleRageShake(start: Boolean, state: RageshakeDetectionState, takeScreenshot: MutableState<Boolean>) {
|
||||||
if (start) {
|
if (start) {
|
||||||
rageShake.start(state.preferenceState.sensitivity)
|
rageShake.start(state.preferenceState.sensitivity)
|
||||||
rageShake.interceptor = {
|
rageShake.setInterceptor {
|
||||||
takeScreenshot.value = true
|
takeScreenshot.value = true
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
rageShake.stop()
|
rageShake.stop()
|
||||||
rageShake.interceptor = null
|
rageShake.setInterceptor(null)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,77 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2022 New Vector Ltd
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package io.element.android.features.rageshake.rageshake
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
|
import android.hardware.Sensor
|
||||||
|
import android.hardware.SensorManager
|
||||||
|
import androidx.core.content.getSystemService
|
||||||
|
import com.squareup.anvil.annotations.ContributesBinding
|
||||||
|
import com.squareup.seismic.ShakeDetector
|
||||||
|
import io.element.android.libraries.di.AppScope
|
||||||
|
import io.element.android.libraries.di.ApplicationContext
|
||||||
|
import io.element.android.libraries.di.SingleIn
|
||||||
|
import javax.inject.Inject
|
||||||
|
|
||||||
|
@SingleIn(AppScope::class)
|
||||||
|
@ContributesBinding(AppScope::class, RageShake::class)
|
||||||
|
class DefaultRageShake @Inject constructor(
|
||||||
|
@ApplicationContext context: Context,
|
||||||
|
) : ShakeDetector.Listener, RageShake {
|
||||||
|
|
||||||
|
private var sensorManager = context.getSystemService<SensorManager>()
|
||||||
|
private var shakeDetector: ShakeDetector? = null
|
||||||
|
private var interceptor: (() -> Unit)? = null
|
||||||
|
|
||||||
|
override fun setInterceptor(interceptor: (() -> Unit)?) {
|
||||||
|
this.interceptor = interceptor
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if the feature is available on this device.
|
||||||
|
*/
|
||||||
|
override fun isAvailable(): Boolean {
|
||||||
|
return sensorManager?.getDefaultSensor(Sensor.TYPE_ACCELEROMETER) != null
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun start(sensitivity: Float) {
|
||||||
|
sensorManager?.let {
|
||||||
|
shakeDetector = ShakeDetector(this).apply {
|
||||||
|
start(it, SensorManager.SENSOR_DELAY_GAME)
|
||||||
|
}
|
||||||
|
setSensitivity(sensitivity)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun stop() {
|
||||||
|
shakeDetector?.stop()
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* sensitivity will be {0, O.25, 0.5, 0.75, 1} and converted to
|
||||||
|
* [ShakeDetector.SENSITIVITY_LIGHT (=11), ShakeDetector.SENSITIVITY_HARD (=15)].
|
||||||
|
*/
|
||||||
|
override fun setSensitivity(sensitivity: Float) {
|
||||||
|
shakeDetector?.setSensitivity(
|
||||||
|
ShakeDetector.SENSITIVITY_LIGHT + (sensitivity * 4).toInt()
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun hearShake() {
|
||||||
|
interceptor?.invoke()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2022 New Vector Ltd
|
* Copyright (c) 2023 New Vector Ltd
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|
@ -16,57 +16,21 @@
|
||||||
|
|
||||||
package io.element.android.features.rageshake.rageshake
|
package io.element.android.features.rageshake.rageshake
|
||||||
|
|
||||||
import android.content.Context
|
interface RageShake {
|
||||||
import android.hardware.Sensor
|
|
||||||
import android.hardware.SensorManager
|
|
||||||
import androidx.core.content.getSystemService
|
|
||||||
import com.squareup.seismic.ShakeDetector
|
|
||||||
import io.element.android.libraries.di.AppScope
|
|
||||||
import io.element.android.libraries.di.ApplicationContext
|
|
||||||
import io.element.android.libraries.di.SingleIn
|
|
||||||
import javax.inject.Inject
|
|
||||||
|
|
||||||
@SingleIn(AppScope::class)
|
|
||||||
class RageShake @Inject constructor(
|
|
||||||
@ApplicationContext context: Context,
|
|
||||||
) : ShakeDetector.Listener {
|
|
||||||
|
|
||||||
private var sensorManager = context.getSystemService<SensorManager>()
|
|
||||||
private var shakeDetector: ShakeDetector? = null
|
|
||||||
|
|
||||||
var interceptor: (() -> Unit)? = null
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check if the feature is available on this device.
|
* Check if the feature is available on this device.
|
||||||
*/
|
*/
|
||||||
fun isAvailable(): Boolean {
|
fun isAvailable(): Boolean
|
||||||
return sensorManager?.getDefaultSensor(Sensor.TYPE_ACCELEROMETER) != null
|
|
||||||
}
|
|
||||||
|
|
||||||
fun start(sensitivity: Float) {
|
fun start(sensitivity: Float)
|
||||||
sensorManager?.let {
|
|
||||||
shakeDetector = ShakeDetector(this).apply {
|
|
||||||
start(it, SensorManager.SENSOR_DELAY_GAME)
|
|
||||||
}
|
|
||||||
setSensitivity(sensitivity)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun stop() {
|
fun stop()
|
||||||
shakeDetector?.stop()
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* sensitivity will be {0, O.25, 0.5, 0.75, 1} and converted to
|
* sensitivity will be {0, O.25, 0.5, 0.75, 1} and converted to
|
||||||
* [ShakeDetector.SENSITIVITY_LIGHT (=11), ShakeDetector.SENSITIVITY_HARD (=15)].
|
* [ShakeDetector.SENSITIVITY_LIGHT (=11), ShakeDetector.SENSITIVITY_HARD (=15)].
|
||||||
*/
|
*/
|
||||||
fun setSensitivity(sensitivity: Float) {
|
fun setSensitivity(sensitivity: Float)
|
||||||
shakeDetector?.setSensitivity(
|
|
||||||
ShakeDetector.SENSITIVITY_LIGHT + (sensitivity * 4).toInt()
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun hearShake() {
|
fun setInterceptor(interceptor: (() -> Unit)?)
|
||||||
interceptor?.invoke()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue