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