Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package de.stephanlindauer.criticalmaps.model

import de.stephanlindauer.criticalmaps.model.chat.ReceivedChatMessage
import de.stephanlindauer.criticalmaps.utils.AeSimpleSHA1
import org.json.JSONArray
import org.json.JSONException
import org.json.JSONObject
import timber.log.Timber
import java.io.UnsupportedEncodingException
import java.net.URLDecoder
import java.net.URLEncoder
import java.util.Date
import javax.inject.Inject
import javax.inject.Singleton

@Singleton
class ChatModel @Inject constructor(
private val userModel: UserModel
) {
private var receivedChatMessages = mutableListOf<ReceivedChatMessage>()

fun getReceivedChatMessages(): List<ReceivedChatMessage> = receivedChatMessages

@Throws(JSONException::class, UnsupportedEncodingException::class)
fun setFromJson(jsonArray: JSONArray) {
receivedChatMessages = ArrayList(jsonArray.length())

for (i in 0 until jsonArray.length()) {
val jsonObject = jsonArray.getJSONObject(i)

val device = URLDecoder.decode(jsonObject.getString("device"), "UTF-8")
val identifier = URLDecoder.decode(jsonObject.getString("identifier"), "UTF-8")
val message = URLDecoder.decode(jsonObject.getString("message"), "UTF-8")
val timestamp = Date(jsonObject.getString("timestamp").toLong() * 1000)

receivedChatMessages.add(ReceivedChatMessage(message, timestamp))
}

receivedChatMessages.sortBy { it.timestamp }
}

fun createNewOutgoingMessage(message: String): JSONObject {
val messageObject = JSONObject()
try {
messageObject.put("text", urlEncodeMessage(message))
messageObject.put("identifier", AeSimpleSHA1.SHA1(message + Math.random()) ?: "")
messageObject.put("device", userModel.changingDeviceToken)
} catch (e: JSONException) {
Timber.d(e)
}
return messageObject
}

private fun urlEncodeMessage(messageToEncode: String): String {
return try {
URLEncoder.encode(messageToEncode, "UTF-8")
} catch (e: UnsupportedEncodingException) {
""
}
}

companion object {
const val MESSAGE_MAX_LENGTH = 255
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package de.stephanlindauer.criticalmaps.model

import org.json.JSONArray
import org.json.JSONException
import org.maplibre.android.geometry.LatLng
import javax.inject.Inject
import javax.inject.Singleton

@Singleton
class OtherUsersLocationModel @Inject constructor(
private val userModel: UserModel
) {
private val otherUsersLocations = HashMap<String, LatLng>()

@Throws(JSONException::class)
fun setFromJson(jsonArray: JSONArray) {
for (i in 0 until jsonArray.length()) {
val locationObject = jsonArray.getJSONObject(i)
val deviceId = locationObject.getString("device")

// Ignore own location
if (deviceId == userModel.changingDeviceToken) {
continue
}

val latitudeE6 = locationObject.getString("latitude").toInt()
val longitudeE6 = locationObject.getString("longitude").toInt()

otherUsersLocations[deviceId] = LatLng(latitudeE6 / 1E6, longitudeE6 / 1E6)
}
}

fun getOtherUsersLocations(): Map<String, LatLng> = otherUsersLocations
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package de.stephanlindauer.criticalmaps.model

import org.json.JSONException
import org.json.JSONObject
import org.maplibre.android.geometry.LatLng
import timber.log.Timber
import javax.inject.Inject
import javax.inject.Singleton

@Singleton
class OwnLocationModel @Inject constructor() {

@JvmField
var ownLocation: LatLng? = null
private var isLocationPrecise = false

fun setLocation(location: LatLng, accuracy: Float) {
ownLocation = location
isLocationPrecise = accuracy < ACCURACY_PRECISE_THRESHOLD
}

fun hasPreciseLocation(): Boolean = ownLocation != null && isLocationPrecise

fun getLocationJson(): JSONObject {
val location = ownLocation
require(location != null) { "Location must be set before calling getLocationJson()" }

val locationObject = JSONObject()
try {
locationObject.put("longitude", (location.longitude * 1_000_000.0).toInt().toString())
locationObject.put("latitude", (location.latitude * 1_000_000.0).toInt().toString())
} catch (e: JSONException) {
Timber.e(e)
}
return locationObject
}

companion object {
private const val ACCURACY_PRECISE_THRESHOLD = 50.0f // meters
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package de.stephanlindauer.criticalmaps.model

import kotlin.random.Random

class PermissionRequest(
val permissions: Array<String>,
val rationale: String,
onGrantedCallback: Runnable? = null,
onDeniedCallback: Runnable? = null,
onPermanentlyDeniedCallback: Runnable? = null
) {
val onGrantedCallback: Runnable = onGrantedCallback ?: Runnable { }
val onDeniedCallback: Runnable = onDeniedCallback ?: Runnable { }
val onPermanentlyDeniedCallback: Runnable = onPermanentlyDeniedCallback ?: Runnable { }

// Can only use lower 16 bits for requestCode --> short
val requestCode: Int = Random.nextInt(Short.MAX_VALUE.toInt())
}
Loading