add chatIdsToDuration to settings
This commit is contained in:
parent
951065e21c
commit
8380b29855
6 changed files with 76 additions and 24 deletions
|
@ -1,6 +1,7 @@
|
|||
package net.osmand.telegram
|
||||
|
||||
import android.content.Context
|
||||
import net.osmand.telegram.helpers.TelegramHelper
|
||||
import net.osmand.telegram.utils.OsmandFormatter.MetricsConstants
|
||||
import net.osmand.telegram.utils.OsmandFormatter.SpeedConstants
|
||||
|
||||
|
@ -17,6 +18,7 @@ private const val SEND_MY_LOCATION_INTERVAL_DEFAULT = 5L * 1000 // 5 seconds
|
|||
|
||||
private const val USER_LOCATION_EXPIRE_TIME_KEY = "user_location_expire_time"
|
||||
private const val USER_LOCATION_EXPIRE_TIME_DEFAULT = 15L * 60 * 1000 // 15 minutes
|
||||
private const val DEFAULT_VISIBLE_TIME_SECONDS = 60 * 60L // 1 hour
|
||||
|
||||
private const val TITLES_REPLACED_WITH_IDS = "changed_to_chat_id"
|
||||
|
||||
|
@ -24,6 +26,7 @@ class TelegramSettings(private val app: TelegramApplication) {
|
|||
|
||||
private var shareLocationChats: Set<Long> = emptySet()
|
||||
private var showOnMapChats: Set<Long> = emptySet()
|
||||
private var chatIdsToDuration = emptyMap<Long,Long>()
|
||||
|
||||
var metricsConstants = MetricsConstants.KILOMETERS_AND_METERS
|
||||
var speedConstants = SpeedConstants.KILOMETERS_PER_HOUR
|
||||
|
@ -52,20 +55,48 @@ class TelegramSettings(private val app: TelegramApplication) {
|
|||
val showOnMapChats = showOnMapChats.toMutableList()
|
||||
showOnMapChats.intersect(presentChatIds)
|
||||
this.showOnMapChats = showOnMapChats.toHashSet()
|
||||
|
||||
val chatIdsToDuration = HashMap<Long, Long>()
|
||||
this.chatIdsToDuration.forEach { chatId, duration ->
|
||||
if (presentChatIds.contains(chatId)) {
|
||||
chatIdsToDuration[chatId] = duration
|
||||
}
|
||||
}
|
||||
this.chatIdsToDuration = chatIdsToDuration.toMap()
|
||||
}
|
||||
|
||||
fun shareLocationToChat(chatId: Long, share: Boolean) {
|
||||
val shareLocationChats = shareLocationChats.toMutableList()
|
||||
val chatIdsToDuration = chatIdsToDuration.toMutableMap()
|
||||
if (share) {
|
||||
shareLocationChats.add(chatId)
|
||||
chatIdsToDuration[chatId] = DEFAULT_VISIBLE_TIME_SECONDS
|
||||
} else {
|
||||
shareLocationChats.remove(chatId)
|
||||
chatIdsToDuration.remove(chatId)
|
||||
}
|
||||
this.shareLocationChats = shareLocationChats.toHashSet()
|
||||
this.chatIdsToDuration = chatIdsToDuration.toMap()
|
||||
}
|
||||
|
||||
fun addChatIdToDuration(chatId: Long, duration: Long) {
|
||||
val chatIdsToDuration = chatIdsToDuration.toMutableMap()
|
||||
val lp: Long = when {
|
||||
duration < TelegramHelper.MIN_LOCATION_MESSAGE_LIVE_PERIOD_SEC -> TelegramHelper.MIN_LOCATION_MESSAGE_LIVE_PERIOD_SEC.toLong()
|
||||
duration > TelegramHelper.MAX_LOCATION_MESSAGE_LIVE_PERIOD_SEC -> TelegramHelper.MAX_LOCATION_MESSAGE_LIVE_PERIOD_SEC.toLong()
|
||||
else -> duration
|
||||
}
|
||||
chatIdsToDuration[chatId] = lp
|
||||
this.chatIdsToDuration = chatIdsToDuration.toMap()
|
||||
}
|
||||
|
||||
fun getChatExpireTime(chatId: Long): Long? {
|
||||
return chatIdsToDuration[chatId]
|
||||
}
|
||||
|
||||
fun stopSharingLocationToChats() {
|
||||
this.shareLocationChats = emptySet()
|
||||
this.chatIdsToDuration = emptyMap()
|
||||
}
|
||||
|
||||
fun showChatOnMap(chatId: Long, show: Boolean) {
|
||||
|
@ -82,6 +113,8 @@ class TelegramSettings(private val app: TelegramApplication) {
|
|||
|
||||
fun getShowOnMapChats() = ArrayList(showOnMapChats)
|
||||
|
||||
fun getChatIdsToDuration() = chatIdsToDuration
|
||||
|
||||
fun getShowOnMapChatsCount() = showOnMapChats.size
|
||||
|
||||
fun save() {
|
||||
|
|
|
@ -38,9 +38,9 @@ class ShareLocationHelper(private val app: TelegramApplication) {
|
|||
lastLocation = location
|
||||
|
||||
if (location != null && app.isInternetConnectionAvailable) {
|
||||
val shareLocationChats = app.settings.getShareLocationChats()
|
||||
if (shareLocationChats.isNotEmpty()) {
|
||||
app.telegramHelper.sendLiveLocationMessage(shareLocationChats, MAX_LOCATION_MESSAGE_LIVE_PERIOD_SEC, location.latitude, location.longitude)
|
||||
val chatIdsToDuration = app.settings.getChatIdsToDuration()
|
||||
if (chatIdsToDuration.isNotEmpty()) {
|
||||
app.telegramHelper.sendLiveLocationMessage(chatIdsToDuration, location.latitude, location.longitude)
|
||||
}
|
||||
lastLocationMessageSentTime = System.currentTimeMillis()
|
||||
}
|
||||
|
|
|
@ -385,15 +385,15 @@ class TelegramHelper private constructor() {
|
|||
* @latitude Latitude of the location
|
||||
* @longitude Longitude of the location
|
||||
*/
|
||||
fun sendLiveLocationMessage(chatIds: List<Long>, livePeriod: Int, latitude: Double, longitude: Double): Boolean {
|
||||
fun sendLiveLocationMessage(chatIdsToDuration: Map<Long, Long>, latitude: Double, longitude: Double): Boolean {
|
||||
if (!requestingActiveLiveLocationMessages && haveAuthorization) {
|
||||
if (needRefreshActiveLiveLocationMessages) {
|
||||
getActiveLiveLocationMessages {
|
||||
sendLiveLocationImpl(chatIds, livePeriod, latitude, longitude)
|
||||
sendLiveLocationImpl(chatIdsToDuration, latitude, longitude)
|
||||
}
|
||||
needRefreshActiveLiveLocationMessages = false
|
||||
} else {
|
||||
sendLiveLocationImpl(chatIds, livePeriod, latitude, longitude)
|
||||
sendLiveLocationImpl(chatIdsToDuration, latitude, longitude)
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
@ -428,24 +428,24 @@ class TelegramHelper private constructor() {
|
|||
}
|
||||
}
|
||||
|
||||
private fun sendLiveLocationImpl(chatIds: List<Long>, livePeriod: Int, latitude: Double, longitude: Double) {
|
||||
val lp = when {
|
||||
livePeriod < MIN_LOCATION_MESSAGE_LIVE_PERIOD_SEC -> MIN_LOCATION_MESSAGE_LIVE_PERIOD_SEC
|
||||
livePeriod > MAX_LOCATION_MESSAGE_LIVE_PERIOD_SEC -> MAX_LOCATION_MESSAGE_LIVE_PERIOD_SEC
|
||||
else -> livePeriod
|
||||
}
|
||||
private fun sendLiveLocationImpl(chatIdsToDuration: Map<Long, Long>, latitude: Double, longitude: Double) {
|
||||
val location = TdApi.Location(latitude, longitude)
|
||||
val content = TdApi.InputMessageLocation(location, lp)
|
||||
|
||||
for (chatId in chatIds) {
|
||||
chatIdsToDuration.forEach { chatId, duration ->
|
||||
val content = TdApi.InputMessageLocation(location, duration.toInt())
|
||||
val msgId = chatLiveMessages[chatId]
|
||||
if (msgId != null) {
|
||||
if (msgId != 0L) {
|
||||
client?.send(TdApi.EditMessageLiveLocation(chatId, msgId, null, location), liveLocationMessageUpdatesHandler)
|
||||
client?.send(
|
||||
TdApi.EditMessageLiveLocation(chatId, msgId, null, location),
|
||||
liveLocationMessageUpdatesHandler
|
||||
)
|
||||
}
|
||||
} else {
|
||||
chatLiveMessages[chatId] = 0L
|
||||
client?.send(TdApi.SendMessage(chatId, 0, false, true, null, content), liveLocationMessageUpdatesHandler)
|
||||
client?.send(
|
||||
TdApi.SendMessage(chatId, 0, false, true, null, content),
|
||||
liveLocationMessageUpdatesHandler
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -159,7 +159,7 @@ class MainActivity : AppCompatActivity(), TelegramListener, ActionButtonsListene
|
|||
if (AndroidUtils.isLocationPermissionAvailable(this)) {
|
||||
app.locationProvider.resumeAllUpdates()
|
||||
} else {
|
||||
requestLocationPermission()
|
||||
AndroidUtils.requestLocationPermission(this)
|
||||
}
|
||||
if (settings.hasAnyChatToShowOnMap() && osmandAidlHelper.isOsmandNotInstalled()) {
|
||||
showOsmandMissingDialog()
|
||||
|
@ -373,10 +373,6 @@ class MainActivity : AppCompatActivity(), TelegramListener, ActionButtonsListene
|
|||
}
|
||||
}
|
||||
|
||||
private fun requestLocationPermission() {
|
||||
ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.ACCESS_FINE_LOCATION), PERMISSION_REQUEST_LOCATION)
|
||||
}
|
||||
|
||||
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray) {
|
||||
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
|
||||
if (grantResults.isEmpty()) {
|
||||
|
@ -475,7 +471,7 @@ class MainActivity : AppCompatActivity(), TelegramListener, ActionButtonsListene
|
|||
if (settings.hasAnyChatToShareLocation()) {
|
||||
if (!AndroidUtils.isLocationPermissionAvailable(view.context)) {
|
||||
if (isChecked) {
|
||||
requestLocationPermission()
|
||||
AndroidUtils.requestLocationPermission(this@MainActivity)
|
||||
}
|
||||
} else {
|
||||
app.shareLocationHelper.startSharingLocation()
|
||||
|
|
|
@ -17,6 +17,7 @@ import net.osmand.telegram.TelegramApplication
|
|||
import net.osmand.telegram.helpers.ShareLocationHelper
|
||||
import net.osmand.telegram.helpers.TelegramUiHelper
|
||||
import net.osmand.telegram.ui.SetTimeDialogFragment.SetTimeListAdapter.ChatViewHolder
|
||||
import net.osmand.telegram.utils.AndroidUtils
|
||||
import org.drinkless.td.libcore.telegram.TdApi
|
||||
import java.util.concurrent.TimeUnit
|
||||
|
||||
|
@ -26,6 +27,7 @@ class SetTimeDialogFragment : DialogFragment() {
|
|||
get() = activity?.application as TelegramApplication
|
||||
|
||||
private val telegramHelper get() = app.telegramHelper
|
||||
private val settings get() = app.settings
|
||||
|
||||
private val adapter = SetTimeListAdapter()
|
||||
|
||||
|
@ -77,6 +79,19 @@ class SetTimeDialogFragment : DialogFragment() {
|
|||
text = getString(R.string.shared_string_share)
|
||||
setOnClickListener {
|
||||
Toast.makeText(context, "Share", Toast.LENGTH_SHORT).show()
|
||||
chatIdsToDuration.forEach { chatId, expireTime ->
|
||||
settings.shareLocationToChat(chatId, true)
|
||||
settings.addChatIdToDuration(chatId, expireTime)
|
||||
}
|
||||
if (settings.hasAnyChatToShareLocation()) {
|
||||
if (!AndroidUtils.isLocationPermissionAvailable(view.context)) {
|
||||
AndroidUtils.requestLocationPermission(activity!!)
|
||||
} else {
|
||||
app.shareLocationHelper.startSharingLocation()
|
||||
}
|
||||
} else {
|
||||
app.shareLocationHelper.stopSharingLocation()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -102,7 +117,8 @@ class SetTimeDialogFragment : DialogFragment() {
|
|||
chatIdsToDuration.clear()
|
||||
bundle?.getLongArray(CHATS_KEY)?.also {
|
||||
for (i in 0 until it.size step 2) {
|
||||
chatIdsToDuration[it[i]] = it[i + 1]
|
||||
val expireTime = settings.getChatExpireTime(it[i])
|
||||
chatIdsToDuration[it[i]] = expireTime ?: it[i + 1]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -19,6 +19,8 @@ import android.view.inputmethod.InputMethodManager
|
|||
import java.io.File
|
||||
|
||||
object AndroidUtils {
|
||||
|
||||
private const val PERMISSION_REQUEST_LOCATION = 1
|
||||
|
||||
private fun isHardwareKeyboardAvailable(context: Context): Boolean {
|
||||
return context.resources.configuration.keyboard != Configuration.KEYBOARD_NOKEYS
|
||||
|
@ -50,6 +52,11 @@ object AndroidUtils {
|
|||
return ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
|
||||
}
|
||||
|
||||
|
||||
fun requestLocationPermission(activity: Activity) {
|
||||
ActivityCompat.requestPermissions(activity, arrayOf(Manifest.permission.ACCESS_FINE_LOCATION), PERMISSION_REQUEST_LOCATION)
|
||||
}
|
||||
|
||||
fun dpToPx(ctx: Context, dp: Float): Int {
|
||||
val r = ctx.resources
|
||||
return TypedValue.applyDimension(
|
||||
|
|
Loading…
Reference in a new issue