17edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler/*
27edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler * Copyright (C) 2015 The Android Open Source Project
37edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler *
47edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler * Licensed under the Apache License, Version 2.0 (the "License");
57edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler * you may not use this file except in compliance with the License.
67edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler * You may obtain a copy of the License at
77edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler *
87edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler *      http://www.apache.org/licenses/LICENSE-2.0
97edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler *
107edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler * Unless required by applicable law or agreed to in writing, software
117edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler * distributed under the License is distributed on an "AS IS" BASIS,
127edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
137edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler * See the License for the specific language governing permissions and
147edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler * limitations under the License.
157edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler */
167edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler
177edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandlerpackage android.app;
187edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler
197edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandlerimport android.content.ContentResolver;
207edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandlerimport android.content.Context;
217edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandlerimport android.content.Intent;
227edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandlerimport android.database.Cursor;
237edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandlerimport android.graphics.Bitmap;
247edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandlerimport android.graphics.Canvas;
257edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandlerimport android.graphics.Typeface;
267edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandlerimport android.graphics.drawable.BitmapDrawable;
277edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandlerimport android.graphics.drawable.Drawable;
287edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandlerimport android.net.Uri;
297edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandlerimport android.os.Bundle;
307edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandlerimport android.os.Handler;
317edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandlerimport android.os.Parcel;
327edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandlerimport android.os.SystemClock;
337edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandlerimport android.provider.ContactsContract;
347edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandlerimport android.test.AndroidTestCase;
357edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandlerimport android.test.suitebuilder.annotation.Suppress;
367edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandlerimport android.text.SpannableStringBuilder;
377edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandlerimport android.text.TextUtils;
387edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandlerimport android.text.style.StyleSpan;
397edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandlerimport android.util.Log;
407edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandlerimport android.view.View;
417edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandlerimport android.widget.Toast;
427edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler
437edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandlerimport java.lang.reflect.Method;
447edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandlerimport java.lang.InterruptedException;
457edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandlerimport java.lang.NoSuchMethodError;
467edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandlerimport java.lang.NoSuchMethodException;
477edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandlerimport java.util.ArrayList;
487edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler
497edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandlerimport com.android.frameworks.tests.notification.R;
507edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler
517edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandlerpublic class NotificationTests extends AndroidTestCase {
527edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler    private static final String TAG = "NOTEST";
537edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler    public static void L(String msg, Object... args) {
547edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler        Log.v(TAG, (args == null || args.length == 0) ? msg : String.format(msg, args));
557edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler    }
567edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler
577edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler    public static final String ACTION_CREATE = "create";
587edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler    public static final int NOTIFICATION_ID = 31338;
597edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler
607edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler    public static final boolean SHOW_PHONE_CALL = false;
617edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler    public static final boolean SHOW_INBOX = true;
627edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler    public static final boolean SHOW_BIG_TEXT = true;
637edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler    public static final boolean SHOW_BIG_PICTURE = true;
647edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler    public static final boolean SHOW_MEDIA = true;
657edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler    public static final boolean SHOW_STOPWATCH = false;
667edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler    public static final boolean SHOW_SOCIAL = false;
677edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler    public static final boolean SHOW_CALENDAR = false;
687edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler    public static final boolean SHOW_PROGRESS = false;
697edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler
707edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler    private static Bitmap getBitmap(Context context, int resId) {
717edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler        int largeIconWidth = (int) context.getResources()
727edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler                .getDimension(R.dimen.notification_large_icon_width);
737edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler        int largeIconHeight = (int) context.getResources()
747edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler                .getDimension(R.dimen.notification_large_icon_height);
757edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler        Drawable d = context.getResources().getDrawable(resId);
767edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler        Bitmap b = Bitmap.createBitmap(largeIconWidth, largeIconHeight, Bitmap.Config.ARGB_8888);
777edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler        Canvas c = new Canvas(b);
787edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler        d.setBounds(0, 0, largeIconWidth, largeIconHeight);
797edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler        d.draw(c);
807edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler        return b;
817edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler    }
827edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler
837edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler    private static PendingIntent makeEmailIntent(Context context, String who) {
847edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler        final Intent intent = new Intent(android.content.Intent.ACTION_SENDTO,
857edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler                Uri.parse("mailto:" + who));
867edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler        return PendingIntent.getActivity(
877edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler                context, 0, intent,
887edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler                PendingIntent.FLAG_CANCEL_CURRENT);
897edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler    }
907edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler
917edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler    static final String[] LINES = new String[] {
927edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler            "Uh oh",
937edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler            "Getting kicked out of this room",
947edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler            "I'll be back in 5-10 minutes.",
957edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler            "And now \u2026 I have to find my shoes. \uD83D\uDC63",
967edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler            "\uD83D\uDC5F \uD83D\uDC5F",
977edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler            "\uD83D\uDC60 \uD83D\uDC60",
987edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler    };
997edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler    static final int MAX_LINES = 5;
1007edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler    public static Notification makeBigTextNotification(Context context, int update, int id,
1017edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler            long when) {
1027edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler        String personUri = null;
1037edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler        /*
1047edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler        Cursor c = null;
1057edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler        try {
1067edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler            String[] projection = new String[] { ContactsContract.Contacts._ID, ContactsContract.Contacts.LOOKUP_KEY };
1077edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler            String selections = ContactsContract.Contacts.DISPLAY_NAME + " = 'Mike Cleron'";
1087edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler            final ContentResolver contentResolver = context.getContentResolver();
1097edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler            c = contentResolver.query(ContactsContract.Contacts.CONTENT_URI,
1107edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler                    projection, selections, null, null);
1117edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler            if (c != null && c.getCount() > 0) {
1127edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler                c.moveToFirst();
1137edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler                int lookupIdx = c.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY);
1147edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler                int idIdx = c.getColumnIndex(ContactsContract.Contacts._ID);
1157edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler                String lookupKey = c.getString(lookupIdx);
1167edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler                long contactId = c.getLong(idIdx);
1177edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler                Uri lookupUri = ContactsContract.Contacts.getLookupUri(contactId, lookupKey);
1187edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler                personUri = lookupUri.toString();
1197edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler            }
1207edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler        } finally {
1217edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler            if (c != null) {
1227edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler                c.close();
1237edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler            }
1247edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler        }
1257edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler        if (TextUtils.isEmpty(personUri)) {
1267edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler            Log.w(TAG, "failed to find contact for Mike Cleron");
1277edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler        } else {
1287edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler            Log.w(TAG, "Mike Cleron is " + personUri);
1297edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler        }
1307edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler        */
1317edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler
1327edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler        StringBuilder longSmsText = new StringBuilder();
1337edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler        int end = 2 + update;
1347edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler        if (end > LINES.length) {
1357edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler            end = LINES.length;
1367edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler        }
1377edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler        final int start = Math.max(0, end - MAX_LINES);
1387edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler        for (int i=start; i<end; i++) {
1397edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler            if (i >= LINES.length) break;
1407edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler            if (i > start) longSmsText.append("\n");
1417edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler            longSmsText.append(LINES[i]);
1427edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler        }
1437edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler        if (update > 2) {
1447edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler            when = System.currentTimeMillis();
1457edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler        }
1467edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler        Notification.BigTextStyle bigTextStyle = new Notification.BigTextStyle()
1477edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler                .bigText(longSmsText);
1487edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler        Notification bigText = new Notification.Builder(context)
1497edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler                .setContentTitle("Mike Cleron")
1507edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler                .setContentIntent(ToastService.getPendingIntent(context, "Clicked on bigText"))
1517edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler                .setContentText(longSmsText)
1527edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler                        //.setTicker("Mike Cleron: " + longSmsText)
1537edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler                .setWhen(when)
1547edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler                .setLargeIcon(getBitmap(context, R.drawable.bucket))
1557edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler                .setPriority(Notification.PRIORITY_HIGH)
1567edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler                .setNumber(update)
1577edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler                .setSmallIcon(R.drawable.stat_notify_talk_text)
1587edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler                .setStyle(bigTextStyle)
1597edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler                .setDefaults(Notification.DEFAULT_SOUND)
1607edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler                .addPerson(personUri)
1617edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler                .build();
1627edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler        return bigText;
1637edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler    }
1647edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler
1657edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler    public static Notification makeUploadNotification(Context context, int progress, long when) {
1667edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler        Notification.Builder uploadNotification = new Notification.Builder(context)
1677edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler                .setContentTitle("File Upload")
1687edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler                .setContentText("foo.txt")
1697edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler                .setPriority(Notification.PRIORITY_MIN)
1707edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler                .setContentIntent(ToastService.getPendingIntent(context, "Clicked on Upload"))
1717edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler                .setWhen(when)
1727edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler                .setSmallIcon(R.drawable.ic_menu_upload)
1737edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler                .setProgress(100, Math.min(progress, 100), false);
1747edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler        return uploadNotification.build();
1757edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler    }
1767edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler
1777edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler    static SpannableStringBuilder BOLD(CharSequence str) {
1787edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler        final SpannableStringBuilder ssb = new SpannableStringBuilder(str);
1797edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler        ssb.setSpan(new StyleSpan(Typeface.BOLD), 0, ssb.length(), 0);
1807edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler        return ssb;
1817edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler    }
1827edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler
1837edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler    public static class ToastService extends IntentService {
1847edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler
1857edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler        private static final String TAG = "ToastService";
1867edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler
1877edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler        private static final String ACTION_TOAST = "toast";
1887edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler
1897edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler        private Handler handler;
1907edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler
1917edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler        public ToastService() {
1927edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler            super(TAG);
1937edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler        }
1947edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler        public ToastService(String name) {
1957edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler            super(name);
1967edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler        }
1977edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler
1987edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler        @Override
1997edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler        public int onStartCommand(Intent intent, int flags, int startId) {
2007edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler            handler = new Handler();
2017edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler            return super.onStartCommand(intent, flags, startId);
2027edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler        }
2037edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler
2047edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler        @Override
2057edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler        protected void onHandleIntent(Intent intent) {
2067edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler            Log.v(TAG, "clicked a thing! intent=" + intent.toString());
2077edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler            if (intent.hasExtra("text")) {
2087edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler                final String text = intent.getStringExtra("text");
2097edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler                handler.post(new Runnable() {
2107edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler                    @Override
2117edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler                    public void run() {
2127edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler                        Toast.makeText(ToastService.this, text, Toast.LENGTH_LONG).show();
2137edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler                        Log.v(TAG, "toast " + text);
2147edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler                    }
2157edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler                });
2167edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler            }
2177edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler        }
2187edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler
2197edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler        public static PendingIntent getPendingIntent(Context context, String text) {
2207edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler            Intent toastIntent = new Intent(context, ToastService.class);
2217edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler            toastIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2227edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler            toastIntent.setAction(ACTION_TOAST + ":" + text); // one per toast message
2237edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler            toastIntent.putExtra("text", text);
2247edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler            PendingIntent pi = PendingIntent.getService(
2257edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler                    context, 58, toastIntent, PendingIntent.FLAG_UPDATE_CURRENT);
2267edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler            return pi;
2277edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler        }
2287edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler    }
2297edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler
2307edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler    public static void sleepIfYouCan(int ms) {
2317edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler        try {
2327edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler            Thread.sleep(ms);
2337edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler        } catch (InterruptedException e) {}
2347edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler    }
2357edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler
2367edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler    @Override
2377edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler    public void setUp() throws Exception {
2387edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler        super.setUp();
2397edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler    }
2407edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler
2417edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler    public static String summarize(Notification n) {
2427edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler        return String.format("<notif title=\"%s\" icon=0x%08x view=%s>",
2437edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler                n.extras.get(Notification.EXTRA_TITLE),
2447edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler                n.icon,
2457edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler                String.valueOf(n.contentView));
2467edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler    }
2477edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler
2487edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler    public void testCreate() throws Exception {
2497edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler        ArrayList<Notification> mNotifications = new ArrayList<Notification>();
2507edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler        NotificationManager noMa =
2517edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler                (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
2527edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler
2537edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler        L("Constructing notifications...");
2547edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler        if (SHOW_BIG_TEXT) {
2557edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler            int bigtextId = mNotifications.size();
2567edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler            final long time = SystemClock.currentThreadTimeMillis();
2577edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler            final Notification n = makeBigTextNotification(mContext, 0, bigtextId, System.currentTimeMillis());
2587edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler            L("  %s: create=%dms", summarize(n), SystemClock.currentThreadTimeMillis() - time);
2597edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler            mNotifications.add(n);
2607edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler        }
2617edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler
2627edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler        int uploadId = mNotifications.size();
2637edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler        long uploadWhen = System.currentTimeMillis();
2647edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler
2657edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler        if (SHOW_PROGRESS) {
2667edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler            mNotifications.add(makeUploadNotification(mContext, 0, uploadWhen));
2677edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler        }
2687edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler
2697edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler        if (SHOW_PHONE_CALL) {
2707edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler            int phoneId = mNotifications.size();
2717edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler            final PendingIntent fullscreenIntent
2727edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler                    = FullScreenActivity.getPendingIntent(mContext, phoneId);
2737edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler            final long time = SystemClock.currentThreadTimeMillis();
2747edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler            Notification phoneCall = new Notification.Builder(mContext)
2757edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler                    .setContentTitle("Incoming call")
2767edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler                    .setContentText("Matias Duarte")
2777edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler                    .setLargeIcon(getBitmap(mContext, R.drawable.matias_hed))
2787edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler                    .setSmallIcon(R.drawable.stat_sys_phone_call)
2797edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler                    .setDefaults(Notification.DEFAULT_SOUND)
2807edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler                    .setPriority(Notification.PRIORITY_MAX)
2817edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler                    .setContentIntent(fullscreenIntent)
2827edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler                    .setFullScreenIntent(fullscreenIntent, true)
2837edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler                    .addAction(R.drawable.ic_dial_action_call, "Answer",
2847edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler                            ToastService.getPendingIntent(mContext, "Clicked on Answer"))
2857edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler                    .addAction(R.drawable.ic_end_call, "Ignore",
2867edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler                            ToastService.getPendingIntent(mContext, "Clicked on Ignore"))
2877edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler                    .setOngoing(true)
2887edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler                    .addPerson(Uri.fromParts("tel", "1 (617) 555-1212", null).toString())
2897edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler                    .build();
2907edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler            L("  %s: create=%dms", phoneCall.toString(), SystemClock.currentThreadTimeMillis() - time);
2917edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler            mNotifications.add(phoneCall);
2927edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler        }
2937edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler
2947edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler        if (SHOW_STOPWATCH) {
2957edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler            final long time = SystemClock.currentThreadTimeMillis();
2967edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler            final Notification n = new Notification.Builder(mContext)
2977edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler                    .setContentTitle("Stopwatch PRO")
2987edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler                    .setContentText("Counting up")
2997edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler                    .setContentIntent(ToastService.getPendingIntent(mContext, "Clicked on Stopwatch"))
3007edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler                    .setSmallIcon(R.drawable.stat_notify_alarm)
3017edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler                    .setUsesChronometer(true)
3027edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler                    .build();
3037edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler            L("  %s: create=%dms", summarize(n), SystemClock.currentThreadTimeMillis() - time);
3047edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler            mNotifications.add(n);
3057edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler        }
3067edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler
3077edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler        if (SHOW_CALENDAR) {
3087edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler            final long time = SystemClock.currentThreadTimeMillis();
3097edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler            final Notification n = new Notification.Builder(mContext)
3107edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler                    .setContentTitle("J Planning")
3117edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler                    .setContentText("The Botcave")
3127edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler                    .setWhen(System.currentTimeMillis())
3137edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler                    .setSmallIcon(R.drawable.stat_notify_calendar)
3147edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler                    .setContentIntent(ToastService.getPendingIntent(mContext, "Clicked on calendar event"))
3157edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler                    .setContentInfo("7PM")
3167edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler                    .addAction(R.drawable.stat_notify_snooze, "+10 min",
3177edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler                            ToastService.getPendingIntent(mContext, "snoozed 10 min"))
3187edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler                    .addAction(R.drawable.stat_notify_snooze_longer, "+1 hour",
3197edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler                            ToastService.getPendingIntent(mContext, "snoozed 1 hr"))
3207edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler                    .addAction(R.drawable.stat_notify_email, "Email",
3217edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler                            ToastService.getPendingIntent(mContext,
3227edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler                                    "Congratulations, you just destroyed someone's inbox zero"))
3237edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler                    .build();
3247edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler            L("  %s: create=%dms", summarize(n), SystemClock.currentThreadTimeMillis() - time);
3257edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler            mNotifications.add(n);
3267edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler        }
3277edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler
3287edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler        if (SHOW_BIG_PICTURE) {
3297edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler            BitmapDrawable d =
3307edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler                    (BitmapDrawable) mContext.getResources().getDrawable(R.drawable.romainguy_rockaway);
3317edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler            final long time = SystemClock.currentThreadTimeMillis();
3327edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler            final Notification n = new Notification.Builder(mContext)
3337edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler                        .setContentTitle("Romain Guy")
3347edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler                        .setContentText("I was lucky to find a Canon 5D Mk III at a local Bay Area "
3357edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler                                + "store last week but I had not been able to try it in the field "
3367edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler                                + "until tonight. After a few days of rain the sky finally cleared "
3377edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler                                + "up. Rockaway Beach did not disappoint and I was finally able to "
3387edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler                                + "see what my new camera feels like when shooting landscapes.")
3397edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler                        .setSmallIcon(android.R.drawable.stat_notify_chat)
3407edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler                        .setContentIntent(
3417edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler                                ToastService.getPendingIntent(mContext, "Clicked picture"))
3427edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler                        .setLargeIcon(getBitmap(mContext, R.drawable.romainguy_hed))
3437edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler                        .addAction(R.drawable.add, "Add to Gallery",
3447edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler                                ToastService.getPendingIntent(mContext, "Added"))
3457edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler                        .setStyle(new Notification.BigPictureStyle()
3467edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler                                .bigPicture(d.getBitmap()))
3477edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler                        .build();
3487edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler            L("  %s: create=%dms", summarize(n), SystemClock.currentThreadTimeMillis() - time);
3497edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler            mNotifications.add(n);
3507edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler        }
3517edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler
3527edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler        if (SHOW_INBOX) {
3537edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler            final long time = SystemClock.currentThreadTimeMillis();
3547edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler            final Notification n = new Notification.Builder(mContext)
3557edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler                    .setContentTitle("New mail")
3567edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler                    .setContentText("3 new messages")
3577edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler                    .setSubText("example@gmail.com")
3587edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler                    .setContentIntent(ToastService.getPendingIntent(mContext, "Clicked on Mail"))
3597edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler                    .setSmallIcon(R.drawable.stat_notify_email)
3607edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler                    .setStyle(new Notification.InboxStyle()
3617edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler                                    .setSummaryText("example@gmail.com")
3627edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler                                    .addLine(BOLD("Alice:").append(" hey there!"))
3637edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler                                    .addLine(BOLD("Bob:").append(" hi there!"))
3647edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler                                    .addLine(BOLD("Charlie:").append(" Iz IN UR EMAILZ!!"))
3657edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler                    ).build();
3667edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler            L("  %s: create=%dms", summarize(n), SystemClock.currentThreadTimeMillis() - time);
3677edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler            mNotifications.add(n);
3687edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler        }
3697edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler
3707edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler        if (SHOW_SOCIAL) {
3717edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler            final long time = SystemClock.currentThreadTimeMillis();
3727edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler            final Notification n = new Notification.Builder(mContext)
3737edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler                    .setContentTitle("Social Network")
3747edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler                    .setContentText("You were mentioned in a post")
3757edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler                    .setContentInfo("example@gmail.com")
3767edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler                    .setContentIntent(ToastService.getPendingIntent(mContext, "Clicked on Social"))
3777edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler                    .setSmallIcon(android.R.drawable.stat_notify_chat)
3787edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler                    .setPriority(Notification.PRIORITY_LOW)
3797edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler                    .build();
3807edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler            L("  %s: create=%dms", summarize(n), SystemClock.currentThreadTimeMillis() - time);
3817edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler            mNotifications.add(n);
3827edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler        }
3837edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler
3847edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler        L("Posting notifications...");
3857edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler        for (int i=0; i<mNotifications.size(); i++) {
3867edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler            final int count = 4;
3877edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler            for (int j=0; j<count; j++) {
3887edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler                long time = SystemClock.currentThreadTimeMillis();
3897edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler                final Notification n = mNotifications.get(i);
3907edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler                noMa.notify(NOTIFICATION_ID + i, n);
3917edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler                time = SystemClock.currentThreadTimeMillis() - time;
3927edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler                L("  %s: notify=%dms (%d/%d)", summarize(n), time,
3937edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler                        j + 1, count);
3947edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler                sleepIfYouCan(150);
3957edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler            }
3967edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler        }
3977edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler
3987edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler        sleepIfYouCan(1000);
3997edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler
4007edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler        L("Canceling notifications...");
4017edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler        for (int i=0; i<mNotifications.size(); i++) {
4027edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler            final Notification n = mNotifications.get(i);
4037edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler            long time = SystemClock.currentThreadTimeMillis();
4047edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler            noMa.cancel(NOTIFICATION_ID + i);
4057edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler            time = SystemClock.currentThreadTimeMillis() - time;
4067edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler            L("  %s: cancel=%dms", summarize(n), time);
4077edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler        }
4087edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler
4097edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler        sleepIfYouCan(500);
4107edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler
4117edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler        L("Parceling notifications...");
4127edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler        // we want to be able to use this test on older OSes that do not have getBlobAshmemSize
4137edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler        Method getBlobAshmemSize = null;
4147edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler        try {
4157edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler            getBlobAshmemSize = Parcel.class.getMethod("getBlobAshmemSize");
4167edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler        } catch (NoSuchMethodException ex) {
4177edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler        }
4187edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler        for (int i=0; i<mNotifications.size(); i++) {
4197edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler            Parcel p = Parcel.obtain();
4207edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler            {
4217edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler                final Notification n = mNotifications.get(i);
4227edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler                long time = SystemClock.currentThreadTimeMillis();
4237edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler                n.writeToParcel(p, 0);
4247edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler                time = SystemClock.currentThreadTimeMillis() - time;
4257edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler                L("  %s: write parcel=%dms size=%d ashmem=%s",
4267edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler                        summarize(n), time, p.dataPosition(),
4277edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler                        (getBlobAshmemSize != null)
4287edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler                            ? getBlobAshmemSize.invoke(p)
4297edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler                            : "???");
4307edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler                p.setDataPosition(0);
4317edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler            }
4327edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler
4337edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler            long time = SystemClock.currentThreadTimeMillis();
4347edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler            final Notification n2 = Notification.CREATOR.createFromParcel(p);
4357edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler            time = SystemClock.currentThreadTimeMillis() - time;
4367edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler            L("  %s: parcel read=%dms", summarize(n2), time);
4377edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler
4387edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler            time = SystemClock.currentThreadTimeMillis();
4397edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler            noMa.notify(NOTIFICATION_ID + i, n2);
4407edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler            time = SystemClock.currentThreadTimeMillis() - time;
4417edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler            L("  %s: notify=%dms", summarize(n2), time);
4427edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler        }
4437edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler
4447edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler        sleepIfYouCan(500);
4457edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler
4467edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler        L("Canceling notifications...");
4477edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler        for (int i=0; i<mNotifications.size(); i++) {
4487edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler            long time = SystemClock.currentThreadTimeMillis();
4497edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler            final Notification n = mNotifications.get(i);
4507edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler            noMa.cancel(NOTIFICATION_ID + i);
4517edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler            time = SystemClock.currentThreadTimeMillis() - time;
4527edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler            L("  %s: cancel=%dms", summarize(n), time);
4537edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler        }
4547edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler
4557edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler
4567edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler//            if (SHOW_PROGRESS) {
4577edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler//                ProgressService.startProgressUpdater(this, uploadId, uploadWhen, 0);
4587edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler//            }
4597edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler    }
4607edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler
4617edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler    public static class FullScreenActivity extends Activity {
4627edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler        public static final String EXTRA_ID = "id";
4637edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler
4647edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler        @Override
4657edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler        public void onCreate(Bundle savedInstanceState) {
4667edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler            super.onCreate(savedInstanceState);
4677edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler            setContentView(R.layout.full_screen);
4687edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler            final Intent intent = getIntent();
4697edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler            if (intent != null && intent.hasExtra(EXTRA_ID)) {
4707edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler                final int id = intent.getIntExtra(EXTRA_ID, -1);
4717edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler                if (id >= 0) {
4727edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler                    NotificationManager noMa =
4737edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler                            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
4747edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler                    noMa.cancel(NOTIFICATION_ID + id);
4757edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler                }
4767edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler            }
4777edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler        }
4787edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler
4797edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler        public void dismiss(View v) {
4807edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler            finish();
4817edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler        }
4827edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler
4837edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler        public static PendingIntent getPendingIntent(Context context, int id) {
4847edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler            Intent fullScreenIntent = new Intent(context, FullScreenActivity.class);
4857edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler            fullScreenIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
4867edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler
4877edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler            fullScreenIntent.putExtra(EXTRA_ID, id);
4887edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler            PendingIntent pi = PendingIntent.getActivity(
4897edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler                    context, 22, fullScreenIntent, PendingIntent.FLAG_UPDATE_CURRENT);
4907edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler            return pi;
4917edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler        }
4927edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler    }
4937edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler}
4947edddb4961c7c64f8d2a06495f742e04b5cc7696Dan Sandler
495