NotificationShowcaseActivity.java revision 2a44008cf0726322ccea418571b921da8ef38c8a
1// dummy notifications for demos
2// for anandx@google.com by dsandler@google.com
3
4package com.android.example.notificationshowcase;
5
6import java.util.ArrayList;
7
8import android.app.Activity;
9import android.app.Notification;
10import android.app.NotificationManager;
11import android.app.PendingIntent;
12import android.content.Context;
13import android.content.Intent;
14import android.graphics.Bitmap;
15import android.graphics.Canvas;
16import android.graphics.drawable.BitmapDrawable;
17import android.graphics.drawable.Drawable;
18import android.net.Uri;
19import android.os.Bundle;
20import android.util.Log;
21import android.view.View;
22import android.widget.Toast;
23
24public class NotificationShowcaseActivity extends Activity {
25    private static final String TAG = "NotificationShowcase";
26
27    private static final int NOTIFICATION_ID = 31338;
28
29    private static final boolean FIRE_AND_FORGET = true;
30
31    public static class ToastFeedbackActivity extends Activity {
32        @Override
33        public void onCreate(Bundle icicle) {
34            super.onCreate(icicle);
35        }
36
37        @Override
38        public void onResume() {
39            super.onResume();
40            Intent i = getIntent();
41            Log.v(TAG, "clicked a thing! intent=" + i.toString());
42            if (i.hasExtra("text")) {
43                final String text = i.getStringExtra("text");
44                Toast.makeText(this, text, Toast.LENGTH_LONG).show();
45            }
46            finish();
47        }
48    }
49
50    private ArrayList<Notification> mNotifications = new ArrayList<Notification>();
51
52    NotificationManager mNoMa;
53    int mLargeIconWidth, mLargeIconHeight;
54
55    private Bitmap getBitmap(int resId) {
56        Drawable d = getResources().getDrawable(resId);
57        Bitmap b = Bitmap.createBitmap(mLargeIconWidth, mLargeIconHeight, Bitmap.Config.ARGB_8888);
58        Canvas c = new Canvas(b);
59        d.setBounds(0, 0, mLargeIconWidth, mLargeIconHeight);
60        d.draw(c);
61        return b;
62    }
63
64    private PendingIntent makeToastIntent(String s) {
65        Intent toastIntent = new Intent(this, ToastFeedbackActivity.class);
66        toastIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
67        toastIntent.putExtra("text", s);
68        PendingIntent pi = PendingIntent.getActivity(
69                this, 58, toastIntent, PendingIntent.FLAG_CANCEL_CURRENT);
70        return pi;
71    }
72
73    private PendingIntent makeEmailIntent(String who) {
74        final Intent intent = new Intent(android.content.Intent.ACTION_SENDTO, Uri.parse("mailto:" + who));
75        return PendingIntent.getActivity(
76                this, 0, intent,
77                PendingIntent.FLAG_CANCEL_CURRENT);
78    }
79
80    @Override
81    public void onCreate(Bundle savedInstanceState) {
82        super.onCreate(savedInstanceState);
83        setContentView(R.layout.main);
84
85        mLargeIconWidth = (int) getResources().getDimension(android.R.dimen.notification_large_icon_width);
86        mLargeIconHeight = (int) getResources().getDimension(android.R.dimen.notification_large_icon_height);
87
88        mNoMa = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
89
90        // none of them does anything; if you want them to auto-destruct when tapped, add a
91        //   .setAutoCancel(true)
92        // if you want to launch an app, you need to do more work, but then again it won't launch the
93        // right thing anyway because these notifications are just dummies. :)
94
95//        mNotifications.add(new Notification.Builder(this)
96//            .setContentTitle("Larry Page")
97//            .setContentText("hey, free nachos at MoMA!")
98//            .setLargeIcon(getBitmap(R.drawable.page_hed))
99//            .setSmallIcon(android.R.drawable.stat_notify_chat)
100//            .setPriority(Notification.PRIORITY_HIGH)
101//            .setNumber(2)
102//            .build());
103
104//        mNotifications.add(new Notification.Builder(this)
105//        .setContentTitle("Andy Rubin")
106//        .setContentText("Drinks tonight?")
107//        .setTicker("Andy Rubin: Drinks tonight?")
108//        .setLargeIcon(getBitmap(R.drawable.arubin_hed))
109//        .setSmallIcon(R.drawable.stat_notify_sms)
110//        .setPriority(Notification.PRIORITY_MAX)
111//        .build());
112
113        String longSmsText = "Hey, looks like I'm getting kicked out of this conference room, so stay in the hangout and I'll rejoin in about 5-10 minutes. If you don't see me, assume I got pulled into another meeting. And now \u2026 I have to find my shoes.";
114        Notification.BigTextStyle bigTextStyle = new Notification.BigTextStyle();
115        bigTextStyle.bigText(longSmsText);
116        Notification.Builder bigTextNotification = new Notification.Builder(this)
117                .setContentTitle("Mike Cleron")
118                .setContentText(longSmsText)
119                .setTicker("Mike Cleron: " + longSmsText)
120                .setLargeIcon(getBitmap(R.drawable.bucket))
121                .setPriority(Notification.PRIORITY_HIGH)
122                .addAction(R.drawable.stat_notify_email, "Email mcleron@test.com",
123                           makeEmailIntent("mcleron@test.com"))
124                .setSmallIcon(R.drawable.stat_notify_talk_text)
125                .setStyle(bigTextStyle);
126        mNotifications.add(bigTextNotification.build());
127
128        mNotifications.add(new Notification.Builder(this)
129        .setContentTitle("Incoming call")
130        .setContentText("Matias Duarte")
131        .setLargeIcon(getBitmap(R.drawable.matias_hed))
132        .setSmallIcon(R.drawable.stat_sys_phone_call)
133        .setPriority(Notification.PRIORITY_MAX)
134        .setContentIntent(makeToastIntent("Clicked on Matias"))
135        .addAction(R.drawable.ic_dial_action_call, "Answer", makeToastIntent("call answered"))
136        .addAction(R.drawable.ic_end_call, "Ignore", makeToastIntent("call ignored"))
137        //.setUsesIntruderAlert(true)
138        //.setIntruderActionsShowText(true)
139        .setAutoCancel(true)
140        .build());
141
142        mNotifications.add(new Notification.Builder(this)
143        .setContentTitle("Stopwatch PRO")
144        .setContentText("Counting up")
145        .setSmallIcon(R.drawable.stat_notify_alarm)
146        .setUsesChronometer(true)
147        .build());
148
149        mNotifications.add(new Notification.Builder(this)
150        .setContentTitle("J Planning")
151        .setContentText("The Botcave")
152        .setSmallIcon(R.drawable.stat_notify_calendar)
153        .setContentInfo("7PM")
154        .build());
155
156        BitmapDrawable d = (BitmapDrawable) getResources().getDrawable(R.drawable.romainguy_rockaway);
157        mNotifications.add(new Notification.BigPictureStyle(
158                new Notification.Builder(this)
159                    .setContentTitle("Romain Guy")
160                    .setContentText("I was lucky to find a Canon 5D Mk III at a local Bay Area store last "
161                            + "week but I had not been able to try it in the field until tonight. After a "
162                            + "few days of rain the sky finally cleared up. Rockaway Beach did not disappoint "
163                            + "and I was finally able to see what my new camera feels like when shooting "
164                            + "landscapes.")
165                    .setSmallIcon(R.drawable.ic_stat_gplus)
166                    .setLargeIcon(getBitmap(R.drawable.romainguy_hed))
167                    .addAction(R.drawable.add, "Add to Gallery", makeToastIntent("added! (just kidding)"))
168                )
169                .bigPicture(d.getBitmap())
170                .build());
171
172        // Note: this may conflict with real email notifications
173        mNotifications.add(new Notification.Builder(this)
174        .setContentTitle("24 new messages")
175        .setContentText("test.hugo2@gmail.com")
176        .setSmallIcon(R.drawable.stat_notify_email)
177        .build());
178
179        // No idea what this would really look like since the app is in flux
180        mNotifications.add(new Notification.Builder(this)
181        .setContentTitle("Google+")
182        .setContentText("Kanye West has added you to his circles")
183        .setSmallIcon(R.drawable.googleplus_icon)
184        .setPriority(Notification.PRIORITY_LOW)
185        .build());
186
187        mNotifications.add(new Notification.Builder(this)
188        .setContentTitle("Twitter")
189        .setContentText("New mentions")
190        .setSmallIcon(R.drawable.twitter_icon)
191        .setNumber(15)
192        .setPriority(Notification.PRIORITY_LOW)
193        .build());
194
195        if (FIRE_AND_FORGET) {
196            doPost(null);
197            finish();
198        }
199    }
200
201    public void doPost(View v) {
202        for (int i=0; i<mNotifications.size(); i++) {
203            mNoMa.notify(NOTIFICATION_ID + i, mNotifications.get(i));
204        }
205    }
206
207    public void doRemove(View v) {
208        for (int i=0; i<mNotifications.size(); i++) {
209            mNoMa.cancel(NOTIFICATION_ID + i);
210        }
211    }
212}
213