diff options
Diffstat (limited to 'app')
3 files changed, 55 insertions, 19 deletions
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 27a2104..76a1e69 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 @@ -42,6 +42,7 @@ import kotlinx.coroutines.flow.update import kotlinx.coroutines.launch import timber.log.Timber import kotlin.random.Random +import android.util.Log class FakeLocationStateUseCase( private val fakeLocationModule: FakeLocationModule, @@ -215,12 +216,14 @@ class FakeLocationStateUseCase( null, null, false, - route, - false, + route ?: localStateRepository.route, + localStateRepository.routeLoopEnabled, false ) localStateRepository.setLocationMode(LocationMode.ROUTE) + if(route != null) + localStateRepository.route = route } fun setRoute(route: List<FakeLocationCoordinate>) { @@ -234,13 +237,16 @@ class FakeLocationStateUseCase( null, false, route, - false, + localStateRepository.routeLoopEnabled, false ) + + localStateRepository.route = route } fun routeStart() { if (hasAcquireMockLocationPermission()) { + fakeLocationModule.startFakeLocation() fakeLocationModule.routeStart(localStateRepository.route, localStateRepository.routeLoopEnabled) } else { useRealLocation() @@ -249,6 +255,7 @@ class FakeLocationStateUseCase( fun routeStop() { if (hasAcquireMockLocationPermission()) { + fakeLocationModule.stopFakeLocation() fakeLocationModule.routeStop() } else { useRealLocation() 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 b70ae36..a155216 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 @@ -67,7 +67,13 @@ import timber.log.Timber import com.google.gson.Gson import com.google.gson.reflect.TypeToken import foundation.e.advancedprivacy.domain.entities.FakeLocationCoordinate -import java.io.File +import java.io.BufferedReader +import java.io.IOException +import java.io.InputStream +import java.io.InputStreamReader +import android.util.Log +import kotlin.math.sqrt +import kotlin.math.pow class FakeLocationFragment : NavToolbarFragment(R.layout.fragment_fake_location) { @@ -341,11 +347,26 @@ class FakeLocationFragment : NavToolbarFragment(R.layout.fragment_fake_location) registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result -> if (result.resultCode == Activity.RESULT_OK) { result.data?.data?.let { uri -> - if(uri.path != null) { - var routeFile = File(uri.path ?: ".") - //val filePath = selectedFile?.uri?.path ?: "Path not found" - //binding.locationRoutePath.text = "Path: $filePath" - route = Gson().fromJson(routeFile.readText(Charsets.UTF_8), object : TypeToken<List<FakeLocationCoordinate>>() {}.type) + var activity = getActivity() + if(uri.path != null && activity != null) { + var inputStream: InputStream? = null + var reader: BufferedReader? = null + var route_str: String? = null + try { + inputStream = activity.contentResolver.openInputStream(uri) + reader = BufferedReader(InputStreamReader(inputStream)) + route_str = reader.readLines().joinToString("") + } catch(e: IOException) { + Log.e("FakeLocationFragment", "Error reading JSON file", e) + } finally { + try { + reader?.close() + inputStream?.close() + } catch (e: IOException) { + Log.e("FakeLocationFragment", "Error closing streams", e) + } + } + route = Gson().fromJson(route_str, object : TypeToken<List<FakeLocationCoordinate>>() {}.type) var route_buf = route route_buf?.let { viewModel.submitAction(Action.SetRoute(route_buf)) diff --git a/app/src/main/java/foundation/e/advancedprivacy/features/location/FakeLocationViewModel.kt b/app/src/main/java/foundation/e/advancedprivacy/features/location/FakeLocationViewModel.kt index c88c638..049c707 100644 --- a/app/src/main/java/foundation/e/advancedprivacy/features/location/FakeLocationViewModel.kt +++ b/app/src/main/java/foundation/e/advancedprivacy/features/location/FakeLocationViewModel.kt @@ -37,6 +37,7 @@ import kotlinx.coroutines.launch import kotlinx.coroutines.withContext import kotlin.time.Duration.Companion.milliseconds import foundation.e.advancedprivacy.domain.entities.FakeLocationCoordinate +import android.util.Log class FakeLocationViewModel( private val fakeLocationStateUseCase: FakeLocationStateUseCase @@ -58,8 +59,8 @@ class FakeLocationViewModel( private val specificLocationInputFlow = MutableSharedFlow<Action.SetSpecificLocationAction>() private val mockLocationParametersInputFlow = MutableSharedFlow<Action.UpdateMockLocationParameters>() - private val setRouteLoopEnabledInputFlow = MutableSharedFlow<Action.SetRouteLoopEnabledAction>() - private val setRouteInputFlow = MutableSharedFlow<Action.SetRoute>() + private val routeLoopEnabledInputFlow = MutableSharedFlow<Action.SetRouteLoopEnabledAction>() + private val routeInputFlow = MutableSharedFlow<Action.SetRoute>() @OptIn(FlowPreview::class) suspend fun doOnStartedState() = withContext(Dispatchers.Main) { @@ -73,7 +74,9 @@ class FakeLocationViewModel( speed = ss.speed, jitter = ss.jitter, specificLatitude = ss.specificLatitude, - specificLongitude = ss.specificLongitude + specificLongitude = ss.specificLongitude, + route = ss.route, + loopRoute = ss.loopRoute ) } }, @@ -85,14 +88,14 @@ class FakeLocationViewModel( .debounce(SET_MOCK_LOCATION_PARAMETERS_DELAY).map { action -> fakeLocationStateUseCase.setFakeLocationParameters(action.altitude, action.speed, action.jitter) }, - setRouteLoopEnabledInputFlow - .debounce(SET_ROUTE_LOOP_ENABLED_DELAY).map { action -> - fakeLocationStateUseCase.setRouteLoopEnabled(action.isEnabled) - }, - setRouteInputFlow + routeInputFlow .debounce(SET_ROUTE_DELAY).map { action -> fakeLocationStateUseCase.setRoute(action.route) }, + routeLoopEnabledInputFlow + .debounce(SET_ROUTE_LOOP_ENABLED_DELAY).map { action -> + fakeLocationStateUseCase.setRouteLoopEnabled(action.isEnabled) + }, ).collect {} } } @@ -106,7 +109,7 @@ class FakeLocationViewModel( is Action.UseRealLocationAction -> fakeLocationStateUseCase.useRealLocation() is Action.UseRoute -> fakeLocationStateUseCase.useRoute() is Action.UpdateMockLocationParameters -> updateMockLocationParameters(action) - is Action.SetRoute -> setRouteInputFlow.emit(action) + is Action.SetRoute -> setRoute(action) is Action.SetRouteLoopEnabledAction -> setRouteLoopEnabled(action) is Action.RouteStartAction -> fakeLocationStateUseCase.routeStart() is Action.RouteStopAction -> fakeLocationStateUseCase.routeStop() @@ -128,8 +131,13 @@ class FakeLocationViewModel( mockLocationParametersInputFlow.emit(action) } + private suspend fun setRoute(action: Action.SetRoute) { + fakeLocationStateUseCase.setRoute(action.route) + routeInputFlow.emit(action) + } + private suspend fun setRouteLoopEnabled(action: Action.SetRouteLoopEnabledAction) { - setRouteLoopEnabledInputFlow.emit(action) + routeLoopEnabledInputFlow.emit(action) } sealed class SingleEvent { |