124 lines
3.6 KiB
Kotlin
124 lines
3.6 KiB
Kotlin
package ru.omni_devel.cards
|
|
|
|
import android.content.Context
|
|
import android.graphics.Bitmap
|
|
import android.graphics.BitmapFactory
|
|
import android.os.LocaleList
|
|
import androidx.compose.ui.graphics.ImageBitmap
|
|
import androidx.compose.ui.graphics.asAndroidBitmap
|
|
import com.google.android.gms.wearable.Wearable
|
|
import com.google.zxing.BarcodeFormat
|
|
import com.google.zxing.EncodeHintType
|
|
import com.google.zxing.MultiFormatWriter
|
|
import com.journeyapps.barcodescanner.BarcodeEncoder
|
|
import org.json.JSONObject
|
|
import java.io.ByteArrayOutputStream
|
|
import android.util.Base64
|
|
import androidx.compose.ui.graphics.asImageBitmap
|
|
|
|
fun generateCode(value: String, format: BarcodeFormat): Bitmap {
|
|
val writer = MultiFormatWriter()
|
|
|
|
val (width, height) = when (format) {
|
|
BarcodeFormat.QR_CODE, BarcodeFormat.DATA_MATRIX, BarcodeFormat.AZTEC -> 800 to 800
|
|
else -> 800 to 400
|
|
}
|
|
|
|
val hints = mapOf(
|
|
EncodeHintType.CHARACTER_SET to "UTF-8"
|
|
)
|
|
|
|
val matrix = writer.encode(value, format, width, height, hints)
|
|
|
|
return BarcodeEncoder().createBitmap(matrix)
|
|
}
|
|
|
|
fun sendToWatch(context: Context, path: String, message: String, onResult: (Boolean) -> Unit) {
|
|
val nodeClient = Wearable.getNodeClient(context)
|
|
|
|
nodeClient.connectedNodes.addOnSuccessListener { nodes ->
|
|
if (nodes.isEmpty()) {
|
|
onResult(false)
|
|
|
|
return@addOnSuccessListener
|
|
}
|
|
|
|
var completedCount = 0
|
|
var isAnySuccess = false
|
|
|
|
nodes.forEach { node ->
|
|
Wearable.getMessageClient(context).sendMessage(
|
|
node.id,
|
|
path,
|
|
message.toByteArray(Charsets.UTF_8)
|
|
).addOnCompleteListener(context.mainExecutor) { task ->
|
|
completedCount++
|
|
|
|
if (task.isSuccessful) {
|
|
isAnySuccess = true
|
|
}
|
|
|
|
if (completedCount == nodes.size) {
|
|
onResult(isAnySuccess)
|
|
}
|
|
}.addOnFailureListener(context.mainExecutor) {
|
|
onResult(false)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
fun checkCardData(name: String, value: String): Int? {
|
|
if (name.isEmpty()) {
|
|
return R.string.card_name_should_not_be_empty
|
|
} else if (value.isEmpty()) {
|
|
return R.string.card_value_should_not_be_empty
|
|
} else if (name.length > 32) {
|
|
return R.string.name_must_be_shorter_than_32_symbols
|
|
}
|
|
|
|
return null
|
|
}
|
|
|
|
fun getLanguage(context: Context): String {
|
|
val locales: LocaleList = context.resources.configuration.locales
|
|
val primaryLocale = locales[0]
|
|
val languageCode = primaryLocale.language
|
|
|
|
return languageCode
|
|
}
|
|
|
|
fun selectItemNameViaLanguage(names: LinkedHashMap<String, String>, language: String): String {
|
|
return names[language] ?: names.values.firstOrNull() ?: "???"
|
|
}
|
|
|
|
fun jsonObjectToMap(obj: JSONObject): LinkedHashMap<String, String> {
|
|
val map = LinkedHashMap<String, String>()
|
|
val keys = obj.keys()
|
|
|
|
while (keys.hasNext()) {
|
|
val key = keys.next()
|
|
map[key] = obj.getString(key)
|
|
}
|
|
|
|
return map
|
|
}
|
|
|
|
fun imageBitmapToBase64(imageBitmap: ImageBitmap): String {
|
|
val bitmap = imageBitmap.asAndroidBitmap()
|
|
val outputStream = ByteArrayOutputStream()
|
|
|
|
bitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream)
|
|
|
|
val byteArray = outputStream.toByteArray()
|
|
|
|
return Base64.encodeToString(byteArray, Base64.DEFAULT)
|
|
}
|
|
|
|
fun base64ToImageBitmap(base64String: String): ImageBitmap {
|
|
val decodedBytes = Base64.decode(base64String, Base64.DEFAULT)
|
|
val bitmap = BitmapFactory.decodeByteArray(decodedBytes, 0, decodedBytes.size)
|
|
|
|
return bitmap.asImageBitmap()
|
|
}
|