2024-11-26 08:38:12 -03:00
|
|
|
/*
|
2025-01-02 06:50:26 -03:00
|
|
|
* Copyright (c) 2025 Meshtastic LLC
|
2024-11-26 08:38:12 -03:00
|
|
|
*
|
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
2023-03-02 21:37:04 -03:00
|
|
|
package com.geeksville.mesh.util
|
|
|
|
|
|
|
|
|
|
import android.widget.EditText
|
|
|
|
|
import com.geeksville.mesh.BuildConfig
|
2024-09-19 17:53:51 -03:00
|
|
|
import com.geeksville.mesh.ConfigProtos
|
2023-03-02 21:37:04 -03:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* When printing strings to logs sometimes we want to print useful debugging information about users
|
|
|
|
|
* or positions. But we don't want to leak things like usernames or locations. So this function
|
|
|
|
|
* if given a string, will return a string which is a maximum of three characters long, taken from the tail
|
|
|
|
|
* of the string. Which should effectively hide real usernames and locations,
|
|
|
|
|
* but still let us see if values were zero, empty or different.
|
|
|
|
|
*/
|
|
|
|
|
val Any?.anonymize: String
|
|
|
|
|
get() = this.anonymize()
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* A version of anonymize that allows passing in a custom minimum length
|
|
|
|
|
*/
|
|
|
|
|
fun Any?.anonymize(maxLen: Int = 3) =
|
|
|
|
|
if (this != null) ("..." + this.toString().takeLast(maxLen)) else "null"
|
|
|
|
|
|
2024-10-13 23:02:05 -03:00
|
|
|
// A toString that makes sure all newlines are removed (for nice logging).
|
2023-03-02 21:37:04 -03:00
|
|
|
fun Any.toOneLineString() = this.toString().replace('\n', ' ')
|
|
|
|
|
|
2024-09-19 17:53:51 -03:00
|
|
|
fun ConfigProtos.Config.toOneLineString(): String {
|
|
|
|
|
val redactedFields = """(wifi_psk:|public_key:|private_key:|admin_key:)\s*".*"""
|
|
|
|
|
return this.toString()
|
|
|
|
|
.replace(redactedFields.toRegex()) { "${it.groupValues[1]} \"[REDACTED]\"" }
|
|
|
|
|
.replace('\n', ' ')
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-13 23:02:05 -03:00
|
|
|
// Return a one line string version of an object (but if a release build, just say 'might be PII)
|
2023-03-02 21:37:04 -03:00
|
|
|
fun Any.toPIIString() =
|
2024-10-13 23:02:05 -03:00
|
|
|
if (!BuildConfig.DEBUG) {
|
2023-03-02 21:37:04 -03:00
|
|
|
"<PII?>"
|
2024-10-13 23:02:05 -03:00
|
|
|
} else {
|
2023-03-02 21:37:04 -03:00
|
|
|
this.toOneLineString()
|
2024-10-13 23:02:05 -03:00
|
|
|
}
|
2023-03-02 21:37:04 -03:00
|
|
|
|
|
|
|
|
fun ByteArray.toHexString() = joinToString("") { "%02x".format(it) }
|
|
|
|
|
|
2024-08-04 05:47:48 -05:00
|
|
|
fun formatAgo(lastSeenUnix: Int, currentTimeMillis: Long = System.currentTimeMillis()): String {
|
|
|
|
|
val currentTime = (currentTimeMillis / 1000).toInt()
|
2023-03-02 21:37:04 -03:00
|
|
|
val diffMin = (currentTime - lastSeenUnix) / 60
|
2024-08-04 05:47:48 -05:00
|
|
|
return when {
|
|
|
|
|
diffMin < 1 -> "now"
|
|
|
|
|
diffMin < 60 -> diffMin.toString() + " min"
|
|
|
|
|
diffMin < 2880 -> (diffMin / 60).toString() + " h"
|
|
|
|
|
diffMin < 1440000 -> (diffMin / (60 * 24)).toString() + " d"
|
|
|
|
|
else -> "?"
|
|
|
|
|
}
|
2023-03-02 21:37:04 -03:00
|
|
|
}
|
|
|
|
|
|
2024-10-13 23:02:05 -03:00
|
|
|
// Allows usage like email.onEditorAction(EditorInfo.IME_ACTION_NEXT, { confirm() })
|
2023-03-02 21:37:04 -03:00
|
|
|
fun EditText.onEditorAction(actionId: Int, func: () -> Unit) {
|
|
|
|
|
setOnEditorActionListener { _, receivedActionId, _ ->
|
|
|
|
|
|
|
|
|
|
if (actionId == receivedActionId) {
|
|
|
|
|
func()
|
|
|
|
|
}
|
|
|
|
|
true
|
|
|
|
|
}
|
|
|
|
|
}
|