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