]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/activity/SplashActivity.java
more pronounced day/month delimiters in the transaction list
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / ui / activity / SplashActivity.java
1 /*
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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.ktnx.mobileledger.ui.activity;
19
20 import android.content.Intent;
21 import android.os.Bundle;
22 import android.os.Handler;
23 import android.os.Looper;
24
25 import androidx.annotation.Nullable;
26
27 import net.ktnx.mobileledger.R;
28 import net.ktnx.mobileledger.db.DB;
29 import net.ktnx.mobileledger.utils.Logger;
30
31 import java.util.Locale;
32
33 public class SplashActivity extends CrashReportingActivity {
34     private static final long keepActiveForMS = 400;
35     private long startupTime;
36     private boolean running = true;
37     @Override
38     protected void onCreate(@Nullable Bundle savedInstanceState) {
39         super.onCreate(savedInstanceState);
40         setTheme(R.style.AppTheme_default);
41         setContentView(R.layout.splash_activity_layout);
42         Logger.debug("splash", "onCreate()");
43
44         DB.initComplete.setValue(false);
45         DB.initComplete.observe(this, this::onDbInitDoneChanged);
46     }
47     @Override
48     protected void onStart() {
49         super.onStart();
50         Logger.debug("splash", "onStart()");
51         running = true;
52
53         startupTime = System.currentTimeMillis();
54
55         DatabaseInitThread dbInitThread = new DatabaseInitThread();
56         Logger.debug("splash", "starting dbInit task");
57         dbInitThread.start();
58     }
59     @Override
60     protected void onPause() {
61         super.onPause();
62         Logger.debug("splash", "onPause()");
63         running = false;
64     }
65     @Override
66     protected void onResume() {
67         super.onResume();
68         Logger.debug("splash", "onResume()");
69         running = true;
70     }
71     private void onDbInitDoneChanged(Boolean done) {
72         if (!done) {
73             Logger.debug("splash", "DB not yet initialized");
74             return;
75         }
76
77         Logger.debug("splash", "DB init done");
78         long now = System.currentTimeMillis();
79         if (now > startupTime + keepActiveForMS)
80             startMainActivity();
81         else {
82             final long delay = keepActiveForMS - (now - startupTime);
83             Logger.debug("splash",
84                     String.format(Locale.ROOT, "Scheduling main activity start in %d milliseconds",
85                             delay));
86             new Handler(Looper.getMainLooper()).postDelayed(this::startMainActivity, delay);
87         }
88     }
89     private void startMainActivity() {
90         if (running) {
91             Logger.debug("splash", "still running, launching main activity");
92             Intent intent = new Intent(this, MainActivity.class);
93             intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NO_USER_ACTION |
94                             Intent.FLAG_ACTIVITY_NEW_TASK);
95             startActivity(intent);
96             overridePendingTransition(R.anim.fade_in_slowly, R.anim.fade_out_slowly);
97         }
98         else {
99             Logger.debug("splash", "Not running, finish and go away");
100             finish();
101         }
102     }
103     private static class DatabaseInitThread extends Thread {
104         @Override
105         public void run() {
106             long ignored = DB.get()
107                              .getProfileDAO()
108                              .getProfileCountSync();
109
110             DB.initComplete.postValue(true);
111         }
112     }
113 }