Skip to content

Commit

Permalink
MBX-3508: change image scale type
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergey Sozinov committed Jul 12, 2024
1 parent 45616df commit 8a371e5
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import android.content.Intent
import android.graphics.Bitmap
import android.graphics.Color
import android.os.Build
import android.widget.RemoteViews
import androidx.annotation.DrawableRes
import androidx.annotation.RequiresApi
import androidx.core.app.NotificationCompat
Expand All @@ -16,6 +17,7 @@ import androidx.core.content.ContextCompat
import cloud.mindbox.mobile_sdk.Mindbox
import cloud.mindbox.mobile_sdk.R
import cloud.mindbox.mobile_sdk.logger.MindboxLoggerImpl
import cloud.mindbox.mobile_sdk.logger.mindboxLogE
import cloud.mindbox.mobile_sdk.logger.mindboxLogI
import cloud.mindbox.mobile_sdk.pushes.handler.MessageHandlingState
import cloud.mindbox.mobile_sdk.pushes.handler.MindboxMessageHandler
Expand Down Expand Up @@ -238,6 +240,7 @@ internal object PushNotificationManager {
state = state,
delay = fallback.delay,
)

is ImageRetryStrategy.Cancel -> {}
is ImageRetryStrategy.ApplyDefaultAndRetry -> applyDefaultAndRetryNotifyRemoteMessage(
context = applicationContext,
Expand All @@ -254,6 +257,7 @@ internal object PushNotificationManager {
imagePlaceholder = fallback.defaultImage,
currentState = state,
)

is ImageRetryStrategy.ApplyDefault -> applyDefaultNotifyRemoteMessage(
context = applicationContext,
notificationManager = notificationManager,
Expand All @@ -267,6 +271,7 @@ internal object PushNotificationManager {
defaultActivity = defaultActivity,
imagePlaceholder = fallback.defaultImage,
)

null -> {
notifyRemoteMessage(
context = applicationContext,
Expand Down Expand Up @@ -500,6 +505,7 @@ internal object PushNotificationManager {
defaultActivity = defaultActivity,
)
.setNotificationStyle(
context =context,
image = image,
title = title,
text = text,
Expand Down Expand Up @@ -624,19 +630,35 @@ internal object PushNotificationManager {
}

private fun NotificationCompat.Builder.setNotificationStyle(
context: Context,
image: Bitmap?,
title: String,
text: String?,
) = apply {
LoggingExceptionHandler.runCatching(
block = {
setStyle(NotificationCompat.DecoratedCustomViewStyle())
setCustomContentView(
RemoteViews(context.packageName, R.layout.notification_custom_text).apply {
setTextViewText(R.id.text_view_title, title)
setTextViewText(R.id.text_view_content, text)
setImageViewBitmap(R.id.image_view_large_icon, image)
})
setCustomBigContentView(
RemoteViews(context.packageName, R.layout.notification_custom_text_with_image).apply {
setTextViewText(R.id.text_view_title, title)
setTextViewText(R.id.text_view_content, text)
setImageViewBitmap(R.id.image_view_picture, image)
})
},
defaultValue = {
mindboxLogE("Error setting notification style, trying to draw using the standard method")
if (image != null) {
setImage(image, title, text)
} else {
setText(text)
}
},
defaultValue = { setText(text) }
}
)
}

Expand Down
33 changes: 33 additions & 0 deletions sdk/src/main/res/layout/notification_custom_text.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/root_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<TextView
android:id="@+id/text_view_title"
style="@style/TextAppearance.Compat.Notification.Title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="@dimen/notification_content_margin_end"
android:layout_toStartOf="@+id/image_view_large_icon"
android:singleLine="true" />

<TextView
android:id="@+id/text_view_content"
style="@style/TextAppearance.Compat.Notification"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/text_view_title"
android:layout_marginEnd="@dimen/notification_content_margin_end"
android:layout_toStartOf="@+id/image_view_large_icon"
android:singleLine="true"/>

<ImageView
android:id="@+id/image_view_large_icon"
android:layout_width="@dimen/notification_large_icon_size"
android:layout_height="@dimen/notification_large_icon_size"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:scaleType="centerCrop" />
</RelativeLayout>
33 changes: 33 additions & 0 deletions sdk/src/main/res/layout/notification_custom_text_with_image.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/root_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<TextView
android:id="@+id/text_view_title"
style="@style/TextAppearance.Compat.Notification.Title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="@dimen/notification_content_margin_end"
android:singleLine="false" />

<TextView
android:id="@+id/text_view_content"
style="@style/TextAppearance.Compat.Notification"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/text_view_title"
android:layout_marginEnd="@dimen/notification_content_margin_end"
android:lineSpacingExtra="@dimen/notification_line_spacing_extra"
android:maxLines="@integer/collapsed_push_notification_content_max_lines" />

<ImageView
android:id="@+id/image_view_picture"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/text_view_content"
android:layout_marginTop="@dimen/notification_expanded_image_margin_top"
android:adjustViewBounds="true"
android:scaleType="fitCenter" />
</RelativeLayout>
8 changes: 8 additions & 0 deletions sdk/src/main/res/values/dimens.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="notification_content_margin_end">16dp</dimen>
<dimen name="notification_large_icon_size">36dp</dimen>
<dimen name="notification_subtext_size">13sp</dimen>
<dimen name="notification_line_spacing_extra">3sp</dimen>
<dimen name="notification_expanded_image_margin_top">13dp</dimen>
</resources>
4 changes: 4 additions & 0 deletions sdk/src/main/res/values/integers.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<integer name="collapsed_push_notification_content_max_lines">4</integer>
</resources>

0 comments on commit 8a371e5

Please sign in to comment.