From ec2f36760dc95d0000b80044889746de1a57b07a Mon Sep 17 00:00:00 2001 From: Chumva Date: Tue, 23 Oct 2018 12:21:15 +0300 Subject: [PATCH 1/2] Add new sharing statuses for telegram --- .../res/layout/fragment_my_location_tab.xml | 2 +- OsmAnd-telegram/res/values/strings.xml | 8 + .../net/osmand/telegram/TelegramSettings.kt | 144 +++++++++++++----- .../telegram/helpers/ShareLocationHelper.kt | 2 +- .../telegram/ui/MyLocationTabFragment.kt | 10 +- .../telegram/ui/SharingStatusBottomSheet.kt | 22 ++- 6 files changed, 133 insertions(+), 55 deletions(-) diff --git a/OsmAnd-telegram/res/layout/fragment_my_location_tab.xml b/OsmAnd-telegram/res/layout/fragment_my_location_tab.xml index 98c101808f..000d64192a 100644 --- a/OsmAnd-telegram/res/layout/fragment_my_location_tab.xml +++ b/OsmAnd-telegram/res/layout/fragment_my_location_tab.xml @@ -277,7 +277,7 @@ app:typeface="@string/font_roboto_regular" /> + Last updated location: + Successfully sent and updated + Not possible to send to Telegram chats: + Waiting for response from Telegram + Sending location messages + Initializing… + Searching for GPS + Connecting to the internet Change battery optimization settings, for stable location sharing Background work For stable sharing of your position in the background, it is advisable to disable battery optimization for OsmAnd Telegram.\n\nIf optimization is enabled, the system may automatically turn off the application running in the background (when the screen is locked and/or the app is minimized). This happens without notification and causes the geo position broadcast to stop. diff --git a/OsmAnd-telegram/src/net/osmand/telegram/TelegramSettings.kt b/OsmAnd-telegram/src/net/osmand/telegram/TelegramSettings.kt index 0f59fc24a4..ed2bd355ee 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/TelegramSettings.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/TelegramSettings.kt @@ -1,6 +1,7 @@ package net.osmand.telegram import android.content.Context +import android.location.LocationManager import android.support.annotation.ColorRes import android.support.annotation.DrawableRes import android.support.annotation.StringRes @@ -69,6 +70,8 @@ private const val SHARE_CHATS_INFO_KEY = "share_chats_info" private const val BATTERY_OPTIMISATION_ASKED = "battery_optimisation_asked" +private const val SHARING_INITIALIZATION_TIME = 60 * 2L // 2 minutes + class TelegramSettings(private val app: TelegramApplication) { private var shareChatsInfo = ConcurrentHashMap() @@ -202,34 +205,7 @@ class TelegramSettings(private val app: TelegramApplication) { } fun updateSharingStatusHistory() { - val newSharingStatus = SharingStatus().apply { - statusChangeTime = System.currentTimeMillis() - statusType = if (!app.isInternetConnectionAvailable) { - locationTime = getLastSuccessfulSendTime() - SharingStatusType.NO_INTERNET - } else if (app.shareLocationHelper.lastLocation == null) { - locationTime = app.shareLocationHelper.lastLocationMessageSentTime - SharingStatusType.NO_GPS - } else { - var sendChatsErrors = false - shareChatsInfo.forEach { (_, shareInfo) -> - if (shareInfo.hasSharingError || shareInfo.lastSuccessfulSendTimeMs == -1L) { - sendChatsErrors = true - locationTime = shareInfo.lastSuccessfulSendTimeMs - val title = app.telegramHelper.getChat(shareInfo.chatId)?.title - if (title != null) { - chatsTitles.add(title) - } - } - } - if (sendChatsErrors) { - SharingStatusType.NOT_POSSIBLE_TO_SENT_TO_CHATS - } else { - locationTime = getLastSuccessfulSendTime() - SharingStatusType.SUCCESSFULLY_SENT - } - } - } + val newSharingStatus = getNewSharingStatusHistoryItem() if (sharingStatusChanges.isNotEmpty()) { val lastSharingStatus = sharingStatusChanges.last() @@ -240,6 +216,14 @@ class TelegramSettings(private val app: TelegramApplication) { statusChangeTime = newSharingStatus.statusChangeTime locationTime = newSharingStatus.locationTime chatsTitles = newSharingStatus.chatsTitles + + if (statusType == SharingStatusType.INITIALIZING + && newSharingStatus.statusType == SharingStatusType.INITIALIZING + && !lastSharingStatus.description.contains(newSharingStatus.description) + ) { + lastSharingStatus.description = + "${lastSharingStatus.description}, ${newSharingStatus.description}" + } } } } else { @@ -247,6 +231,80 @@ class TelegramSettings(private val app: TelegramApplication) { } } + private fun getNewSharingStatusHistoryItem(): SharingStatus { + return SharingStatus().apply { + statusChangeTime = System.currentTimeMillis() + val lm = app.getSystemService(Context.LOCATION_SERVICE) as LocationManager + val gpsEnabled = try { + lm.isProviderEnabled(LocationManager.GPS_PROVIDER) + } catch (ex: Exception) { + false + } + + var initializing = false + var sendChatsErrors = false + + shareChatsInfo.forEach { (_, shareInfo) -> + if (shareInfo.lastSuccessfulSendTimeMs == -1L && ((statusChangeTime / 1000 - shareInfo.start) < SHARING_INITIALIZATION_TIME)) { + initializing = true + } + if (shareInfo.hasSharingError) { + sendChatsErrors = true + locationTime = shareInfo.lastSuccessfulSendTimeMs + val title = app.telegramHelper.getChat(shareInfo.chatId)?.title + if (title != null) { + chatsTitles.add(title) + } + } + } + + if (sendChatsErrors) { + title = app.getString(R.string.not_possible_to_send_to_telegram_chats) + description = app.getString(R.string.last_updated_location) + statusType = SharingStatusType.NOT_POSSIBLE_TO_SENT_TO_CHATS + } else if (!initializing) { + when { + !gpsEnabled -> { + locationTime = app.shareLocationHelper.lastLocationMessageSentTime + title = app.getString(R.string.no_gps_connection) + description = app.getString(R.string.last_updated_location) + statusType = SharingStatusType.NO_GPS + } + !app.isInternetConnectionAvailable -> { + locationTime = getLastSuccessfulSendTime() + title = app.getString(R.string.no_internet_connection) + description = app.getString(R.string.last_updated_location) + statusType = SharingStatusType.NO_INTERNET + } + else -> { + locationTime = getLastSuccessfulSendTime() + title = app.getString(R.string.successfully_sent_and_updated) + description = app.getString(R.string.last_updated_location) + statusType = SharingStatusType.SUCCESSFULLY_SENT + } + } + } else { + when { + !gpsEnabled -> { + title = app.getString(R.string.initializing) + description = app.getString(R.string.searching_for_gps) + statusType = SharingStatusType.INITIALIZING + } + !app.isInternetConnectionAvailable -> { + title = app.getString(R.string.initializing) + description = app.getString(R.string.connecting_to_the_internet) + statusType = SharingStatusType.INITIALIZING + } + else -> { + title = app.getString(R.string.sending_location_messages) + description = app.getString(R.string.waiting_for_response_from_telegram) + statusType = SharingStatusType.SENDING + } + } + } + } + } + fun onDeleteLiveMessages(chatId: Long, messages: List) { val currentMessageId = shareChatsInfo[chatId]?.currentMessageId if (messages.contains(currentMessageId)) { @@ -508,36 +566,36 @@ class TelegramSettings(private val app: TelegramApplication) { enum class SharingStatusType( @DrawableRes val iconId: Int, @ColorRes val iconColorRes: Int, - @StringRes val titleId: Int, - @StringRes val descriptionId: Int, val canResendLocation: Boolean ) { NO_INTERNET( R.drawable.ic_action_wifi_off, R.color.sharing_status_icon_error, - R.string.no_internet_connection, - R.string.last_sent_location, true ), SUCCESSFULLY_SENT( R.drawable.ic_action_share_location, R.color.sharing_status_icon_success, - R.string.sharing_success, - R.string.last_sent_location, + false + ), + SENDING( + R.drawable.ic_action_share_location, + R.color.sharing_status_icon_success, false ), NOT_POSSIBLE_TO_SENT_TO_CHATS( R.drawable.ic_action_message_send_error, R.color.sharing_status_icon_error, - R.string.not_possible_to_send_to_chats, - R.string.last_sent_location, true ), NO_GPS( R.drawable.ic_action_location_off, R.color.sharing_status_icon_error, - R.string.no_gps_connection, - R.string.last_available_location, + false + ), + INITIALIZING( + R.drawable.ic_action_connect, + R.color.sharing_status_icon_error, false ); } @@ -552,16 +610,20 @@ class TelegramSettings(private val app: TelegramApplication) { } class SharingStatus { + + var title: String = "" + var description: String = "" var locationTime: Long = -1 var statusChangeTime: Long = -1 var chatsTitles: MutableList = mutableListOf() + lateinit var statusType: SharingStatusType - fun getDescription(app: TelegramApplication): CharSequence { + fun getTitle(app: TelegramApplication): CharSequence { return if (statusType != SharingStatusType.NOT_POSSIBLE_TO_SENT_TO_CHATS || chatsTitles.isEmpty()) { - app.getString(statusType.titleId) + title } else { - val spannableString = SpannableStringBuilder(app.getString(statusType.titleId)) + val spannableString = SpannableStringBuilder(title) val iterator = chatsTitles.iterator() while (iterator.hasNext()) { val chatTitle = iterator.next() diff --git a/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShareLocationHelper.kt b/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShareLocationHelper.kt index 8a8e3b0dec..74001a6d03 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShareLocationHelper.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShareLocationHelper.kt @@ -64,7 +64,7 @@ class ShareLocationHelper(private val app: TelegramApplication) { fun updateSendLiveMessages() { log.info("updateSendLiveMessages") if (app.settings.hasAnyChatToShareLocation()) { - app.settings.getChatsShareInfo().forEach { chatId, shareInfo -> + app.settings.getChatsShareInfo().forEach { (chatId, shareInfo) -> val currentTime = System.currentTimeMillis() / 1000 when { shareInfo.getChatLiveMessageExpireTime() <= 0 -> app.settings.shareLocationToChat(chatId, false) diff --git a/OsmAnd-telegram/src/net/osmand/telegram/ui/MyLocationTabFragment.kt b/OsmAnd-telegram/src/net/osmand/telegram/ui/MyLocationTabFragment.kt index 684c5b954f..41fa47db5b 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/ui/MyLocationTabFragment.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/ui/MyLocationTabFragment.kt @@ -59,7 +59,7 @@ class MyLocationTabFragment : Fragment(), TelegramListener { private lateinit var description: TextView private lateinit var searchBox: FrameLayout private lateinit var stopSharingSwitcher: Switch - private lateinit var sharingStatusDescription: TextView + private lateinit var sharingStatusTitle: TextView private lateinit var sharingStatusIcon: ImageView private lateinit var startSharingBtn: View @@ -205,7 +205,7 @@ class MyLocationTabFragment : Fragment(), TelegramListener { stopSharingSwitcher = mainView.findViewById(R.id.stop_all_sharing_switcher) - sharingStatusDescription = mainView.findViewById(R.id.sharing_status_description) + sharingStatusTitle = mainView.findViewById(R.id.sharing_status_title) startSharingBtn = mainView.findViewById(R.id.start_sharing_btn).apply { visibility = if (sharingMode) View.VISIBLE else View.GONE @@ -443,9 +443,11 @@ class MyLocationTabFragment : Fragment(), TelegramListener { private fun updateSharingStatus() { if (sharingMode) { - settings.updateSharingStatusHistory() + if (settings.sharingStatusChanges.isEmpty()) { + settings.updateSharingStatusHistory() + } val sharingStatus = settings.sharingStatusChanges.last() - sharingStatusDescription.text = sharingStatus.getDescription(app) + sharingStatusTitle.text = sharingStatus.getTitle(app) sharingStatusIcon.setImageDrawable(app.uiUtils.getIcon(sharingStatus.statusType.iconId, sharingStatus.statusType.iconColorRes)) } } diff --git a/OsmAnd-telegram/src/net/osmand/telegram/ui/SharingStatusBottomSheet.kt b/OsmAnd-telegram/src/net/osmand/telegram/ui/SharingStatusBottomSheet.kt index 77edb348db..0708a2ff06 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/ui/SharingStatusBottomSheet.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/ui/SharingStatusBottomSheet.kt @@ -51,18 +51,24 @@ class SharingStatusBottomSheet : DialogFragment() { val time = sharingStatus.locationTime findViewById(R.id.icon).setImageDrawable(uiUtils.getIcon(sharingStatusType.iconId, sharingStatusType.iconColorRes)) - findViewById(R.id.title).text = sharingStatus.getDescription(app) + findViewById(R.id.title).text = sharingStatus.getTitle(app) findViewById(R.id.status_change_time).text = OsmandFormatter.getFormattedTime(sharingStatus.statusChangeTime, false) - findViewById(R.id.last_location_line).text = getString(sharingStatusType.descriptionId) + findViewById(R.id.last_location_line).text = sharingStatus.description - val descriptionTime = when { - time > 0 -> OsmandFormatter.getFormattedTime(time, false) - sharingStatusType == TelegramSettings.SharingStatusType.NO_GPS -> getString(R.string.not_found_yet) - else -> getString(R.string.not_sent_yet) + if (sharingStatusType != TelegramSettings.SharingStatusType.INITIALIZING + && sharingStatusType != TelegramSettings.SharingStatusType.SENDING) { + val descriptionTime = when { + time > 0 -> OsmandFormatter.getFormattedTime(time, false) + sharingStatusType == TelegramSettings.SharingStatusType.NO_GPS -> getString( + R.string.not_found_yet + ) + else -> getString(R.string.not_sent_yet) + } + findViewById(R.id.last_location_line_time).text = descriptionTime + } else { + findViewById(R.id.last_location_line_time).visibility = View.GONE } - findViewById(R.id.last_location_line_time).text = descriptionTime - findViewById(R.id.re_send_location).apply { if (sharingStatusType.canResendLocation) { if (i == 0) { From 6dc64ef13c8c037700d2c3d46ac7aeb9163178c3 Mon Sep 17 00:00:00 2001 From: Chumva Date: Tue, 23 Oct 2018 12:36:28 +0300 Subject: [PATCH 2/2] Remove unnecessary code --- OsmAnd-telegram/res/values/strings.xml | 2 +- .../net/osmand/telegram/TelegramSettings.kt | 28 ++++++++----------- 2 files changed, 12 insertions(+), 18 deletions(-) diff --git a/OsmAnd-telegram/res/values/strings.xml b/OsmAnd-telegram/res/values/strings.xml index c3302b090a..483b28bd91 100644 --- a/OsmAnd-telegram/res/values/strings.xml +++ b/OsmAnd-telegram/res/values/strings.xml @@ -4,7 +4,7 @@ Not possible to send to Telegram chats: Waiting for response from Telegram Sending location messages - Initializing… + Initializing Searching for GPS Connecting to the internet Change battery optimization settings, for stable location sharing diff --git a/OsmAnd-telegram/src/net/osmand/telegram/TelegramSettings.kt b/OsmAnd-telegram/src/net/osmand/telegram/TelegramSettings.kt index ed2bd355ee..451918a91f 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/TelegramSettings.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/TelegramSettings.kt @@ -219,10 +219,8 @@ class TelegramSettings(private val app: TelegramApplication) { if (statusType == SharingStatusType.INITIALIZING && newSharingStatus.statusType == SharingStatusType.INITIALIZING - && !lastSharingStatus.description.contains(newSharingStatus.description) - ) { - lastSharingStatus.description = - "${lastSharingStatus.description}, ${newSharingStatus.description}" + && !lastSharingStatus.description.contains(newSharingStatus.description)) { + lastSharingStatus.description = "${lastSharingStatus.description}, ${newSharingStatus.description}" } } } @@ -284,21 +282,17 @@ class TelegramSettings(private val app: TelegramApplication) { } } } else { - when { - !gpsEnabled -> { - title = app.getString(R.string.initializing) + if (gpsEnabled && app.isInternetConnectionAvailable) { + title = app.getString(R.string.sending_location_messages) + description = app.getString(R.string.waiting_for_response_from_telegram) + statusType = SharingStatusType.SENDING + } else { + title = app.getString(R.string.initializing) + statusType = SharingStatusType.INITIALIZING + if (!gpsEnabled) { description = app.getString(R.string.searching_for_gps) - statusType = SharingStatusType.INITIALIZING - } - !app.isInternetConnectionAvailable -> { - title = app.getString(R.string.initializing) + } else if (!app.isInternetConnectionAvailable) { description = app.getString(R.string.connecting_to_the_internet) - statusType = SharingStatusType.INITIALIZING - } - else -> { - title = app.getString(R.string.sending_location_messages) - description = app.getString(R.string.waiting_for_response_from_telegram) - statusType = SharingStatusType.SENDING } } }