Skip to content

Commit

Permalink
Flags changed check fix, refactor and cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
AkosPaha01 committed May 25, 2022
1 parent fc475fa commit 16d0fe2
Show file tree
Hide file tree
Showing 39 changed files with 188 additions and 433 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ android {
applicationId = "de.dertyp7214.rboardthememanager"
minSdk = 23
targetSdk = 32
versionCode = 352000
versionName = "3.5.2"
versionCode = 353000
versionName = "3.5.3"

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ import android.view.animation.AnimationUtils
import android.widget.ImageView
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.core.graphics.drawable.toBitmap
import androidx.core.graphics.get
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.card.MaterialCardView
import de.dertyp7214.rboardthememanager.R
import de.dertyp7214.rboardthememanager.core.getAttr
import de.dertyp7214.rboardthememanager.core.getBitmap
import de.dertyp7214.rboardthememanager.core.setAll
import de.dertyp7214.rboardthememanager.data.ThemeDataClass
import de.dertyp7214.rboardthememanager.utils.ColorUtils
Expand Down Expand Up @@ -48,7 +48,7 @@ class ThemeAdapter(
ContextCompat.getDrawable(
context,
R.drawable.ic_keyboard
)!!.getBitmap()
)!!.toBitmap()
}
private val selectedBackground by lazy {
ColorDrawable(context.getAttr(R.attr.colorBackgroundFloating)).apply { alpha = 187 }
Expand Down Expand Up @@ -167,7 +167,7 @@ class ThemeAdapter(
ImageView(context).let { view ->
view.setImageBitmap(themeDataClass.image ?: default)
view.colorFilter = themeDataClass.colorFilter
val color = view.drawable.getBitmap().let {
val color = view.drawable.toBitmap().let {
it[0, it.height / 2]
}
Pair(color, ColorUtils.isColorLight(color))
Expand All @@ -183,7 +183,7 @@ class ThemeAdapter(
} ?: ImageView(context).let { view ->
view.setImageBitmap(dataClass.image ?: default)
view.colorFilter = dataClass.colorFilter
val color = view.drawable.getBitmap().let {
val color = view.drawable.toBitmap().let {
it[0, it.height / 2]
}
colorCache[position] = Pair(color, ColorUtils.isColorLight(color))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class ThemePackAdapter(
if (holder is ViewHolder) {
holder.size.text =
"${themePack.themes?.size?.let { "($it)" } ?: ""} ${
themePack.size.zeroOrElse {
themePack.size.zeroOrNull {
it.toHumanReadableBytes(
activity
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
@file:Suppress("MemberVisibilityCanBePrivate")

package de.dertyp7214.rboardthememanager.components

import com.topjohnwu.superuser.io.SuFile
Expand All @@ -9,17 +11,22 @@ import java.util.*
import kotlin.collections.HashMap

@Suppress("unused")
class XMLFile(val path: String? = null, private val initMap: Map<String, Any>? = null) {
private val values: HashMap<String, XMLEntry> = hashMapOf()
class XMLFile(
val path: String? = null,
private val initMap: Map<String, Any>? = null,
initValues: Map<String, XMLEntry> = hashMapOf(),
empty: Boolean = false
) {
private val values: HashMap<String, XMLEntry> = HashMap(initValues)

constructor(initMap: Map<String, Any>?) : this(null, initMap)
constructor(initString: String?) : this(initString?.readXML())
constructor(path: String, initString: String?) : this(path, initString?.readXML())

init {
path.let { if (it != null) SuFile(it).readXML() else initMap ?: mapOf() }
if (!empty) path.let { if (it != null) SuFile(it).readXML() else initMap ?: mapOf() }
.forEach { (key, value) ->
values[key] = XMLEntry.parse(key, value)
values[key] = XMLEntry[key, value]
}
}

Expand All @@ -42,19 +49,28 @@ class XMLFile(val path: String? = null, private val initMap: Map<String, Any>? =
if (path != null) SuFile(path).writeFile(toString())
}

fun forEach(block: (XMLEntry) -> Unit) = values.forEach { (_, entry) -> block(entry) }
fun filter(predicate: (XMLEntry) -> Boolean) =
XMLFile(initValues = values.filter { (_, entry) -> predicate(entry) })

fun has(name: String) = values.containsKey(name)
fun has(entry: XMLEntry) = has(entry.name)
fun hasNot(entry: XMLEntry) = !has(entry)
fun entryEquals(entry: XMLEntry) = values[entry.name]?.value == entry.value
fun entryNotEquals(entry: XMLEntry) = !entryEquals(entry)
operator fun get(name: String): Any? = values[name]?.value

fun toString(comment: String = ""): String {
return "<?xml version='1.0' encoding='utf-8' standalone='yes' ?>\n${comment.let { if (it.isNotEmpty()) "<!--RBOARD:$comment-->" else "" }}\n<map>\n${
values.joinToString("\n") { "${" " * 4}${it.value}" }
return "<?xml version='1.0' encoding='utf-8' standalone='yes' ?>\n${comment.let { if (it.isNotEmpty()) "<!--RBOARD:$comment-->\n" else "" }}<map>\n${
values.joinToString("\n") { it.value[4] }
}\n</map>"
}

override fun toString() = toString("")

override fun equals(other: Any?): Boolean {
if (other !is XMLFile) return false
return other.values.size == values.size && other.values.filter { it1 -> values.any { it2 -> it2.key == it1.key } }.size == values.size
return other.values.size == values.size && other.values.filter { (_, entry) -> values[entry.name] == entry }.size == values.size
}

override fun hashCode(): Int {
Expand Down Expand Up @@ -123,22 +139,34 @@ class XMLEntry(val name: String, _value: Any, val type: XMLType) {
else -> getValue()
}

override fun toString(): String {
return try {
when (type) {
XMLType.LONG, XMLType.BOOLEAN, XMLType.INT, XMLType.DOUBLE, XMLType.FLOAT -> "<${type.identifier} name=\"$name\" value=\"$value\"/>"
XMLType.STRING -> "<string name=\"$name\">$value</string>"
XMLType.SET -> "<set name=\"$name\">\n${
(value as Set<*>).joinToString("\n") { "${" " * 4}$it" }
}\n</set>"
}
} catch (e: Exception) {
"<${type.identifier} name=\"$name\" />"
operator fun get(indent: Int) = try {
when (type) {
XMLType.LONG, XMLType.BOOLEAN, XMLType.INT, XMLType.DOUBLE, XMLType.FLOAT -> "${" " * indent}<${type.identifier} name=\"$name\" value=\"$value\"/>"
XMLType.STRING -> "${" " * indent}<string name=\"$name\">$value</string>"
XMLType.SET -> "${" " * indent}<set name=\"$name\">\n${
(value as Set<*>).joinToString("\n") { "${" " * (4 + indent)}$it" }
}\n${" " * indent}</set>"
}
} catch (e: Exception) {
"${" " * indent}<${type.identifier} name=\"$name\" />"
}

override fun toString() = this[0]

override fun equals(other: Any?): Boolean {
if (other !is XMLEntry) return false
return other.value == value && other.name == name && other.type == type
}

override fun hashCode(): Int {
var result = name.hashCode()
result = 31 * result + type.hashCode()
result = 31 * result + value.hashCode()
return result
}

companion object {
fun <T> parse(name: String, value: T): XMLEntry {
operator fun <T> get(name: String, value: T): XMLEntry {
val type = when (value) {
is Long -> XMLType.LONG
is Boolean -> XMLType.BOOLEAN
Expand Down
18 changes: 0 additions & 18 deletions app/src/main/java/de/dertyp7214/rboardthememanager/core/Bitmap.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,6 @@ package de.dertyp7214.rboardthememanager.core

import android.graphics.*


fun Bitmap.getDominantColor(): Int {
val newBitmap = Bitmap.createScaledBitmap(
copy(
Bitmap.Config.ARGB_8888,
true
), 1, 1, true
)
val color = try {
newBitmap.getPixel(0, 0)
} catch (e: Exception) {
e.printStackTrace()
Color.BLACK
}
newBitmap.recycle()
return color
}

fun Bitmap.resize(width: Int? = null, height: Int? = null): Bitmap {
if (width == null && height == null) return this
val w = width ?: (((height ?: 0).toFloat() / this.height.toFloat()) * this.width.toFloat())
Expand Down
14 changes: 7 additions & 7 deletions app/src/main/java/de/dertyp7214/rboardthememanager/core/Clazz.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import android.content.Context
import android.content.Intent
import androidx.activity.result.ActivityResultLauncher

fun <T : Activity> Class<T>.start(context: Context, block: Intent.() -> Unit = {}) {
operator fun <T : Activity> Class<T>.get(context: Context) =
context.startActivity(Intent(context, this))

operator fun <T : Activity> Class<T>.set(context: Context, block: Intent.() -> Unit) =
context.startActivity(Intent(context, this).apply(block))
}

fun <T : Activity> Class<T>.start(
operator fun <T : Activity> Class<T>.set(
context: Context,
launcher: ActivityResultLauncher<Intent>,
block: Intent.() -> Unit = {}
) {
launcher.launch(Intent(context, this).apply(block))
}
block: Intent.() -> Unit
) = launcher.launch(Intent(context, this).apply(block))
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ fun Context.getAttr(@AttrRes attr: Int): Int {
return typedValue.data
}

@SuppressLint("DiscouragedApi")
@SuppressLint("DiscouragedApi", "InternalInsetResource")
fun Context.getNavigationBarHeight(): Int {
val resourceId: Int = resources.getIdentifier("navigation_bar_height", "dimen", "android")
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.R) {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
package de.dertyp7214.rboardthememanager.core

fun <E> List<E>.containsAny(list: List<E>): Boolean {
var contains = false
forEach {
contains = contains || list.contains(it)
if (contains) return true
}
return contains
}

operator fun <E> List<E>.times(times: Int): ArrayList<E> {
val list = ArrayList(this)
for (i in 0..times) list.addAll(this)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
@file:Suppress("unused")

package de.dertyp7214.rboardthememanager.core

import android.content.Context
Expand All @@ -22,11 +24,11 @@ fun Any.equalsNumber(second: Any?): Boolean {
}
}

fun <E> Int.zeroOrElse(block: (Int) -> E): E? {
fun <E> Int.zeroOrNull(block: (Int) -> E): E? {
return if (this > 0) block(this) else null
}

fun <E> Long.zeroOrElse(block: (Long) -> E): E? {
fun <E> Long.zeroOrNull(block: (Long) -> E): E? {
return if (this > 0) block(this) else null
}

Expand Down
52 changes: 0 additions & 52 deletions app/src/main/java/de/dertyp7214/rboardthememanager/core/String.kt
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,6 @@ fun String.runAsCommand(callback: (result: Array<String>) -> Unit = {}): Boolean
fun String.capitalize() =
replaceFirstChar { if (it.isLowerCase()) it.titlecase(Locale.getDefault()) else it.toString() }

fun String.booleanOrNull(): Boolean? {
return if (this == "true" || this == "false") toBoolean() else null
}

fun String.getSystemProperty(): String {
return try {
Runtime.getRuntime().exec("getprop $this").inputStream.bufferedReader().readLine()
Expand Down Expand Up @@ -107,54 +103,6 @@ fun List<String>.runAsCommand(callback: (result: Array<String>) -> Unit = {}): B
}
}

fun <T> String.setXmlValue(value: T, key: String): String {
return this.let { fileText ->
val type = when (value) {
is Boolean -> "boolean"
is Int -> "integer"
is Long -> "long"
is Float -> "float"
else -> "string"
}
Logger.log(
Logger.Companion.Type.DEBUG,
"CHANGE FLAG",
"value: $value key: $key type: $type"
)
if (type != "string") {
when {
"<$type name=\"$key\"" in fileText -> fileText.replace(
"""<$type name="$key" value=".*" />""".toRegex(),
"""<$type name="$key" value="$value" />"""
)
Regex("<map( |)/>") in fileText -> fileText.replace(
Regex("<map( |)/>"),
"""<map><$type name="$key" value="$value" /></map>"""
)
else -> fileText.replace(
"<map>",
"""<map><$type name="$key" value="$value" />"""
)
}
} else {
when {
"<$type name\"$key\"" in fileText -> fileText.replace(
"""<$type name="$key">.*</$type>""".toRegex(),
"""<$type name="$key">$value</$type>"""
)
Regex("<map( |)/>") in fileText -> fileText.replace(
Regex("<map( |)/>"),
"""<map><$type name="$key">$value</$type></map>"""
)
else -> fileText.replace(
"<map>",
"""<map><$type name="$key">$value</$type>"""
)
}
}
}
}

fun String.readXML(): Map<String, Any> {
val output = HashMap<String, Any>()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,6 @@ fun SuFile.decodeBitmap(opts: BitmapFactory.Options? = null): Bitmap? {
return bm
}

fun SuFile.tar(zip: File) =
listOf("cd $absolutePath", "tar -cf ${zip.absolutePath} .").runAsCommand()

fun SuFile.writeFile(content: String) {
if (exists()) SuFileOutputStream.open(this).writer(UTF_8)
.use { outputStreamWriter ->
Expand Down

This file was deleted.

Loading

0 comments on commit 16d0fe2

Please sign in to comment.