Remove liveUpdatesHandler

This commit is contained in:
Chumva 2018-10-16 18:42:43 +03:00
parent 3b617fcfc9
commit 3ed8c1ec84
11 changed files with 272 additions and 256 deletions

View file

@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" <android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_width="match_parent"
@ -15,7 +16,6 @@
android:id="@+id/image_container" android:id="@+id/image_container"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="@dimen/my_location_image_height" android:layout_height="@dimen/my_location_image_height"
android:visibility="gone"
app:layout_scrollFlags="scroll"> app:layout_scrollFlags="scroll">
<ImageView <ImageView
@ -54,8 +54,7 @@
android:animateLayoutChanges="true" android:animateLayoutChanges="true"
android:orientation="vertical" android:orientation="vertical"
android:paddingLeft="@dimen/my_location_text_sides_margin" android:paddingLeft="@dimen/my_location_text_sides_margin"
android:paddingRight="@dimen/my_location_text_sides_margin" android:paddingRight="@dimen/my_location_text_sides_margin">
android:visibility="gone">
<LinearLayout <LinearLayout
android:layout_width="match_parent" android:layout_width="match_parent"
@ -302,8 +301,7 @@
<FrameLayout <FrameLayout
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" app:layout_behavior="@string/appbar_scrolling_view_behavior">
tools:visibility="gone">
<android.support.v7.widget.RecyclerView <android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view" android:id="@+id/recycler_view"

View file

@ -1,106 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/content_padding_standard"
android:layout_marginRight="@dimen/content_padding_standard"
android:layout_marginTop="@dimen/content_padding_standard"
tools:src="@drawable/ic_action_wifi_off"
tools:tint="@color/icon_light"/>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="top"
android:orientation="horizontal">
<net.osmand.telegram.ui.views.TextViewEx
android:id="@+id/title"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="?android:textColorPrimary"
android:textSize="@dimen/list_item_title_text_size"
app:firstBaselineToTopHeight="28sp"
app:typeface="@string/font_roboto_regular"
tools:text="@string/no_internet_connection"/>
<net.osmand.telegram.ui.views.TextViewEx
android:id="@+id/status_change_time"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:paddingLeft="@dimen/content_padding_standard"
android:paddingRight="@dimen/content_padding_standard"
android:textColor="?android:textColorSecondary"
android:textSize="@dimen/list_item_description_text_size"
app:firstBaselineToTopHeight="26sp"
app:typeface="@string/font_roboto_regular"
tools:text="14:30"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<net.osmand.telegram.ui.views.TextViewEx
android:id="@+id/last_location_line"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="?android:attr/textColorSecondary"
android:textSize="@dimen/list_item_description_text_size"
app:firstBaselineToTopHeight="22dp"
app:lastBaselineToBottomHeight="20dp"
app:typeface="@string/font_roboto_regular"
tools:text="@string/last_available_location"/>
<net.osmand.telegram.ui.views.TextViewEx
android:id="@+id/last_location_line_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="@dimen/content_padding_half"
android:paddingRight="@dimen/content_padding_half"
android:textColor="?android:attr/textColorSecondary"
android:textSize="@dimen/list_item_description_text_size"
app:firstBaselineToTopHeight="22dp"
app:lastBaselineToBottomHeight="20dp"
app:typeface="@string/font_roboto_medium"
tools:text="14:24"/>
</LinearLayout>
<net.osmand.telegram.ui.views.TextViewEx
android:id="@+id/re_send_location"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:ellipsize="end"
android:maxLines="1"
android:text="@string/re_send_location"
android:textColor="?attr/ctrl_active_color"
android:textSize="@dimen/hint_text_size"
app:lastBaselineToBottomHeight="16dp"
app:typeface="@string/font_roboto_medium"/>
<View
android:id="@+id/bottom_divider"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="?attr/card_divider_color"/>
</LinearLayout>
</LinearLayout>

View file

@ -0,0 +1,120 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/content_padding_standard"
android:layout_marginRight="@dimen/content_padding_standard"
android:layout_marginTop="@dimen/content_padding_standard"
tools:src="@drawable/ic_action_wifi_off"
tools:tint="@color/icon_light" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="top"
android:orientation="horizontal">
<net.osmand.telegram.ui.views.TextViewEx
android:id="@+id/title"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="?android:textColorPrimary"
android:textSize="@dimen/list_item_title_text_size"
app:firstBaselineToTopHeight="28sp"
app:typeface="@string/font_roboto_regular"
tools:text="@string/no_internet_connection" />
<net.osmand.telegram.ui.views.TextViewEx
android:id="@+id/status_change_time"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:paddingLeft="@dimen/content_padding_standard"
android:paddingRight="@dimen/content_padding_standard"
android:textColor="?android:textColorSecondary"
android:textSize="@dimen/list_item_description_text_size"
app:firstBaselineToTopHeight="26sp"
app:typeface="@string/font_roboto_regular"
tools:text="14:30" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<net.osmand.telegram.ui.views.TextViewEx
android:id="@+id/last_location_line"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="?android:attr/textColorSecondary"
android:textSize="@dimen/list_item_description_text_size"
app:firstBaselineToTopHeight="22dp"
app:lastBaselineToBottomHeight="20dp"
app:typeface="@string/font_roboto_regular"
tools:text="@string/last_available_location" />
<net.osmand.telegram.ui.views.TextViewEx
android:id="@+id/last_location_line_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="@dimen/content_padding_half"
android:paddingRight="@dimen/content_padding_half"
android:textColor="?android:attr/textColorSecondary"
android:textSize="@dimen/list_item_description_text_size"
app:firstBaselineToTopHeight="22dp"
app:lastBaselineToBottomHeight="20dp"
app:typeface="@string/font_roboto_medium"
tools:text="14:24" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
<net.osmand.telegram.ui.views.TextViewEx
android:id="@+id/re_send_location"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="?attr/selectableItemBackground"
android:ellipsize="end"
android:maxLines="1"
android:paddingLeft="@dimen/list_item_height"
android:paddingRight="@dimen/list_item_height"
android:text="@string/re_send_location"
android:textColor="?attr/ctrl_active_color"
android:textSize="@dimen/hint_text_size"
app:lastBaselineToBottomHeight="16dp"
app:typeface="@string/font_roboto_medium" />
<View
android:id="@+id/bottom_divider"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginLeft="@dimen/list_item_height"
android:layout_marginStart="@dimen/list_item_height"
android:background="?attr/card_divider_color" />
</LinearLayout>

View file

@ -180,7 +180,6 @@ class TelegramService : Service(), LocationListener, TelegramIncomingMessagesLis
updateShareInfoHandler?.postDelayed({ updateShareInfoHandler?.postDelayed({
if (isUsedByMyLocation(usedBy)) { if (isUsedByMyLocation(usedBy)) {
app().shareLocationHelper.updateSendLiveMessages() app().shareLocationHelper.updateSendLiveMessages()
app().settings.updateSharingStatusHistory()
startShareInfoUpdates() startShareInfoUpdates()
} }
}, UPDATE_LIVE_MESSAGES_INTERVAL_MS) }, UPDATE_LIVE_MESSAGES_INTERVAL_MS)
@ -233,7 +232,6 @@ class TelegramService : Service(), LocationListener, TelegramIncomingMessagesLis
} }
override fun onLocationChanged(l: Location?) { override fun onLocationChanged(l: Location?) {
if (l != null) {
val location = convertLocation(l) val location = convertLocation(l)
if (!isContinuous()) { if (!isContinuous()) {
// unregister listener and wait next time // unregister listener and wait next time
@ -254,7 +252,6 @@ class TelegramService : Service(), LocationListener, TelegramIncomingMessagesLis
app().shareLocationHelper.updateLocation(location) app().shareLocationHelper.updateLocation(location)
} }
} }
}
override fun onProviderDisabled(provider: String) { override fun onProviderDisabled(provider: String) {
Toast.makeText(this, getString(R.string.location_service_no_gps_available), Toast.LENGTH_LONG).show() Toast.makeText(this, getString(R.string.location_service_no_gps_available), Toast.LENGTH_LONG).show()

View file

@ -205,8 +205,7 @@ class TelegramSettings(private val app: TelegramApplication) {
if (shareChatInfo != null && content is TdApi.MessageLocation) { if (shareChatInfo != null && content is TdApi.MessageLocation) {
shareChatInfo.currentMessageId = message.id shareChatInfo.currentMessageId = message.id
shareChatInfo.lastSuccessfulLocation = LatLon(content.location.latitude, content.location.longitude) shareChatInfo.lastSuccessfulLocation = LatLon(content.location.latitude, content.location.longitude)
shareChatInfo.lastSuccessfulSendTimeMs = Math.max(message.editDate, message.date) * shareChatInfo.lastSuccessfulSendTimeMs = Math.max(message.editDate, message.date) * 1000L
1000L
} }
} }
@ -214,14 +213,16 @@ class TelegramSettings(private val app: TelegramApplication) {
val newSharingStatus = SharingStatus().apply { val newSharingStatus = SharingStatus().apply {
statusChangeTime = System.currentTimeMillis() statusChangeTime = System.currentTimeMillis()
statusType = if (!app.isInternetConnectionAvailable) { statusType = if (!app.isInternetConnectionAvailable) {
locationTime = getLastSuccessfulSendTime()
SharingStatusType.NO_INTERNET SharingStatusType.NO_INTERNET
} else if (app.locationProvider.lastKnownLocation == null || !app.locationProvider.gpsInfo.fixed) { } else if (app.shareLocationHelper.lastLocation == null) {
locationTime = app.shareLocationHelper.lastLocationMessageSentTime
SharingStatusType.NO_GPS SharingStatusType.NO_GPS
} else { } else {
var sendChatErrors = false var sendChatsErrors = false
shareChatsInfo.forEach { _, shareInfo -> shareChatsInfo.forEach { _, shareInfo ->
if ((statusChangeTime - shareInfo.lastSuccessfulSendTimeMs) > (sendMyLocInterval * 2) * 1000) { if (shareInfo.hasSharingError) {
sendChatErrors = true sendChatsErrors = true
locationTime = shareInfo.lastSuccessfulSendTimeMs locationTime = shareInfo.lastSuccessfulSendTimeMs
val title = app.telegramHelper.getChat(shareInfo.chatId)?.title val title = app.telegramHelper.getChat(shareInfo.chatId)?.title
if (title != null) { if (title != null) {
@ -229,17 +230,13 @@ class TelegramSettings(private val app: TelegramApplication) {
} }
} }
} }
if (sendChatErrors) { if (sendChatsErrors) {
SharingStatusType.NOT_POSSIBLE_TO_SENT_TO_CHATS SharingStatusType.NOT_POSSIBLE_TO_SENT_TO_CHATS
} else { } else {
locationTime = getLastSuccessfulSendTime()
SharingStatusType.SUCCESSFULLY_SENT SharingStatusType.SUCCESSFULLY_SENT
} }
} }
if (statusType == SharingStatusType.NO_INTERNET || statusType == SharingStatusType.SUCCESSFULLY_SENT) {
locationTime = getLastSuccessfulSendTime()
} else if (statusType == SharingStatusType.NO_GPS) {
locationTime = app.locationProvider.lastKnownLocationTime ?: -1
}
} }
if (sharingStatusChanges.isNotEmpty()) { if (sharingStatusChanges.isNotEmpty()) {
@ -575,12 +572,7 @@ class TelegramSettings(private val app: TelegramApplication) {
val start = spannableString.length val start = spannableString.length
val newSpannable = if (iterator.hasNext()) " @$chatTitle," else " @$chatTitle." val newSpannable = if (iterator.hasNext()) " @$chatTitle," else " @$chatTitle."
spannableString.append(newSpannable) spannableString.append(newSpannable)
spannableString.setSpan( spannableString.setSpan(ForegroundColorSpan(app.uiUtils.getActiveColor()), start, spannableString.length - 1, 0)
ForegroundColorSpan(app.uiUtils.getActiveColor()),
start,
spannableString.length - 1,
0
)
} }
spannableString spannableString
} }
@ -600,6 +592,7 @@ class TelegramSettings(private val app: TelegramApplication) {
var lastSuccessfulSendTimeMs = -1L var lastSuccessfulSendTimeMs = -1L
var shouldDeletePreviousMessage = false var shouldDeletePreviousMessage = false
var additionalActiveTime = ADDITIONAL_ACTIVE_TIME_VALUES_SEC[0] var additionalActiveTime = ADDITIONAL_ACTIVE_TIME_VALUES_SEC[0]
var hasSharingError = false
fun getNextAdditionalActiveTime(): Long { fun getNextAdditionalActiveTime(): Long {
var index = ADDITIONAL_ACTIVE_TIME_VALUES_SEC.indexOf(additionalActiveTime) var index = ADDITIONAL_ACTIVE_TIME_VALUES_SEC.indexOf(additionalActiveTime)

View file

@ -23,9 +23,7 @@ class ShareLocationHelper(private val app: TelegramApplication) {
var lastLocationMessageSentTime: Long = 0 var lastLocationMessageSentTime: Long = 0
private var lastTimeInMillis: Long = 0L var lastLocation: Location? = null
private var lastLocation: Location? = null
set(value) { set(value) {
if (lastTimeInMillis == 0L) { if (lastTimeInMillis == 0L) {
lastTimeInMillis = System.currentTimeMillis() lastTimeInMillis = System.currentTimeMillis()
@ -40,6 +38,8 @@ class ShareLocationHelper(private val app: TelegramApplication) {
field = value field = value
} }
private var lastTimeInMillis: Long = 0L
fun updateLocation(location: Location?) { fun updateLocation(location: Location?) {
lastLocation = location lastLocation = location
@ -54,9 +54,10 @@ class ShareLocationHelper(private val app: TelegramApplication) {
val url = "https://live.osmand.net/device/$sharingMode/send?lat=${location.latitude}&lon=${location.longitude}" val url = "https://live.osmand.net/device/$sharingMode/send?lat=${location.latitude}&lon=${location.longitude}"
AndroidNetworkUtils.sendRequestAsync(url, null) AndroidNetworkUtils.sendRequestAsync(url, null)
} }
}
lastLocationMessageSentTime = System.currentTimeMillis() lastLocationMessageSentTime = System.currentTimeMillis()
} }
} app.settings.updateSharingStatusHistory()
refreshNotification() refreshNotification()
} }

View file

@ -28,6 +28,7 @@ class TelegramHelper private constructor() {
private val log = PlatformUtil.getLog(TelegramHelper::class.java) private val log = PlatformUtil.getLog(TelegramHelper::class.java)
private const val CHATS_LIMIT = 100 private const val CHATS_LIMIT = 100
private const val IGNORED_ERROR_CODE = 406 private const val IGNORED_ERROR_CODE = 406
private const val MESSAGE_CANNOT_BE_EDITED_ERROR_CODE = 5
private const val DEVICE_PREFIX = "Device: " private const val DEVICE_PREFIX = "Device: "
private const val LOCATION_PREFIX = "Location: " private const val LOCATION_PREFIX = "Location: "
@ -97,7 +98,6 @@ class TelegramHelper private constructor() {
private var haveAuthorization = false private var haveAuthorization = false
private val defaultHandler = DefaultHandler() private val defaultHandler = DefaultHandler()
private val liveLocationMessageUpdatesHandler = LiveLocationMessageUpdatesHandler()
private var updateLiveMessagesExecutor: ScheduledExecutorService? = null private var updateLiveMessagesExecutor: ScheduledExecutorService? = null
@ -595,19 +595,19 @@ class TelegramHelper private constructor() {
return false return false
} }
fun stopSendingLiveLocationToChat(chatId: Long, msgId: Long) { fun stopSendingLiveLocationToChat(shareInfo: TelegramSettings.ShareChatInfo) {
if (msgId != -1L) { if (shareInfo.currentMessageId != -1L && shareInfo.chatId != -1L) {
client?.send( client?.send(
TdApi.EditMessageLiveLocation(chatId, msgId, null, null), TdApi.EditMessageLiveLocation(shareInfo.chatId, shareInfo.currentMessageId, null, null)) { obj ->
liveLocationMessageUpdatesHandler handleLiveLocationMessageUpdate(obj, shareInfo)
) }
} }
needRefreshActiveLiveLocationMessages = true needRefreshActiveLiveLocationMessages = true
} }
fun stopSendingLiveLocationMessages(chatsShareInfo: Map<Long, TelegramSettings.ShareChatInfo>) { fun stopSendingLiveLocationMessages(chatsShareInfo: Map<Long, TelegramSettings.ShareChatInfo>) {
chatsShareInfo.forEach { (chatId, chatInfo) -> chatsShareInfo.forEach { (_, chatInfo) ->
stopSendingLiveLocationToChat(chatId, chatInfo.currentMessageId) stopSendingLiveLocationToChat(chatInfo)
} }
} }
@ -641,16 +641,14 @@ class TelegramHelper private constructor() {
} }
} }
private fun recreateLiveLocationMessage(chatId: Long, msgId: Long, content: TdApi.InputMessageLocation) { private fun recreateLiveLocationMessage(shareInfo: TelegramSettings.ShareChatInfo, content: TdApi.InputMessageLocation) {
if (msgId != -1L) { if (shareInfo.currentMessageId != -1L && shareInfo.chatId != -1L) {
log.info("recreateLiveLocationMessage - $msgId") log.info("recreateLiveLocationMessage - $shareInfo.currentMessageId")
val array = LongArray(1) val array = LongArray(1)
array[0] = msgId array[0] = shareInfo.currentMessageId
client?.send(TdApi.DeleteMessages(chatId, array, true)) { obj -> client?.send(TdApi.DeleteMessages(shareInfo.chatId, array, true)) { obj ->
when (obj.constructor) { when (obj.constructor) {
TdApi.Ok.CONSTRUCTOR -> { TdApi.Ok.CONSTRUCTOR -> sendNewLiveLocationMessage(shareInfo, content)
sendNewLiveLocationMessage(chatId, content)
}
TdApi.Error.CONSTRUCTOR -> { TdApi.Error.CONSTRUCTOR -> {
val error = obj as TdApi.Error val error = obj as TdApi.Error
if (error.code != IGNORED_ERROR_CODE) { if (error.code != IGNORED_ERROR_CODE) {
@ -666,13 +664,13 @@ class TelegramHelper private constructor() {
needRefreshActiveLiveLocationMessages = true needRefreshActiveLiveLocationMessages = true
} }
private fun sendNewLiveLocationMessage(chatId: Long, content: TdApi.InputMessageLocation) { private fun sendNewLiveLocationMessage(shareInfo: TelegramSettings.ShareChatInfo, content: TdApi.InputMessageLocation) {
needRefreshActiveLiveLocationMessages = true needRefreshActiveLiveLocationMessages = true
log.info("sendNewLiveLocationMessage") log.info("sendNewLiveLocationMessage")
client?.send( client?.send(
TdApi.SendMessage(chatId, 0, false, true, null, content), TdApi.SendMessage(shareInfo.chatId, 0, false, true, null, content)) { obj ->
liveLocationMessageUpdatesHandler handleLiveLocationMessageUpdate(obj, shareInfo)
) }
} }
private fun sendLiveLocationImpl(chatsShareInfo: Map<Long, TelegramSettings.ShareChatInfo>, latitude: Double, longitude: Double) { private fun sendLiveLocationImpl(chatsShareInfo: Map<Long, TelegramSettings.ShareChatInfo>, latitude: Double, longitude: Double) {
@ -688,18 +686,51 @@ class TelegramHelper private constructor() {
val msgId = shareInfo.currentMessageId val msgId = shareInfo.currentMessageId
if (msgId != -1L) { if (msgId != -1L) {
if (shareInfo.shouldDeletePreviousMessage) { if (shareInfo.shouldDeletePreviousMessage) {
recreateLiveLocationMessage(chatId, msgId, content) recreateLiveLocationMessage(shareInfo, content)
shareInfo.shouldDeletePreviousMessage = false shareInfo.shouldDeletePreviousMessage = false
shareInfo.currentMessageId = -1 shareInfo.currentMessageId = -1
} else { } else {
log.info("EditMessageLiveLocation - $msgId") log.info("EditMessageLiveLocation - $msgId")
client?.send( client?.send(
TdApi.EditMessageLiveLocation(chatId, msgId, null, location), TdApi.EditMessageLiveLocation(chatId, msgId, null, location)) { obj ->
liveLocationMessageUpdatesHandler handleLiveLocationMessageUpdate(obj, shareInfo)
) }
} }
} else { } else {
sendNewLiveLocationMessage(chatId, content) sendNewLiveLocationMessage(shareInfo, content)
}
}
}
private fun handleLiveLocationMessageUpdate(obj: TdApi.Object, shareInfo: TelegramSettings.ShareChatInfo) {
when (obj.constructor) {
TdApi.Error.CONSTRUCTOR -> {
val error = obj as TdApi.Error
needRefreshActiveLiveLocationMessages = true
if (error.code == MESSAGE_CANNOT_BE_EDITED_ERROR_CODE) {
shareInfo.shouldDeletePreviousMessage = true
} else if (error.code != IGNORED_ERROR_CODE) {
shareInfo.hasSharingError = true
outgoingMessagesListeners.forEach {
it.onSendLiveLocationError(error.code, error.message)
}
}
}
TdApi.Message.CONSTRUCTOR -> {
if (obj is TdApi.Message) {
if (obj.sendingState?.constructor == TdApi.MessageSendingStateFailed.CONSTRUCTOR) {
shareInfo.hasSharingError = true
needRefreshActiveLiveLocationMessages = true
outgoingMessagesListeners.forEach {
it.onSendLiveLocationError(-1, "Live location message ${obj.id} failed to send")
}
} else {
shareInfo.hasSharingError = false
outgoingMessagesListeners.forEach {
it.onUpdateMessages(listOf(obj))
}
}
}
} }
} }
} }
@ -757,36 +788,6 @@ class TelegramHelper private constructor() {
} }
} }
private inner class LiveLocationMessageUpdatesHandler : ResultHandler {
override fun onResult(obj: TdApi.Object) {
when (obj.constructor) {
TdApi.Error.CONSTRUCTOR -> {
val error = obj as TdApi.Error
if (error.code != IGNORED_ERROR_CODE) {
needRefreshActiveLiveLocationMessages = true
outgoingMessagesListeners.forEach {
it.onSendLiveLocationError(error.code, error.message)
}
}
}
TdApi.Message.CONSTRUCTOR -> {
if (obj is TdApi.Message) {
if (obj.sendingState?.constructor == TdApi.MessageSendingStateFailed.CONSTRUCTOR) {
needRefreshActiveLiveLocationMessages = true
outgoingMessagesListeners.forEach {
it.onSendLiveLocationError(-1, "Live location message ${obj.id} failed to send")
}
} else {
outgoingMessagesListeners.forEach {
it.onUpdateMessages(listOf(obj))
}
}
}
}
}
}
}
private fun onAuthorizationStateUpdated(authorizationState: AuthorizationState?, info: Boolean = false) { private fun onAuthorizationStateUpdated(authorizationState: AuthorizationState?, info: Boolean = false) {
val prevAuthState = getTelegramAuthorizationState() val prevAuthState = getTelegramAuthorizationState()
if (authorizationState != null) { if (authorizationState != null) {
@ -809,7 +810,10 @@ class TelegramHelper private constructor() {
parameters.applicationVersion = "1.0" parameters.applicationVersion = "1.0"
parameters.enableStorageOptimizer = true parameters.enableStorageOptimizer = true
client!!.send(TdApi.SetTdlibParameters(parameters), AuthorizationRequestHandler()) client!!.send(
TdApi.SetTdlibParameters(parameters),
AuthorizationRequestHandler()
)
} }
} }
TdApi.AuthorizationStateWaitEncryptionKey.CONSTRUCTOR -> { TdApi.AuthorizationStateWaitEncryptionKey.CONSTRUCTOR -> {
@ -819,15 +823,21 @@ class TelegramHelper private constructor() {
} }
TdApi.AuthorizationStateWaitPhoneNumber.CONSTRUCTOR -> { TdApi.AuthorizationStateWaitPhoneNumber.CONSTRUCTOR -> {
log.info("Request phone number") log.info("Request phone number")
telegramAuthorizationRequestHandler?.telegramAuthorizationRequestListener?.onRequestTelegramAuthenticationParameter(PHONE_NUMBER) telegramAuthorizationRequestHandler?.telegramAuthorizationRequestListener?.onRequestTelegramAuthenticationParameter(
PHONE_NUMBER
)
} }
TdApi.AuthorizationStateWaitCode.CONSTRUCTOR -> { TdApi.AuthorizationStateWaitCode.CONSTRUCTOR -> {
log.info("Request code") log.info("Request code")
telegramAuthorizationRequestHandler?.telegramAuthorizationRequestListener?.onRequestTelegramAuthenticationParameter(CODE) telegramAuthorizationRequestHandler?.telegramAuthorizationRequestListener?.onRequestTelegramAuthenticationParameter(
CODE
)
} }
TdApi.AuthorizationStateWaitPassword.CONSTRUCTOR -> { TdApi.AuthorizationStateWaitPassword.CONSTRUCTOR -> {
log.info("Request password") log.info("Request password")
telegramAuthorizationRequestHandler?.telegramAuthorizationRequestListener?.onRequestTelegramAuthenticationParameter(PASSWORD) telegramAuthorizationRequestHandler?.telegramAuthorizationRequestListener?.onRequestTelegramAuthenticationParameter(
PASSWORD
)
} }
TdApi.AuthorizationStateReady.CONSTRUCTOR -> { TdApi.AuthorizationStateReady.CONSTRUCTOR -> {
log.info("Ready") log.info("Ready")
@ -844,7 +854,8 @@ class TelegramHelper private constructor() {
else -> log.error("Unsupported authorization state: " + this.authorizationState!!) else -> log.error("Unsupported authorization state: " + this.authorizationState!!)
} }
val wasAuthorized = haveAuthorization val wasAuthorized = haveAuthorization
haveAuthorization = this.authorizationState?.constructor == TdApi.AuthorizationStateReady.CONSTRUCTOR haveAuthorization = this.authorizationState?.constructor ==
TdApi.AuthorizationStateReady.CONSTRUCTOR
if (wasAuthorized != haveAuthorization) { if (wasAuthorized != haveAuthorization) {
needRefreshActiveLiveLocationMessages = true needRefreshActiveLiveLocationMessages = true
if (haveAuthorization) { if (haveAuthorization) {

View file

@ -193,7 +193,6 @@ class MyLocationTabFragment : Fragment(), TelegramListener {
} }
mainView.findViewById<View>(R.id.sharing_status_container).setOnClickListener { mainView.findViewById<View>(R.id.sharing_status_container).setOnClickListener {
settings.updateSharingStatusHistory()
fragmentManager?.also { fm -> fragmentManager?.also { fm ->
SharingStatusBottomSheet.showInstance(fm, this) SharingStatusBottomSheet.showInstance(fm, this)
} }
@ -247,6 +246,9 @@ class MyLocationTabFragment : Fragment(), TelegramListener {
app.stopSharingLocation() app.stopSharingLocation()
updateContent() updateContent()
} }
SharingStatusBottomSheet.SHARING_STATUS_REQUEST_CODE -> {
updateSharingStatus()
}
} }
} }
@ -429,17 +431,16 @@ class MyLocationTabFragment : Fragment(), TelegramListener {
private fun updateSharingStatus() { private fun updateSharingStatus() {
if (sharingMode) { if (sharingMode) {
if (settings.sharingStatusChanges.isEmpty()) { if (settings.sharingStatusChanges.isNotEmpty()) {
settings.updateSharingStatusHistory()
}
sharingStatusDescription.text = settings.sharingStatusChanges.last().getDescription(app) sharingStatusDescription.text = settings.sharingStatusChanges.last().getDescription(app)
} }
} }
}
private fun updateList() { private fun updateList() {
val chats: MutableList<TdApi.Chat> = mutableListOf() val chats: MutableList<TdApi.Chat> = mutableListOf()
val currentUser = telegramHelper.getCurrentUser() val currentUser = telegramHelper.getCurrentUser()
val chatList = if (sharingMode && settings.hasAnyChatToShareLocation()) { val chatList = if (sharingMode) {
settings.getShareLocationChats() settings.getShareLocationChats()
} else { } else {
telegramHelper.getChatListIds() telegramHelper.getChatListIds()
@ -547,10 +548,9 @@ class MyLocationTabFragment : Fragment(), TelegramListener {
isChecked = live isChecked = live
setOnCheckedChangeListener { _, isChecked -> setOnCheckedChangeListener { _, isChecked ->
if (!isChecked) { if (!isChecked) {
val currentMessageId = shareInfo?.currentMessageId
settings.shareLocationToChat(chat.id, false) settings.shareLocationToChat(chat.id, false)
if (currentMessageId != null) { if (shareInfo != null) {
telegramHelper.stopSendingLiveLocationToChat(chat.id, currentMessageId) telegramHelper.stopSendingLiveLocationToChat(shareInfo)
} }
removeItem(chat) removeItem(chat)
} }

View file

@ -12,7 +12,6 @@ import android.widget.ImageView
import android.widget.TextView import android.widget.TextView
import net.osmand.telegram.R import net.osmand.telegram.R
import net.osmand.telegram.TelegramApplication import net.osmand.telegram.TelegramApplication
import net.osmand.telegram.TelegramSettings
import net.osmand.telegram.ui.views.BottomSheetDialog import net.osmand.telegram.ui.views.BottomSheetDialog
import net.osmand.telegram.utils.OsmandFormatter import net.osmand.telegram.utils.OsmandFormatter
@ -24,17 +23,16 @@ class SharingStatusBottomSheet : DialogFragment() {
override fun onCreateDialog(savedInstanceState: Bundle?) = BottomSheetDialog(context!!) override fun onCreateDialog(savedInstanceState: Bundle?) = BottomSheetDialog(context!!)
override fun onCreateView( override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val mainView = inflater.inflate(R.layout.bottom_sheet_sharing_status, container, false) val mainView = inflater.inflate(R.layout.bottom_sheet_sharing_status, container, false)
mainView.findViewById<View>(R.id.scroll_view_container).setOnClickListener { dismiss() } mainView.findViewById<View>(R.id.scroll_view_container).setOnClickListener { dismiss() }
BottomSheetBehavior.from(mainView.findViewById<View>(R.id.scroll_view)) BottomSheetBehavior.from(mainView.findViewById<View>(R.id.scroll_view))
.setBottomSheetCallback(object : BottomSheetBehavior.BottomSheetCallback() { .setBottomSheetCallback(object : BottomSheetBehavior.BottomSheetCallback() {
override fun onStateChanged(bottomSheet: View, newState: Int) { override fun onStateChanged(bottomSheet: View, newState: Int) {
if (newState == BottomSheetBehavior.STATE_HIDDEN) { if (newState == BottomSheetBehavior.STATE_HIDDEN) {
targetFragment?.also { target ->
target.onActivityResult(targetRequestCode, SHARING_STATUS_REQUEST_CODE, null)
}
dismiss() dismiss()
} }
} }
@ -44,7 +42,7 @@ class SharingStatusBottomSheet : DialogFragment() {
val itemsCont = mainView.findViewById<ViewGroup>(R.id.items_container) val itemsCont = mainView.findViewById<ViewGroup>(R.id.items_container)
settings.sharingStatusChanges.reversed().forEach { sharingStatus -> settings.sharingStatusChanges.reversed().forEach { sharingStatus ->
inflater.inflate(R.layout.item_with_four_text_lines, itemsCont, false).apply { inflater.inflate(R.layout.item_with_three_text_lines, itemsCont, false).apply {
val sharingStatusType = sharingStatus.statusType val sharingStatusType = sharingStatus.statusType
findViewById<ImageView>(R.id.icon).setImageDrawable(uiUtils.getIcon(sharingStatusType.iconId, sharingStatusType.iconColorRes)) findViewById<ImageView>(R.id.icon).setImageDrawable(uiUtils.getIcon(sharingStatusType.iconId, sharingStatusType.iconColorRes))
findViewById<TextView>(R.id.title).text = sharingStatus.getDescription(app) findViewById<TextView>(R.id.title).text = sharingStatus.getDescription(app)
@ -71,10 +69,14 @@ class SharingStatusBottomSheet : DialogFragment() {
itemsCont.addView(this) itemsCont.addView(this)
} }
} }
mainView.findViewById<TextView>(R.id.secondary_btn).apply { mainView.findViewById<TextView>(R.id.secondary_btn).apply {
setText(R.string.shared_string_close) setText(R.string.shared_string_close)
setOnClickListener { dismiss() } setOnClickListener {
targetFragment?.also { target ->
target.onActivityResult(targetRequestCode, SHARING_STATUS_REQUEST_CODE, null)
}
dismiss()
}
} }
return mainView return mainView
} }

View file

@ -21,7 +21,7 @@ object OsmandFormatter {
private val fixed1 = DecimalFormat("0.0") private val fixed1 = DecimalFormat("0.0")
private const val SHORT_TIME_FORMAT = "%02d:%02d" private const val SHORT_TIME_FORMAT = "%02d:%02d"
private const val SIMPLE_TIME_OF_DAY_FORMAT = "HH:mm:ss" private const val SIMPLE_TIME_OF_DAY_FORMAT = "HH:mm"
private const val SIMPLE_DATE_FORMAT = "dd MMM HH:mm:ss" private const val SIMPLE_DATE_FORMAT = "dd MMM HH:mm:ss"
private const val MIN_DURATION_FOR_DATE_FORMAT = 48 * 60 * 60 private const val MIN_DURATION_FOR_DATE_FORMAT = 48 * 60 * 60