]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/activity/SplashActivity.java
Room-based profile management
[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.AsyncTask;
22 import android.os.Bundle;
23 import android.os.Handler;
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 import net.ktnx.mobileledger.utils.MobileLedgerDatabase;
31
32 public class SplashActivity extends CrashReportingActivity {
33     private static final long keepActiveForMS = 400;
34     private long startupTime;
35     private boolean running = true;
36     @Override
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()");
42
43         MobileLedgerDatabase.initComplete.setValue(false);
44         MobileLedgerDatabase.initComplete.observe(this, this::onDbInitDoneChanged);
45     }
46     @Override
47     protected void onStart() {
48         super.onStart();
49         Logger.debug("splash", "onStart()");
50         running = true;
51
52         startupTime = System.currentTimeMillis();
53
54         AsyncTask<Void, Void, Void> dbInitTask = new DatabaseInitTask();
55         Logger.debug("splash", "starting dbInit task");
56         dbInitTask.execute();
57     }
58     @Override
59     protected void onPause() {
60         super.onPause();
61         Logger.debug("splash", "onPause()");
62         running = false;
63     }
64     @Override
65     protected void onResume() {
66         super.onResume();
67         Logger.debug("splash", "onResume()");
68         running = true;
69     }
70     private void onDbInitDoneChanged(Boolean done) {
71         if (!done) {
72             Logger.debug("splash", "DB not yet initialized");
73             return;
74         }
75
76         Logger.debug("splash", "DB init done");
77         long now = System.currentTimeMillis();
78         if (now > startupTime + keepActiveForMS)
79             startMainActivity();
80         else {
81             new Handler().postDelayed(this::startMainActivity,
82                     keepActiveForMS - (now - startupTime));
83         }
84     }
85     private void startMainActivity() {
86         if (running) {
87             Logger.debug("splash", "still running, launching main activity");
88             Intent intent = new Intent(this, MainActivity.class);
89             intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NO_USER_ACTION |
90                             Intent.FLAG_ACTIVITY_NEW_TASK);
91             startActivity(intent);
92             overridePendingTransition(R.anim.fade_in_slowly, R.anim.fade_out_slowly);
93         }
94         else {
95             Logger.debug("splash", "Not running, finish and go away");
96             finish();
97         }
98     }
99     private static class DatabaseInitTask extends AsyncTask<Void, Void, Void> {
100         @Override
101         protected Void doInBackground(Void... voids) {
102             long ignored = DB.get().getProfileDAO().getProfileCountSync();
103
104             return null;
105         }
106         @Override
107         protected void onPostExecute(Void aVoid) {
108             Logger.debug("splash", "DatabaseInitTask::onPostExecute()");
109             super.onPostExecute(aVoid);
110             MobileLedgerDatabase.initComplete.setValue(true);
111         }
112     }
113 }