From 5540b2e31b629b3e29b2bdb5c19af3747ce05455 Mon Sep 17 00:00:00 2001 From: Chumva Date: Fri, 28 Sep 2018 16:44:47 +0300 Subject: [PATCH] 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 + } +}