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