]> git.ktnx.net Git - mobile-ledger.git/blobdiff - app/src/main/java/net/ktnx/mobileledger/ui/activity/SplashActivity.java
more pronounced day/month delimiters in the transaction list
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / ui / activity / SplashActivity.java
index 921abb7512f7363ed0bca78ea6a140e28d45cd46..3fe204834bb572c05e7e77a8caf5a1687f15699f 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright © 2020 Damyan Ivanov.
+ * Copyright © 2021 Damyan Ivanov.
  * This file is part of MoLe.
  * MoLe is free software: you can distribute it and/or modify it
  * under the term of the GNU General Public License as published by
 package net.ktnx.mobileledger.ui.activity;
 
 import android.content.Intent;
-import android.os.AsyncTask;
 import android.os.Bundle;
 import android.os.Handler;
+import android.os.Looper;
 
-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.db.DB;
 import net.ktnx.mobileledger.utils.Logger;
-import net.ktnx.mobileledger.utils.MLDB;
-import net.ktnx.mobileledger.utils.MobileLedgerDatabase;
 
-public class SplashActivity extends ComponentActivity {
-    private static final long keepActiveForMS = 500;
+import java.util.Locale;
+
+public class SplashActivity extends CrashReportingActivity {
+    private static final long keepActiveForMS = 400;
     private long startupTime;
     private boolean running = true;
     @Override
@@ -43,8 +41,8 @@ public class SplashActivity extends ComponentActivity {
         setContentView(R.layout.splash_activity_layout);
         Logger.debug("splash", "onCreate()");
 
-        MobileLedgerDatabase.initComplete.setValue(false);
-        MobileLedgerDatabase.initComplete.observe(this, this::onDbInitDoneChanged);
+        DB.initComplete.setValue(false);
+        DB.initComplete.observe(this, this::onDbInitDoneChanged);
     }
     @Override
     protected void onStart() {
@@ -54,8 +52,9 @@ public class SplashActivity extends ComponentActivity {
 
         startupTime = System.currentTimeMillis();
 
-        AsyncTask<Void, Void, Void> dbInitTask = new DatabaseInitTask();
-        dbInitTask.execute();
+        DatabaseInitThread dbInitThread = new DatabaseInitThread();
+        Logger.debug("splash", "starting dbInit task");
+        dbInitThread.start();
     }
     @Override
     protected void onPause() {
@@ -80,8 +79,11 @@ public class SplashActivity extends ComponentActivity {
         if (now > startupTime + keepActiveForMS)
             startMainActivity();
         else {
-            new Handler().postDelayed(this::startMainActivity,
-                    keepActiveForMS - (now - startupTime));
+            final long delay = keepActiveForMS - (now - startupTime);
+            Logger.debug("splash",
+                    String.format(Locale.ROOT, "Scheduling main activity start in %d milliseconds",
+                            delay));
+            new Handler(Looper.getMainLooper()).postDelayed(this::startMainActivity, delay);
         }
     }
     private void startMainActivity() {
@@ -98,22 +100,14 @@ public class SplashActivity extends ComponentActivity {
             finish();
         }
     }
-    private static class DatabaseInitTask extends AsyncTask<Void, Void, Void> {
+    private static class DatabaseInitThread extends Thread {
         @Override
-        protected Void doInBackground(Void... voids) {
-            MobileLedgerProfile.loadAllFromDB(null);
+        public void run() {
+            long ignored = DB.get()
+                             .getProfileDAO()
+                             .getProfileCountSync();
 
-            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);
+            DB.initComplete.postValue(true);
         }
     }
 }