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()
|
shareDevices = list.toHashSet()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun addShareDevice(device: DeviceBot) {
|
||||||
|
val devices = shareDevices.toMutableList()
|
||||||
|
devices.add(device)
|
||||||
|
shareDevices = devices.toHashSet()
|
||||||
|
}
|
||||||
|
|
||||||
fun updateCurrentSharingMode(sharingMode: String) {
|
fun updateCurrentSharingMode(sharingMode: String) {
|
||||||
if (currentSharingMode != sharingMode) {
|
if (currentSharingMode != sharingMode) {
|
||||||
shareChatsInfo.forEach { (_, shareInfo) ->
|
shareChatsInfo.forEach { (_, shareInfo) ->
|
||||||
|
@ -268,7 +274,12 @@ class TelegramSettings(private val app: TelegramApplication) {
|
||||||
statusChangeTime = System.currentTimeMillis()
|
statusChangeTime = System.currentTimeMillis()
|
||||||
val lm = app.getSystemService(Context.LOCATION_SERVICE) as LocationManager
|
val lm = app.getSystemService(Context.LOCATION_SERVICE) as LocationManager
|
||||||
val gpsEnabled = try {
|
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) {
|
} catch (ex: Exception) {
|
||||||
false
|
false
|
||||||
}
|
}
|
||||||
|
|
|
@ -64,13 +64,13 @@ class ShareLocationHelper(private val app: TelegramApplication) {
|
||||||
object : AndroidNetworkUtils.OnRequestResultListener {
|
object : AndroidNetworkUtils.OnRequestResultListener {
|
||||||
override fun onResult(result: String?) {
|
override fun onResult(result: String?) {
|
||||||
updateShareInfoSuccessfulSendTime(result, chatsShareInfo)
|
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()
|
lastLocationMessageSentTime = System.currentTimeMillis()
|
||||||
|
|
|
@ -982,7 +982,9 @@ class TelegramHelper private constructor() {
|
||||||
val updatedStr = s.removePrefix(UPDATED_PREFIX)
|
val updatedStr = s.removePrefix(UPDATED_PREFIX)
|
||||||
val endIndex = updatedStr.indexOf("(")
|
val endIndex = updatedStr.indexOf("(")
|
||||||
val updatedS = updatedStr.substring(0, if (endIndex != -1) endIndex else updatedStr.length)
|
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 {
|
object : AndroidNetworkUtils.OnRequestResultListener {
|
||||||
override fun onResult(result: String?) {
|
override fun onResult(result: String?) {
|
||||||
updatePrimaryBtnAndProgress(false)
|
updatePrimaryBtnAndProgress(false)
|
||||||
val deviceBot = getDeviceFromJson(result)
|
val deviceJson = getDeviceJson(result)
|
||||||
if (deviceBot != null) {
|
if (deviceJson != null) {
|
||||||
targetFragment?.also { target ->
|
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)
|
target.onActivityResult(targetRequestCode, NEW_DEVICE_REQUEST_CODE, intent)
|
||||||
}
|
}
|
||||||
dismiss()
|
dismiss()
|
||||||
|
@ -151,28 +151,23 @@ class AddNewDeviceBottomSheet : BaseDialogFragment() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun getDeviceFromJson(json: String?): TelegramSettings.DeviceBot? {
|
private fun getDeviceJson(json: String?): JSONObject? {
|
||||||
var device: TelegramSettings.DeviceBot? = null
|
|
||||||
if (json != null) {
|
if (json != null) {
|
||||||
device = try {
|
try {
|
||||||
val jsonResult = JSONObject(json)
|
val jsonResult = JSONObject(json)
|
||||||
val status = jsonResult.getString("status")
|
val status = jsonResult.getString("status")
|
||||||
if (status == "OK") {
|
if (status == "OK") {
|
||||||
OsmandApiUtils.parseDeviceBot(jsonResult.getJSONObject("device"))
|
return jsonResult.getJSONObject("device")
|
||||||
} else {
|
|
||||||
null
|
|
||||||
}
|
}
|
||||||
} catch (e: JSONException) {
|
} catch (e: JSONException) {
|
||||||
null
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return device
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
const val NEW_DEVICE_REQUEST_CODE = 5
|
const val NEW_DEVICE_REQUEST_CODE = 5
|
||||||
const val DEVICE_NAME = "DEVICE_NAME"
|
const val DEVICE_JSON = "DEVICE_JSON"
|
||||||
const val DEVICE_EXTERNAL_ID = "DEVICE_EXTERNAL_ID"
|
|
||||||
const val MAX_DEVICE_NAME_LENGTH = 200
|
const val MAX_DEVICE_NAME_LENGTH = 200
|
||||||
|
|
||||||
private const val TAG = "AddNewDeviceBottomSheet"
|
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.TelegramHelper.Companion.OSMAND_BOT_USERNAME
|
||||||
import net.osmand.telegram.helpers.TelegramUiHelper
|
import net.osmand.telegram.helpers.TelegramUiHelper
|
||||||
import net.osmand.telegram.utils.AndroidUtils
|
import net.osmand.telegram.utils.AndroidUtils
|
||||||
|
import net.osmand.telegram.utils.OsmandApiUtils
|
||||||
import org.drinkless.td.libcore.telegram.TdApi
|
import org.drinkless.td.libcore.telegram.TdApi
|
||||||
|
import org.json.JSONObject
|
||||||
|
|
||||||
class SettingsDialogFragment : BaseDialogFragment() {
|
class SettingsDialogFragment : BaseDialogFragment() {
|
||||||
|
|
||||||
|
@ -182,14 +184,16 @@ class SettingsDialogFragment : BaseDialogFragment() {
|
||||||
}
|
}
|
||||||
AddNewDeviceBottomSheet.NEW_DEVICE_REQUEST_CODE -> {
|
AddNewDeviceBottomSheet.NEW_DEVICE_REQUEST_CODE -> {
|
||||||
val user = app.telegramHelper.getCurrentUser()
|
val user = app.telegramHelper.getCurrentUser()
|
||||||
if (user != null && data != null) {
|
if (user != null && data != null && data.hasExtra(AddNewDeviceBottomSheet.DEVICE_JSON)) {
|
||||||
val deviceName = data.getStringExtra(AddNewDeviceBottomSheet.DEVICE_NAME)
|
val deviceJson = data.getStringExtra(AddNewDeviceBottomSheet.DEVICE_JSON)
|
||||||
val deviceExternalId = data.getStringExtra(AddNewDeviceBottomSheet.DEVICE_EXTERNAL_ID)
|
val device = OsmandApiUtils.parseDeviceBot(JSONObject(deviceJson))
|
||||||
|
if (device != null) {
|
||||||
val inflater = activity?.layoutInflater
|
app.settings.addShareDevice(device)
|
||||||
if (inflater != null && deviceName != null && deviceExternalId != null) {
|
val inflater = activity?.layoutInflater
|
||||||
addItemToContainer(inflater, shareAsContainer, deviceExternalId, deviceName)
|
if (inflater != null) {
|
||||||
Toast.makeText(app, getString(R.string.device_added_successfully, deviceName), Toast.LENGTH_SHORT).show()
|
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?) {
|
override fun onResult(result: String?) {
|
||||||
if (result != null) {
|
if (result != null) {
|
||||||
val list = parseJsonContents(result)
|
val list = parseJsonContents(result)
|
||||||
app.settings.updateShareDevicesIds(list)
|
app.settings.updateShareDevices(list)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue