Fix sharing to new device, different parsed time and add check for last known location
This commit is contained in:
parent
5089bdfcde
commit
f89cf5a5a4
6 changed files with 42 additions and 30 deletions
|
@ -159,10 +159,16 @@ class TelegramSettings(private val app: TelegramApplication) {
|
|||
}
|
||||
}
|
||||
|
||||
fun updateShareDevicesIds(list: List<DeviceBot>) {
|
||||
fun updateShareDevices(list: List<DeviceBot>) {
|
||||
shareDevices = list.toHashSet()
|
||||
}
|
||||
|
||||
fun addShareDevice(device: DeviceBot) {
|
||||
val devices = shareDevices.toMutableList()
|
||||
devices.add(device)
|
||||
shareDevices = devices.toHashSet()
|
||||
}
|
||||
|
||||
fun updateCurrentSharingMode(sharingMode: String) {
|
||||
if (currentSharingMode != sharingMode) {
|
||||
shareChatsInfo.forEach { (_, shareInfo) ->
|
||||
|
@ -268,7 +274,12 @@ class TelegramSettings(private val app: TelegramApplication) {
|
|||
statusChangeTime = System.currentTimeMillis()
|
||||
val lm = app.getSystemService(Context.LOCATION_SERVICE) as LocationManager
|
||||
val gpsEnabled = try {
|
||||
lm.isProviderEnabled(LocationManager.GPS_PROVIDER)
|
||||
if (lm.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
|
||||
val loc = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER)
|
||||
loc != null && ((statusChangeTime - loc.time) / 1000) < 180
|
||||
} else {
|
||||
false
|
||||
}
|
||||
} catch (ex: Exception) {
|
||||
false
|
||||
}
|
||||
|
|
|
@ -64,13 +64,13 @@ class ShareLocationHelper(private val app: TelegramApplication) {
|
|||
object : AndroidNetworkUtils.OnRequestResultListener {
|
||||
override fun onResult(result: String?) {
|
||||
updateShareInfoSuccessfulSendTime(result, chatsShareInfo)
|
||||
|
||||
val osmandBot = app.telegramHelper.getOsmandBot()
|
||||
if (osmandBot != null) {
|
||||
checkAndSendViaBotMessages(chatsShareInfo, TdApi.Location(latitude, longitude), osmandBot)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
val osmandBot = app.telegramHelper.getOsmandBot()
|
||||
if (osmandBot != null) {
|
||||
checkAndSendViaBotMessages(chatsShareInfo, TdApi.Location(latitude, longitude), osmandBot)
|
||||
}
|
||||
}
|
||||
}
|
||||
lastLocationMessageSentTime = System.currentTimeMillis()
|
||||
|
|
|
@ -982,7 +982,9 @@ class TelegramHelper private constructor() {
|
|||
val updatedStr = s.removePrefix(UPDATED_PREFIX)
|
||||
val endIndex = updatedStr.indexOf("(")
|
||||
val updatedS = updatedStr.substring(0, if (endIndex != -1) endIndex else updatedStr.length)
|
||||
res.lastUpdated = (parseTime(updatedS.trim()) / 1000).toInt()
|
||||
val parsedTime = (parseTime(updatedS.trim()) / 1000).toInt()
|
||||
val currentTime = (System.currentTimeMillis() / 1000) - 1
|
||||
res.lastUpdated = if (parsedTime < currentTime) parsedTime else currentTime.toInt()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -105,10 +105,10 @@ class AddNewDeviceBottomSheet : BaseDialogFragment() {
|
|||
object : AndroidNetworkUtils.OnRequestResultListener {
|
||||
override fun onResult(result: String?) {
|
||||
updatePrimaryBtnAndProgress(false)
|
||||
val deviceBot = getDeviceFromJson(result)
|
||||
if (deviceBot != null) {
|
||||
val deviceJson = getDeviceJson(result)
|
||||
if (deviceJson != null) {
|
||||
targetFragment?.also { target ->
|
||||
val intent = Intent().putExtra(DEVICE_NAME, deviceBot.deviceName).putExtra(DEVICE_EXTERNAL_ID, deviceBot.externalId)
|
||||
val intent = Intent().putExtra(DEVICE_JSON, deviceJson.toString())
|
||||
target.onActivityResult(targetRequestCode, NEW_DEVICE_REQUEST_CODE, intent)
|
||||
}
|
||||
dismiss()
|
||||
|
@ -151,28 +151,23 @@ class AddNewDeviceBottomSheet : BaseDialogFragment() {
|
|||
}
|
||||
}
|
||||
|
||||
private fun getDeviceFromJson(json: String?): TelegramSettings.DeviceBot? {
|
||||
var device: TelegramSettings.DeviceBot? = null
|
||||
private fun getDeviceJson(json: String?): JSONObject? {
|
||||
if (json != null) {
|
||||
device = try {
|
||||
try {
|
||||
val jsonResult = JSONObject(json)
|
||||
val status = jsonResult.getString("status")
|
||||
if (status == "OK") {
|
||||
OsmandApiUtils.parseDeviceBot(jsonResult.getJSONObject("device"))
|
||||
} else {
|
||||
null
|
||||
return jsonResult.getJSONObject("device")
|
||||
}
|
||||
} catch (e: JSONException) {
|
||||
null
|
||||
}
|
||||
}
|
||||
return device
|
||||
return null
|
||||
}
|
||||
|
||||
companion object {
|
||||
const val NEW_DEVICE_REQUEST_CODE = 5
|
||||
const val DEVICE_NAME = "DEVICE_NAME"
|
||||
const val DEVICE_EXTERNAL_ID = "DEVICE_EXTERNAL_ID"
|
||||
const val DEVICE_JSON = "DEVICE_JSON"
|
||||
const val MAX_DEVICE_NAME_LENGTH = 200
|
||||
|
||||
private const val TAG = "AddNewDeviceBottomSheet"
|
||||
|
|
|
@ -20,7 +20,9 @@ import net.osmand.telegram.TelegramSettings.DurationPref
|
|||
import net.osmand.telegram.helpers.TelegramHelper.Companion.OSMAND_BOT_USERNAME
|
||||
import net.osmand.telegram.helpers.TelegramUiHelper
|
||||
import net.osmand.telegram.utils.AndroidUtils
|
||||
import net.osmand.telegram.utils.OsmandApiUtils
|
||||
import org.drinkless.td.libcore.telegram.TdApi
|
||||
import org.json.JSONObject
|
||||
|
||||
class SettingsDialogFragment : BaseDialogFragment() {
|
||||
|
||||
|
@ -182,14 +184,16 @@ class SettingsDialogFragment : BaseDialogFragment() {
|
|||
}
|
||||
AddNewDeviceBottomSheet.NEW_DEVICE_REQUEST_CODE -> {
|
||||
val user = app.telegramHelper.getCurrentUser()
|
||||
if (user != null && data != null) {
|
||||
val deviceName = data.getStringExtra(AddNewDeviceBottomSheet.DEVICE_NAME)
|
||||
val deviceExternalId = data.getStringExtra(AddNewDeviceBottomSheet.DEVICE_EXTERNAL_ID)
|
||||
|
||||
val inflater = activity?.layoutInflater
|
||||
if (inflater != null && deviceName != null && deviceExternalId != null) {
|
||||
addItemToContainer(inflater, shareAsContainer, deviceExternalId, deviceName)
|
||||
Toast.makeText(app, getString(R.string.device_added_successfully, deviceName), Toast.LENGTH_SHORT).show()
|
||||
if (user != null && data != null && data.hasExtra(AddNewDeviceBottomSheet.DEVICE_JSON)) {
|
||||
val deviceJson = data.getStringExtra(AddNewDeviceBottomSheet.DEVICE_JSON)
|
||||
val device = OsmandApiUtils.parseDeviceBot(JSONObject(deviceJson))
|
||||
if (device != null) {
|
||||
app.settings.addShareDevice(device)
|
||||
val inflater = activity?.layoutInflater
|
||||
if (inflater != null) {
|
||||
addItemToContainer(inflater, shareAsContainer, device.externalId, device.deviceName)
|
||||
Toast.makeText(app, getString(R.string.device_added_successfully, device.deviceName), Toast.LENGTH_SHORT).show()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -20,7 +20,7 @@ object OsmandApiUtils {
|
|||
override fun onResult(result: String?) {
|
||||
if (result != null) {
|
||||
val list = parseJsonContents(result)
|
||||
app.settings.updateShareDevicesIds(list)
|
||||
app.settings.updateShareDevices(list)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue