Skip to content

Commit

Permalink
all: log launches (fixes #3810) (#3811)
Browse files Browse the repository at this point in the history
Co-authored-by: dogi <[email protected]>
  • Loading branch information
Okuro3499 and dogi authored Jul 10, 2024
1 parent b1a892b commit 3ac027b
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 7 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ android {
applicationId "org.ole.planet.myplanet"
minSdkVersion 21
targetSdkVersion 34
versionCode 1669
versionName "0.16.69"
versionCode 1670
versionName "0.16.70"
ndkVersion '21.3.6528147'
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables.useSupportLibrary = true
Expand Down
66 changes: 61 additions & 5 deletions app/src/main/java/org/ole/planet/myplanet/MainApplication.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -88,6 +92,7 @@ class MainApplication : Application(), Application.ActivityLifecycleCallbacks {
}
registerActivityLifecycleCallbacks(this)
startListenNetworkState()
onAppStarted()
}

private fun scheduleAutoSyncWork(syncInterval: Int?) {
Expand Down Expand Up @@ -127,20 +132,66 @@ 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) {}

override fun onActivityDestroyed(activity: Activity) {
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)
Expand All @@ -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)
}
}

override fun onTerminate() {
super.onTerminate()
onAppClosed()
}
}

0 comments on commit 3ac027b

Please sign in to comment.