Telegram - fixed location sending interval

This commit is contained in:
crimean 2018-06-16 19:28:39 +03:00
parent 9886e733d2
commit 6114df46d7
4 changed files with 23 additions and 12 deletions

View file

@ -6,6 +6,7 @@
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<application <application
android:name="net.osmand.telegram.TelegramApplication" android:name="net.osmand.telegram.TelegramApplication"

View file

@ -110,6 +110,7 @@ class TelegramApplication : Application(), OsmandHelperListener {
serviceIntent.putExtra(TelegramService.USAGE_INTENT, i) serviceIntent.putExtra(TelegramService.USAGE_INTENT, i)
serviceIntent.putExtra(TelegramService.USAGE_OFF_INTERVAL, interval) serviceIntent.putExtra(TelegramService.USAGE_OFF_INTERVAL, interval)
serviceIntent.putExtra(TelegramService.SEND_LOCATION_INTERVAL, settings.sendMyLocationInterval)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startForegroundService(serviceIntent) startForegroundService(serviceIntent)
} else { } else {

View file

@ -34,7 +34,10 @@ class TelegramService : Service(), LocationListener, TelegramIncomingMessagesLis
private set private set
var serviceErrorInterval = 0L var serviceErrorInterval = 0L
private set private set
var sendLocationInterval = 0L
private set
private var lastLocationSentTime = 0L
private var pendingIntent: PendingIntent? = null private var pendingIntent: PendingIntent? = null
class LocationServiceBinder : Binder() class LocationServiceBinder : Binder()
@ -56,10 +59,7 @@ class TelegramService : Service(), LocationListener, TelegramIncomingMessagesLis
if (app.settings.sendMyLocationInterval >= OFF_INTERVAL_THRESHOLD && serviceOffInterval == 0L) { if (app.settings.sendMyLocationInterval >= OFF_INTERVAL_THRESHOLD && serviceOffInterval == 0L) {
serviceOffInterval = app.settings.sendMyLocationInterval serviceOffInterval = app.settings.sendMyLocationInterval
setupServiceErrorInterval() setupServiceErrorInterval()
setupAlarm()
val alarmManager = getSystemService(Context.ALARM_SERVICE) as AlarmManager
pendingIntent = PendingIntent.getBroadcast(this, 0, Intent(this, OnTelegramServiceAlarmReceiver::class.java), PendingIntent.FLAG_UPDATE_CURRENT)
alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + 500, serviceOffInterval, pendingIntent)
} }
app.notificationHelper.refreshNotification(NotificationType.LOCATION) app.notificationHelper.refreshNotification(NotificationType.LOCATION)
} }
@ -71,6 +71,7 @@ class TelegramService : Service(), LocationListener, TelegramIncomingMessagesLis
usedBy = intent.getIntExtra(USAGE_INTENT, 0) usedBy = intent.getIntExtra(USAGE_INTENT, 0)
serviceOffInterval = intent.getLongExtra(USAGE_OFF_INTERVAL, 0) serviceOffInterval = intent.getLongExtra(USAGE_OFF_INTERVAL, 0)
sendLocationInterval = intent.getLongExtra(SEND_LOCATION_INTERVAL, 0)
setupServiceErrorInterval() setupServiceErrorInterval()
app.telegramService = this app.telegramService = this
@ -141,11 +142,15 @@ class TelegramService : Service(), LocationListener, TelegramIncomingMessagesLis
Log.d(PlatformUtil.TAG, "GPS location provider not available") //$NON-NLS-1$ Log.d(PlatformUtil.TAG, "GPS location provider not available") //$NON-NLS-1$
} }
} else { } else {
setupAlarm()
}
}
private fun setupAlarm() {
val alarmManager = getSystemService(Context.ALARM_SERVICE) as AlarmManager val alarmManager = getSystemService(Context.ALARM_SERVICE) as AlarmManager
pendingIntent = PendingIntent.getBroadcast(this, 0, Intent(this, OnTelegramServiceAlarmReceiver::class.java), PendingIntent.FLAG_UPDATE_CURRENT) pendingIntent = PendingIntent.getBroadcast(this, 0, Intent(this, OnTelegramServiceAlarmReceiver::class.java), PendingIntent.FLAG_UPDATE_CURRENT)
alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + 500, serviceOffInterval, pendingIntent) alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + 500, serviceOffInterval, pendingIntent)
} }
}
private fun removeLocationUpdates() { private fun removeLocationUpdates() {
// remove updates // remove updates
@ -169,7 +174,7 @@ class TelegramService : Service(), LocationListener, TelegramIncomingMessagesLis
val locationManager = getSystemService(Context.LOCATION_SERVICE) as LocationManager val locationManager = getSystemService(Context.LOCATION_SERVICE) as LocationManager
try { try {
locationManager.removeUpdates(this) locationManager.removeUpdates(this)
} catch (e: SecurityException) { } catch (e: Throwable) {
Log.d(PlatformUtil.TAG, "Location service permission not granted") //$NON-NLS-1$ Log.d(PlatformUtil.TAG, "Location service permission not granted") //$NON-NLS-1$
} }
@ -177,8 +182,11 @@ class TelegramService : Service(), LocationListener, TelegramIncomingMessagesLis
if (lock.isHeld) { if (lock.isHeld) {
lock.release() lock.release()
} }
}
app().shareLocationHelper.updateLocation(location) app().shareLocationHelper.updateLocation(location)
} else if (System.currentTimeMillis() - lastLocationSentTime > sendLocationInterval) {
lastLocationSentTime = System.currentTimeMillis()
app().shareLocationHelper.updateLocation(location)
}
} }
} }
@ -204,7 +212,7 @@ class TelegramService : Service(), LocationListener, TelegramIncomingMessagesLis
override fun onReceiveChatLocationMessages(chatTitle: String, vararg messages: TdApi.Message) { override fun onReceiveChatLocationMessages(chatTitle: String, vararg messages: TdApi.Message) {
val app = app() val app = app()
if (app.settings.isShowingChatOnMap(chatTitle)) { if (app.settings.isShowingChatOnMap(chatTitle)) {
ShowMessagesTask(app, chatTitle).executeOnExecutor(executor, *messages) ShowMessagesTask(app).executeOnExecutor(executor, *messages)
} }
} }
@ -212,7 +220,7 @@ class TelegramService : Service(), LocationListener, TelegramIncomingMessagesLis
UpdateMessagesTask(app()).executeOnExecutor(executor) UpdateMessagesTask(app()).executeOnExecutor(executor)
} }
private class ShowMessagesTask(private val app: TelegramApplication, private val chatTitle: String) : AsyncTask<TdApi.Message, Void, Void?>() { private class ShowMessagesTask(private val app: TelegramApplication) : AsyncTask<TdApi.Message, Void, Void?>() {
override fun doInBackground(vararg messages: TdApi.Message): Void? { override fun doInBackground(vararg messages: TdApi.Message): Void? {
for (message in messages) { for (message in messages) {
@ -236,6 +244,7 @@ class TelegramService : Service(), LocationListener, TelegramIncomingMessagesLis
const val USED_BY_USERS_LOCATIONS: Int = 2 const val USED_BY_USERS_LOCATIONS: Int = 2
const val USAGE_INTENT = "SERVICE_USED_BY" const val USAGE_INTENT = "SERVICE_USED_BY"
const val USAGE_OFF_INTERVAL = "SERVICE_OFF_INTERVAL" const val USAGE_OFF_INTERVAL = "SERVICE_OFF_INTERVAL"
const val SEND_LOCATION_INTERVAL = "SEND_LOCATION_INTERVAL"
const val OFF_INTERVAL_THRESHOLD: Long = 30000L const val OFF_INTERVAL_THRESHOLD: Long = 30000L

View file

@ -26,7 +26,7 @@ class LocationNotification(app: TelegramApplication) : TelegramNotification(app,
} }
override val isEnabled: Boolean override val isEnabled: Boolean
get() = app.settings.hasAnyChatToShareLocation() get() = app.settings.hasAnyChatToShareLocation() || app.settings.hasAnyChatToShowOnMap()
override val telegramNotificationId: Int override val telegramNotificationId: Int
get() = TelegramNotification.LOCATION_NOTIFICATION_SERVICE_ID get() = TelegramNotification.LOCATION_NOTIFICATION_SERVICE_ID