Initial commit

This commit is contained in:
MM20
2021-09-18 23:37:52 +02:00
commit 749e4e3073
938 changed files with 50475 additions and 0 deletions
+1
View File
@@ -0,0 +1 @@
/build
+50
View File
@@ -0,0 +1,50 @@
plugins {
id("com.android.library")
id("kotlin-android")
id("kotlin-android-extensions")
}
android {
compileSdk = sdk.versions.compileSdk.get().toInt()
defaultConfig {
minSdk = sdk.versions.minSdk.get().toInt()
targetSdk = sdk.versions.targetSdk.get().toInt()
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles("consumer-rules.pro")
}
buildTypes {
release {
isMinifyEnabled = false
proguardFiles(
getDefaultProguardFile("proguard-android-optimize.txt"),
"proguard-rules.pro"
)
}
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = "1.8"
}
}
dependencies {
implementation(libs.bundles.kotlin)
implementation(libs.androidx.core)
implementation(libs.androidx.appcompat)
implementation(libs.bundles.androidx.lifecycle)
implementation(libs.mathparser)
implementation(project(":preferences"))
implementation(project(":search"))
}
View File
+21
View File
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.kts.kts.kts.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html
# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}
# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable
# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
+5
View File
@@ -0,0 +1,5 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="de.mm20.launcher2.calculator">
/
</manifest>
@@ -0,0 +1,64 @@
package de.mm20.launcher2.calculator
import androidx.lifecycle.MutableLiveData
import de.mm20.launcher2.preferences.LauncherPreferences
import de.mm20.launcher2.search.BaseSearchableRepository
import de.mm20.launcher2.search.data.Calculator
import org.mariuszgromada.math.mxparser.Expression
class CalculatorRepository private constructor() : BaseSearchableRepository() {
val calculator = MutableLiveData<Calculator?>()
override suspend fun search(query: String) {
if (query.isBlank()) {
calculator.value = null
return
}
if (!LauncherPreferences.instance.searchCalculator) return
val calc = when {
query.matches(Regex("0x[0-9a-fA-F]+")) -> {
val solution = query.substring(2).toIntOrNull(16) ?: run {
calculator.value = null
return
}
Calculator(term = query, solution = solution.toDouble())
}
query.matches(Regex("0b[01]+")) -> {
val solution = query.substring(2).toIntOrNull(2) ?: run {
calculator.value = null
return
}
Calculator(term = query, solution = solution.toDouble())
}
query.matches(Regex("0[0-7]+")) -> {
val solution = query.substring(1).toIntOrNull(8) ?: run {
calculator.value = null
return
}
Calculator(term = query, solution = solution.toDouble())
}
else -> {
val exp = Expression(query)
if (exp.checkSyntax()) {
Calculator(term = query, solution = exp.calculate())
} else {
val exp2 = Expression(query.replace(',', '.').replace(';', ','))
if (exp2.checkSyntax()) {
Calculator(term = query, solution = exp2.calculate())
} else null
}
}
}
calculator.value = calc
}
companion object {
private lateinit var instance: CalculatorRepository
fun getInstance(): CalculatorRepository {
if (!::instance.isInitialized) instance = CalculatorRepository()
return instance
}
}
}
@@ -0,0 +1,7 @@
package de.mm20.launcher2.calculator
import androidx.lifecycle.ViewModel
class CalculatorViewModel: ViewModel() {
val calculator = CalculatorRepository.getInstance().calculator
}
@@ -0,0 +1,89 @@
package de.mm20.launcher2.search.data
import java.text.DecimalFormat
import java.util.*
import kotlin.math.abs
import kotlin.math.roundToInt
class Calculator(
val term: String,
val solution: Double
) {
val formattedString: String
val formattedBinaryString: String
val formattedHexString: String
val formattedOctString: String
init {
if (solution.isNaN()) {
formattedString = "NaN"
formattedOctString = "NaN"
formattedBinaryString = "NaN"
formattedHexString = "NaN"
} else {
val nf =
if ((abs(solution) > 1e12 || abs(solution) < 1e-5) && solution != 0.0) DecimalFormat(
"#.######E0"
)
else DecimalFormat("#,###.######")
formattedString = nf.format(solution)
var s = StringBuffer(solution.roundToInt().toString(2))
while (s.length % 4 != 0) {
s = s.insert(0, '0')
}
for (i in s.length - 4 downTo 4 step 4) {
s.insert(i, ' ')
}
formattedBinaryString = s.toString()
s = StringBuffer(solution.roundToInt().toString(8))
while (s.length % 3 != 0) {
s = s.insert(0, '0')
}
for (i in s.length - 3 downTo 3 step 3) {
s.insert(i, ' ')
}
formattedOctString = s.toString()
s = StringBuffer(solution.roundToInt().toString(16).toUpperCase())
while (s.length % 2 != 0) {
s = s.insert(0, '0')
}
for (i in s.length - 2 downTo 2 step 2) {
s.insert(i, ' ')
}
formattedHexString = s.toString()
}
}
fun getBeatifiedTerm(): String {
if(term.matches(Regex("0x[0-9a-fA-F]+"))) {
return term.substring(2).toUpperCase(Locale.ROOT) + "₁₆"
}
if(term.matches(Regex("0b[01]+"))) {
return term.substring(2) + ""
}
if(term.matches(Regex("0[0-7]+"))) {
return term.substring(1) + ""
}
return term.replace(Regex("\\s+"), "")
.replace("pi", " \u03C0 ", ignoreCase = true)
.replace("*", " \u00D7 ")
.replace("-", " \u2212 ")
.replace("/", " \u2215 ")
.replace("+", " + ")
.replace(Regex("&{1,2}"), " \u2227 ")
.replace(Regex("\\|{1,2}"), " \u2228 ")
.replace("!=", " \u2260 ")
.replace("<>", " \u2260 ")
.replace(">=", " \u2265 ")
.replace("<=", " \u2264 ")
.replace("=", " = ")
.replace("<", " < ")
.replace(">", " > ")
}
}