Skip to content

Commit

Permalink
Flags changed check fix, refactor and cleanup, Custom flags and prefs…
Browse files Browse the repository at this point in the history
… input, import and share prefs, Flags Fix, Libs Update, Update Italian translation, Fixing dynamic and system automatic theme hiding on old android.
  • Loading branch information
AkosPaha01 authored May 25, 2022
2 parents 932cd66 + 507adc6 commit 83977ce
Show file tree
Hide file tree
Showing 52 changed files with 574 additions and 466 deletions.
4 changes: 4 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 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 = 351000
versionName = "3.5.1"
versionCode = 353000
versionName = "3.5.3"

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
}
Expand Down Expand Up @@ -89,7 +89,7 @@ android {
}*/

dependencies {
implementation(platform("com.google.firebase:firebase-bom:30.0.0"))
implementation(platform("com.google.firebase:firebase-bom:30.0.1"))
implementation("com.google.firebase:firebase-messaging-ktx:23.0.5")
implementation("com.google.firebase:firebase-analytics-ktx:21.0.0")

Expand All @@ -104,10 +104,10 @@ dependencies {
implementation("dev.chrisbanes.insetter:insetter:0.6.1")
implementation("androidx.core:core-ktx:1.9.0-alpha04")
//noinspection DifferentStdlibGradleVersion
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.7.0-Beta")
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.7.0-RC")
implementation("androidx.core:core:1.9.0-alpha04")
implementation("com.google.android.material:material:1.7.0-alpha01")
implementation("androidx.constraintlayout:constraintlayout:2.1.3")
implementation("androidx.constraintlayout:constraintlayout:2.1.4")
implementation("androidx.preference:preference-ktx:1.2.0")
implementation("androidx.activity:activity-ktx:1.6.0-alpha04")
implementation("androidx.fragment:fragment-ktx:1.5.0-rc01")
Expand All @@ -129,7 +129,7 @@ dependencies {
implementation(fileTree(mapOf("dir" to "libs", "include" to listOf("*.jar", "*.aar"))))

debugImplementation("androidx.compose.ui:ui-tooling:1.2.0-beta02")
implementation("org.jetbrains.kotlin:kotlin-reflect:1.7.0-Beta")
implementation("org.jetbrains.kotlin:kotlin-reflect:1.7.0-RC")

/*var grpc_version = "1.45.0"
implementation("io.grpc:grpc-android:$grpc_version")
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
131 changes: 107 additions & 24 deletions app/src/main/java/de/dertyp7214/rboardthememanager/components/XMLFile.kt
Original file line number Diff line number Diff line change
@@ -1,18 +1,33 @@
@file:Suppress("MemberVisibilityCanBePrivate")

package de.dertyp7214.rboardthememanager.components

import com.topjohnwu.superuser.io.SuFile
import de.dertyp7214.rboardthememanager.core.joinToString
import de.dertyp7214.rboardthememanager.core.readXML
import de.dertyp7214.rboardthememanager.core.writeFile
import de.dertyp7214.rboardthememanager.core.times
import de.dertyp7214.rboardthememanager.core.writeFile
import java.util.*
import kotlin.collections.HashMap

@Suppress("unused")
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)

class XMLFile(val path: String) {
private val values: HashMap<String, XMLEntry> = hashMapOf()
constructor(initMap: Map<String, Any>?) : this(null, initMap)
constructor(initString: String?) : this(initString?.readXML())
constructor(path: String, initString: String?) : this(path, initString?.readXML())

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

fun simpleMap(): Map<String, Any> {
Expand All @@ -31,14 +46,39 @@ class XMLFile(val path: String) {
}

fun writeFile() {
SuFile(path).writeFile(toString())
if (path != null) SuFile(path).writeFile(toString())
}

override fun toString(): String {
return "<?xml version='1.0' encoding='utf-8' standalone='yes' ?>\n<map>\n${
values.joinToString("\n") { "${" " * 4}${it.value}" }
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-->\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 { (_, entry) -> values[entry.name] == entry }.size == values.size
}

override fun hashCode(): Int {
var result = path?.hashCode() ?: 0
result = 31 * result + (initMap?.hashCode() ?: 0)
result = 31 * result + values.hashCode()
return result
}
}

enum class XMLType(val identifier: String) {
Expand All @@ -48,7 +88,20 @@ enum class XMLType(val identifier: String) {
INT("int"),
DOUBLE("double"),
FLOAT("float"),
SET("set")
SET("set");

companion object {
fun parseType(type: String?): XMLType? = when (type?.lowercase(Locale.getDefault())) {
"string" -> STRING
"long" -> LONG
"boolean" -> BOOLEAN
"integer", "int" -> INT
"double" -> DOUBLE
"float" -> FLOAT
"set" -> SET
else -> null
}
}
}

class XMLEntry(val name: String, _value: Any, val type: XMLType) {
Expand All @@ -64,26 +117,56 @@ class XMLEntry(val name: String, _value: Any, val type: XMLType) {

fun setValue(newValue: Any): XMLEntry {
value = if (type == XMLType.SET && newValue is String) newValue.split(",").toSet()
else newValue
else when (type) {
XMLType.BOOLEAN -> newValue.toString().lowercase().toBooleanStrictOrNull() ?: false
XMLType.DOUBLE -> newValue.toString().toDoubleOrNull() ?: .0
XMLType.FLOAT -> newValue.toString().toFloatOrNull() ?: .0f
XMLType.LONG -> newValue.toString().toLongOrNull() ?: 0L
XMLType.INT -> newValue.toString().toIntOrNull() ?: 0
else -> newValue
}
return this
}

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(rightType: Boolean): Any? =
if (!rightType) getValue()
else when (type) {
XMLType.LONG -> getValue().toLongOrNull()
XMLType.BOOLEAN -> getValue().toBooleanStrictOrNull()
XMLType.INT -> getValue().toIntOrNull()
XMLType.DOUBLE -> getValue().toDoubleOrNull()
XMLType.FLOAT -> getValue().toFloatOrNull()
else -> getValue()
}

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
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,12 @@ import androidx.fragment.app.FragmentActivity
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.preference.PreferenceManager
import com.google.android.material.button.MaterialButton
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.google.android.material.textfield.TextInputLayout
import de.dertyp7214.rboardthememanager.R
import de.dertyp7214.rboardthememanager.components.XMLEntry
import de.dertyp7214.rboardthememanager.components.XMLType
import de.dertyp7214.rboardthememanager.data.ThemeDataClass

val Activity.preferences: SharedPreferences
Expand Down Expand Up @@ -197,6 +201,53 @@ fun Activity.openLoadingDialog(@StringRes message: Int) =
findViewById<TextView>(R.id.message).setText(message)
}

fun Activity.openXMLEntryDialog(block: DialogInterface.(XMLEntry) -> Unit) =
openDialog(R.layout.xml_entry_dialog_layout) { dialog ->
val xmlNameInput = findViewById<TextInputLayout>(R.id.xmlName)
val xmlValueInput = findViewById<TextInputLayout>(R.id.xmlValue)
val xmlTypeInput = findViewById<TextInputLayout>(R.id.xmlType)

val positiveButton = findViewById<MaterialButton>(R.id.positive)
val negativeButton = findViewById<MaterialButton>(R.id.negative)

fun validate(inputLayout: TextInputLayout) =
if (inputLayout.value.isNullOrEmpty()) {
inputLayout.error = getString(R.string.not_empty)
false
} else true

fun getValues(inputLayouts: Map<String, TextInputLayout>): Map<String, String>? =
if (inputLayouts.all { validate(it.value) }) inputLayouts.map {
Pair(
it.key,
it.value.value ?: ""
)
}.toMap()
else null

positiveButton.setOnClickListener {
getValues(
mapOf(
"name" to xmlNameInput,
"value" to xmlValueInput,
"type" to xmlTypeInput
)
)?.let {
block(
dialog,
XMLEntry(
it["name"] ?: "",
it["value"] ?: "",
XMLType.parseType(it["type"]) ?: XMLType.STRING
)
)
dialog.dismiss()
}
}

negativeButton.setOnClickListener { dialog.dismiss() }
}

fun Activity.openDialog(
@LayoutRes layout: Int,
cancelable: Boolean = true,
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
Loading

0 comments on commit 83977ce

Please sign in to comment.