NotificationService.java revision 56e1b4d8207e9ae177111bc824877a780723287b
1/* 2 * Copyright (C) 2013 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package com.android.example.notificationshowcase; 18 19import android.app.IntentService; 20import android.app.Notification; 21import android.app.NotificationManager; 22import android.app.PendingIntent; 23import android.content.Context; 24import android.content.Intent; 25import android.graphics.Bitmap; 26import android.graphics.Canvas; 27import android.graphics.Typeface; 28import android.graphics.drawable.BitmapDrawable; 29import android.graphics.drawable.Drawable; 30import android.net.Uri; 31import android.support.v4.app.NotificationCompat; 32import android.text.SpannableString; 33import android.text.style.StyleSpan; 34import android.view.View; 35 36import java.util.ArrayList; 37 38public class NotificationService extends IntentService { 39 40 private static final String TAG = "NotificationService"; 41 42 public static final String ACTION_CREATE = "create"; 43 public static final int NOTIFICATION_ID = 31338; 44 45 public NotificationService() { 46 super(TAG); 47 } 48 49 public NotificationService(String name) { 50 super(name); 51 } 52 53 private static Bitmap getBitmap(Context context, int resId) { 54 int largeIconWidth = (int) context.getResources() 55 .getDimension(R.dimen.notification_large_icon_width); 56 int largeIconHeight = (int) context.getResources() 57 .getDimension(R.dimen.notification_large_icon_height); 58 Drawable d = context.getResources().getDrawable(resId); 59 Bitmap b = Bitmap.createBitmap(largeIconWidth, largeIconHeight, Bitmap.Config.ARGB_8888); 60 Canvas c = new Canvas(b); 61 d.setBounds(0, 0, largeIconWidth, largeIconHeight); 62 d.draw(c); 63 return b; 64 } 65 66 private static PendingIntent makeEmailIntent(Context context, String who) { 67 final Intent intent = new Intent(android.content.Intent.ACTION_SENDTO, 68 Uri.parse("mailto:" + who)); 69 return PendingIntent.getActivity( 70 context, 0, intent, 71 PendingIntent.FLAG_CANCEL_CURRENT); 72 } 73 74 public static Notification makeBigTextNotification(Context context, int update, int id, 75 long when) { 76 String addendum = update > 0 ? "(updated) " : ""; 77 String longSmsText = "Hey, looks like\nI'm getting kicked out of this conference" + 78 " room"; 79 if (update > 1) { 80 longSmsText += ", so stay in the hangout and I'll rejoin in about 5-10 minutes" + 81 ". If you don't see me, assume I got pulled into another meeting. And" + 82 " now \u2026 I have to find my shoes."; 83 } 84 if (update > 2) { 85 when = System.currentTimeMillis(); 86 } 87 NotificationCompat.BigTextStyle bigTextStyle = new NotificationCompat.BigTextStyle(); 88 bigTextStyle.bigText(addendum + longSmsText); 89 NotificationCompat.Builder bigTextNotification = new NotificationCompat.Builder(context) 90 .setContentTitle(addendum + "Mike Cleron") 91 .setContentIntent(ToastService.getPendingIntent(context, "Clicked on bigText")) 92 .setContentText(addendum + longSmsText) 93 .setTicker(addendum + "Mike Cleron: " + longSmsText) 94 .setWhen(when) 95 .setLargeIcon(getBitmap(context, R.drawable.bucket)) 96 .setPriority(NotificationCompat.PRIORITY_HIGH) 97 .addAction(R.drawable.ic_media_next, 98 "update: " + update, 99 UpdateService.getPendingIntent(context, update + 1, id, when)) 100 .setSmallIcon(R.drawable.stat_notify_talk_text) 101 .setStyle(bigTextStyle); 102 return bigTextNotification.build(); 103 } 104 105 public static Notification makeUploadNotification(Context context, int progress, long when) { 106 NotificationCompat.Builder uploadNotification = new NotificationCompat.Builder(context) 107 .setContentTitle("File Upload") 108 .setContentText("foo.txt") 109 .setPriority(NotificationCompat.PRIORITY_MIN) 110 .setContentIntent(ToastService.getPendingIntent(context, "Clicked on Upload")) 111 .setWhen(when) 112 .setSmallIcon(R.drawable.ic_menu_upload) 113 .setProgress(100, Math.min(progress, 100), false); 114 return uploadNotification.build(); 115 } 116 117 @Override 118 protected void onHandleIntent(Intent intent) { 119 ArrayList<Notification> mNotifications = new ArrayList<Notification>(); 120 NotificationManager noMa = 121 (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 122 123 int bigtextId = mNotifications.size(); 124 mNotifications.add(makeBigTextNotification(this, 0, bigtextId, System.currentTimeMillis())); 125 126 int uploadId = mNotifications.size(); 127 long uploadWhen = System.currentTimeMillis(); 128 mNotifications.add(makeUploadNotification(this, 10, uploadWhen)); 129 130 mNotifications.add(new NotificationCompat.Builder(this) 131 .setContentTitle("Incoming call") 132 .setContentText("Matias Duarte") 133 .setLargeIcon(getBitmap(this, R.drawable.matias_hed)) 134 .setSmallIcon(R.drawable.stat_sys_phone_call) 135 .setDefaults(Notification.DEFAULT_SOUND) 136 .setPriority(NotificationCompat.PRIORITY_MAX) 137 .setContentIntent(ToastService.getPendingIntent(this, "Clicked on Matias")) 138 .addAction(R.drawable.ic_dial_action_call, "Answer", 139 ToastService.getPendingIntent(this, "call answered")) 140 .addAction(R.drawable.ic_end_call, "Ignore", 141 ToastService.getPendingIntent(this, "call ignored")) 142 .setAutoCancel(true) 143 .build()); 144 145 mNotifications.add(new NotificationCompat.Builder(this) 146 .setContentTitle("Stopwatch PRO") 147 .setContentText("Counting up") 148 .setContentIntent(ToastService.getPendingIntent(this, "Clicked on Stopwatch")) 149 .setSmallIcon(R.drawable.stat_notify_alarm) 150 .setUsesChronometer(true) 151 .build()); 152 153 mNotifications.add(new NotificationCompat.Builder(this) 154 .setContentTitle("J Planning") 155 .setContentText("The Botcave") 156 .setWhen(System.currentTimeMillis()) 157 .setSmallIcon(R.drawable.stat_notify_calendar) 158 .setContentIntent(ToastService.getPendingIntent(this, "Clicked on calendar event")) 159 .setContentInfo("7PM") 160 .addAction(R.drawable.stat_notify_snooze, "+10 min", 161 ToastService.getPendingIntent(this, "snoozed 10 min")) 162 .addAction(R.drawable.stat_notify_snooze_longer, "+1 hour", 163 ToastService.getPendingIntent(this, "snoozed 1 hr")) 164 .addAction(R.drawable.stat_notify_email, "Email", 165 makeEmailIntent(this, 166 "gabec@example.com,mcleron@example.com,dsandler@example.com")) 167 .build()); 168 169 BitmapDrawable d = 170 (BitmapDrawable) getResources().getDrawable(R.drawable.romainguy_rockaway); 171 mNotifications.add(new NotificationCompat.BigPictureStyle( 172 new NotificationCompat.Builder(this) 173 .setContentTitle("Romain Guy") 174 .setContentText("I was lucky to find a Canon 5D Mk III at a local Bay Area " 175 + "store last week but I had not been able to try it in the field " 176 + "until tonight. After a few days of rain the sky finally cleared " 177 + "up. Rockaway Beach did not disappoint and I was finally able to " 178 + "see what my new camera feels like when shooting landscapes.") 179 .setSmallIcon(R.drawable.ic_stat_gplus) 180 .setContentIntent( 181 ToastService.getPendingIntent(this, "Clicked on bigPicture")) 182 .setLargeIcon(getBitmap(this, R.drawable.romainguy_hed)) 183 .addAction(R.drawable.add, "Add to Gallery", 184 ToastService.getPendingIntent(this, "added! (just kidding)")) 185 .setSubText("talk rocks!")) 186 .bigPicture(d.getBitmap()) 187 .build()); 188 189 // Note: this may conflict with real email notifications 190 StyleSpan bold = new StyleSpan(Typeface.BOLD); 191 SpannableString line1 = new SpannableString("Alice: hey there!"); 192 line1.setSpan(bold, 0, 5, 0); 193 SpannableString line2 = new SpannableString("Bob: hi there!"); 194 line2.setSpan(bold, 0, 3, 0); 195 SpannableString line3 = new SpannableString("Charlie: Iz IN UR EMAILZ!!"); 196 line3.setSpan(bold, 0, 7, 0); 197 mNotifications.add(new NotificationCompat.InboxStyle( 198 new NotificationCompat.Builder(this) 199 .setContentTitle("24 new messages") 200 .setContentText("You have mail!") 201 .setSubText("test.hugo2@gmail.com") 202 .setContentIntent(ToastService.getPendingIntent(this, "Clicked on Email")) 203 .setSmallIcon(R.drawable.stat_notify_email)) 204 .setSummaryText("+21 more") 205 .addLine(line1) 206 .addLine(line2) 207 .addLine(line3) 208 .build()); 209 210 mNotifications.add(new NotificationCompat.Builder(this) 211 .setContentTitle("Twitter") 212 .setContentText("New mentions") 213 .setContentIntent(ToastService.getPendingIntent(this, "Clicked on Twitter")) 214 .setSmallIcon(R.drawable.twitter_icon) 215 .setNumber(15) 216 .setPriority(NotificationCompat.PRIORITY_LOW) 217 .build()); 218 219 220 for (int i=0; i<mNotifications.size(); i++) { 221 noMa.notify(NOTIFICATION_ID + i, mNotifications.get(i)); 222 } 223 224 ProgressService.startProgressUpdater(this, uploadId, uploadWhen, 0); 225 } 226} 227