From: Damyan Ivanov Date: Sat, 26 Sep 2020 14:24:09 +0000 (+0300) Subject: splash: setup DB in the background X-Git-Tag: v0.16.0~46 X-Git-Url: https://git.ktnx.net/?a=commitdiff_plain;h=f0fecef867dd49fe41fc733c11418f95a270be4a;p=mobile-ledger.git splash: setup DB in the background this puts all initial DB work in the background - creation and upgrade avoids empty screen when there is a ig DB rework on upgrade --- diff --git a/app/src/main/java/net/ktnx/mobileledger/model/Data.java b/app/src/main/java/net/ktnx/mobileledger/model/Data.java index 0bd50ebb..f0ba846e 100644 --- a/app/src/main/java/net/ktnx/mobileledger/model/Data.java +++ b/app/src/main/java/net/ktnx/mobileledger/model/Data.java @@ -92,7 +92,7 @@ public final class Data { } public static void setCurrentProfile(@NonNull MobileLedgerProfile newProfile) { MLDB.setOption(MLDB.OPT_PROFILE_UUID, newProfile.getUuid()); - profile.setValue(newProfile); + profile.postValue(newProfile); } public static int getProfileIndex(MobileLedgerProfile profile) { try (LockHolder ignored = profilesLocker.lockForReading()) { diff --git a/app/src/main/java/net/ktnx/mobileledger/model/MobileLedgerProfile.java b/app/src/main/java/net/ktnx/mobileledger/model/MobileLedgerProfile.java index 6f6e51cb..c9e358e2 100644 --- a/app/src/main/java/net/ktnx/mobileledger/model/MobileLedgerProfile.java +++ b/app/src/main/java/net/ktnx/mobileledger/model/MobileLedgerProfile.java @@ -122,7 +122,7 @@ public final class MobileLedgerProfile { result = item; } } - Data.profiles.setValue(list); + Data.profiles.postValue(list); return result; } public static void storeProfilesOrder() { diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/activity/SplashActivity.java b/app/src/main/java/net/ktnx/mobileledger/ui/activity/SplashActivity.java index 32b71d65..921abb75 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/activity/SplashActivity.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/activity/SplashActivity.java @@ -17,42 +17,103 @@ 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 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 { + @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); + } } } diff --git a/app/src/main/java/net/ktnx/mobileledger/utils/MobileLedgerDatabase.java b/app/src/main/java/net/ktnx/mobileledger/utils/MobileLedgerDatabase.java index 29d650a5..307d4bc9 100644 --- a/app/src/main/java/net/ktnx/mobileledger/utils/MobileLedgerDatabase.java +++ b/app/src/main/java/net/ktnx/mobileledger/utils/MobileLedgerDatabase.java @@ -24,6 +24,8 @@ import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.util.Log; +import androidx.lifecycle.MutableLiveData; + import net.ktnx.mobileledger.BuildConfig; import java.io.BufferedReader; @@ -35,10 +37,10 @@ import java.util.Locale; import static net.ktnx.mobileledger.utils.Logger.debug; public class MobileLedgerDatabase extends SQLiteOpenHelper { + public static final MutableLiveData initComplete = new MutableLiveData<>(false); private static final String DB_NAME = "MoLe.db"; private static final int LATEST_REVISION = 40; private static final String CREATE_DB_SQL = "create_db"; - private final Application mContext; public MobileLedgerDatabase(Application context) { diff --git a/app/src/main/res/anim/fade_in_slowly.xml b/app/src/main/res/anim/fade_in_slowly.xml new file mode 100644 index 00000000..1c76bb18 --- /dev/null +++ b/app/src/main/res/anim/fade_in_slowly.xml @@ -0,0 +1,25 @@ + + + + + \ No newline at end of file diff --git a/app/src/main/res/anim/fade_out_slowly.xml b/app/src/main/res/anim/fade_out_slowly.xml new file mode 100644 index 00000000..296c095c --- /dev/null +++ b/app/src/main/res/anim/fade_out_slowly.xml @@ -0,0 +1,25 @@ + + + + + \ No newline at end of file