Commit 849e9119 authored by Andres Käver's avatar Andres Käver
Browse files

small ui improvements

parent 317807b0
......@@ -18,10 +18,15 @@ class C {
const val LOCATION_UPDATE_ACTION_OVERALL_DIRECT = PREFIX + "location_update.overall_direct"
const val LOCATION_UPDATE_ACTION_OVERALL_TOTAL = PREFIX + "location_update.overall_total"
const val LOCATION_UPDATE_ACTION_OVERALL_TIME = PREFIX + "location_update.overall_time"
const val LOCATION_UPDATE_ACTION_CP_DIRECT = PREFIX + "location_update.cp_direct"
const val LOCATION_UPDATE_ACTION_CP_TOTAL = PREFIX + "location_update.cp_total"
const val LOCATION_UPDATE_ACTION_CP_TIME = PREFIX + "location_update.cp_time"
const val LOCATION_UPDATE_ACTION_WP_DIRECT = PREFIX + "location_update.wp_direct"
const val LOCATION_UPDATE_ACTION_WP_TOTAL = PREFIX + "location_update.wp_total"
const val LOCATION_UPDATE_ACTION_WP_TIME = PREFIX + "location_update.wp_time"
const val NOTIFICATION_ID = 4321
const val REQUEST_PERMISSIONS_REQUEST_CODE = 34
......
package com.akaver.sportmap
import android.content.Context
import android.util.Log
import com.google.android.gms.maps.model.LatLng
import com.google.android.gms.maps.model.PolylineOptions
import java.util.concurrent.TimeUnit
class Helpers {
companion object {
private val TAG = this::class.java.declaringClass!!.simpleName
private var mapPolylineOptions: PolylineOptions? = null
@Synchronized
fun getMapPolylineOptions(): PolylineOptions {
if (mapPolylineOptions == null) {
mapPolylineOptions = PolylineOptions()
}
return mapPolylineOptions!!;
}
fun clearMapPolylineOptions(){
mapPolylineOptions = PolylineOptions()
}
fun addToMapPolylineOptions(lat: Double, lon: Double){
getMapPolylineOptions().add(LatLng(lat, lon))
}
fun getTimeString(millis: Long): String {
return String.format(
"%02d:%02d:%02d",
TimeUnit.MILLISECONDS.toHours(millis),
TimeUnit.MILLISECONDS.toMinutes(millis) - TimeUnit.HOURS.toMinutes(
TimeUnit.MILLISECONDS.toHours(millis)
),
TimeUnit.MILLISECONDS.toSeconds(millis) - TimeUnit.MINUTES.toSeconds(
TimeUnit.MILLISECONDS.toMinutes(millis)
)
)
}
fun getPace(millis: Long, distance: Float): String {
Log.d(TAG, millis.toString() + '-' + distance.toString())
val speed = millis / 60.0 / distance
if (speed > 99) return "--:--"
val minutes = (speed ).toInt();
val seconds = ((speed - minutes) * 60).toInt()
return minutes.toString() + ":" + (if (seconds < 10) "0" else "") +seconds.toString();
}
}
}
\ No newline at end of file
......@@ -45,14 +45,22 @@ class LocationService : Service() {
private var distanceOverallDirect = 0f
private var distanceOverallTotal = 0f
private var locationStart: Location? = null
private var distanceOverallTime: Long = 0L
private var distanceCPDirect = 0f
private var distanceCPTotal = 0f
private var locationCP: Location? = null
private var distanceCPTime: Long = 0L
private var distanceWPDirect = 0f
private var distanceWPTotal = 0f
private var distanceWPTime: Long = 0L
private var locationWP: Location? = null
private var jwt: String? = null
private var trackingSessionId: String? = null
......@@ -213,6 +221,9 @@ class LocationService : Service() {
private fun onNewLocation(location: Location) {
Log.i(TAG, "New location: $location")
if (location.accuracy > 100) {
return
}
if (currentLocation == null) {
locationStart = location
locationCP = location
......@@ -220,17 +231,24 @@ class LocationService : Service() {
} else {
distanceOverallDirect = location.distanceTo(locationStart)
distanceOverallTotal += location.distanceTo(currentLocation)
distanceOverallTime += (location.time - currentLocation!!.time)
distanceCPDirect = location.distanceTo(locationCP)
distanceCPTotal += location.distanceTo(currentLocation)
distanceCPTime += (location.time - currentLocation!!.time)
distanceWPDirect = location.distanceTo(locationWP)
distanceWPTotal += location.distanceTo(currentLocation)
distanceWPTime += (location.time - currentLocation!!.time)
}
// save the location for calculations
currentLocation = location
showNotification()
// save the data to mapPolyLine singleton
Helpers.addToMapPolylineOptions(location.latitude, location.longitude)
saveRestLocation(location, C.REST_LOCATIONID_LOC)
// broadcast new location to UI
......@@ -241,10 +259,15 @@ class LocationService : Service() {
intent.putExtra(C.LOCATION_UPDATE_ACTION_OVERALL_DIRECT, distanceOverallDirect)
intent.putExtra(C.LOCATION_UPDATE_ACTION_OVERALL_TOTAL, distanceOverallTotal)
intent.putExtra(C.LOCATION_UPDATE_ACTION_OVERALL_TIME, distanceOverallTime)
intent.putExtra(C.LOCATION_UPDATE_ACTION_CP_DIRECT, distanceCPDirect)
intent.putExtra(C.LOCATION_UPDATE_ACTION_CP_TOTAL, distanceCPTotal)
intent.putExtra(C.LOCATION_UPDATE_ACTION_CP_TIME, distanceCPTime)
intent.putExtra(C.LOCATION_UPDATE_ACTION_WP_DIRECT, distanceWPDirect)
intent.putExtra(C.LOCATION_UPDATE_ACTION_WP_TOTAL, distanceWPTotal)
intent.putExtra(C.LOCATION_UPDATE_ACTION_WP_TIME, distanceWPTime)
LocalBroadcastManager.getInstance(this).sendBroadcast(intent)
......@@ -358,14 +381,21 @@ class LocationService : Service() {
notifyview.setOnClickPendingIntent(R.id.imageButtonWP, pendingIntentWp)
notifyview.setTextViewText(R.id.textViewOverallDirect, "%.2f".format(distanceOverallDirect))
notifyview.setTextViewText(R.id.textViewOverallTotal, "%.2f".format(distanceOverallTotal))
notifyview.setTextViewText(R.id.textViewOverallDirect, "%.1f".format(distanceOverallDirect))
notifyview.setTextViewText(R.id.textViewOverallTotal, "%.1f".format(distanceOverallTotal))
notifyview.setTextViewText(R.id.textViewOverallDuration, Helpers.getTimeString(distanceOverallTime))
notifyview.setTextViewText(R.id.textViewOverallSpeed, Helpers.getPace(distanceOverallTime, distanceOverallTotal))
notifyview.setTextViewText(R.id.textViewWPDirect, "%.1f".format(distanceWPDirect))
notifyview.setTextViewText(R.id.textViewWPTotal, "%.1f".format(distanceWPTotal))
notifyview.setTextViewText(R.id.textViewWPDuration, Helpers.getTimeString(distanceWPTime))
notifyview.setTextViewText(R.id.textViewWPSpeed, Helpers.getPace(distanceWPTime, distanceWPTotal))
notifyview.setTextViewText(R.id.textViewWPDirect, "%.2f".format(distanceWPDirect))
notifyview.setTextViewText(R.id.textViewWPTotal, "%.2f".format(distanceWPTotal))
notifyview.setTextViewText(R.id.textViewCPDirect, "%.1f".format(distanceCPDirect))
notifyview.setTextViewText(R.id.textViewCPTotal, "%.1f".format(distanceCPTotal))
notifyview.setTextViewText(R.id.textViewCPDuration, Helpers.getTimeString(distanceCPTime))
notifyview.setTextViewText(R.id.textViewCPSpeed, Helpers.getPace(distanceCPTime, distanceCPTotal))
notifyview.setTextViewText(R.id.textViewCPDirect, "%.2f".format(distanceCPDirect))
notifyview.setTextViewText(R.id.textViewCPTotal, "%.2f".format(distanceCPTotal))
// construct and show notification
var builder = NotificationCompat.Builder(applicationContext, C.NOTIFICATION_CHANNEL)
......@@ -390,6 +420,7 @@ class LocationService : Service() {
locationWP = currentLocation
distanceWPDirect = 0f
distanceWPTotal = 0f
distanceWPTime = 0
saveRestLocation(locationWP!!, C.REST_LOCATIONID_WP)
showNotification()
}
......@@ -397,6 +428,7 @@ class LocationService : Service() {
locationCP = currentLocation
distanceCPDirect = 0f
distanceCPTotal = 0f
distanceCPTime = 0
saveRestLocation(locationCP!!, C.REST_LOCATIONID_WP)
showNotification()
}
......
......@@ -35,8 +35,8 @@ class MapsActivity : AppCompatActivity(), OnMapReadyCallback {
private lateinit var mMap: GoogleMap
private var marker: Marker? = null
private var mapPolylineOptions: PolylineOptions = PolylineOptions()
private var mapPolyline : Polyline? = null
private var mapPolyline: Polyline? = null
private val broadcastReceiver = InnerBroadcastReceiver()
private val broadcastReceiverIntentFilter: IntentFilter = IntentFilter()
......@@ -66,9 +66,17 @@ class MapsActivity : AppCompatActivity(), OnMapReadyCallback {
if (locationServiceActive) {
// stopping the service
stopService(Intent(this, LocationService::class.java))
imageButtonStartStop.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.baseline_play_arrow_24))
imageButtonStartStop.setImageDrawable(
ContextCompat.getDrawable(
this,
R.drawable.baseline_play_arrow_24
)
)
} else {
// clear the track on map
Helpers.clearMapPolylineOptions()
if (Build.VERSION.SDK_INT >= 26) {
// starting the FOREGROUND service
// service has to display non-dismissable notification within 5 secs
......@@ -76,7 +84,12 @@ class MapsActivity : AppCompatActivity(), OnMapReadyCallback {
} else {
startService(Intent(this, LocationService::class.java))
}
imageButtonStartStop.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.baseline_stop_24))
imageButtonStartStop.setImageDrawable(
ContextCompat.getDrawable(
this,
R.drawable.baseline_stop_24
)
)
}
locationServiceActive = !locationServiceActive
......@@ -109,33 +122,39 @@ class MapsActivity : AppCompatActivity(), OnMapReadyCallback {
// Add a marker in Sydney and move the camera
//val sydney = LatLng(-34.0, 151.0)
//mMap.addMarker(MarkerOptions().position(sydney).title("Marker in Sydney"))
mMap.moveCamera(CameraUpdateFactory.newLatLng( LatLng(59.3927437,24.6642)))
mMap.moveCamera(CameraUpdateFactory.newLatLng(LatLng(59.3927437, 24.6642)))
mMap.animateCamera(CameraUpdateFactory.zoomTo(17.0f))
}
private fun updateMap(lat: Double, lon: Double){
private fun updateMap(lat: Double, lon: Double) {
// mMap.clear()
val center = LatLng(lat, lon)
if (marker != null ){
if (marker != null) {
marker!!.remove()
}
if (mapPolyline != null ){
if (mapPolyline != null) {
mapPolyline!!.remove()
}
mapPolylineOptions.add(center)
marker = mMap.addMarker(MarkerOptions().position(center).icon(BitmapDescriptorFactory.fromResource(R.drawable.baseline_location_searching_black_36)))
marker = mMap
.addMarker(
MarkerOptions()
.position(center)
.icon(BitmapDescriptorFactory.fromResource(R.drawable.baseline_location_searching_black_36))
.anchor(0.5f, 0.5f) // use icon center for lat,lon
)
mapPolyline = mMap.addPolyline(mapPolylineOptions)
mapPolyline = mMap.addPolyline(Helpers.getMapPolylineOptions())
mMap.moveCamera(CameraUpdateFactory.newLatLng(center))
}
// ============================================== LIFECYCLE CALLBACKS =============================================
override fun onStart() {
Log.d(TAG, "onStart")
......@@ -146,7 +165,8 @@ class MapsActivity : AppCompatActivity(), OnMapReadyCallback {
Log.d(TAG, "onResume")
super.onResume()
LocalBroadcastManager.getInstance(this).registerReceiver(broadcastReceiver, broadcastReceiverIntentFilter)
LocalBroadcastManager.getInstance(this)
.registerReceiver(broadcastReceiver, broadcastReceiverIntentFilter)
}
......@@ -278,24 +298,60 @@ class MapsActivity : AppCompatActivity(), OnMapReadyCallback {
}
}
// ============================================== BROADCAST RECEIVER =============================================
private inner class InnerBroadcastReceiver: BroadcastReceiver() {
private inner class InnerBroadcastReceiver : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
Log.d(TAG, intent!!.action)
when (intent!!.action){
when (intent!!.action) {
C.LOCATION_UPDATE_ACTION -> {
textViewOverallDirect.text = intent.getFloatExtra(C.LOCATION_UPDATE_ACTION_OVERALL_DIRECT, 0.0f).toInt().toString()
textViewOverallTotal.text = intent.getFloatExtra(C.LOCATION_UPDATE_ACTION_OVERALL_TOTAL, 0.0f).toInt().toString()
textViewCPDirect.text = intent.getFloatExtra(C.LOCATION_UPDATE_ACTION_CP_DIRECT, 0.0f).toInt().toString()
textViewCPTotal.text = intent.getFloatExtra(C.LOCATION_UPDATE_ACTION_CP_TOTAL, 0.0f).toInt().toString()
textViewWPDirect.text = intent.getFloatExtra(C.LOCATION_UPDATE_ACTION_WP_DIRECT, 0.0f).toInt().toString()
textViewWPTotal.text = intent.getFloatExtra(C.LOCATION_UPDATE_ACTION_WP_TOTAL, 0.0f).toInt().toString()
updateMap(intent.getDoubleExtra(C.LOCATION_UPDATE_ACTION_LAT, 0.0), intent.getDoubleExtra(C.LOCATION_UPDATE_ACTION_LON, 0.0))
textViewOverallDirect.text =
intent.getFloatExtra(C.LOCATION_UPDATE_ACTION_OVERALL_DIRECT, 0.0f).toInt()
.toString()
textViewOverallTotal.text =
intent.getFloatExtra(C.LOCATION_UPDATE_ACTION_OVERALL_TOTAL, 0.0f).toInt()
.toString()
var duration = intent.getLongExtra(C.LOCATION_UPDATE_ACTION_OVERALL_TIME, 0)
textViewOverallDuration.text = Helpers.getTimeString(duration)
textViewOverallSpeed.text = Helpers.getPace(
duration,
intent.getFloatExtra(C.LOCATION_UPDATE_ACTION_OVERALL_TOTAL, 0.0f)
)
textViewCPDirect.text =
intent.getFloatExtra(C.LOCATION_UPDATE_ACTION_CP_DIRECT, 0.0f).toInt()
.toString()
textViewCPTotal.text =
intent.getFloatExtra(C.LOCATION_UPDATE_ACTION_CP_TOTAL, 0.0f).toInt()
.toString()
duration = intent.getLongExtra(C.LOCATION_UPDATE_ACTION_CP_TIME, 0)
textViewCPDuration.text = Helpers.getTimeString(duration)
textViewCPSpeed.text = Helpers.getPace(
duration,
intent.getFloatExtra(C.LOCATION_UPDATE_ACTION_CP_TOTAL, 0.0f)
)
textViewWPDirect.text =
intent.getFloatExtra(C.LOCATION_UPDATE_ACTION_WP_DIRECT, 0.0f).toInt()
.toString()
textViewWPTotal.text =
intent.getFloatExtra(C.LOCATION_UPDATE_ACTION_WP_TOTAL, 0.0f).toInt()
.toString()
duration = intent.getLongExtra(C.LOCATION_UPDATE_ACTION_WP_TIME, 0)
textViewWPDuration.text = Helpers.getTimeString(duration)
textViewWPSpeed.text = Helpers.getPace(
duration,
intent.getFloatExtra(C.LOCATION_UPDATE_ACTION_WP_TOTAL, 0.0f)
)
updateMap(
intent.getDoubleExtra(C.LOCATION_UPDATE_ACTION_LAT, 0.0),
intent.getDoubleExtra(C.LOCATION_UPDATE_ACTION_LON, 0.0)
)
}
C.LOCATION_UPDATE_STOP -> {
}
......@@ -305,5 +361,4 @@ class MapsActivity : AppCompatActivity(), OnMapReadyCallback {
}
}
......@@ -14,12 +14,25 @@
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="#1A000000"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/include">
</FrameLayout>
<include
android:id="@+id/include"
layout="@layout/map_actions"
android:layout_width="0dp"
android:layout_height="64dp"
android:background="#FFFFFF"
android:backgroundTint="#80000000"
android:layout_marginBottom="8dp"
android:background="#B01717"
android:backgroundTint="#80CA2828"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
......
......@@ -4,6 +4,7 @@
xmlns:internal="http://schemas.android.com/apk/prv/res/android"
android:layout_width="match_parent"
android:layout_height="64dp"
android:background="#1A000000"
android:orientation="horizontal"
internal:layout_maxHeight="64dp">
......@@ -12,6 +13,7 @@
android:id="@+id/imageButtonStartStop"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:backgroundTint="#FFFFFF"
android:src="@drawable/baseline_play_arrow_24"
app:srcCompat="@drawable/baseline_play_arrow_24" />
......@@ -27,6 +29,7 @@
android:layout_height="wrap_content"
android:text="Direct"
android:textAlignment="viewEnd"
android:textColor="#000000"
android:textSize="12sp" />
<TextView
......@@ -35,6 +38,7 @@
android:layout_height="wrap_content"
android:text="Total"
android:textAlignment="viewEnd"
android:textColor="#000000"
android:textSize="12sp" />
<TextView
......@@ -43,6 +47,7 @@
android:layout_height="wrap_content"
android:text="Duration"
android:textAlignment="viewEnd"
android:textColor="#000000"
android:textSize="12sp" />
<TextView
......@@ -51,13 +56,20 @@
android:layout_height="wrap_content"
android:text="Speed"
android:textAlignment="viewEnd"
android:textColor="#000000"
android:textSize="12sp" />
</LinearLayout>
<LinearLayout
android:layout_width="10dp"
android:layout_height="match_parent"
android:orientation="horizontal"></LinearLayout>
<ImageButton
android:id="@+id/imageButtonCP"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:backgroundTint="#FFFFFF"
android:src="@drawable/baseline_outlined_flag_black_24"
app:srcCompat="@drawable/baseline_outlined_flag_black_24" />
......@@ -73,6 +85,7 @@
android:layout_height="wrap_content"
android:text="Direct"
android:textAlignment="viewEnd"
android:textColor="#000000"
android:textSize="12sp" />
<TextView
......@@ -81,6 +94,7 @@
android:layout_height="wrap_content"
android:text="Total"
android:textAlignment="viewEnd"
android:textColor="#000000"
android:textSize="12sp" />
<TextView
......@@ -89,6 +103,7 @@
android:layout_height="wrap_content"
android:text="Duration"
android:textAlignment="viewEnd"
android:textColor="#000000"
android:textSize="12sp" />
<TextView
......@@ -97,14 +112,20 @@
android:layout_height="wrap_content"
android:text="Speed"
android:textAlignment="viewEnd"
android:textColor="#000000"
android:textSize="12sp" />
</LinearLayout>
<LinearLayout
android:layout_width="10dp"
android:layout_height="match_parent"
android:orientation="horizontal"></LinearLayout>
<ImageButton
android:id="@+id/imageButtonWP"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:backgroundTint="#FFFFFF"
android:src="@drawable/baseline_location_searching_black_24"
app:srcCompat="@drawable/baseline_location_searching_black_24" />
......@@ -121,6 +142,7 @@
android:layout_height="wrap_content"
android:text="Direct"
android:textAlignment="viewEnd"
android:textColor="#000000"
android:textSize="12sp" />
<TextView
......@@ -129,6 +151,7 @@
android:layout_height="wrap_content"
android:text="Total"
android:textAlignment="viewEnd"
android:textColor="#000000"
android:textSize="12sp" />
<TextView
......@@ -137,6 +160,7 @@
android:layout_height="wrap_content"
android:text="Duration"
android:textAlignment="viewEnd"
android:textColor="#000000"
android:textSize="12sp" />
<TextView
......@@ -145,6 +169,7 @@
android:layout_height="wrap_content"
android:text="Speed"
android:textAlignment="viewEnd"
android:textColor="#000000"
android:textSize="12sp" />
</LinearLayout>
......
<resources>
<string name="app_name">SportMap</string>
<string name="title_activity_maps">Map</string>
<string name="title_activity_maps">SportMap</string>
</resources>
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment