From 3162dad033483b3cb07b6e778d7c1661fd3574fc Mon Sep 17 00:00:00 2001 From: Gideon Okuro Date: Wed, 10 Jul 2024 18:13:24 +0300 Subject: [PATCH 1/2] save app launches --- .../ole/planet/myplanet/MainApplication.kt | 66 +++++++++++++++++-- 1 file changed, 61 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/org/ole/planet/myplanet/MainApplication.kt b/app/src/main/java/org/ole/planet/myplanet/MainApplication.kt index e18cb32f53..251427ddcc 100644 --- a/app/src/main/java/org/ole/planet/myplanet/MainApplication.kt +++ b/app/src/main/java/org/ole/planet/myplanet/MainApplication.kt @@ -62,12 +62,16 @@ class MainApplication : Application(), Application.ActivityLifecycleCallbacks { return "0" } } + + private var activityReferences = 0 + private var isActivityChangingConfigurations = false + private var isFirstLaunch = true + @RequiresApi(Build.VERSION_CODES.N) override fun onCreate() { super.onCreate() initialize(CoroutineScope(Dispatchers.IO)) - context = this preferences = getSharedPreferences(PREFS_NAME, MODE_PRIVATE) val builder = VmPolicy.Builder() @@ -88,6 +92,7 @@ class MainApplication : Application(), Application.ActivityLifecycleCallbacks { } registerActivityLifecycleCallbacks(this) startListenNetworkState() + onAppStarted() } private fun scheduleAutoSyncWork(syncInterval: Int?) { @@ -127,13 +132,22 @@ class MainApplication : Application(), Application.ActivityLifecycleCallbacks { override fun onActivityCreated(activity: Activity, bundle: Bundle?) {} - override fun onActivityStarted(activity: Activity) {} + override fun onActivityStarted(activity: Activity) { + if (++activityReferences == 1 && !isActivityChangingConfigurations) { + onAppForegrounded() + } + } override fun onActivityResumed(activity: Activity) {} override fun onActivityPaused(activity: Activity) {} - override fun onActivityStopped(activity: Activity) {} + override fun onActivityStopped(activity: Activity) { + isActivityChangingConfigurations = activity.isChangingConfigurations + if (--activityReferences == 0 && !isActivityChangingConfigurations) { + onAppBackgrounded() + } + } override fun onActivitySaveInstanceState(activity: Activity, bundle: Bundle) {} @@ -141,6 +155,43 @@ class MainApplication : Application(), Application.ActivityLifecycleCallbacks { cancellAll(this) } + private fun onAppForegrounded() { + if (isFirstLaunch) { + isFirstLaunch = false + } else { + val fromForeground = "foreground" + createLog(fromForeground) + } + } + + private fun onAppBackgrounded() {} + + private fun onAppStarted() { + val newStart = "new launch" + createLog(newStart) + } + + private fun onAppClosed() {} + + private fun createLog(type: String) { + val service = DatabaseService(this) + val mRealm = service.realmInstance + if (!mRealm.isInTransaction) { + mRealm.beginTransaction() + } + val log = mRealm.createObject(RealmApkLog::class.java, "${UUID.randomUUID()}") + val model = UserProfileDbHandler(this).userModel + if (model != null) { + log.parentCode = model.parentCode + log.createdOn = model.planetCode + } + log.time = "${Date().time}" + log.page = "" + log.version = getVersionName(this) + log.type = type + mRealm.commitTransaction() + } + private fun handleUncaughtException(e: Throwable) { e.printStackTrace() val service = DatabaseService(this) @@ -162,7 +213,12 @@ class MainApplication : Application(), Application.ActivityLifecycleCallbacks { mRealm.commitTransaction() val homeIntent = Intent(Intent.ACTION_MAIN) homeIntent.addCategory(Intent.CATEGORY_HOME) - homeIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) + homeIntent.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP startActivity(homeIntent) } -} \ No newline at end of file + + override fun onTerminate() { + super.onTerminate() + onAppClosed() + } +} From b7852520537c6c1752644f651576209f47c81c77 Mon Sep 17 00:00:00 2001 From: dogi Date: Wed, 10 Jul 2024 15:04:13 -0400 Subject: [PATCH 2/2] Update build.gradle --- app/build.gradle | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 6e53b5b014..76dedd04d4 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -9,8 +9,8 @@ android { applicationId "org.ole.planet.myplanet" minSdkVersion 21 targetSdkVersion 34 - versionCode 1665 - versionName "0.16.65" + versionCode 1670 + versionName "0.16.70" ndkVersion '21.3.6528147' testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" vectorDrawables.useSupportLibrary = true