From 18b2098a4f4885fe11f7bc83693d329d870bb21b Mon Sep 17 00:00:00 2001 From: Leonard Kugis Date: Tue, 26 Dec 2023 03:12:52 +0100 Subject: Fixed fake location parameters entry --- .../domain/usecases/FakeLocationStateUseCase.kt | 26 +++-- .../features/location/FakeLocationFragment.kt | 45 +++++---- app/src/main/res/layout/fragment_fake_location.xml | 108 +++++++++------------ 3 files changed, 87 insertions(+), 92 deletions(-) (limited to 'app') diff --git a/app/src/main/java/foundation/e/advancedprivacy/domain/usecases/FakeLocationStateUseCase.kt b/app/src/main/java/foundation/e/advancedprivacy/domain/usecases/FakeLocationStateUseCase.kt index 2a69b3a..581df09 100644 --- a/app/src/main/java/foundation/e/advancedprivacy/domain/usecases/FakeLocationStateUseCase.kt +++ b/app/src/main/java/foundation/e/advancedprivacy/domain/usecases/FakeLocationStateUseCase.kt @@ -60,7 +60,7 @@ class FakeLocationStateUseCase( init { coroutineScope.launch { localStateRepository.fakeLocationEnabled.collect { - applySettings(it, localStateRepository.fakeLocation) + applySettings(it, it) } } } @@ -73,12 +73,16 @@ class FakeLocationStateUseCase( permissionsModule.toggleDangerousPermission(appDesc, android.Manifest.permission.ACCESS_FINE_LOCATION, true) } - private fun applySettings(isEnabled: Boolean, fakeLocation: Pair, isSpecificLocation: Boolean = false) { - _configuredLocationMode.value = computeLocationMode(isEnabled, fakeLocation, isSpecificLocation) + private fun applySettings(isEnabled: Boolean, isSpecificLocation: Boolean = false) { + _configuredLocationMode.value = computeLocationMode(isEnabled, localStateRepository.fakeAltitude, localStateRepository.fakeSpeed, localStateRepository.fakeJitter, localStateRepository.fakeLocation, isSpecificLocation) if (isEnabled && hasAcquireMockLocationPermission()) { fakeLocationModule.startFakeLocation() - fakeLocationModule.setFakeLocation(localStateRepository.fakeAltitude.toDouble(), localStateRepository.fakeSpeed, localStateRepository.fakeJitter, fakeLocation.first.toDouble(), fakeLocation.second.toDouble()) + fakeLocationModule.setFakeLocation(localStateRepository.fakeAltitude.toDouble(), + localStateRepository.fakeSpeed, + localStateRepository.fakeJitter, + localStateRepository.fakeLocation.first.toDouble(), + localStateRepository.fakeLocation.second.toDouble()) localStateRepository.locationMode.value = configuredLocationMode.value.mode } else { fakeLocationModule.stopFakeLocation() @@ -95,6 +99,7 @@ class FakeLocationStateUseCase( localStateRepository.fakeAltitude = altitude localStateRepository.fakeSpeed = speed localStateRepository.fakeJitter = jitter + applySettings(localStateRepository.fakeLocationEnabled.value, localStateRepository.fakeLocationEnabled.value) } fun setSpecificLocation(latitude: Float, longitude: Float) { @@ -111,16 +116,19 @@ class FakeLocationStateUseCase( private fun setFakeLocation(location: Pair, isSpecificLocation: Boolean = false) { localStateRepository.fakeLocation = location localStateRepository.setFakeLocationEnabled(true) - applySettings(true, location, isSpecificLocation) + applySettings(true, isSpecificLocation) } fun stopFakeLocation() { localStateRepository.setFakeLocationEnabled(false) - applySettings(false, localStateRepository.fakeLocation) + applySettings(false, false) } private fun computeLocationMode( isFakeLocationEnabled: Boolean, + altitude: Float, + speed: Float, + jitter: Float, fakeLocation: Pair, isSpecificLocation: Boolean = false, ): FakeLocationState { @@ -132,9 +140,9 @@ class FakeLocationStateUseCase( else -> LocationMode.SPECIFIC_LOCATION }, null, - null, - null, - null, + altitude, + speed, + jitter, false, fakeLocation.first, fakeLocation.second, diff --git a/app/src/main/java/foundation/e/advancedprivacy/features/location/FakeLocationFragment.kt b/app/src/main/java/foundation/e/advancedprivacy/features/location/FakeLocationFragment.kt index 2ba8613..e31f21d 100644 --- a/app/src/main/java/foundation/e/advancedprivacy/features/location/FakeLocationFragment.kt +++ b/app/src/main/java/foundation/e/advancedprivacy/features/location/FakeLocationFragment.kt @@ -246,13 +246,17 @@ class FakeLocationFragment : NavToolbarFragment(R.layout.fragment_fake_location) } private fun updateMockLocationParameters() { - viewModel.submitAction( - Action.UpdateMockLocationParameters( - binding.altitude.text.toString().toFloat(), - binding.speed.text.toString().toFloat(), - binding.jitter.text.toString().toFloat(), + try { + viewModel.submitAction( + Action.UpdateMockLocationParameters( + binding.edittextAltitude.text.toString().toFloat(), + binding.edittextSpeed.text.toString().toFloat(), + binding.edittextJitter.text.toString().toFloat(), + ) ) - ) + } catch (e: NumberFormatException) { + Timber.e("Unfiltered wrong altitude/speed/jitter format") + } } @Suppress("UNUSED_PARAMETER") @@ -292,7 +296,7 @@ class FakeLocationFragment : NavToolbarFragment(R.layout.fragment_fake_location) private val latLonOnFocusChangeListener = object : View.OnFocusChangeListener { override fun onFocusChange(v: View?, hasFocus: Boolean) { - if (!isEditingLatLon) { + if (!isEditingLatLon && !binding.edittextAltitude.isFocused && !binding.edittextSpeed.isFocused && !binding.edittextJitter.isFocused) { (context?.getSystemService(Context.INPUT_METHOD_SERVICE) as? InputMethodManager)?.hideSoftInputFromWindow( v?.windowToken, 0 @@ -317,11 +321,14 @@ class FakeLocationFragment : NavToolbarFragment(R.layout.fragment_fake_location) } } - binding.altitude.addTextChangedListener(afterTextChanged = ::onAltitudeTextChanged) - binding.speed.addTextChangedListener(afterTextChanged = ::onSpeedTextChanged) - binding.jitter.addTextChangedListener(afterTextChanged = ::onJitterTextChanged) + binding.edittextAltitude.addTextChangedListener(afterTextChanged = ::onAltitudeTextChanged) + binding.edittextSpeed.addTextChangedListener(afterTextChanged = ::onSpeedTextChanged) + binding.edittextJitter.addTextChangedListener(afterTextChanged = ::onJitterTextChanged) binding.edittextLatitude.addTextChangedListener(afterTextChanged = ::onLatTextChanged) binding.edittextLongitude.addTextChangedListener(afterTextChanged = ::onLonTextChanged) + binding.edittextAltitude.onFocusChangeListener = latLonOnFocusChangeListener + binding.edittextSpeed.onFocusChangeListener = latLonOnFocusChangeListener + binding.edittextJitter.onFocusChangeListener = latLonOnFocusChangeListener binding.edittextLatitude.onFocusChangeListener = latLonOnFocusChangeListener binding.edittextLongitude.onFocusChangeListener = latLonOnFocusChangeListener } @@ -336,18 +343,18 @@ class FakeLocationFragment : NavToolbarFragment(R.layout.fragment_fake_location) binding.mapView.isEnabled = (state.mode == LocationMode.SPECIFIC_LOCATION) - binding.altitude.isEnabled = state.mode == LocationMode.SPECIFIC_LOCATION - binding.speed.isEnabled = state.mode == LocationMode.SPECIFIC_LOCATION - binding.jitter.isEnabled = state.mode == LocationMode.SPECIFIC_LOCATION + binding.textlayoutAltitude.isVisible = state.mode == LocationMode.SPECIFIC_LOCATION + binding.textlayoutSpeed.isVisible = state.mode == LocationMode.SPECIFIC_LOCATION + binding.textlayoutJitter.isVisible = state.mode == LocationMode.SPECIFIC_LOCATION - if(!binding.altitude.isFocused) - binding.altitude.setText(state.altitude?.toString()) + if(!binding.edittextAltitude.isFocused) + binding.edittextAltitude.setText(state.altitude?.toString()) - if(!binding.speed.isFocused) - binding.speed.setText(state.speed?.toString()) + if(!binding.edittextSpeed.isFocused) + binding.edittextSpeed.setText(state.speed?.toString()) - if(!binding.jitter.isFocused) - binding.jitter.setText(state.jitter?.toString()) + if(!binding.edittextJitter.isFocused) + binding.edittextJitter.setText(state.jitter?.toString()) if (state.mode == LocationMode.REAL_LOCATION) { binding.centeredMarker.isVisible = false diff --git a/app/src/main/res/layout/fragment_fake_location.xml b/app/src/main/res/layout/fragment_fake_location.xml index 5483bc2..7890edc 100644 --- a/app/src/main/res/layout/fragment_fake_location.xml +++ b/app/src/main/res/layout/fragment_fake_location.xml @@ -84,84 +84,63 @@ /> - - - - - + - - - - - - - + - - - - - - - - - - + +