From fe3c0499999995f24d46fbeb4f25da5fbec8f45f Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Sat, 20 Feb 2021 19:34:19 +0100 Subject: [PATCH] Nitification store logging --- .../notifications/NotificationStore.kt | 74 +++++++------------ .../DummyServiceHelper.kt | 5 +- .../res/layout/overview_notification_item.xml | 12 +-- .../services/AlarmSoundServiceHelper.kt | 10 ++- .../utils/protection/PasswordCheck.kt | 3 - 5 files changed, 42 insertions(+), 62 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/notifications/NotificationStore.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/notifications/NotificationStore.kt index a736e78367..3d8b34d1b4 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/notifications/NotificationStore.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/notifications/NotificationStore.kt @@ -1,6 +1,5 @@ package info.nightscout.androidaps.plugins.general.overview.notifications -import android.annotation.SuppressLint import android.app.NotificationChannel import android.app.NotificationManager import android.app.PendingIntent @@ -11,14 +10,10 @@ import android.media.RingtoneManager import android.view.LayoutInflater import android.view.View import android.view.ViewGroup -import android.widget.Button -import android.widget.TextView -import androidx.cardview.widget.CardView import androidx.core.app.NotificationCompat -import androidx.core.app.TaskStackBuilder import androidx.recyclerview.widget.RecyclerView -import info.nightscout.androidaps.MainActivity import info.nightscout.androidaps.R +import info.nightscout.androidaps.databinding.OverviewNotificationItemBinding import info.nightscout.androidaps.logging.AAPSLogger import info.nightscout.androidaps.logging.LTag import info.nightscout.androidaps.plugins.bus.RxBusWrapper @@ -45,8 +40,7 @@ class NotificationStore @Inject constructor( private val dateUtil: DateUtil ) { - var store: MutableList = ArrayList() - private var usesChannels = false + private var store: MutableList = ArrayList() companion object { @@ -71,33 +65,35 @@ class NotificationStore @Inject constructor( } } store.add(n) - if (sp.getBoolean(R.string.key_raise_notifications_as_android_notifications, true) && n !is NotificationWithAction) { + if (sp.getBoolean(R.string.key_raise_notifications_as_android_notifications, true) && n !is NotificationWithAction) raiseSystemNotification(n) - if (usesChannels && n.soundId != null && n.soundId != 0) alarmSoundServiceHelper.startAlarm(context, n.soundId) - } else { - if (n.soundId != null && n.soundId != 0) alarmSoundServiceHelper.startAlarm(context, n.soundId) - } + if (n.soundId != null && n.soundId != 0) alarmSoundServiceHelper.startAlarm(context, n.soundId) Collections.sort(store, NotificationComparator()) return true } - @Synchronized fun remove(id: Int): Boolean { + @Synchronized + fun remove(id: Int): Boolean { for (i in store.indices) { if (store[i].id == id) { if (store[i].soundId != null) alarmSoundServiceHelper.stopService(context) store.removeAt(i) + aapsLogger.debug(LTag.NOTIFICATION, "Notification removed: " + store[i].text) return true } } return false } - @Synchronized private fun removeExpired() { + @Synchronized + private fun removeExpired() { var i = 0 while (i < store.size) { val n = store[i] if (n.validTo != 0L && n.validTo < System.currentTimeMillis()) { + if (store[i].soundId != null) alarmSoundServiceHelper.stopService(context) store.removeAt(i) + aapsLogger.debug(LTag.NOTIFICATION, "Notification expired: " + store[i].text) i-- } i++ @@ -135,20 +131,17 @@ class NotificationStore @Inject constructor( } fun createNotificationChannel() { - usesChannels = true val mNotificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager - @SuppressLint("WrongConstant") val channel = NotificationChannel(CHANNEL_ID, - CHANNEL_ID, - NotificationManager.IMPORTANCE_HIGH) + val channel = NotificationChannel(CHANNEL_ID, CHANNEL_ID, NotificationManager.IMPORTANCE_HIGH) mNotificationManager.createNotificationChannel(channel) } @Synchronized fun updateNotifications(notificationsView: RecyclerView) { removeExpired() -// unSnooze() - if (store.size > 0) { - val adapter = NotificationRecyclerViewAdapter(cloneStore()) + val clonedStore = ArrayList(store) + if (clonedStore.isNotEmpty()) { + val adapter = NotificationRecyclerViewAdapter(clonedStore) notificationsView.adapter = adapter notificationsView.visibility = View.VISIBLE } else { @@ -156,33 +149,24 @@ class NotificationStore @Inject constructor( } } - @Synchronized - private fun cloneStore(): List { - val clone: MutableList = ArrayList(store.size) - clone.addAll(store) - return clone - } - inner class NotificationRecyclerViewAdapter internal constructor(private val notificationsList: List) : RecyclerView.Adapter() { - override fun onCreateViewHolder(viewGroup: ViewGroup, viewType: Int): NotificationsViewHolder { - val v = LayoutInflater.from(viewGroup.context).inflate(R.layout.overview_notification_item, viewGroup, false) - return NotificationsViewHolder(v) - } + override fun onCreateViewHolder(viewGroup: ViewGroup, viewType: Int): NotificationsViewHolder = + NotificationsViewHolder(LayoutInflater.from(viewGroup.context).inflate(R.layout.overview_notification_item, viewGroup, false)) override fun onBindViewHolder(holder: NotificationsViewHolder, position: Int) { val notification = notificationsList[position] - holder.dismiss.tag = notification - if (notification.buttonText != 0) holder.dismiss.setText(notification.buttonText) - else holder.dismiss.setText(R.string.snooze) + holder.binding.dismiss.tag = notification + if (notification.buttonText != 0) holder.binding.dismiss.setText(notification.buttonText) + else holder.binding.dismiss.setText(R.string.snooze) @Suppress("SetTextI18n") - holder.text.text = dateUtil.timeString(notification.date) + " " + notification.text + holder.binding.text.text = dateUtil.timeString(notification.date) + " " + notification.text when (notification.level) { - Notification.URGENT -> holder.cv.setBackgroundColor(resourceHelper.gc(R.color.notificationUrgent)) - Notification.NORMAL -> holder.cv.setBackgroundColor(resourceHelper.gc(R.color.notificationNormal)) - Notification.LOW -> holder.cv.setBackgroundColor(resourceHelper.gc(R.color.notificationLow)) - Notification.INFO -> holder.cv.setBackgroundColor(resourceHelper.gc(R.color.notificationInfo)) - Notification.ANNOUNCEMENT -> holder.cv.setBackgroundColor(resourceHelper.gc(R.color.notificationAnnouncement)) + Notification.URGENT -> holder.binding.cv.setBackgroundColor(resourceHelper.gc(R.color.notificationUrgent)) + Notification.NORMAL -> holder.binding.cv.setBackgroundColor(resourceHelper.gc(R.color.notificationNormal)) + Notification.LOW -> holder.binding.cv.setBackgroundColor(resourceHelper.gc(R.color.notificationLow)) + Notification.INFO -> holder.binding.cv.setBackgroundColor(resourceHelper.gc(R.color.notificationInfo)) + Notification.ANNOUNCEMENT -> holder.binding.cv.setBackgroundColor(resourceHelper.gc(R.color.notificationAnnouncement)) } } @@ -192,12 +176,10 @@ class NotificationStore @Inject constructor( inner class NotificationsViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { - var cv: CardView = itemView.findViewById(R.id.notification_cardview) - var text: TextView = itemView.findViewById(R.id.notification_text) - var dismiss: Button = itemView.findViewById(R.id.notification_dismiss) + val binding = OverviewNotificationItemBinding.bind(itemView) init { - dismiss.setOnClickListener { + binding.dismiss.setOnClickListener { val notification = it.tag as Notification rxBus.send(EventDismissNotification(notification.id)) notification.action?.run() diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/persistentNotification/DummyServiceHelper.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/persistentNotification/DummyServiceHelper.kt index d1adfc00ed..5f52035bac 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/persistentNotification/DummyServiceHelper.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/persistentNotification/DummyServiceHelper.kt @@ -4,9 +4,7 @@ import android.content.ComponentName import android.content.Context import android.content.Intent import android.content.ServiceConnection -import android.os.Build import android.os.IBinder -import androidx.annotation.RequiresApi import info.nightscout.androidaps.interfaces.NotificationHolderInterface import javax.inject.Inject import javax.inject.Singleton @@ -14,14 +12,13 @@ import javax.inject.Singleton /* This code replaces following val alarm = Intent(context, DummyService::class.java) - alarm.putExtra("soundid", n.soundId) + alarm.putExtra("soundId", n.soundId) if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) context.startForegroundService(alarm) else context.startService(alarm) it fails randomly with error Context.startForegroundService() did not then call Service.startForeground(): ServiceRecord{e317f7e u0 info.nightscout.nsclient/info.nightscout.androidaps.services.DummyService} */ -@RequiresApi(Build.VERSION_CODES.O) @Singleton class DummyServiceHelper @Inject constructor( private val notificationHolder: NotificationHolderInterface diff --git a/app/src/main/res/layout/overview_notification_item.xml b/app/src/main/res/layout/overview_notification_item.xml index 03b5688fc3..f8f1974d33 100644 --- a/app/src/main/res/layout/overview_notification_item.xml +++ b/app/src/main/res/layout/overview_notification_item.xml @@ -1,7 +1,8 @@ + android:text="Notification text. Notification text. Notification text. Notification text. Notification text. Notification text. " + tools:ignore="HardcodedText" />