Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mute Audio Default Setting #268

Open
wants to merge 22 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ local.properties
.idea/deploymentTargetDropDown.xml
.idea/gradle.xml
.idea/deploymentTargetSelector.xml
.idea/androidTestResultsUserPreferences.xml
.idea/androidTestResultsUserPreferences.xml
.idea/inspectionProfiles

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,7 @@ interface SettingsModule {
fun bindsSettableConstraintsRepository(
settableConstraintsRepository: SettableConstraintsRepositoryImpl
): SettableConstraintsRepository

@Binds
fun bindsPermissionChecker(localSettingsRepository: LocalPermissionChecker): PermissionChecker


/**
* ConstraintsRepository without setter.
*
Expand Down
3 changes: 3 additions & 0 deletions feature/settings/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ dependencies {

implementation(libs.androidx.core.ktx)

// Accompanist - Permissions
implementation(libs.accompanist.permissions)

// Futures
implementation(libs.futures.ktx)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ internal class CameraAppSettingsViewModelTest {
}
settingsViewModel = SettingsViewModel(
settingsRepository,
FakePermissionChecker(),
constraintsRepository
)
advanceUntilIdle()
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.google.jetpackcamera.settings

import android.Manifest
import android.content.res.Configuration
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Column
Expand All @@ -28,6 +29,9 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.hilt.navigation.compose.hiltViewModel
import com.google.accompanist.permissions.ExperimentalPermissionsApi
import com.google.accompanist.permissions.MultiplePermissionsState
import com.google.accompanist.permissions.rememberMultiplePermissionsState
import com.google.jetpackcamera.settings.model.AspectRatio
import com.google.jetpackcamera.settings.model.CaptureMode
import com.google.jetpackcamera.settings.model.DarkMode
Expand All @@ -47,10 +51,13 @@ import com.google.jetpackcamera.settings.ui.StabilizationSetting
import com.google.jetpackcamera.settings.ui.TargetFpsSetting
import com.google.jetpackcamera.settings.ui.VersionInfo
import com.google.jetpackcamera.settings.ui.theme.SettingsPreviewTheme
import com.google.accompanist.permissions.rememberPermissionState

/**
* Screen used for the Settings feature.
*/

@OptIn(ExperimentalPermissionsApi::class)
@Composable
fun SettingsScreen(
versionInfo: VersionInfoHolder,
Expand All @@ -74,6 +81,13 @@ fun SettingsScreen(
setMaxVideoDuration = viewModel::setMaxVideoDuration,
setDarkMode = viewModel::setDarkMode
)
val permissionStates = rememberMultiplePermissionsState(
permissions =
listOf(
Manifest.permission.CAMERA,
Manifest.permission.RECORD_AUDIO))

viewModel.setGrantedPermissions(permissionStates)
}

@Composable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ sealed interface SettingsUiState {
val lensFlipUiState: FlipLensUiState,
val stabilizationUiState: StabilizationUiState,
val maxVideoDurationUiState: MaxVideoDurationUiState.Enabled,
val muteAudioUiState: MuteAudioUiState
val muteAudioUiState: MuteAudioUiState,
) : SettingsUiState
}

Expand Down Expand Up @@ -262,5 +262,4 @@ val TYPICAL_SETTINGS_UISTATE = SettingsUiState.Enabled(
stabilizationUiState =
StabilizationUiState.Disabled(
DeviceUnsupportedRationale(R.string.stabilization_rationale_prefix)
)
)
))
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ import android.Manifest
import android.util.Log
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.google.accompanist.permissions.ExperimentalPermissionsApi
import com.google.accompanist.permissions.MultiplePermissionsState
import com.google.accompanist.permissions.isGranted
import com.google.jetpackcamera.settings.DisabledRationale.DeviceUnsupportedRationale
import com.google.jetpackcamera.settings.DisabledRationale.FpsUnsupportedRationale
import com.google.jetpackcamera.settings.DisabledRationale.StabilizationUnsupportedRationale
Expand All @@ -32,13 +35,13 @@ import com.google.jetpackcamera.settings.model.Stabilization
import com.google.jetpackcamera.settings.model.SupportedStabilizationMode
import com.google.jetpackcamera.settings.model.SystemConstraints
import dagger.hilt.android.lifecycle.HiltViewModel
import javax.inject.Inject
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.filterNotNull
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.launch
import javax.inject.Inject

private const val TAG = "SettingsViewModel"
private val fpsOptions = setOf(FPS_15, FPS_30, FPS_60)
Expand All @@ -49,9 +52,9 @@ private val fpsOptions = setOf(FPS_15, FPS_30, FPS_60)
@HiltViewModel
class SettingsViewModel @Inject constructor(
private val settingsRepository: SettingsRepository,
private val permissionChecker: PermissionChecker,
constraintsRepository: ConstraintsRepository
) : ViewModel() {
private var grantedPermissions = mutableSetOf<String>()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should make this a MutableStateFlow<Set<String>> so that it can be combined into the below flow. Otherwise you have a race between this being set and the settings/constraints being updated.


val settingsUiState: StateFlow<SettingsUiState> =
combine(
Expand All @@ -67,24 +70,29 @@ class SettingsViewModel @Inject constructor(
flashUiState = FlashUiState.Enabled(updatedSettings.flashMode),
darkModeUiState = DarkModeUiState.Enabled(updatedSettings.darkMode),
muteAudioUiState = getMuteAudioUiState(
permissionChecker,
updatedSettings.audioMuted
updatedSettings.audioMuted, grantedPermissions.contains(Manifest.permission.RECORD_AUDIO)
),
fpsUiState = getFpsUiState(constraints, updatedSettings),
lensFlipUiState = getLensFlipUiState(constraints, updatedSettings),
stabilizationUiState = getStabilizationUiState(constraints, updatedSettings)
stabilizationUiState = getStabilizationUiState(constraints, updatedSettings),
)
}.stateIn(
scope = viewModelScope,
started = SharingStarted.WhileSubscribed(5_000),
initialValue = SettingsUiState.Disabled
)

// ////////////////////////////////////////////////////////////
//
// Get UiStates for components
//
// ////////////////////////////////////////////////////////////

private fun getMuteAudioUiState(
permissionChecker: PermissionChecker,
isMuted: Boolean
isMuted: Boolean,
permissionGranted: Boolean
): MuteAudioUiState {
return if (permissionChecker.isPermissionGranted(Manifest.permission.RECORD_AUDIO)) {
return if (permissionGranted) {
MuteAudioUiState.Enabled(isMuted)
} else {
MuteAudioUiState.Disabled(
Expand All @@ -97,6 +105,16 @@ class SettingsViewModel @Inject constructor(
}
}

@OptIn(ExperimentalPermissionsApi::class)
fun setGrantedPermissions(multiplePermissionsState: MultiplePermissionsState) {
val permissions = mutableSetOf<String>()
for (permissionState in multiplePermissionsState.permissions){
if (permissionState.status.isGranted)
permissions.add(permissionState.permission)
}
grantedPermissions = permissions.toMutableSet()
}

private fun getStabilizationUiState(
systemConstraints: SystemConstraints,
cameraAppSettings: CameraAppSettings
Expand Down Expand Up @@ -408,6 +426,12 @@ class SettingsViewModel @Inject constructor(
return SingleSelectableState.Selectable
}

// ////////////////////////////////////////////////////////////
//
// Settings Repository functions
//
// ////////////////////////////////////////////////////////////

fun setDefaultLensFacing(lensFacing: LensFacing) {
viewModelScope.launch {
settingsRepository.updateDefaultLensFacing(lensFacing)
Expand Down