Skip to content

Commit

Permalink
improve stability of AppService
Browse files Browse the repository at this point in the history
  • Loading branch information
wuan committed Jun 7, 2020
1 parent 1060555 commit 540fee3
Showing 1 changed file with 27 additions and 16 deletions.
43 changes: 27 additions & 16 deletions app/src/main/java/org/blitzortung/android/app/AppService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import android.app.AlarmManager
import android.app.PendingIntent
import android.app.Service
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.content.SharedPreferences
import android.content.pm.PackageManager
Expand All @@ -49,8 +48,10 @@ class AppService : Service(), OnSharedPreferenceChangeListener {
Log.d(Main.LOG_TAG, "AppService() create")
}

@Volatile
private var isEnabled = false

@Volatile
private var backgroundPeriod: Int = 0
private set

Expand All @@ -69,8 +70,10 @@ class AppService : Service(), OnSharedPreferenceChangeListener {
@set:Inject
internal lateinit var alarmManager: AlarmManager

@Volatile
private var alertEnabled: Boolean = false

@Volatile
private var pendingIntent: PendingIntent? = null

@set:Inject
Expand Down Expand Up @@ -159,6 +162,10 @@ class AppService : Service(), OnSharedPreferenceChangeListener {
override fun onDestroy() {
super.onDestroy()

isEnabled = false

discardAlarm()

locationHandler.removeUpdates(dataHandler.locationEventConsumer)
dataHandler.removeUpdates(dataEventConsumer)
dataHandler.removeUpdates(alertHandler.dataEventConsumer)
Expand Down Expand Up @@ -218,25 +225,29 @@ class AppService : Service(), OnSharedPreferenceChangeListener {
}

private fun createAlarm() {
if (backgroundPeriod > 0 && pendingIntent == null) {
Log.v(Main.LOG_TAG, "AppService.createAlarm() with backgroundPeriod=%d".format(backgroundPeriod))
val intent = Intent(this, AppService::class.java)
intent.action = RETRIEVE_DATA_ACTION
pendingIntent = PendingIntent.getService(this, 0, intent, 0)

val period = (backgroundPeriod * 1000).toLong()
alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, period, period, pendingIntent)
synchronized(alarmManager) {
if (backgroundPeriod > 0 && pendingIntent == null) {
Log.v(Main.LOG_TAG, "AppService.createAlarm() with backgroundPeriod=%d".format(backgroundPeriod))
val intent = Intent(this, AppService::class.java)
intent.action = RETRIEVE_DATA_ACTION
pendingIntent = PendingIntent.getService(this, 0, intent, 0)

val period = (backgroundPeriod * 1000).toLong()
alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, period, period, pendingIntent)
}
}
}

private fun discardAlarm() {
pendingIntent?.let {pendingIntent ->
Log.v(Main.LOG_TAG, "AppService.discardAlarm()")
try {
alarmManager.cancel(pendingIntent)
pendingIntent.cancel()
} finally {
this.pendingIntent = null
synchronized(alarmManager) {
pendingIntent?.let { pendingIntent ->
Log.v(Main.LOG_TAG, "AppService.discardAlarm()")
try {
alarmManager.cancel(pendingIntent)
pendingIntent.cancel()
} finally {
this.pendingIntent = null
}
}
}
}
Expand Down

0 comments on commit 540fee3

Please sign in to comment.