Debian Codegraph @

templates/other/Notification/root/src/app_package/NotificationHelper.java.ftl

raw
1 package ${packageName};
2
3 import android.annotation.TargetApi;
4 import android.app.Notification;
5 import android.app.NotificationManager;
6 import android.app.PendingIntent;
7 import android.content.Context;
8 import android.content.Intent;
9 import android.content.res.Resources;
10 import android.graphics.Bitmap;
11 import android.graphics.BitmapFactory;
12 import android.net.Uri;
13 import android.os.Build;
14 import android.support.v4.app.NotificationCompat;
15 <#if expandedStyle == 'list'>
16 import android.graphics.Color;
17 import android.text.SpannableStringBuilder;
18 import android.text.style.ForegroundColorSpan;
19 </#if>
20 <#if applicationPackage??>
21 import ${applicationPackage}.R;
22 </#if>
23
24 /**
25 * Helper class for showing and canceling ${display_title?lower_case}
26 * notifications.
27 * <p>
28 * This class makes heavy use of the {@link NotificationCompat.Builder} helper
29 * class to create notifications in a backward-compatible way.
30 */
31 public class ${className} {
32 /**
33 * The unique identifier for this type of notification.
34 */
35 private static final String NOTIFICATION_TAG = "${notificationName}";
36
37 /**
38 * Shows the notification, or updates a previously shown notification of
39 * this type, with the given parameters.
40 * <p>
41 * TODO: Customize this method's arguments to present relevant content in
42 * the notification.
43 * <p>
44 * TODO: Customize the contents of this method to tweak the behavior and
45 * presentation of ${display_title?lower_case} notifications. Make
46 * sure to follow the
47 * <a href="https://developer.android.com/design/patterns/notifications.html">
48 * Notification design guidelines</a> when doing so.
49 *
50 * @see #cancel(Context)
51 */
52 public static void notify(final Context context,
53 final String exampleString, final int number) {
54 final Resources res = context.getResources();
55
56 <#if expandedStyle == "picture">
57 // This image is used as the notification's large icon (thumbnail) when
58 // the notification is collapsed, and as the big picture to show when
59 // the notification is expanded.
60 <#else>
61 // This image is used as the notification's large icon (thumbnail).
62 // TODO: Remove this if your notification has no relevant thumbnail.
63 </#if>
64 final Bitmap picture = BitmapFactory.decodeResource(res, R.drawable.example_picture);
65
66 <#if expandedStyle == 'list'>
67 final SpannableStringBuilder exampleItem = new SpannableStringBuilder();
68 exampleItem.append("Dummy");
69 exampleItem.setSpan(new ForegroundColorSpan(Color.WHITE), 0, exampleItem.length(), 0);
70 exampleItem.append(" Example content");
71 </#if>
72
73 final String ticker = exampleString;
74 final String title = res.getString(
75 R.string.${notification_name}_notification_title_template, exampleString);
76 final String text = res.getString(
77 R.string.${notification_name}_notification_placeholder_text_template, exampleString);
78
79 final NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
80
81 // Set appropriate defaults for the notification light, sound,
82 // and vibration.
83 .setDefaults(Notification.DEFAULT_ALL)
84
85 // Set required fields, including the small icon, the
86 // notification title, and text.
87 .setSmallIcon(R.drawable.${icon_resource})
88 .setContentTitle(title)
89 .setContentText(text)
90
91 // All fields below this line are optional.
92
93 // Use a default priority (recognized on devices running Android
94 // 4.1 or later)
95 .setPriority(NotificationCompat.PRIORITY_DEFAULT)
96
97 // Provide a large icon, shown with the notification in the
98 // notification drawer on devices running Android 3.0 or later.
99 .setLargeIcon(picture)
100
101 // Set ticker text (preview) information for this notification.
102 .setTicker(ticker)
103
104 // Show a number. This is useful when stacking notifications of
105 // a single type.
106 .setNumber(number)
107
108 // If this notification relates to a past or upcoming event, you
109 // should set the relevant time information using the setWhen
110 // method below. If this call is omitted, the notification's
111 // timestamp will by set to the time at which it was shown.
112 // TODO: Call setWhen if this notification relates to a past or
113 // upcoming event. The sole argument to this method should be
114 // the notification timestamp in milliseconds.
115 //.setWhen(...)
116
117 // Set the pending intent to be initiated when the user touches
118 // the notification.
119 .setContentIntent(
120 PendingIntent.getActivity(
121 context,
122 0,
123 new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com")),
124 PendingIntent.FLAG_UPDATE_CURRENT))
125 <#if expandedStyle == 'picture'>
126
127 // Show an expanded photo on devices running Android 4.1 or
128 // later.
129 .setStyle(new NotificationCompat.BigPictureStyle()<#--
130 TODO: .bigLargeIcon(null) when the support library supports it -->
131 .bigPicture(picture)
132 .setBigContentTitle(title)
133 .setSummaryText("Dummy summary text"))
134 <#elseif expandedStyle == 'list'>
135
136 // Show an expanded list of items on devices running Android 4.1
137 // or later.
138 .setStyle(new NotificationCompat.InboxStyle()
139 .addLine(exampleItem)
140 .addLine(exampleItem)
141 .addLine(exampleItem)
142 .addLine(exampleItem)
143 .setBigContentTitle(title)
144 .setSummaryText("Dummy summary text"))
145 <#elseif expandedStyle == 'text'>
146
147 // Show expanded text content on devices running Android 4.1 or
148 // later.
149 .setStyle(new NotificationCompat.BigTextStyle()
150 .bigText(text)
151 .setBigContentTitle(title)
152 .setSummaryText("Dummy summary text"))
153 </#if>
154 <#if moreActions>
155
156 // Example additional actions for this notification. These will
157 // only show on devices running Android 4.1 or later, so you
158 // should ensure that the activity in this notification's
159 // content intent provides access to the same actions in
160 // another way.
161 .addAction(
162 R.drawable.ic_action_stat_share,
163 res.getString(R.string.action_share),
164 PendingIntent.getActivity(
165 context,
166 0,
167 Intent.createChooser(new Intent(Intent.ACTION_SEND)
168 .setType("text/plain")
169 .putExtra(Intent.EXTRA_TEXT, "Dummy text"), "Dummy title"),
170 PendingIntent.FLAG_UPDATE_CURRENT))
171 .addAction(
172 R.drawable.ic_action_stat_reply,
173 res.getString(R.string.action_reply),
174 null)
175 </#if>
176
177 // Automatically dismiss the notification when it is touched.
178 .setAutoCancel(true);
179
180 notify(context, builder.build());
181 }
182
183 @TargetApi(Build.VERSION_CODES.ECLAIR)
184 private static void notify(final Context context, final Notification notification) {
185 final NotificationManager nm = (NotificationManager) context
186 .getSystemService(Context.NOTIFICATION_SERVICE);
187 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ECLAIR) {
188 nm.notify(NOTIFICATION_TAG, 0, notification);
189 } else {
190 nm.notify(NOTIFICATION_TAG.hashCode(), notification);
191 }
192 }
193
194 /**
195 * Cancels any notifications of this type previously shown using
196 * {@link #notify(Context, String, int)}.
197 */
198 @TargetApi(Build.VERSION_CODES.ECLAIR)
199 public static void cancel(final Context context) {
200 final NotificationManager nm = (NotificationManager) context
201 .getSystemService(Context.NOTIFICATION_SERVICE);
202 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ECLAIR) {
203 nm.cancel(NOTIFICATION_TAG, 0);
204 } else {
205 nm.cancel(NOTIFICATION_TAG.hashCode());
206 }
207 }
208 }