NotificationShowcaseActivity.java revision 2c223ee3c280612fcf4d9536b94ad08c9f5f7431
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//            .getNotification());
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//        .getNotification());
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        mNotifications.add(new Notification.BigTextStyle(
115                new Notification.Builder(this)
116                    .setContentTitle("Mike Cleron")
117                    .setContentText(longSmsText)
118                    .setTicker("Mike Cleron: " + longSmsText)
119                    .setLargeIcon(getBitmap(R.drawable.bucket))
120                    .setPriority(Notification.PRIORITY_HIGH)
121                    .addAction(R.drawable.stat_notify_email, "Email mcleron@test.com", makeEmailIntent("mcleron@test.com"))
122                    .setSmallIcon(R.drawable.stat_notify_talk_text))
123                .bigText(longSmsText)
124                .build());
125
126        mNotifications.add(new Notification.Builder(this)
127        .setContentTitle("Incoming call")
128        .setContentText("Matias Duarte")
129        .setLargeIcon(getBitmap(R.drawable.matias_hed))
130        .setSmallIcon(R.drawable.stat_sys_phone_call)
131        .setPriority(Notification.PRIORITY_MAX)
132        .setContentIntent(makeToastIntent("Clicked on Matias"))
133        .addAction(R.drawable.ic_dial_action_call, "Answer", makeToastIntent("call answered"))
134        .addAction(R.drawable.ic_end_call, "Ignore", makeToastIntent("call ignored"))
135        //.setUsesIntruderAlert(true)
136        //.setIntruderActionsShowText(true)
137        .setAutoCancel(true)
138        .getNotification());
139
140        mNotifications.add(new Notification.Builder(this)
141        .setContentTitle("Stopwatch PRO")
142        .setContentText("Counting up")
143        .setSmallIcon(R.drawable.stat_notify_alarm)
144        .setUsesChronometer(true)
145        .getNotification());
146
147        mNotifications.add(new Notification.Builder(this)
148        .setContentTitle("J Planning")
149        .setContentText("The Botcave")
150        .setSmallIcon(R.drawable.stat_notify_calendar)
151        .setContentInfo("7PM")
152        .getNotification());
153
154        BitmapDrawable d = (BitmapDrawable) getResources().getDrawable(R.drawable.romainguy_rockaway);
155        mNotifications.add(new Notification.BigPictureStyle(
156                new Notification.Builder(this)
157                    .setContentTitle("Romain Guy")
158                    .setContentText("I was lucky to find a Canon 5D Mk III at a local Bay Area store last "
159                            + "week but I had not been able to try it in the field until tonight. After a "
160                            + "few days of rain the sky finally cleared up. Rockaway Beach did not disappoint "
161                            + "and I was finally able to see what my new camera feels like when shooting "
162                            + "landscapes.")
163                    .setSmallIcon(R.drawable.ic_stat_gplus)
164                    .setLargeIcon(getBitmap(R.drawable.romainguy_hed))
165                    .addAction(R.drawable.add, "Add to Gallery", makeToastIntent("added! (just kidding)"))
166                )
167                .bigPicture(d.getBitmap())
168                .build());
169
170        // Note: this may conflict with real email notifications
171        mNotifications.add(new Notification.Builder(this)
172        .setContentTitle("24 new messages")
173        .setContentText("test.hugo2@gmail.com")
174        .setSmallIcon(R.drawable.stat_notify_email)
175        .getNotification());
176
177        // No idea what this would really look like since the app is in flux
178        mNotifications.add(new Notification.Builder(this)
179        .setContentTitle("Google+")
180        .setContentText("Kanye West has added you to his circles")
181        .setSmallIcon(R.drawable.googleplus_icon)
182        .setPriority(Notification.PRIORITY_LOW)
183        .getNotification());
184
185        mNotifications.add(new Notification.Builder(this)
186        .setContentTitle("Twitter")
187        .setContentText("New mentions")
188        .setSmallIcon(R.drawable.twitter_icon)
189        .setNumber(15)
190        .setPriority(Notification.PRIORITY_LOW)
191        .getNotification());
192
193        if (FIRE_AND_FORGET) {
194            doPost(null);
195            finish();
196        }
197    }
198
199    public void doPost(View v) {
200        for (int i=0; i<mNotifications.size(); i++) {
201            mNoMa.notify(NOTIFICATION_ID + i, mNotifications.get(i));
202        }
203    }
204
205    public void doRemove(View v) {
206        for (int i=0; i<mNotifications.size(); i++) {
207            mNoMa.cancel(NOTIFICATION_ID + i);
208        }
209    }
210}
211