From 03a3cb32332f08a93f6521f660725344e1ecc61e Mon Sep 17 00:00:00 2001 From: Rene de Groot Date: Mon, 13 Mar 2017 16:54:40 +0100 Subject: [PATCH] Mock to meta-data update / insert --- .../ConnectedServicePresenter.java | 19 +++-- .../common/controllers/ContentController.kt | 2 +- .../gpstracker/ng/map/TrackMapPresenter.kt | 24 +++--- .../gpstracker/ng/map/TrackPresenter.kt | 34 ++++++++- .../gpstracker/ng/util/MockServiceManager.kt | 6 +- .../gpstracker/ng/util/MockTracksProvider.kt | 73 ++++++++++++++----- 6 files changed, 115 insertions(+), 43 deletions(-) diff --git a/studio/app/src/main/java/nl/sogeti/android/gpstracker/ng/common/abstractpresenters/ConnectedServicePresenter.java b/studio/app/src/main/java/nl/sogeti/android/gpstracker/ng/common/abstractpresenters/ConnectedServicePresenter.java index 313ba30..c32a60e 100644 --- a/studio/app/src/main/java/nl/sogeti/android/gpstracker/ng/common/abstractpresenters/ConnectedServicePresenter.java +++ b/studio/app/src/main/java/nl/sogeti/android/gpstracker/ng/common/abstractpresenters/ConnectedServicePresenter.java @@ -75,13 +75,18 @@ public void run() { synchronized (ConnectedServicePresenter.this) { Context context = ConnectedServicePresenter.this.getContext(); if (context != null) { - Uri trackUri = TrackUriExtensionKt.trackUri(serviceManager.getTrackId()); - String name = ContentProviderExtensionsKt.apply(trackUri, context, new Function1() { - @Override - public String invoke(Cursor cursor) { - return ContentProviderExtensionsKt.getString(cursor, NAME); - } - }, null, null); + long trackId = serviceManager.getTrackId(); + Uri trackUri = null; + String name = null; + if (trackId > 0) { + trackUri = TrackUriExtensionKt.trackUri(trackId); + name = ContentProviderExtensionsKt.apply(trackUri, context, new Function1() { + @Override + public String invoke(Cursor cursor) { + return ContentProviderExtensionsKt.getString(cursor, NAME); + } + }, null, null); + } didConnectToService(trackUri, name, serviceManager.getLoggingState()); } } diff --git a/studio/app/src/main/java/nl/sogeti/android/gpstracker/ng/common/controllers/ContentController.kt b/studio/app/src/main/java/nl/sogeti/android/gpstracker/ng/common/controllers/ContentController.kt index 5c4d53b..bff7776 100644 --- a/studio/app/src/main/java/nl/sogeti/android/gpstracker/ng/common/controllers/ContentController.kt +++ b/studio/app/src/main/java/nl/sogeti/android/gpstracker/ng/common/controllers/ContentController.kt @@ -36,7 +36,7 @@ class ContentController(val context: Context, val listener: ContentListener) { fun unregister() { if (registeredUri != null) { context.contentResolver.unregisterContentObserver(contentObserver) - registeredUri = null4 + registeredUri = null4ยง } } diff --git a/studio/app/src/main/java/nl/sogeti/android/gpstracker/ng/map/TrackMapPresenter.kt b/studio/app/src/main/java/nl/sogeti/android/gpstracker/ng/map/TrackMapPresenter.kt index e113014..4bc2a78 100644 --- a/studio/app/src/main/java/nl/sogeti/android/gpstracker/ng/map/TrackMapPresenter.kt +++ b/studio/app/src/main/java/nl/sogeti/android/gpstracker/ng/map/TrackMapPresenter.kt @@ -45,8 +45,8 @@ import nl.sogeti.android.gpstracker.ng.utils.* import javax.inject.Inject class TrackMapPresenter(private val viewModel: TrackMapViewModel) : ConnectedServicePresenter(), OnMapReadyCallback, ContentController.ContentListener, TrackSelection.Listener { - private var executingReader: TrackReader? = null + private var executingReader: TrackReader? = null private var contentController: ContentController? = null private var googleMap: GoogleMap? = null @@ -107,21 +107,13 @@ class TrackMapPresenter(private val viewModel: TrackMapViewModel) : ConnectedSer //endregion - /* Content watching */ + //region Content watching override fun onChangeUriContent(contentUri: Uri, changesUri: Uri) { startReadingTrack(contentUri) } - private fun startReadingTrack(trackUri: Uri) { - var executingReader = this.executingReader - if (executingReader == null || executingReader.trackUri != trackUri) { - executingReader?.cancel(true) - executingReader = TrackReader(trackUri, viewModel) - executingReader.execute() - this.executingReader = executingReader - } - } + //endregion /* Google Map Tiles */ @@ -141,6 +133,16 @@ class TrackMapPresenter(private val viewModel: TrackMapViewModel) : ConnectedSer /* Private */ + private fun startReadingTrack(trackUri: Uri) { + var executingReader = this.executingReader + if (executingReader == null || executingReader.trackUri != trackUri) { + executingReader?.cancel(true) + executingReader = TrackReader(trackUri, viewModel) + executingReader.execute() + this.executingReader = executingReader + } + } + private fun makeTrackSelection() { val trackUri = trackSelection.trackUri if (trackUri != null && trackUri.lastPathSegment != "-1") { diff --git a/studio/app/src/main/java/nl/sogeti/android/gpstracker/ng/map/TrackPresenter.kt b/studio/app/src/main/java/nl/sogeti/android/gpstracker/ng/map/TrackPresenter.kt index f0bcae8..923bdf4 100644 --- a/studio/app/src/main/java/nl/sogeti/android/gpstracker/ng/map/TrackPresenter.kt +++ b/studio/app/src/main/java/nl/sogeti/android/gpstracker/ng/map/TrackPresenter.kt @@ -29,15 +29,23 @@ package nl.sogeti.android.gpstracker.ng.map import android.net.Uri +import nl.sogeti.android.gpstracker.integration.ContentConstants import nl.sogeti.android.gpstracker.ng.common.GpsTrackerApplication import nl.sogeti.android.gpstracker.ng.common.abstractpresenters.ContextedPresenter +import nl.sogeti.android.gpstracker.ng.common.controllers.ContentController +import nl.sogeti.android.gpstracker.ng.common.controllers.ContentControllerProvider import nl.sogeti.android.gpstracker.ng.model.TrackSelection +import nl.sogeti.android.gpstracker.ng.utils.apply +import nl.sogeti.android.gpstracker.ng.utils.getString import javax.inject.Inject -class TrackPresenter(private val viewModel: TrackViewModel, private val view: TrackViewModel.View) : ContextedPresenter(), TrackSelection.Listener { +class TrackPresenter(private val viewModel: TrackViewModel, private val view: TrackViewModel.View) : ContextedPresenter(), TrackSelection.Listener, ContentController.ContentListener { + private var contentController: ContentController? = null @Inject lateinit var trackSelection: TrackSelection + @Inject + lateinit var contentControllerProvider: ContentControllerProvider init { GpsTrackerApplication.appComponent.inject(this) @@ -47,14 +55,16 @@ class TrackPresenter(private val viewModel: TrackViewModel, private val view: Tr override fun didStart() { trackSelection.addListener(this) + contentController = contentControllerProvider.createContentControllerProvider(context!!, this) trackSelection.trackUri?.let { didSelectTrack(it, trackSelection.trackName) } - } override fun willStop() { trackSelection.removeListener(this) + contentController?.unregisterObserver() + contentController = null } //endregion @@ -79,6 +89,26 @@ class TrackPresenter(private val viewModel: TrackViewModel, private val view: Tr override fun didSelectTrack(trackUri: Uri, name: String) { viewModel.trackUri.set(trackUri) + contentController?.registerObserver(trackUri) + showName(name) + } + + //endregion + + //region Content watching + + override fun onChangeUriContent(contentUri: Uri, changesUri: Uri) { + val name = contentUri.apply(context!!, { it.getString(ContentConstants.Tracks.NAME) }) + name?.let { + showName(it) + } + } + + //endregion + + //region Private + + private fun showName(name: String) { viewModel.name.set(name) view.showTrackName(name) } diff --git a/studio/app/src/mock/java/nl/sogeti/android/gpstracker/ng/util/MockServiceManager.kt b/studio/app/src/mock/java/nl/sogeti/android/gpstracker/ng/util/MockServiceManager.kt index 3c77883..8e4f6ce 100644 --- a/studio/app/src/mock/java/nl/sogeti/android/gpstracker/ng/util/MockServiceManager.kt +++ b/studio/app/src/mock/java/nl/sogeti/android/gpstracker/ng/util/MockServiceManager.kt @@ -82,13 +82,13 @@ class MockServiceManager : ServiceManagerInterface { fun reset() { started = false globalState.loggingState = STATE_UNKNOWN - globalState.trackId = 5L + globalState.trackId = -1L } companion object globalState { var started = false var loggingState = STATE_UNKNOWN - var trackId = 5L + var trackId = -1L var segmentId = 10L var waypointId = 100L } @@ -117,7 +117,7 @@ class MockServiceManager : ServiceManagerInterface { } private fun recordNewTrack() { - trackId++ + if (trackId > 0) trackId++ else trackId = 2 MockTracksProvider.globalState.addTrack(trackId) recordNewSegment() } diff --git a/studio/app/src/mock/java/nl/sogeti/android/gpstracker/ng/util/MockTracksProvider.kt b/studio/app/src/mock/java/nl/sogeti/android/gpstracker/ng/util/MockTracksProvider.kt index cd902d7..02fc51a 100644 --- a/studio/app/src/mock/java/nl/sogeti/android/gpstracker/ng/util/MockTracksProvider.kt +++ b/studio/app/src/mock/java/nl/sogeti/android/gpstracker/ng/util/MockTracksProvider.kt @@ -51,26 +51,57 @@ class MockTracksProvider : ContentProvider() { if (content != null) { cursor = globalState.buildMatrixCursor(content.first, content.second) } else { - Timber.w("Query on $uri did not match anything in global state $globalState ") + Timber.e("Query on $uri did not match anything in global state $globalState ") } return cursor } - override fun getType(uri: Uri): String? { - return null + override fun insert(uri: Uri, values: ContentValues?): Uri? { + val content = globalState.uriContent[uri] + if (content != null && values != null) { + val row = mutableListOf() + val columns = content.first + for (index in 0..columns.size - 1) { + val column = columns[index] + val value = values.getAsString(column) + row.add(value) + } + content.second.add(row) + } else { + Timber.e("Insert on $uri did not match anything in global state $globalState ") + } + + return uri } - override fun insert(uri: Uri, values: ContentValues?): Uri? { - return null + override fun update(uri: Uri, values: ContentValues?, selection: String?, selectionArgs: Array?): Int { + var changed = 0 + val content = globalState.uriContent[uri] + if (content != null && values != null && content.second.size > 0) { + val row = content.second.first() + val columns = content.first + for (index in 0..columns.size - 1) { + val column = columns[index] + val value = values.getAsString(column) + value?.let { row[index] = value; changed++ } + } + globalState.context?.contentResolver?.notifyChange(uri, null) + } else { + Timber.e("Update on $uri did not match anything in global state $globalState ") + } + + return changed } override fun delete(uri: Uri, selection: String?, selectionArgs: Array?): Int { + Timber.e("Delete on $uri did not match anything in global state $globalState ") return 0 } - override fun update(uri: Uri, values: ContentValues?, selection: String?, selectionArgs: Array?): Int { - return 0 + override fun getType(uri: Uri): String? { + Timber.e("getType on $uri did not match anything in global state $globalState ") + return null } fun reset() { @@ -79,7 +110,7 @@ class MockTracksProvider : ContentProvider() { companion object globalState { var context: Context? = null - val uriContent = mutableMapOf, MutableList>>>() + val uriContent = mutableMapOf, MutableList>>>() get() { val preload = gpxAmsterdam if (!preload.isEmpty()) { @@ -105,7 +136,7 @@ class MockTracksProvider : ContentProvider() { } fun addTrack(trackId: Long) { - // .../track + // .../tracks val tracksUri = tracksUri() var content = uriContent[tracksUri] if (content == null) { @@ -114,18 +145,22 @@ class MockTracksProvider : ContentProvider() { } addContentToTrackContent(content.second, trackId) context?.contentResolver?.notifyChange(tracksUri, null) - // .../track/id + // .../tracks/id content = createEmptyTrackContent() val trackUri = trackUri(trackId) uriContent[trackUri] = content addContentToTrackContent(content.second, trackId) context?.contentResolver?.notifyChange(trackUri, null) + // tracks/id/metadata + val metaContent = Pair(arrayOf(MetaDataColumns.KEY, MetaDataColumns.VALUE), mutableListOf>()) + val metaUri = metaDataTrackUri(trackId) + uriContent[metaUri] = metaContent } - private fun createEmptyTrackContent() = Pair(arrayOf(Tracks._ID, Tracks.NAME, Tracks.CREATION_TIME), mutableListOf>()) + private fun createEmptyTrackContent() = Pair(arrayOf(Tracks._ID, Tracks.NAME, Tracks.CREATION_TIME), mutableListOf>()) - private fun addContentToTrackContent(content: MutableList>, trackId: Long) { - content.add(listOf( + private fun addContentToTrackContent(content: MutableList>, trackId: Long) { + content.add(mutableListOf( trackId, "track $trackId", Date().time)) @@ -149,10 +184,10 @@ class MockTracksProvider : ContentProvider() { context?.contentResolver?.notifyChange(segmentUri, null) } - private fun createEmptySegmentContent() = Pair(arrayOf(Segments._ID, Segments.TRACK), mutableListOf>()) + private fun createEmptySegmentContent() = Pair(arrayOf(Segments._ID, Segments.TRACK), mutableListOf>()) - private fun addContentToSegmentContent(content: MutableList>, trackId: Long, segmentId: Long) { - content.add(listOf( + private fun addContentToSegmentContent(content: MutableList>, trackId: Long, segmentId: Long) { + content.add(mutableListOf( segmentId, trackId)) } @@ -178,10 +213,10 @@ class MockTracksProvider : ContentProvider() { context?.contentResolver?.notifyChange(waypointsTrackUri, null) } - private fun createEmptyWaypointContent() = Pair(arrayOf(Waypoints._ID, Waypoints.SEGMENT, Waypoints.LATITUDE, Waypoints.LONGITUDE, Waypoints.TIME), mutableListOf>()) + private fun createEmptyWaypointContent() = Pair(arrayOf(Waypoints._ID, Waypoints.SEGMENT, Waypoints.LATITUDE, Waypoints.LONGITUDE, Waypoints.TIME), mutableListOf>()) - private fun addContentToWaypointContent(content: MutableList>, segmentId: Long, waypointId: Long, latitude: Double, longitude: Double, time: Long) { - content.add(listOf(waypointId, segmentId, latitude, longitude, time)) + private fun addContentToWaypointContent(content: MutableList>, segmentId: Long, waypointId: Long, latitude: Double, longitude: Double, time: Long) { + content.add(mutableListOf(waypointId, segmentId, latitude, longitude, time)) } fun buildMatrixCursor(columns: Array, content: List>): Cursor? {