From db11fb61814a9097dbe508bff8bb7ad54c7edb5d Mon Sep 17 00:00:00 2001 From: Chumva Date: Fri, 24 May 2019 11:58:26 +0300 Subject: [PATCH 1/2] Add gpx logging settings initial commit --- .../res/layout/fragement_settings_dialog.xml | 29 ++++ OsmAnd-telegram/res/values/strings.xml | 8 ++ .../net/osmand/telegram/TelegramSettings.kt | 126 ++++++++++++++++-- .../telegram/helpers/ShareLocationHelper.kt | 32 ++++- .../telegram/ui/SettingsDialogFragment.kt | 37 +++-- 5 files changed, 200 insertions(+), 32 deletions(-) diff --git a/OsmAnd-telegram/res/layout/fragement_settings_dialog.xml b/OsmAnd-telegram/res/layout/fragement_settings_dialog.xml index a3a9f4a177..6b4af3e0e8 100644 --- a/OsmAnd-telegram/res/layout/fragement_settings_dialog.xml +++ b/OsmAnd-telegram/res/layout/fragement_settings_dialog.xml @@ -131,6 +131,35 @@ + + + + + + + + + + + Select + Minimum logging distance + Filter: minimum distance to log a new point + Minimum logging accuracy + Filter: no logging unless the accuracy is reached + Minimum logging speed + Filter: no logging below selected speed + GPX settings Key Password Username diff --git a/OsmAnd-telegram/src/net/osmand/telegram/TelegramSettings.kt b/OsmAnd-telegram/src/net/osmand/telegram/TelegramSettings.kt index 34bbd5bda3..354f64538c 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/TelegramSettings.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/TelegramSettings.kt @@ -43,6 +43,9 @@ private val LOC_HISTORY_VALUES_SEC = listOf( 12 * 60 * 60L, 24 * 60 * 60L ) +private val MIN_LOCATION_DISTANCE = listOf(0f, 2.0f, 5.0f, 10.0f, 20.0f, 30.0f, 50.0f) +private val MIN_LOCATION_ACCURACY = listOf(0f, 1.0f, 2.0f, 5.0f, 10.0f, 15.0f, 20.0f, 50.0f, 100.0f) +private val MIN_LOCATION_SPEED = listOf(0f, 0.000001f, 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f) const val SHARE_TYPE_MAP = "Map" const val SHARE_TYPE_TEXT = "Text" @@ -53,6 +56,9 @@ private const val SEND_MY_LOC_DEFAULT_INDEX = 6 private const val STALE_LOC_DEFAULT_INDEX = 0 private const val LOC_HISTORY_DEFAULT_INDEX = 7 private const val SHARE_TYPE_DEFAULT_INDEX = 2 +private const val MIN_LOCATION_DISTANCE_INDEX = 0 +private const val MIN_LOCATION_ACCURACY_INDEX = 0 +private const val MIN_LOCATION_SPEED_INDEX = 0 private const val SETTINGS_NAME = "osmand_telegram_settings" @@ -69,6 +75,10 @@ private const val STALE_LOC_TIME_KEY = "stale_loc_time" private const val LOC_HISTORY_TIME_KEY = "loc_history_time" private const val SHARE_TYPE_KEY = "share_type" +private const val MIN_LOCATION_DISTANCE_KEY = "min_location_distance" +private const val MIN_LOCATION_ACCURACY_KEY = "min_location_accuracy" +private const val MIN_LOCATION_SPEED_KEY = "min_location_speed" + private const val APP_TO_CONNECT_PACKAGE_KEY = "app_to_connect_package" private const val DEFAULT_VISIBLE_TIME_SECONDS = 60 * 60L // 1 hour @@ -117,12 +127,17 @@ class TelegramSettings(private val app: TelegramApplication) { var locHistoryTime = LOC_HISTORY_VALUES_SEC[LOC_HISTORY_DEFAULT_INDEX] var shareTypeValue = SHARE_TYPE_VALUES[SHARE_TYPE_DEFAULT_INDEX] + var minLocationDistance = MIN_LOCATION_DISTANCE[MIN_LOCATION_DISTANCE_INDEX] + var minLocationAccuracy = MIN_LOCATION_ACCURACY[MIN_LOCATION_ACCURACY_INDEX] + var minLocationSpeed = MIN_LOCATION_SPEED[MIN_LOCATION_SPEED_INDEX] + var appToConnectPackage = "" private set var liveNowSortType = LiveNowSortType.SORT_BY_DISTANCE val gpsAndLocPrefs = listOf(SendMyLocPref(), StaleLocPref(), LocHistoryPref(), ShareTypePref()) + val gpxLoggingPrefs = listOf(MinLocationDistance(), MinLocationAccuracy(), MinLocationSpeed()) var batteryOptimisationAsked = false @@ -557,6 +572,10 @@ class TelegramSettings(private val app: TelegramApplication) { edit.putLong(STALE_LOC_TIME_KEY, staleLocTime) edit.putLong(LOC_HISTORY_TIME_KEY, locHistoryTime) + edit.putFloat(MIN_LOCATION_DISTANCE_KEY, minLocationDistance) + edit.putFloat(MIN_LOCATION_ACCURACY_KEY, minLocationAccuracy) + edit.putFloat(MIN_LOCATION_SPEED_KEY, minLocationSpeed) + edit.putString(SHARE_TYPE_KEY, shareTypeValue) edit.putString(APP_TO_CONNECT_PACKAGE_KEY, appToConnectPackage) @@ -623,6 +642,13 @@ class TelegramSettings(private val app: TelegramApplication) { val shareTypeDef = SHARE_TYPE_VALUES[SHARE_TYPE_DEFAULT_INDEX] shareTypeValue = prefs.getString(SHARE_TYPE_KEY, shareTypeDef) + val minLocationDistanceDef = MIN_LOCATION_DISTANCE[MIN_LOCATION_DISTANCE_INDEX] + minLocationDistance = prefs.getFloat(MIN_LOCATION_DISTANCE_KEY, minLocationDistanceDef) + val minLocationPrecisionDef = MIN_LOCATION_ACCURACY[MIN_LOCATION_ACCURACY_INDEX] + minLocationAccuracy = prefs.getFloat(MIN_LOCATION_ACCURACY_KEY, minLocationPrecisionDef) + val minLocationSpeedDef = MIN_LOCATION_SPEED[MIN_LOCATION_SPEED_INDEX] + minLocationSpeed = prefs.getFloat(MIN_LOCATION_SPEED_KEY, minLocationSpeedDef) + val currentUserId = app.telegramHelper.getCurrentUserId() currentSharingMode = prefs.getString(SHARING_MODE_KEY, if (currentUserId != -1) currentUserId.toString() else "") @@ -787,7 +813,7 @@ class TelegramSettings(private val app: TelegramApplication) { } } - inner class SendMyLocPref : DurationPref( + inner class SendMyLocPref : NumericPref( R.drawable.ic_action_share_location, R.string.send_my_location, R.string.send_my_location_desc, @@ -798,12 +824,15 @@ class TelegramSettings(private val app: TelegramApplication) { OsmandFormatter.getFormattedDuration(app, sendMyLocInterval) override fun setCurrentValue(index: Int) { - sendMyLocInterval = values[index] + sendMyLocInterval = values[index].toLong() app.updateSendLocationInterval() } + + override fun getMenuItems() = + values.map { OsmandFormatter.getFormattedDuration(app, it.toLong()) } } - inner class StaleLocPref : DurationPref( + inner class StaleLocPref : NumericPref( R.drawable.ic_action_time_span, R.string.stale_location, R.string.stale_location_desc, @@ -814,11 +843,14 @@ class TelegramSettings(private val app: TelegramApplication) { OsmandFormatter.getFormattedDuration(app, staleLocTime) override fun setCurrentValue(index: Int) { - staleLocTime = values[index] + staleLocTime = values[index].toLong() } + + override fun getMenuItems() = + values.map { OsmandFormatter.getFormattedDuration(app, it.toLong()) } } - inner class LocHistoryPref : DurationPref( + inner class LocHistoryPref : NumericPref( R.drawable.ic_action_location_history, R.string.location_history, R.string.location_history_desc, @@ -830,12 +862,15 @@ class TelegramSettings(private val app: TelegramApplication) { override fun setCurrentValue(index: Int) { val value = values[index] - locHistoryTime = value - app.telegramHelper.messageActiveTimeSec = value + locHistoryTime = value.toLong() + app.telegramHelper.messageActiveTimeSec = value.toLong() } + + override fun getMenuItems() = + values.map { OsmandFormatter.getFormattedDuration(app, it.toLong()) } } - inner class ShareTypePref : DurationPref( + inner class ShareTypePref : NumericPref( R.drawable.ic_action_location_history, R.string.send_location_as, R.string.send_location_as_descr, @@ -871,18 +906,87 @@ class TelegramSettings(private val app: TelegramApplication) { } } - abstract inner class DurationPref( + inner class MinLocationDistance : NumericPref( + R.drawable.ic_action_location_history, + R.string.min_logging_distance, + R.string.min_logging_distance_descr, + MIN_LOCATION_DISTANCE + ) { + + override fun getCurrentValue() = getFormattedValue(minLocationDistance) + + override fun setCurrentValue(index: Int) { + val value = values[index] + minLocationDistance = value.toFloat() + } + + override fun getMenuItems() = values.map { getFormattedValue(it.toFloat()) } + + private fun getFormattedValue(value: Float): String { + return if (value == 0f) app.getString(R.string.shared_string_select) + else OsmandFormatter.getFormattedDistance(value, app) + } + } + + inner class MinLocationAccuracy : NumericPref( + R.drawable.ic_action_location_history, + R.string.min_logging_accuracy, + R.string.min_logging_accuracy_descr, + MIN_LOCATION_ACCURACY + ) { + + override fun getCurrentValue() = getFormattedValue(minLocationAccuracy) + + override fun setCurrentValue(index: Int) { + val value = values[index] + minLocationAccuracy = value.toFloat() + } + + override fun getMenuItems() = values.map { getFormattedValue(it.toFloat()) } + + private fun getFormattedValue(value: Float): String { + return if (value == 0f) app.getString(R.string.shared_string_select) + else OsmandFormatter.getFormattedDistance(value, app) + } + } + + inner class MinLocationSpeed : NumericPref( + R.drawable.ic_action_location_history, + R.string.min_logging_speed, + R.string.min_logging_speed_descr, + MIN_LOCATION_SPEED + ) { + + override fun getCurrentValue() = getFormattedValue(minLocationSpeed) + + override fun setCurrentValue(index: Int) { + val value = values[index] + minLocationSpeed = value.toFloat() + } + + override fun getMenuItems() = values.map { getFormattedValue(it.toFloat()) } + + private fun getFormattedValue(value: Float): String { + return when (value) { + MIN_LOCATION_SPEED[0] -> app.getString(R.string.shared_string_select) + MIN_LOCATION_SPEED[1] -> "> 0" + else -> OsmandFormatter.getFormattedSpeed(value, app) + } + } + } + + abstract inner class NumericPref( @DrawableRes val iconId: Int, @StringRes val titleId: Int, @StringRes val descriptionId: Int, - val values: List + val values: List ) { abstract fun getCurrentValue(): String abstract fun setCurrentValue(index: Int) - open fun getMenuItems() = values.map { OsmandFormatter.getFormattedDuration(app, it) } + abstract fun getMenuItems(): List } enum class AppConnect( diff --git a/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShareLocationHelper.kt b/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShareLocationHelper.kt index 449d9a9fe1..21c48be3c5 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShareLocationHelper.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShareLocationHelper.kt @@ -7,14 +7,13 @@ import net.osmand.telegram.helpers.LocationMessages.BufferMessage import net.osmand.telegram.notifications.TelegramNotification.NotificationType import net.osmand.telegram.utils.AndroidNetworkUtils import net.osmand.telegram.utils.BASE_URL +import net.osmand.util.MapUtils import org.drinkless.td.libcore.telegram.TdApi import org.json.JSONException import org.json.JSONObject private const val USER_SET_LIVE_PERIOD_DELAY_MS = 5000 // 5 sec -private const val UPDATE_LOCATION_ACCURACY = 150 // 150 meters - class ShareLocationHelper(private val app: TelegramApplication) { private val log = PlatformUtil.getLog(ShareLocationHelper::class.java) @@ -48,14 +47,33 @@ class ShareLocationHelper(private val app: TelegramApplication) { private var lastTimeInMillis: Long = 0L fun updateLocation(location: Location?) { - lastLocation = location + val lastPoint = lastLocation + var record = true + if (location != null) { + val minDistance = app.settings.minLocationDistance + if (minDistance > 0 && lastPoint != null) { + val calculatedDistance = MapUtils.getDistance(lastPoint.latitude, lastPoint.longitude, location.latitude, location.longitude) + if (calculatedDistance < minDistance) { + record = false + } + } + val accuracy = app.settings.minLocationAccuracy + if (accuracy > 0 && (!location.hasAccuracy() || location.accuracy > accuracy)) { + record = false + } + val minSpeed = app.settings.minLocationSpeed + if (minSpeed > 0 && (!location.hasSpeed() || location.speed < minSpeed)) { + record = false + } - if (location != null && location.accuracy < UPDATE_LOCATION_ACCURACY) { - lastLocationUpdateTime = System.currentTimeMillis() - if (app.settings.getChatsShareInfo().isNotEmpty()) { - shareLocationMessages(location, app.telegramHelper.getCurrentUserId()) + if (record) { + lastLocationUpdateTime = System.currentTimeMillis() + if (app.settings.getChatsShareInfo().isNotEmpty()) { + shareLocationMessages(location, app.telegramHelper.getCurrentUserId()) + } } } + lastLocation = location app.settings.updateSharingStatusHistory() refreshNotification() } diff --git a/OsmAnd-telegram/src/net/osmand/telegram/ui/SettingsDialogFragment.kt b/OsmAnd-telegram/src/net/osmand/telegram/ui/SettingsDialogFragment.kt index 71b5fe9a29..66697bff9e 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/ui/SettingsDialogFragment.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/ui/SettingsDialogFragment.kt @@ -17,7 +17,7 @@ import android.view.ViewGroup import android.widget.* import net.osmand.telegram.R import net.osmand.telegram.TelegramSettings -import net.osmand.telegram.TelegramSettings.DurationPref +import net.osmand.telegram.TelegramSettings.NumericPref import net.osmand.telegram.helpers.TelegramHelper.Companion.OSMAND_BOT_USERNAME import net.osmand.telegram.helpers.TelegramUiHelper import net.osmand.telegram.utils.AndroidUtils @@ -49,18 +49,8 @@ class SettingsDialogFragment : BaseDialogFragment() { window.statusBarColor = ContextCompat.getColor(app, R.color.card_bg_light) } var container = mainView.findViewById(R.id.gps_and_loc_container) - for (pref in settings.gpsAndLocPrefs) { - inflater.inflate(R.layout.item_with_desc_and_right_value, container, false).apply { - findViewById(R.id.icon).setImageDrawable(uiUtils.getThemedIcon(pref.iconId)) - findViewById(R.id.title).setText(pref.titleId) - findViewById(R.id.description).setText(pref.descriptionId) - val valueView = findViewById(R.id.value) - valueView.text = pref.getCurrentValue() - setOnClickListener { - showPopupMenu(pref, valueView) - } - container.addView(this) - } + settings.gpsAndLocPrefs.forEach { + createNumericPref(inflater, container, it) } if (Build.VERSION.SDK_INT >= 26) { @@ -148,6 +138,11 @@ class SettingsDialogFragment : BaseDialogFragment() { } } + container = mainView.findViewById(R.id.gpx_settings_container) + settings.gpxLoggingPrefs.forEach { + createNumericPref(inflater, container, it) + } + container = mainView.findViewById(R.id.osmand_connect_container) for (appConn in TelegramSettings.AppConnect.values()) { val pack = appConn.appPackage @@ -249,6 +244,20 @@ class SettingsDialogFragment : BaseDialogFragment() { } } + private fun createNumericPref(inflater: LayoutInflater, container: ViewGroup, pref: NumericPref) { + inflater.inflate(R.layout.item_with_desc_and_right_value, container, false).apply { + findViewById(R.id.icon).setImageDrawable(uiUtils.getThemedIcon(pref.iconId)) + findViewById(R.id.title).setText(pref.titleId) + findViewById(R.id.description).setText(pref.descriptionId) + val valueView = findViewById(R.id.value) + valueView.text = pref.getCurrentValue() + setOnClickListener { + showPopupMenu(pref, valueView) + } + container.addView(this) + } + } + private fun addItemToContainer(inflater: LayoutInflater, container: ViewGroup, tag: String, title: String) { inflater.inflate(R.layout.item_with_rb_and_btn, container, false).apply { val checked = tag == settings.currentSharingMode @@ -270,7 +279,7 @@ class SettingsDialogFragment : BaseDialogFragment() { } } - private fun showPopupMenu(pref: DurationPref, valueView: TextView) { + private fun showPopupMenu(pref: NumericPref, valueView: TextView) { val menuList = pref.getMenuItems() val ctx = valueView.context ListPopupWindow(ctx).apply { From 3d2c045b3a950e4258ebf7b50fb28065de4bee43 Mon Sep 17 00:00:00 2001 From: Chumva Date: Sun, 26 May 2019 14:19:15 +0300 Subject: [PATCH 2/2] Fix updating lastLocation --- .../src/net/osmand/telegram/helpers/ShareLocationHelper.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShareLocationHelper.kt b/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShareLocationHelper.kt index 21c48be3c5..cc498d49d8 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShareLocationHelper.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShareLocationHelper.kt @@ -68,12 +68,12 @@ class ShareLocationHelper(private val app: TelegramApplication) { if (record) { lastLocationUpdateTime = System.currentTimeMillis() + lastLocation = location if (app.settings.getChatsShareInfo().isNotEmpty()) { shareLocationMessages(location, app.telegramHelper.getCurrentUserId()) } } } - lastLocation = location app.settings.updateSharingStatusHistory() refreshNotification() }