ProjectLaogai/app/src/main/java/org/mosad/seil0/projectlaogai/MainActivity.kt

224 lines
8.1 KiB
Kotlin
Raw Normal View History

2018-10-24 18:22:05 +02:00
/**
* ProjectLaogai
*
* Copyright 2019-2020 <seil0@mosad.xyz>
2018-10-24 18:22:05 +02:00
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*
*/
package org.mosad.seil0.projectlaogai
import android.app.PendingIntent
2019-08-21 20:43:08 +02:00
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
2019-08-17 01:51:15 +02:00
import android.nfc.NfcAdapter
2019-08-21 20:43:08 +02:00
import android.nfc.NfcManager
import android.nfc.tech.NfcA
2018-10-24 18:22:05 +02:00
import android.os.Bundle
2020-02-05 14:10:45 +01:00
import android.util.Log
2018-10-24 18:22:05 +02:00
import android.view.Menu
import android.view.MenuItem
import androidx.appcompat.app.ActionBarDrawerToggle
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.GravityCompat
import androidx.fragment.app.Fragment
2018-10-26 00:50:37 +02:00
import androidx.fragment.app.FragmentTransaction
2018-12-14 15:02:19 +01:00
import com.afollestad.aesthetic.Aesthetic
import com.afollestad.aesthetic.NavigationViewMode
import com.google.android.material.navigation.NavigationView
2018-10-24 18:22:05 +02:00
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.app_bar_main.*
import org.mosad.seil0.projectlaogai.controller.cache.CacheController
import org.mosad.seil0.projectlaogai.controller.NFCMensaCard
import org.mosad.seil0.projectlaogai.controller.PreferencesController
import org.mosad.seil0.projectlaogai.controller.PreferencesController.Companion.cColorAccent
import org.mosad.seil0.projectlaogai.controller.PreferencesController.Companion.cColorPrimary
import org.mosad.seil0.projectlaogai.controller.login.LoginController
2018-11-19 12:00:39 +01:00
import org.mosad.seil0.projectlaogai.fragments.*
import kotlin.system.measureTimeMillis
2020-01-15 15:28:07 +01:00
/**
* TODO save the current fragment to show it when the app is restarted
*/
2018-10-24 18:22:05 +02:00
class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelectedListener {
private var activeFragment: Fragment = HomeFragment() // the currently active fragment, home at the start
2020-02-05 14:10:45 +01:00
private val className = "MainActivity"
private lateinit var adapter: NfcAdapter
private lateinit var pendingIntent: PendingIntent
private lateinit var intentFiltersArray: Array<IntentFilter>
private lateinit var techListsArray: Array<Array<String>>
2019-08-21 20:43:08 +02:00
private var useNFC = false
2018-10-24 18:22:05 +02:00
override fun onCreate(savedInstanceState: Bundle?) {
val fragmentTransaction: FragmentTransaction = supportFragmentManager.beginTransaction()
2018-12-14 15:02:19 +01:00
Aesthetic.attach(this)
2018-10-24 18:22:05 +02:00
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setSupportActionBar(toolbar)
// load mensa, timetable and color
load()
initAesthetic()
initForegroundDispatch()
2018-12-14 15:02:19 +01:00
2018-10-24 18:22:05 +02:00
val toggle = ActionBarDrawerToggle(
this, drawer_layout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close
)
drawer_layout.addDrawerListener(toggle)
toggle.syncState()
nav_view.setNavigationItemSelectedListener(this)
2019-08-17 01:51:15 +02:00
// based on the inent we get, call readBalance or open a Fragment
when (intent.action) {
NfcAdapter.ACTION_TECH_DISCOVERED -> NFCMensaCard.readBalance(intent, this)
"org.mosad.seil0.projectlaogai.fragments.MensaFragment" -> activeFragment = MensaFragment()
"org.mosad.seil0.projectlaogai.fragments.TimeTableFragment" -> activeFragment = TimeTableFragment()
"org.mosad.seil0.projectlaogai.fragments.MoodleFragment" -> activeFragment = MoodleFragment()
}
// open the activeFragment, default is the HomeFragment
fragmentTransaction.replace(R.id.fragment_container, activeFragment)
fragmentTransaction.commit()
}
override fun onNewIntent(intent: Intent) {
super.onNewIntent(intent)
if (NfcAdapter.ACTION_TECH_DISCOVERED == intent.action)
NFCMensaCard.readBalance(intent, this)
2018-10-24 18:22:05 +02:00
}
2018-12-14 15:02:19 +01:00
override fun onResume() {
super.onResume()
Aesthetic.resume(this)
2019-08-21 20:43:08 +02:00
if(useNFC)
adapter.enableForegroundDispatch(this, pendingIntent, intentFiltersArray, techListsArray)
2018-12-14 15:02:19 +01:00
}
override fun onPause() {
super.onPause()
Aesthetic.pause(this)
2019-08-21 20:43:08 +02:00
if(useNFC)
adapter.disableForegroundDispatch(this)
2018-12-14 15:02:19 +01:00
}
2018-10-24 18:22:05 +02:00
override fun onBackPressed() {
if (drawer_layout.isDrawerOpen(GravityCompat.START)) {
drawer_layout.closeDrawer(GravityCompat.START)
} else {
// TODO only call on double tap
2018-10-24 18:22:05 +02:00
super.onBackPressed()
}
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
// Inflate the menu; this adds items to the action bar if it is present.
menuInflater.inflate(R.menu.main, menu)
return true
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
return when (item.itemId) {
R.id.action_settings -> true
else -> super.onOptionsItemSelected(item)
}
}
override fun onNavigationItemSelected(item: MenuItem): Boolean {
// Handle navigation view item clicks here.
2019-06-03 10:59:32 +02:00
activeFragment = when(item.itemId) {
R.id.nav_home -> HomeFragment()
R.id.nav_mensa -> MensaFragment()
R.id.nav_timetable -> TimeTableFragment()
R.id.nav_moodle -> MoodleFragment()
R.id.nav_settings -> SettingsFragment()
else -> HomeFragment()
2018-10-24 18:22:05 +02:00
}
val fragmentTransaction: FragmentTransaction = supportFragmentManager.beginTransaction()
fragmentTransaction.replace(R.id.fragment_container, activeFragment)
fragmentTransaction.commit()
2018-10-24 18:22:05 +02:00
drawer_layout.closeDrawer(GravityCompat.START)
2018-10-24 18:22:05 +02:00
return true
}
/**
* load the mensa menus of the current week
*/
private fun load() {
val startupTime = measureTimeMillis {
PreferencesController.load(this) // load the settings, must be finished before doing anything else
CacheController(this) // load the cache
LoginController.load(this)
}
2020-02-05 14:10:45 +01:00
Log.i(className, "startup completed in $startupTime ms")
}
private fun initAesthetic() {
// If we haven't set any defaults, do that now
if (Aesthetic.isFirstTime) {
// set the default theme at the first app start
Aesthetic.config {
activityTheme(R.style.AppTheme_Light)
apply()
}
// show the onboarding activity
startActivity(Intent(this, OnboardingActivity::class.java))
finish()
}
Aesthetic.config {
colorPrimary(cColorPrimary)
colorPrimaryDark(cColorPrimary)
colorAccent(cColorAccent)
navigationViewMode(NavigationViewMode.SELECTED_ACCENT)
apply()
}
}
private fun initForegroundDispatch() {
2019-08-21 20:43:08 +02:00
val nfcManager = this.getSystemService(Context.NFC_SERVICE) as NfcManager
val nfcAdapter = nfcManager.defaultAdapter
if (nfcAdapter != null) {
useNFC = true
intentFiltersArray = arrayOf(IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED).apply { addDataType("*/*") })
techListsArray = arrayOf(arrayOf(NfcA::class.java.name))
adapter = NfcAdapter.getDefaultAdapter(this)
pendingIntent = PendingIntent.getActivity(
this, 0,
Intent(this, javaClass).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0
)
}
}
2018-10-24 18:22:05 +02:00
}