Add OsmandApiUtils
This commit is contained in:
parent
66db443340
commit
7e87e45b3a
5 changed files with 67 additions and 51 deletions
|
@ -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<Long> = emptySet()
|
||||
|
||||
var shareDevicesIds = mutableMapOf<String, String>()
|
||||
var currentSharingMode: String? = null
|
||||
var currentSharingMode = ""
|
||||
|
||||
var metricsConstants = MetricsConstants.KILOMETERS_AND_METERS
|
||||
var speedConstants = SpeedConstants.KILOMETERS_PER_HOUR
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
@ -243,30 +228,6 @@ class MainActivity : AppCompatActivity(), TelegramListener, ActionButtonsListene
|
|||
}
|
||||
}
|
||||
|
||||
fun parseJsonContents(contentsJson: String): List<TelegramSettings.DeviceBot> {
|
||||
val list = mutableListOf<TelegramSettings.DeviceBot>()
|
||||
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()
|
||||
|
|
|
@ -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<View>(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<TextView>(R.id.title).text = name
|
||||
findViewById<View>(R.id.primary_btn).visibility = View.GONE
|
||||
if (settings.currentSharingMode.isEmpty()) {
|
||||
settings.currentSharingMode = name
|
||||
}
|
||||
findViewById<RadioButton>(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 {
|
||||
|
|
|
@ -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<TelegramSettings.DeviceBot> {
|
||||
val list = mutableListOf<TelegramSettings.DeviceBot>()
|
||||
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
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue