-
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor: Refactor app settings and notification handling
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
1 parent
2c0279f
commit 2885e77
Showing
9 changed files
with
169 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
128 changes: 128 additions & 0 deletions
128
app/src/main/java/com/github/droidworksstudio/mlauncher/mLauncher.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters