Skip to content

Commit

Permalink
Refactor: Initialize prefs in attachBaseContext for timely initializa…
Browse files Browse the repository at this point in the history
…tion.

Include missing ReportField declarations for ACRA configuration. Remove redundant battery receiver registration in HomeFragment.
  • Loading branch information
HeCodes2Much committed Apr 18, 2024
1 parent 6cfe07c commit 5a42051
Showing 1 changed file with 67 additions and 54 deletions.
121 changes: 67 additions & 54 deletions app/src/main/java/com/github/droidworksstudio/mlauncher/mLauncher.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import android.content.Context
import android.content.Intent
import android.os.Build
import com.github.droidworksstudio.mlauncher.data.Prefs
import org.acra.ReportField
import org.acra.config.dialog
import org.acra.config.mailSender
import org.acra.config.notification
Expand All @@ -16,62 +17,11 @@ import org.acra.ktx.initAcra
class Mlauncher : Application() {
private lateinit var prefs: Prefs

override fun onCreate() {
super.onCreate()

prefs = Prefs(baseContext)
createNotificationChannel()

// Check if notifications are enabled
if (!areNotificationsEnabled() && prefs.enableNotifications ) {
// Prompt the user to enable notifications
showNotificationSettings()
}
}

private fun createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val channelId = applicationInfo.uid.toString()
val channelName = getString(R.string.acra_send_report)
val importance = NotificationManager.IMPORTANCE_DEFAULT
val channel = NotificationChannel(channelId, channelName, importance).apply {
name = channelName
}

val notificationManager: NotificationManager =
getSystemService(NotificationManager::class.java)
?: return

notificationManager.createNotificationChannel(channel)
}
}

private fun areNotificationsEnabled(): Boolean {
val notificationManager =
getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager

// Check if notifications are enabled
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
notificationManager.areNotificationsEnabled()
} else {
true
}
}

private fun showNotificationSettings() {
val intent = Intent()
intent.action = "android.settings.APP_NOTIFICATION_SETTINGS"
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
intent.putExtra(android.provider.Settings.EXTRA_APP_PACKAGE, packageName)
} else {
intent.putExtra("app_package", packageName)
intent.putExtra("app_uid", applicationInfo.uid)
}
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
startActivity(intent)
}
override fun attachBaseContext(base: Context) {
super.attachBaseContext(base)
// Initialize prefs here
prefs = Prefs(base)

val pkgName = getString(R.string.app_name)
val pkgVersion = base.packageManager.getPackageInfo(
base.packageName,
Expand All @@ -82,6 +32,15 @@ class Mlauncher : Application() {
//core configuration:
buildConfigClass = BuildConfig::class.java
reportFormat = StringFormat.KEY_VALUE_LIST
reportContent = listOf(
ReportField.APP_VERSION_CODE,
ReportField.APP_VERSION_NAME,
ReportField.ANDROID_VERSION,
ReportField.PHONE_MODEL,
ReportField.CUSTOM_DATA,
ReportField.STACK_TRACE,
ReportField.LOGCAT
)
//each plugin you chose above can be configured in a block like this:
if (prefs.enableNotifications) {
notification {
Expand Down Expand Up @@ -127,4 +86,58 @@ class Mlauncher : Application() {
}
}
}

override fun onCreate() {
super.onCreate()

createNotificationChannel()

// Check if notifications are enabled
if (!areNotificationsEnabled() && prefs.enableNotifications ) {
// Prompt the user to enable notifications
showNotificationSettings()
}
}

private fun createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val channelId = applicationInfo.uid.toString()
val channelName = getString(R.string.acra_send_report)
val importance = NotificationManager.IMPORTANCE_DEFAULT
val channel = NotificationChannel(channelId, channelName, importance).apply {
name = channelName
}

val notificationManager: NotificationManager =
getSystemService(NotificationManager::class.java)
?: return

notificationManager.createNotificationChannel(channel)
}
}

private fun areNotificationsEnabled(): Boolean {
val notificationManager =
getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager

// Check if notifications are enabled
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
notificationManager.areNotificationsEnabled()
} else {
true
}
}

private fun showNotificationSettings() {
val intent = Intent()
intent.action = "android.settings.APP_NOTIFICATION_SETTINGS"
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
intent.putExtra(android.provider.Settings.EXTRA_APP_PACKAGE, packageName)
} else {
intent.putExtra("app_package", packageName)
intent.putExtra("app_uid", applicationInfo.uid)
}
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
startActivity(intent)
}
}

0 comments on commit 5a42051

Please sign in to comment.