]> git.ktnx.net Git - mobile-ledger-staging.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/activity/SplashActivity.java
splash: setup DB in the background
[mobile-ledger-staging.git] / app / src / main / java / net / ktnx / mobileledger / ui / activity / SplashActivity.java
1 /*
2  * Copyright © 2020 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.activity.ComponentActivity;
26 import androidx.annotation.Nullable;
27
28 import net.ktnx.mobileledger.R;
29 import net.ktnx.mobileledger.model.Data;
30 import net.ktnx.mobileledger.model.MobileLedgerProfile;
31 import net.ktnx.mobileledger.utils.Logger;
32 import net.ktnx.mobileledger.utils.MLDB;
33 import net.ktnx.mobileledger.utils.MobileLedgerDatabase;
34
35 public class SplashActivity extends ComponentActivity {
36     private static final long keepActiveForMS = 500;
37     private long startupTime;
38     private boolean running = true;
39     @Override
40     protected void onCreate(@Nullable Bundle savedInstanceState) {
41         super.onCreate(savedInstanceState);
42         setTheme(R.style.AppTheme_default);
43         setContentView(R.layout.splash_activity_layout);
44         Logger.debug("splash", "onCreate()");
45
46         MobileLedgerDatabase.initComplete.setValue(false);
47         MobileLedgerDatabase.initComplete.observe(this, this::onDbInitDoneChanged);
48     }
49     @Override
50     protected void onStart() {
51         super.onStart();
52         Logger.debug("splash", "onStart()");
53         running = true;
54
55         startupTime = System.currentTimeMillis();
56
57         AsyncTask<Void, Void, Void> dbInitTask = new DatabaseInitTask();
58         dbInitTask.execute();
59     }
60     @Override
61     protected void onPause() {
62         super.onPause();
63         Logger.debug("splash", "onPause()");
64         running = false;
65     }
66     @Override
67     protected void onResume() {
68         super.onResume();
69         Logger.debug("splash", "onResume()");
70         running = true;
71     }
72     private void onDbInitDoneChanged(Boolean done) {
73         if (!done) {
74             Logger.debug("splash", "DB not yet initialized");
75             return;
76         }
77
78         Logger.debug("splash", "DB init done");
79         long now = System.currentTimeMillis();
80         if (now > startupTime + keepActiveForMS)
81             startMainActivity();
82         else {
83             new Handler().postDelayed(this::startMainActivity,
84                     keepActiveForMS - (now - startupTime));
85         }
86     }
87     private void startMainActivity() {
88         if (running) {
89             Logger.debug("splash", "still running, launching main activity");
90             Intent intent = new Intent(this, MainActivity.class);
91             intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NO_USER_ACTION |
92                             Intent.FLAG_ACTIVITY_NEW_TASK);
93             startActivity(intent);
94             overridePendingTransition(R.anim.fade_in_slowly, R.anim.fade_out_slowly);
95         }
96         else {
97             Logger.debug("splash", "Not running, finish and go away");
98             finish();
99         }
100     }
101     private static class DatabaseInitTask extends AsyncTask<Void, Void, Void> {
102         @Override
103         protected Void doInBackground(Void... voids) {
104             MobileLedgerProfile.loadAllFromDB(null);
105
106             String profileUUID = MLDB.getOption(MLDB.OPT_PROFILE_UUID, null);
107             MobileLedgerProfile startupProfile = Data.getProfile(profileUUID);
108             if (startupProfile != null)
109                 Data.setCurrentProfile(startupProfile);
110             return null;
111         }
112         @Override
113         protected void onPostExecute(Void aVoid) {
114             Logger.debug("splash", "DatabaseInitTask::onPostExecute()");
115             super.onPostExecute(aVoid);
116             MobileLedgerDatabase.initComplete.setValue(true);
117         }
118     }
119 }