Home | History | Annotate | Download | only in notificationshowcase
      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 
     17 package com.android.example.notificationshowcase;
     18 
     19 import android.app.IntentService;
     20 import android.app.Notification;
     21 import android.app.NotificationManager;
     22 import android.app.PendingIntent;
     23 import android.content.Context;
     24 import android.content.Intent;
     25 import android.graphics.Bitmap;
     26 import android.graphics.Canvas;
     27 import android.graphics.Typeface;
     28 import android.graphics.drawable.BitmapDrawable;
     29 import android.graphics.drawable.Drawable;
     30 import android.net.Uri;
     31 import android.support.v4.app.NotificationCompat;
     32 import android.text.SpannableString;
     33 import android.text.style.StyleSpan;
     34 import android.view.View;
     35 
     36 import java.util.ArrayList;
     37 
     38 public 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         Notification phoneCall = 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         phoneCall.flags |= Notification.FLAG_INSISTENT;
    145         mNotifications.add(phoneCall);
    146 
    147         mNotifications.add(new NotificationCompat.Builder(this)
    148                 .setContentTitle("Stopwatch PRO")
    149                 .setContentText("Counting up")
    150                 .setContentIntent(ToastService.getPendingIntent(this, "Clicked on Stopwatch"))
    151                 .setSmallIcon(R.drawable.stat_notify_alarm)
    152                 .setUsesChronometer(true)
    153                 .build());
    154 
    155         mNotifications.add(new NotificationCompat.Builder(this)
    156                 .setContentTitle("J Planning")
    157                 .setContentText("The Botcave")
    158                 .setWhen(System.currentTimeMillis())
    159                 .setSmallIcon(R.drawable.stat_notify_calendar)
    160                 .setContentIntent(ToastService.getPendingIntent(this, "Clicked on calendar event"))
    161                 .setContentInfo("7PM")
    162                 .addAction(R.drawable.stat_notify_snooze, "+10 min",
    163                         ToastService.getPendingIntent(this, "snoozed 10 min"))
    164                 .addAction(R.drawable.stat_notify_snooze_longer, "+1 hour",
    165                         ToastService.getPendingIntent(this, "snoozed 1 hr"))
    166                 .addAction(R.drawable.stat_notify_email, "Email",
    167                         makeEmailIntent(this,
    168                                 "gabec (at) example.com,mcleron (at) example.com,dsandler (at) example.com"))
    169                 .build());
    170 
    171         BitmapDrawable d =
    172                 (BitmapDrawable) getResources().getDrawable(R.drawable.romainguy_rockaway);
    173         mNotifications.add(new NotificationCompat.BigPictureStyle(
    174                 new NotificationCompat.Builder(this)
    175                         .setContentTitle("Romain Guy")
    176                         .setContentText("I was lucky to find a Canon 5D Mk III at a local Bay Area "
    177                                 + "store last week but I had not been able to try it in the field "
    178                                 + "until tonight. After a few days of rain the sky finally cleared "
    179                                 + "up. Rockaway Beach did not disappoint and I was finally able to "
    180                                 + "see what my new camera feels like when shooting landscapes.")
    181                         .setSmallIcon(R.drawable.ic_stat_gplus)
    182                         .setContentIntent(
    183                                 ToastService.getPendingIntent(this, "Clicked on bigPicture"))
    184                         .setLargeIcon(getBitmap(this, R.drawable.romainguy_hed))
    185                         .addAction(R.drawable.add, "Add to Gallery",
    186                                 ToastService.getPendingIntent(this, "added! (just kidding)"))
    187                         .setSubText("talk rocks!"))
    188                 .bigPicture(d.getBitmap())
    189                 .build());
    190 
    191         // Note: this may conflict with real email notifications
    192         StyleSpan bold = new StyleSpan(Typeface.BOLD);
    193         SpannableString line1 = new SpannableString("Alice: hey there!");
    194         line1.setSpan(bold, 0, 5, 0);
    195         SpannableString line2 = new SpannableString("Bob: hi there!");
    196         line2.setSpan(bold, 0, 3, 0);
    197         SpannableString line3 = new SpannableString("Charlie: Iz IN UR EMAILZ!!");
    198         line3.setSpan(bold, 0, 7, 0);
    199         mNotifications.add(new NotificationCompat.InboxStyle(
    200                 new NotificationCompat.Builder(this)
    201                         .setContentTitle("24 new messages")
    202                         .setContentText("You have mail!")
    203                         .setSubText("test.hugo2 (at) gmail.com")
    204                         .setContentIntent(ToastService.getPendingIntent(this, "Clicked on Email"))
    205                         .setSmallIcon(R.drawable.stat_notify_email))
    206                 .setSummaryText("+21 more")
    207                 .addLine(line1)
    208                 .addLine(line2)
    209                 .addLine(line3)
    210                 .build());
    211 
    212         mNotifications.add(new NotificationCompat.Builder(this)
    213                 .setContentTitle("Twitter")
    214                 .setContentText("New mentions")
    215                 .setContentIntent(ToastService.getPendingIntent(this, "Clicked on Twitter"))
    216                 .setSmallIcon(R.drawable.twitter_icon)
    217                 .setNumber(15)
    218                 .setPriority(NotificationCompat.PRIORITY_LOW)
    219                 .build());
    220 
    221 
    222         for (int i=0; i<mNotifications.size(); i++) {
    223             noMa.notify(NOTIFICATION_ID + i, mNotifications.get(i));
    224         }
    225 
    226         ProgressService.startProgressUpdater(this, uploadId, uploadWhen, 0);
    227     }
    228 }
    229