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