]> git.ktnx.net Git - mobile-ledger-staging.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/activity/SplashActivity.java
fixed a hard to reproduce crash upon returning to the app
[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.annotation.Nullable;
26
27 import net.ktnx.mobileledger.R;
28 import net.ktnx.mobileledger.model.Data;
29 import net.ktnx.mobileledger.model.MobileLedgerProfile;
30 import net.ktnx.mobileledger.utils.Logger;
31 import net.ktnx.mobileledger.utils.MLDB;
32 import net.ktnx.mobileledger.utils.MobileLedgerDatabase;
33
34 public class SplashActivity extends CrashReportingActivity {
35     private static final long keepActiveForMS = 400;
36     private long startupTime;
37     private boolean running = true;
38     @Override
39     protected void onCreate(@Nullable Bundle savedInstanceState) {
40         super.onCreate(savedInstanceState);
41         setTheme(R.style.AppTheme_default);
42         setContentView(R.layout.splash_activity_layout);
43         Logger.debug("splash", "onCreate()");
44
45         MobileLedgerDatabase.initComplete.setValue(false);
46         MobileLedgerDatabase.initComplete.observe(this, this::onDbInitDoneChanged);
47     }
48     @Override
49     protected void onStart() {
50         super.onStart();
51         Logger.debug("splash", "onStart()");
52         running = true;
53
54         startupTime = System.currentTimeMillis();
55
56         AsyncTask<Void, Void, Void> dbInitTask = new DatabaseInitTask();
57         dbInitTask.execute();
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             new Handler().postDelayed(this::startMainActivity,
83                     keepActiveForMS - (now - startupTime));
84         }
85     }
86     private void startMainActivity() {
87         if (running) {
88             Logger.debug("splash", "still running, launching main activity");
89             Intent intent = new Intent(this, MainActivity.class);
90             intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NO_USER_ACTION |
91                             Intent.FLAG_ACTIVITY_NEW_TASK);
92             startActivity(intent);
93             overridePendingTransition(R.anim.fade_in_slowly, R.anim.fade_out_slowly);
94         }
95         else {
96             Logger.debug("splash", "Not running, finish and go away");
97             finish();
98         }
99     }
100     private static class DatabaseInitTask extends AsyncTask<Void, Void, Void> {
101         @Override
102         protected Void doInBackground(Void... voids) {
103             MobileLedgerProfile.loadAllFromDB(null);
104
105             String profileUUID = MLDB.getOption(MLDB.OPT_PROFILE_UUID, null);
106             MobileLedgerProfile startupProfile = Data.getProfile(profileUUID);
107             if (startupProfile != null)
108                 Data.postCurrentProfile(startupProfile);
109             return null;
110         }
111         @Override
112         protected void onPostExecute(Void aVoid) {
113             Logger.debug("splash", "DatabaseInitTask::onPostExecute()");
114             super.onPostExecute(aVoid);
115             MobileLedgerDatabase.initComplete.setValue(true);
116         }
117     }
118 }