Home | History | Annotate | Download | only in notificationshowcase
      1 // dummy notifications for demos
      2 // for anandx (at) google.com by dsandler (at) google.com
      3 
      4 package com.android.example.notificationshowcase;
      5 
      6 import java.util.ArrayList;
      7 
      8 import android.app.Activity;
      9 import android.app.Notification;
     10 import android.app.NotificationManager;
     11 import android.app.PendingIntent;
     12 import android.content.Context;
     13 import android.content.Intent;
     14 import android.graphics.Bitmap;
     15 import android.graphics.Canvas;
     16 import android.graphics.Typeface;
     17 import android.graphics.drawable.BitmapDrawable;
     18 import android.graphics.drawable.Drawable;
     19 import android.net.Uri;
     20 import android.os.Bundle;
     21 import android.text.SpannableString;
     22 import android.text.style.StyleSpan;
     23 import android.util.Log;
     24 import android.view.View;
     25 import android.widget.Toast;
     26 
     27 public class NotificationShowcaseActivity extends Activity {
     28     private static final String TAG = "NotificationShowcase";
     29 
     30     private static final int NOTIFICATION_ID = 31338;
     31 
     32     private static final boolean FIRE_AND_FORGET = true;
     33 
     34     public static class ToastFeedbackActivity extends Activity {
     35         @Override
     36         public void onCreate(Bundle icicle) {
     37             super.onCreate(icicle);
     38         }
     39 
     40         @Override
     41         public void onResume() {
     42             super.onResume();
     43             Intent i = getIntent();
     44             Log.v(TAG, "clicked a thing! intent=" + i.toString());
     45             if (i.hasExtra("text")) {
     46                 final String text = i.getStringExtra("text");
     47                 Toast.makeText(this, text, Toast.LENGTH_LONG).show();
     48             }
     49             finish();
     50         }
     51     }
     52 
     53     private ArrayList<Notification> mNotifications = new ArrayList<Notification>();
     54 
     55     NotificationManager mNoMa;
     56     int mLargeIconWidth, mLargeIconHeight;
     57 
     58     private Bitmap getBitmap(int resId) {
     59         Drawable d = getResources().getDrawable(resId);
     60         Bitmap b = Bitmap.createBitmap(mLargeIconWidth, mLargeIconHeight, Bitmap.Config.ARGB_8888);
     61         Canvas c = new Canvas(b);
     62         d.setBounds(0, 0, mLargeIconWidth, mLargeIconHeight);
     63         d.draw(c);
     64         return b;
     65     }
     66 
     67     private PendingIntent makeToastIntent(String s) {
     68         Intent toastIntent = new Intent(this, ToastFeedbackActivity.class);
     69         toastIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
     70         toastIntent.putExtra("text", s);
     71         PendingIntent pi = PendingIntent.getActivity(
     72                 this, 58, toastIntent, PendingIntent.FLAG_CANCEL_CURRENT);
     73         return pi;
     74     }
     75 
     76     private PendingIntent makeEmailIntent(String who) {
     77         final Intent intent = new Intent(android.content.Intent.ACTION_SENDTO, Uri.parse("mailto:" + who));
     78         return PendingIntent.getActivity(
     79                 this, 0, intent,
     80                 PendingIntent.FLAG_CANCEL_CURRENT);
     81     }
     82 
     83     @Override
     84     public void onCreate(Bundle savedInstanceState) {
     85         super.onCreate(savedInstanceState);
     86         setContentView(R.layout.main);
     87 
     88         mLargeIconWidth = (int) getResources().getDimension(android.R.dimen.notification_large_icon_width);
     89         mLargeIconHeight = (int) getResources().getDimension(android.R.dimen.notification_large_icon_height);
     90 
     91         mNoMa = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
     92 
     93         // none of them does anything; if you want them to auto-destruct when tapped, add a
     94         //   .setAutoCancel(true)
     95         // if you want to launch an app, you need to do more work, but then again it won't launch the
     96         // right thing anyway because these notifications are just dummies. :)
     97 
     98 //        mNotifications.add(new Notification.Builder(this)
     99 //            .setContentTitle("Larry Page")
    100 //            .setContentText("hey, free nachos at MoMA!")
    101 //            .setLargeIcon(getBitmap(R.drawable.page_hed))
    102 //            .setSmallIcon(android.R.drawable.stat_notify_chat)
    103 //            .setPriority(Notification.PRIORITY_HIGH)
    104 //            .setNumber(2)
    105 //            .build());
    106 
    107 //        mNotifications.add(new Notification.Builder(this)
    108 //        .setContentTitle("Andy Rubin")
    109 //        .setContentText("Drinks tonight?")
    110 //        .setTicker("Andy Rubin: Drinks tonight?")
    111 //        .setLargeIcon(getBitmap(R.drawable.arubin_hed))
    112 //        .setSmallIcon(R.drawable.stat_notify_sms)
    113 //        .setPriority(Notification.PRIORITY_MAX)
    114 //        .build());
    115 
    116         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.";
    117         Notification.BigTextStyle bigTextStyle = new Notification.BigTextStyle();
    118         bigTextStyle.bigText(longSmsText);
    119         Notification.Builder bigTextNotification = new Notification.Builder(this)
    120                 .setContentTitle("Mike Cleron")
    121                 .setContentText(longSmsText)
    122                 .setTicker("Mike Cleron: " + longSmsText)
    123                 .setLargeIcon(getBitmap(R.drawable.bucket))
    124                 .setPriority(Notification.PRIORITY_HIGH)
    125                 .addAction(R.drawable.stat_notify_email, "Email mcleron (at) test.com",
    126                            makeEmailIntent("mcleron (at) test.com"))
    127                 .setSmallIcon(R.drawable.stat_notify_talk_text)
    128                 .setStyle(bigTextStyle);
    129         mNotifications.add(bigTextNotification.build());
    130 
    131         mNotifications.add(new Notification.Builder(this)
    132         .setContentTitle("Incoming call")
    133         .setContentText("Matias Duarte")
    134         .setLargeIcon(getBitmap(R.drawable.matias_hed))
    135         .setSmallIcon(R.drawable.stat_sys_phone_call)
    136         .setPriority(Notification.PRIORITY_MAX)
    137         .setContentIntent(makeToastIntent("Clicked on Matias"))
    138         .addAction(R.drawable.ic_dial_action_call, "Answer", makeToastIntent("call answered"))
    139         .addAction(R.drawable.ic_end_call, "Ignore", makeToastIntent("call ignored"))
    140         //.setUsesIntruderAlert(true)
    141         //.setIntruderActionsShowText(true)
    142         .setAutoCancel(true)
    143         .build());
    144 
    145         mNotifications.add(new Notification.Builder(this)
    146         .setContentTitle("Stopwatch PRO")
    147         .setContentText("Counting up")
    148         .setSmallIcon(R.drawable.stat_notify_alarm)
    149         .setUsesChronometer(true)
    150         .build());
    151 
    152         mNotifications.add(new Notification.Builder(this)
    153         .setContentTitle("J Planning")
    154         .setContentText("The Botcave")
    155         .setSmallIcon(R.drawable.stat_notify_calendar)
    156         .setContentInfo("7PM")
    157         .build());
    158 
    159         BitmapDrawable d = (BitmapDrawable) getResources().getDrawable(R.drawable.romainguy_rockaway);
    160         mNotifications.add(new Notification.BigPictureStyle(
    161                 new Notification.Builder(this)
    162                     .setContentTitle("Romain Guy")
    163                     .setContentText("I was lucky to find a Canon 5D Mk III at a local Bay Area store last "
    164                             + "week but I had not been able to try it in the field until tonight. After a "
    165                             + "few days of rain the sky finally cleared up. Rockaway Beach did not disappoint "
    166                             + "and I was finally able to see what my new camera feels like when shooting "
    167                             + "landscapes.")
    168                     .setSmallIcon(R.drawable.ic_stat_gplus)
    169                     .setLargeIcon(getBitmap(R.drawable.romainguy_hed))
    170                     .addAction(R.drawable.add, "Add to Gallery", makeToastIntent("added! (just kidding)"))
    171                     .setSubText("talk rocks!")
    172                 )
    173                 .bigPicture(d.getBitmap())
    174                 .build());
    175 
    176         // Note: this may conflict with real email notifications
    177         StyleSpan bold = new StyleSpan(Typeface.BOLD);
    178         SpannableString line1 = new SpannableString("Alice: hey there!");
    179         line1.setSpan(bold, 0, 5, 0);
    180         SpannableString line2 = new SpannableString("Bob: hi there!");
    181         line2.setSpan(bold, 0, 3, 0);
    182         SpannableString line3 = new SpannableString("Charlie: Iz IN UR EMAILZ!!");
    183         line3.setSpan(bold, 0, 7, 0);
    184         mNotifications.add(new Notification.InboxStyle(
    185             new Notification.Builder(this)
    186                 .setContentTitle("24 new messages")
    187                 .setContentText("You have mail!")
    188                 .setSubText("test.hugo2 (at) gmail.com")
    189                 .setSmallIcon(R.drawable.stat_notify_email))
    190            .setSummaryText("+21 more")
    191            .addLine(line1)
    192            .addLine(line2)
    193            .addLine(line3)
    194            .build());
    195 
    196         // No idea what this would really look like since the app is in flux
    197         mNotifications.add(new Notification.Builder(this)
    198         .setContentTitle("Google+")
    199         .setContentText("Kanye West has added you to his circles")
    200         .setSmallIcon(R.drawable.googleplus_icon)
    201         .setPriority(Notification.PRIORITY_LOW)
    202         .build());
    203 
    204         mNotifications.add(new Notification.Builder(this)
    205         .setContentTitle("Twitter")
    206         .setContentText("New mentions")
    207         .setSmallIcon(R.drawable.twitter_icon)
    208         .setNumber(15)
    209         .setPriority(Notification.PRIORITY_LOW)
    210         .build());
    211 
    212         if (FIRE_AND_FORGET) {
    213             doPost(null);
    214             finish();
    215         }
    216     }
    217 
    218     public void doPost(View v) {
    219         for (int i=0; i<mNotifications.size(); i++) {
    220             mNoMa.notify(NOTIFICATION_ID + i, mNotifications.get(i));
    221         }
    222     }
    223 
    224     public void doRemove(View v) {
    225         for (int i=0; i<mNotifications.size(); i++) {
    226             mNoMa.cancel(NOTIFICATION_ID + i);
    227         }
    228     }
    229 }
    230