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

@ -36,13 +36,13 @@
android:textColor="?android:textColorPrimary"
android:textSize="@dimen/list_item_title_text_size"
app:firstBaselineToTopHeight="28sp"
app:typeface="@string/font_roboto_medium"/>
app:typeface="@string/font_roboto_medium" />
<LinearLayout
android:id="@+id/items_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"/>
android:orientation="vertical" />
</LinearLayout>
@ -60,7 +60,7 @@
layout="@layout/secondary_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/content_padding_half"/>
android:layout_margin="@dimen/content_padding_half" />
</FrameLayout>

View file

@ -1,5 +1,6 @@
<?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:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
@ -15,7 +16,6 @@
android:id="@+id/image_container"
android:layout_width="match_parent"
android:layout_height="@dimen/my_location_image_height"
android:visibility="gone"
app:layout_scrollFlags="scroll">
<ImageView
@ -54,8 +54,7 @@
android:animateLayoutChanges="true"
android:orientation="vertical"
android:paddingLeft="@dimen/my_location_text_sides_margin"
android:paddingRight="@dimen/my_location_text_sides_margin"
android:visibility="gone">
android:paddingRight="@dimen/my_location_text_sides_margin">
<LinearLayout
android:layout_width="match_parent"
@ -171,7 +170,7 @@
android:textColor="@color/app_bar_title_light"
android:textSize="@dimen/title_text_size"
app:typeface="@string/font_roboto_mono_bold"
tools:text="@string/sharing_enabled"/>
tools:text="@string/sharing_enabled" />
<ImageView
android:id="@+id/options_title"
@ -182,7 +181,7 @@
android:paddingRight="@dimen/content_padding_standard"
tools:src="@drawable/ic_action_other_menu"
tools:tint="@color/icon_light"
tools:visibility="visible"/>
tools:visibility="visible" />
</LinearLayout>
@ -192,7 +191,7 @@
android:layout_height="1dp"
android:layout_marginLeft="@dimen/content_padding_standard"
android:layout_marginStart="@dimen/content_padding_standard"
android:background="?attr/card_divider_color"/>
android:background="?attr/card_divider_color" />
<LinearLayout
android:id="@+id/stop_all_sharing_row"
@ -224,7 +223,7 @@
android:textColor="?attr/ctrl_active_color"
android:textSize="@dimen/hint_text_size"
app:lastBaselineToBottomHeight="24dp"
app:typeface="@string/font_roboto_medium"/>
app:typeface="@string/font_roboto_medium" />
<Switch
android:id="@+id/stop_all_sharing_switcher"
@ -233,7 +232,7 @@
android:layout_gravity="end|center_vertical"
android:background="@null"
android:clickable="false"
android:focusable="false"/>
android:focusable="false" />
</LinearLayout>
@ -241,7 +240,7 @@
android:id="@+id/appbar_divider2"
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="?attr/card_divider_color"/>
android:background="?attr/card_divider_color" />
<LinearLayout
android:id="@+id/sharing_status_container"
@ -261,7 +260,7 @@
android:layout_marginRight="@dimen/content_padding_standard"
android:layout_marginTop="@dimen/content_padding_standard"
android:src="@drawable/ic_action_live_now"
android:tint="@color/ctrl_active_light"/>
android:tint="@color/ctrl_active_light" />
<LinearLayout
android:layout_width="match_parent"
@ -275,7 +274,7 @@
android:text="@string/shared_string_status"
android:textColor="?android:attr/textColorSecondary"
android:textSize="@dimen/list_item_description_text_size"
app:typeface="@string/font_roboto_regular"/>
app:typeface="@string/font_roboto_regular" />
<net.osmand.telegram.ui.views.TextViewEx
android:id="@+id/sharing_status_description"
@ -287,7 +286,7 @@
android:textSize="@dimen/hint_text_size"
app:lastBaselineToBottomHeight="@dimen/content_padding_standard"
app:typeface="@string/font_roboto_medium"
tools:text="@string/no_gps_connection"/>
tools:text="@string/no_gps_connection" />
</LinearLayout>
@ -302,8 +301,7 @@
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:visibility="gone">
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
@ -328,8 +326,8 @@
android:textColor="@color/white"
android:visibility="gone"
app:typeface="@string/font_roboto_medium"
tools:visibility="visible"/>
tools:visibility="visible" />
</FrameLayout>
</android.support.design.widget.CoordinatorLayout>

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({
if (isUsedByMyLocation(usedBy)) {
app().shareLocationHelper.updateSendLiveMessages()
app().settings.updateSharingStatusHistory()
startShareInfoUpdates()
}
}, UPDATE_LIVE_MESSAGES_INTERVAL_MS)
@ -233,26 +232,24 @@ class TelegramService : Service(), LocationListener, TelegramIncomingMessagesLis
}
override fun onLocationChanged(l: Location?) {
if (l != null) {
val location = convertLocation(l)
if (!isContinuous()) {
// unregister listener and wait next time
val locationManager = getSystemService(Context.LOCATION_SERVICE) as LocationManager
try {
locationManager.removeUpdates(this)
} catch (e: Throwable) {
Log.d(PlatformUtil.TAG, "Location service permission not granted") //$NON-NLS-1$
}
val lock = getLock(this)
if (lock.isHeld) {
lock.release()
}
app().shareLocationHelper.updateLocation(location)
} else if (System.currentTimeMillis() - lastLocationSentTime > sendLocationInterval * 1000) {
lastLocationSentTime = System.currentTimeMillis()
app().shareLocationHelper.updateLocation(location)
val location = convertLocation(l)
if (!isContinuous()) {
// unregister listener and wait next time
val locationManager = getSystemService(Context.LOCATION_SERVICE) as LocationManager
try {
locationManager.removeUpdates(this)
} catch (e: Throwable) {
Log.d(PlatformUtil.TAG, "Location service permission not granted") //$NON-NLS-1$
}
val lock = getLock(this)
if (lock.isHeld) {
lock.release()
}
app().shareLocationHelper.updateLocation(location)
} else if (System.currentTimeMillis() - lastLocationSentTime > sendLocationInterval * 1000) {
lastLocationSentTime = System.currentTimeMillis()
app().shareLocationHelper.updateLocation(location)
}
}

View file

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

View file

@ -23,9 +23,7 @@ class ShareLocationHelper(private val app: TelegramApplication) {
var lastLocationMessageSentTime: Long = 0
private var lastTimeInMillis: Long = 0L
private var lastLocation: Location? = null
var lastLocation: Location? = null
set(value) {
if (lastTimeInMillis == 0L) {
lastTimeInMillis = System.currentTimeMillis()
@ -40,6 +38,8 @@ class ShareLocationHelper(private val app: TelegramApplication) {
field = value
}
private var lastTimeInMillis: Long = 0L
fun updateLocation(location: 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}"
AndroidNetworkUtils.sendRequestAsync(url, null)
}
lastLocationMessageSentTime = System.currentTimeMillis()
}
lastLocationMessageSentTime = System.currentTimeMillis()
}
app.settings.updateSharingStatusHistory()
refreshNotification()
}

View file

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

View file

@ -74,7 +74,7 @@ class MyLocationTabFragment : Fragment(), TelegramListener {
private var actionButtonsListener: ActionButtonsListener? = null
private var sharingMode = false
private var updateEnable: Boolean = false
override fun onCreateView(
@ -93,7 +93,7 @@ class MyLocationTabFragment : Fragment(), TelegramListener {
searchBoxSidesMargin = resources.getDimensionPixelSize(R.dimen.content_padding_half)
sharingMode = settings.hasAnyChatToShareLocation()
savedInstanceState?.apply {
selectedChats.addAll(getLongArray(SELECTED_CHATS_KEY).toSet())
if (selectedChats.isNotEmpty()) {
@ -193,7 +193,6 @@ class MyLocationTabFragment : Fragment(), TelegramListener {
}
mainView.findViewById<View>(R.id.sharing_status_container).setOnClickListener {
settings.updateSharingStatusHistory()
fragmentManager?.also { fm ->
SharingStatusBottomSheet.showInstance(fm, this)
}
@ -211,7 +210,7 @@ class MyLocationTabFragment : Fragment(), TelegramListener {
updateContent()
}
}
return mainView
}
@ -228,7 +227,7 @@ class MyLocationTabFragment : Fragment(), TelegramListener {
super.onPause()
updateEnable = false
}
override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
outState.putLongArray(SELECTED_CHATS_KEY, selectedChats.toLongArray())
@ -247,6 +246,9 @@ class MyLocationTabFragment : Fragment(), TelegramListener {
app.stopSharingLocation()
updateContent()
}
SharingStatusBottomSheet.SHARING_STATUS_REQUEST_CODE -> {
updateSharingStatus()
}
}
}
@ -323,7 +325,7 @@ class MyLocationTabFragment : Fragment(), TelegramListener {
}
}, ADAPTER_UPDATE_INTERVAL_MIL)
}
private fun animateStartSharingBtn(show: Boolean) {
if (startSharingBtn.visibility == View.VISIBLE) {
val scale = if (show) 1f else 0f
@ -335,7 +337,7 @@ class MyLocationTabFragment : Fragment(), TelegramListener {
.start()
}
}
private fun clearSelection() {
selectedChats.clear()
adapter.notifyDataSetChanged()
@ -429,17 +431,16 @@ class MyLocationTabFragment : Fragment(), TelegramListener {
private fun updateSharingStatus() {
if (sharingMode) {
if (settings.sharingStatusChanges.isEmpty()) {
settings.updateSharingStatusHistory()
if (settings.sharingStatusChanges.isNotEmpty()) {
sharingStatusDescription.text = settings.sharingStatusChanges.last().getDescription(app)
}
sharingStatusDescription.text = settings.sharingStatusChanges.last().getDescription(app)
}
}
private fun updateList() {
val chats: MutableList<TdApi.Chat> = mutableListOf()
val currentUser = telegramHelper.getCurrentUser()
val chatList = if (sharingMode && settings.hasAnyChatToShareLocation()) {
val chatList = if (sharingMode) {
settings.getShareLocationChats()
} else {
telegramHelper.getChatListIds()
@ -468,7 +469,7 @@ class MyLocationTabFragment : Fragment(), TelegramListener {
list.sortWith(Comparator<TdApi.Chat> { o1, o2 -> o1.title.compareTo(o2.title) })
return list
}
inner class MyLocationListAdapter : RecyclerView.Adapter<MyLocationListAdapter.BaseViewHolder>() {
var chats = mutableListOf<TdApi.Chat>()
set(value) {
@ -547,10 +548,9 @@ class MyLocationTabFragment : Fragment(), TelegramListener {
isChecked = live
setOnCheckedChangeListener { _, isChecked ->
if (!isChecked) {
val currentMessageId = shareInfo?.currentMessageId
settings.shareLocationToChat(chat.id, false)
if (currentMessageId != null) {
telegramHelper.stopSendingLiveLocationToChat(chat.id, currentMessageId)
if (shareInfo != null) {
telegramHelper.stopSendingLiveLocationToChat(shareInfo)
}
removeItem(chat)
}
@ -567,7 +567,7 @@ class MyLocationTabFragment : Fragment(), TelegramListener {
}
val expiresIn = settings.getChatLiveMessageExpireTime(chat.id)
holder.textInArea?.apply {
val time = shareInfo?.additionalActiveTime ?: ADDITIONAL_ACTIVE_TIME_VALUES_SEC[0]
visibility = View.VISIBLE

View file

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

View file

@ -19,9 +19,9 @@ object OsmandFormatter {
val FEET_IN_ONE_METER = YARDS_IN_ONE_METER * 3f
private val fixed2 = DecimalFormat("0.00")
private val fixed1 = DecimalFormat("0.0")
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 MIN_DURATION_FOR_DATE_FORMAT = 48 * 60 * 60