X-Git-Url: https://git.ktnx.net/?p=mobile-ledger.git;a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fnet%2Fktnx%2Fmobileledger%2Fui%2Factivity%2FSplashActivity.java;fp=app%2Fsrc%2Fmain%2Fjava%2Fnet%2Fktnx%2Fmobileledger%2Fui%2Factivity%2FSplashActivity.java;h=921abb7512f7363ed0bca78ea6a140e28d45cd46;hp=32b71d653e038d9348747c89e716f7da3de4953f;hb=f0fecef867dd49fe41fc733c11418f95a270be4a;hpb=4b9f172b76c923c1c150f7b2cf306674e7b71b50 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..921abb75 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 @@ -17,42 +17,103 @@ 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.activity.ComponentActivity; 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.Logger; import net.ktnx.mobileledger.utils.MLDB; +import net.ktnx.mobileledger.utils.MobileLedgerDatabase; -public class SplashActivity extends Activity { +public class SplashActivity extends ComponentActivity { + private static final long keepActiveForMS = 500; + 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(); + 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) { + MobileLedgerProfile.loadAllFromDB(null); + + String profileUUID = MLDB.getOption(MLDB.OPT_PROFILE_UUID, null); + MobileLedgerProfile startupProfile = Data.getProfile(profileUUID); + if (startupProfile != null) + Data.setCurrentProfile(startupProfile); + return null; + } + @Override + protected void onPostExecute(Void aVoid) { + Logger.debug("splash", "DatabaseInitTask::onPostExecute()"); + super.onPostExecute(aVoid); + MobileLedgerDatabase.initComplete.setValue(true); + } } }