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