Skip to content

Commit

Permalink
sync: revive crash logs (fixes #3491) (#3496)
Browse files Browse the repository at this point in the history
Co-authored-by: dogi <[email protected]>
  • Loading branch information
Okuro3499 and dogi authored May 23, 2024
1 parent 1bd1a98 commit 543bedc
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 11 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 1536
versionName "0.15.36"
versionCode 1537
versionName "0.15.37"
ndkVersion '21.3.6528147'
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables.useSupportLibrary = true
Expand Down
65 changes: 57 additions & 8 deletions app/src/main/java/org/ole/planet/myplanet/MainApplication.kt
Original file line number Diff line number Diff line change
@@ -1,25 +1,33 @@
package org.ole.planet.myplanet

import android.app.Activity
import android.app.Application
import android.content.Context
import android.content.Intent
import android.content.SharedPreferences
import android.os.Bundle
import android.os.StrictMode
import android.os.StrictMode.VmPolicy
import android.provider.Settings
import androidx.work.ExistingPeriodicWorkPolicy
import androidx.work.PeriodicWorkRequest
import androidx.work.WorkManager
import org.ole.planet.myplanet.callback.TeamPageListener
import org.ole.planet.myplanet.datamanager.DatabaseService
import org.ole.planet.myplanet.model.RealmApkLog
import org.ole.planet.myplanet.service.AutoSyncWorker
import org.ole.planet.myplanet.service.StayOnlineWorker
import org.ole.planet.myplanet.service.TaskNotificationWorker
import org.ole.planet.myplanet.ui.sync.SyncActivity
import org.ole.planet.myplanet.service.UserProfileDbHandler
import org.ole.planet.myplanet.utilities.Constants.PREFS_NAME
import org.ole.planet.myplanet.utilities.LocaleHelper
import org.ole.planet.myplanet.utilities.NotificationUtil.cancellAll
import org.ole.planet.myplanet.utilities.Utilities
import org.ole.planet.myplanet.utilities.VersionUtils.getVersionName
import java.util.*
import java.util.concurrent.TimeUnit

class MainApplication : Application() {
class MainApplication : Application(), Application.ActivityLifecycleCallbacks {
companion object {
private const val AUTO_SYNC_WORK_TAG = "autoSyncWork"
private const val STAY_ONLINE_WORK_TAG = "stayOnlineWork"
Expand Down Expand Up @@ -51,23 +59,23 @@ class MainApplication : Application() {
super.onCreate()
context = this
preferences = getSharedPreferences(PREFS_NAME, MODE_PRIVATE)

// Initialize libraries and settings
val builder = VmPolicy.Builder()
StrictMode.setVmPolicy(builder.build())
builder.detectFileUriExposure()

// Set up auto-sync using WorkManager
if (preferences?.getBoolean("autoSync", false) == true && preferences?.contains("autoSyncInterval")!!) {
if (preferences?.getBoolean("autoSync", false) == true && preferences?.contains("autoSyncInterval") == true) {
val syncInterval = preferences?.getInt("autoSyncInterval", 60 * 60)
scheduleAutoSyncWork(syncInterval)
} else {
cancelAutoSyncWork()
}

// Set up other periodic works using WorkManager
scheduleStayOnlineWork()
scheduleTaskNotificationWork()

Thread.setDefaultUncaughtExceptionHandler { _: Thread?, e: Throwable ->
handleUncaughtException(e)
}
registerActivityLifecycleCallbacks(this)
}

private fun scheduleAutoSyncWork(syncInterval: Int?) {
Expand Down Expand Up @@ -99,4 +107,45 @@ class MainApplication : Application() {
super.attachBaseContext(LocaleHelper.onAttach(base, "en"))
Utilities.setContext(base)
}

override fun onActivityCreated(activity: Activity, bundle: Bundle?) {}

override fun onActivityStarted(activity: Activity) {}

override fun onActivityResumed(activity: Activity) {}

override fun onActivityPaused(activity: Activity) {}

override fun onActivityStopped(activity: Activity) {}

override fun onActivitySaveInstanceState(activity: Activity, bundle: Bundle) {}

override fun onActivityDestroyed(activity: Activity) {
cancellAll(this)
}

private fun handleUncaughtException(e: Throwable) {
e.printStackTrace()
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 = RealmApkLog.ERROR_TYPE_CRASH
log.setError(e)
mRealm.commitTransaction()
val homeIntent = Intent(Intent.ACTION_MAIN)
homeIntent.addCategory(Intent.CATEGORY_HOME)
homeIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
startActivity(homeIntent)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ class LoginActivity : SyncActivity(), TeamListAdapter.OnItemClickListener {
becomeMember = activityLoginBinding.becomeMember
btnGuestLogin = activityLoginBinding.btnGuestLogin
imgBtnSetting = activityLoginBinding.imgBtnSetting
syncIcon = activityLoginBinding.syncIcon
lblVersion = activityLoginBinding.lblVersion
btnLang = activityLoginBinding.btnLang
tvAvailableSpace = activityLoginBinding.tvAvailableSpace
Expand Down

0 comments on commit 543bedc

Please sign in to comment.