Skip to content

Commit

Permalink
Refactor: Refactor app settings and notification handling
Browse files Browse the repository at this point in the history
This commit refactors various parts of the app related to settings management and notification handling:

- Removed redundant `versionNameSuffix` setting in the app's Gradle configuration.
- Moved the application-specific settings initialization to a dedicated `Prefs` class for better organization and maintainability.
- Added a new preference to enable or disable notifications in the app.
- Updated the `Mlauncher` class to check if notifications are enabled and prompt the user to enable them if necessary during app startup.
- Cleaned up unused imports and added missing comments for clarity.

These changes improve the code structure and enhance user experience by providing better control over notifications.
  • Loading branch information
HeCodes2Much committed Apr 17, 2024
1 parent 2c0279f commit 2885e77
Show file tree
Hide file tree
Showing 9 changed files with 169 additions and 1 deletion.
7 changes: 6 additions & 1 deletion app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ android {
isShrinkResources = false
isDebuggable = true
applicationIdSuffix = ".debug"
versionNameSuffix = "-debug"
resValue("string", "app_name", "mLauncher Debug")
}

Expand Down Expand Up @@ -119,6 +118,12 @@ dependencies {
implementation("androidx.compose.foundation:foundation:$androidxTestKotlin")
implementation("androidx.biometric:biometric-ktx:1.2.0-alpha05")

val acraVersion = "5.11.3"
implementation("ch.acra:acra-core:$acraVersion")
implementation("ch.acra:acra-dialog:$acraVersion")
implementation("ch.acra:acra-mail:$acraVersion")
implementation("ch.acra:acra-notification:$acraVersion")

val androidxTestEspresso = "3.5.1"
androidTestImplementation("androidx.test.espresso:espresso-core:$androidxTestEspresso")
androidTestImplementation("androidx.test.espresso:espresso-contrib:$androidxTestEspresso")
Expand Down
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<uses-permission android:name="android.permission.REQUEST_DELETE_PACKAGES" />

<application
android:name="com.github.droidworksstudio.mlauncher.Mlauncher"
android:enableOnBackInvokedCallback="true"
android:allowBackup="true"
android:icon="@drawable/app_launcher"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ private const val CLOCK_SIZE_TEXT = "CLOCK_SIZE_TEXT"
private const val TEXT_SIZE_SETTINGS = "TEXT_SIZE_SETTINGS"
private const val TEXT_MARGIN_SIZE = "TEXT_MARGIN_SIZE"

private const val ENABLE_NOTIFICATION_SERVICE = "ENABLE_NOTIFICATION_SERVICE"

class Prefs(val context: Context) {

private val prefs: SharedPreferences = context.getSharedPreferences(PREFS_FILENAME, 0)
Expand Down Expand Up @@ -114,6 +116,10 @@ class Prefs(val context: Context) {
get() = prefs.getBoolean(FIRST_OPEN, true)
set(value) = prefs.edit().putBoolean(FIRST_OPEN, value).apply()

var enableNotifications: Boolean
get() = prefs.getBoolean(ENABLE_NOTIFICATION_SERVICE, true)
set(value) = prefs.edit().putBoolean(ENABLE_NOTIFICATION_SERVICE, value).apply()

var firstSettingsOpen: Boolean
get() = prefs.getBoolean(FIRST_SETTINGS_OPEN, true)
set(value) = prefs.edit().putBoolean(FIRST_SETTINGS_OPEN, value).apply()
Expand Down
128 changes: 128 additions & 0 deletions app/src/main/java/com/github/droidworksstudio/mlauncher/mLauncher.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
package com.github.droidworksstudio.mlauncher

import android.app.Application
import android.app.NotificationChannel
import android.app.NotificationManager
import android.content.Context
import android.content.Intent
import android.os.Build
import com.github.droidworksstudio.mlauncher.data.Prefs
import org.acra.config.dialog
import org.acra.config.mailSender
import org.acra.config.notification
import org.acra.data.StringFormat
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)
val pkgName = getString(R.string.app_name)
val pkgVersion = base.packageManager.getPackageInfo(
base.packageName,
0
).versionName

initAcra {
//core configuration:
buildConfigClass = BuildConfig::class.java
reportFormat = StringFormat.KEY_VALUE_LIST
//each plugin you chose above can be configured in a block like this:
dialog {
//required
text = getString(R.string.acra_dialog_text).format(pkgName)
//optional, enables the dialog title
title = getString(R.string.acra_crash)
//defaults to android.R.string.ok
positiveButtonText = getString(R.string.acra_send_report)
//defaults to android.R.string.cancel
negativeButtonText = getString(R.string.acra_dont_send)
//optional, defaults to @android:style/Theme.Dialog
resTheme = R.style.MaterialDialogTheme
}

notification {
//required
title = getString(R.string.acra_dialog_text).format(pkgName)
//required
text = getString(R.string.acra_crash)
//required
channelName = getString(R.string.acra_send_report)
//defaults to android.R.string.ok
sendButtonText = getString(R.string.acra_send_report)
//defaults to android.R.string.cancel
discardButtonText = getString(R.string.acra_dont_send)
//defaults to false
sendOnClick = true
}

mailSender {
//required
mailTo = "wayne6324@gmail.com"
//defaults to true
reportAsFile = true
//defaults to ACRA-report.stacktrace
reportFileName = "$pkgName-crash-report.stacktrace"
//defaults to "<applicationId> Crash Report"
subject = "$pkgName $pkgVersion Crash Report"
//defaults to empty
body = getString(R.string.acra_mail_body)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ class HomeFragment : Fragment(), View.OnClickListener, View.OnLongClickListener
override fun onPause() {
super.onPause()
/* unregister battery changes if the receiver is registered */
requireContext().unregisterReceiver(batteryReceiver)
try {
requireContext().unregisterReceiver(batteryReceiver)
} catch (e: IllegalArgumentException) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,14 @@ class SettingsFragment : Fragment() {
state = remember { mutableStateOf(prefs.autoShowKeyboard) },
) { toggleKeyboardText() }
},
{ _, onChange ->
SettingsToggle(
title = stringResource(R.string.enable_notifications),
fontSize = iconFs,
onChange = onChange,
state = remember { mutableStateOf(prefs.enableNotifications) },
) { toggleEnableNotifications() }
},
{ _, onChange ->
SettingsToggle(
title = stringResource(R.string.display_recent_apps),
Expand Down Expand Up @@ -826,6 +834,10 @@ class SettingsFragment : Fragment() {
prefs.autoShowKeyboard = !prefs.autoShowKeyboard
}

private fun toggleEnableNotifications() {
prefs.enableNotifications = !prefs.enableNotifications
}

private fun toggleAutoOpenApp() {
prefs.autoOpenApp = !prefs.autoOpenApp
}
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/res/values-night/styles.xml
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,7 @@
<item name="android:layout_margin">18sp</item>
<item name="android:padding">10sp</item>
</style>
<style name="MaterialDialogTheme" parent="Theme.AppCompat.DayNight.Dialog">
<item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
</style>
</resources>
9 changes: 9 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
<!-- Behavior Strings -->
<string name="auto_show_keyboard">Auto Show Keyboard</string>
<string name="auto_open_apps">Auto Open Apps</string>
<string name="enable_notifications">Enable Notifications</string>
<string name="display_recent_apps">Display Recent Apps</string>
<string name="number_of_recents">Number of Recent Apps</string>
<string name="filter_strength">Filter Strength</string>
Expand Down Expand Up @@ -129,6 +130,14 @@
<string name="text_biometric_login">Biometric login for mLauncher</string>
<string name="text_biometric_login_sub">Log in using your biometric credentials.</string>

<!-- Crash Strings -->
<string name="acra_crash">Crash</string>
<string name="acra_dialog_text">We\'re sorry, but %1$s has encountered an unexpected issue and needs to close. Please help us improve by sending a crash report.\n\n%1$s values your privacy and the data collected is solely used for improving app stability. No personal information is collected. Thank you for your support!</string>
<string name="acra_send_report">Send Report</string>
<string name="acra_dont_send">Don\'t Send</string>
<string name="acra_mail_body">Please describe the issue you encountered:</string>


<!-- Unsorted Strings -->
<string name="show_apps">All apps</string>
<string name="can_not_delete_system_apps">Can\'t Delete System Apps</string>
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,7 @@
<item name="android:layout_margin">18sp</item>
<item name="android:padding">10sp</item>
</style>
<style name="MaterialDialogTheme" parent="Theme.AppCompat.DayNight.Dialog">
<item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
</style>
</resources>

0 comments on commit 2885e77

Please sign in to comment.