I have been given the following code by a dev to decide if I want to collaborate furter. Is it possible to say if the code is well written or needs to be refactored?
class RecordService : Service(), Recorder.OnSecondTick {
private var recordCoordinates = ArrayList<Point>()
private var lastStoredPoint: Point? = null
private var distance = Const.ZERO_DOUBLE
private var distanceElevation = Const.ZERO_DOUBLE
private var elevationGain = Const.ZERO_DOUBLE
private var highestPoint = Const.ZERO_DOUBLE
private var isFinished = false
private var checkPointDone = false
private var endLatLon = Point.fromLngLat(Const.ZERO_DOUBLE, Const.ZERO_DOUBLE)
private var checkPointLatLon = Point.fromLngLat(Const.ZERO_DOUBLE, Const.ZERO_DOUBLE)
private var serializeTrek: SerializeTrek = SerializeTrek()
private var previousLocationSec: Int = Const.ZERO
private var wakeLock: PowerManager.WakeLock? = null
private val recorder = Recorder()
private val tickIntent = Intent(Const.Intents.ON_TICK_INTENT)
private var lastLocation: Location? = GpsTools.lastLocation
private lateinit var gpsClient: FusedLocationProviderClient
private var locationCallBack: LocationCallback? = null
private val locationListener = GpsTools.OnLocationChange { l ->
LogApp.info("OnLocationRecieve")
lastLocation = l
}
override fun onBind(intent: Intent?): IBinder? {
return null
}
@SuppressLint("WakelockTimeout")
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
val powerManager = getSystemService(POWER_SERVICE) as PowerManager
wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, javaClass.name)
wakeLock?.acquire()
LogApp.info("OnStartCommand")
startForeground(Const.Notification.RECORD_SERVICE_FOREGROUND_ID, NotifyBuilder.getRecordNotification(applicationContext))
recorder.startTimer()
AppValues.storage.apply {
AppPreferences.Companion.Record.apply {
preferences.apply {
if (getBoolean(KEY_RECORD_RUN, false)) {
val dbTrek = db.getDbTrek()
serializeTrek = SerializeTrek().apply {
dbTrek?.let { trek ->
distance = trek.distance
trekId = trek.trekId
trekName = trek.name
elevationGain = trek.elevationDifference
distanceElevation = trek.overallAscent.toDouble() / Const.THOUSAND
highestPoint = trek.highestPoint
recorder.secondsCount = trek.duration.toLong()
setStartLatLon(trek.getStartPoint())
setEndLatLon(trek.getEndPoint())
setCheckPointLatLon(trek.getCheckPoint())
checkPointDone = trek.isCheckPoint
exp = trek.expPoints
db.getAllPoints()?.let {
recordCoordinates = it
if (recordCoordinates.isNotEmpty()) {
lastStoredPoint = it.first()
}
}
}
}
db.clearTrekRecording()
} else {
Const.Extras.apply {
intent?.let { pIntent ->
pIntent.extras?.let { pExtras ->
serializeTrek = pExtras.getSerializable(TREK_SERIALIZE) as SerializeTrek
}
}
}
db.clearRecording()
}
endLatLon = serializeTrek.getEndLatLon().toPoint()
checkPointLatLon = serializeTrek.getCheckPointLatLon().toPoint()
setBoolean(KEY_RECORD_RUN, true)
}
}
}
return super.onStartCommand(intent, flags, startId)
}
override fun onTaskRemoved(rootIntent: Intent?) {
LogApp.info("OnTaskRemoved")
saveTrek()
stopForeground(true)
stopSelf()
super.onTaskRemoved(rootIntent)
}
override fun onStart(intent: Intent?, startId: Int) {
LogApp.info("OnStart")
AppWakefulBroadcastReceiver.completeWakefulIntent(intent)
AppValues.app.currentActivity?.let {
recorder.setOnSecondTickListener(this)
gpsClient = GpsTools.getClient(this)
locationCallBack = GpsTools.startCheckingLocation(gpsClient, locationListener)
}
isRunning = true
}
override fun onDestroy() {
LogApp.info("OnDestroy")
LogApp.info("SET_PAUSE_TRUE")
AppValues.storage.preferences.setBoolean(AppPreferences.Companion.Record.KEY_RECORD_PAUSE, true)
super.onDestroy()
saveTrek()
recorder.cancelTimer()
locationCallBack?.let {
gpsClient.removeLocationUpdates(it)
}
stopForeground(true)
if (wakeLock?.isHeld == true) {
wakeLock?.release()
}
isRunning = false
}
override fun onTick(seconds: Long) {
tickIntent.apply {
Const.Extras.apply {
GpsTools.lastLocation?.let {
putExtra(
LOCATION_POINT,
LocationPoint(seconds, it.latitude, it.longitude, it.altitude, it.accuracy, seconds - previousLocationSec)
)
}
putExtra(DISTANCE, Const.ZERO_DOUBLE)
putExtra(TREK_FINISHED, false)
}
}
LogApp.info("TICK: " + seconds)
lastLocation?.let { location ->
location.apply {
LogApp.info("LASTLOC: LONG" + longitude + " LAT: " + latitude)
val locationPoint = LocationPoint(seconds, latitude, longitude, altitude, accuracy, seconds - previousLocationSec)
locationPoint.apply {
/**
* Check for valid cordination
*/
if (long != Const.ZERO_DOUBLE && lat != Const.ZERO_DOUBLE) {
if (altitude > highestPoint) {
highestPoint = altitude
}
val newPoint = Point.fromLngLat(long, lat, altitude)
if (recordCoordinates.isNotEmpty()) {
val recordsSize = recordCoordinates.size - Const.ONE
val distanceNewLastPoint = TurfMeasurement.distance(recordCoordinates[recordsSize], newPoint)
// val elevationNewLastPoint = newPoint.altitude() - recordCoordinates[recordsSize].altitude()
/**
* CheckPoint validation
*/
if(checkPointLatLon.isNotNull() && checkPointDone.not()){
if(newPoint.isNotNull()) {
checkPointDone = TurfMeasurement.distance(checkPointLatLon, newPoint) < Const.Tolerance.END_POINT_DISTANCE
}
}else{
checkPointDone = true
}
/**
* EndPoint validation
*/
if (endLatLon.isNotNull() && newPoint.isNotNull() && checkPointDone) {
isFinished = TurfMeasurement.distance(endLatLon, newPoint) < Const.Tolerance.END_POINT_DISTANCE
}
LogApp.info("DIS: " + distanceNewLastPoint)
/**
* Check for 20m distance and accuracy
*/
if (distanceNewLastPoint * Const.THOUSAND >= accuracy * Const.HALF_DOUBLE && distanceNewLastPoint > Const.Tolerance.MINIMAL_POINT_DISTANCE) {
LogApp.info("DIS_BETWEN_POINT: " + distanceNewLastPoint * Const.THOUSAND)
if (AppValues.storage.preferences.getBoolean(AppPreferences.Companion.Record.KEY_RECORD_PAUSE, false).not()) {
LogApp.info("DISTANCE +++++++++++++++++ ")
distance += distanceNewLastPoint
} else {
LogApp.info("SET_PAUSE_FALSE")
AppValues.storage.preferences.setBoolean(AppPreferences.Companion.Record.KEY_RECORD_PAUSE, false)
}
recordCoordinates.add(newPoint)
if (lastStoredPoint == null) {
lastStoredPoint = newPoint
} else {
val lastStoredDistance = TurfMeasurement.distance(lastStoredPoint!!, newPoint)
/**
* Check for 100m distance
*/
if (lastStoredDistance > Const.Tolerance.MINIMAL_STORED_POINT_DISTANCE) {
val elevationNewLastStoredPoint = newPoint.altitude() - lastStoredPoint!!.altitude()
LogApp.info("ELEVATION 100: ELEVATION: " + elevationNewLastStoredPoint + " DISTANCE: " + lastStoredDistance)
if (elevationNewLastStoredPoint >= Const.ONE_DOUBLE) {
elevationGain += elevationNewLastStoredPoint
distanceElevation += lastStoredDistance
LogApp.info("ELEVATION_GAIN: ELEVATION: " + elevationNewLastStoredPoint + " DISTANCE: " + lastStoredDistance)
}
lastStoredPoint = newPoint
if (recordCoordinates[recordsSize].latitude() != lat && recordCoordinates[recordsSize].longitude() != long) {
AppValues.storage.db.putCoordinate(DbCoordinate(serializeTrek.trekId, latitude, longitude, altitude))
LogApp.info("ADD_POINT: ")
}
}
}
}
} else {
LogApp.info("ADD_POINT: ")
lastStoredPoint = newPoint
AppValues.storage.db.putCoordinate(DbCoordinate(serializeTrek.trekId, latitude, longitude, altitude))
recordCoordinates.add(newPoint)
}
tickIntent.apply {
Const.Extras.apply {
putExtra(LOCATION_POINT, locationPoint)
putExtra(DISTANCE, distance)
putExtra(TREK_FINISHED, isFinished)
}
}
}
}
previousLocationSec = seconds.toInt()
}
}
LocalBroadcastManager.getInstance(this).sendBroadcast(tickIntent)
}
private fun saveTrek() {
LogApp.info("SAVE: " + distance)
val json = Gson().toJson(recordCoordinates.map { LatLon().apply {
lat = it.latitude()
long = it.longitude()} },object : TypeToken<List<LatLon>>() {}.type)
LogApp.info("JSON: " + json)
serializeTrek.apply {
val averagePeace = if (distance > Const.ONE / Const.THOUSAND) {
(Const.ONE / distance * previousLocationSec).toInt()
} else {
Const.ZERO
}
AppValues.storage.db.putTrek(
DbTrek(
trekId,
trekName,
isFinished,
checkPointDone,
checkPointLat,
checkPointLon,
previousLocationSec,
distance,
elevationGain,
highestPoint,
(distanceElevation * Const.THOUSAND).toInt(),
averagePeace,
startLat,
startLon,
endLat,
endLon,
exp
)
)
}
}
companion object {
var isRunning = false
}
}