Home | History | Annotate | Download | only in app
      1 /*
      2  * Copyright (C) 2007 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.example.android.apis.app;
     18 
     19 import com.example.android.apis.R;
     20 
     21 import android.app.Activity;
     22 import android.app.Notification;
     23 import android.app.NotificationManager;
     24 import android.app.PendingIntent;
     25 import android.content.Intent;
     26 import android.os.Bundle;
     27 import android.view.View;
     28 import android.widget.Button;
     29 import android.widget.RemoteViews;
     30 
     31 /**
     32  * Demonstrates adding notifications to the status bar
     33  */
     34 public class StatusBarNotifications extends Activity {
     35 
     36     private NotificationManager mNotificationManager;
     37 
     38     // Use our layout id for a unique identifier
     39     private static int MOOD_NOTIFICATIONS = R.layout.status_bar_notifications;
     40 
     41     @Override
     42     protected void onCreate(Bundle savedInstanceState) {
     43         super.onCreate(savedInstanceState);
     44 
     45         setContentView(R.layout.status_bar_notifications);
     46 
     47         Button button;
     48 
     49         // Get the notification manager serivce.
     50         mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
     51 
     52         button = (Button) findViewById(R.id.happy);
     53         button.setOnClickListener(new Button.OnClickListener() {
     54             public void onClick(View v) {
     55                 setMood(R.drawable.stat_happy, R.string.status_bar_notifications_happy_message,
     56                         false);
     57             }
     58         });
     59 
     60         button = (Button) findViewById(R.id.neutral);
     61         button.setOnClickListener(new Button.OnClickListener() {
     62             public void onClick(View v) {
     63                 setMood(R.drawable.stat_neutral, R.string.status_bar_notifications_ok_message,
     64                         false);
     65             }
     66         });
     67 
     68         button = (Button) findViewById(R.id.sad);
     69         button.setOnClickListener(new Button.OnClickListener() {
     70             public void onClick(View v) {
     71                 setMood(R.drawable.stat_sad, R.string.status_bar_notifications_sad_message, false);
     72             }
     73         });
     74 
     75         button = (Button) findViewById(R.id.happyMarquee);
     76         button.setOnClickListener(new Button.OnClickListener() {
     77             public void onClick(View v) {
     78                 setMood(R.drawable.stat_happy, R.string.status_bar_notifications_happy_message,
     79                         true);
     80             }
     81         });
     82 
     83         button = (Button) findViewById(R.id.neutralMarquee);
     84         button.setOnClickListener(new Button.OnClickListener() {
     85             public void onClick(View v) {
     86                 setMood(R.drawable.stat_neutral, R.string.status_bar_notifications_ok_message, true);
     87             }
     88         });
     89 
     90         button = (Button) findViewById(R.id.sadMarquee);
     91         button.setOnClickListener(new Button.OnClickListener() {
     92             public void onClick(View v) {
     93                 setMood(R.drawable.stat_sad, R.string.status_bar_notifications_sad_message, true);
     94             }
     95         });
     96 
     97         button = (Button) findViewById(R.id.happyViews);
     98         button.setOnClickListener(new Button.OnClickListener() {
     99             public void onClick(View v) {
    100                 setMoodView(R.drawable.stat_happy, R.string.status_bar_notifications_happy_message);
    101             }
    102         });
    103 
    104         button = (Button) findViewById(R.id.neutralViews);
    105         button.setOnClickListener(new Button.OnClickListener() {
    106             public void onClick(View v) {
    107                 setMoodView(R.drawable.stat_neutral, R.string.status_bar_notifications_ok_message);
    108             }
    109         });
    110 
    111         button = (Button) findViewById(R.id.sadViews);
    112         button.setOnClickListener(new Button.OnClickListener() {
    113             public void onClick(View v) {
    114                 setMoodView(R.drawable.stat_sad, R.string.status_bar_notifications_sad_message);
    115             }
    116         });
    117 
    118         button = (Button) findViewById(R.id.defaultSound);
    119         button.setOnClickListener(new Button.OnClickListener() {
    120             public void onClick(View v) {
    121                 setDefault(Notification.DEFAULT_SOUND);
    122             }
    123         });
    124 
    125         button = (Button) findViewById(R.id.defaultVibrate);
    126         button.setOnClickListener(new Button.OnClickListener() {
    127             public void onClick(View v) {
    128                 setDefault(Notification.DEFAULT_VIBRATE);
    129             }
    130         });
    131 
    132         button = (Button) findViewById(R.id.defaultAll);
    133         button.setOnClickListener(new Button.OnClickListener() {
    134             public void onClick(View v) {
    135                 setDefault(Notification.DEFAULT_ALL);
    136             }
    137         });
    138 
    139         button = (Button) findViewById(R.id.clear);
    140         button.setOnClickListener(new Button.OnClickListener() {
    141             public void onClick(View v) {
    142                 mNotificationManager.cancel(R.layout.status_bar_notifications);
    143             }
    144         });
    145     }
    146 
    147     private PendingIntent makeMoodIntent(int moodId) {
    148         // The PendingIntent to launch our activity if the user selects this
    149         // notification.  Note the use of FLAG_UPDATE_CURRENT so that if there
    150         // is already an active matching pending intent, we will update its
    151         // extras to be the ones passed in here.
    152         PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
    153                 new Intent(this, NotificationDisplay.class)
    154                         .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
    155                         .putExtra("moodimg", moodId),
    156                 PendingIntent.FLAG_UPDATE_CURRENT);
    157         return contentIntent;
    158     }
    159 
    160     private void setMood(int moodId, int textId, boolean showTicker) {
    161         // In this sample, we'll use the same text for the ticker and the expanded notification
    162         CharSequence text = getText(textId);
    163 
    164         // choose the ticker text
    165         String tickerText = showTicker ? getString(textId) : null;
    166 
    167         // Set the icon, scrolling text and timestamp
    168         Notification notification = new Notification(moodId, tickerText,
    169                 System.currentTimeMillis());
    170 
    171         // Set the info for the views that show in the notification panel.
    172         notification.setLatestEventInfo(this, getText(R.string.status_bar_notifications_mood_title),
    173                        text, makeMoodIntent(moodId));
    174 
    175         // Send the notification.
    176         // We use a layout id because it is a unique number.  We use it later to cancel.
    177         mNotificationManager.notify(MOOD_NOTIFICATIONS, notification);
    178     }
    179 
    180     private void setMoodView(int moodId, int textId) {
    181         // Instead of the normal constructor, we're going to use the one with no args and fill
    182         // in all of the data ourselves.  The normal one uses the default layout for notifications.
    183         // You probably want that in most cases, but if you want to do something custom, you
    184         // can set the contentView field to your own RemoteViews object.
    185         Notification notif = new Notification();
    186 
    187         // This is who should be launched if the user selects our notification.
    188         notif.contentIntent = makeMoodIntent(moodId);
    189 
    190         // In this sample, we'll use the same text for the ticker and the expanded notification
    191         CharSequence text = getText(textId);
    192         notif.tickerText = text;
    193 
    194         // the icon for the status bar
    195         notif.icon = moodId;
    196 
    197         // our custom view
    198         RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.status_bar_balloon);
    199         contentView.setTextViewText(R.id.text, text);
    200         contentView.setImageViewResource(R.id.icon, moodId);
    201         notif.contentView = contentView;
    202 
    203         // we use a string id because is a unique number.  we use it later to cancel the
    204         // notification
    205         mNotificationManager.notify(MOOD_NOTIFICATIONS, notif);
    206     }
    207 
    208     private void setDefault(int defaults) {
    209 
    210         // This method sets the defaults on the notification before posting it.
    211 
    212         // This is who should be launched if the user selects our notification.
    213         PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
    214                 new Intent(this, StatusBarNotifications.class), 0);
    215 
    216         // In this sample, we'll use the same text for the ticker and the expanded notification
    217         CharSequence text = getText(R.string.status_bar_notifications_happy_message);
    218 
    219         final Notification notification = new Notification(
    220                 R.drawable.stat_happy,       // the icon for the status bar
    221                 text,                        // the text to display in the ticker
    222                 System.currentTimeMillis()); // the timestamp for the notification
    223 
    224         notification.setLatestEventInfo(
    225                 this,                        // the context to use
    226                 getText(R.string.status_bar_notifications_mood_title),
    227                                              // the title for the notification
    228                 text,                        // the details to display in the notification
    229                 contentIntent);              // the contentIntent (see above)
    230 
    231         notification.defaults = defaults;
    232 
    233         mNotificationManager.notify(
    234                 MOOD_NOTIFICATIONS, // we use a string id because it is a unique
    235                                     // number.  we use it later to cancel the notification
    236                 notification);
    237     }
    238 }
    239