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