NotificationService.java revision 9bf14882a56bfd5e0bd415993da7d37d8b5c7eb6
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 int phoneId = mNotifications.size(); 131 final PendingIntent fullscreenIntent = FullScreenActivity.getPendingIntent(this, phoneId); 132 Notification phoneCall = new NotificationCompat.Builder(this) 133 .setContentTitle("Incoming call") 134 .setContentText("Matias Duarte") 135 .setLargeIcon(getBitmap(this, R.drawable.matias_hed)) 136 .setSmallIcon(R.drawable.stat_sys_phone_call) 137 .setDefaults(Notification.DEFAULT_SOUND) 138 .setPriority(NotificationCompat.PRIORITY_MAX) 139 .setContentIntent(fullscreenIntent) 140 .setFullScreenIntent(fullscreenIntent, true) 141 .addAction(R.drawable.ic_dial_action_call, "Answer", 142 PhoneService.getPendingIntent(this, phoneId, PhoneService.ACTION_ANSWER)) 143 .addAction(R.drawable.ic_end_call, "Ignore", 144 PhoneService.getPendingIntent(this, phoneId, PhoneService.ACTION_IGNORE)) 145 .setOngoing(true) 146 .build(); 147 mNotifications.add(phoneCall); 148 149 mNotifications.add(new NotificationCompat.Builder(this) 150 .setContentTitle("Stopwatch PRO") 151 .setContentText("Counting up") 152 .setContentIntent(ToastService.getPendingIntent(this, "Clicked on Stopwatch")) 153 .setSmallIcon(R.drawable.stat_notify_alarm) 154 .setUsesChronometer(true) 155 .build()); 156 157 mNotifications.add(new NotificationCompat.Builder(this) 158 .setContentTitle("J Planning") 159 .setContentText("The Botcave") 160 .setWhen(System.currentTimeMillis()) 161 .setSmallIcon(R.drawable.stat_notify_calendar) 162 .setContentIntent(ToastService.getPendingIntent(this, "Clicked on calendar event")) 163 .setContentInfo("7PM") 164 .addAction(R.drawable.stat_notify_snooze, "+10 min", 165 ToastService.getPendingIntent(this, "snoozed 10 min")) 166 .addAction(R.drawable.stat_notify_snooze_longer, "+1 hour", 167 ToastService.getPendingIntent(this, "snoozed 1 hr")) 168 .addAction(R.drawable.stat_notify_email, "Email", 169 makeEmailIntent(this, 170 "gabec@example.com,mcleron@example.com,dsandler@example.com")) 171 .build()); 172 173 BitmapDrawable d = 174 (BitmapDrawable) getResources().getDrawable(R.drawable.romainguy_rockaway); 175 mNotifications.add(new NotificationCompat.BigPictureStyle( 176 new NotificationCompat.Builder(this) 177 .setContentTitle("Romain Guy") 178 .setContentText("I was lucky to find a Canon 5D Mk III at a local Bay Area " 179 + "store last week but I had not been able to try it in the field " 180 + "until tonight. After a few days of rain the sky finally cleared " 181 + "up. Rockaway Beach did not disappoint and I was finally able to " 182 + "see what my new camera feels like when shooting landscapes.") 183 .setSmallIcon(R.drawable.ic_stat_gplus) 184 .setContentIntent( 185 ToastService.getPendingIntent(this, "Clicked on bigPicture")) 186 .setLargeIcon(getBitmap(this, R.drawable.romainguy_hed)) 187 .addAction(R.drawable.add, "Add to Gallery", 188 ToastService.getPendingIntent(this, "added! (just kidding)")) 189 .setSubText("talk rocks!")) 190 .bigPicture(d.getBitmap()) 191 .build()); 192 193 // Note: this may conflict with real email notifications 194 StyleSpan bold = new StyleSpan(Typeface.BOLD); 195 SpannableString line1 = new SpannableString("Alice: hey there!"); 196 line1.setSpan(bold, 0, 5, 0); 197 SpannableString line2 = new SpannableString("Bob: hi there!"); 198 line2.setSpan(bold, 0, 3, 0); 199 SpannableString line3 = new SpannableString("Charlie: Iz IN UR EMAILZ!!"); 200 line3.setSpan(bold, 0, 7, 0); 201 mNotifications.add(new NotificationCompat.InboxStyle( 202 new NotificationCompat.Builder(this) 203 .setContentTitle("24 new messages") 204 .setContentText("You have mail!") 205 .setSubText("test.hugo2@gmail.com") 206 .setContentIntent(ToastService.getPendingIntent(this, "Clicked on Email")) 207 .setSmallIcon(R.drawable.stat_notify_email)) 208 .setSummaryText("+21 more") 209 .addLine(line1) 210 .addLine(line2) 211 .addLine(line3) 212 .build()); 213 214 mNotifications.add(new NotificationCompat.Builder(this) 215 .setContentTitle("Twitter") 216 .setContentText("New mentions") 217 .setContentIntent(ToastService.getPendingIntent(this, "Clicked on Twitter")) 218 .setSmallIcon(R.drawable.twitter_icon) 219 .setNumber(15) 220 .setPriority(NotificationCompat.PRIORITY_LOW) 221 .build()); 222 223 224 for (int i=0; i<mNotifications.size(); i++) { 225 noMa.notify(NOTIFICATION_ID + i, mNotifications.get(i)); 226 } 227 228 ProgressService.startProgressUpdater(this, uploadId, uploadWhen, 0); 229 } 230} 231