/*
- * 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
package net.ktnx.mobileledger.ui.activity;
-import android.app.Activity;
import android.content.Intent;
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;
-public class SplashActivity extends Activity {
+import java.util.Locale;
+
+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()");
+
+ DB.initComplete.setValue(false);
+ DB.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);
+ DatabaseInitThread dbInitThread = new DatabaseInitThread();
+ Logger.debug("splash", "starting dbInit task");
+ dbInitThread.start();
+ }
+ @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 {
+ final long delay = keepActiveForMS - (now - startupTime);
+ Logger.debug("splash",
+ String.format(Locale.ROOT, "Scheduling main activity start in %d milliseconds",
+ delay));
+ new Handler().postDelayed(this::startMainActivity, delay);
+ }
+ }
+ 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 DatabaseInitThread extends Thread {
+ @Override
+ public void run() {
+ long ignored = DB.get()
+ .getProfileDAO()
+ .getProfileCountSync();
+
+ DB.initComplete.postValue(true);
+ }
}
}