Skip to content

Commit

Permalink
Merge branch 'release/1.2.5'
Browse files Browse the repository at this point in the history
  • Loading branch information
jingjoeh committed Mar 10, 2020
2 parents 46d4e6c + 09a8c05 commit 788c625
Show file tree
Hide file tree
Showing 45 changed files with 993 additions and 223 deletions.
3 changes: 3 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ dependencies {
/* viewpager2 */
implementation "androidx.viewpager2:viewpager2:1.0.0-alpha01"

/* dialog loading */
implementation 'com.github.d-max:spots-dialog:1.1@aar'

/* firebase */
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'com.google.firebase:firebase-core:17.2.0'
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
android:resource="@xml/file_paths" />
</provider>

<activity android:name=".view.profile.coordinates.CoordinatesActivity"
android:screenOrientation="portrait" />
<activity
android:name=".view.profile.editprofile.EditProfileActivity"
android:screenOrientation="portrait" />
Expand Down
11 changes: 2 additions & 9 deletions app/src/main/java/org/rfcx/ranger/data/local/CachedEndpointDb.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,11 @@ import java.util.*
class CachedEndpointDb(val realm: Realm) {

fun updateCachedEndpoint(endpoint: String) {
realm.use { it ->
it.executeTransaction {
it.copyToRealmOrUpdate(CachedEndpoint(endpoint, Date()))
}
realm.executeTransaction {
it.copyToRealmOrUpdate(CachedEndpoint(endpoint, Date()))
}
}

fun clearCachedEndpoint(endpoint: String) {
realm.where(CachedEndpoint::class.java).like(CachedEndpoint.FIELD_ENDPOINT,
"$endpoint*").findAll().deleteAllFromRealm()
}

fun hasCachedEndpoint(endpoint: String, hours: Double = 1.0): Boolean {
val cachedEndpoint = realm.where(CachedEndpoint::class.java).equalTo(
CachedEndpoint.FIELD_ENDPOINT, endpoint).findFirst() ?: return false
Expand Down
20 changes: 20 additions & 0 deletions app/src/main/java/org/rfcx/ranger/data/local/EventDb.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package org.rfcx.ranger.data.local
import io.realm.Realm
import io.realm.RealmResults
import io.realm.Sort
import org.rfcx.ranger.entity.CachedEndpoint
import org.rfcx.ranger.entity.event.Event
import org.rfcx.ranger.entity.event.EventReview

Expand Down Expand Up @@ -121,6 +122,25 @@ class EventDb(val realm: Realm) {
}
}

fun deleteAllEvents(callback: (Boolean) -> Unit) {
realm.use { realm ->
realm.executeTransactionAsync({ bgRealm ->
bgRealm.delete(Event::class.java)
// clear cache endpoint
bgRealm.where(CachedEndpoint::class.java).like(CachedEndpoint.FIELD_ENDPOINT,
"guardians/group/*").findAll().deleteAllFromRealm()
bgRealm.where(CachedEndpoint::class.java).like(CachedEndpoint.FIELD_ENDPOINT,
"v2/events/?guardian_groups[]=*").findAll().deleteAllFromRealm()
}, {
// success
callback(true)
}, {
// fail
callback(false)
})
}
}

fun deleteAllEvents() {
realm.use { it ->
it.executeTransaction {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,15 @@ class GetEventsUseCase(private val eventRepository: EventRepository,
if (events.isNotEmpty()) {
if (isStarting) {
val eventCached = eventDb.getEvents()
val r = events.filter {
eventCached.firstOrNull { cached -> cached.id == it.id &&
cached.reviewCreated.time == it.reviewCreated.time } == null // new event?
var r = listOf<Event>()
if (events.isNotEmpty() && eventCached.isNotEmpty()) {
r = events.filter {
eventCached.firstOrNull { cached ->
cached.id == it.id
&& cached.rejectedCount == it.rejectedCount
&& cached.confirmedCount == it.confirmedCount
} == null // new event?
}
}
// has new event?
if (r.isNotEmpty()) {
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/org/rfcx/ranger/di/UiModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ object UiModule {

val profileModule = module {
viewModel { ProfileViewModel(androidContext(), get(), get(), get()) }
viewModel { GuardianGroupViewModel(androidContext(), get(), get(), get(), get()) }
viewModel { GuardianGroupViewModel(androidContext(), get(), get(), get(), get(), get()) }
viewModel { FeedbackViewModel(androidContext()) }
viewModel { PasswordChangeViewModel(get()) }
viewModel { EditProfileViewModel(androidContext(), get()) }
Expand Down
135 changes: 87 additions & 48 deletions app/src/main/java/org/rfcx/ranger/service/LocationTrackerService.kt
Original file line number Diff line number Diff line change
@@ -1,28 +1,31 @@
package org.rfcx.ranger.service

import android.Manifest
import android.annotation.SuppressLint
import android.app.*
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.graphics.BitmapFactory
import android.location.*
import android.media.RingtoneManager
import android.os.*
import android.os.Binder
import android.os.Build
import android.os.Bundle
import android.os.IBinder
import android.util.Log
import androidx.annotation.RequiresApi
import androidx.core.app.ActivityCompat
import androidx.core.app.NotificationCompat
import com.google.android.gms.location.LocationRequest
import com.google.firebase.firestore.FirebaseFirestore
import io.realm.Realm
import org.rfcx.ranger.BuildConfig
import org.rfcx.ranger.R
import org.rfcx.ranger.data.local.WeeklySummaryData
import org.rfcx.ranger.entity.location.CheckIn
import org.rfcx.ranger.localdb.LocationDb
import org.rfcx.ranger.util.Analytics
import org.rfcx.ranger.util.Preferences
import org.rfcx.ranger.util.RealmHelper
import org.rfcx.ranger.util.*
import org.rfcx.ranger.view.MainActivityNew
import java.util.*
import java.util.concurrent.TimeUnit
Expand Down Expand Up @@ -54,19 +57,16 @@ class LocationTrackerService : Service() {
private var mLocationManager: LocationManager? = null
private var isLocationAvailability: Boolean = true
private var trackingStatTimer: Timer? = null
private var trackingWorkTimer: Timer? = null
private var trackingSatelliteTimer: Timer? = null
private lateinit var weeklySummaryData: WeeklySummaryData
var lastUpdated: Date? = null
private val analytics by lazy { Analytics(this) }
private var satelliteCount = 0

private val delayTime = 1000L * 30L // 30 seconds
private var satelliteHandler: Handler? = null
private val satelliteRunnable = object : Runnable {
override fun run() {
analytics.trackSatelliteCount(satelliteCount)
satelliteHandler?.postDelayed(this, delayTime)
}
}
// Logs location service
private val logDb = FirebaseFirestore.getInstance()
private var logDocumentId: String? = null

fun calculateTime(newTime: Date, lastTime: Date): Long {
val differenceTime1 = newTime.time - lastTime.time
Expand Down Expand Up @@ -111,6 +111,38 @@ class LocationTrackerService : Service() {

}

private val gnssStatusCallback = @RequiresApi(Build.VERSION_CODES.N)
object : GnssStatus.Callback() {
override fun onSatelliteStatusChanged(status: GnssStatus?) {
super.onSatelliteStatusChanged(status)
val satCount = status?.satelliteCount ?: 0
satelliteCount = satCount
}
}

@Deprecated("For old version")
@SuppressLint("MissingPermission")
private val gpsStatusListener = GpsStatus.Listener { event ->
if (event == GpsStatus.GPS_EVENT_SATELLITE_STATUS) {
var satCount: Int
try {
val status = mLocationManager?.getGpsStatus(null)
val sat = status?.satellites?.iterator()
satCount = 0
if (sat != null) {
while (sat.hasNext()) {
satCount++
}
}
} catch (e: java.lang.Exception) {
e.printStackTrace()
satCount = 0 // set min of satellite?
}
satelliteCount = satCount
}
}


override fun onBind(p0: Intent?): IBinder? {
return binder
}
Expand Down Expand Up @@ -138,35 +170,9 @@ class LocationTrackerService : Service() {

// Get satellite count
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
mLocationManager?.registerGnssStatusCallback(object : GnssStatus.Callback() {
override fun onSatelliteStatusChanged(status: GnssStatus?) {
super.onSatelliteStatusChanged(status)
val satCount = status?.satelliteCount ?: 0
Log.i(TAG, "satellite count = $satCount")
satelliteCount = satCount
}
})
mLocationManager?.registerGnssStatusCallback(gnssStatusCallback)
} else {
mLocationManager?.addGpsStatusListener { event ->
if (event == GpsStatus.GPS_EVENT_SATELLITE_STATUS) {
var satCount : Int
try {
val status = mLocationManager?.getGpsStatus(null)
val sat = status?.satellites?.iterator()
satCount = 0
if (sat != null) {
while (sat.hasNext()) {
satCount++
}
}
} catch (e: java.lang.Exception) {
e.printStackTrace()
satCount = 0 // set min of satellite?
}
Log.i(TAG, "satellite count = $satCount")
satelliteCount = satCount
}
}
mLocationManager?.addGpsStatusListener(gpsStatusListener)
}

// Start notification on duty tracking
Expand All @@ -178,9 +184,21 @@ class LocationTrackerService : Service() {
getNotificationManager().notify(NOTIFICATION_LOCATION_ID, createLocationTrackerNotification(isLocationAvailability))
}

// Start handle run
satelliteHandler = Handler()
satelliteHandler?.postDelayed(satelliteRunnable, delayTime)
// Tracking last know location timer
trackingWorkTimer?.cancel()
logDocumentId = null // clear
LocationServiceLogs.start(logDb, this.getUserEmail()) { successful, documentId ->
if (successful) {
this.logDocumentId = documentId
documentId?.let { startLogLastLocation(it) }
}
}

// Tracking satellite
trackingSatelliteTimer?.cancel()
trackingSatelliteTimer = fixedRateTimer("satellite_timer", false, 30 * 1000, 30 * 1000) {
analytics.trackSatelliteCount(satelliteCount) // tracking satellite count per 30s
}
} catch (ex: SecurityException) {
ex.printStackTrace()
Log.w(TAG, "fail to request location update, ignore", ex)
Expand All @@ -191,12 +209,32 @@ class LocationTrackerService : Service() {

}

@SuppressLint("MissingPermission")
private fun startLogLastLocation(documentId: String) {
trackingWorkTimer = fixedRateTimer("last_location_timer", false, 0, 20 * 1000) {
val lastLocation = mLocationManager?.getLastKnownLocation(LocationManager.GPS_PROVIDER)
LocationServiceLogs.addLastKnowLocation(logDb, documentId, lastLocation)
}
}

override fun onDestroy() {
super.onDestroy()
Log.e(TAG, "onDestroy")
clearSatelliteHandler()
mLocationManager?.removeUpdates(locationListener)
trackingStatTimer?.cancel()

try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
mLocationManager?.unregisterGnssStatusCallback(gnssStatusCallback)
} else {
mLocationManager?.removeGpsStatusListener(gpsStatusListener)
}
} catch (e: Exception) {
e.printStackTrace()
}

// set end time of tracking service
logDocumentId?.let { LocationServiceLogs.setEndTime(logDb, it) }
clearTimer()
}

override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
Expand Down Expand Up @@ -263,8 +301,9 @@ class LocationTrackerService : Service() {
}
}

private fun clearSatelliteHandler() {
satelliteHandler?.removeCallbacks(satelliteRunnable)
satelliteHandler = null
private fun clearTimer() {
trackingStatTimer?.cancel()
trackingSatelliteTimer?.cancel()
trackingWorkTimer?.cancel()
}
}
Loading

0 comments on commit 788c625

Please sign in to comment.