✨
This commit is contained in:
@@ -1,14 +1,11 @@
|
|||||||
package devs.org.calculator.activities
|
package devs.org.calculator.activities
|
||||||
|
|
||||||
import android.app.Activity
|
|
||||||
import android.content.Context
|
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
import android.content.pm.PackageManager
|
import android.content.pm.PackageManager
|
||||||
import android.net.Uri
|
import android.net.Uri
|
||||||
import android.os.Build
|
import android.os.Build
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import android.os.Environment
|
import android.os.Environment
|
||||||
import android.provider.Settings
|
|
||||||
import android.util.Log
|
import android.util.Log
|
||||||
import android.widget.Toast
|
import android.widget.Toast
|
||||||
import androidx.activity.result.ActivityResultLauncher
|
import androidx.activity.result.ActivityResultLauncher
|
||||||
@@ -40,7 +37,6 @@ class MainActivity : AppCompatActivity(), DialogActionsCallback {
|
|||||||
binding = ActivityMainBinding.inflate(layoutInflater)
|
binding = ActivityMainBinding.inflate(layoutInflater)
|
||||||
setContentView(binding.root)
|
setContentView(binding.root)
|
||||||
|
|
||||||
// Initialize ActivityResultLauncher
|
|
||||||
launcher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
|
launcher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
|
||||||
handleActivityResult(result)
|
handleActivityResult(result)
|
||||||
}
|
}
|
||||||
@@ -57,7 +53,6 @@ class MainActivity : AppCompatActivity(), DialogActionsCallback {
|
|||||||
this
|
this
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
// Number buttons
|
|
||||||
setupNumberButton(binding.btn0, "0")
|
setupNumberButton(binding.btn0, "0")
|
||||||
setupNumberButton(binding.btn1, "1")
|
setupNumberButton(binding.btn1, "1")
|
||||||
setupNumberButton(binding.btn2, "2")
|
setupNumberButton(binding.btn2, "2")
|
||||||
@@ -68,14 +63,11 @@ class MainActivity : AppCompatActivity(), DialogActionsCallback {
|
|||||||
setupNumberButton(binding.btn7, "7")
|
setupNumberButton(binding.btn7, "7")
|
||||||
setupNumberButton(binding.btn8, "8")
|
setupNumberButton(binding.btn8, "8")
|
||||||
setupNumberButton(binding.btn9, "9")
|
setupNumberButton(binding.btn9, "9")
|
||||||
|
|
||||||
// Operator buttons
|
|
||||||
setupOperatorButton(binding.btnPlus, "+")
|
setupOperatorButton(binding.btnPlus, "+")
|
||||||
setupOperatorButton(binding.btnMinus, "-")
|
setupOperatorButton(binding.btnMinus, "-")
|
||||||
setupOperatorButton(binding.btnMultiply, "*")
|
setupOperatorButton(binding.btnMultiply, "×")
|
||||||
setupOperatorButton(binding.btnDivide, "/")
|
setupOperatorButton(binding.btnDivide, "/")
|
||||||
|
|
||||||
// Special buttons
|
|
||||||
binding.btnClear.setOnClickListener { clearDisplay() }
|
binding.btnClear.setOnClickListener { clearDisplay() }
|
||||||
binding.btnDot.setOnClickListener { addDecimal() }
|
binding.btnDot.setOnClickListener { addDecimal() }
|
||||||
binding.btnEquals.setOnClickListener { calculateResult() }
|
binding.btnEquals.setOnClickListener { calculateResult() }
|
||||||
@@ -88,8 +80,6 @@ class MainActivity : AppCompatActivity(), DialogActionsCallback {
|
|||||||
result.data?.data?.let { uri ->
|
result.data?.data?.let { uri ->
|
||||||
baseDocumentTreeUri = uri
|
baseDocumentTreeUri = uri
|
||||||
val takeFlags = Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION
|
val takeFlags = Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION
|
||||||
|
|
||||||
// Take persistable Uri Permission for future use
|
|
||||||
contentResolver.takePersistableUriPermission(uri, takeFlags)
|
contentResolver.takePersistableUriPermission(uri, takeFlags)
|
||||||
|
|
||||||
val preferences = getSharedPreferences("com.example.fileutility", MODE_PRIVATE)
|
val preferences = getSharedPreferences("com.example.fileutility", MODE_PRIVATE)
|
||||||
@@ -115,7 +105,10 @@ class MainActivity : AppCompatActivity(), DialogActionsCallback {
|
|||||||
private fun setupOperatorButton(button: MaterialButton, operator: String) {
|
private fun setupOperatorButton(button: MaterialButton, operator: String) {
|
||||||
button.setOnClickListener {
|
button.setOnClickListener {
|
||||||
if (!lastWasOperator) {
|
if (!lastWasOperator) {
|
||||||
currentExpression += operator
|
currentExpression += when (operator) {
|
||||||
|
"×" -> "*"
|
||||||
|
else -> operator
|
||||||
|
}
|
||||||
lastWasOperator = true
|
lastWasOperator = true
|
||||||
hasDecimal = false
|
hasDecimal = false
|
||||||
}
|
}
|
||||||
@@ -123,6 +116,7 @@ class MainActivity : AppCompatActivity(), DialogActionsCallback {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private fun clearDisplay() {
|
private fun clearDisplay() {
|
||||||
currentExpression = "0"
|
currentExpression = "0"
|
||||||
binding.total.text = ""
|
binding.total.text = ""
|
||||||
@@ -150,8 +144,7 @@ class MainActivity : AppCompatActivity(), DialogActionsCallback {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun calculateResult() {
|
private fun calculateResult() {
|
||||||
// Check for secret code
|
if (currentExpression == "123456") {
|
||||||
if (currentExpression == "123456") { // Replace with your desired code
|
|
||||||
val intent = Intent(this, SetupPasswordActivity::class.java)
|
val intent = Intent(this, SetupPasswordActivity::class.java)
|
||||||
intent.putExtra("password", currentExpression)
|
intent.putExtra("password", currentExpression)
|
||||||
startActivity(intent)
|
startActivity(intent)
|
||||||
@@ -159,7 +152,6 @@ class MainActivity : AppCompatActivity(), DialogActionsCallback {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Validate password
|
|
||||||
if (PrefsUtil(this).validatePassword(currentExpression)) {
|
if (PrefsUtil(this).validatePassword(currentExpression)) {
|
||||||
val intent = Intent(this, HiddenVaultActivity::class.java)
|
val intent = Intent(this, HiddenVaultActivity::class.java)
|
||||||
intent.putExtra("password", currentExpression)
|
intent.putExtra("password", currentExpression)
|
||||||
@@ -169,6 +161,7 @@ class MainActivity : AppCompatActivity(), DialogActionsCallback {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
currentExpression = currentExpression.replace("×", "*")
|
||||||
val expression = ExpressionBuilder(currentExpression).build()
|
val expression = ExpressionBuilder(currentExpression).build()
|
||||||
val result = expression.evaluate()
|
val result = expression.evaluate()
|
||||||
|
|
||||||
@@ -180,25 +173,26 @@ class MainActivity : AppCompatActivity(), DialogActionsCallback {
|
|||||||
|
|
||||||
lastWasOperator = false
|
lastWasOperator = false
|
||||||
hasDecimal = currentExpression.contains(".")
|
hasDecimal = currentExpression.contains(".")
|
||||||
|
|
||||||
updateDisplay()
|
updateDisplay()
|
||||||
|
binding.total.text = ""
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
binding.display.text = getString(R.string.invalid_message)
|
binding.display.text = getString(R.string.invalid_message)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun updateDisplay() {
|
|
||||||
binding.display.text = currentExpression
|
|
||||||
|
|
||||||
if (currentExpression == "0"){
|
private fun updateDisplay() {
|
||||||
|
binding.display.text = currentExpression.replace("*", "×")
|
||||||
|
|
||||||
|
if (currentExpression == "0") {
|
||||||
binding.total.text = ""
|
binding.total.text = ""
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
// Evaluate the expression and update total
|
|
||||||
try {
|
try {
|
||||||
val expression = ExpressionBuilder(currentExpression).build()
|
val expression = ExpressionBuilder(currentExpression).build()
|
||||||
val result = expression.evaluate()
|
val result = expression.evaluate()
|
||||||
|
|
||||||
// Format the result and update total.text
|
|
||||||
val formattedResult = if (result.toLong().toDouble() == result) {
|
val formattedResult = if (result.toLong().toDouble() == result) {
|
||||||
result.toLong().toString()
|
result.toLong().toString()
|
||||||
} else {
|
} else {
|
||||||
@@ -207,11 +201,11 @@ class MainActivity : AppCompatActivity(), DialogActionsCallback {
|
|||||||
|
|
||||||
binding.total.text = formattedResult
|
binding.total.text = formattedResult
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
// Show a blank or placeholder for invalid expressions
|
|
||||||
binding.total.text = ""
|
binding.total.text = ""
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private fun cutNumbers() {
|
private fun cutNumbers() {
|
||||||
if (currentExpression.isNotEmpty()){
|
if (currentExpression.isNotEmpty()){
|
||||||
if (currentExpression.length == 1){
|
if (currentExpression.length == 1){
|
||||||
|
|||||||
@@ -15,7 +15,6 @@
|
|||||||
android:layout_margin="16dp"
|
android:layout_margin="16dp"
|
||||||
android:layout_weight="3"
|
android:layout_weight="3"
|
||||||
android:layout_marginTop="0dp"
|
android:layout_marginTop="0dp"
|
||||||
android:elevation="4dp"
|
|
||||||
android:orientation="vertical"
|
android:orientation="vertical"
|
||||||
android:gravity="right|bottom">
|
android:gravity="right|bottom">
|
||||||
<TextView
|
<TextView
|
||||||
@@ -108,10 +107,12 @@
|
|||||||
android:layout_rowWeight="1"
|
android:layout_rowWeight="1"
|
||||||
android:layout_columnWeight="1"
|
android:layout_columnWeight="1"
|
||||||
android:textSize="30sp"
|
android:textSize="30sp"
|
||||||
|
android:gravity="center"
|
||||||
android:layout_margin="4dp"
|
android:layout_margin="4dp"
|
||||||
app:icon="@drawable/backspace"
|
app:icon="@drawable/backspace"
|
||||||
|
android:textAlignment="center"
|
||||||
app:layout_constraintDimensionRatio="1:1"
|
app:layout_constraintDimensionRatio="1:1"
|
||||||
app:iconSize="30dp"
|
app:iconSize="32dp"
|
||||||
app:cornerRadius="15dp"/>
|
app:cornerRadius="15dp"/>
|
||||||
<!-- Row 2 -->
|
<!-- Row 2 -->
|
||||||
<com.google.android.material.button.MaterialButton
|
<com.google.android.material.button.MaterialButton
|
||||||
|
|||||||
@@ -4,17 +4,8 @@
|
|||||||
<color name="white">#FFFFFFFF</color>
|
<color name="white">#FFFFFFFF</color>
|
||||||
|
|
||||||
<!-- Material You dynamic colors -->
|
<!-- Material You dynamic colors -->
|
||||||
<color name="primary">@android:color/system_accent1_600</color>
|
<color name="primary">#00B43B</color>
|
||||||
<color name="on_primary">@android:color/system_accent1_0</color>
|
<color name="colorPrimaryVariant">#00C15C</color>
|
||||||
<color name="primary_container">@android:color/system_accent1_100</color>
|
<color name="colorSecondary">#39FF97</color>
|
||||||
<color name="on_primary_container">@android:color/system_accent1_900</color>
|
|
||||||
|
|
||||||
<color name="secondary">@android:color/system_accent2_600</color>
|
|
||||||
<color name="on_secondary">@android:color/system_accent2_0</color>
|
|
||||||
<color name="secondary_container">@android:color/system_accent2_100</color>
|
|
||||||
<color name="on_secondary_container">@android:color/system_accent2_900</color>
|
|
||||||
|
|
||||||
<color name="surface">@android:color/system_neutral1_50</color>
|
|
||||||
<color name="on_surface">@android:color/system_neutral1_900</color>
|
|
||||||
<color name="textColor">#ffffff</color>
|
<color name="textColor">#ffffff</color>
|
||||||
</resources>
|
</resources>
|
||||||
@@ -2,13 +2,13 @@
|
|||||||
<!-- Base application theme. -->
|
<!-- Base application theme. -->
|
||||||
<style name="Base.Theme.Calculator" parent="Theme.Material3.DayNight.NoActionBar">
|
<style name="Base.Theme.Calculator" parent="Theme.Material3.DayNight.NoActionBar">
|
||||||
<!-- Primary brand color -->
|
<!-- Primary brand color -->
|
||||||
<item name="colorPrimary">@android:color/system_accent1_200</item>
|
<item name="colorPrimary">@color/primary</item>
|
||||||
<item name="colorPrimaryVariant">@android:color/system_accent1_300</item>
|
<item name="colorPrimaryVariant">@color/colorPrimaryVariant</item>
|
||||||
<item name="colorOnPrimary">@color/black</item>
|
<item name="colorOnPrimary">@color/black</item>
|
||||||
|
<item name="android:background">@color/black</item>
|
||||||
<!-- Secondary brand color -->
|
<!-- Secondary brand color -->
|
||||||
<item name="colorSecondary">@android:color/system_accent2_200</item>
|
<item name="colorSecondary">@color/colorSecondary</item>
|
||||||
<item name="colorSecondaryVariant">@android:color/system_accent2_300</item>
|
<item name="colorSecondaryVariant">@color/colorSecondary</item>
|
||||||
<item name="colorOnSecondary">@color/black</item>
|
<item name="colorOnSecondary">@color/black</item>
|
||||||
<item name="fontFamily">@font/ubuntu_regular</item>
|
<item name="fontFamily">@font/ubuntu_regular</item>
|
||||||
<!-- Status bar color -->
|
<!-- Status bar color -->
|
||||||
@@ -17,6 +17,6 @@
|
|||||||
|
|
||||||
<!-- Enable window decor fitting -->
|
<!-- Enable window decor fitting -->
|
||||||
<item name="android:windowLightStatusBar">false</item>
|
<item name="android:windowLightStatusBar">false</item>
|
||||||
<item name="android:windowLightNavigationBar">false</item>
|
<item name="android:windowLightNavigationBar" tools:targetApi="o_mr1">false</item>
|
||||||
</style>
|
</style>
|
||||||
</resources>
|
</resources>
|
||||||
@@ -4,17 +4,8 @@
|
|||||||
<color name="white">#FFFFFFFF</color>
|
<color name="white">#FFFFFFFF</color>
|
||||||
|
|
||||||
<!-- Material You dynamic colors -->
|
<!-- Material You dynamic colors -->
|
||||||
<color name="primary">@android:color/system_accent1_600</color>
|
<color name="primary">#00B43B</color>
|
||||||
<color name="on_primary">@android:color/system_accent1_0</color>
|
<color name="colorPrimaryVariant">#00C15C</color>
|
||||||
<color name="primary_container">@android:color/system_accent1_100</color>
|
<color name="colorSecondary">#39FF97</color>
|
||||||
<color name="on_primary_container">@android:color/system_accent1_900</color>
|
|
||||||
|
|
||||||
<color name="secondary">@android:color/system_accent2_600</color>
|
|
||||||
<color name="on_secondary">@android:color/system_accent2_0</color>
|
|
||||||
<color name="secondary_container">@android:color/system_accent2_100</color>
|
|
||||||
<color name="on_secondary_container">@android:color/system_accent2_900</color>
|
|
||||||
|
|
||||||
<color name="surface">@android:color/system_neutral1_50</color>
|
|
||||||
<color name="on_surface">@android:color/system_neutral1_900</color>
|
|
||||||
<color name="textColor">#000000</color>
|
<color name="textColor">#000000</color>
|
||||||
</resources>
|
</resources>
|
||||||
@@ -2,24 +2,21 @@
|
|||||||
<!-- Base application theme. -->
|
<!-- Base application theme. -->
|
||||||
<style name="Base.Theme.Calculator" parent="Theme.Material3.DayNight.NoActionBar">
|
<style name="Base.Theme.Calculator" parent="Theme.Material3.DayNight.NoActionBar">
|
||||||
<!-- Primary brand color -->
|
<!-- Primary brand color -->
|
||||||
<item name="colorPrimary">@android:color/system_accent1_600</item>
|
<item name="colorPrimary">@color/primary</item>
|
||||||
<item name="colorPrimaryVariant">@android:color/system_accent1_700</item>
|
<item name="colorPrimaryVariant">@color/colorPrimaryVariant</item>
|
||||||
<item name="colorOnPrimary">@color/white</item>
|
<item name="colorOnPrimary">@color/white</item>
|
||||||
<item name="android:textColor"></item>
|
|
||||||
<item name="fontFamily">@font/ubuntu_regular</item>
|
|
||||||
|
|
||||||
<!-- Secondary brand color -->
|
<!-- Secondary brand color -->
|
||||||
<item name="colorSecondary">@android:color/system_accent2_600</item>
|
<item name="colorSecondary">@color/colorSecondary</item>
|
||||||
<item name="colorSecondaryVariant">@android:color/system_accent2_700</item>
|
<item name="colorSecondaryVariant">@color/colorSecondary</item>
|
||||||
<item name="colorOnSecondary">@color/black</item>
|
<item name="colorOnSecondary">@color/white</item>
|
||||||
|
<item name="fontFamily">@font/ubuntu_regular</item>
|
||||||
<!-- Status bar color -->
|
<!-- Status bar color -->
|
||||||
<item name="android:statusBarColor">@android:color/transparent</item>
|
<item name="android:statusBarColor">@android:color/transparent</item>
|
||||||
<item name="android:navigationBarColor">@android:color/transparent</item>
|
<item name="android:navigationBarColor">@android:color/transparent</item>
|
||||||
|
|
||||||
<!-- Enable window decor fitting -->
|
<!-- Enable window decor fitting -->
|
||||||
<item name="android:windowLightStatusBar">true</item>
|
<item name="android:windowLightStatusBar">true</item>
|
||||||
<item name="android:windowLightNavigationBar">true</item>
|
<item name="android:windowLightNavigationBar" tools:targetApi="o_mr1">true</item>
|
||||||
</style>
|
</style>
|
||||||
|
|
||||||
<style name="Theme.Calculator" parent="Base.Theme.Calculator" />
|
<style name="Theme.Calculator" parent="Base.Theme.Calculator" />
|
||||||
|
|||||||
Reference in New Issue
Block a user