2018-10-24 18:22:05 +02:00
/ * *
* ProjectLaogai
*
2020-01-15 15:00:05 +01:00
* 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
2019-08-18 22:09:33 +02:00
import android.app.PendingIntent
2019-08-21 20:43:08 +02:00
import android.content.Context
2019-08-17 18:59:28 +02:00
import android.content.Intent
2019-08-18 22:09:33 +02:00
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
2019-08-18 22:09:33 +02:00
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
2020-08-17 22:30:12 +02:00
import android.util.TypedValue
2018-10-24 18:22:05 +02:00
import android.view.Menu
import android.view.MenuItem
2018-11-05 20:59:41 +01:00
import androidx.appcompat.app.ActionBarDrawerToggle
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.GravityCompat
2019-02-17 15:05:03 +01:00
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
2019-08-23 20:25:20 +02:00
import com.afollestad.aesthetic.NavigationViewMode
2018-11-05 20:59:41 +01:00
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.*
2019-08-18 22:09:33 +02:00
import org.mosad.seil0.projectlaogai.controller.NFCMensaCard
2020-08-13 21:01:21 +02:00
import org.mosad.seil0.projectlaogai.controller.cache.CacheController
import org.mosad.seil0.projectlaogai.controller.preferences.EncryptedPreferences
import org.mosad.seil0.projectlaogai.controller.preferences.Preferences
2018-11-19 12:00:39 +01:00
import org.mosad.seil0.projectlaogai.fragments.*
2020-08-31 23:03:58 +02:00
import org.mosad.seil0.projectlaogai.util.NotificationUtils
2018-11-01 02:12:43 +01:00
import kotlin.system.measureTimeMillis
2019-08-17 18:59:28 +02:00
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 {
2019-02-17 15:05:03 +01:00
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 "
2019-02-14 16:25:13 +01:00
2019-08-18 22:09:33 +02:00
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
2019-08-18 22:09:33 +02:00
2018-10-24 18:22:05 +02:00
override fun onCreate ( savedInstanceState : Bundle ? ) {
2020-01-15 15:00:05 +01:00
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 )
2019-01-03 01:45:28 +01:00
// load mensa, timetable and color
load ( )
2019-06-24 22:41:45 +02:00
initAesthetic ( )
2019-08-18 22:09:33 +02:00
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
2020-08-23 14:27:41 +02:00
// based on the intent we get, call readBalance or open a Fragment
2020-01-15 15:00:05 +01:00
when ( intent . action ) {
NfcAdapter . ACTION _TECH _DISCOVERED -> NFCMensaCard . readBalance ( intent , this )
2020-09-10 18:26:42 +02:00
getString ( R . string . intent _action _mensaFragment ) -> activeFragment = MensaFragment ( )
getString ( R . string . intent _action _timetableFragment ) -> activeFragment = TimetableFragment ( )
getString ( R . string . intent _action _moodleFragment ) -> activeFragment = MoodleFragment ( )
getString ( R . string . intent _action _gradesFragment ) -> activeFragment = GradesFragment ( )
else -> activeFragment = HomeFragment ( )
2019-11-28 16:38:25 +01:00
}
2020-01-15 15:00:05 +01:00
// open the activeFragment, default is the HomeFragment
fragmentTransaction . replace ( R . id . fragment _container , activeFragment )
fragmentTransaction . commit ( )
2019-08-17 18:59:28 +02:00
}
override fun onNewIntent ( intent : Intent ) {
super . onNewIntent ( intent )
2019-08-18 22:09:33 +02:00
if ( NfcAdapter . ACTION _TECH _DISCOVERED == intent . action )
NFCMensaCard . readBalance ( intent , this )
2018-10-24 18:22:05 +02:00
}
2019-08-17 18:59:28 +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 )
2019-08-18 22:09:33 +02:00
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 {
2019-08-18 22:09:33 +02:00
// 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 ( )
2020-09-10 18:26:42 +02:00
R . id . nav _timetable -> TimetableFragment ( )
2019-06-03 10:59:32 +02:00
R . id . nav _moodle -> MoodleFragment ( )
2020-08-14 21:41:43 +02:00
R . id . nav _grades -> GradesFragment ( )
2019-06-03 10:59:32 +02:00
R . id . nav _settings -> SettingsFragment ( )
else -> HomeFragment ( )
2018-10-24 18:22:05 +02:00
}
2019-02-17 15:05:03 +01: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 )
2019-06-24 22:41:45 +02:00
2018-10-24 18:22:05 +02:00
return true
}
2018-10-29 18:03:20 +01:00
/ * *
* load the mensa menus of the current week
* /
private fun load ( ) {
2019-03-17 18:12:36 +01:00
val startupTime = measureTimeMillis {
2020-08-13 21:01:21 +02:00
Preferences . load ( this ) // load the settings, must be finished before doing anything else
2019-06-26 15:00:43 +02:00
CacheController ( this ) // load the cache
2020-08-13 21:01:21 +02:00
EncryptedPreferences . load ( this )
2020-08-31 23:03:58 +02:00
NotificationUtils ( this )
2018-11-01 02:12:43 +01:00
}
2020-02-05 14:10:45 +01:00
Log . i ( className , " startup completed in $startupTime ms " )
2018-10-29 18:03:20 +01:00
}
2018-11-01 01:06:53 +01:00
2019-06-24 22:41:45 +02:00
private fun initAesthetic ( ) {
// If we haven't set any defaults, do that now
if ( Aesthetic . isFirstTime ) {
2020-02-25 15:25:45 +01:00
// set the default theme at the first app start
2019-06-24 22:41:45 +02:00
Aesthetic . config {
2019-06-25 13:09:17 +02:00
activityTheme ( R . style . AppTheme _Light )
2019-06-24 22:41:45 +02:00
apply ( )
}
2020-02-25 15:25:45 +01:00
// show the onboarding activity
startActivity ( Intent ( this , OnboardingActivity :: class . java ) )
2020-02-27 12:13:46 +01:00
finish ( )
2019-08-23 20:25:20 +02:00
}
Aesthetic . config {
2020-09-02 15:36:03 +02:00
colorPrimary ( Preferences . colorPrimary )
colorPrimaryDark ( Preferences . colorPrimary )
colorAccent ( Preferences . colorAccent )
2019-08-23 20:25:20 +02:00
navigationViewMode ( NavigationViewMode . SELECTED _ACCENT )
apply ( )
2019-06-24 22:41:45 +02:00
}
2019-08-23 20:25:20 +02:00
2020-08-17 22:30:12 +02:00
// set theme color values
val out = TypedValue ( )
this . theme . resolveAttribute ( R . attr . themePrimary , out , true )
Preferences . themePrimary = out . data
this . theme . resolveAttribute ( R . attr . themeSecondary , out , true )
Preferences . themeSecondary = out . data
2019-06-24 22:41:45 +02:00
}
2019-08-18 22:09:33 +02:00
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
)
}
2019-08-18 22:09:33 +02:00
}
2018-10-24 18:22:05 +02:00
}