Skip to content

Commit 882e070

Browse files
authored
feat(BigPictureCustomizer): Adds support for modification of extended layout… (#145)
* feat(BigPictureCustomizer): Adds support for modification of extended layout in BigPictureStyle with 2 lines of text.
1 parent 08f8f9a commit 882e070

File tree

5 files changed

+241
-217
lines changed

5 files changed

+241
-217
lines changed

AndroidSDKPush/src/main/java/com/leanplum/LeanplumNotificationBuilderCustomizer.java

Lines changed: 0 additions & 46 deletions
This file was deleted.

AndroidSDKPush/src/main/java/com/leanplum/LeanplumNotificationHelper.java

Lines changed: 118 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import android.graphics.drawable.Drawable;
3535
import android.os.Build;
3636
import android.os.Bundle;
37+
import android.support.annotation.Nullable;
3738
import android.support.v4.app.NotificationCompat;
3839
import android.text.TextUtils;
3940
import android.util.TypedValue;
@@ -42,6 +43,8 @@
4243
import com.leanplum.internal.Constants;
4344
import com.leanplum.internal.JsonConverter;
4445
import com.leanplum.internal.Log;
46+
import com.leanplum.internal.Util;
47+
import com.leanplum.utils.BitmapUtil;
4548
import com.leanplum.utils.BuildUtil;
4649

4750
import java.util.IllegalFormatCodePointException;
@@ -211,8 +214,13 @@ private static Notification.Builder getNotificationBuilder(Context context, Bund
211214
static NotificationCompat.Builder getNotificationCompatBuilder(Context context, Bundle message,
212215
PendingIntent contentIntent, String title, final String messageText, Bitmap bigPicture,
213216
int defaultNotificationIconResourceId) {
217+
if (message == null) {
218+
return null;
219+
}
220+
214221
NotificationCompat.Builder notificationCompatBuilder =
215222
getNotificationCompatBuilder(context, message);
223+
216224
if (notificationCompatBuilder == null) {
217225
return null;
218226
}
@@ -246,91 +254,125 @@ static NotificationCompat.Builder getNotificationCompatBuilder(Context context,
246254
notificationCompatBuilder.setContentIntent(contentIntent);
247255

248256
return notificationCompatBuilder;
257+
}
258+
259+
/**
260+
* Calls setStyle for notificationBuilder and it tries modify notification layout to support two
261+
* lines.
262+
*
263+
* @param notificationBuilder current Notification.Builder.
264+
* @param bigPictureStyle current Notification.BigPictureStyle.
265+
*/
266+
static void setModifiedBigPictureStyle(Notification.Builder notificationBuilder,
267+
Notification.Style bigPictureStyle) {
268+
if (Build.VERSION.SDK_INT < 16 || notificationBuilder == null || bigPictureStyle == null) {
269+
return;
270+
}
271+
try {
272+
notificationBuilder.setStyle(bigPictureStyle);
273+
274+
if (Build.VERSION.SDK_INT >= 24) {
275+
// By default we cannot reach getStandardView method on API>=24. If we call
276+
// createBigContentView, Android will call getStandardView method and we can get
277+
// modified RemoteView.
278+
try {
279+
RemoteViews remoteView = notificationBuilder.createBigContentView();
280+
if (remoteView != null) {
281+
// We need to set received RemoteView as a custom big content view.
282+
notificationBuilder.setCustomBigContentView(remoteView);
283+
}
284+
} catch (Throwable t) {
285+
Log.e("Cannot modify push notification layout.", t);
286+
}
287+
}
288+
} catch (Throwable t) {
289+
Log.e("Cannot set BigPicture style for push notification.", t);
290+
}
291+
}
292+
293+
/**
294+
* Gets Notification.BigPictureStyle with 2 lines text.
295+
*
296+
* @param message Push notification Bundle.
297+
* @param bigPicture Bitmap for BigPictureStyle notification.
298+
* @param title String with title for push notification.
299+
* @param messageText String with text for push notification.
300+
* @return Notification.BigPictureStyle or null.
301+
*/
302+
static Notification.BigPictureStyle getBigPictureStyle(Bundle message, Bitmap bigPicture,
303+
String title, final String messageText) {
304+
if (Build.VERSION.SDK_INT < 16 || message == null || bigPicture == null) {
305+
return null;
306+
}
307+
308+
Notification.BigPictureStyle bigPictureStyle = new Notification.BigPictureStyle() {
309+
@Override
310+
protected RemoteViews getStandardView(int layoutId) {
311+
RemoteViews remoteViews = super.getStandardView(layoutId);
312+
if (messageText != null && messageText.length() >= MAX_ONE_LINE_TEXT_LENGTH) {
313+
// Modifications of standard push RemoteView.
314+
try {
315+
int id = Resources.getSystem().getIdentifier("text", "id", "android");
316+
remoteViews.setBoolean(id, "setSingleLine", false);
317+
remoteViews.setInt(id, "setLines", 2);
318+
if (Build.VERSION.SDK_INT < 23) {
319+
// Make text smaller.
320+
remoteViews.setViewPadding(id, 0, BIGPICTURE_TEXT_TOP_PADDING, 0, 0);
321+
remoteViews.setTextViewTextSize(id, TypedValue.COMPLEX_UNIT_SP, BIGPICTURE_TEXT_SIZE);
322+
}
323+
} catch (Throwable throwable) {
324+
Log.e("Cannot modify push notification layout.");
325+
}
326+
}
327+
return remoteViews;
328+
}
329+
};
330+
331+
bigPictureStyle.bigPicture(bigPicture)
332+
.setBigContentTitle(title)
333+
.setSummaryText(message.getString(Constants.Keys.PUSH_MESSAGE_TEXT));
249334

335+
return bigPictureStyle;
250336
}
251337

252338
/**
253-
* Gets Notification.Builder with 2 lines at BigPictureStyle notification text.
339+
* Gets Notification.Builder for provided parameters.
254340
*
255341
* @param context The application context.
256342
* @param message Push notification Bundle.
257343
* @param contentIntent PendingIntent.
258344
* @param title String with title for push notification.
259345
* @param messageText String with text for push notification.
260-
* @param bigPicture Bitmap for BigPictureStyle notification.
261346
* @param defaultNotificationIconResourceId int Resource id for default push notification icon.
262347
* @return Notification.Builder or null.
263348
*/
264349
static Notification.Builder getNotificationBuilder(Context context, Bundle message,
265-
PendingIntent contentIntent, String title, final String messageText, Bitmap bigPicture,
350+
PendingIntent contentIntent, String title, final String messageText,
266351
int defaultNotificationIconResourceId) {
267-
if (Build.VERSION.SDK_INT < 16) {
268-
return null;
269-
}
270-
Notification.Builder notificationBuilder =
271-
getNotificationBuilder(context, message);
352+
Notification.Builder notificationBuilder = getNotificationBuilder(context, message);
272353
if (notificationBuilder == null) {
273354
return null;
274355
}
356+
275357
if (defaultNotificationIconResourceId == 0) {
276358
notificationBuilder.setSmallIcon(context.getApplicationInfo().icon);
277359
} else {
278360
notificationBuilder.setSmallIcon(defaultNotificationIconResourceId);
279361
}
362+
280363
notificationBuilder.setContentTitle(title)
281-
.setStyle(new Notification.BigTextStyle()
282-
.bigText(messageText))
283364
.setContentText(messageText);
284-
if (bigPicture != null) {
285-
Notification.BigPictureStyle bigPictureStyle = new Notification.BigPictureStyle() {
286-
@Override
287-
protected RemoteViews getStandardView(int layoutId) {
288-
RemoteViews remoteViews = super.getStandardView(layoutId);
289-
if (messageText != null && messageText.length() >= MAX_ONE_LINE_TEXT_LENGTH) {
290-
// Modifications of standard push RemoteView.
291-
try {
292-
int id = Resources.getSystem().getIdentifier("text", "id", "android");
293-
remoteViews.setBoolean(id, "setSingleLine", false);
294-
remoteViews.setInt(id, "setLines", 2);
295-
if (Build.VERSION.SDK_INT < 23) {
296-
// Make text smaller.
297-
remoteViews.setViewPadding(id, 0, BIGPICTURE_TEXT_TOP_PADDING, 0, 0);
298-
remoteViews.setTextViewTextSize(id, TypedValue.COMPLEX_UNIT_SP, BIGPICTURE_TEXT_SIZE);
299-
}
300-
} catch (Throwable throwable) {
301-
Log.e("Cannot modify push notification layout.");
302-
}
303-
}
304-
return remoteViews;
305-
}
306-
};
307365

308-
bigPictureStyle.bigPicture(bigPicture)
309-
.setBigContentTitle(title)
310-
.setSummaryText(message.getString(Constants.Keys.PUSH_MESSAGE_TEXT));
311-
notificationBuilder.setStyle(bigPictureStyle);
312-
313-
if (Build.VERSION.SDK_INT >= 24) {
314-
// By default we cannot reach getStandardView method on API>=24. I we call
315-
// createBigContentView, Android will call getStandardView method and we can get
316-
// modified RemoteView.
317-
try {
318-
RemoteViews remoteView = notificationBuilder.createBigContentView();
319-
if (remoteView != null) {
320-
// We need to set received RemoteView as a custom big content view.
321-
notificationBuilder.setCustomBigContentView(remoteView);
322-
}
323-
} catch (Throwable t) {
324-
Log.e("Cannot modify push notification layout.", t);
325-
}
366+
if (Build.VERSION.SDK_INT > 16) {
367+
notificationBuilder.setStyle(new Notification.BigTextStyle()
368+
.bigText(messageText));
369+
if (!BuildUtil.isNotificationChannelSupported(context)) {
370+
//noinspection deprecation
371+
notificationBuilder.setPriority(Notification.PRIORITY_MAX);
326372
}
327373
}
328374
notificationBuilder.setAutoCancel(true);
329375
notificationBuilder.setContentIntent(contentIntent);
330-
if (!BuildUtil.isNotificationChannelSupported(context)) {
331-
//noinspection deprecation
332-
notificationBuilder.setPriority(Notification.PRIORITY_MAX);
333-
}
334376
return notificationBuilder;
335377
}
336378

@@ -463,4 +505,25 @@ static void startPushRegistrationService(Context context, String providerName) {
463505
Log.e("Couldn't update " + providerName + " InstanceId token.", t);
464506
}
465507
}
508+
509+
/**
510+
* Gets bitmap for BigPicture style push notification.
511+
*
512+
* @param context Current application context.
513+
* @param imageUrl String with url to image.
514+
* @return Scaled bitmap for push notification with big image or null.
515+
*/
516+
@Nullable
517+
static Bitmap getBigPictureBitmap(Context context, String imageUrl) {
518+
Bitmap bigPicture = null;
519+
// BigPictureStyle support requires API 16 and higher.
520+
if (!TextUtils.isEmpty(imageUrl) && Build.VERSION.SDK_INT >= 16) {
521+
bigPicture = BitmapUtil.getScaledBitmap(context, imageUrl);
522+
if (bigPicture == null) {
523+
Log.w(String.format("Image download failed for push notification with big picture. " +
524+
"No image will be included with the push notification. Image URL: %s.", imageUrl));
525+
}
526+
}
527+
return bigPicture;
528+
}
466529
}

AndroidSDKPush/src/main/java/com/leanplum/LeanplumPushNotificationCustomizer.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,45 @@
2121

2222
package com.leanplum;
2323

24+
import android.app.Notification;
2425
import android.os.Bundle;
26+
import android.support.annotation.Nullable;
2527
import android.support.v4.app.NotificationCompat;
2628

2729
/**
2830
* Implement LeanplumPushNotificationCustomizer to customize the appearance of notifications.
2931
*/
3032
public interface LeanplumPushNotificationCustomizer {
33+
/**
34+
* Implement this method to customize push notification. Please call {@link
35+
* LeanplumPushService#setCustomizer(LeanplumPushNotificationCustomizer)} to activate this method.
36+
* Leave this method empty if you want to support 2 lines of text
37+
* in BigPicture style push notification and implement {@link
38+
* LeanplumPushNotificationCustomizer#customize(Notification.Builder, Bundle, Notification.Style)}
39+
*
40+
* @param builder NotificationCompat.Builder for push notification.
41+
* @param notificationPayload Bundle notification payload.
42+
*/
3143
void customize(NotificationCompat.Builder builder, Bundle notificationPayload);
44+
45+
/**
46+
* Implement this method to support 2 lines of text in BigPicture style push notification,
47+
* otherwise implement {@link
48+
* LeanplumPushNotificationCustomizer#customize(NotificationCompat.Builder, Bundle)} and leave
49+
* this method empty. Please call {@link
50+
* LeanplumPushService#setCustomizer(LeanplumPushNotificationCustomizer, boolean)} with true
51+
* value to activate this method.
52+
*
53+
* @param builder Notification.Builder for push notification.
54+
* @param notificationPayload Bundle notification payload.
55+
* @param notificationStyle - Notification.BigPictureStyle or null - BigPicture style for current
56+
* push notification. Call ((Notification.BigPictureStyle) notificationStyle).bigLargeIcon(largeIcon)
57+
* if you want to set large icon on expanded push notification. If notificationStyle wasn't null
58+
* it will be set to push notification. Note: If you call notificationStyle = new
59+
* Notification.BigPictureStyle() or other Notification.Style - there will be no support 2 lines
60+
* of text on BigPicture push and you need to call builder.setStyle(notificationStyle) to set
61+
* yours expanded layout for push notification.
62+
*/
63+
void customize(Notification.Builder builder, Bundle notificationPayload,
64+
@Nullable Notification.Style notificationStyle);
3265
}

0 commit comments

Comments
 (0)