From 5540b2e31b629b3e29b2bdb5c19af3747ce05455 Mon Sep 17 00:00:00 2001 From: Chumva Date: Fri, 28 Sep 2018 16:44:47 +0300 Subject: [PATCH 01/14] Add setting to share location from different devices to url --- .../res/layout/bottom_sheet_add_device.xml | 111 ++++++++++++++++++ .../res/layout/fragement_settings_dialog.xml | 65 ++++++++++ OsmAnd-telegram/res/values/strings.xml | 6 + .../net/osmand/telegram/TelegramSettings.kt | 29 +++++ .../telegram/helpers/ShareLocationHelper.kt | 11 +- .../osmand/telegram/helpers/TelegramHelper.kt | 18 +-- .../telegram/ui/AddDeviceBottomSheet.kt | 91 ++++++++++++++ .../telegram/ui/SettingsDialogFragment.kt | 76 +++++++++++- .../telegram/utils/AndroidNetworkUtils.kt | 51 ++++++++ 9 files changed, 443 insertions(+), 15 deletions(-) create mode 100644 OsmAnd-telegram/res/layout/bottom_sheet_add_device.xml create mode 100644 OsmAnd-telegram/src/net/osmand/telegram/ui/AddDeviceBottomSheet.kt create mode 100644 OsmAnd-telegram/src/net/osmand/telegram/utils/AndroidNetworkUtils.kt diff --git a/OsmAnd-telegram/res/layout/bottom_sheet_add_device.xml b/OsmAnd-telegram/res/layout/bottom_sheet_add_device.xml new file mode 100644 index 0000000000..c78ee9305c --- /dev/null +++ b/OsmAnd-telegram/res/layout/bottom_sheet_add_device.xml @@ -0,0 +1,111 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/OsmAnd-telegram/res/layout/fragement_settings_dialog.xml b/OsmAnd-telegram/res/layout/fragement_settings_dialog.xml index a09cfd8b2f..54e9546175 100644 --- a/OsmAnd-telegram/res/layout/fragement_settings_dialog.xml +++ b/OsmAnd-telegram/res/layout/fragement_settings_dialog.xml @@ -74,6 +74,71 @@ + + + + + + + + + + + + + + + + + Save + Enter your device id that you can find at https://live.osmand.net/device/ID + Device id + Add device + The option allow you to share this device location, or location of custom added via API devices. + Share location as Contacts and groups who share their location with you. Are you sure you want to log out of OsmAnd Telegram? After that, you will not be able to send your position and see the locations of your contacts on the map in OsmAnd. Logout from OsmAnd Telegram? diff --git a/OsmAnd-telegram/src/net/osmand/telegram/TelegramSettings.kt b/OsmAnd-telegram/src/net/osmand/telegram/TelegramSettings.kt index 0753799ed3..60d7e1b38b 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/TelegramSettings.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/TelegramSettings.kt @@ -36,6 +36,9 @@ 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" private const val SPEED_CONSTANTS_KEY = "speed_constants" @@ -59,6 +62,9 @@ class TelegramSettings(private val app: TelegramApplication) { private var shareLocationChats: Set = emptySet() private var hiddenOnMapChats: Set = emptySet() + var shareDevicesIds: Set = emptySet() + var currentSharingMode = "" + var metricsConstants = MetricsConstants.KILOMETERS_AND_METERS var speedConstants = SpeedConstants.KILOMETERS_PER_HOUR @@ -167,6 +173,12 @@ class TelegramSettings(private val app: TelegramApplication) { hiddenOnMapChats = hiddenChats.toHashSet() } + fun addSharingDevice(deviceId: String) { + val shareDevices = shareDevicesIds.toMutableList() + shareDevices.add(deviceId) + shareDevicesIds = shareDevices.toHashSet() + } + fun getShareLocationChats() = ArrayList(shareLocationChats) fun getShowOnMapChats() = getLiveNowChats().minus(hiddenOnMapChats) @@ -202,6 +214,14 @@ class TelegramSettings(private val app: TelegramApplication) { } edit.putStringSet(HIDDEN_ON_MAP_CHATS_KEY, hiddenChatsSet) + val shareDevicesSet = mutableSetOf() + val shareDevices = ArrayList(shareDevicesIds) + for (device in shareDevices) { + shareDevicesSet.add(device) + } + edit.putStringSet(ADDED_DEVICES_IDS_KEY, shareDevicesSet) + edit.putString(SHARING_MODE_KEY, currentSharingMode) + edit.putString(METRICS_CONSTANTS_KEY, metricsConstants.name) edit.putString(SPEED_CONSTANTS_KEY, speedConstants.name) @@ -233,6 +253,13 @@ class TelegramSettings(private val app: TelegramApplication) { } hiddenOnMapChats = hiddenChats + val shareDevices = mutableSetOf() + val shareDevicesSet = prefs.getStringSet(ADDED_DEVICES_IDS_KEY, mutableSetOf()) + for (deviceId in shareDevicesSet) { + shareDevices.add(deviceId.toString()) + } + shareDevicesIds = shareDevices + metricsConstants = MetricsConstants.valueOf( prefs.getString(METRICS_CONSTANTS_KEY, MetricsConstants.KILOMETERS_AND_METERS.name) ) @@ -247,6 +274,8 @@ class TelegramSettings(private val app: TelegramApplication) { val locHistoryDef = LOC_HISTORY_VALUES_SEC[LOC_HISTORY_DEFAULT_INDEX] locHistoryTime = prefs.getLong(LOC_HISTORY_TIME_KEY, locHistoryDef) + currentSharingMode = prefs.getString(SHARING_MODE_KEY, "") + appToConnectPackage = prefs.getString(APP_TO_CONNECT_PACKAGE_KEY, "") liveNowSortType = LiveNowSortType.valueOf( diff --git a/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShareLocationHelper.kt b/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShareLocationHelper.kt index 0db88fa2a7..5eeaf15442 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShareLocationHelper.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShareLocationHelper.kt @@ -3,6 +3,7 @@ package net.osmand.telegram.helpers import net.osmand.Location import net.osmand.telegram.TelegramApplication import net.osmand.telegram.notifications.TelegramNotification.NotificationType +import net.osmand.telegram.utils.AndroidNetworkUtils class ShareLocationHelper(private val app: TelegramApplication) { @@ -40,7 +41,15 @@ class ShareLocationHelper(private val app: TelegramApplication) { if (location != null && app.isInternetConnectionAvailable) { val chatLivePeriods = app.settings.getChatLivePeriods() if (chatLivePeriods.isNotEmpty()) { - app.telegramHelper.sendLiveLocationMessage(chatLivePeriods, location.latitude, location.longitude) + if (app.settings.currentSharingMode == TelegramUiHelper.getUserName(app.telegramHelper.getCurrentUser()!!)) { + app.telegramHelper.sendLiveLocationMessage(chatLivePeriods, location.latitude, location.longitude) + } else { + chatLivePeriods.forEach { (chatId, livePeriod) -> + val deviceId = app.settings.currentSharingMode + val url = "https://live.osmand.net/device/$deviceId/send?lat=${location.latitude}&lon=${location.longitude}" + AndroidNetworkUtils.sendRequestAsync(app, url) + } + } } lastLocationMessageSentTime = System.currentTimeMillis() } diff --git a/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramHelper.kt b/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramHelper.kt index ca6f7c717f..8ddeaf0076 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramHelper.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramHelper.kt @@ -1203,15 +1203,15 @@ class TelegramHelper private constructor() { //listener?.onTelegramChatsChanged() } } - TdApi.UpdateChatNotificationSettings.CONSTRUCTOR -> { - val update = obj as TdApi.UpdateChatNotificationSettings - val chat = chats[update.chatId] - if (chat != null) { - synchronized(chat) { - chat.notificationSettings = update.notificationSettings - } - } - } +// TdApi.UpdateChatNotificationSettings.CONSTRUCTOR -> { +// val update = obj as TdApi.UpdateChatNotificationSettings +// val chat = chats[update.chatId] +// if (chat != null) { +// synchronized(chat) { +// chat.notificationSettings = update.notificationSettings +// } +// } +// } TdApi.UpdateFile.CONSTRUCTOR -> { val updateFile = obj as TdApi.UpdateFile diff --git a/OsmAnd-telegram/src/net/osmand/telegram/ui/AddDeviceBottomSheet.kt b/OsmAnd-telegram/src/net/osmand/telegram/ui/AddDeviceBottomSheet.kt new file mode 100644 index 0000000000..6a71d85d95 --- /dev/null +++ b/OsmAnd-telegram/src/net/osmand/telegram/ui/AddDeviceBottomSheet.kt @@ -0,0 +1,91 @@ +package net.osmand.telegram.ui + +import android.content.Intent +import android.os.Bundle +import android.support.design.widget.BottomSheetBehavior +import android.support.v4.app.DialogFragment +import android.support.v4.app.Fragment +import android.support.v4.app.FragmentManager +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import android.widget.EditText +import android.widget.TextView +import net.osmand.telegram.R +import net.osmand.telegram.TelegramApplication +import net.osmand.telegram.ui.views.BottomSheetDialog + + +class AddDeviceBottomSheet : DialogFragment() { + private lateinit var editText: EditText + + override fun onCreateDialog(savedInstanceState: Bundle?) = BottomSheetDialog(context!!) + + override fun onCreateView( + inflater: LayoutInflater, + container: ViewGroup?, + savedInstanceState: Bundle? + ): View? { + val mainView = inflater.inflate(R.layout.bottom_sheet_add_device, container, false) + + mainView.findViewById(R.id.scroll_view_container).setOnClickListener { dismiss() } + + BottomSheetBehavior.from(mainView.findViewById(R.id.scroll_view)) + .setBottomSheetCallback(object : BottomSheetBehavior.BottomSheetCallback() { + + override fun onStateChanged(bottomSheet: View, newState: Int) { + if (newState == BottomSheetBehavior.STATE_HIDDEN) { + dismiss() + } + } + + override fun onSlide(bottomSheet: View, slideOffset: Float) {} + }) + + editText = mainView.findViewById(R.id.device_id_edit_text) + + mainView.findViewById(R.id.secondary_btn).apply { + setText(R.string.shared_string_cancel) + setOnClickListener { dismiss() } + } + + mainView.findViewById(R.id.primary_btn).apply { + setText(R.string.shared_string_save) + setOnClickListener { + val app = activity?.application as TelegramApplication + val settings = app.settings + val newDeviceId = editText.text.toString() + settings.addSharingDevice(newDeviceId) + targetFragment?.also { target -> + val intent = Intent() + intent.putExtra(NEW_DEVICE_ID, newDeviceId) + target.onActivityResult(targetRequestCode, ADD_DEVICE_REQUEST_CODE, intent) + } + dismiss() + } + } + + + return mainView + } + + companion object { + + const val ADD_DEVICE_REQUEST_CODE = 5 + const val NEW_DEVICE_ID = "NEW_DEVICE_ID" + + private const val TAG = "AddDeviceBottomSheet" + + fun showInstance(fm: FragmentManager, target: Fragment): Boolean { + return try { + AddDeviceBottomSheet().apply { + setTargetFragment(target, ADD_DEVICE_REQUEST_CODE) + show(fm, TAG) + } + true + } catch (e: RuntimeException) { + false + } + } + } +} \ No newline at end of file diff --git a/OsmAnd-telegram/src/net/osmand/telegram/ui/SettingsDialogFragment.kt b/OsmAnd-telegram/src/net/osmand/telegram/ui/SettingsDialogFragment.kt index 98996fbadf..e94887d072 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/ui/SettingsDialogFragment.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/ui/SettingsDialogFragment.kt @@ -9,10 +9,7 @@ import android.view.Gravity import android.view.LayoutInflater import android.view.View import android.view.ViewGroup -import android.widget.ArrayAdapter -import android.widget.ImageView -import android.widget.RadioButton -import android.widget.TextView +import android.widget.* import net.osmand.telegram.R import net.osmand.telegram.TelegramSettings import net.osmand.telegram.TelegramSettings.DurationPref @@ -22,13 +19,14 @@ 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 { - val mainView = inflater.inflate(R.layout.fragement_settings_dialog, parent) + mainView = inflater.inflate(R.layout.fragement_settings_dialog, parent) val appBarLayout = mainView.findViewById(R.id.app_bar_layout) AndroidUtils.addStatusBarPadding19v(context!!, appBarLayout) @@ -94,6 +92,39 @@ class SettingsDialogFragment : BaseDialogFragment() { } updateSelectedAppConn() + container = mainView.findViewById(R.id.share_as_container) + if (settings.shareDevicesIds.isEmpty()) { + val user = telegramHelper.getCurrentUser() + if (user != null) { + settings.addSharingDevice(TelegramUiHelper.getUserName(user)) + settings.currentSharingMode = TelegramUiHelper.getUserName(user) + } + } + settings.shareDevicesIds.forEach { + val title = it + inflater.inflate(R.layout.item_with_rb_and_btn, container, false).apply { + findViewById(R.id.title).text = title + findViewById(R.id.primary_btn).visibility = View.GONE + findViewById(R.id.radio_button).apply { + visibility = View.VISIBLE + isChecked = it == settings.currentSharingMode + } + setOnClickListener { + settings.currentSharingMode = title + updateSelectedSharingMode() + } + tag = it + container.addView(this) + } + } + + mainView.findViewById(R.id.add_device).setOnClickListener { + val fm = fragmentManager + if (fm != null) { + AddDeviceBottomSheet.showInstance(fm, this) + } + } + val user = telegramHelper.getCurrentUser() if (user != null) { TelegramUiHelper.setupPhoto( @@ -131,6 +162,11 @@ class SettingsDialogFragment : BaseDialogFragment() { logoutTelegram() dismiss() } + AddDeviceBottomSheet.ADD_DEVICE_REQUEST_CODE -> { + if (data != null && data.hasExtra(AddDeviceBottomSheet.NEW_DEVICE_ID)) { + addNewSharingDevice(data.getStringExtra(AddDeviceBottomSheet.NEW_DEVICE_ID)) + } + } } } @@ -164,6 +200,36 @@ class SettingsDialogFragment : BaseDialogFragment() { } } + private fun updateSelectedSharingMode() { + view?.findViewById(R.id.share_as_container)?.apply { + for (i in 0 until childCount) { + getChildAt(i).apply { + findViewById(R.id.radio_button).isChecked = + tag == settings.currentSharingMode + } + } + } + } + + private fun addNewSharingDevice(title: String) { + val inflater = LayoutInflater.from(context) + val container = mainView.findViewById(R.id.share_as_container) + inflater.inflate(R.layout.item_with_rb_and_btn, null, false).apply { + findViewById(R.id.title).text = title + findViewById(R.id.primary_btn).visibility = View.GONE + findViewById(R.id.radio_button).apply { + visibility = View.VISIBLE + isChecked = title == settings.currentSharingMode + } + setOnClickListener { + settings.currentSharingMode = title + updateSelectedSharingMode() + } + tag = title + container.addView(this) + } + } + private fun logoutTelegram() { val act = activity ?: return (act as MainActivity).logoutTelegram() diff --git a/OsmAnd-telegram/src/net/osmand/telegram/utils/AndroidNetworkUtils.kt b/OsmAnd-telegram/src/net/osmand/telegram/utils/AndroidNetworkUtils.kt new file mode 100644 index 0000000000..6fe66f0d39 --- /dev/null +++ b/OsmAnd-telegram/src/net/osmand/telegram/utils/AndroidNetworkUtils.kt @@ -0,0 +1,51 @@ +package net.osmand.telegram.utils + +import android.os.AsyncTask +import android.widget.Toast +import net.osmand.PlatformUtil +import net.osmand.osm.io.NetworkUtils +import net.osmand.telegram.TelegramApplication +import java.net.HttpURLConnection + +object AndroidNetworkUtils { + + private const val CONNECTION_TIMEOUT = 15000 + private val log = PlatformUtil.getLog(AndroidNetworkUtils::class.java) + + fun sendRequestAsync(ctx: TelegramApplication, url: String) { + + object : AsyncTask() { + override fun doInBackground(vararg params: Void): String? { + try { + return sendRequest(ctx, url) + } catch (e: Exception) { + return null + } + + } + }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, null as Void?) + } + + fun sendRequest(ctx: TelegramApplication, url: String): String? { + var connection: HttpURLConnection? = null + try { + connection = NetworkUtils.getHttpURLConnection(url) + if (connection != null) { + connection.setRequestProperty("Accept-Charset", "UTF-8") + connection.setRequestProperty("User-Agent", ctx.packageName) + connection.connectTimeout = CONNECTION_TIMEOUT + connection.requestMethod = "GET" + connection.connect() + if (connection.responseCode != HttpURLConnection.HTTP_OK) { + Toast.makeText(ctx, connection.responseMessage, Toast.LENGTH_LONG).show() + } + } + } catch (e: Exception) { + log.error(e) + } finally { + connection?.disconnect() + } + + return null + } +} From 41271f87e82ac11791d0c09a37be5674b3672be5 Mon Sep 17 00:00:00 2001 From: Vitaliy Date: Fri, 28 Sep 2018 16:51:39 +0300 Subject: [PATCH 02/14] remove unnecessary changes --- .../osmand/telegram/helpers/TelegramHelper.kt | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramHelper.kt b/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramHelper.kt index 8ddeaf0076..ca6f7c717f 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramHelper.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramHelper.kt @@ -1203,15 +1203,15 @@ class TelegramHelper private constructor() { //listener?.onTelegramChatsChanged() } } -// TdApi.UpdateChatNotificationSettings.CONSTRUCTOR -> { -// val update = obj as TdApi.UpdateChatNotificationSettings -// val chat = chats[update.chatId] -// if (chat != null) { -// synchronized(chat) { -// chat.notificationSettings = update.notificationSettings -// } -// } -// } + TdApi.UpdateChatNotificationSettings.CONSTRUCTOR -> { + val update = obj as TdApi.UpdateChatNotificationSettings + val chat = chats[update.chatId] + if (chat != null) { + synchronized(chat) { + chat.notificationSettings = update.notificationSettings + } + } + } TdApi.UpdateFile.CONSTRUCTOR -> { val updateFile = obj as TdApi.UpdateFile From 3b1e1d19a0dd07ccd9c6ef9aa73f26e1293ae217 Mon Sep 17 00:00:00 2001 From: Vitaliy Date: Fri, 28 Sep 2018 17:01:11 +0300 Subject: [PATCH 03/14] change check for currentSharingMode --- .../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 5eeaf15442..a205da6d04 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShareLocationHelper.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShareLocationHelper.kt @@ -41,7 +41,7 @@ class ShareLocationHelper(private val app: TelegramApplication) { if (location != null && app.isInternetConnectionAvailable) { val chatLivePeriods = app.settings.getChatLivePeriods() if (chatLivePeriods.isNotEmpty()) { - if (app.settings.currentSharingMode == TelegramUiHelper.getUserName(app.telegramHelper.getCurrentUser()!!)) { + if (app.settings.currentSharingMode == null) { app.telegramHelper.sendLiveLocationMessage(chatLivePeriods, location.latitude, location.longitude) } else { chatLivePeriods.forEach { (chatId, livePeriod) -> From 9169766dbb5200cdeb213e31288723144a96fad6 Mon Sep 17 00:00:00 2001 From: Vitaliy Date: Fri, 28 Sep 2018 17:05:26 +0300 Subject: [PATCH 04/14] Make currentSharingMode nullable --- OsmAnd-telegram/src/net/osmand/telegram/TelegramSettings.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/OsmAnd-telegram/src/net/osmand/telegram/TelegramSettings.kt b/OsmAnd-telegram/src/net/osmand/telegram/TelegramSettings.kt index 60d7e1b38b..add0a73db8 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/TelegramSettings.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/TelegramSettings.kt @@ -63,7 +63,7 @@ class TelegramSettings(private val app: TelegramApplication) { private var hiddenOnMapChats: Set = emptySet() var shareDevicesIds: Set = emptySet() - var currentSharingMode = "" + var currentSharingMode: String? = null var metricsConstants = MetricsConstants.KILOMETERS_AND_METERS var speedConstants = SpeedConstants.KILOMETERS_PER_HOUR From c71386e19a38468230d778cb92646ee1ae04c244 Mon Sep 17 00:00:00 2001 From: Chumva Date: Fri, 28 Sep 2018 18:13:27 +0300 Subject: [PATCH 05/14] remove default currentSharingMode --- .../net/osmand/telegram/ui/SettingsDialogFragment.kt | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/OsmAnd-telegram/src/net/osmand/telegram/ui/SettingsDialogFragment.kt b/OsmAnd-telegram/src/net/osmand/telegram/ui/SettingsDialogFragment.kt index e94887d072..131ac031b4 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/ui/SettingsDialogFragment.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/ui/SettingsDialogFragment.kt @@ -97,9 +97,10 @@ class SettingsDialogFragment : BaseDialogFragment() { val user = telegramHelper.getCurrentUser() if (user != null) { settings.addSharingDevice(TelegramUiHelper.getUserName(user)) - settings.currentSharingMode = TelegramUiHelper.getUserName(user) +// settings.currentSharingMode = TelegramUiHelper.getUserName(user) } } + val user = telegramHelper.getCurrentUser() settings.shareDevicesIds.forEach { val title = it inflater.inflate(R.layout.item_with_rb_and_btn, container, false).apply { @@ -110,7 +111,11 @@ class SettingsDialogFragment : BaseDialogFragment() { isChecked = it == settings.currentSharingMode } setOnClickListener { - settings.currentSharingMode = title + if (user != null && TelegramUiHelper.getUserName(user) == title) { + settings.currentSharingMode = null + } else { + settings.currentSharingMode = title + } updateSelectedSharingMode() } tag = it @@ -125,7 +130,6 @@ class SettingsDialogFragment : BaseDialogFragment() { } } - val user = telegramHelper.getCurrentUser() if (user != null) { TelegramUiHelper.setupPhoto( app, From ac844ab537a5edfaa37984dde4de99a3b26634c3 Mon Sep 17 00:00:00 2001 From: Chumva Date: Fri, 28 Sep 2018 18:38:09 +0300 Subject: [PATCH 06/14] remove unnecessary changes --- .../src/net/osmand/telegram/ui/SettingsDialogFragment.kt | 1 - 1 file changed, 1 deletion(-) diff --git a/OsmAnd-telegram/src/net/osmand/telegram/ui/SettingsDialogFragment.kt b/OsmAnd-telegram/src/net/osmand/telegram/ui/SettingsDialogFragment.kt index 131ac031b4..7e7db595e3 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/ui/SettingsDialogFragment.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/ui/SettingsDialogFragment.kt @@ -97,7 +97,6 @@ class SettingsDialogFragment : BaseDialogFragment() { val user = telegramHelper.getCurrentUser() if (user != null) { settings.addSharingDevice(TelegramUiHelper.getUserName(user)) -// settings.currentSharingMode = TelegramUiHelper.getUserName(user) } } val user = telegramHelper.getCurrentUser() From 06e80d514db6b80f0e3fb346e3ad0036e349f89a Mon Sep 17 00:00:00 2001 From: Chumva Date: Tue, 2 Oct 2018 13:01:26 +0300 Subject: [PATCH 07/14] Get devices ids from url --- .../net/osmand/telegram/TelegramSettings.kt | 36 +++--- .../telegram/helpers/ShareLocationHelper.kt | 2 +- .../net/osmand/telegram/ui/MainActivity.kt | 43 ++++++- .../telegram/ui/SettingsDialogFragment.kt | 14 +-- .../telegram/utils/AndroidNetworkUtils.kt | 109 ++++++++++++------ 5 files changed, 138 insertions(+), 66 deletions(-) diff --git a/OsmAnd-telegram/src/net/osmand/telegram/TelegramSettings.kt b/OsmAnd-telegram/src/net/osmand/telegram/TelegramSettings.kt index add0a73db8..ea195d8941 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/TelegramSettings.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/TelegramSettings.kt @@ -62,9 +62,9 @@ class TelegramSettings(private val app: TelegramApplication) { private var shareLocationChats: Set = emptySet() private var hiddenOnMapChats: Set = emptySet() - var shareDevicesIds: Set = emptySet() + var shareDevicesIds = mutableMapOf() var currentSharingMode: String? = null - + var metricsConstants = MetricsConstants.KILOMETERS_AND_METERS var speedConstants = SpeedConstants.KILOMETERS_PER_HOUR @@ -133,6 +133,12 @@ class TelegramSettings(private val app: TelegramApplication) { this.shareLocationChats = shareLocationChats.toHashSet() } + fun updateShareDevicesIds(list: List) { + list.forEach { + shareDevicesIds[it.externalId] = it.deviceName + } + } + fun getChatLivePeriod(chatId: Long) = chatLivePeriods[chatId] fun getChatLivePeriods(): Map { @@ -174,9 +180,7 @@ class TelegramSettings(private val app: TelegramApplication) { } fun addSharingDevice(deviceId: String) { - val shareDevices = shareDevicesIds.toMutableList() - shareDevices.add(deviceId) - shareDevicesIds = shareDevices.toHashSet() + shareDevicesIds[deviceId] = deviceId } fun getShareLocationChats() = ArrayList(shareLocationChats) @@ -214,12 +218,6 @@ class TelegramSettings(private val app: TelegramApplication) { } edit.putStringSet(HIDDEN_ON_MAP_CHATS_KEY, hiddenChatsSet) - val shareDevicesSet = mutableSetOf() - val shareDevices = ArrayList(shareDevicesIds) - for (device in shareDevices) { - shareDevicesSet.add(device) - } - edit.putStringSet(ADDED_DEVICES_IDS_KEY, shareDevicesSet) edit.putString(SHARING_MODE_KEY, currentSharingMode) edit.putString(METRICS_CONSTANTS_KEY, metricsConstants.name) @@ -253,13 +251,6 @@ class TelegramSettings(private val app: TelegramApplication) { } hiddenOnMapChats = hiddenChats - val shareDevices = mutableSetOf() - val shareDevicesSet = prefs.getStringSet(ADDED_DEVICES_IDS_KEY, mutableSetOf()) - for (deviceId in shareDevicesSet) { - shareDevices.add(deviceId.toString()) - } - shareDevicesIds = shareDevices - metricsConstants = MetricsConstants.valueOf( prefs.getString(METRICS_CONSTANTS_KEY, MetricsConstants.KILOMETERS_AND_METERS.name) ) @@ -429,4 +420,13 @@ class TelegramSettings(private val app: TelegramApplication) { fun isSortByGroup() = this == SORT_BY_GROUP } + + class DeviceBot { + var id: Long = -1 + var userId: Long = -1 + var chatId: Long = -1 + var deviceName: String = "" + var externalId: String = "" + var data: String = "" + } } diff --git a/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShareLocationHelper.kt b/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShareLocationHelper.kt index a205da6d04..32570ec234 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShareLocationHelper.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShareLocationHelper.kt @@ -47,7 +47,7 @@ class ShareLocationHelper(private val app: TelegramApplication) { chatLivePeriods.forEach { (chatId, livePeriod) -> val deviceId = app.settings.currentSharingMode val url = "https://live.osmand.net/device/$deviceId/send?lat=${location.latitude}&lon=${location.longitude}" - AndroidNetworkUtils.sendRequestAsync(app, url) + 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 7975803506..96f80958de 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/ui/MainActivity.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/ui/MainActivity.kt @@ -19,16 +19,21 @@ 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 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" @@ -216,7 +221,19 @@ class MainActivity : AppCompatActivity(), TelegramListener, ActionButtonsListene } else { AndroidUtils.requestLocationPermission(this) } - } + 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) + } + } + ) + } + } else -> Unit } @@ -226,6 +243,30 @@ 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 { removeNonexistingChatsFromSettings() diff --git a/OsmAnd-telegram/src/net/osmand/telegram/ui/SettingsDialogFragment.kt b/OsmAnd-telegram/src/net/osmand/telegram/ui/SettingsDialogFragment.kt index 7e7db595e3..4d45cc8be1 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/ui/SettingsDialogFragment.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/ui/SettingsDialogFragment.kt @@ -93,21 +93,15 @@ class SettingsDialogFragment : BaseDialogFragment() { updateSelectedAppConn() container = mainView.findViewById(R.id.share_as_container) - if (settings.shareDevicesIds.isEmpty()) { - val user = telegramHelper.getCurrentUser() - if (user != null) { - settings.addSharingDevice(TelegramUiHelper.getUserName(user)) - } - } val user = telegramHelper.getCurrentUser() settings.shareDevicesIds.forEach { - val title = it + val title = it.key inflater.inflate(R.layout.item_with_rb_and_btn, container, false).apply { - findViewById(R.id.title).text = title + findViewById(R.id.title).text = it.value findViewById(R.id.primary_btn).visibility = View.GONE findViewById(R.id.radio_button).apply { visibility = View.VISIBLE - isChecked = it == settings.currentSharingMode + isChecked = title == settings.currentSharingMode } setOnClickListener { if (user != null && TelegramUiHelper.getUserName(user) == title) { @@ -117,7 +111,7 @@ class SettingsDialogFragment : BaseDialogFragment() { } updateSelectedSharingMode() } - tag = it + tag = title container.addView(this) } } diff --git a/OsmAnd-telegram/src/net/osmand/telegram/utils/AndroidNetworkUtils.kt b/OsmAnd-telegram/src/net/osmand/telegram/utils/AndroidNetworkUtils.kt index 6fe66f0d39..89a78e1c14 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/utils/AndroidNetworkUtils.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/utils/AndroidNetworkUtils.kt @@ -1,51 +1,88 @@ package net.osmand.telegram.utils import android.os.AsyncTask -import android.widget.Toast import net.osmand.PlatformUtil -import net.osmand.osm.io.NetworkUtils -import net.osmand.telegram.TelegramApplication -import java.net.HttpURLConnection +import java.io.* +import java.net.* + object AndroidNetworkUtils { - private const val CONNECTION_TIMEOUT = 15000 private val log = PlatformUtil.getLog(AndroidNetworkUtils::class.java) - fun sendRequestAsync(ctx: TelegramApplication, url: String) { - - object : AsyncTask() { - override fun doInBackground(vararg params: Void): String? { - try { - return sendRequest(ctx, url) - } catch (e: Exception) { - return null - } - - } - }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, null as Void?) + interface OnRequestResultListener { + fun onResult(result: String) } - fun sendRequest(ctx: TelegramApplication, url: String): String? { - var connection: HttpURLConnection? = null - try { - connection = NetworkUtils.getHttpURLConnection(url) - if (connection != null) { - connection.setRequestProperty("Accept-Charset", "UTF-8") - connection.setRequestProperty("User-Agent", ctx.packageName) - connection.connectTimeout = CONNECTION_TIMEOUT - connection.requestMethod = "GET" - connection.connect() - if (connection.responseCode != HttpURLConnection.HTTP_OK) { - Toast.makeText(ctx, connection.responseMessage, Toast.LENGTH_LONG).show() - } + fun sendRequestAsync(urlText: String, listener: OnRequestResultListener?) { + SendRequestTask(urlText, listener).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR) + } + + private class SendRequestTask( + private val urlText: String, + private val listener: OnRequestResultListener? + ) : AsyncTask() { + + override fun doInBackground(vararg params: Void): String? { + return try { + sendRequest(urlText) + } catch (e: Exception) { + null } - } catch (e: Exception) { - log.error(e) - } finally { - connection?.disconnect() } - return null + override fun onPostExecute(response: String?) { + if (response != null) { + listener?.onResult(response) + } + } } -} + + fun sendRequest(urlText: String): String? { + try { + log.info("GET : $urlText") + val conn = getHttpURLConnection(urlText) + conn.doInput = true + conn.doOutput = false + conn.requestMethod = "GET" + conn.setRequestProperty("User-Agent", "OsmAnd Sharing") + log.info("Response code and message : " + conn.responseCode + " " + conn.responseMessage) + if (conn.responseCode != 200) { + return conn.responseMessage + } + val inputStream = conn.inputStream + val responseBody = StringBuilder() + responseBody.setLength(0) + if (inputStream != null) { + val bufferedInput = + BufferedReader(InputStreamReader(inputStream, "UTF-8")) + var s = bufferedInput.readLine() + var first = true + while (s != null) { + if (first) { + first = false + } else { + responseBody.append("\n") + } + responseBody.append(s) + s = bufferedInput.readLine() + } + inputStream.close() + } + return responseBody.toString() + } catch (e: IOException) { + log.error(e.message, e) + return e.message + } + } + + @Throws(MalformedURLException::class, IOException::class) + fun getHttpURLConnection(urlString: String): HttpURLConnection { + return getHttpURLConnection(URL(urlString)) + } + + @Throws(IOException::class) + fun getHttpURLConnection(url: URL): HttpURLConnection { + return url.openConnection() as HttpURLConnection + } +} \ No newline at end of file From a06164057945eea406ce4f1c12a0c1b0759e295e Mon Sep 17 00:00:00 2001 From: Chumva Date: Tue, 2 Oct 2018 18:06:35 +0300 Subject: [PATCH 08/14] Add local device for sharing to telegram and remove unnecessary dialog --- .../res/layout/bottom_sheet_add_device.xml | 111 ------------------ .../net/osmand/telegram/TelegramSettings.kt | 5 +- .../telegram/helpers/ShareLocationHelper.kt | 11 +- .../telegram/ui/AddDeviceBottomSheet.kt | 91 -------------- .../telegram/ui/SettingsDialogFragment.kt | 24 ++-- 5 files changed, 13 insertions(+), 229 deletions(-) delete mode 100644 OsmAnd-telegram/res/layout/bottom_sheet_add_device.xml delete mode 100644 OsmAnd-telegram/src/net/osmand/telegram/ui/AddDeviceBottomSheet.kt diff --git a/OsmAnd-telegram/res/layout/bottom_sheet_add_device.xml b/OsmAnd-telegram/res/layout/bottom_sheet_add_device.xml deleted file mode 100644 index c78ee9305c..0000000000 --- a/OsmAnd-telegram/res/layout/bottom_sheet_add_device.xml +++ /dev/null @@ -1,111 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/OsmAnd-telegram/src/net/osmand/telegram/TelegramSettings.kt b/OsmAnd-telegram/src/net/osmand/telegram/TelegramSettings.kt index ea195d8941..e35962bfc5 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/TelegramSettings.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/TelegramSettings.kt @@ -134,6 +134,7 @@ class TelegramSettings(private val app: TelegramApplication) { } fun updateShareDevicesIds(list: List) { + shareDevicesIds.clear() list.forEach { shareDevicesIds[it.externalId] = it.deviceName } @@ -179,10 +180,6 @@ class TelegramSettings(private val app: TelegramApplication) { hiddenOnMapChats = hiddenChats.toHashSet() } - fun addSharingDevice(deviceId: String) { - shareDevicesIds[deviceId] = deviceId - } - fun getShareLocationChats() = ArrayList(shareLocationChats) fun getShowOnMapChats() = getLiveNowChats().minus(hiddenOnMapChats) diff --git a/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShareLocationHelper.kt b/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShareLocationHelper.kt index 32570ec234..778ba887aa 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShareLocationHelper.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShareLocationHelper.kt @@ -41,14 +41,13 @@ class ShareLocationHelper(private val app: TelegramApplication) { if (location != null && app.isInternetConnectionAvailable) { val chatLivePeriods = app.settings.getChatLivePeriods() if (chatLivePeriods.isNotEmpty()) { - if (app.settings.currentSharingMode == null) { + val user = app.telegramHelper.getCurrentUser() + if (user != null && app.settings.currentSharingMode == TelegramUiHelper.getUserName(user)) { app.telegramHelper.sendLiveLocationMessage(chatLivePeriods, location.latitude, location.longitude) } else { - chatLivePeriods.forEach { (chatId, livePeriod) -> - val deviceId = app.settings.currentSharingMode - val url = "https://live.osmand.net/device/$deviceId/send?lat=${location.latitude}&lon=${location.longitude}" - AndroidNetworkUtils.sendRequestAsync(url, null) - } + val deviceId = app.settings.currentSharingMode + val url = "https://live.osmand.net/device/$deviceId/send?lat=${location.latitude}&lon=${location.longitude}" + AndroidNetworkUtils.sendRequestAsync(url, null) } } lastLocationMessageSentTime = System.currentTimeMillis() diff --git a/OsmAnd-telegram/src/net/osmand/telegram/ui/AddDeviceBottomSheet.kt b/OsmAnd-telegram/src/net/osmand/telegram/ui/AddDeviceBottomSheet.kt deleted file mode 100644 index 6a71d85d95..0000000000 --- a/OsmAnd-telegram/src/net/osmand/telegram/ui/AddDeviceBottomSheet.kt +++ /dev/null @@ -1,91 +0,0 @@ -package net.osmand.telegram.ui - -import android.content.Intent -import android.os.Bundle -import android.support.design.widget.BottomSheetBehavior -import android.support.v4.app.DialogFragment -import android.support.v4.app.Fragment -import android.support.v4.app.FragmentManager -import android.view.LayoutInflater -import android.view.View -import android.view.ViewGroup -import android.widget.EditText -import android.widget.TextView -import net.osmand.telegram.R -import net.osmand.telegram.TelegramApplication -import net.osmand.telegram.ui.views.BottomSheetDialog - - -class AddDeviceBottomSheet : DialogFragment() { - private lateinit var editText: EditText - - override fun onCreateDialog(savedInstanceState: Bundle?) = BottomSheetDialog(context!!) - - override fun onCreateView( - inflater: LayoutInflater, - container: ViewGroup?, - savedInstanceState: Bundle? - ): View? { - val mainView = inflater.inflate(R.layout.bottom_sheet_add_device, container, false) - - mainView.findViewById(R.id.scroll_view_container).setOnClickListener { dismiss() } - - BottomSheetBehavior.from(mainView.findViewById(R.id.scroll_view)) - .setBottomSheetCallback(object : BottomSheetBehavior.BottomSheetCallback() { - - override fun onStateChanged(bottomSheet: View, newState: Int) { - if (newState == BottomSheetBehavior.STATE_HIDDEN) { - dismiss() - } - } - - override fun onSlide(bottomSheet: View, slideOffset: Float) {} - }) - - editText = mainView.findViewById(R.id.device_id_edit_text) - - mainView.findViewById(R.id.secondary_btn).apply { - setText(R.string.shared_string_cancel) - setOnClickListener { dismiss() } - } - - mainView.findViewById(R.id.primary_btn).apply { - setText(R.string.shared_string_save) - setOnClickListener { - val app = activity?.application as TelegramApplication - val settings = app.settings - val newDeviceId = editText.text.toString() - settings.addSharingDevice(newDeviceId) - targetFragment?.also { target -> - val intent = Intent() - intent.putExtra(NEW_DEVICE_ID, newDeviceId) - target.onActivityResult(targetRequestCode, ADD_DEVICE_REQUEST_CODE, intent) - } - dismiss() - } - } - - - return mainView - } - - companion object { - - const val ADD_DEVICE_REQUEST_CODE = 5 - const val NEW_DEVICE_ID = "NEW_DEVICE_ID" - - private const val TAG = "AddDeviceBottomSheet" - - fun showInstance(fm: FragmentManager, target: Fragment): Boolean { - return try { - AddDeviceBottomSheet().apply { - setTargetFragment(target, ADD_DEVICE_REQUEST_CODE) - show(fm, TAG) - } - true - } catch (e: RuntimeException) { - false - } - } - } -} \ No newline at end of file diff --git a/OsmAnd-telegram/src/net/osmand/telegram/ui/SettingsDialogFragment.kt b/OsmAnd-telegram/src/net/osmand/telegram/ui/SettingsDialogFragment.kt index 4d45cc8be1..635a91b234 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/ui/SettingsDialogFragment.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/ui/SettingsDialogFragment.kt @@ -94,6 +94,12 @@ class SettingsDialogFragment : BaseDialogFragment() { container = mainView.findViewById(R.id.share_as_container) val user = telegramHelper.getCurrentUser() + if (user != null) { + val name = TelegramUiHelper.getUserName(user) + if (!settings.shareDevicesIds.containsKey(name)) { + settings.shareDevicesIds[name] = name + } + } settings.shareDevicesIds.forEach { val title = it.key inflater.inflate(R.layout.item_with_rb_and_btn, container, false).apply { @@ -104,11 +110,7 @@ class SettingsDialogFragment : BaseDialogFragment() { isChecked = title == settings.currentSharingMode } setOnClickListener { - if (user != null && TelegramUiHelper.getUserName(user) == title) { - settings.currentSharingMode = null - } else { - settings.currentSharingMode = title - } + settings.currentSharingMode = title updateSelectedSharingMode() } tag = title @@ -116,13 +118,6 @@ class SettingsDialogFragment : BaseDialogFragment() { } } - mainView.findViewById(R.id.add_device).setOnClickListener { - val fm = fragmentManager - if (fm != null) { - AddDeviceBottomSheet.showInstance(fm, this) - } - } - if (user != null) { TelegramUiHelper.setupPhoto( app, @@ -159,11 +154,6 @@ class SettingsDialogFragment : BaseDialogFragment() { logoutTelegram() dismiss() } - AddDeviceBottomSheet.ADD_DEVICE_REQUEST_CODE -> { - if (data != null && data.hasExtra(AddDeviceBottomSheet.NEW_DEVICE_ID)) { - addNewSharingDevice(data.getStringExtra(AddDeviceBottomSheet.NEW_DEVICE_ID)) - } - } } } From c7cf2be2aa59c4982fcc9fcb7220d49d1ce1df1a Mon Sep 17 00:00:00 2001 From: Chumva Date: Tue, 2 Oct 2018 18:07:40 +0300 Subject: [PATCH 09/14] Remove unused view --- .../res/layout/fragement_settings_dialog.xml | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/OsmAnd-telegram/res/layout/fragement_settings_dialog.xml b/OsmAnd-telegram/res/layout/fragement_settings_dialog.xml index 54e9546175..7d5f238f99 100644 --- a/OsmAnd-telegram/res/layout/fragement_settings_dialog.xml +++ b/OsmAnd-telegram/res/layout/fragement_settings_dialog.xml @@ -120,21 +120,6 @@ android:layout_height="wrap_content" android:orientation="vertical" /> - - From 66db4433402c130704588d75341d73918a1fdd47 Mon Sep 17 00:00:00 2001 From: Chumva Date: Tue, 2 Oct 2018 18:11:01 +0300 Subject: [PATCH 10/14] Remove unnecessary method --- .../telegram/ui/SettingsDialogFragment.kt | 19 ------------------- 1 file changed, 19 deletions(-) diff --git a/OsmAnd-telegram/src/net/osmand/telegram/ui/SettingsDialogFragment.kt b/OsmAnd-telegram/src/net/osmand/telegram/ui/SettingsDialogFragment.kt index 635a91b234..7f4b7c532f 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/ui/SettingsDialogFragment.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/ui/SettingsDialogFragment.kt @@ -198,25 +198,6 @@ class SettingsDialogFragment : BaseDialogFragment() { } } - private fun addNewSharingDevice(title: String) { - val inflater = LayoutInflater.from(context) - val container = mainView.findViewById(R.id.share_as_container) - inflater.inflate(R.layout.item_with_rb_and_btn, null, false).apply { - findViewById(R.id.title).text = title - findViewById(R.id.primary_btn).visibility = View.GONE - findViewById(R.id.radio_button).apply { - visibility = View.VISIBLE - isChecked = title == settings.currentSharingMode - } - setOnClickListener { - settings.currentSharingMode = title - updateSelectedSharingMode() - } - tag = title - container.addView(this) - } - } - private fun logoutTelegram() { val act = activity ?: return (act as MainActivity).logoutTelegram() From 7e87e45b3ae6538c12e22a8d1e03efff8ae29df9 Mon Sep 17 00:00:00 2001 From: Chumva Date: Wed, 3 Oct 2018 12:25:42 +0300 Subject: [PATCH 11/14] Add OsmandApiUtils --- .../net/osmand/telegram/TelegramSettings.kt | 3 +- .../telegram/helpers/ShareLocationHelper.kt | 8 ++-- .../net/osmand/telegram/ui/MainActivity.kt | 43 +------------------ .../telegram/ui/SettingsDialogFragment.kt | 21 +++++++-- .../osmand/telegram/utils/OsmandApiUtils.kt | 43 +++++++++++++++++++ 5 files changed, 67 insertions(+), 51 deletions(-) create mode 100644 OsmAnd-telegram/src/net/osmand/telegram/utils/OsmandApiUtils.kt 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 From c15922d08b61d27ff75466b4889c05f333cb7c21 Mon Sep 17 00:00:00 2001 From: Chumva Date: Wed, 3 Oct 2018 12:50:15 +0300 Subject: [PATCH 12/14] Add addItemToContainer --- .../net/osmand/telegram/ui/MainActivity.kt | 6 ++- .../telegram/ui/SettingsDialogFragment.kt | 51 +++++++------------ 2 files changed, 24 insertions(+), 33 deletions(-) diff --git a/OsmAnd-telegram/src/net/osmand/telegram/ui/MainActivity.kt b/OsmAnd-telegram/src/net/osmand/telegram/ui/MainActivity.kt index 6f43fff86b..ab8bbb4147 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/ui/MainActivity.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/ui/MainActivity.kt @@ -22,6 +22,7 @@ import net.osmand.telegram.TelegramApplication import net.osmand.telegram.helpers.OsmandAidlHelper import net.osmand.telegram.helpers.TelegramHelper import net.osmand.telegram.helpers.TelegramHelper.* +import net.osmand.telegram.helpers.TelegramUiHelper import net.osmand.telegram.ui.LoginDialogFragment.LoginDialogType import net.osmand.telegram.ui.MyLocationTabFragment.ActionButtonsListener import net.osmand.telegram.ui.views.LockableViewPager @@ -217,6 +218,9 @@ class MainActivity : AppCompatActivity(), TelegramListener, ActionButtonsListene val user = telegramHelper.getCurrentUser() if (user != null) { OsmandApiUtils.updateSharingDevices(app, user.id) + if (settings.currentSharingMode.isEmpty()) { + settings.currentSharingMode = TelegramUiHelper.getUserName(user) + } } } else -> Unit @@ -227,7 +231,7 @@ class MainActivity : AppCompatActivity(), TelegramListener, ActionButtonsListene } } } - + override fun onTelegramChatsRead() { runOnUi { removeNonexistingChatsFromSettings() diff --git a/OsmAnd-telegram/src/net/osmand/telegram/ui/SettingsDialogFragment.kt b/OsmAnd-telegram/src/net/osmand/telegram/ui/SettingsDialogFragment.kt index 628aa3c6e4..8cc6297547 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/ui/SettingsDialogFragment.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/ui/SettingsDialogFragment.kt @@ -95,40 +95,10 @@ class SettingsDialogFragment : BaseDialogFragment() { val user = telegramHelper.getCurrentUser() if (user != null) { val name = TelegramUiHelper.getUserName(user) - 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) - } + addItemToContainer(inflater, container, name, name) } settings.shareDevicesIds.forEach { - val title = it.key - inflater.inflate(R.layout.item_with_rb_and_btn, container, false).apply { - findViewById(R.id.title).text = it.value - findViewById(R.id.primary_btn).visibility = View.GONE - findViewById(R.id.radio_button).apply { - visibility = View.VISIBLE - isChecked = title == settings.currentSharingMode - } - setOnClickListener { - settings.currentSharingMode = title - updateSelectedSharingMode() - } - tag = title - container.addView(this) - } + addItemToContainer(inflater, container, it.key, it.value) } if (user != null) { @@ -169,6 +139,23 @@ class SettingsDialogFragment : BaseDialogFragment() { } } } + + private fun addItemToContainer(inflater: LayoutInflater, container: ViewGroup, tag: String, title: String) { + inflater.inflate(R.layout.item_with_rb_and_btn, container, false).apply { + findViewById(R.id.title).text = title + findViewById(R.id.primary_btn).visibility = View.GONE + findViewById(R.id.radio_button).apply { + visibility = View.VISIBLE + isChecked = tag == settings.currentSharingMode + } + setOnClickListener { + settings.currentSharingMode = tag + updateSelectedSharingMode() + } + this.tag = tag + container.addView(this) + } + } private fun showPopupMenu(pref: DurationPref, valueView: TextView) { val menuList = pref.getMenuItems() From bc1f9421664f9ef6d42de00cbf6c8ab85be0140a Mon Sep 17 00:00:00 2001 From: Chumva Date: Wed, 3 Oct 2018 13:14:55 +0300 Subject: [PATCH 13/14] Add log to OsmandApiUtils --- .../src/net/osmand/telegram/utils/AndroidNetworkUtils.kt | 4 ++-- .../src/net/osmand/telegram/utils/OsmandApiUtils.kt | 5 ++++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/OsmAnd-telegram/src/net/osmand/telegram/utils/AndroidNetworkUtils.kt b/OsmAnd-telegram/src/net/osmand/telegram/utils/AndroidNetworkUtils.kt index 89a78e1c14..928ccc4905 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/utils/AndroidNetworkUtils.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/utils/AndroidNetworkUtils.kt @@ -27,6 +27,7 @@ object AndroidNetworkUtils { return try { sendRequest(urlText) } catch (e: Exception) { + log.error(e.message, e) null } } @@ -54,8 +55,7 @@ object AndroidNetworkUtils { val responseBody = StringBuilder() responseBody.setLength(0) if (inputStream != null) { - val bufferedInput = - BufferedReader(InputStreamReader(inputStream, "UTF-8")) + val bufferedInput = BufferedReader(InputStreamReader(inputStream, "UTF-8")) var s = bufferedInput.readLine() var first = true while (s != null) { diff --git a/OsmAnd-telegram/src/net/osmand/telegram/utils/OsmandApiUtils.kt b/OsmAnd-telegram/src/net/osmand/telegram/utils/OsmandApiUtils.kt index 36ec57c642..be10f8f601 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/utils/OsmandApiUtils.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/utils/OsmandApiUtils.kt @@ -1,5 +1,6 @@ package net.osmand.telegram.utils +import net.osmand.PlatformUtil import net.osmand.telegram.TelegramApplication import net.osmand.telegram.TelegramSettings import org.json.JSONException @@ -7,6 +8,8 @@ import org.json.JSONObject object OsmandApiUtils { + private val log = PlatformUtil.getLog(OsmandApiUtils::class.java) + fun updateSharingDevices(app: TelegramApplication, userId: Int) { AndroidNetworkUtils.sendRequestAsync( "https://osmand.net/device/send-devices?uid=$userId", @@ -36,7 +39,7 @@ object OsmandApiUtils { list.add(deviceBot) } } catch (e: JSONException) { - + log.error(e.message, e) } return list } From c1f90b1317e44ad81c8cda34235a7fed72639b15 Mon Sep 17 00:00:00 2001 From: Chumva Date: Wed, 3 Oct 2018 13:27:53 +0300 Subject: [PATCH 14/14] Save userId in currentSharingMode --- .../src/net/osmand/telegram/helpers/ShareLocationHelper.kt | 2 +- OsmAnd-telegram/src/net/osmand/telegram/ui/MainActivity.kt | 3 +-- .../src/net/osmand/telegram/ui/SettingsDialogFragment.kt | 3 +-- 3 files changed, 3 insertions(+), 5 deletions(-) diff --git a/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShareLocationHelper.kt b/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShareLocationHelper.kt index d9d7d602cb..991ec25de8 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShareLocationHelper.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShareLocationHelper.kt @@ -43,7 +43,7 @@ class ShareLocationHelper(private val app: TelegramApplication) { if (chatLivePeriods.isNotEmpty()) { val user = app.telegramHelper.getCurrentUser() val sharingMode = app.settings.currentSharingMode - if (user != null && sharingMode == TelegramUiHelper.getUserName(user)) { + if (user != null && sharingMode == user.id.toString()) { app.telegramHelper.sendLiveLocationMessage(chatLivePeriods, location.latitude, location.longitude) } else if (sharingMode.isNotEmpty()) { val url = "https://live.osmand.net/device/$sharingMode/send?lat=${location.latitude}&lon=${location.longitude}" diff --git a/OsmAnd-telegram/src/net/osmand/telegram/ui/MainActivity.kt b/OsmAnd-telegram/src/net/osmand/telegram/ui/MainActivity.kt index ab8bbb4147..14cf76596b 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/ui/MainActivity.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/ui/MainActivity.kt @@ -22,7 +22,6 @@ import net.osmand.telegram.TelegramApplication import net.osmand.telegram.helpers.OsmandAidlHelper import net.osmand.telegram.helpers.TelegramHelper import net.osmand.telegram.helpers.TelegramHelper.* -import net.osmand.telegram.helpers.TelegramUiHelper import net.osmand.telegram.ui.LoginDialogFragment.LoginDialogType import net.osmand.telegram.ui.MyLocationTabFragment.ActionButtonsListener import net.osmand.telegram.ui.views.LockableViewPager @@ -219,7 +218,7 @@ class MainActivity : AppCompatActivity(), TelegramListener, ActionButtonsListene if (user != null) { OsmandApiUtils.updateSharingDevices(app, user.id) if (settings.currentSharingMode.isEmpty()) { - settings.currentSharingMode = TelegramUiHelper.getUserName(user) + settings.currentSharingMode = user.id.toString() } } } diff --git a/OsmAnd-telegram/src/net/osmand/telegram/ui/SettingsDialogFragment.kt b/OsmAnd-telegram/src/net/osmand/telegram/ui/SettingsDialogFragment.kt index 8cc6297547..9e79964551 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/ui/SettingsDialogFragment.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/ui/SettingsDialogFragment.kt @@ -94,8 +94,7 @@ class SettingsDialogFragment : BaseDialogFragment() { container = mainView.findViewById(R.id.share_as_container) val user = telegramHelper.getCurrentUser() if (user != null) { - val name = TelegramUiHelper.getUserName(user) - addItemToContainer(inflater, container, name, name) + addItemToContainer(inflater, container, user.id.toString(), TelegramUiHelper.getUserName(user)) } settings.shareDevicesIds.forEach { addItemToContainer(inflater, container, it.key, it.value)