]> git.ktnx.net Git - mobile-ledger.git/blobdiff - app/src/main/java/net/ktnx/mobileledger/ui/activity/SplashActivity.java
splash: setup DB in the background
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / ui / activity / SplashActivity.java
index 32b71d653e038d9348747c89e716f7da3de4953f..921abb7512f7363ed0bca78ea6a140e28d45cd46 100644 (file)
 
 package net.ktnx.mobileledger.ui.activity;
 
-import android.app.Activity;
 import android.content.Intent;
+import android.os.AsyncTask;
 import android.os.Bundle;
 import android.os.Handler;
 
+import androidx.activity.ComponentActivity;
 import androidx.annotation.Nullable;
 
 import net.ktnx.mobileledger.R;
 import net.ktnx.mobileledger.model.Data;
 import net.ktnx.mobileledger.model.MobileLedgerProfile;
+import net.ktnx.mobileledger.utils.Logger;
 import net.ktnx.mobileledger.utils.MLDB;
+import net.ktnx.mobileledger.utils.MobileLedgerDatabase;
 
-public class SplashActivity extends Activity {
+public class SplashActivity extends ComponentActivity {
+    private static final long keepActiveForMS = 500;
+    private long startupTime;
+    private boolean running = true;
     @Override
     protected void onCreate(@Nullable Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setTheme(R.style.AppTheme_default);
         setContentView(R.layout.splash_activity_layout);
+        Logger.debug("splash", "onCreate()");
+
+        MobileLedgerDatabase.initComplete.setValue(false);
+        MobileLedgerDatabase.initComplete.observe(this, this::onDbInitDoneChanged);
     }
     @Override
     protected void onStart() {
         super.onStart();
+        Logger.debug("splash", "onStart()");
+        running = true;
 
-        MobileLedgerProfile.loadAllFromDB(null);
+        startupTime = System.currentTimeMillis();
 
-        String profileUUID = MLDB.getOption(MLDB.OPT_PROFILE_UUID, null);
-        MobileLedgerProfile startupProfile = Data.getProfile(profileUUID);
-        if (startupProfile != null)
-            Data.setCurrentProfile(startupProfile);
+        AsyncTask<Void, Void, Void> dbInitTask = new DatabaseInitTask();
+        dbInitTask.execute();
+    }
+    @Override
+    protected void onPause() {
+        super.onPause();
+        Logger.debug("splash", "onPause()");
+        running = false;
+    }
+    @Override
+    protected void onResume() {
+        super.onResume();
+        Logger.debug("splash", "onResume()");
+        running = true;
+    }
+    private void onDbInitDoneChanged(Boolean done) {
+        if (!done) {
+            Logger.debug("splash", "DB not yet initialized");
+            return;
+        }
 
-        new Handler().postDelayed(() -> {
+        Logger.debug("splash", "DB init done");
+        long now = System.currentTimeMillis();
+        if (now > startupTime + keepActiveForMS)
+            startMainActivity();
+        else {
+            new Handler().postDelayed(this::startMainActivity,
+                    keepActiveForMS - (now - startupTime));
+        }
+    }
+    private void startMainActivity() {
+        if (running) {
+            Logger.debug("splash", "still running, launching main activity");
             Intent intent = new Intent(this, MainActivity.class);
             intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NO_USER_ACTION |
                             Intent.FLAG_ACTIVITY_NEW_TASK);
             startActivity(intent);
-            overridePendingTransition(R.anim.fade_in, R.anim.dummy);
-        }, 250);
+            overridePendingTransition(R.anim.fade_in_slowly, R.anim.fade_out_slowly);
+        }
+        else {
+            Logger.debug("splash", "Not running, finish and go away");
+            finish();
+        }
+    }
+    private static class DatabaseInitTask extends AsyncTask<Void, Void, Void> {
+        @Override
+        protected Void doInBackground(Void... voids) {
+            MobileLedgerProfile.loadAllFromDB(null);
+
+            String profileUUID = MLDB.getOption(MLDB.OPT_PROFILE_UUID, null);
+            MobileLedgerProfile startupProfile = Data.getProfile(profileUUID);
+            if (startupProfile != null)
+                Data.setCurrentProfile(startupProfile);
+            return null;
+        }
+        @Override
+        protected void onPostExecute(Void aVoid) {
+            Logger.debug("splash", "DatabaseInitTask::onPostExecute()");
+            super.onPostExecute(aVoid);
+            MobileLedgerDatabase.initComplete.setValue(true);
+        }
     }
 }