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">
<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.INTERNET" />
<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)
}
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) {
log.debug("addBufferedMessage $message")
val messages = mutableListOf(*this.bufferedMessages.toTypedArray())
@ -91,7 +94,7 @@ class LocationMessages(val app: TelegramApplication) {
val content = OsmandLocationUtils.parseMessageContent(message, app.telegramHelper)
if (content != null) {
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
}
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.os.Build
import android.os.Bundle
import android.os.Handler
import android.os.Message
import android.support.design.widget.Snackbar
import android.support.v4.app.FragmentManager
import android.util.DisplayMetrics
@ -63,6 +65,10 @@ class UserGpxInfoFragment : BaseDialogFragment() {
private var chatId = -1L
private var deviceName = ""
private var handler: Handler = Handler()
private var endTimeChanged: Boolean = false
override fun onCreateView(
inflater: LayoutInflater,
parent: ViewGroup?,
@ -126,9 +132,12 @@ class UserGpxInfoFragment : BaseDialogFragment() {
liveBtn = mainView.findViewById<TextView>(R.id.live_btn).apply {
setOnClickListener {
val enabled = settings.isLiveTrackEnabled(userId, chatId, deviceName)
settings.updateLiveTrack(userId, chatId, deviceName, !enabled)
val enabled = !liveTrackEnabled()
settings.updateLiveTrack(userId, chatId, deviceName, enabled)
updateLiveTrackBtn()
if (enabled) {
startHandler()
}
}
}
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 {
val version = AndroidUtils.getAppVersionCode(app, app.settings.appToConnectPackage)
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) {
textView.setTextColor(AndroidUtils.createPressedColorStateList(app, true, R.color.ctrl_active_light, R.color.ctrl_light))
}
private fun updateLiveTrackBtn() {
val enabled = settings.isLiveTrackEnabled(userId, chatId, deviceName)
val enabled = liveTrackEnabled()
val icon = getLiveTrackBtnIcon(enabled)
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() {
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) {
val time = startCalendar.timeInMillis
startCalendar.timeInMillis = endCalendar.timeInMillis
@ -393,6 +426,7 @@ class UserGpxInfoFragment : BaseDialogFragment() {
endCalendar.set(Calendar.YEAR, year)
endCalendar.set(Calendar.MONTH, monthOfYear)
endCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth)
endTimeChanged = true
updateGpxInfo()
}
DatePickerDialog(context, dateFromDialog,
@ -406,6 +440,7 @@ class UserGpxInfoFragment : BaseDialogFragment() {
TimePickerDialog.OnTimeSetListener { _, hours, minutes ->
endCalendar.set(Calendar.HOUR_OF_DAY, hours)
endCalendar.set(Calendar.MINUTE, minutes)
endTimeChanged = true
updateGpxInfo()
}, 0, 0, true).show()
}
@ -421,6 +456,8 @@ class UserGpxInfoFragment : BaseDialogFragment() {
private const val GPX_TRACK_COLOR = -65536
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 {
return try {