NotificationService.java revision 1985e017990466a92064b4610070ee66eefddfca
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 Notification bigText = 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 .setDefaults(Notification.DEFAULT_SOUND) 98 .addAction(R.drawable.ic_media_next, 99 "update: " + update, 100 UpdateService.getPendingIntent(context, update + 1, id, when)) 101 .setSmallIcon(R.drawable.stat_notify_talk_text) 102 .setStyle(bigTextStyle) 103 .build(); 104 return bigText; 105 } 106 107 public static Notification makeUploadNotification(Context context, int progress, long when) { 108 Notification uploadNotification = new NotificationCompat.Builder(context) 109 .setContentTitle("File Upload") 110 .setContentText("foo.txt") 111 .setPriority(NotificationCompat.PRIORITY_MIN) 112 .setContentIntent(ToastService.getPendingIntent(context, "Clicked on Upload")) 113 .setDeleteIntent(ProgressService.getSilencePendingIntent(context)) 114 .setWhen(when) 115 .setSmallIcon(R.drawable.ic_menu_upload) 116 .setProgress(100, Math.min(progress, 100), false) 117 .build(); 118 return uploadNotification; 119 } 120 121 @Override 122 protected void onHandleIntent(Intent intent) { 123 ArrayList<Notification> mNotifications = new ArrayList<Notification>(); 124 NotificationManager noMa = 125 (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 126 127 int bigtextId = mNotifications.size(); 128 mNotifications.add(makeBigTextNotification(this, 0, bigtextId, System.currentTimeMillis())); 129 130 int uploadId = mNotifications.size(); 131 long uploadWhen = System.currentTimeMillis(); 132 mNotifications.add(makeUploadNotification(this, 0, uploadWhen)); 133 134 Notification phoneCall = new NotificationCompat.Builder(this) 135 .setContentTitle("Incoming call") 136 .setContentText("Matias Duarte") 137 .setLargeIcon(getBitmap(this, R.drawable.matias_hed)) 138 .setSmallIcon(R.drawable.stat_sys_phone_call) 139 .setDefaults(Notification.DEFAULT_SOUND) 140 .setPriority(NotificationCompat.PRIORITY_MAX) 141 .setContentIntent(ToastService.getPendingIntent(this, "Clicked on Matias")) 142 .addAction(R.drawable.ic_dial_action_call, "Answer", 143 ToastService.getPendingIntent(this, "call answered")) 144 .addAction(R.drawable.ic_end_call, "Ignore", 145 ToastService.getPendingIntent(this, "call ignored")) 146 .setAutoCancel(true) 147 .build(); 148 phoneCall.flags |= Notification.FLAG_INSISTENT; 149 mNotifications.add(phoneCall); 150 151 mNotifications.add(new NotificationCompat.Builder(this) 152 .setContentTitle("Stopwatch PRO") 153 .setContentText("Counting up") 154 .setContentIntent(ToastService.getPendingIntent(this, "Clicked on Stopwatch")) 155 .setSmallIcon(R.drawable.stat_notify_alarm) 156 .setUsesChronometer(true) 157 .build()); 158 159 mNotifications.add(new NotificationCompat.Builder(this) 160 .setContentTitle("J Planning") 161 .setContentText("The Botcave") 162 .setWhen(System.currentTimeMillis()) 163 .setSmallIcon(R.drawable.stat_notify_calendar) 164 .setContentIntent(ToastService.getPendingIntent(this, "Clicked on calendar event")) 165 .setContentInfo("7PM") 166 .addAction(R.drawable.stat_notify_snooze, "+10 min", 167 ToastService.getPendingIntent(this, "snoozed 10 min")) 168 .addAction(R.drawable.stat_notify_snooze_longer, "+1 hour", 169 ToastService.getPendingIntent(this, "snoozed 1 hr")) 170 .addAction(R.drawable.stat_notify_email, "Email", 171 makeEmailIntent(this, 172 "gabec@example.com,mcleron@example.com,dsandler@example.com")) 173 .build()); 174 175 BitmapDrawable d = 176 (BitmapDrawable) getResources().getDrawable(R.drawable.romainguy_rockaway); 177 mNotifications.add(new NotificationCompat.BigPictureStyle( 178 new NotificationCompat.Builder(this) 179 .setContentTitle("Romain Guy") 180 .setContentText("I was lucky to find a Canon 5D Mk III at a local Bay Area " 181 + "store last week but I had not been able to try it in the field " 182 + "until tonight. After a few days of rain the sky finally cleared " 183 + "up. Rockaway Beach did not disappoint and I was finally able to " 184 + "see what my new camera feels like when shooting landscapes.") 185 .setSmallIcon(R.drawable.ic_stat_gplus) 186 .setContentIntent( 187 ToastService.getPendingIntent(this, "Clicked on bigPicture")) 188 .setLargeIcon(getBitmap(this, R.drawable.romainguy_hed)) 189 .addAction(R.drawable.add, "Add to Gallery", 190 ToastService.getPendingIntent(this, "added! (just kidding)")) 191 .setSubText("talk rocks!")) 192 .bigPicture(d.getBitmap()) 193 .build()); 194 195 // Note: this may conflict with real email notifications 196 StyleSpan bold = new StyleSpan(Typeface.BOLD); 197 SpannableString line1 = new SpannableString("Alice: hey there!"); 198 line1.setSpan(bold, 0, 5, 0); 199 SpannableString line2 = new SpannableString("Bob: hi there!"); 200 line2.setSpan(bold, 0, 3, 0); 201 SpannableString line3 = new SpannableString("Charlie: Iz IN UR EMAILZ!!"); 202 line3.setSpan(bold, 0, 7, 0); 203 mNotifications.add(new NotificationCompat.InboxStyle( 204 new NotificationCompat.Builder(this) 205 .setContentTitle("24 new messages") 206 .setContentText("You have mail!") 207 .setSubText("test.hugo2@gmail.com") 208 .setContentIntent(ToastService.getPendingIntent(this, "Clicked on Email")) 209 .setSmallIcon(R.drawable.stat_notify_email)) 210 .setSummaryText("+21 more") 211 .addLine(line1) 212 .addLine(line2) 213 .addLine(line3) 214 .build()); 215 216 mNotifications.add(new NotificationCompat.Builder(this) 217 .setContentTitle("Twitter") 218 .setContentText("New mentions") 219 .setContentIntent(ToastService.getPendingIntent(this, "Clicked on Twitter")) 220 .setSmallIcon(R.drawable.twitter_icon) 221 .setNumber(15) 222 .setPriority(NotificationCompat.PRIORITY_LOW) 223 .build()); 224 225 226 for (int i=0; i<mNotifications.size(); i++) { 227 noMa.notify(NOTIFICATION_ID + i, mNotifications.get(i)); 228 } 229 230 ProgressService.startProgressUpdater(this, uploadId, uploadWhen, 0); 231 } 232} 233