Skip to content

Commit

Permalink
Merge pull request #170 from wuan/improve_ui_performance
Browse files Browse the repository at this point in the history
Improve ui performance
  • Loading branch information
wuan authored Apr 9, 2018
2 parents 8c81870 + 1e2927c commit f66a336
Show file tree
Hide file tree
Showing 30 changed files with 444 additions and 328 deletions.
7 changes: 1 addition & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
sudo: false

language: android
# Gradle needs JDK8
jdk: oraclejdk8

android:
components:
# Uncomment the lines below if you want to
Expand All @@ -12,7 +7,7 @@ android:
- tools

# The BuildTools version used by your project
- build-tools-25.0.3
- build-tools-27.0.3

# The SDK version used to compile your project
- android-24
Expand Down
38 changes: 14 additions & 24 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,28 +1,18 @@
buildscript {
ext.kotlin_version = "1.1.4-2"
repositories {
mavenCentral()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'jacoco'
apply plugin: 'com.github.kt3k.coveralls'

android {
buildToolsVersion '25.0.3'
buildToolsVersion '27.0.3'
defaultConfig {
compileSdkVersion "Google Inc.:Google APIs:24"
applicationId "org.blitzortung.android.app"
minSdkVersion 9
targetSdkVersion 25
versionCode 196
versionName "1.5.21"
versionCode 198
versionName "1.6-beta4"
multiDexEnabled true
}
buildTypes {
Expand All @@ -42,21 +32,21 @@ android {
}

jacoco {
toolVersion = "0.7.9"
toolVersion = "0.8.1"
}

def anko_version = '0.9.1'

dependencies {
compile 'com.android.support:appcompat-v7:25.3.1'
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
compile "org.jetbrains.anko:anko-sdk23:$anko_version" // sdk19, sdk21, sdk23 are also available
compile "org.jetbrains.anko:anko-appcompat-v7:$anko_version" // For appcompat-v7 bindings
testCompile 'junit:junit:4.12'
testCompile 'org.assertj:assertj-core:3.8.0'
testCompile 'com.nhaarman:mockito-kotlin:1.5.0'
testCompile "org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version"
testCompile "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
testCompile 'org.robolectric:robolectric:3.4.2'
implementation 'com.android.support:appcompat-v7:25.3.1'
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation "org.jetbrains.anko:anko-sdk23:$anko_version" // sdk19, sdk21, sdk23 are also available
implementation "org.jetbrains.anko:anko-appcompat-v7:$anko_version" // For appcompat-v7 bindings
testImplementation 'junit:junit:4.12'
testImplementation 'org.assertj:assertj-core:3.9.1'
testImplementation 'com.nhaarman:mockito-kotlin:1.5.0'
testImplementation "org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version"
testImplementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
testImplementation 'org.robolectric:robolectric:3.4.2'
}

Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ import org.blitzortung.android.protocol.ConsumerContainer
import org.blitzortung.android.protocol.Event
import org.blitzortung.android.util.MeasurementSystem
import org.blitzortung.android.util.isAtLeast
import org.jetbrains.anko.doAsync
import org.jetbrains.anko.uiThread


class AlertHandler(
Expand Down Expand Up @@ -192,9 +194,15 @@ class AlertHandler(
}

fun checkStrikes(strikes: Collection<Strike>?) {
Log.v(Main.LOG_TAG, "AlertHandler.checkStrikes() strikes: ${strikes != null}, location: ${currentLocation != null}")
val alertResult = checkStrikes(strikes, currentLocation)
processResult(alertResult)
//Do the checkStrikes stuff in another thread
doAsync {
Log.v(Main.LOG_TAG, "AlertHandler.checkStrikes() strikes: ${strikes != null}, location: ${currentLocation != null}")
val alertResult = checkStrikes(strikes, currentLocation)

uiThread {
processResult(alertResult)
}
}
}

fun checkStrikes(strikes: Collection<Strike>?, location: Location?): AlertResult? {
Expand Down
106 changes: 58 additions & 48 deletions app/src/main/java/org/blitzortung/android/app/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ import android.os.Bundle
import android.os.Handler
import android.os.IBinder
import android.preference.PreferenceManager
import android.provider.Settings
import android.text.format.DateFormat
import android.util.Log
import android.view.KeyEvent
Expand Down Expand Up @@ -64,7 +63,7 @@ import org.blitzortung.android.map.OwnMapView
import org.blitzortung.android.map.overlay.FadeOverlay
import org.blitzortung.android.map.overlay.OwnLocationOverlay
import org.blitzortung.android.map.overlay.ParticipantsOverlay
import org.blitzortung.android.map.overlay.StrikesOverlay
import org.blitzortung.android.map.overlay.StrikeListOverlay
import org.blitzortung.android.map.overlay.color.ParticipantColorHandler
import org.blitzortung.android.map.overlay.color.StrikeColorHandler
import org.blitzortung.android.util.LogUtil
Expand All @@ -73,12 +72,11 @@ import org.blitzortung.android.util.isAtLeast
import org.jetbrains.anko.intentFor

class Main : OwnMapActivity(), OnSharedPreferenceChangeListener {
private val androidIdsForExtendedFunctionality = emptySet<String>() // setOf("f0f71d2b06703e28")

private lateinit var statusComponent: StatusComponent
private lateinit var versionComponent: VersionComponent

private lateinit var strikesOverlay: StrikesOverlay
private lateinit var strikeColorHandler: StrikeColorHandler
private lateinit var strikeListOverlay: StrikeListOverlay
private lateinit var participantsOverlay: ParticipantsOverlay
private lateinit var ownLocationOverlay: OwnLocationOverlay
private lateinit var fadeOverlay: FadeOverlay
Expand All @@ -99,6 +97,9 @@ class Main : OwnMapActivity(), OnSharedPreferenceChangeListener {

private var currentResult: ResultEvent? = null

private val keepZoomOnGotoOwnLocation: Boolean
inline get() = BOApplication.sharedPreferences.get(PreferenceKey.KEEP_ZOOM_GOTO_OWN_LOCATION, false)

val dataEventConsumer: (DataEvent) -> Unit = { event ->
if (event is RequestStartedEvent) {
Log.d(Main.LOG_TAG, "Main.onDataUpdate() received request started event")
Expand All @@ -115,28 +116,28 @@ class Main : OwnMapActivity(), OnSharedPreferenceChangeListener {

clearDataIfRequested()

val initializeOverlay = strikesOverlay.parameters != resultParameters
with(strikesOverlay) {
val initializeOverlay = strikeListOverlay.parameters != resultParameters
with(strikeListOverlay) {
parameters = resultParameters
rasterParameters = event.rasterParameters
referenceTime = event.referenceTime
}

if (event.incrementalData && !initializeOverlay) {
strikesOverlay.expireStrikes()
strikeListOverlay.expireStrikes()
} else {
strikesOverlay.clear()
strikeListOverlay.clear()
}

if (initializeOverlay && event.totalStrikes != null) {
strikesOverlay.addStrikes(event.totalStrikes)
strikeListOverlay.addStrikes(event.totalStrikes)
} else if (event.strikes != null) {
strikesOverlay.addStrikes(event.strikes)
strikeListOverlay.addStrikes(event.strikes)
}

alert_view.setColorHandler(strikesOverlay.getColorHandler(), strikesOverlay.parameters.intervalDuration)
alert_view.setColorHandler(strikeColorHandler, strikeListOverlay.parameters.intervalDuration)

strikesOverlay.refresh()
strikeListOverlay.refresh()

legend_view.requestLayout()

Expand Down Expand Up @@ -180,19 +181,20 @@ class Main : OwnMapActivity(), OnSharedPreferenceChangeListener {
PreferenceManager.setDefaultValues(this, R.xml.preferences, false)
preferences.registerOnSharedPreferenceChangeListener(this)

strikesOverlay = StrikesOverlay(this, StrikeColorHandler(preferences))
strikeColorHandler = StrikeColorHandler(preferences)
strikeListOverlay = StrikeListOverlay(this, strikeColorHandler)
participantsOverlay = ParticipantsOverlay(this, ParticipantColorHandler(preferences))

mapView.addZoomListener { zoomLevel ->
strikesOverlay.updateZoomLevel(zoomLevel)
strikeListOverlay.updateZoomLevel(zoomLevel)
participantsOverlay.updateZoomLevel(zoomLevel)
}

fadeOverlay = FadeOverlay(strikesOverlay.getColorHandler())
fadeOverlay = FadeOverlay(strikeColorHandler)
ownLocationOverlay = OwnLocationOverlay(this, mapView)

addOverlay(fadeOverlay)
addOverlay(strikesOverlay)
addOverlay(strikeListOverlay)
addOverlay(participantsOverlay)
addOverlay(ownLocationOverlay)
updateOverlays()
Expand All @@ -208,7 +210,7 @@ class Main : OwnMapActivity(), OnSharedPreferenceChangeListener {

buttonColumnHandler.addAllElements(historyController.getButtons(), ButtonGroup.DATA_UPDATING)

setupDebugModeButton()
//setupDetailModeButton()

buttonColumnHandler.updateButtonColumn()

Expand Down Expand Up @@ -248,51 +250,54 @@ class Main : OwnMapActivity(), OnSharedPreferenceChangeListener {
bindService(serviceIntent, serviceConnection, 0)
}

private fun setupDebugModeButton() {
val androidId = Settings.Secure.getString(baseContext.contentResolver, Settings.Secure.ANDROID_ID)
Log.v(Main.LOG_TAG, "AndroidId: $androidId")
if ((androidId != null && androidIdsForExtendedFunctionality.contains(androidId))) {
with(toggleExtendedMode) {
isEnabled = true
visibility = View.VISIBLE
private fun setupDetailModeButton() {
with(toggleExtendedMode) {
isEnabled = true
visibility = View.VISIBLE

setOnClickListener {
BOApplication.dataHandler.toggleExtendedMode()
reloadData()
}

buttonColumnHandler.addElement(this, ButtonGroup.DATA_UPDATING)
setOnClickListener {
BOApplication.dataHandler.toggleExtendedMode()
reloadData()
}

buttonColumnHandler.addElement(this, ButtonGroup.DATA_UPDATING)
}
}

private fun setupCustomViews() {
with(legend_view) {
strikesOverlay = this@Main.strikesOverlay
strikesOverlay = this@Main.strikeListOverlay
setAlpha(150)
setOnClickListener { openQuickSettingsDialog() }
}

with(alert_view) {
setColorHandler(strikesOverlay.getColorHandler(), strikesOverlay.parameters.intervalDuration)
setColorHandler(strikeColorHandler, strikeListOverlay.parameters.intervalDuration)
setBackgroundColor(Color.TRANSPARENT)
setAlpha(200)
setOnClickListener {
if (alertHandler.alertEnabled) {
val currentLocation = alertHandler.currentLocation
if (currentLocation != null) {
var radius = determineTargetZoomRadius(alertHandler)
val diameter = if(!keepZoomOnGotoOwnLocation) {
var radius = determineTargetZoomRadius(alertHandler)

//Calculate the new diameter
1.5f * 2f * radius
} else {
//User doesn't want to zoom, so we do not provide a diameter
null
}

val diameter = 1.5f * 2f * radius
animateToLocationAndVisibleSize(currentLocation.longitude, currentLocation.latitude, diameter)
}
}
}
}

with(histogram_view) {
setStrikesOverlay(strikesOverlay)
setOnClickListener {
setStrikesOverlay(strikeListOverlay)
setOnClickListener { view ->
val currentResult = currentResult
if (currentResult != null) {
val rasterParameters = currentResult.rasterParameters
Expand Down Expand Up @@ -326,14 +331,18 @@ class Main : OwnMapActivity(), OnSharedPreferenceChangeListener {
}
}

private fun animateToLocationAndVisibleSize(longitude: Double, latitude: Double, diameter: Float) {
private fun animateToLocationAndVisibleSize(longitude: Double, latitude: Double, diameter: Float?) {
Log.d(Main.LOG_TAG, "Main.animateAndZoomTo() %.4f, %.4f, %.0fkm".format(longitude, latitude, diameter))

val mapView = mapView
val controller = mapView.controller

val startZoomLevel = mapView.zoomLevel
val targetZoomLevel = mapView.calculateTargetZoomLevel(diameter * 1000f)
//If no diameter is provided, we keep the current zoomLevel
val targetZoomLevel = if(diameter is Float)
mapView.calculateTargetZoomLevel(diameter * 1000f)
else
startZoomLevel

controller.animateTo(GeoPoint((latitude * 1e6).toInt(), (longitude * 1e6).toInt()), {
if (startZoomLevel != targetZoomLevel) {
Expand Down Expand Up @@ -474,7 +483,7 @@ class Main : OwnMapActivity(), OnSharedPreferenceChangeListener {
Log.v(Main.LOG_TAG, "Main.clearData()")
clearData = false

strikesOverlay.clear()
strikeListOverlay.clear()
participantsOverlay.clear()
}

Expand Down Expand Up @@ -525,9 +534,9 @@ class Main : OwnMapActivity(), OnSharedPreferenceChangeListener {
if (keyCode == KeyEvent.KEYCODE_MENU) {
Log.v(Main.LOG_TAG, "Main.onKeyUp(KEYCODE_MENU)")
showPopupMenu(upper_row)
return true;
return true
}
return super.onKeyUp(keyCode, event);
return super.onKeyUp(keyCode, event)
}

override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences, keyString: String) {
Expand All @@ -544,7 +553,7 @@ class Main : OwnMapActivity(), OnSharedPreferenceChangeListener {
PreferenceKey.MAP_TYPE -> {
val mapTypeString = sharedPreferences.get(key, "SATELLITE")
mapView.isSatellite = mapTypeString == "SATELLITE"
strikesOverlay.refresh()
strikeListOverlay.refresh()
participantsOverlay.refresh()
}

Expand All @@ -555,7 +564,7 @@ class Main : OwnMapActivity(), OnSharedPreferenceChangeListener {
}

PreferenceKey.COLOR_SCHEME -> {
strikesOverlay.refresh()
strikeListOverlay.refresh()
participantsOverlay.refresh()
}

Expand All @@ -578,17 +587,18 @@ class Main : OwnMapActivity(), OnSharedPreferenceChangeListener {
}

protected fun setHistoricStatusString() {
if (!strikesOverlay.hasRealtimeData()) {
val timeString = DateFormat.format("@ kk:mm", strikesOverlay.referenceTime) as String
if (!strikeListOverlay.hasRealtimeData()) {
val referenceTime = strikeListOverlay.referenceTime + strikeListOverlay.parameters.intervalOffset * 60 * 1000
val timeString = DateFormat.format("@ kk:mm", referenceTime) as String
setStatusString(timeString)
}
}

protected fun setStatusString(runStatus: String) {
val numberOfStrikes = strikesOverlay.totalNumberOfStrikes
val numberOfStrikes = strikeListOverlay.totalNumberOfStrikes
var statusText = resources.getQuantityString(R.plurals.strike, numberOfStrikes, numberOfStrikes)
statusText += "/"
val intervalDuration = strikesOverlay.parameters.intervalDuration
val intervalDuration = strikeListOverlay.parameters.intervalDuration
statusText += resources.getQuantityString(R.plurals.minute, intervalDuration, intervalDuration)
statusText += " " + runStatus

Expand Down
Loading

0 comments on commit f66a336

Please sign in to comment.