taler-android

Android apps for GNU Taler (wallet, PoS, cashier)
Log | Files | Refs | README | LICENSE

MainActivity.kt (3067B)


      1 /*
      2  * This file is part of GNU Taler
      3  * (C) 2020 Taler Systems S.A.
      4  *
      5  * GNU Taler is free software; you can redistribute it and/or modify it under the
      6  * terms of the GNU General Public License as published by the Free Software
      7  * Foundation; either version 3, or (at your option) any later version.
      8  *
      9  * GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY
     10  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
     11  * A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
     12  *
     13  * You should have received a copy of the GNU General Public License along with
     14  * GNU Taler; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
     15  */
     16 
     17 package net.taler.cashier
     18 
     19 import android.content.Intent
     20 import android.content.Intent.ACTION_MAIN
     21 import android.content.Intent.CATEGORY_HOME
     22 import android.content.Intent.FLAG_ACTIVITY_NEW_TASK
     23 import android.os.Bundle
     24 import androidx.activity.viewModels
     25 import androidx.appcompat.app.AppCompatActivity
     26 import androidx.core.view.WindowCompat
     27 import androidx.navigation.NavController
     28 import androidx.navigation.fragment.NavHostFragment
     29 import net.taler.cashier.databinding.ActivityMainBinding
     30 import net.taler.lib.android.TalerNfcService
     31 
     32 class MainActivity : AppCompatActivity() {
     33 
     34     private val viewModel: MainViewModel by viewModels()
     35     private val configManager by lazy { viewModel.configManager}
     36 
     37     private lateinit var ui: ActivityMainBinding
     38     private lateinit var nav: NavController
     39 
     40     override fun onCreate(savedInstanceState: Bundle?) {
     41         super.onCreate(savedInstanceState)
     42         WindowCompat.enableEdgeToEdge(window)
     43         ui = ActivityMainBinding.inflate(layoutInflater)
     44         setContentView(ui.root)
     45         setSupportActionBar(ui.toolbar)
     46         val navHostFragment =
     47             supportFragmentManager.findFragmentById(R.id.nav_host_fragment) as NavHostFragment
     48         nav = navHostFragment.navController
     49 
     50         TalerNfcService.startService(this)
     51     }
     52 
     53     override fun onStart() {
     54         super.onStart()
     55         if (!configManager.hasConfig() && nav.currentDestination?.id != R.id.configFragment) {
     56             nav.navigate(configManager.configDestination)
     57         }
     58     }
     59 
     60     override fun onResume() {
     61         super.onResume()
     62         TalerNfcService.setDefaultHandler(this)
     63     }
     64 
     65     override fun onPause() {
     66         super.onPause()
     67         TalerNfcService.unsetDefaultHandler(this)
     68     }
     69 
     70     override fun onDestroy() {
     71         super.onDestroy()
     72         TalerNfcService.stopService(this)
     73     }
     74 
     75     @Deprecated("Deprecated in Java")
     76     override fun onBackPressed() {
     77         if (!configManager.hasConfig() && nav.currentDestination?.id == R.id.configFragment) {
     78             // we are in the configuration screen and need a config to continue
     79             val intent = Intent(ACTION_MAIN).apply {
     80                 addCategory(CATEGORY_HOME)
     81                 flags = FLAG_ACTIVITY_NEW_TASK
     82             }
     83             startActivity(intent)
     84         } else {
     85             super.onBackPressed()
     86         }
     87     }
     88 
     89 }