diff options
author | Allan Wang <me@allanwang.ca> | 2019-04-24 19:12:25 -0700 |
---|---|---|
committer | Allan Wang <me@allanwang.ca> | 2019-04-24 19:12:25 -0700 |
commit | 7e227078373adf0ef8c243b7e1e05804524cd3ea (patch) | |
tree | bd6585c85a7641555e4bd9c10fa919145a5ba6df /app | |
parent | 9df2a878226fb5f7adcf1af61a1ce87dff826fbc (diff) | |
download | frost-7e227078373adf0ef8c243b7e1e05804524cd3ea.tar.gz frost-7e227078373adf0ef8c243b7e1e05804524cd3ea.tar.bz2 frost-7e227078373adf0ef8c243b7e1e05804524cd3ea.zip |
Add click events to notifications
Diffstat (limited to 'app')
8 files changed, 122 insertions, 36 deletions
diff --git a/app/src/androidTest/kotlin/com/pitchedapps/frost/db/NotificationDbTest.kt b/app/src/androidTest/kotlin/com/pitchedapps/frost/db/NotificationDbTest.kt index 9167c7bf..45a09cbe 100644 --- a/app/src/androidTest/kotlin/com/pitchedapps/frost/db/NotificationDbTest.kt +++ b/app/src/androidTest/kotlin/com/pitchedapps/frost/db/NotificationDbTest.kt @@ -38,7 +38,8 @@ class NotificationDbTest : BaseDbTest() { title = null, text = "", timestamp = time, - profileUrl = null + profileUrl = null, + unread = true ) @Test diff --git a/app/src/main/kotlin/com/pitchedapps/frost/services/FrostNotifications.kt b/app/src/main/kotlin/com/pitchedapps/frost/services/FrostNotifications.kt index 68ed859c..1c37bc29 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/services/FrostNotifications.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/services/FrostNotifications.kt @@ -61,7 +61,7 @@ private val _40_DP = 40.dpToPx * Enum to handle notification creations */ enum class NotificationType( - private val channelId: String, + val channelId: String, private val overlayContext: OverlayContext, private val fbItem: FbItem, private val parser: FrostParser<ParseNotification>, @@ -95,8 +95,8 @@ enum class NotificationType( */ internal open fun bindRequest(content: NotificationContent, cookie: String): (BaseBundle.() -> Unit)? = null - private fun bindRequest(intent: Intent, content: NotificationContent, cookie: String?) { - cookie ?: return + private fun bindRequest(intent: Intent, content: NotificationContent) { + val cookie = content.data.cookie ?: return val binder = bindRequest(content, cookie) ?: return val bundle = Bundle() bundle.binder() @@ -188,17 +188,33 @@ enum class NotificationType( } /** + * Attach content related data to an intent + */ + fun putContentExtra(intent: Intent, content: NotificationContent): Intent { + // We will show the notification page for dependent urls. We can trigger a click next time + intent.data = Uri.parse(if (content.href.isIndependent) content.href else FbItem.NOTIFICATIONS.url) + bindRequest(intent, content) + return intent + } + + /** + * Create a generic content for the provided type and user id. + * No content related data is added + */ + fun createCommonIntent(context: Context, userId: Long): Intent { + val intent = Intent(context, FrostWebActivity::class.java) + intent.putExtra(ARG_USER_ID, userId) + overlayContext.put(intent) + return intent + } + + /** * Create and submit a new notification with the given [content] */ private fun createNotification(context: Context, content: NotificationContent): FrostNotification = with(content) { - val intent = Intent(context, FrostWebActivity::class.java) - // TODO temp fix; we will show notification page for dependent urls. We can trigger a click next time - intent.data = Uri.parse(if (href.isIndependent) href else FbItem.NOTIFICATIONS.url) - intent.putExtra(ARG_USER_ID, data.id) - overlayContext.put(intent) - bindRequest(intent, content, data.cookie) - + val intent = createCommonIntent(context, content.data.id) + putContentExtra(intent, content) val group = "${groupPrefix}_${data.id}" val pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT) val notifBuilder = context.frostNotification(channelId) diff --git a/app/src/main/kotlin/com/pitchedapps/frost/utils/TimeUtils.kt b/app/src/main/kotlin/com/pitchedapps/frost/utils/TimeUtils.kt new file mode 100644 index 00000000..1ca6333b --- /dev/null +++ b/app/src/main/kotlin/com/pitchedapps/frost/utils/TimeUtils.kt @@ -0,0 +1,2 @@ +package com.pitchedapps.frost.utils + diff --git a/app/src/main/kotlin/com/pitchedapps/frost/utils/Utils.kt b/app/src/main/kotlin/com/pitchedapps/frost/utils/Utils.kt index 711d7e18..76ffd8cd 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/utils/Utils.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/utils/Utils.kt @@ -186,7 +186,7 @@ fun MaterialDialog.Builder.theme(): MaterialDialog.Builder { } fun Activity.setFrostTheme(forceTransparent: Boolean = false) { - val isTransparent = (Color.alpha(Prefs.bgColor) != 255) || forceTransparent + val isTransparent = (Color.alpha(Prefs.bgColor) != 255) || (Color.alpha(Prefs.headerColor) != 255) || forceTransparent if (Prefs.bgColor.isColorDark) setTheme(if (isTransparent) R.style.FrostTheme_Transparent else R.style.FrostTheme) else diff --git a/app/src/main/kotlin/com/pitchedapps/frost/widgets/NotificationWidget.kt b/app/src/main/kotlin/com/pitchedapps/frost/widgets/NotificationWidget.kt index 4ddd7225..ae45fa7b 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/widgets/NotificationWidget.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/widgets/NotificationWidget.kt @@ -16,35 +16,69 @@ */ package com.pitchedapps.frost.widgets +import android.app.PendingIntent import android.appwidget.AppWidgetManager import android.appwidget.AppWidgetProvider import android.content.Context import android.content.Intent +import android.graphics.Bitmap +import android.graphics.BitmapFactory +import android.graphics.Canvas +import android.graphics.Paint +import android.graphics.PorterDuff +import android.graphics.PorterDuffColorFilter +import android.graphics.drawable.Icon +import android.os.Build import android.widget.RemoteViews import android.widget.RemoteViewsService import androidx.annotation.ColorInt +import androidx.annotation.DrawableRes +import androidx.annotation.IdRes import ca.allanwang.kau.utils.dimenPixelSize +import ca.allanwang.kau.utils.string import ca.allanwang.kau.utils.withAlpha import com.pitchedapps.frost.R +import com.pitchedapps.frost.activities.MainActivity import com.pitchedapps.frost.db.NotificationDao import com.pitchedapps.frost.db.selectNotificationsSync import com.pitchedapps.frost.glide.FrostGlide import com.pitchedapps.frost.glide.GlideApp -import com.pitchedapps.frost.services.NOTIF_CHANNEL_GENERAL import com.pitchedapps.frost.services.NotificationContent +import com.pitchedapps.frost.services.NotificationType import com.pitchedapps.frost.utils.L import com.pitchedapps.frost.utils.Prefs import org.koin.standalone.KoinComponent import org.koin.standalone.inject +import java.text.DateFormat class NotificationWidget : AppWidgetProvider() { override fun onUpdate(context: Context, appWidgetManager: AppWidgetManager, appWidgetIds: IntArray) { super.onUpdate(context, appWidgetManager, appWidgetIds) - val intent = NotificationWidgetService.createIntent(context, NOTIF_CHANNEL_GENERAL) + val type = NotificationType.GENERAL + val userId = Prefs.userId + val intent = NotificationWidgetService.createIntent(context, type, userId) for (id in appWidgetIds) { val views = RemoteViews(context.packageName, R.layout.widget_notifications) - views.setBackgroundColor(R.id.widget_layout_container, Prefs.bgColor) + + views.setBackgroundColor(R.id.widget_layout_toolbar, Prefs.headerColor) + views.setIcon(R.id.img_frost, context, R.drawable.frost_f_24, Prefs.iconColor) + views.setOnClickPendingIntent( + R.id.img_frost, + PendingIntent.getActivity(context, 0, Intent(context, MainActivity::class.java), 0) + ) + + views.setBackgroundColor(R.id.widget_notification_list, Prefs.bgColor) views.setRemoteAdapter(R.id.widget_notification_list, intent) + + val pendingIntentTemplate = PendingIntent.getActivity( + context, + 0, + type.createCommonIntent(context, userId), + PendingIntent.FLAG_UPDATE_CURRENT + ) + + views.setPendingIntentTemplate(R.id.widget_notification_list, pendingIntentTemplate) + appWidgetManager.updateAppWidget(id, views) } appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetIds, R.id.widget_notification_list) @@ -52,18 +86,44 @@ class NotificationWidget : AppWidgetProvider() { } private const val NOTIF_WIDGET_TYPE = "notif_widget_type" +private const val NOTIF_WIDGET_USER_ID = "notif_widget_user_id" +private const val NOTIF_WIDGET_USER_COOKIE = "notif_widget_user_id" -private fun RemoteViews.setBackgroundColor(viewId: Int, @ColorInt color: Int) { +private fun RemoteViews.setBackgroundColor(@IdRes viewId: Int, @ColorInt color: Int) { setInt(viewId, "setBackgroundColor", color) } +/** + * Adds backward compatibility to setting tinted icons + */ +private fun RemoteViews.setIcon(@IdRes viewId: Int, context: Context, @DrawableRes res: Int, @ColorInt color: Int) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + val icon = Icon.createWithResource(context, res).setTint(color).setTintMode(PorterDuff.Mode.SRC_IN) + setImageViewIcon(viewId, icon) + } else { + val bitmap = BitmapFactory.decodeResource(context.resources, res) + if (bitmap != null) { + val paint = Paint() + paint.colorFilter = PorterDuffColorFilter(color, PorterDuff.Mode.SRC_IN) + val result = Bitmap.createBitmap(bitmap.width, bitmap.height, Bitmap.Config.ARGB_8888) + val canvas = Canvas(result) + canvas.drawBitmap(bitmap, 0f, 0f, paint) + setImageViewBitmap(viewId, result) + } else { + // Fallback to just icon + setImageViewResource(viewId, res) + } + } +} + class NotificationWidgetService : RemoteViewsService() { override fun onGetViewFactory(intent: Intent): RemoteViewsFactory = NotificationWidgetDataProvider(this, intent) companion object { - fun createIntent(context: Context, type: String): Intent = + fun createIntent(context: Context, type: NotificationType, userId: Long): Intent = Intent(context, NotificationWidgetService::class.java) - .putExtra(NOTIF_WIDGET_TYPE, type) + .putExtra(NOTIF_WIDGET_TYPE, type.name) + .putExtra(NOTIF_WIDGET_USER_ID, userId) } } @@ -74,14 +134,16 @@ class NotificationWidgetDataProvider(val context: Context, val intent: Intent) : @Volatile private var content: List<NotificationContent> = emptyList() - private val type = intent.getStringExtra(NOTIF_WIDGET_TYPE) + private val type = NotificationType.valueOf(intent.getStringExtra(NOTIF_WIDGET_TYPE)) + + private val userId = intent.getLongExtra(NOTIF_WIDGET_USER_ID, -1) private val avatarSize = context.dimenPixelSize(R.dimen.avatar_image_size) private val glide = GlideApp.with(context).asBitmap() private fun loadNotifications() { - content = notifDao.selectNotificationsSync(Prefs.userId, type) + content = notifDao.selectNotificationsSync(userId, type.channelId) L._d { "Updated notif widget with ${content.size} items" } } @@ -107,9 +169,10 @@ class NotificationWidgetDataProvider(val context: Context, val intent: Intent) : views.setTextViewText(R.id.item_content, notif.text) views.setTextColor(R.id.item_date, Prefs.textColor.withAlpha(150)) views.setTextViewText(R.id.item_date, notif.timestamp.toString()) // TODO -// views.setOnClickPendingIntent() + val avatar = glide.load(notif.profileUrl).transform(FrostGlide.circleCrop).submit(avatarSize, avatarSize).get() views.setImageViewBitmap(R.id.item_avatar, avatar) + views.setOnClickFillInIntent(R.id.item_frame, type.putContentExtra(Intent(), notif)) return views } diff --git a/app/src/main/res/layout/widget_notification_item.xml b/app/src/main/res/layout/widget_notification_item.xml index d02e2611..f36f2766 100644 --- a/app/src/main/res/layout/widget_notification_item.xml +++ b/app/src/main/res/layout/widget_notification_item.xml @@ -1,22 +1,19 @@ <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" - xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/item_frame" android:layout_width="match_parent" android:layout_height="wrap_content" - android:clickable="true" - android:focusable="true" + android:background="?android:selectableItemBackground" android:orientation="horizontal" android:paddingStart="@dimen/kau_activity_horizontal_margin" android:paddingTop="@dimen/kau_activity_vertical_margin" + android:paddingEnd="@dimen/kau_activity_horizontal_margin" android:paddingBottom="@dimen/kau_activity_vertical_margin"> <ImageView android:id="@+id/item_avatar" android:layout_width="@dimen/avatar_image_size" - android:layout_height="@dimen/avatar_image_size" - app:layout_constraintStart_toStartOf="parent" - app:layout_constraintTop_toTopOf="parent" /> + android:layout_height="@dimen/avatar_image_size" /> <!-- Unlike the actual notification panel, @@ -26,6 +23,7 @@ <LinearLayout android:layout_width="0dp" android:layout_height="wrap_content" + android:layout_marginStart="@dimen/kau_padding_normal" android:layout_weight="1" android:orientation="vertical"> @@ -33,8 +31,6 @@ android:id="@+id/item_content" android:layout_width="match_parent" android:layout_height="wrap_content" - android:layout_marginStart="@dimen/kau_padding_normal" - android:layout_marginEnd="@dimen/kau_padding_normal" android:ellipsize="end" android:lines="2" /> @@ -42,12 +38,9 @@ android:id="@+id/item_date" android:layout_width="match_parent" android:layout_height="wrap_content" - android:layout_marginStart="@dimen/kau_padding_normal" - android:layout_marginEnd="@dimen/kau_padding_normal" android:ellipsize="end" android:lines="1" android:textSize="12sp" /> </LinearLayout> - </LinearLayout>
\ No newline at end of file diff --git a/app/src/main/res/layout/widget_notifications.xml b/app/src/main/res/layout/widget_notifications.xml index 098bb9eb..4c42be85 100644 --- a/app/src/main/res/layout/widget_notifications.xml +++ b/app/src/main/res/layout/widget_notifications.xml @@ -1,8 +1,8 @@ <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" + android:id="@+id/widget_layout_container" android:layout_width="match_parent" android:layout_height="match_parent" - android:id="@+id/widget_layout_container" android:orientation="vertical"> <LinearLayout @@ -14,12 +14,13 @@ android:paddingEnd="@dimen/kau_padding_small"> <ImageView - android:id="@+id/img_refresh" + android:id="@+id/img_frost" android:layout_width="@dimen/toolbar_icon_size" - android:layout_margin="@dimen/kau_padding_small" android:layout_height="@dimen/toolbar_icon_size" android:layout_gravity="center_vertical" - android:src="@drawable/ic_refresh_24dp" /> + android:layout_margin="@dimen/kau_padding_small" + android:background="?android:selectableItemBackgroundBorderless" /> + </LinearLayout> <ListView diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 5fd35613..2b6a2ba6 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -62,4 +62,14 @@ <string name="no_new_notifications">No new notifications found</string> + <string name="today">Today</string> + <string name="yesterday">Today</string> + <!-- + Template used to display human readable string; + For instance: + Today at 1:23 PM + Mar 13 at 9:00 AM + --> + <string name="time_template">%s at %s</string> + </resources> |