forked from gotify/android
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtils.kt
More file actions
119 lines (106 loc) · 3.78 KB
/
Copy pathUtils.kt
File metadata and controls
119 lines (106 loc) · 3.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package com.github.gotify
import android.app.Activity
import android.app.ActivityManager
import android.content.Context
import android.graphics.drawable.Drawable
import android.text.format.DateUtils
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.lifecycleScope
import coil.target.Target
import com.github.gotify.client.JSON
import com.google.android.material.snackbar.Snackbar
import com.google.gson.Gson
import java.io.BufferedReader
import java.io.IOException
import java.io.InputStream
import java.io.InputStreamReader
import java.net.MalformedURLException
import java.net.URI
import java.net.URISyntaxException
import java.net.URL
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import okio.Buffer
import org.threeten.bp.OffsetDateTime
import org.tinylog.kotlin.Logger
internal object Utils {
val JSON: Gson = JSON().gson
fun showSnackBar(activity: Activity, message: String?) {
val rootView = activity.window.decorView.findViewById<View>(android.R.id.content)
Snackbar.make(rootView, message!!, Snackbar.LENGTH_SHORT).show()
}
fun longToInt(value: Long): Int {
return (value % Int.MAX_VALUE).toInt()
}
fun dateToRelative(data: OffsetDateTime): String {
val time = data.toInstant().toEpochMilli()
val now = System.currentTimeMillis()
return DateUtils.getRelativeTimeSpanString(time, now, DateUtils.MINUTE_IN_MILLIS)
.toString()
}
fun resolveAbsoluteUrl(baseURL: String, target: String?): String? {
return if (target == null) {
null
} else {
try {
val targetUri = URI(target)
if (targetUri.isAbsolute) {
target
} else {
URL(URL(baseURL), target).toString()
}
} catch (e: MalformedURLException) {
Logger.error(e, "Could not resolve absolute url")
target
} catch (e: URISyntaxException) {
Logger.error(e, "Could not resolve absolute url")
target
}
}
}
fun toDrawable(drawableReceiver: DrawableReceiver): Target {
return object : Target {
override fun onSuccess(result: Drawable) {
drawableReceiver.loaded(result)
}
override fun onError(error: Drawable?) {
Logger.error("Bitmap failed")
}
}
}
fun readFileFromStream(inputStream: InputStream): String {
val sb = StringBuilder()
var currentLine: String?
try {
BufferedReader(InputStreamReader(inputStream)).use { reader ->
while (reader.readLine().also { currentLine = it } != null) {
sb.append(currentLine).append("\n")
}
}
} catch (e: IOException) {
throw IllegalArgumentException("failed to read input")
}
return sb.toString()
}
fun stringToInputStream(str: String?): InputStream? {
return if (str == null) null else Buffer().writeUtf8(str).inputStream()
}
fun AppCompatActivity.launchCoroutine(
dispatcher: CoroutineDispatcher = Dispatchers.IO,
action: suspend (coroutineScope: CoroutineScope) -> Unit
) {
this.lifecycleScope.launch(dispatcher) {
action(this)
}
}
fun interface DrawableReceiver {
fun loaded(drawable: Drawable?)
}
fun setExcludeFromRecent(context: Context, excludeFromRecent: Boolean) {
context.getSystemService(ActivityManager::class.java).appTasks?.getOrNull(0)
?.setExcludeFromRecents(excludeFromRecent)
}
}