Add auto map updates in UserGpxInfoFragment

This commit is contained in:
Chumva 2019-07-15 17:09:28 +03:00
parent d2e811d6e7
commit 44caece1d4
3 changed files with 45 additions and 4 deletions

View file

@ -3,6 +3,7 @@
package="net.osmand.telegram"> package="net.osmand.telegram">
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
<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" />

View file

@ -76,6 +76,9 @@ class LocationMessages(val app: TelegramApplication) {
return dbHelper.getMessagesForUser(userId, start, end) return dbHelper.getMessagesForUser(userId, start, end)
} }
fun getLastLocationInfoForUserInChat(userId: Int, chatId: Long, deviceName: String) =
lastLocationPoints.sortedByDescending { it.time }.firstOrNull { it.userId == userId && it.chatId == chatId && it.deviceName == deviceName }
fun addBufferedMessage(message: BufferMessage) { fun addBufferedMessage(message: BufferMessage) {
log.debug("addBufferedMessage $message") log.debug("addBufferedMessage $message")
val messages = mutableListOf(*this.bufferedMessages.toTypedArray()) val messages = mutableListOf(*this.bufferedMessages.toTypedArray())
@ -91,7 +94,7 @@ class LocationMessages(val app: TelegramApplication) {
val content = OsmandLocationUtils.parseMessageContent(message, app.telegramHelper) val content = OsmandLocationUtils.parseMessageContent(message, app.telegramHelper)
if (content != null) { if (content != null) {
val deviceName = if (content is OsmandLocationUtils.MessageOsmAndBotLocation) content.deviceName else "" val deviceName = if (content is OsmandLocationUtils.MessageOsmAndBotLocation) content.deviceName else ""
val previousLocationMessage = lastLocationPoints.sortedBy { it.time }.firstOrNull { val previousLocationMessage = lastLocationPoints.sortedByDescending { it.time }.firstOrNull {
it.userId == senderId && it.chatId == message.chatId && it.deviceName == deviceName && it.type == type it.userId == senderId && it.chatId == message.chatId && it.deviceName == deviceName && it.type == type
} }
if (previousLocationMessage == null || content.lastUpdated * 1000L > previousLocationMessage.time) { if (previousLocationMessage == null || content.lastUpdated * 1000L > previousLocationMessage.time) {

View file

@ -10,6 +10,8 @@ import android.graphics.drawable.Drawable
import android.graphics.drawable.LayerDrawable import android.graphics.drawable.LayerDrawable
import android.os.Build import android.os.Build
import android.os.Bundle import android.os.Bundle
import android.os.Handler
import android.os.Message
import android.support.design.widget.Snackbar import android.support.design.widget.Snackbar
import android.support.v4.app.FragmentManager import android.support.v4.app.FragmentManager
import android.util.DisplayMetrics import android.util.DisplayMetrics
@ -63,6 +65,10 @@ class UserGpxInfoFragment : BaseDialogFragment() {
private var chatId = -1L private var chatId = -1L
private var deviceName = "" private var deviceName = ""
private var handler: Handler = Handler()
private var endTimeChanged: Boolean = false
override fun onCreateView( override fun onCreateView(
inflater: LayoutInflater, inflater: LayoutInflater,
parent: ViewGroup?, parent: ViewGroup?,
@ -126,9 +132,12 @@ class UserGpxInfoFragment : BaseDialogFragment() {
liveBtn = mainView.findViewById<TextView>(R.id.live_btn).apply { liveBtn = mainView.findViewById<TextView>(R.id.live_btn).apply {
setOnClickListener { setOnClickListener {
val enabled = settings.isLiveTrackEnabled(userId, chatId, deviceName) val enabled = !liveTrackEnabled()
settings.updateLiveTrack(userId, chatId, deviceName, !enabled) settings.updateLiveTrack(userId, chatId, deviceName, enabled)
updateLiveTrackBtn() updateLiveTrackBtn()
if (enabled) {
startHandler()
}
} }
} }
updateLiveTrackBtn() updateLiveTrackBtn()
@ -215,6 +224,21 @@ class UserGpxInfoFragment : BaseDialogFragment() {
} }
} }
private fun startHandler() {
log.debug("startHandler")
if (!handler.hasMessages(TRACK_UPDATE_MSG_ID)) {
val msg = Message.obtain(handler) {
log.debug("Handler postDelayed")
if (isResumed && liveTrackEnabled()) {
updateGpxInfo()
startHandler()
}
}
msg.what = TRACK_UPDATE_MSG_ID
handler.sendMessageDelayed(msg, UPDATE_TRACK_INTERVAL_MS)
}
}
private fun canOsmandCreateBitmap(): Boolean { private fun canOsmandCreateBitmap(): Boolean {
val version = AndroidUtils.getAppVersionCode(app, app.settings.appToConnectPackage) val version = AndroidUtils.getAppVersionCode(app, app.settings.appToConnectPackage)
return version >= MIN_OSMAND_BITMAP_VERSION_CODE return version >= MIN_OSMAND_BITMAP_VERSION_CODE
@ -248,12 +272,14 @@ class UserGpxInfoFragment : BaseDialogFragment() {
} }
} }
private fun liveTrackEnabled() = settings.isLiveTrackEnabled(userId, chatId, deviceName)
private fun setupBtnTextColor(textView: TextView) { private fun setupBtnTextColor(textView: TextView) {
textView.setTextColor(AndroidUtils.createPressedColorStateList(app, true, R.color.ctrl_active_light, R.color.ctrl_light)) textView.setTextColor(AndroidUtils.createPressedColorStateList(app, true, R.color.ctrl_active_light, R.color.ctrl_light))
} }
private fun updateLiveTrackBtn() { private fun updateLiveTrackBtn() {
val enabled = settings.isLiveTrackEnabled(userId, chatId, deviceName) val enabled = liveTrackEnabled()
val icon = getLiveTrackBtnIcon(enabled) val icon = getLiveTrackBtnIcon(enabled)
val normalTextColor = if (enabled) R.color.ctrl_active_light else R.color.secondary_text_light val normalTextColor = if (enabled) R.color.ctrl_active_light else R.color.secondary_text_light
@ -301,6 +327,13 @@ class UserGpxInfoFragment : BaseDialogFragment() {
} }
private fun checkTime() { private fun checkTime() {
val enabled = liveTrackEnabled()
if (enabled && !endTimeChanged) {
val locationMessage = app.locationMessages.getLastLocationInfoForUserInChat(userId, chatId, deviceName)
if (locationMessage != null && locationMessage.time > endCalendar.timeInMillis) {
endCalendar.timeInMillis = locationMessage.time
}
}
if (startCalendar.timeInMillis > endCalendar.timeInMillis) { if (startCalendar.timeInMillis > endCalendar.timeInMillis) {
val time = startCalendar.timeInMillis val time = startCalendar.timeInMillis
startCalendar.timeInMillis = endCalendar.timeInMillis startCalendar.timeInMillis = endCalendar.timeInMillis
@ -393,6 +426,7 @@ class UserGpxInfoFragment : BaseDialogFragment() {
endCalendar.set(Calendar.YEAR, year) endCalendar.set(Calendar.YEAR, year)
endCalendar.set(Calendar.MONTH, monthOfYear) endCalendar.set(Calendar.MONTH, monthOfYear)
endCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth) endCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth)
endTimeChanged = true
updateGpxInfo() updateGpxInfo()
} }
DatePickerDialog(context, dateFromDialog, DatePickerDialog(context, dateFromDialog,
@ -406,6 +440,7 @@ class UserGpxInfoFragment : BaseDialogFragment() {
TimePickerDialog.OnTimeSetListener { _, hours, minutes -> TimePickerDialog.OnTimeSetListener { _, hours, minutes ->
endCalendar.set(Calendar.HOUR_OF_DAY, hours) endCalendar.set(Calendar.HOUR_OF_DAY, hours)
endCalendar.set(Calendar.MINUTE, minutes) endCalendar.set(Calendar.MINUTE, minutes)
endTimeChanged = true
updateGpxInfo() updateGpxInfo()
}, 0, 0, true).show() }, 0, 0, true).show()
} }
@ -421,6 +456,8 @@ class UserGpxInfoFragment : BaseDialogFragment() {
private const val GPX_TRACK_COLOR = -65536 private const val GPX_TRACK_COLOR = -65536
private const val MIN_OSMAND_BITMAP_VERSION_CODE = 330 private const val MIN_OSMAND_BITMAP_VERSION_CODE = 330
private const val UPDATE_TRACK_INTERVAL_MS = 30 * 1000L // 30 sec
private const val TRACK_UPDATE_MSG_ID = 1001
fun showInstance(fm: FragmentManager,userId:Int,chatId:Long,deviceName:String, start: Long, end: Long): Boolean { fun showInstance(fm: FragmentManager,userId:Int,chatId:Long,deviceName:String, start: Long, end: Long): Boolean {
return try { return try {