-
Notifications
You must be signed in to change notification settings - Fork 84
/
VolumeScreen.kt
251 lines (241 loc) · 9.61 KB
/
VolumeScreen.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/*
* Copyright 2022 The Android Open Source Project
*
* 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
*
* https://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.
*/
@file:OptIn(ExperimentalWearFoundationApi::class)
package com.google.android.horologist.audio.ui
import android.media.AudioManager
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.outlined.VolumeDown
import androidx.compose.material.icons.automirrored.outlined.VolumeUp
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.semantics.LiveRegionMode
import androidx.compose.ui.semantics.contentDescription
import androidx.compose.ui.semantics.liveRegion
import androidx.compose.ui.semantics.semantics
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.wear.compose.foundation.ExperimentalWearFoundationApi
import androidx.wear.compose.foundation.rememberActiveFocusRequester
import androidx.wear.compose.foundation.rotary.rotaryScrollable
import androidx.wear.compose.material.InlineSlider
import androidx.wear.compose.material.MaterialTheme
import androidx.wear.compose.material.Stepper
import androidx.wear.compose.material.Text
import com.google.android.horologist.annotations.ExperimentalHorologistApi
import com.google.android.horologist.audio.AudioOutput
import com.google.android.horologist.audio.ui.components.AudioOutputUi
import com.google.android.horologist.audio.ui.components.DeviceChip
import com.google.android.horologist.audio.ui.components.toAudioOutputUi
import com.google.android.horologist.compose.material.Icon
import com.google.android.horologist.compose.material.IconRtlMode
import com.google.android.horologist.compose.material.util.DECORATIVE_ELEMENT_CONTENT_DESCRIPTION
import com.google.android.horologist.images.base.paintable.ImageVectorPaintable.Companion.asPaintable
import kotlin.math.roundToInt
/**
* Volume Screen with an [InlineSlider] and Increase/Decrease buttons for the Audio Stream Volume.
*
* Contains a Stepper with Up and Down buttons, plus a button to show the current [AudioOutput] and
* prompt to select a new one.
*
* The volume and audio output come indirectly from the [AudioManager] and accessed via
* [VolumeViewModel].
*
* See [VolumeViewModel]
* See [AudioManager.STREAM_MUSIC]
*/
@Composable
@ExperimentalHorologistApi
public fun VolumeScreen(
modifier: Modifier = Modifier,
volumeViewModel: VolumeViewModel = viewModel(factory = VolumeViewModel.Factory),
showVolumeIndicator: Boolean = true,
increaseIcon: @Composable () -> Unit = { VolumeScreenDefaults.IncreaseIcon() },
decreaseIcon: @Composable () -> Unit = { VolumeScreenDefaults.DecreaseIcon() },
) {
val volumeUiState by volumeViewModel.volumeUiState.collectAsState()
val audioOutput by volumeViewModel.audioOutput.collectAsState()
VolumeScreen(
modifier = modifier
.rotaryScrollable(
volumeRotaryBehavior(
volumeUiStateProvider = { volumeViewModel.volumeUiState.value },
onRotaryVolumeInput = { newVolume -> volumeViewModel.setVolume(newVolume) },
),
focusRequester = rememberActiveFocusRequester(),
),
volume = { volumeUiState },
audioOutputUi = audioOutput.toAudioOutputUi(),
increaseVolume = { volumeViewModel.increaseVolume() },
decreaseVolume = { volumeViewModel.decreaseVolume() },
onAudioOutputClick = { volumeViewModel.launchOutputSelection() },
showVolumeIndicator = showVolumeIndicator,
increaseIcon = increaseIcon,
decreaseIcon = decreaseIcon,
)
}
/**
* Volume Screen with a Output Device chip.
*/
@Composable
public fun VolumeScreen(
volume: () -> VolumeUiState,
audioOutputUi: AudioOutputUi,
increaseVolume: () -> Unit,
decreaseVolume: () -> Unit,
onAudioOutputClick: () -> Unit,
modifier: Modifier = Modifier,
increaseIcon: @Composable () -> Unit = { VolumeScreenDefaults.IncreaseIcon() },
decreaseIcon: @Composable () -> Unit = { VolumeScreenDefaults.DecreaseIcon() },
showVolumeIndicator: Boolean = true,
) {
VolumeScreen(
volume = volume,
contentSlot = {
DeviceChip(
modifier = Modifier.padding(horizontal = 18.dp),
volumeDescription = if (audioOutputUi.isConnected) {
stringResource(id = R.string.horologist_volume_screen_connected_state)
} else {
stringResource(id = R.string.horologist_volume_screen_not_connected_state)
},
deviceName = audioOutputUi.displayName,
icon = {
Icon(
paintable = audioOutputUi.imageVector.asPaintable(),
contentDescription = DECORATIVE_ELEMENT_CONTENT_DESCRIPTION,
tint = MaterialTheme.colors.onSurfaceVariant,
)
},
onAudioOutputClick = onAudioOutputClick,
)
},
increaseVolume = increaseVolume,
decreaseVolume = decreaseVolume,
modifier = modifier,
increaseIcon = increaseIcon,
decreaseIcon = decreaseIcon,
showVolumeIndicator = showVolumeIndicator,
)
}
/**
* Volume Screen with a simple "Volume" label.
*/
@Composable
public fun VolumeWithLabelScreen(
volume: () -> VolumeUiState,
increaseVolume: () -> Unit,
decreaseVolume: () -> Unit,
modifier: Modifier = Modifier,
increaseIcon: @Composable () -> Unit = { VolumeScreenDefaults.IncreaseIcon() },
decreaseIcon: @Composable () -> Unit = { VolumeScreenDefaults.DecreaseIcon() },
showVolumeIndicator: Boolean = true,
volumeColor: Color = MaterialTheme.colors.secondary,
) {
VolumeScreen(
volume = volume,
contentSlot = {
Text(
stringResource(id = R.string.horologist_volume_screen_volume_label),
style = MaterialTheme.typography.button.copy(
color = MaterialTheme.colors.onBackground,
fontWeight = FontWeight.Normal,
),
maxLines = 1,
overflow = TextOverflow.Clip,
)
},
increaseVolume = increaseVolume,
decreaseVolume = decreaseVolume,
modifier = modifier,
increaseIcon = increaseIcon,
decreaseIcon = decreaseIcon,
showVolumeIndicator = showVolumeIndicator,
volumeColor = volumeColor,
)
}
@Composable
internal fun VolumeScreen(
volume: () -> VolumeUiState,
contentSlot: @Composable () -> Unit,
increaseVolume: () -> Unit,
decreaseVolume: () -> Unit,
modifier: Modifier = Modifier,
increaseIcon: @Composable () -> Unit = { VolumeScreenDefaults.IncreaseIcon() },
decreaseIcon: @Composable () -> Unit = { VolumeScreenDefaults.DecreaseIcon() },
showVolumeIndicator: Boolean = true,
volumeColor: Color = MaterialTheme.colors.secondary,
) {
val volumeState = volume()
val volumePercent = (100f * volumeState.current / volumeState.max).roundToInt()
val volumeDescription = if (volumeState.current == 0) {
stringResource(id = R.string.horologist_volume_screen_volume_zero)
} else {
stringResource(id = R.string.horologist_volume_screen_volume_percent, volumePercent)
}
Stepper(
modifier = modifier.semantics {
liveRegion = LiveRegionMode.Assertive
contentDescription = volumeDescription
},
value = volumeState.current.toFloat(),
onValueChange = { if (it > volumeState.current) increaseVolume() else decreaseVolume() },
steps = volumeState.max - 1,
valueRange = (0f..volumeState.max.toFloat()),
increaseIcon = {
increaseIcon()
},
decreaseIcon = {
decreaseIcon()
},
enableRangeSemantics = false,
) {
contentSlot()
}
if (showVolumeIndicator) {
VolumePositionIndicator(
volumeUiState = { volume() },
color = volumeColor,
)
}
}
public object VolumeScreenDefaults {
@Composable
public fun IncreaseIcon() {
Icon(
modifier = Modifier.size(26.dp),
paintable = Icons.AutoMirrored.Outlined.VolumeUp.asPaintable(),
contentDescription = stringResource(id = R.string.horologist_volume_screen_volume_up_content_description),
rtlMode = IconRtlMode.Mirrored,
)
}
@Composable
public fun DecreaseIcon() {
Icon(
modifier = Modifier.size(26.dp),
paintable = Icons.AutoMirrored.Outlined.VolumeDown.asPaintable(),
contentDescription = stringResource(id = R.string.horologist_volume_screen_volume_down_content_description),
rtlMode = IconRtlMode.Mirrored,
)
}
}