X-Git-Url: https://git.ktnx.net/?a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fnet%2Fktnx%2Fmobileledger%2Fui%2Factivity%2FSplashActivity.java;h=495a328f9a2aef6c30c7231db3333e6c3462806f;hb=5df10dc0b58df4d4be4e9ab34f1e0f477ca46766;hp=32b71d653e038d9348747c89e716f7da3de4953f;hpb=bc308eb5e470d3f51e91bebad2f368d618593907;p=mobile-ledger.git diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/activity/SplashActivity.java b/app/src/main/java/net/ktnx/mobileledger/ui/activity/SplashActivity.java index 32b71d65..495a328f 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/activity/SplashActivity.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/activity/SplashActivity.java @@ -1,5 +1,5 @@ /* - * Copyright © 2020 Damyan Ivanov. + * Copyright © 2021 Damyan Ivanov. * This file is part of MoLe. * MoLe is free software: you can distribute it and/or modify it * under the term of the GNU General Public License as published by @@ -17,42 +17,97 @@ package net.ktnx.mobileledger.ui.activity; -import android.app.Activity; import android.content.Intent; +import android.os.AsyncTask; import android.os.Bundle; import android.os.Handler; import androidx.annotation.Nullable; import net.ktnx.mobileledger.R; -import net.ktnx.mobileledger.model.Data; -import net.ktnx.mobileledger.model.MobileLedgerProfile; -import net.ktnx.mobileledger.utils.MLDB; +import net.ktnx.mobileledger.db.DB; +import net.ktnx.mobileledger.utils.Logger; +import net.ktnx.mobileledger.utils.MobileLedgerDatabase; -public class SplashActivity extends Activity { +public class SplashActivity extends CrashReportingActivity { + private static final long keepActiveForMS = 400; + private long startupTime; + private boolean running = true; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setTheme(R.style.AppTheme_default); setContentView(R.layout.splash_activity_layout); + Logger.debug("splash", "onCreate()"); + + MobileLedgerDatabase.initComplete.setValue(false); + MobileLedgerDatabase.initComplete.observe(this, this::onDbInitDoneChanged); } @Override protected void onStart() { super.onStart(); + Logger.debug("splash", "onStart()"); + running = true; - MobileLedgerProfile.loadAllFromDB(null); + startupTime = System.currentTimeMillis(); - String profileUUID = MLDB.getOption(MLDB.OPT_PROFILE_UUID, null); - MobileLedgerProfile startupProfile = Data.getProfile(profileUUID); - if (startupProfile != null) - Data.setCurrentProfile(startupProfile); + AsyncTask dbInitTask = new DatabaseInitTask(); + Logger.debug("splash", "starting dbInit task"); + dbInitTask.execute(); + } + @Override + protected void onPause() { + super.onPause(); + Logger.debug("splash", "onPause()"); + running = false; + } + @Override + protected void onResume() { + super.onResume(); + Logger.debug("splash", "onResume()"); + running = true; + } + private void onDbInitDoneChanged(Boolean done) { + if (!done) { + Logger.debug("splash", "DB not yet initialized"); + return; + } - new Handler().postDelayed(() -> { + Logger.debug("splash", "DB init done"); + long now = System.currentTimeMillis(); + if (now > startupTime + keepActiveForMS) + startMainActivity(); + else { + new Handler().postDelayed(this::startMainActivity, + keepActiveForMS - (now - startupTime)); + } + } + private void startMainActivity() { + if (running) { + Logger.debug("splash", "still running, launching main activity"); Intent intent = new Intent(this, MainActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NO_USER_ACTION | Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(intent); - overridePendingTransition(R.anim.fade_in, R.anim.dummy); - }, 250); + overridePendingTransition(R.anim.fade_in_slowly, R.anim.fade_out_slowly); + } + else { + Logger.debug("splash", "Not running, finish and go away"); + finish(); + } + } + private static class DatabaseInitTask extends AsyncTask { + @Override + protected Void doInBackground(Void... voids) { + long ignored = DB.get().getProfileDAO().getProfileCountSync(); + + return null; + } + @Override + protected void onPostExecute(Void aVoid) { + Logger.debug("splash", "DatabaseInitTask::onPostExecute()"); + super.onPostExecute(aVoid); + MobileLedgerDatabase.initComplete.setValue(true); + } } }