NotificationShowcaseActivity.java revision bcdc72c9b04ece5bbc8fd146e35349f984abb8a3
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.Drawable;
17import android.os.Bundle;
18import android.view.View;
19import android.widget.Toast;
20
21public class NotificationShowcaseActivity extends Activity {
22    public static class ToastFeedbackActivity extends Activity {
23       @Override
24       public void onStart() {
25           Intent i = getIntent();
26           if (i.hasExtra("text")) {
27               final String text = i.getStringExtra("text");
28               Toast.makeText(this, text, Toast.LENGTH_LONG).show();
29           }
30           finish();
31       }
32    }
33
34    private static final int NOTIFICATION_ID = 31338;
35
36    private static final boolean FIRE_AND_FORGET = true;
37
38    private ArrayList<Notification> mNotifications = new ArrayList<Notification>();
39
40    NotificationManager mNoMa;
41    int mLargeIconWidth, mLargeIconHeight;
42
43    private Bitmap getBitmap(int resId) {
44        Drawable d = getResources().getDrawable(resId);
45        Bitmap b = Bitmap.createBitmap(mLargeIconWidth, mLargeIconHeight, Bitmap.Config.ARGB_8888);
46        Canvas c = new Canvas(b);
47        d.setBounds(0, 0, mLargeIconWidth, mLargeIconHeight);
48        d.draw(c);
49        return b;
50    }
51
52    @Override
53    public void onCreate(Bundle savedInstanceState) {
54        super.onCreate(savedInstanceState);
55        setContentView(R.layout.main);
56
57        mLargeIconWidth = (int) getResources().getDimension(android.R.dimen.notification_large_icon_width);
58        mLargeIconHeight = (int) getResources().getDimension(android.R.dimen.notification_large_icon_height);
59
60        mNoMa = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
61
62
63        // these will appear in the shade in reverse post order, so adjust the order here to taste
64
65        // none of them does anything; if you want them to auto-destruct when tapped, add a
66        //   .setAutoCancel(true)
67        // if you want to launch an app, you need to do more work, but then again it won't launch the
68        // right thing anyway because these notifications are just dummies. :)
69
70        mNotifications.add(new Notification.Builder(this)
71            .setContentTitle("Larry Page")
72            .setContentText("hey, free nachos at MoMA!")
73            .setLargeIcon(getBitmap(R.drawable.page_hed))
74            .setSmallIcon(android.R.drawable.stat_notify_chat)
75            .setPriority(Notification.PRIORITY_HIGH)
76            .setNumber(2)
77            .getNotification());
78
79        mNotifications.add(new Notification.Builder(this)
80        .setContentTitle("Andy Rubin")
81        .setContentText("Drinks tonight?")
82        .setTicker("Andy Rubin: Drinks tonight?")
83        .setLargeIcon(getBitmap(R.drawable.arubin_hed))
84        .setSmallIcon(R.drawable.stat_notify_sms)
85        .setPriority(Notification.PRIORITY_MAX)
86        .getNotification());
87
88        Intent toastIntent = new Intent(this, ToastFeedbackActivity.class);
89        toastIntent.putExtra("text", "Clicked on Matias");
90        PendingIntent contentIntent = PendingIntent.getActivity(
91                this, 0, toastIntent, PendingIntent.FLAG_CANCEL_CURRENT);
92
93        mNotifications.add(new Notification.Builder(this)
94        .setContentTitle("Incoming call")
95        .setContentText("Matias Duarte")
96        .setLargeIcon(getBitmap(R.drawable.matias_hed))
97        .setSmallIcon(R.drawable.stat_sys_phone_call)
98        .setPriority(Notification.PRIORITY_MAX)
99        .setContentIntent(contentIntent)
100        .addAction(R.drawable.ic_dial_action_call, "Answer", null)
101        .addAction(R.drawable.ic_end_call, "Ignore", null)
102        .setUsesIntruderAlert(true)
103        .setIntruderActionsShowText(true)
104        .setAutoCancel(true)
105        .getNotification());
106
107
108        mNotifications.add(new Notification.Builder(this)
109        .setContentTitle("J Planning")
110        .setContentText("The Botcave")
111        .setSmallIcon(R.drawable.stat_notify_calendar)
112        .setContentInfo("7PM")
113        .getNotification());
114
115        // Note: this may conflict with real email notifications
116        mNotifications.add(new Notification.Builder(this)
117        .setContentTitle("24 new messages")
118        .setContentText("test.hugo2@gmail.com")
119        .setSmallIcon(R.drawable.stat_notify_email)
120        .getNotification());
121
122        // No idea what this would really look like since the app is in flux
123        mNotifications.add(new Notification.Builder(this)
124        .setContentTitle("Google+")
125        .setContentText("Kanye West has added you to his circles")
126        .setSmallIcon(R.drawable.googleplus_icon)
127        .setPriority(Notification.PRIORITY_LOW)
128        .getNotification());
129
130        mNotifications.add(new Notification.Builder(this)
131        .setContentTitle("Twitter")
132        .setContentText("New mentions")
133        .setSmallIcon(R.drawable.twitter_icon)
134        .setNumber(15)
135        .setPriority(Notification.PRIORITY_LOW)
136        .getNotification());
137
138        if (FIRE_AND_FORGET) {
139            doPost(null);
140            finish();
141        }
142    }
143
144    public void doPost(View v) {
145        for (int i=0; i<mNotifications.size(); i++) {
146            mNoMa.notify(NOTIFICATION_ID + i, mNotifications.get(i));
147        }
148    }
149
150    public void doRemove(View v) {
151        for (int i=0; i<mNotifications.size(); i++) {
152            mNoMa.cancel(NOTIFICATION_ID + i);
153        }
154    }
155}
156