Skip to content

Commit

Permalink
Implement calculator pin code query
Browse files Browse the repository at this point in the history
  • Loading branch information
tuomas2 committed Oct 6, 2022
1 parent 6cfecf5 commit d3a5ecf
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package net.bible.android.view.activity
import android.annotation.SuppressLint
import android.app.Activity
import android.app.AlertDialog
import android.app.Instrumentation
import android.content.Context
import android.content.Intent
import android.os.Build
Expand Down Expand Up @@ -201,6 +202,12 @@ open class StartupActivity : CustomTitlebarActivityBase() {
// switch back to ui thread to continue
withContext(Dispatchers.Main) {
postBasicInitialisationControl()
if(CommonUtils.settings.getBoolean("show_calculator", false)) {
spinnerBinding.imageView.setImageDrawable(
CommonUtils.getTintedDrawable(R.drawable.ic_baseline_calculate_24, R.color.grey_500)
)
spinnerBinding.splashTitleText.text = getString(R.string.app_name_calculator)
}
}
}
}
Expand Down Expand Up @@ -228,8 +235,7 @@ open class StartupActivity : CustomTitlebarActivityBase() {
} else {
Log.i(TAG, "Going to main bible view")

gotoCalculator()
//gotoMainBibleActivity()
gotoMainBibleActivity()
spinnerBinding.progressText.text =getString(R.string.initializing_app)
}
}
Expand Down Expand Up @@ -305,10 +311,13 @@ open class StartupActivity : CustomTitlebarActivityBase() {
startActivityForResult(handlerIntent, DOWNLOAD_DOCUMENT_REQUEST)
}

private fun gotoCalculator() {
Log.i(TAG, "Going to Calculator")
val handlerIntent = Intent(this, CalculatorActivity::class.java)
startActivity(handlerIntent)
private suspend fun gotoCalculator() {
val show = CommonUtils.settings.getBoolean("show_calculator", false)
if(show) {
Log.i(TAG, "Going to Calculator")
val handlerIntent = Intent(this, CalculatorActivity::class.java)
while(awaitIntent(handlerIntent)?.resultCode != RESULT_OK) {}
}
}

private fun gotoMainBibleActivity() {
Expand All @@ -326,6 +335,7 @@ open class StartupActivity : CustomTitlebarActivityBase() {
}
}

gotoCalculator()
startActivity(handlerIntent)
finish()
}
Expand All @@ -338,40 +348,43 @@ open class StartupActivity : CustomTitlebarActivityBase() {
Log.i(TAG, "Activity result:$resultCode")
super.onActivityResult(requestCode, resultCode, data)

if (requestCode == DOWNLOAD_DOCUMENT_REQUEST) {
Log.i(TAG, "Returned from Download")
if (swordDocumentFacade.bibles.isNotEmpty()) {
Log.i(TAG, "Bibles now exist so go to main bible view")
// select appropriate default verse e.g. John 3.16 if NT only
GlobalScope.launch(Dispatchers.Main) {
gotoMainBibleActivity()
}
when (requestCode) {
DOWNLOAD_DOCUMENT_REQUEST -> {
Log.i(TAG, "Returned from Download")
if (swordDocumentFacade.bibles.isNotEmpty()) {
Log.i(TAG, "Bibles now exist so go to main bible view")
// select appropriate default verse e.g. John 3.16 if NT only
GlobalScope.launch(Dispatchers.Main) {
gotoMainBibleActivity()
}

} else {
Log.i(TAG, "No Bibles exist so start again")
GlobalScope.launch(Dispatchers.Main) {
postBasicInitialisationControl()
} else {
Log.i(TAG, "No Bibles exist so start again")
GlobalScope.launch(Dispatchers.Main) {
postBasicInitialisationControl()
}
}
}
} else if (requestCode == REQUEST_PICK_FILE_FOR_BACKUP_RESTORE) {
// this and the one in MainActivity could potentially be merged into the same thing
if (resultCode == Activity.RESULT_OK) {
CurrentActivityHolder.currentActivity = this
Dialogs.showMsg(R.string.restore_confirmation, true) {
ABEventBus.post(ToastEvent(getString(R.string.loading_backup)))
val hourglass = Hourglass(this)
GlobalScope.launch(Dispatchers.IO) {
hourglass.show()
val inputStream = contentResolver.openInputStream(data!!.data!!)
if (BackupControl.restoreDatabaseViaIntent(inputStream!!)) {
Log.i(TAG, "Restored database successfully")

withContext(Dispatchers.Main) {
Dialogs.showMsg(R.string.restore_success)
postBasicInitialisationControl()
REQUEST_PICK_FILE_FOR_BACKUP_RESTORE -> {
// this and the one in MainActivity could potentially be merged into the same thing
if (resultCode == Activity.RESULT_OK) {
CurrentActivityHolder.currentActivity = this
Dialogs.showMsg(R.string.restore_confirmation, true) {
ABEventBus.post(ToastEvent(getString(R.string.loading_backup)))
val hourglass = Hourglass(this)
GlobalScope.launch(Dispatchers.IO) {
hourglass.show()
val inputStream = contentResolver.openInputStream(data!!.data!!)
if (BackupControl.restoreDatabaseViaIntent(inputStream!!)) {
Log.i(TAG, "Restored database successfully")

withContext(Dispatchers.Main) {
Dialogs.showMsg(R.string.restore_success)
postBasicInitialisationControl()
}
}
hourglass.dismiss()
}
hourglass.dismiss()
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import android.app.Activity
import android.util.Log
import net.bible.android.control.event.ABEventBus
import net.bible.android.control.event.apptobackground.AppToBackgroundEvent
import net.bible.service.common.CommonUtils

/** Allow operations form middle tier that require a reference to the current Activity
*
Expand Down Expand Up @@ -48,6 +49,10 @@ object CurrentActivityHolder {
appIsInForeground = false
ABEventBus
.post(AppToBackgroundEvent(AppToBackgroundEvent.Position.BACKGROUND))
if (CommonUtils.initialized && CommonUtils.settings.getBoolean("show_calculator", false)) {
Log.d(TAG, "Closing app to start from calculator again...")
CommonUtils.forceStopApp()
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import android.view.MotionEvent
import android.view.View
import android.widget.Button
import android.widget.Toast
import net.bible.service.common.CommonUtils
import java.lang.Exception
import java.lang.NumberFormatException
import java.math.BigDecimal
Expand Down Expand Up @@ -171,7 +172,7 @@ class CalculatorActivity : AppCompatActivity(), View.OnClickListener, OnTouchLis
override fun onTouch(view: View, motionEvent: MotionEvent): Boolean {
when (motionEvent.action) {
MotionEvent.ACTION_DOWN -> {
view.background.setColorFilter(Color.RED, PorterDuff.Mode.SRC_ATOP)
view.background.setColorFilter(Color.DKGRAY, PorterDuff.Mode.SRC_ATOP)
view.invalidate()
}
MotionEvent.ACTION_UP -> {
Expand Down Expand Up @@ -314,6 +315,11 @@ class CalculatorActivity : AppCompatActivity(), View.OnClickListener, OnTouchLis
}

private fun calculate(input: String) {
val pin = CommonUtils.settings.getString("calculator_pin", "1234")
if(input == pin) {
setResult(RESULT_OK)
finish()
}
var result = ""
try {
var temp = input
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ import net.bible.android.control.event.apptobackground.AppToBackgroundEvent
import net.bible.android.control.event.passage.CurrentVerseChangedEvent
import net.bible.android.control.event.passage.PassageChangedEvent
import net.bible.android.control.event.passage.SynchronizeWindowsEvent
import net.bible.android.control.event.phonecall.PhoneCallMonitor
import net.bible.android.control.event.window.CurrentWindowChangedEvent
import net.bible.android.control.event.window.NumberOfWindowsChangedEvent
import net.bible.android.control.navigation.NavigationControl
Expand All @@ -92,6 +91,7 @@ import net.bible.android.view.activity.base.Dialogs
import net.bible.android.view.activity.base.IntentHelper
import net.bible.android.view.activity.base.SharedActivityState
import net.bible.android.view.activity.bookmark.Bookmarks
import net.bible.android.view.activity.discrete.CalculatorActivity
import net.bible.android.view.activity.navigation.ChooseDocument
import net.bible.android.view.activity.navigation.GridChoosePassageBook
import net.bible.android.view.activity.navigation.History
Expand Down
8 changes: 7 additions & 1 deletion app/src/main/java/net/bible/service/common/CommonUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import android.app.AlertDialog
import android.app.PendingIntent
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.content.SharedPreferences
import android.content.pm.PackageInfo
import android.content.pm.PackageManager
Expand Down Expand Up @@ -82,6 +83,7 @@ import net.bible.android.database.bookmarks.LabelType
import net.bible.android.database.json
import net.bible.android.view.activity.ActivityComponent
import net.bible.android.view.activity.DaggerActivityComponent
import net.bible.android.view.activity.StartupActivity
import net.bible.android.view.activity.base.ActivityBase
import net.bible.android.view.activity.base.CurrentActivityHolder
import net.bible.android.view.activity.download.DownloadActivity
Expand Down Expand Up @@ -604,14 +606,18 @@ object CommonUtils : CommonUtilsBase() {
}

fun restartApp(callingActivity: Activity) {
val contentIntent = application.packageManager.getLaunchIntentForPackage(application.packageName)
val contentIntent = Intent(callingActivity, StartupActivity::class.java)//application.packageManager.getLaunchIntentForPackage(application.packageName)
val pendingIntent = PendingIntent.getActivity(callingActivity, 0, contentIntent, if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) PendingIntent.FLAG_IMMUTABLE else 0)

val mgr = callingActivity.getSystemService(Context.ALARM_SERVICE) as AlarmManager
mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 1000, pendingIntent)
exitProcess(2)
}

fun forceStopApp() {
exitProcess(2)
}

val lastDisplaySettings: List<WorkspaceEntities.TextDisplaySettings.Types> get() {
val lastDisplaySettingsString = settings.getString("lastDisplaySettings", null)
var lastTypes = mutableListOf<WorkspaceEntities.TextDisplaySettings.Types>()
Expand Down

0 comments on commit d3a5ecf

Please sign in to comment.