Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

all: log launches (fixes #3810) #3811

Merged
merged 3 commits into from
Jul 10, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
}
}