mirror of
https://github.com/meshtastic/Meshtastic-Android.git
synced 2026-04-20 22:23:37 +00:00
incorporate androidlib
This commit is contained in:
parent
20cf3f0825
commit
5eb5cd1421
63 changed files with 1451 additions and 108 deletions
27
app/src/main/java/com/geeksville/mesh/util/AnyExtensions.kt
Normal file
27
app/src/main/java/com/geeksville/mesh/util/AnyExtensions.kt
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
package com.geeksville.mesh.util
|
||||
|
||||
import com.geeksville.mesh.BuildConfig
|
||||
|
||||
/// A toString that makes sure all newlines are removed (for nice logging).
|
||||
fun Any.toOneLineString() = this.toString().replace('\n', ' ')
|
||||
|
||||
/// Return a one line string version of an object (but if a release build, just say 'might be PII)
|
||||
fun Any.toPIIString() =
|
||||
if (!BuildConfig.DEBUG)
|
||||
"<PII?>"
|
||||
else
|
||||
this.toOneLineString()
|
||||
|
||||
fun formatAgo(lastSeenUnix: Int): String {
|
||||
val currentTime = (System.currentTimeMillis() / 1000).toInt()
|
||||
val diffMin = (currentTime - lastSeenUnix) / 60;
|
||||
if (diffMin < 1)
|
||||
return "now";
|
||||
if (diffMin < 100)
|
||||
return diffMin.toString() + "m"
|
||||
if (diffMin < 6000)
|
||||
return (diffMin / 60).toString() + "h"
|
||||
if (diffMin < 144000)
|
||||
return (diffMin / (60 * 24)).toString() + "d";
|
||||
return "?";
|
||||
}
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
package com.geeksville.mesh.util
|
||||
|
||||
fun ByteArray.toHexString() = joinToString("") { "%02x".format(it) }
|
||||
79
app/src/main/java/com/geeksville/mesh/util/Exceptions.kt
Normal file
79
app/src/main/java/com/geeksville/mesh/util/Exceptions.kt
Normal file
|
|
@ -0,0 +1,79 @@
|
|||
package com.geeksville.mesh.util
|
||||
|
||||
import android.os.RemoteException
|
||||
import android.util.Log
|
||||
import android.view.View
|
||||
import com.geeksville.mesh.android.Logging
|
||||
import com.google.android.material.snackbar.Snackbar
|
||||
|
||||
|
||||
object Exceptions : Logging {
|
||||
/// Set in Application.onCreate
|
||||
var reporter: ((Throwable, String?, String?) -> Unit)? = null
|
||||
|
||||
/**
|
||||
* Report an exception to our analytics provider (if installed - otherwise just log)
|
||||
*
|
||||
* After reporting return
|
||||
*/
|
||||
fun report(exception: Throwable, tag: String? = null, message: String? = null) {
|
||||
errormsg(
|
||||
"Exceptions.report: $tag $message",
|
||||
exception
|
||||
) // print the message to the log _before_ telling the crash reporter
|
||||
reporter?.let { r ->
|
||||
r(exception, tag, message)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This wraps (and discards) exceptions, but first it reports them to our bug tracking system and prints
|
||||
* a message to the log.
|
||||
*/
|
||||
fun exceptionReporter(inner: () -> Unit) {
|
||||
try {
|
||||
inner()
|
||||
} catch (ex: Throwable) {
|
||||
// DO NOT THROW users expect we have fully handled/discarded the exception
|
||||
Exceptions.report(ex, "exceptionReporter", "Uncaught Exception")
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* If an exception occurs, show the message in a snackbar and continue
|
||||
*/
|
||||
fun exceptionToSnackbar(view: View, inner: () -> Unit) {
|
||||
try {
|
||||
inner()
|
||||
} catch (ex: Throwable) {
|
||||
Snackbar.make(view, ex.message ?: "An exception occurred", Snackbar.LENGTH_LONG).show()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* This wraps (and discards) exceptions, but it does output a log message
|
||||
*/
|
||||
fun ignoreException(silent: Boolean = false, inner: () -> Unit) {
|
||||
try {
|
||||
inner()
|
||||
} catch (ex: Throwable) {
|
||||
// DO NOT THROW users expect we have fully handled/discarded the exception
|
||||
if(!silent)
|
||||
Exceptions.errormsg("ignoring exception", ex)
|
||||
}
|
||||
}
|
||||
|
||||
/// Convert any exceptions in this service call into a RemoteException that the client can
|
||||
/// then handle
|
||||
fun <T> toRemoteExceptions(inner: () -> T): T = try {
|
||||
inner()
|
||||
} catch (ex: Throwable) {
|
||||
Log.e("toRemoteExceptions", "Uncaught exception, returning to remote client", ex)
|
||||
when(ex) { // don't double wrap remote exceptions
|
||||
is RemoteException -> throw ex
|
||||
else -> throw RemoteException(ex.message)
|
||||
}
|
||||
}
|
||||
|
||||
17
app/src/main/java/com/geeksville/mesh/util/anonymize.kt
Normal file
17
app/src/main/java/com/geeksville/mesh/util/anonymize.kt
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
package com.geeksville.mesh.util
|
||||
|
||||
/**
|
||||
* 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"
|
||||
Loading…
Add table
Add a link
Reference in a new issue