diff --git a/app/src/main/java/cy/agorise/bitsybitshareswallet/activities/MainActivity.kt b/app/src/main/java/cy/agorise/bitsybitshareswallet/activities/MainActivity.kt index d968721..92a619a 100644 --- a/app/src/main/java/cy/agorise/bitsybitshareswallet/activities/MainActivity.kt +++ b/app/src/main/java/cy/agorise/bitsybitshareswallet/activities/MainActivity.kt @@ -1,7 +1,7 @@ package cy.agorise.bitsybitshareswallet.activities -import android.content.res.Resources import android.os.Bundle +import android.os.Handler import android.preference.PreferenceManager import android.view.MenuItem import androidx.navigation.findNavController @@ -21,6 +21,10 @@ class MainActivity : ConnectedActivity() { private lateinit var appBarConfiguration : AppBarConfiguration + // Handler and Runnable used to add a timer for user inaction and close the app if enough time has passed + private lateinit var mHandler: Handler + private lateinit var mRunnable: Runnable + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) // Sets the theme to night mode if it has been selected by the user @@ -42,13 +46,33 @@ class MainActivity : ConnectedActivity() { setupActionBarWithNavController(navController, appBarConfiguration) - navController.addOnDestinationChangedListener { _, destination, _ -> - val dest: String = try { - resources.getResourceName(destination.id) - } catch (e: Resources.NotFoundException) { - Integer.toString(destination.id) - } + mHandler = Handler() + mRunnable = Runnable { + if (PreferenceManager.getDefaultSharedPreferences(this) + .getBoolean(Constants.KEY_AUTO_CLOSE_ACTIVATED, false)) + finish() + else + restartHandler() } + startHandler() + } + + override fun onUserInteraction() { + super.onUserInteraction() + restartHandler() + } + + private fun restartHandler() { + stopHandler() + startHandler() + } + + private fun stopHandler() { + mHandler.removeCallbacks(mRunnable) + } + + private fun startHandler() { + mHandler.postDelayed(mRunnable, 3 * 60 * 1000) //for 3 minutes } override fun onOptionsItemSelected(item: MenuItem): Boolean { diff --git a/app/src/main/java/cy/agorise/bitsybitshareswallet/fragments/SettingsFragment.kt b/app/src/main/java/cy/agorise/bitsybitshareswallet/fragments/SettingsFragment.kt index 327e77a..42728f4 100644 --- a/app/src/main/java/cy/agorise/bitsybitshareswallet/fragments/SettingsFragment.kt +++ b/app/src/main/java/cy/agorise/bitsybitshareswallet/fragments/SettingsFragment.kt @@ -25,9 +25,23 @@ class SettingsFragment : Fragment() { override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) + initAutoCloseSwitch() + initNightModeSwitch() } + private fun initAutoCloseSwitch() { + val autoCloseOn = PreferenceManager.getDefaultSharedPreferences(context) + .getBoolean(Constants.KEY_AUTO_CLOSE_ACTIVATED, false) + + switchAutoClose.isChecked = autoCloseOn + + switchAutoClose.setOnCheckedChangeListener { buttonView, isChecked -> + PreferenceManager.getDefaultSharedPreferences(buttonView.context).edit() + .putBoolean(Constants.KEY_AUTO_CLOSE_ACTIVATED, isChecked).apply() + } + } + private fun initNightModeSwitch() { val nightModeOn = PreferenceManager.getDefaultSharedPreferences(context) .getBoolean(Constants.KEY_NIGHT_MODE_ACTIVATED, false) diff --git a/app/src/main/java/cy/agorise/bitsybitshareswallet/utils/Constants.kt b/app/src/main/java/cy/agorise/bitsybitshareswallet/utils/Constants.kt index 1226d68..990228d 100644 --- a/app/src/main/java/cy/agorise/bitsybitshareswallet/utils/Constants.kt +++ b/app/src/main/java/cy/agorise/bitsybitshareswallet/utils/Constants.kt @@ -44,16 +44,15 @@ object Constants { */ const val MISSING_PAYMENT_CHECK_PERIOD: Long = 5000 - /** - * Time period to wait to send a request to the NetworkService, and retry in case it is still not connected - */ + /** Time period to wait to send a request to the NetworkService, and retry in case it is still not connected */ const val NETWORK_SERVICE_RETRY_PERIOD: Long = 5000 - /** - * Key used to store the number of operations that the currently selected account had last time we checked - */ + /** Key used to store the number of operations that the currently selected account had last time we checked */ const val KEY_ACCOUNT_OPERATION_COUNT = "key_account_operation_count" + /** Key used to store the auto close app if no user activity setting into the shared preferences */ + const val KEY_AUTO_CLOSE_ACTIVATED = "key_auto_close_activated" + /** Key used to store the night mode setting into the shared preferences */ const val KEY_NIGHT_MODE_ACTIVATED = "key_night_mode_activated" } diff --git a/app/src/main/res/layout/fragment_settings.xml b/app/src/main/res/layout/fragment_settings.xml index b2ca929..11a1402 100644 --- a/app/src/main/res/layout/fragment_settings.xml +++ b/app/src/main/res/layout/fragment_settings.xml @@ -19,7 +19,7 @@ android:textStyle="bold"/>