Skip to content

Commit

Permalink
enable historic data for blitzortung data source
Browse files Browse the repository at this point in the history
  • Loading branch information
wuan committed Apr 23, 2016
1 parent 3511511 commit 5b6b09e
Show file tree
Hide file tree
Showing 16 changed files with 69 additions and 95 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
Copyright 2015 Andreas Würl
Copyright 2015, 2016 Andreas Würl
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand All @@ -23,64 +23,49 @@ import android.util.Log
import android.view.View
import android.widget.ImageButton
import android.widget.Toast
import org.blitzortung.android.app.BOApplication
import org.blitzortung.android.app.AppService
import org.blitzortung.android.app.ButtonGroup
import org.blitzortung.android.app.Main
import org.blitzortung.android.app.R
import kotlinx.android.synthetic.main.map_overlay.*
import org.blitzortung.android.app.*
import org.blitzortung.android.data.DataChannel
import org.blitzortung.android.data.DataHandler
import org.blitzortung.android.data.provider.result.ResultEvent
import org.blitzortung.android.protocol.Event

class HistoryController(activity: Activity, private val buttonHandler: ButtonColumnHandler<ImageButton, ButtonGroup>) {
class HistoryController(private val activity: Activity, private val buttonHandler: ButtonColumnHandler<ImageButton, ButtonGroup>) {

private var appService: AppService? = null

private val dataHandler: DataHandler = BOApplication.dataHandler

private val buttons: MutableCollection<ImageButton> = arrayListOf()

private lateinit var historyRewind: ImageButton

private lateinit var historyForward: ImageButton

private lateinit var goRealtime: ImageButton

val dataConsumer = { event: Event ->
if (event is ResultEvent) {
setRealtimeData(event.containsRealtimeData())
}
}

init {
setupHistoryRewindButton(activity)
setupHistoryForwardButton(activity)
setupGoRealtimeButton(activity)
setupHistoryRewindButton()
setupHistoryForwardButton()
setupGoRealtimeButton()

setRealtimeData(true)
}

fun setRealtimeData(realtimeData: Boolean) {
if (dataHandler.isCapableOfHistoricalData) {
historyRewind.visibility = View.VISIBLE
val historyButtonsVisibility = if (realtimeData) View.INVISIBLE else View.VISIBLE
historyForward.visibility = historyButtonsVisibility
goRealtime.visibility = historyButtonsVisibility
} else {
historyRewind.visibility = View.INVISIBLE
historyForward.visibility = View.INVISIBLE
goRealtime.visibility = View.INVISIBLE
}
activity.historyRew.visibility = View.VISIBLE
val historyButtonsVisibility = if (realtimeData) View.INVISIBLE else View.VISIBLE
activity.historyFfwd.visibility = historyButtonsVisibility
activity.goRealtime.visibility = historyButtonsVisibility
updateButtonColumn()
}

private fun setupHistoryRewindButton(activity: Activity) {
historyRewind = addButtonWithAction(activity, R.id.historyRew, { v ->
private fun setupHistoryRewindButton() {
addButtonWithOnClickAction(activity.historyRew, { v ->
if (dataHandler.rewInterval()) {
disableButtonColumn()
historyForward.visibility = View.VISIBLE
goRealtime.visibility = View.VISIBLE
activity.historyFfwd.visibility = View.VISIBLE
activity.goRealtime.visibility = View.VISIBLE
updateButtonColumn()
updateData()
} else {
Expand All @@ -90,8 +75,8 @@ class HistoryController(activity: Activity, private val buttonHandler: ButtonCol
})
}

private fun setupHistoryForwardButton(activity: Activity) {
historyForward = addButtonWithAction(activity, R.id.historyFfwd, { v ->
private fun setupHistoryForwardButton() {
addButtonWithOnClickAction(activity.historyFfwd, { v ->
if (dataHandler.ffwdInterval()) {
if (dataHandler.isRealtime) {
configureForRealtimeOperation()
Expand All @@ -102,16 +87,16 @@ class HistoryController(activity: Activity, private val buttonHandler: ButtonCol
})
}

private fun setupGoRealtimeButton(activity: Activity) {
goRealtime = addButtonWithAction(activity, R.id.goRealtime, { v ->
private fun setupGoRealtimeButton() {
addButtonWithOnClickAction(activity.goRealtime, { v ->
if (dataHandler.goRealtime()) {
configureForRealtimeOperation()
}
})
}

private fun addButtonWithAction(activity: Activity, id: Int, action: (View) -> Unit): ImageButton {
return (activity.findViewById(id) as ImageButton).apply {
private fun addButtonWithOnClickAction(button: ImageButton, action: (View) -> Unit): ImageButton {
return button.apply {
buttons.add(this)
visibility = View.INVISIBLE
setOnClickListener(action)
Expand All @@ -120,9 +105,10 @@ class HistoryController(activity: Activity, private val buttonHandler: ButtonCol

private fun configureForRealtimeOperation() {
disableButtonColumn()
historyForward.visibility = View.INVISIBLE
goRealtime.visibility = View.INVISIBLE
activity.historyFfwd.visibility = View.INVISIBLE
activity.goRealtime.visibility = View.INVISIBLE
updateButtonColumn()
dataHandler.updateData()
appService?.restart()
}

Expand Down
17 changes: 10 additions & 7 deletions app/src/main/java/org/blitzortung/android/data/DataHandler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,11 @@ import org.blitzortung.android.protocol.ConsumerContainer
import java.util.*
import java.util.concurrent.locks.ReentrantLock

class DataHandler @JvmOverloads constructor(private val wakeLock: PowerManager.WakeLock, private val agentSuffix: String,
private val dataProviderFactory: DataProviderFactory = DataProviderFactory()) : OnSharedPreferenceChangeListener {
class DataHandler @JvmOverloads constructor(
private val wakeLock: PowerManager.WakeLock,
private val agentSuffix: String,
private val dataProviderFactory: DataProviderFactory = DataProviderFactory()
) : OnSharedPreferenceChangeListener {

private val sharedPreferences = BOApplication.sharedPreferences

Expand Down Expand Up @@ -160,6 +163,9 @@ class DataHandler @JvmOverloads constructor(private val wakeLock: PowerManager.W
dataProvider!!.reset()
notifyDataReset()
}

else -> {
}
}
}

Expand Down Expand Up @@ -212,9 +218,6 @@ class DataHandler @JvmOverloads constructor(private val wakeLock: PowerManager.W
val isRealtime: Boolean
get() = parameters.isRealtime()

val isCapableOfHistoricalData: Boolean
get() = dataProvider!!.isCapableOfHistoricalData

private open inner class FetchDataTask : AsyncTask<TaskParameters, Int, ResultEvent>() {

override fun onProgressUpdate(vararg values: Int?) {
Expand Down Expand Up @@ -280,11 +283,11 @@ class DataHandler @JvmOverloads constructor(private val wakeLock: PowerManager.W
}
}

override fun doInBackground(vararg params: TaskParameters): ResultEvent? {
override fun doInBackground(vararg taskParametersArray: TaskParameters): ResultEvent? {
wakeLock.acquire()
Log.v(Main.LOG_TAG, "FetchBackgroundDataTask aquire wakelock " + wakeLock)

val taskParameters = params[0]
val taskParameters = taskParametersArray[0]
val updatedParameters = taskParameters.parameters.copy(intervalDuration = 10)
val updatedParams = arrayOf(taskParameters.copy(parameters = updatedParameters))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ abstract class DataProvider(

abstract fun reset()

abstract val isCapableOfHistoricalData: Boolean

abstract fun <T> retrieveData(retrieve: DataRetriever.() -> T): T

interface DataRetriever {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,6 @@ class BlitzortungHttpDataProvider @JvmOverloads constructor(
strikes = emptyList()
}

override val isCapableOfHistoricalData: Boolean = false

enum class Type {
STRIKES, STATIONS
}
Expand All @@ -138,15 +136,16 @@ class BlitzortungHttpDataProvider @JvmOverloads constructor(
}

val intervalDuration = parameters.intervalDuration
val intervalOffset = parameters.intervalOffset
val region = parameters.region

val tz = TimeZone.getTimeZone("UTC")
val intervalTime = GregorianCalendar(tz)

val millisecondsPerMinute = 60 * 1000L
val currentTime = System.currentTimeMillis()
val startTime = currentTime - intervalDuration * millisecondsPerMinute
val intervalSequence = createTimestampSequence(10 * millisecondsPerMinute, Math.max(startTime, latestTime))
val endTime = System.currentTimeMillis() + intervalOffset * millisecondsPerMinute
val startTime = endTime - intervalDuration * millisecondsPerMinute
val intervalSequence = createTimestampSequence(10 * millisecondsPerMinute, Math.max(startTime, latestTime), endTime)

Authenticator.setDefault(MyAuthenticator())

Expand All @@ -163,20 +162,19 @@ class BlitzortungHttpDataProvider @JvmOverloads constructor(

if (latestTime > 0L) {
resultVar = resultVar.copy(incrementalData = true)
val expireTime = resultVar.referenceTime - (parameters.intervalDuration - parameters.intervalOffset) * 60 * 1000
val expireTime = resultVar.referenceTime - (intervalDuration - intervalOffset) * millisecondsPerMinute
this@BlitzortungHttpDataProvider.strikes = this@BlitzortungHttpDataProvider.strikes.filter { it.timestamp > expireTime }
this@BlitzortungHttpDataProvider.strikes += strikes
} else {
this@BlitzortungHttpDataProvider.strikes = strikes
}

if (strikes.count() > 0) {
latestTime = currentTime - millisecondsPerMinute
latestTime = endTime - millisecondsPerMinute
Log.v(Main.LOG_TAG, "BlitzortungHttpDataProvider.getStrikes() set latest time to $latestTime")
}


resultVar = resultVar.copy(strikes = strikes, totalStrikes = this@BlitzortungHttpDataProvider.strikes)
resultVar = resultVar.copy(strikes = strikes, totalStrikes = this@BlitzortungHttpDataProvider.strikes, referenceTime = endTime)

return resultVar
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ package org.blitzortung.android.data.provider.blitzortung
class TimestampIterator(
private val intervalLength: Long,
startTime: Long,
private val endTime: Long = System.currentTimeMillis()
private val endTime: Long
) : Iterator<Long> {

private var currentTime: Long = roundTime(startTime)
Expand All @@ -46,6 +46,6 @@ class TimestampIterator(
* @param intervalLength Interval length
* @param startTime Start time of the sequence
*/
internal fun createTimestampSequence(intervalLength: Long, startTime: Long): Sequence<Long> {
return Sequence { TimestampIterator(intervalLength, startTime) }
internal fun createTimestampSequence(intervalLength: Long, startTime: Long, endTime: Long): Sequence<Long> {
return Sequence { TimestampIterator(intervalLength, startTime, endTime) }
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,6 @@ class JsonRpcDataProvider(
nextId = 0
}

override val isCapableOfHistoricalData: Boolean = true

@Throws(JSONException::class)
private fun addStrikes(response: JSONObject, result: ResultEvent): ResultEvent {
val strikes = ArrayList<Strike>()
Expand All @@ -95,7 +93,7 @@ class JsonRpcDataProvider(
strikes.add(dataBuilder.createRasterElement(rasterParameters, referenceTimestamp, strikes_array.getJSONArray(i)))
}

return result.copy(strikes = strikes, rasterParameters = rasterParameters, incrementalData = false)
return result.copy(strikes = strikes, rasterParameters = rasterParameters, incrementalData = false, referenceTime = referenceTimestamp)
}

@Throws(JSONException::class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ class StrikesOverlay(mapActivity: OwnMapActivity, private val colorHandler: Stri
}

fun expireStrikes() {
val expireTime = referenceTime - (parameters.intervalDuration - parameters.intervalOffset) * 60 * 1000
val expireTime = referenceTime - parameters.intervalDuration * 60 * 1000

val sizeBefore = strikes.size
strikes = strikes.filter { it.timestamp > expireTime }
Expand Down Expand Up @@ -144,7 +144,7 @@ class StrikesOverlay(mapActivity: OwnMapActivity, private val colorHandler: Stri
System.currentTimeMillis()
else
referenceTime,
item.timestamp, parameters)
item.timestamp, parameters.intervalDuration)

if (hasRasterParameters() || current_section != section) {
drawable = updateAndReturnDrawable(item, section, colorHandler)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,8 @@ package org.blitzortung.android.map.overlay.color

import android.content.SharedPreferences
import android.graphics.Color

import org.blitzortung.android.app.view.PreferenceKey
import org.blitzortung.android.app.view.get
import org.blitzortung.android.data.TimeIntervalWithOffset

abstract class ColorHandler(private val preferences: SharedPreferences) {

Expand All @@ -46,18 +44,14 @@ abstract class ColorHandler(private val preferences: SharedPreferences) {

protected abstract fun getColors(target: ColorTarget): IntArray

fun getColorSection(now: Long, eventTime: Long, timeIntervalWithOffset: TimeIntervalWithOffset): Int {
return getColorSection(now, eventTime, timeIntervalWithOffset.intervalDuration, timeIntervalWithOffset.intervalOffset)
}

private fun getColorSection(now: Long, eventTime: Long, intervalDuration: Int, intervalOffset: Int): Int {
fun getColorSection(referenceTime: Long, eventTime: Long, intervalDuration: Int): Int {
val minutesPerColor = intervalDuration / colors.size
var section = (now + intervalOffset * 60 * 1000 - eventTime).toInt() / 1000 / 60 / minutesPerColor
var section = ((referenceTime - eventTime) / 1000 / 60 / minutesPerColor).toInt()
return limitToValidRange(section)
}

fun getColor(now: Long, eventTime: Long, intervalDuration: Int): Int {
return getColor(getColorSection(now, eventTime, intervalDuration, 0))
fun getColor(referenceTime: Long, eventTime: Long, intervalDuration: Int): Int {
return getColor(getColorSection(referenceTime, eventTime, intervalDuration))
}

fun getColor(index: Int): Int {
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/res/values-cs/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -189,12 +189,12 @@
</string>

<string-array name="data_sources">
<item>Blitzortung.org</item>
<item>Původní</item>
<item>Blitzortung.org</item>
</string-array>
<string-array name="data_source_values">
<item>HTTP</item>
<item>RPC</item>
<item>HTTP</item>
</string-array>

<string name="location_settings">Nastavení polohy</string>
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/res/values-de/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -190,12 +190,12 @@
</string>

<string-array name="data_sources">
<item>Blitzortung.org</item>
<item>Standard</item>
<item>Blitzortung.org</item>
</string-array>
<string-array name="data_source_values">
<item>HTTP</item>
<item>RPC</item>
<item>HTTP</item>
</string-array>

<string name="location_settings">Ortsbestimmung</string>
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/res/values-es/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -191,12 +191,12 @@
</string>

<string-array name="data_sources">
<item>Blitzortung.org</item>
<item>Default</item>
<item>Blitzortung.org</item>
</string-array>
<string-array name="data_source_values">
<item>HTTP</item>
<item>RPC</item>
<item>HTTP</item>
</string-array>

<string name="location_settings">Location settings</string>
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/res/values-nl/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -191,12 +191,12 @@
</string>

<string-array name="data_sources">
<item>Blitzortung.org</item>
<item>Default</item>
<item>Blitzortung.org</item>
</string-array>
<string-array name="data_source_values">
<item>HTTP</item>
<item>RPC</item>
<item>HTTP</item>
</string-array>

<string name="location_settings">Location settings</string>
Expand Down

0 comments on commit 5b6b09e

Please sign in to comment.