Merge pull request #6212 from osmandapp/TelegramStatusImprovements

Telegram status improvements
This commit is contained in:
Alexander Sytnyk 2018-10-23 13:29:57 +03:00 committed by GitHub
commit 05b1e76f38
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 127 additions and 55 deletions

View file

@ -277,7 +277,7 @@
app:typeface="@string/font_roboto_regular" />
<net.osmand.telegram.ui.views.TextViewEx
android:id="@+id/sharing_status_description"
android:id="@+id/sharing_status_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="end"

View file

@ -1,4 +1,12 @@
<resources>
<string name="last_updated_location">Last updated location:</string>
<string name="successfully_sent_and_updated">Successfully sent and updated</string>
<string name="not_possible_to_send_to_telegram_chats">Not possible to send to Telegram chats:</string>
<string name="waiting_for_response_from_telegram">Waiting for response from Telegram</string>
<string name="sending_location_messages">Sending location messages</string>
<string name="initializing">Initializing</string>
<string name="searching_for_gps">Searching for GPS</string>
<string name="connecting_to_the_internet">Connecting to the internet</string>
<string name="background_work_description">Change battery optimization settings, for stable location sharing</string>
<string name="background_work">Background work</string>
<string name="battery_optimization_description">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.</string>

View file

@ -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<Long, ShareChatInfo>()
@ -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,12 @@ 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 +229,76 @@ 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 {
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)
} else if (!app.isInternetConnectionAvailable) {
description = app.getString(R.string.connecting_to_the_internet)
}
}
}
}
}
fun onDeleteLiveMessages(chatId: Long, messages: List<Long>) {
val currentMessageId = shareChatsInfo[chatId]?.currentMessageId
if (messages.contains(currentMessageId)) {
@ -508,36 +560,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 +604,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<String> = 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()

View file

@ -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)

View file

@ -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<View>(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))
}
}

View file

@ -51,18 +51,24 @@ class SharingStatusBottomSheet : DialogFragment() {
val time = sharingStatus.locationTime
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.getTitle(app)
findViewById<TextView>(R.id.status_change_time).text = OsmandFormatter.getFormattedTime(sharingStatus.statusChangeTime, false)
findViewById<TextView>(R.id.last_location_line).text = getString(sharingStatusType.descriptionId)
findViewById<TextView>(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<TextView>(R.id.last_location_line_time).text = descriptionTime
} else {
findViewById<TextView>(R.id.last_location_line_time).visibility = View.GONE
}
findViewById<TextView>(R.id.last_location_line_time).text = descriptionTime
findViewById<TextView>(R.id.re_send_location).apply {
if (sharingStatusType.canResendLocation) {
if (i == 0) {