From 2fe37a16915ec129c69688a44989035b349179c7 Mon Sep 17 00:00:00 2001 From: Matthieu <24-artectrex@users.noreply.shinice.net> Date: Sat, 25 Sep 2021 13:43:04 +0200 Subject: [PATCH] Make notification channels on adding account --- .../java/org/pixeldroid/app/LoginActivity.kt | 11 ++ .../java/org/pixeldroid/app/MainActivity.kt | 2 +- .../utils/db/entities/UserDatabaseEntity.kt | 5 +- .../NotificationsWorker.kt | 101 +++++++++--------- 4 files changed, 67 insertions(+), 52 deletions(-) diff --git a/app/src/main/java/org/pixeldroid/app/LoginActivity.kt b/app/src/main/java/org/pixeldroid/app/LoginActivity.kt index 4500d758..a97c89ff 100644 --- a/app/src/main/java/org/pixeldroid/app/LoginActivity.kt +++ b/app/src/main/java/org/pixeldroid/app/LoginActivity.kt @@ -9,6 +9,8 @@ import android.os.Bundle import android.view.View import android.view.inputmethod.InputMethodManager import androidx.lifecycle.lifecycleScope +import androidx.work.OneTimeWorkRequestBuilder +import androidx.work.WorkManager import org.pixeldroid.app.databinding.ActivityLoginBinding import org.pixeldroid.app.utils.* import org.pixeldroid.app.utils.api.PixelfedAPI @@ -16,6 +18,9 @@ import org.pixeldroid.app.utils.api.objects.* import org.pixeldroid.app.utils.db.addUser import org.pixeldroid.app.utils.db.storeInstance import kotlinx.coroutines.* +import org.pixeldroid.app.utils.notificationsWorker.NotificationsWorker +import org.pixeldroid.app.utils.notificationsWorker.makeChannelGroupId +import org.pixeldroid.app.utils.notificationsWorker.makeNotificationChannels import retrofit2.HttpException import java.io.IOException import java.lang.IllegalArgumentException @@ -340,6 +345,12 @@ class LoginActivity : BaseActivity() { } catch (exception: NullPointerException) { return failedRegistration(getString(R.string.login_notifications)) } + + makeNotificationChannels( + applicationContext, + user.fullHandle, + makeChannelGroupId(user) + ) } } diff --git a/app/src/main/java/org/pixeldroid/app/MainActivity.kt b/app/src/main/java/org/pixeldroid/app/MainActivity.kt index 3b84304e..62444f56 100644 --- a/app/src/main/java/org/pixeldroid/app/MainActivity.kt +++ b/app/src/main/java/org/pixeldroid/app/MainActivity.kt @@ -312,7 +312,7 @@ class MainActivity : BaseActivity() { iconUrl = user.avatar_static isNameShown = true identifier = user.user_id.toLong() - descriptionText = "@${user.username}@${user.instance_uri.removePrefix("https://")}" + descriptionText = user.fullHandle } }.toMutableList() diff --git a/app/src/main/java/org/pixeldroid/app/utils/db/entities/UserDatabaseEntity.kt b/app/src/main/java/org/pixeldroid/app/utils/db/entities/UserDatabaseEntity.kt index cf6e451f..6aa92cdb 100644 --- a/app/src/main/java/org/pixeldroid/app/utils/db/entities/UserDatabaseEntity.kt +++ b/app/src/main/java/org/pixeldroid/app/utils/db/entities/UserDatabaseEntity.kt @@ -28,4 +28,7 @@ data class UserDatabaseEntity( val refreshToken: String?, val clientId: String, val clientSecret: String -): Serializable \ No newline at end of file +): Serializable { + val fullHandle: String + get() = "@${username}@${instance_uri.removePrefix("https://")}" +} \ No newline at end of file diff --git a/app/src/main/java/org/pixeldroid/app/utils/notificationsWorker/NotificationsWorker.kt b/app/src/main/java/org/pixeldroid/app/utils/notificationsWorker/NotificationsWorker.kt index baa7cb60..ddac7f4c 100644 --- a/app/src/main/java/org/pixeldroid/app/utils/notificationsWorker/NotificationsWorker.kt +++ b/app/src/main/java/org/pixeldroid/app/utils/notificationsWorker/NotificationsWorker.kt @@ -51,7 +51,8 @@ class NotificationsWorker( val uniqueUserId = makeChannelGroupId(user) val notificationsEnabledForUser = makeNotificationChannels( - "@${user.username}@${user.instance_uri.removePrefix("https://")}", + applicationContext, + user.fullHandle, uniqueUserId ) @@ -176,54 +177,6 @@ class NotificationsWorker( } } - private fun makeNotificationChannels(handle: String, channelGroupId: String): Boolean { - val notificationManager: NotificationManager = - applicationContext.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager - // Create the NotificationChannel, but only on API 26+ because - // the NotificationChannel class is new and not in the support library - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - // The id of the group, hashed (since when creating the group, it may be truncated if too long) - val hashedGroupId = channelGroupId.hashCode().toString() - notificationManager.createNotificationChannelGroup(NotificationChannelGroup(hashedGroupId, handle)) - - val importance = NotificationManager.IMPORTANCE_DEFAULT - - val channels: List = listOf( - NotificationChannel(makeChannelId(channelGroupId, follow), applicationContext.getString(R.string.followed_notification_channel), importance), - NotificationChannel(makeChannelId(channelGroupId, mention), applicationContext.getString(R.string.mention_notification_channel), importance), - NotificationChannel(makeChannelId(channelGroupId, reblog), applicationContext.getString(R.string.shared_notification_channel), importance), - NotificationChannel(makeChannelId(channelGroupId, favourite), applicationContext.getString(R.string.liked_notification_channel), importance), - NotificationChannel(makeChannelId(channelGroupId, comment), applicationContext.getString(R.string.comment_notification_channel), importance), - NotificationChannel(makeChannelId(channelGroupId, poll), applicationContext.getString(R.string.poll_notification_channel), importance), - NotificationChannel(makeChannelId(channelGroupId, null), applicationContext.getString(R.string.other_notification_channel), importance), - ).map { - it.apply { group = hashedGroupId } - } - - // Register the channels with the system - notificationManager.createNotificationChannels(channels) - - //Return true if notifications are enabled, false if disabled - return notificationManager.areNotificationsEnabled() and - (if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { - val channelGroup = - notificationManager.getNotificationChannelGroup(hashedGroupId) - !channelGroup.isBlocked - } else true) and - (if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { - !notificationManager.areNotificationsPaused() - } else true) and - !channels.all { - notificationManager.getNotificationChannel(it.id).importance <= NotificationManager.IMPORTANCE_NONE - } - } - return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { - notificationManager.areNotificationsEnabled() - } else { - true - } - } - companion object { const val SHOW_NOTIFICATION_TAG = "org.pixeldroid.app.SHOW_NOTIFICATION" const val INSTANCE_NOTIFICATION_TAG = "org.pixeldroid.app.USER_NOTIFICATION" @@ -234,6 +187,54 @@ class NotificationsWorker( } +fun makeNotificationChannels(context: Context, handle: String, channelGroupId: String): Boolean { + val notificationManager: NotificationManager = + context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager + // Create the NotificationChannel, but only on API 26+ because + // the NotificationChannel class is new and not in the support library + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + // The id of the group, hashed (since when creating the group, it may be truncated if too long) + val hashedGroupId = channelGroupId.hashCode().toString() + notificationManager.createNotificationChannelGroup(NotificationChannelGroup(hashedGroupId, handle)) + + val importance = NotificationManager.IMPORTANCE_DEFAULT + + val channels: List = listOf( + NotificationChannel(makeChannelId(channelGroupId, follow), context.getString(R.string.followed_notification_channel), importance), + NotificationChannel(makeChannelId(channelGroupId, mention), context.getString(R.string.mention_notification_channel), importance), + NotificationChannel(makeChannelId(channelGroupId, reblog), context.getString(R.string.shared_notification_channel), importance), + NotificationChannel(makeChannelId(channelGroupId, favourite), context.getString(R.string.liked_notification_channel), importance), + NotificationChannel(makeChannelId(channelGroupId, comment), context.getString(R.string.comment_notification_channel), importance), + NotificationChannel(makeChannelId(channelGroupId, poll), context.getString(R.string.poll_notification_channel), importance), + NotificationChannel(makeChannelId(channelGroupId, null), context.getString(R.string.other_notification_channel), importance), + ).map { + it.apply { group = hashedGroupId } + } + + // Register the channels with the system + notificationManager.createNotificationChannels(channels) + + //Return true if notifications are enabled, false if disabled + return notificationManager.areNotificationsEnabled() and + (if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { + val channelGroup = + notificationManager.getNotificationChannelGroup(hashedGroupId) + !channelGroup.isBlocked + } else true) and + (if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { + !notificationManager.areNotificationsPaused() + } else true) and + !channels.all { + notificationManager.getNotificationChannel(it.id).importance <= NotificationManager.IMPORTANCE_NONE + } + } + return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { + notificationManager.areNotificationsEnabled() + } else { + true + } +} + /** * [channelGroupId] is the id used to uniquely identify the group: for us it is a unique id * identifying a user consisting of the concatenation of the instance uri and user id. @@ -241,7 +242,7 @@ class NotificationsWorker( private fun makeChannelId(channelGroupId: String, type: Notification.NotificationType?): String = (channelGroupId + (type ?: NotificationsWorker.otherNotificationType)).hashCode().toString() -private fun makeChannelGroupId(user: UserDatabaseEntity) = user.instance_uri + user.user_id +fun makeChannelGroupId(user: UserDatabaseEntity) = user.instance_uri + user.user_id fun removeNotificationChannelsFromAccount(context: Context, user: UserDatabaseEntity?) = user?.let {