NotificationBuilderTest.java revision 0e78de6c0fadd70277eb29f56ccce97c1dffc4f2
1/*
2 * Copyright (C) 2010 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
17package com.android.statusbartest;
18
19import java.util.GregorianCalendar;
20
21import android.app.Activity;
22import android.app.Notification;
23import android.app.NotificationManager;
24import android.app.PendingIntent;
25import android.content.Context;
26import android.content.ContentResolver;
27import android.content.Intent;
28import android.graphics.Bitmap;
29import android.graphics.drawable.BitmapDrawable;
30import android.net.Uri;
31import android.os.Bundle;
32import android.os.Environment;
33import android.os.Handler;
34import android.os.Vibrator;
35import android.os.Handler;
36import android.text.SpannableStringBuilder;
37import android.text.TextUtils;
38import android.util.Log;
39import android.net.Uri;
40import android.os.SystemClock;
41import android.view.View;
42import android.widget.CompoundButton;
43import android.widget.RadioButton;
44import android.widget.RadioGroup;
45import android.widget.RemoteViews;
46import android.os.PowerManager;
47
48public class NotificationBuilderTest extends Activity
49{
50    private final static String TAG = "NotificationTestList";
51
52    private final static String NOTIFY_TAG = "foo";
53
54    NotificationManager mNM;
55    Handler mHandler;
56    int mStartDelay;
57
58    @Override
59    public void onCreate(Bundle icicle) {
60        super.onCreate(icicle);
61        mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
62        mHandler = new Handler();
63        setContentView(R.layout.notification_builder_test);
64        if (icicle == null) {
65            setDefaults();
66        }
67        for (int id: new int[] {
68                    R.id.clear_1,
69                    R.id.clear_2,
70                    R.id.clear_3,
71                    R.id.clear_4,
72                    R.id.clear_5,
73                    R.id.clear_6,
74                    R.id.clear_7,
75                    R.id.clear_8,
76                    R.id.clear_9,
77                    R.id.clear_10,
78                    R.id.notify_1,
79                    R.id.notify_2,
80                    R.id.notify_3,
81                    R.id.notify_4,
82                    R.id.notify_5,
83                    R.id.notify_6,
84                    R.id.notify_7,
85                    R.id.notify_8,
86                    R.id.notify_9,
87                    R.id.notify_10,
88                    R.id.ten,
89                    R.id.clear_all,
90                }) {
91            findViewById(id).setOnClickListener(mClickListener);
92        }
93    }
94
95    private void setDefaults() {
96        setChecked(R.id.when_now);
97        setChecked(R.id.icon_surprise);
98        setChecked(R.id.title_medium);
99        setChecked(R.id.text_medium);
100        setChecked(R.id.info_none);
101        setChecked(R.id.number_0);
102        setChecked(R.id.intent_alert);
103        setChecked(R.id.delete_none);
104        setChecked(R.id.full_screen_none);
105        setChecked(R.id.ticker_none);
106        setChecked(R.id.large_icon_none);
107        setChecked(R.id.sound_none);
108        setChecked(R.id.vibrate_none);
109        setChecked(R.id.pri_default);
110        setChecked(R.id.lights_red);
111        setChecked(R.id.lights_off);
112        setChecked(R.id.delay_none);
113//        setChecked(R.id.default_vibrate);
114//        setChecked(R.id.default_sound);
115//        setChecked(R.id.default_lights);
116    }
117
118    private View.OnClickListener mClickListener = new View.OnClickListener() {
119        public void onClick(View v) {
120            switch (v.getId()) {
121                case R.id.clear_1:
122                    mNM.cancel(1);
123                    break;
124                case R.id.clear_2:
125                    mNM.cancel(2);
126                    break;
127                case R.id.clear_3:
128                    mNM.cancel(3);
129                    break;
130                case R.id.clear_4:
131                    mNM.cancel(4);
132                    break;
133                case R.id.clear_5:
134                    mNM.cancel(5);
135                    break;
136                case R.id.clear_6:
137                    mNM.cancel(6);
138                    break;
139                case R.id.clear_7:
140                    mNM.cancel(7);
141                    break;
142                case R.id.clear_8:
143                    mNM.cancel(8);
144                    break;
145                case R.id.clear_9:
146                    mNM.cancel(9);
147                    break;
148                case R.id.clear_10:
149                    mNM.cancel(10);
150                    break;
151                case R.id.notify_1:
152                    sendNotification(1);
153                    break;
154                case R.id.notify_2:
155                    sendNotification(2);
156                    break;
157                case R.id.notify_3:
158                    sendNotification(3);
159                    break;
160                case R.id.notify_4:
161                    sendNotification(4);
162                    break;
163                case R.id.notify_5:
164                    sendNotification(5);
165                    break;
166                case R.id.notify_6:
167                    sendNotification(6);
168                    break;
169                case R.id.notify_7:
170                    sendNotification(7);
171                    break;
172                case R.id.notify_8:
173                    sendNotification(8);
174                    break;
175                case R.id.notify_9:
176                    sendNotification(9);
177                    break;
178                case R.id.notify_10:
179                    sendNotification(10);
180                    break;
181                case R.id.ten: {
182                    for (int id=1; id<=10; id++) {
183                        sendNotification(id);
184                    }
185                    break;
186                }
187                case R.id.clear_all: {
188                    for (int id=1; id<=10; id++) {
189                        mNM.cancel(id);
190                    }
191                    break;
192                }
193            }
194        }
195    };
196
197    private void sendNotification(final int id) {
198        final Notification n = buildNotification(id);
199        mHandler.postDelayed(new Runnable() {
200            public void run() {
201                mNM.notify(NOTIFY_TAG, id, n);
202            }
203        }, mStartDelay);
204    }
205
206    private static CharSequence subst(CharSequence in, char ch, CharSequence sub) {
207        int i=0;
208        SpannableStringBuilder edit = new SpannableStringBuilder(in);
209        while (i<edit.length()) {
210            if (edit.charAt(i) == ch) {
211                edit.replace(i, i+1, sub);
212                i += sub.length();
213            } else {
214                i ++;
215            }
216        }
217        return edit;
218    }
219
220    private Notification buildNotification(int id) {
221        Notification.Builder b = new Notification.Builder(this);
222
223        // when
224        switch (getRadioChecked(R.id.group_when)) {
225            case R.id.when_midnight: {
226                GregorianCalendar c = new GregorianCalendar();
227                c.set(GregorianCalendar.HOUR_OF_DAY, 0);
228                c.set(GregorianCalendar.MINUTE, 0);
229                c.set(GregorianCalendar.SECOND, 0);
230                b.setWhen(c.getTimeInMillis());
231                break;
232            }
233            case R.id.when_now:
234                b.setWhen(System.currentTimeMillis());
235                break;
236            case R.id.when_now_plus_1h:
237                break;
238            case R.id.when_tomorrow:
239                break;
240        }
241
242        // icon
243        switch (getRadioChecked(R.id.group_icon)) {
244            case R.id.icon_im:
245                b.setSmallIcon(R.drawable.icon1);
246                break;
247            case R.id.icon_alert:
248                b.setSmallIcon(R.drawable.icon2);
249                break;
250            case R.id.icon_surprise:
251                b.setSmallIcon(R.drawable.emo_im_kissing);
252                break;
253        }
254
255        // title
256        final CharSequence title = getRadioTag(R.id.group_title);
257        if (!TextUtils.isEmpty(title)) {
258            b.setContentTitle(title);
259        }
260
261        // text
262        final CharSequence text = getRadioTag(R.id.group_text);
263        if (!TextUtils.isEmpty(text)) {
264            if (getRadioChecked(R.id.group_text) == R.id.text_emoji) {
265                // UTF-16 for +1F335
266                b.setContentText(subst(text,
267                        '_', "\ud83c\udf35"));
268            } else {
269                b.setContentText(text);
270            }
271        }
272
273        // info
274        final CharSequence info = getRadioTag(R.id.group_info);
275        if (!TextUtils.isEmpty(info)) {
276            b.setContentInfo(info);
277        }
278
279        // number
280        b.setNumber(getRadioInt(R.id.group_number, 0));
281
282        // contentIntent
283        switch (getRadioChecked(R.id.group_intent)) {
284            case R.id.intent_none:
285                break;
286            case R.id.intent_alert:
287                b.setContentIntent(makeContentIntent(id));
288                break;
289        }
290
291        // deleteIntent
292        switch (getRadioChecked(R.id.group_delete)) {
293            case R.id.delete_none:
294                break;
295            case R.id.delete_alert:
296                b.setDeleteIntent(makeDeleteIntent(id));
297                break;
298        }
299
300        // fullScreenIntent TODO
301
302        // ticker
303        switch (getRadioChecked(R.id.group_ticker)) {
304            case R.id.ticker_none:
305                break;
306            case R.id.ticker_short:
307            case R.id.ticker_wrap:
308            case R.id.ticker_haiku:
309                b.setTicker(getRadioTag(R.id.group_ticker));
310                break;
311            case R.id.ticker_emoji:
312                // UTF-16 for +1F335
313                b.setTicker(subst(getRadioTag(R.id.group_ticker),
314                        '_', "\ud83c\udf35"));
315                break;
316            case R.id.ticker_custom:
317                // TODO
318                break;
319        }
320
321        // largeIcon
322        switch (getRadioChecked(R.id.group_large_icon)) {
323            case R.id.large_icon_none:
324                break;
325            case R.id.large_icon_pineapple:
326                b.setLargeIcon(loadBitmap(R.drawable.pineapple));
327                break;
328            case R.id.large_icon_pineapple2:
329                b.setLargeIcon(loadBitmap(R.drawable.pineapple2));
330                break;
331            case R.id.large_icon_small:
332                b.setLargeIcon(loadBitmap(R.drawable.icon2));
333                break;
334        }
335
336        // sound TODO
337
338        // vibrate
339        switch (getRadioChecked(R.id.group_vibrate)) {
340            case R.id.vibrate_none:
341                b.setVibrate(null);
342                break;
343            case R.id.vibrate_zero:
344                b.setVibrate(new long[] { 0 });
345                break;
346            case R.id.vibrate_short:
347                b.setVibrate(new long[] { 0, 100 });
348                break;
349            case R.id.vibrate_long:
350                b.setVibrate(new long[] { 0, 1000 });
351                break;
352            case R.id.vibrate_pattern:
353                b.setVibrate(new long[] { 0, 50,  200, 50,  200, 50,  500,
354                                             500, 200, 500, 200, 500, 500,
355                                             50,  200, 50,  200, 50        });
356                break;
357        }
358
359        // lights
360        final int color = getRadioHex(R.id.group_lights_color, 0xff0000);
361        int onMs;
362        int offMs;
363        switch (getRadioChecked(R.id.group_lights_blink)) {
364            case R.id.lights_slow:
365                onMs = 1300;
366                offMs = 1300;
367                break;
368            case R.id.lights_fast:
369                onMs = 300;
370                offMs = 300;
371                break;
372            case R.id.lights_on:
373                onMs = 1;
374                offMs = 0;
375                break;
376            case R.id.lights_off:
377            default:
378                onMs = 0;
379                offMs = 0;
380                break;
381        }
382        if (onMs != 0 && offMs != 0) {
383            b.setLights(color, onMs, offMs);
384        }
385
386        // priority
387        switch (getRadioChecked(R.id.group_priority)) {
388            case R.id.pri_min:
389                b.setPriority(Notification.PRIORITY_MIN);
390                break;
391            case R.id.pri_low:
392                b.setPriority(Notification.PRIORITY_LOW);
393                break;
394            case R.id.pri_default:
395                b.setPriority(Notification.PRIORITY_DEFAULT);
396                break;
397            case R.id.pri_high:
398                b.setPriority(Notification.PRIORITY_HIGH);
399                break;
400            case R.id.pri_max:
401                b.setPriority(Notification.PRIORITY_MAX);
402                break;
403        }
404
405        // start delay
406        switch (getRadioChecked(R.id.group_delay)) {
407            case R.id.delay_none:
408                mStartDelay = 0;
409                break;
410            case R.id.delay_5:
411                mStartDelay = 5000;
412                break;
413        }
414
415        // flags
416        b.setOngoing(getChecked(R.id.flag_ongoing));
417        b.setOnlyAlertOnce(getChecked(R.id.flag_once));
418        b.setAutoCancel(getChecked(R.id.flag_auto_cancel));
419
420        // defaults
421        int defaults = 0;
422        if (getChecked(R.id.default_sound)) {
423            defaults |= Notification.DEFAULT_SOUND;
424        }
425        if (getChecked(R.id.default_vibrate)) {
426            defaults |= Notification.DEFAULT_VIBRATE;
427        }
428        if (getChecked(R.id.default_lights)) {
429            defaults |= Notification.DEFAULT_LIGHTS;
430        }
431        b.setDefaults(defaults);
432
433        return b.build();
434    }
435
436    private void setChecked(int id) {
437        final CompoundButton b = (CompoundButton)findViewById(id);
438        b.setChecked(true);
439    }
440
441    private int getRadioChecked(int id) {
442        final RadioGroup g = (RadioGroup)findViewById(id);
443        return g.getCheckedRadioButtonId();
444    }
445
446    private String getRadioTag(int id) {
447        final RadioGroup g = (RadioGroup)findViewById(id);
448        final View v = findViewById(g.getCheckedRadioButtonId());
449        return (String) v.getTag();
450    }
451
452    private int getRadioInt(int id, int def) {
453        String str = getRadioTag(id);
454        if (TextUtils.isEmpty(str)) {
455            return def;
456        } else {
457            try {
458                return Integer.parseInt(str.toString());
459            } catch (NumberFormatException ex) {
460                return def;
461            }
462        }
463    }
464
465    private int getRadioHex(int id, int def) {
466        String str = getRadioTag(id);
467        if (TextUtils.isEmpty(str)) {
468            return def;
469        } else {
470            if (str.startsWith("0x")) {
471                str = str.substring(2);
472            }
473            try {
474                return Integer.parseInt(str.toString(), 16);
475            } catch (NumberFormatException ex) {
476                return def;
477            }
478        }
479    }
480
481    private boolean getChecked(int id) {
482        final CompoundButton b = (CompoundButton)findViewById(id);
483        return b.isChecked();
484    }
485
486    private Bitmap loadBitmap(int id) {
487        final BitmapDrawable bd = (BitmapDrawable)getResources().getDrawable(id);
488        return Bitmap.createBitmap(bd.getBitmap());
489    }
490
491    private PendingIntent makeDeleteIntent(int id) {
492        Intent intent = new Intent(this, ConfirmationActivity.class);
493        intent.setData(Uri.fromParts("content", "//status_bar_test/delete/" + id, null));
494        intent.putExtra(ConfirmationActivity.EXTRA_TITLE, "Delete intent");
495        intent.putExtra(ConfirmationActivity.EXTRA_TEXT, "id: " + id);
496        return PendingIntent.getActivity(this, 0, intent, 0);
497    }
498
499    private PendingIntent makeContentIntent(int id) {
500        Intent intent = new Intent(this, ConfirmationActivity.class);
501        intent.setData(Uri.fromParts("content", "//status_bar_test/content/" + id, null));
502        intent.putExtra(ConfirmationActivity.EXTRA_TITLE, "Content intent");
503        intent.putExtra(ConfirmationActivity.EXTRA_TEXT, "id: " + id);
504        return PendingIntent.getActivity(this, 0, intent, 0);
505    }
506}
507
508