Compare commits
1 Commits
assignment
...
assignment
| Author | SHA1 | Date | |
|---|---|---|---|
| e8519d7a3d |
@ -5,29 +5,30 @@ import android.os.VibrationEffect
|
||||
import android.os.Vibrator
|
||||
import androidx.activity.ComponentActivity
|
||||
import androidx.activity.compose.setContent
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.Row
|
||||
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
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.selection.selectable
|
||||
import androidx.compose.foundation.selection.selectableGroup
|
||||
import androidx.compose.material.MaterialTheme
|
||||
import androidx.compose.material.RadioButton
|
||||
import androidx.compose.material.Surface
|
||||
import androidx.compose.material.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
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.semantics.Role
|
||||
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
|
||||
|
||||
class MainActivity : ComponentActivity() {
|
||||
private lateinit var vibrator: Vibrator
|
||||
|
||||
@ -35,90 +36,84 @@ class MainActivity : ComponentActivity() {
|
||||
super.onCreate(savedInstanceState)
|
||||
vibrator = getSystemService(Vibrator::class.java)
|
||||
setContent {
|
||||
View()
|
||||
View(vibrator.hasVibrator(), vibrator.hasAmplitudeControl())
|
||||
}
|
||||
}
|
||||
|
||||
@Preview(showBackground = true)
|
||||
@Composable
|
||||
fun View() {
|
||||
var isVibrating by remember { mutableStateOf(false) }
|
||||
fun PreviewView() {
|
||||
|
||||
View(hasVibrator = true, hasAmplitudeControl = true)
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun View(hasVibrator: Boolean, hasAmplitudeControl: Boolean) {
|
||||
|
||||
VibrationPatternTheme {
|
||||
Surface(
|
||||
modifier = Modifier.fillMaxSize(),
|
||||
modifier = Modifier
|
||||
.fillMaxSize()
|
||||
.padding(8.dp),
|
||||
color = MaterialTheme.colors.background,
|
||||
) {
|
||||
Column(
|
||||
modifier = Modifier.fillMaxSize(),
|
||||
verticalArrangement = Arrangement.Center,
|
||||
horizontalAlignment = Alignment.CenterHorizontally
|
||||
) {
|
||||
Text(text = "Select vibration pattern")
|
||||
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)
|
||||
) {
|
||||
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")
|
||||
}
|
||||
Column {
|
||||
Text(
|
||||
text = "Play vibration pattern",
|
||||
fontSize = 32.sp,
|
||||
color = MaterialTheme.colors.primary
|
||||
)
|
||||
Text(
|
||||
text = "has vibrator: $hasVibrator",
|
||||
fontSize = 12.sp,
|
||||
color = MaterialTheme.colors.secondary
|
||||
)
|
||||
Text(
|
||||
text = "has amplitude control: $hasAmplitudeControl",
|
||||
fontSize = 12.sp,
|
||||
color = MaterialTheme.colors.secondary
|
||||
)
|
||||
|
||||
Spacer(modifier = Modifier.height(20.dp))
|
||||
Button(
|
||||
onClick = { vibrator.vibrate(vibes.random()); 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
|
||||
val radioOptions =
|
||||
listOf("None", "Heartbeat", "SOS", "Waltz", "Angry", "Chill", "Random")
|
||||
val (selectedOption, onOptionSelected) = remember {
|
||||
mutableStateOf(radioOptions[0])
|
||||
}
|
||||
Column(Modifier.selectableGroup()) {
|
||||
radioOptions.forEach { text ->
|
||||
Row(
|
||||
Modifier
|
||||
.fillMaxWidth()
|
||||
.height(48.dp)
|
||||
.padding(horizontal = 16.dp)
|
||||
.selectable(
|
||||
selected = (text == selectedOption),
|
||||
onClick = {
|
||||
onOptionSelected(text)
|
||||
when (text) {
|
||||
"Heartbeat" -> vibrator.vibrate(HEART)
|
||||
"SOS" -> vibrator.vibrate(SOS)
|
||||
"Waltz" -> vibrator.vibrate(WALTZ)
|
||||
"Angry" -> vibrator.vibrate(ANGRY)
|
||||
"Chill" -> vibrator.vibrate(CHILL)
|
||||
"Random" -> vibrator.vibrate(vibes.random())
|
||||
else -> vibrator.cancel()
|
||||
}
|
||||
},
|
||||
role = Role.RadioButton
|
||||
),
|
||||
verticalAlignment = Alignment.CenterVertically
|
||||
) {
|
||||
RadioButton(selected = (text == selectedOption), onClick = null)
|
||||
Text(
|
||||
text = text,
|
||||
modifier = Modifier.padding(start = 16.dp)
|
||||
)
|
||||
) {
|
||||
Text("Stop")
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -136,10 +131,27 @@ class MainActivity : ComponentActivity() {
|
||||
)
|
||||
private val SOS = VibrationEffect.createWaveform(
|
||||
longArrayOf(
|
||||
0, 100, 100, 100, 100, 100, 100,
|
||||
300, 100, 300, 100, 300, 100,
|
||||
100, 100, 100, 100, 100, 100,
|
||||
0, 1000
|
||||
0,
|
||||
100,
|
||||
100,
|
||||
100,
|
||||
100,
|
||||
100,
|
||||
100,
|
||||
300,
|
||||
100,
|
||||
300,
|
||||
100,
|
||||
300,
|
||||
100,
|
||||
100,
|
||||
100,
|
||||
100,
|
||||
100,
|
||||
100,
|
||||
100,
|
||||
0,
|
||||
1000
|
||||
), 1
|
||||
)
|
||||
private val WALTZ = VibrationEffect.createWaveform(
|
||||
|
||||
Reference in New Issue
Block a user