diff --git a/OsmAnd-telegram/src/net/osmand/telegram/TelegramSettings.kt b/OsmAnd-telegram/src/net/osmand/telegram/TelegramSettings.kt index e35962bfc5..f598e8470b 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/TelegramSettings.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/TelegramSettings.kt @@ -36,7 +36,6 @@ private const val SETTINGS_NAME = "osmand_telegram_settings" private const val SHARE_LOCATION_CHATS_KEY = "share_location_chats" private const val HIDDEN_ON_MAP_CHATS_KEY = "hidden_on_map_chats" -private const val ADDED_DEVICES_IDS_KEY = "added_devices_ids" private const val SHARING_MODE_KEY = "current_sharing_mode" private const val METRICS_CONSTANTS_KEY = "metrics_constants" @@ -63,7 +62,7 @@ class TelegramSettings(private val app: TelegramApplication) { private var hiddenOnMapChats: Set = emptySet() var shareDevicesIds = mutableMapOf() - var currentSharingMode: String? = null + var currentSharingMode = "" var metricsConstants = MetricsConstants.KILOMETERS_AND_METERS var speedConstants = SpeedConstants.KILOMETERS_PER_HOUR diff --git a/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShareLocationHelper.kt b/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShareLocationHelper.kt index 778ba887aa..d9d7d602cb 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShareLocationHelper.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShareLocationHelper.kt @@ -42,11 +42,11 @@ class ShareLocationHelper(private val app: TelegramApplication) { val chatLivePeriods = app.settings.getChatLivePeriods() if (chatLivePeriods.isNotEmpty()) { val user = app.telegramHelper.getCurrentUser() - if (user != null && app.settings.currentSharingMode == TelegramUiHelper.getUserName(user)) { + val sharingMode = app.settings.currentSharingMode + if (user != null && sharingMode == TelegramUiHelper.getUserName(user)) { app.telegramHelper.sendLiveLocationMessage(chatLivePeriods, location.latitude, location.longitude) - } else { - val deviceId = app.settings.currentSharingMode - val url = "https://live.osmand.net/device/$deviceId/send?lat=${location.latitude}&lon=${location.longitude}" + } else if (sharingMode.isNotEmpty()) { + val url = "https://live.osmand.net/device/$sharingMode/send?lat=${location.latitude}&lon=${location.longitude}" AndroidNetworkUtils.sendRequestAsync(url, null) } } diff --git a/OsmAnd-telegram/src/net/osmand/telegram/ui/MainActivity.kt b/OsmAnd-telegram/src/net/osmand/telegram/ui/MainActivity.kt index 96f80958de..6f43fff86b 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/ui/MainActivity.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/ui/MainActivity.kt @@ -19,21 +19,14 @@ import android.widget.* import net.osmand.PlatformUtil import net.osmand.telegram.R import net.osmand.telegram.TelegramApplication -import net.osmand.telegram.TelegramSettings import net.osmand.telegram.helpers.OsmandAidlHelper import net.osmand.telegram.helpers.TelegramHelper import net.osmand.telegram.helpers.TelegramHelper.* import net.osmand.telegram.ui.LoginDialogFragment.LoginDialogType import net.osmand.telegram.ui.MyLocationTabFragment.ActionButtonsListener import net.osmand.telegram.ui.views.LockableViewPager -import net.osmand.telegram.utils.AndroidNetworkUtils -import net.osmand.telegram.utils.AndroidUtils -import net.osmand.telegram.utils.GRAYSCALE_PHOTOS_DIR -import net.osmand.telegram.utils.GRAYSCALE_PHOTOS_EXT +import net.osmand.telegram.utils.* import org.drinkless.td.libcore.telegram.TdApi -import org.json.JSONArray -import org.json.JSONException -import org.json.JSONObject import java.lang.ref.WeakReference const val OPEN_MY_LOCATION_TAB_KEY = "open_my_location_tab" @@ -223,15 +216,7 @@ class MainActivity : AppCompatActivity(), TelegramListener, ActionButtonsListene } val user = telegramHelper.getCurrentUser() if (user != null) { - AndroidNetworkUtils.sendRequestAsync( - "https://osmand.net/device/send-devices?uid=${user.id}", - object : AndroidNetworkUtils.OnRequestResultListener { - override fun onResult(result: String) { - val list = parseJsonContents(result) - settings.updateShareDevicesIds(list) - } - } - ) + OsmandApiUtils.updateSharingDevices(app, user.id) } } else -> Unit @@ -242,30 +227,6 @@ class MainActivity : AppCompatActivity(), TelegramListener, ActionButtonsListene } } } - - fun parseJsonContents(contentsJson: String): List { - val list = mutableListOf() - val jArray: JSONArray - val reader: JSONObject - try { - reader = JSONObject(contentsJson) - jArray = reader.getJSONArray("devices") - for (i in 0 until jArray.length()) { - val deviceJSON: JSONObject = jArray.getJSONObject(i) - val deviceBot = TelegramSettings.DeviceBot() - deviceBot.id = deviceJSON.getLong("id") - deviceBot.userId = deviceJSON.getLong("userId") - deviceBot.chatId = deviceJSON.getLong("chatId") - deviceBot.deviceName = deviceJSON.getString("deviceName") - deviceBot.externalId = deviceJSON.getString("externalId") - deviceBot.data = deviceJSON.getString("data") - list.add(deviceBot) - } - } catch (e: JSONException) { - - } - return list - } override fun onTelegramChatsRead() { runOnUi { diff --git a/OsmAnd-telegram/src/net/osmand/telegram/ui/SettingsDialogFragment.kt b/OsmAnd-telegram/src/net/osmand/telegram/ui/SettingsDialogFragment.kt index 7f4b7c532f..628aa3c6e4 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/ui/SettingsDialogFragment.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/ui/SettingsDialogFragment.kt @@ -19,14 +19,13 @@ import net.osmand.telegram.utils.AndroidUtils class SettingsDialogFragment : BaseDialogFragment() { private val uiUtils get() = app.uiUtils - private lateinit var mainView: View override fun onCreateView( inflater: LayoutInflater, parent: ViewGroup?, savedInstanceState: Bundle? ): View { - mainView = inflater.inflate(R.layout.fragement_settings_dialog, parent) + val mainView = inflater.inflate(R.layout.fragement_settings_dialog, parent) val appBarLayout = mainView.findViewById(R.id.app_bar_layout) AndroidUtils.addStatusBarPadding19v(context!!, appBarLayout) @@ -96,8 +95,22 @@ class SettingsDialogFragment : BaseDialogFragment() { val user = telegramHelper.getCurrentUser() if (user != null) { val name = TelegramUiHelper.getUserName(user) - if (!settings.shareDevicesIds.containsKey(name)) { - settings.shareDevicesIds[name] = name + inflater.inflate(R.layout.item_with_rb_and_btn, container, false).apply { + findViewById(R.id.title).text = name + findViewById(R.id.primary_btn).visibility = View.GONE + if (settings.currentSharingMode.isEmpty()) { + settings.currentSharingMode = name + } + findViewById(R.id.radio_button).apply { + visibility = View.VISIBLE + isChecked = name == settings.currentSharingMode + } + setOnClickListener { + settings.currentSharingMode = name + updateSelectedSharingMode() + } + tag = name + container.addView(this) } } settings.shareDevicesIds.forEach { diff --git a/OsmAnd-telegram/src/net/osmand/telegram/utils/OsmandApiUtils.kt b/OsmAnd-telegram/src/net/osmand/telegram/utils/OsmandApiUtils.kt new file mode 100644 index 0000000000..36ec57c642 --- /dev/null +++ b/OsmAnd-telegram/src/net/osmand/telegram/utils/OsmandApiUtils.kt @@ -0,0 +1,43 @@ +package net.osmand.telegram.utils + +import net.osmand.telegram.TelegramApplication +import net.osmand.telegram.TelegramSettings +import org.json.JSONException +import org.json.JSONObject + +object OsmandApiUtils { + + fun updateSharingDevices(app: TelegramApplication, userId: Int) { + AndroidNetworkUtils.sendRequestAsync( + "https://osmand.net/device/send-devices?uid=$userId", + object : AndroidNetworkUtils.OnRequestResultListener { + override fun onResult(result: String) { + val list = parseJsonContents(result) + app.settings.updateShareDevicesIds(list) + } + } + ) + } + + fun parseJsonContents(contentsJson: String): List { + val list = mutableListOf() + try { + val jArray = JSONObject(contentsJson).getJSONArray("devices") + for (i in 0 until jArray.length()) { + val deviceJSON = jArray.getJSONObject(i) + val deviceBot = TelegramSettings.DeviceBot().apply { + id = deviceJSON.getLong("id") + userId = deviceJSON.getLong("userId") + chatId = deviceJSON.getLong("chatId") + deviceName = deviceJSON.getString("deviceName") + externalId = deviceJSON.getString("externalId") + data = deviceJSON.getString("data") + } + list.add(deviceBot) + } + } catch (e: JSONException) { + + } + return list + } +} \ No newline at end of file