Compare commits
2 Commits
f70a7444a6
...
1ac060d19a
| Author | SHA1 | Date | |
|---|---|---|---|
| 1ac060d19a | |||
| 8273fe2bbc |
@ -5,11 +5,11 @@ import android.os.VibrationEffect
|
||||
import android.os.Vibrator
|
||||
import androidx.activity.ComponentActivity
|
||||
import androidx.activity.compose.setContent
|
||||
import androidx.compose.foundation.background
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.Spacer
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.foundation.layout.height
|
||||
import androidx.compose.material.Button
|
||||
import androidx.compose.material.ButtonDefaults
|
||||
@ -24,10 +24,11 @@ import androidx.compose.runtime.setValue
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.graphics.Color
|
||||
import androidx.compose.ui.graphics.luminance
|
||||
import androidx.compose.ui.tooling.preview.Preview
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.compose.ui.unit.sp
|
||||
import de.luh.hci.mi.vibrationpattern.ui.theme.VibrationPatternTheme
|
||||
import kotlin.random.Random
|
||||
|
||||
class MainActivity : ComponentActivity() {
|
||||
private lateinit var vibrator: Vibrator
|
||||
@ -43,7 +44,8 @@ class MainActivity : ComponentActivity() {
|
||||
@Preview(showBackground = true)
|
||||
@Composable
|
||||
fun View() {
|
||||
var isVibrating by remember { mutableStateOf(false)}
|
||||
var isVibrating by remember { mutableStateOf(false) }
|
||||
|
||||
VibrationPatternTheme {
|
||||
Surface(
|
||||
modifier = Modifier.fillMaxSize(),
|
||||
@ -55,36 +57,73 @@ class MainActivity : ComponentActivity() {
|
||||
horizontalAlignment = Alignment.CenterHorizontally
|
||||
) {
|
||||
Text(text = "Select vibration pattern")
|
||||
if (vibrator.hasVibrator()){
|
||||
Text(text = "✅ Supports vibration")
|
||||
} else {
|
||||
Text(text = "❌ Vibration not supported")
|
||||
}
|
||||
if (vibrator.hasAmplitudeControl()){
|
||||
Text(text = "✅ Supports amplitude control")
|
||||
} else {
|
||||
Text(text = "❌ Amplitude control not supported")
|
||||
}
|
||||
Button(
|
||||
onClick = { vibrator.vibrate(HEART); isVibrating = true },
|
||||
val vibrationSupport =
|
||||
if (vibrator.hasVibrator()) "✅ Supports vibration" else "❌ Vibration not supported"
|
||||
val amplitudeSupport =
|
||||
if (vibrator.hasAmplitudeControl()) "✅ Supports amplitude control" else "❌ Amplitude control not supported"
|
||||
Text(text = vibrationSupport, fontSize = 12.sp)
|
||||
Text(text = amplitudeSupport, fontSize = 12.sp)
|
||||
Spacer(modifier = Modifier.height(50.dp))
|
||||
|
||||
|
||||
Column(
|
||||
modifier = Modifier.fillMaxWidth(0.5f)
|
||||
) {
|
||||
Text("Heartbeat")
|
||||
}
|
||||
Button(
|
||||
onClick = { vibrator.vibrate(SOS); isVibrating = true },
|
||||
) {
|
||||
Text("SOS")
|
||||
}
|
||||
Button(
|
||||
onClick = { vibrator.vibrate(WALTZ); isVibrating = true },
|
||||
) {
|
||||
Text("Waltz")
|
||||
Button(
|
||||
onClick = { vibrator.vibrate(HEART); isVibrating = true },
|
||||
modifier = Modifier.fillMaxWidth()
|
||||
) {
|
||||
Text("Heartbeat")
|
||||
}
|
||||
Button(
|
||||
onClick = { vibrator.vibrate(SOS); isVibrating = true },
|
||||
modifier = Modifier.fillMaxWidth()
|
||||
) {
|
||||
Text("SOS")
|
||||
}
|
||||
Button(
|
||||
onClick = { vibrator.vibrate(WALTZ); isVibrating = true },
|
||||
modifier = Modifier.fillMaxWidth()
|
||||
) {
|
||||
Text("Waltz")
|
||||
}
|
||||
|
||||
Spacer(modifier = Modifier.height(20.dp))
|
||||
Button(
|
||||
onClick = {
|
||||
vibrator.vibrate(
|
||||
VibrationEffect.createWaveform(
|
||||
LongArray(Random.Default.nextInt(2,10)){
|
||||
Random.Default.nextLong(100, 2000)},
|
||||
1))
|
||||
isVibrating = true
|
||||
},
|
||||
modifier = Modifier.fillMaxWidth()
|
||||
) {
|
||||
Text("Random")
|
||||
}
|
||||
Button(
|
||||
onClick = { vibrator.vibrate(ANGRY); isVibrating = true },
|
||||
modifier = Modifier.fillMaxWidth()
|
||||
) {
|
||||
Text("Angry")
|
||||
}
|
||||
Button(
|
||||
onClick = { vibrator.vibrate(CHILL); isVibrating = true },
|
||||
modifier = Modifier.fillMaxWidth()
|
||||
) {
|
||||
Text("Chill")
|
||||
}
|
||||
}
|
||||
|
||||
Spacer(modifier = Modifier.height(50.dp))
|
||||
Button(
|
||||
enabled = isVibrating,
|
||||
onClick = { vibrator.cancel(); isVibrating = false },
|
||||
colors = ButtonDefaults.buttonColors(backgroundColor = Color.Red, contentColor = Color.White)
|
||||
colors = ButtonDefaults.buttonColors(
|
||||
backgroundColor = Color.Red,
|
||||
contentColor = Color.White
|
||||
)
|
||||
) {
|
||||
Text("Stop")
|
||||
}
|
||||
@ -97,7 +136,11 @@ class MainActivity : ComponentActivity() {
|
||||
companion object {
|
||||
private val HEART = VibrationEffect.createWaveform(
|
||||
longArrayOf(
|
||||
0, 65, 297, 44, 552 // wait 0ms, vibrate 65 ms, pause 297 ms, vibrate 44 ms, pause 552 ms
|
||||
0,
|
||||
65,
|
||||
297,
|
||||
44,
|
||||
552 // wait 0ms, vibrate 65 ms, pause 297 ms, vibrate 44 ms, pause 552 ms
|
||||
), 1
|
||||
)
|
||||
private val SOS = VibrationEffect.createWaveform(
|
||||
@ -113,6 +156,16 @@ class MainActivity : ComponentActivity() {
|
||||
0, 100, 200, 70, 230, 70, 230
|
||||
), 1
|
||||
)
|
||||
private val ANGRY = VibrationEffect.createWaveform(
|
||||
longArrayOf(
|
||||
0, 500, 100
|
||||
), 1
|
||||
)
|
||||
private val CHILL = VibrationEffect.createWaveform(
|
||||
longArrayOf(
|
||||
0, 100, 50, 150, 1600
|
||||
), 1
|
||||
)
|
||||
private val vibes = arrayOf(HEART, SOS, WALTZ)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user