Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(440)

Side by Side Diff: chrome/android/java/src/org/chromium/chrome/browser/notifications/NotificationBuilderBase.java

Issue 1639163003: Extract NotificationBuilderBase for holding the arguments. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: similarity=20 Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 package org.chromium.chrome.browser.notifications; 5 package org.chromium.chrome.browser.notifications;
6 6
7 import android.app.Notification; 7 import android.app.Notification;
8 import android.app.PendingIntent; 8 import android.app.PendingIntent;
9 import android.content.Context;
10 import android.graphics.Bitmap; 9 import android.graphics.Bitmap;
11 import android.support.v4.app.NotificationCompat; 10 import android.support.v4.app.NotificationCompat.Action;
11
12 import org.chromium.base.VisibleForTesting;
13
14 import java.util.ArrayList;
15 import java.util.Arrays;
16 import java.util.List;
17
18 import javax.annotation.Nullable;
12 19
13 /** 20 /**
14 * Builds a notification using the given inputs. Relies on NotificationCompat an d 21 * Abstract base class for building a notification. Stores all given arguments f or later use.
15 * NotificationCompat.BigTextStyle to provide a standard layout.
16 */ 22 */
17 public class StandardNotificationBuilder implements NotificationBuilder { 23 public abstract class NotificationBuilderBase implements NotificationBuilder {
Peter Beverloo 2016/01/27 11:36:03 Do we need to keep the distinction between the int
Michael van Ouwerkerk 2016/01/27 15:04:25 From a purist perspective, I'd like to keep the in
18 private final NotificationCompat.Builder mBuilder; 24 /**
25 * Maximum length of CharSequence inputs to prevent excessive memory consump tion. At current
26 * screen sizes we display about 500 characters at most, so this is a pretty generous limit, and
27 * it matches what the Notification class does.
28 */
29 @VisibleForTesting
30 static final int MAX_CHARSEQUENCE_LENGTH = 5 * 1024;
19 31
20 public StandardNotificationBuilder(Context context) { 32 /**
21 mBuilder = new NotificationCompat.Builder(context); 33 * The maximum number of action buttons. One is for the settings button, and two more slots are
22 } 34 * for developer provided buttons.
35 */
36 private static final int MAX_ACTION_BUTTONS = 3;
37
38 protected CharSequence mTitle;
39 protected CharSequence mBody;
40 protected CharSequence mOrigin;
41 protected CharSequence mTickerText;
42 protected Bitmap mLargeIcon;
43 protected int mSmallIconId;
44 protected PendingIntent mContentIntent;
45 protected PendingIntent mDeleteIntent;
46 protected List<Action> mActions = new ArrayList<>(MAX_ACTION_BUTTONS);
47 protected Action mSettingsAction;
48 protected int mDefaults = Notification.DEFAULT_ALL;
49 protected long[] mVibratePattern;
23 50
24 @Override 51 @Override
25 public Notification build() { 52 public NotificationBuilder setTitle(@Nullable CharSequence title) {
26 return mBuilder.build(); 53 mTitle = limitLength(title);
27 }
28
29 @Override
30 public NotificationBuilder setTitle(CharSequence title) {
31 mBuilder.setContentTitle(title);
32 return this; 54 return this;
33 } 55 }
34 56
35 @Override 57 @Override
36 public NotificationBuilder setBody(CharSequence body) { 58 public NotificationBuilder setBody(@Nullable CharSequence body) {
37 mBuilder.setContentText(body).setStyle(new NotificationCompat.BigTextSty le().bigText(body)); 59 mBody = limitLength(body);
38 return this; 60 return this;
39 } 61 }
40 62
41 @Override 63 @Override
42 public NotificationBuilder setOrigin(CharSequence origin) { 64 public NotificationBuilder setOrigin(@Nullable CharSequence origin) {
43 mBuilder.setSubText(origin); 65 mOrigin = limitLength(origin);
44 return this; 66 return this;
45 } 67 }
46 68
47 @Override 69 @Override
48 public NotificationBuilder setTicker(CharSequence tickerText) { 70 public NotificationBuilder setTicker(@Nullable CharSequence tickerText) {
49 mBuilder.setTicker(tickerText); 71 mTickerText = limitLength(tickerText);
50 return this; 72 return this;
51 } 73 }
52 74
53 @Override 75 @Override
54 public NotificationBuilder setLargeIcon(Bitmap icon) { 76 public NotificationBuilder setLargeIcon(@Nullable Bitmap icon) {
55 mBuilder.setLargeIcon(icon); 77 mLargeIcon = icon;
56 return this; 78 return this;
57 } 79 }
58 80
59 @Override 81 @Override
60 public NotificationBuilder setSmallIcon(int iconId) { 82 public NotificationBuilder setSmallIcon(int iconId) {
61 mBuilder.setSmallIcon(iconId); 83 mSmallIconId = iconId;
62 return this; 84 return this;
63 } 85 }
64 86
65 @Override 87 @Override
66 public NotificationBuilder setContentIntent(PendingIntent intent) { 88 public NotificationBuilder setContentIntent(@Nullable PendingIntent intent) {
67 mBuilder.setContentIntent(intent); 89 mContentIntent = intent;
68 return this; 90 return this;
69 } 91 }
70 92
71 @Override 93 @Override
72 public NotificationBuilder setDeleteIntent(PendingIntent intent) { 94 public NotificationBuilder setDeleteIntent(@Nullable PendingIntent intent) {
73 mBuilder.setDeleteIntent(intent); 95 mDeleteIntent = intent;
74 return this; 96 return this;
75 } 97 }
76 98
77 @Override 99 @Override
78 public NotificationBuilder addAction(int iconId, CharSequence title, Pending Intent intent) { 100 public NotificationBuilder addAction(
79 mBuilder.addAction(iconId, title, intent); 101 int iconId, @Nullable CharSequence title, @Nullable PendingIntent in tent) {
102 if (mActions.size() == MAX_ACTION_BUTTONS) {
103 throw new IllegalStateException(
104 "Cannot add more than " + MAX_ACTION_BUTTONS + " actions.");
105 }
106 mActions.add(new Action(iconId, limitLength(title), intent));
80 return this; 107 return this;
81 } 108 }
82 109
83 @Override 110 @Override
84 public NotificationBuilder addSettingsAction( 111 public NotificationBuilder addSettingsAction(
85 int iconId, CharSequence title, PendingIntent intent) { 112 int iconId, @Nullable CharSequence title, @Nullable PendingIntent in tent) {
86 return addAction(iconId, title, intent); 113 mSettingsAction = new Action(iconId, limitLength(title), intent);
114 return this;
87 } 115 }
88 116
89 @Override 117 @Override
90 public NotificationBuilder setDefaults(int defaults) { 118 public NotificationBuilder setDefaults(int defaults) {
91 mBuilder.setDefaults(defaults); 119 mDefaults = defaults;
92 return this; 120 return this;
93 } 121 }
94 122
95 @Override 123 @Override
96 public NotificationBuilder setVibrate(long[] pattern) { 124 public NotificationBuilder setVibrate(@Nullable long[] pattern) {
Peter Beverloo 2016/01/27 11:36:03 This seems incorrect? You're marking it as @Nullab
Michael van Ouwerkerk 2016/01/27 15:04:25 Done.
97 mBuilder.setVibrate(pattern); 125 mVibratePattern = Arrays.copyOf(pattern, pattern.length);
98 return this; 126 return this;
99 } 127 }
128
129 @Nullable
130 private static CharSequence limitLength(@Nullable CharSequence input) {
131 if (input == null) {
132 return input;
133 }
134 if (input.length() > MAX_CHARSEQUENCE_LENGTH) {
135 return input.subSequence(0, MAX_CHARSEQUENCE_LENGTH);
136 }
137 return input;
138 }
100 } 139 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698