2 * Copyright © 2021 Damyan Ivanov.
3 * This file is part of MoLe.
4 * MoLe is free software: you can distribute it and/or modify it
5 * under the term of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your opinion), any later version.
9 * MoLe is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License terms for details.
14 * You should have received a copy of the GNU General Public License
15 * along with MoLe. If not, see <https://www.gnu.org/licenses/>.
18 package net.ktnx.mobileledger.ui.activity;
20 import android.content.Intent;
21 import android.os.Bundle;
22 import android.os.Handler;
24 import androidx.annotation.Nullable;
26 import net.ktnx.mobileledger.R;
27 import net.ktnx.mobileledger.db.DB;
28 import net.ktnx.mobileledger.utils.Logger;
30 import java.util.Locale;
32 public class SplashActivity extends CrashReportingActivity {
33 private static final long keepActiveForMS = 400;
34 private long startupTime;
35 private boolean running = true;
37 protected void onCreate(@Nullable Bundle savedInstanceState) {
38 super.onCreate(savedInstanceState);
39 setTheme(R.style.AppTheme_default);
40 setContentView(R.layout.splash_activity_layout);
41 Logger.debug("splash", "onCreate()");
43 DB.initComplete.setValue(false);
44 DB.initComplete.observe(this, this::onDbInitDoneChanged);
47 protected void onStart() {
49 Logger.debug("splash", "onStart()");
52 startupTime = System.currentTimeMillis();
54 DatabaseInitThread dbInitThread = new DatabaseInitThread();
55 Logger.debug("splash", "starting dbInit task");
59 protected void onPause() {
61 Logger.debug("splash", "onPause()");
65 protected void onResume() {
67 Logger.debug("splash", "onResume()");
70 private void onDbInitDoneChanged(Boolean done) {
72 Logger.debug("splash", "DB not yet initialized");
76 Logger.debug("splash", "DB init done");
77 long now = System.currentTimeMillis();
78 if (now > startupTime + keepActiveForMS)
81 final long delay = keepActiveForMS - (now - startupTime);
82 Logger.debug("splash",
83 String.format(Locale.ROOT, "Scheduling main activity start in %d milliseconds",
85 new Handler().postDelayed(this::startMainActivity, delay);
88 private void startMainActivity() {
90 Logger.debug("splash", "still running, launching main activity");
91 Intent intent = new Intent(this, MainActivity.class);
92 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NO_USER_ACTION |
93 Intent.FLAG_ACTIVITY_NEW_TASK);
94 startActivity(intent);
95 overridePendingTransition(R.anim.fade_in_slowly, R.anim.fade_out_slowly);
98 Logger.debug("splash", "Not running, finish and go away");
102 private static class DatabaseInitThread extends Thread {
105 long ignored = DB.get()
107 .getProfileCountSync();
109 DB.initComplete.postValue(true);