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