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