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

218 lines
7.6 KiB
Kotlin

/**
* ProjectLaogai
*
* Copyright 2019 <seil0@mosad.xyz>
*
* 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.content.Intent
import android.graphics.Color
import android.nfc.NdefMessage
import android.nfc.NfcAdapter
import android.nfc.Tag
import android.nfc.tech.IsoDep
import android.os.Bundle
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
import androidx.fragment.app.FragmentTransaction
import com.afollestad.aesthetic.Aesthetic
import com.afollestad.materialdialogs.MaterialDialog
import com.codebutler.farebot.card.desfire.DesfireProtocol
import com.google.android.material.navigation.NavigationView
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.app_bar_main.*
import org.mosad.seil0.projectlaogai.controller.CacheController
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.fragments.*
import kotlin.system.measureTimeMillis
import com.codebutler.farebot.Utils.selectAppFile
import com.codebutler.farebot.card.desfire.DesfireFileSettings
import java.lang.Exception
// TODO save the current fragment to show it when the app is restarted
class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelectedListener {
private var activeFragment: Fragment = HomeFragment() // the currently active fragment, home at the start
override fun onCreate(savedInstanceState: Bundle?) {
Aesthetic.attach(this)
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setSupportActionBar(toolbar)
// load mensa, timetable and color
load()
initAesthetic()
//init home fragment
val fragmentTransaction: FragmentTransaction = supportFragmentManager.beginTransaction()
fragmentTransaction.replace(R.id.fragment_container, activeFragment)
fragmentTransaction.commit()
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)
// TODO nfc stuff, needs to move to it's own function
if (NfcAdapter.ACTION_TECH_DISCOVERED == intent.action) {
val appId = 0x5F8415
val fileId = 1
val tag: Tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG)
val isoDep = IsoDep.get(tag)
isoDep.connect()
val card = DesfireProtocol(isoDep)
val settings = selectAppFile(card, appId, fileId)
if (settings is DesfireFileSettings.ValueDesfireFileSettings) {
val data = try {
card.readValue(fileId)
} catch (ex: Exception) { 0 }
MaterialDialog(this)
.title(text = "Mensa balance")
.message(text = "current: ${data / 1000}.${(data % 1000) / 10}\n" +
"latest: ${settings.value / 1000}.${(settings.value % 1000) / 10}")
.show()
}
}
}
override fun onNewIntent(intent: Intent) {
super.onNewIntent(intent)
if (NfcAdapter.ACTION_TECH_DISCOVERED == intent.action) {
intent.getParcelableArrayExtra(NfcAdapter.EXTRA_TAG)?.also { rawMessages ->
val messages: List<NdefMessage> = rawMessages.map { it as NdefMessage }
// Process the messages array.
MaterialDialog(this)
.title(text = "nfc tag detected (onNewIntent)")
.message(text = messages[0].toString())
.show()
}
}
}
override fun onResume() {
super.onResume()
Aesthetic.resume(this)
}
override fun onPause() {
super.onPause()
Aesthetic.pause(this)
}
override fun onBackPressed() {
if (drawer_layout.isDrawerOpen(GravityCompat.START)) {
drawer_layout.closeDrawer(GravityCompat.START)
} else {
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.
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()
}
val fragmentTransaction: FragmentTransaction = supportFragmentManager.beginTransaction()
fragmentTransaction.replace(R.id.fragment_container, activeFragment)
fragmentTransaction.commit()
drawer_layout.closeDrawer(GravityCompat.START)
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
}
println("startup completed in $startupTime ms")
}
private fun initAesthetic() {
// If we haven't set any defaults, do that now
if (Aesthetic.isFirstTime) {
// this is executed on the first app start, use this to show tutorial etc.
Aesthetic.config {
activityTheme(R.style.AppTheme_Light)
colorPrimary(Color.BLACK)
colorPrimaryDark(Color.BLACK)
colorAccent(Color.parseColor("#3f51b5"))
apply()
}
SettingsFragment().selectCourse(this)
} else {
Aesthetic.config {
colorPrimary(cColorPrimary)
colorPrimaryDark(cColorPrimary)
colorAccent(cColorAccent)
apply()
}
}
}
}