Skip to content

Most visited

Recently visited

navigation
Notifications / Application / src / com.example.android.support.wearable.notifications /

NotificationPresets.java

1
/*
2
 * Copyright (C) 2014 The Android Open Source Project
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 *      http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16
 
17
package com.example.android.support.wearable.notifications;
18
 
19
import android.app.Notification;
20
import android.content.Context;
21
import android.graphics.BitmapFactory;
22
import android.graphics.Color;
23
import android.graphics.Typeface;
24
import android.support.v4.app.NotificationCompat;
25
import android.text.SpannableStringBuilder;
26
import android.text.style.ForegroundColorSpan;
27
import android.text.style.RelativeSizeSpan;
28
import android.text.style.StrikethroughSpan;
29
import android.text.style.StyleSpan;
30
import android.text.style.SubscriptSpan;
31
import android.text.style.SuperscriptSpan;
32
import android.text.style.TypefaceSpan;
33
import android.text.style.UnderlineSpan;
34
import android.view.Gravity;
35
 
36
/**
37
 * Collection of notification builder presets.
38
 */
39
public class NotificationPresets {
40
    private static final String EXAMPLE_GROUP_KEY = "example";
41
 
42
    public static final NotificationPreset BASIC = new BasicNotificationPreset();
43
    public static final NotificationPreset STYLIZED_TEXT = new StylizedTextNotificationPreset();
44
    public static final NotificationPreset INBOX = new InboxNotificationPreset();
45
    public static final NotificationPreset BIG_PICTURE = new BigPictureNotificationPreset();
46
    public static final NotificationPreset BIG_TEXT = new BigTextNotificationPreset();
47
    public static final NotificationPreset BOTTOM_ALIGNED = new BottomAlignedNotificationPreset();
48
    public static final NotificationPreset GRAVITY = new GravityNotificationPreset();
49
    public static final NotificationPreset CONTENT_ACTION = new ContentActionNotificationPreset();
50
    public static final NotificationPreset CONTENT_ICON = new ContentIconNotificationPreset();
51
    public static final NotificationPreset MULTIPLE_PAGE = new MultiplePageNotificationPreset();
52
    public static final NotificationPreset BUNDLE = new NotificationBundlePreset();
53
    public static final NotificationPreset BARCODE = new NotificationBarcodePreset();
54
 
55
    public static final NotificationPreset[] PRESETS = new NotificationPreset[] {
56
            BASIC,
57
            STYLIZED_TEXT,
58
            INBOX,
59
            BIG_PICTURE,
60
            BIG_TEXT,
61
            BOTTOM_ALIGNED,
62
            GRAVITY,
63
            CONTENT_ACTION,
64
            CONTENT_ICON,
65
            MULTIPLE_PAGE,
66
            BUNDLE,
67
            BARCODE
68
    };
69
 
70
    private static NotificationCompat.Builder applyBasicOptions(Context context,
71
            NotificationCompat.Builder builder, NotificationCompat.WearableExtender wearableOptions,
72
            NotificationPreset.BuildOptions options) {
73
        builder.setContentTitle(options.titlePreset)
74
                .setContentText(options.textPreset)
75
                .setSmallIcon(R.mipmap.ic_launcher)
76
                .setDeleteIntent(NotificationUtil.getExamplePendingIntent(
77
                        context, R.string.example_notification_deleted));
78
        options.actionsPreset.apply(context, builder, wearableOptions);
79
        options.priorityPreset.apply(builder, wearableOptions);
80
        if (options.includeLargeIcon) {
81
            builder.setLargeIcon(BitmapFactory.decodeResource(
82
                    context.getResources(), R.drawable.example_large_icon));
83
        }
84
        if (options.isLocalOnly) {
85
            builder.setLocalOnly(true);
86
        }
87
        if (options.hasContentIntent) {
88
            builder.setContentIntent(NotificationUtil.getExamplePendingIntent(context,
89
                    R.string.content_intent_clicked));
90
        }
91
        if (options.vibrate) {
92
            builder.setVibrate(new long[] {0, 100, 50, 100} );
93
        }
94
        return builder;
95
    }
96
 
97
    private static class BasicNotificationPreset extends NotificationPreset {
98
        public BasicNotificationPreset() {
99
            super(R.string.basic_example, R.string.example_content_title,
100
                R.string.example_content_text);
101
        }
102
 
103
        @Override
104
        public Notification[] buildNotifications(Context context, BuildOptions options) {
105
            NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
106
            NotificationCompat.WearableExtender wearableOptions =
107
                    new NotificationCompat.WearableExtender();
108
            applyBasicOptions(context, builder, wearableOptions, options);
109
            builder.extend(wearableOptions);
110
            return new Notification[] { builder.build() };
111
        }
112
    }
113
 
114
    private static class StylizedTextNotificationPreset extends NotificationPreset {
115
        public StylizedTextNotificationPreset() {
116
            super(R.string.stylized_text_example, R.string.example_content_title,
117
                    R.string.example_content_text);
118
        }
119
 
120
        @Override
121
        public Notification[] buildNotifications(Context context, BuildOptions options) {
122
            NotificationCompat.BigTextStyle style = new NotificationCompat.BigTextStyle();
123
 
124
            SpannableStringBuilder title = new SpannableStringBuilder();
125
            appendStyled(title, "Stylized", new StyleSpan(Typeface.BOLD_ITALIC));
126
            title.append(" title");
127
            SpannableStringBuilder text = new SpannableStringBuilder("Stylized text: ");
128
            appendStyled(text, "C", new ForegroundColorSpan(Color.RED));
129
            appendStyled(text, "O", new ForegroundColorSpan(Color.GREEN));
130
            appendStyled(text, "L", new ForegroundColorSpan(Color.BLUE));
131
            appendStyled(text, "O", new ForegroundColorSpan(Color.YELLOW));
132
            appendStyled(text, "R", new ForegroundColorSpan(Color.MAGENTA));
133
            appendStyled(text, "S", new ForegroundColorSpan(Color.CYAN));
134
            text.append("; ");
135
            appendStyled(text, "1.25x size", new RelativeSizeSpan(1.25f));
136
            text.append("; ");
137
            appendStyled(text, "0.75x size", new RelativeSizeSpan(0.75f));
138
            text.append("; ");
139
            appendStyled(text, "underline", new UnderlineSpan());
140
            text.append("; ");
141
            appendStyled(text, "strikethrough", new StrikethroughSpan());
142
            text.append("; ");
143
            appendStyled(text, "bold", new StyleSpan(Typeface.BOLD));
144
            text.append("; ");
145
            appendStyled(text, "italic", new StyleSpan(Typeface.ITALIC));
146
            text.append("; ");
147
            appendStyled(text, "sans-serif-thin", new TypefaceSpan("sans-serif-thin"));
148
            text.append("; ");
149
            appendStyled(text, "monospace", new TypefaceSpan("monospace"));
150
            text.append("; ");
151
            appendStyled(text, "sub", new SubscriptSpan());
152
            text.append("script");
153
            appendStyled(text, "super", new SuperscriptSpan());
154
 
155
            style.setBigContentTitle(title);
156
            style.bigText(text);
157
 
158
            NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
159
                    .setStyle(style);
160
            NotificationCompat.WearableExtender wearableOptions =
161
                    new NotificationCompat.WearableExtender();
162
            applyBasicOptions(context, builder, wearableOptions, options);
163
            builder.extend(wearableOptions);
164
            return new Notification[] { builder.build() };
165
        }
166
 
167
        private void appendStyled(SpannableStringBuilder builder, String str, Object... spans) {
168
            builder.append(str);
169
            for (Object span : spans) {
170
                builder.setSpan(span, builder.length() - str.length(), builder.length(), 0);
171
            }
172
        }
173
    }
174
 
175
    private static class InboxNotificationPreset extends NotificationPreset {
176
        public InboxNotificationPreset() {
177
            super(R.string.inbox_example, R.string.example_content_title,
178
                R.string.example_content_text);
179
        }
180
 
181
        @Override
182
        public Notification[] buildNotifications(Context context, BuildOptions options) {
183
            NotificationCompat.InboxStyle style = new NotificationCompat.InboxStyle();
184
            style.addLine(context.getString(R.string.inbox_style_example_line1));
185
            style.addLine(context.getString(R.string.inbox_style_example_line2));
186
            style.addLine(context.getString(R.string.inbox_style_example_line3));
187
            style.setBigContentTitle(context.getString(R.string.inbox_style_example_title));
188
            style.setSummaryText(context.getString(R.string.inbox_style_example_summary_text));
189
 
190
            NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
191
                    .setStyle(style);
192
            NotificationCompat.WearableExtender wearableOptions =
193
                    new NotificationCompat.WearableExtender();
194
            applyBasicOptions(context, builder, wearableOptions, options);
195
            builder.extend(wearableOptions);
196
            return new Notification[] { builder.build() };
197
        }
198
    }
199
 
200
    private static class BigPictureNotificationPreset extends NotificationPreset {
201
        public BigPictureNotificationPreset() {
202
            super(R.string.big_picture_example, R.string.example_content_title,
203
                R.string.example_content_text);
204
        }
205
 
206
        @Override
207
        public Notification[] buildNotifications(Context context, BuildOptions options) {
208
            NotificationCompat.BigPictureStyle style = new NotificationCompat.BigPictureStyle();
209
            style.bigPicture(BitmapFactory.decodeResource(context.getResources(),
210
                    R.drawable.example_big_picture));
211
            style.setBigContentTitle(context.getString(R.string.big_picture_style_example_title));
212
            style.setSummaryText(context.getString(
213
                    R.string.big_picture_style_example_summary_text));
214
 
215
            NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
216
                    .setStyle(style);
217
            NotificationCompat.WearableExtender wearableOptions =
218
                    new NotificationCompat.WearableExtender();
219
            applyBasicOptions(context, builder, wearableOptions, options);
220
            builder.extend(wearableOptions);
221
            return new Notification[] { builder.build() };
222
        }
223
    }
224
 
225
    private static class BigTextNotificationPreset extends NotificationPreset {
226
        public BigTextNotificationPreset() {
227
            super(R.string.big_text_example, R.string.example_content_title,
228
                R.string.example_content_text);
229
        }
230
 
231
        @Override
232
        public Notification[] buildNotifications(Context context, BuildOptions options) {
233
            NotificationCompat.BigTextStyle style = new NotificationCompat.BigTextStyle();
234
            style.bigText(context.getString(R.string.big_text_example_big_text));
235
            style.setBigContentTitle(context.getString(R.string.big_text_example_title));
236
            style.setSummaryText(context.getString(R.string.big_text_example_summary_text));
237
 
238
            NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
239
                    .setStyle(style);
240
            NotificationCompat.WearableExtender wearableOptions =
241
                    new NotificationCompat.WearableExtender();
242
            applyBasicOptions(context, builder, wearableOptions, options);
243
            builder.extend(wearableOptions);
244
            return new Notification[] { builder.build() };
245
        }
246
    }
247
 
248
    private static class BottomAlignedNotificationPreset extends NotificationPreset {
249
        public BottomAlignedNotificationPreset() {
250
            super(R.string.bottom_aligned_example, R.string.example_content_title,
251
                R.string.example_content_text);
252
        }
253
 
254
        @Override
255
        public Notification[] buildNotifications(Context context, BuildOptions options) {
256
            NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
257
            NotificationCompat.WearableExtender wearableOptions =
258
                    new NotificationCompat.WearableExtender();
259
            applyBasicOptions(context, builder, wearableOptions, options);
260
 
261
            NotificationCompat.Builder secondPageBuilder = new NotificationCompat.Builder(context);
262
            secondPageBuilder.setContentTitle(
263
                    context.getString(R.string.second_page_content_title));
264
            secondPageBuilder.setContentText(context.getString(R.string.big_text_example_big_text));
265
            secondPageBuilder.extend(new NotificationCompat.WearableExtender()
266
                            .setStartScrollBottom(true));
267
 
268
            wearableOptions.addPage(secondPageBuilder.build());
269
            builder.extend(wearableOptions);
270
            return new Notification[] { builder.build() };
271
        }
272
    }
273
 
274
    private static class GravityNotificationPreset extends NotificationPreset {
275
        public GravityNotificationPreset() {
276
            super(R.string.gravity_example, R.string.example_content_title,
277
                R.string.example_content_text);
278
        }
279
 
280
        @Override
281