javauis/nokiauiapi_qt/javasrc/com/nokia/mid/ui/SoftNotification.java
changeset 79 2f468c1958d0
child 78 71ad690e91f5
equal deleted inserted replaced
76:4ad59aaee882 79:2f468c1958d0
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:
       
    15 *
       
    16 */
       
    17 
       
    18 package com.nokia.mid.ui;
       
    19 
       
    20 
       
    21 /**
       
    22  * <p>The <code>SoftNotification</code> is a class to manage soft
       
    23  * notifications. Soft notifications are reminders that inform the user of events
       
    24  * that have occurred in the user's absence, or while the user was busy with some
       
    25  * application. Text, and also graphics, can be used to communicate the message
       
    26  * to the user. Soft notifications are displayed as pop-ups in device home screen
       
    27  * (idle view).
       
    28  * Soft notifications are persistent and restarting the device does not delete
       
    29  * created soft notifications from the home screen.</p>
       
    30  *
       
    31  * <p>The user can respond to the soft notification by using the softkeys. The
       
    32  * softkey 1 is used for activating a function, for example opening a message
       
    33  * that has arrived. Application that created the soft notification will be activated
       
    34  * when the soft notification is accepted by the user. The softkey 2 is used to
       
    35  * discard the notification without taking any further action.</p>
       
    36  *
       
    37  * <p>The application that launched a soft notification can control it and also
       
    38  * discard it. It is possible to use the Applications key during a soft
       
    39  * notification; in that case, the soft notification disappears, but reappears
       
    40  * when the user returns to the device home screen, unless the application
       
    41  * responsible for the notification has discarded it.</p>
       
    42  *
       
    43  * <h3>Grouping soft notifications</h3>
       
    44  *
       
    45  * <p>Soft notifications can be displayed for the user in two different appearances:
       
    46  * <li> <b>Ungrouped</b> soft notification (left figure): These notifications
       
    47  * contain one piece of information each. In ungrouped soft notification there can
       
    48  * be one or two lines of text and an image visible.
       
    49  * Ungrouped appearance is used when there's
       
    50  * only one active soft notification or the topmost soft notification does not
       
    51  * support grouping.
       
    52  * <li> <b>Grouped</b> soft notification (right figure): If there are several
       
    53  * soft notifications on the screen and they support grouping, the notifications
       
    54  * are grouped and shown as a list of notifications.
       
    55  * Only one line of text is shown per a notification and there are no images visible.
       
    56  * The user can pick up one notification at a time and react
       
    57  * to it. If user discards the notification group, then all the shown notifications
       
    58  * are discarded and are not to be shown again.</p>
       
    59  *
       
    60  * <p><img src="images/ungrouped.jpg" alt="Image of ungrouped soft notification.">
       
    61  * <img src="images/grouped.jpg" alt="Image of grouped soft notification."></p>
       
    62  *
       
    63  * <p>Soft notifications created through this API may be grouped with other
       
    64  * platform notifications if all of them supports grouping. A soft notification
       
    65  * supports grouping if <code>groupText</code> is given in
       
    66  * {@link #setText(String text, String groupText)}</p>
       
    67  *
       
    68  * <h3>Persistent notifications</h3>
       
    69  * <p>Soft notifications are persistent meaning that when an application
       
    70  * has created a notification and set it to the home screen, the notification
       
    71  * exists in the home screen until:
       
    72  * <li>User accepts the notification,
       
    73  * <li>User dismisses the notification, or
       
    74  * <li>Application removes the notification</p>
       
    75  *
       
    76  * <p>A soft notification stays on the device home screen even if the application
       
    77  * is closed or the device is restarted.</p>
       
    78  *
       
    79  * <p>Accepting (selecting) a soft notification created by an application causes
       
    80  * this application to be activated in the following ways:
       
    81  * <li>If the application was running on the background, it will be set to
       
    82  * the foreground.
       
    83  * <li>If the application was running and had registered a listener
       
    84  * for the notification,
       
    85  * {@link SoftNotificationListener#notificationSelected(SoftNotification)}
       
    86  * is called.
       
    87  * <li>If the application is not running, it will be started and set to the
       
    88  * foreground.</p>
       
    89  *
       
    90  * <p><b>Notice</b> that accepting a notification belonging to a closed application
       
    91  * causes only the application to be started but does not
       
    92  * generate any listener events for the application. This is because there cannot
       
    93  * be any listeners registered by the closed application.
       
    94  * Thus, when an application is started by a notification, the application has
       
    95  * to decide which view is shown for the user without having the information about
       
    96  * the selected notification.</p>
       
    97  *
       
    98  * <p>Dismissing a soft notification does not bring the application to
       
    99  * the foreground or start the application neither. However, if a
       
   100  * listener has been set,
       
   101  * {@link SoftNotificationListener#notificationDismissed(SoftNotification)}
       
   102  * is called.</p>
       
   103  *
       
   104  * <p>Because soft notifications are persistent, they will remain on the home screen
       
   105  * even if the application is closed. It is possible to get access to these
       
   106  * notifications later when the application is restarted.
       
   107  * For accessing notifications later, the application has to store the IDs of
       
   108  * the notifications to a permanent memory (for example, RecordStore).
       
   109  * When the application is
       
   110  * restarted, the application may recreate those notifications by using the
       
   111  * stored IDs.</p>
       
   112  *
       
   113  * <p><b>Notice</b> that since it is possible that the user has dismissed notifications
       
   114  * when the application was closed, it is not possible to know whether the
       
   115  * recreated notification still exist on the device home screen.
       
   116  * In many cases it may be worthile to first remove all those notifications
       
   117  * from the screen and then add only the needed ones back to the screen.</p>
       
   118  *
       
   119  *
       
   120  * <h3>Example of usage</h3>
       
   121  *
       
   122  * <code style="white-space: pre">
       
   123  *class SoftNoteExample implements SoftNotificationListener
       
   124  *    {
       
   125  *    private SoftNotification iSoftNotification;
       
   126  *
       
   127  *    public SoftNoteExample()
       
   128  *        {
       
   129  *        iSoftNotification = SoftNotification.newInstance();
       
   130  *        iSoftNotification.setListener( this );
       
   131  *        }
       
   132  *
       
   133  *    public void Show1NewMail() throws SoftNotificationException
       
   134  *        {
       
   135  *        // Read image e.g. from the MIDlet JAR package or filesystem
       
   136  *        byte[] image = readImage( "mail.png" );
       
   137  *
       
   138  *        // Supports grouping since groupText is given here.
       
   139  *        iSoftNotification.setText( "You have 1 new mail", "1 new mail" );
       
   140  *        iSoftNotification.setSoftkeyLabels( "Show", "Exit" );
       
   141  *        iSoftNotification.setImage( image );
       
   142  *        iSoftNotification.post();
       
   143  *        }
       
   144  *
       
   145  *    public void notificationSelected(SoftNotification notification)
       
   146  *        {
       
   147  *        // called when user selects the soft notification
       
   148  *        }
       
   149  *
       
   150  *    public void notificationDismissed(SoftNotification notification)
       
   151  *        {
       
   152  *        // called when user dismisses the soft notification
       
   153  *        }
       
   154  *
       
   155  *    private byte[] readImage( String aImageName )
       
   156  *        {
       
   157  *        // Read image e.g. from the MIDlet JAR package or filesystem.
       
   158  *        byte[] imageData = ...
       
   159  *        //...
       
   160  *        return imageData;
       
   161  *        }
       
   162  *    }</code>
       
   163  */
       
   164 public abstract class SoftNotification
       
   165 {
       
   166 
       
   167     /**
       
   168      * Hidden default constructor.
       
   169      *
       
   170      * @see #newInstance()
       
   171      * @see #newInstance(int notificationId)
       
   172      */
       
   173     protected SoftNotification()
       
   174     {
       
   175     }
       
   176 
       
   177     /**
       
   178      * Returns a new instance of SoftNotification class with a reference to
       
   179      * previous soft notification instance.
       
   180      *
       
   181      * <p>Identification is used to access a soft notification created by another
       
   182      * instance of SoftNotification. If the provided identification is valid and
       
   183      * such soft notification exists, it can be updated and removed through
       
   184      * this instance. If the identification is not valid or does not exist,
       
   185      * it will be ignored and a new soft notification is created as defined in
       
   186      * {@link #newInstance()}.</p>
       
   187      *
       
   188      * @param notificationId Identification of previous soft notification.
       
   189      * @return An instance of SoftNotification class.
       
   190      * @see #getId()
       
   191      */
       
   192     public static SoftNotification newInstance(int notificationId)
       
   193     {
       
   194         return new SoftNotificationImpl(notificationId);
       
   195     }
       
   196 
       
   197     /**
       
   198      * <p>Returns a new instance of SoftNotification class. This instance's
       
   199      * identification will be a nonvalid negative value until
       
   200      * {@link #post()} is called.</p>
       
   201      *
       
   202      * @return an instance of SoftNotification class.
       
   203      */
       
   204     public static SoftNotification newInstance()
       
   205     {
       
   206         return new SoftNotificationImpl();
       
   207     }
       
   208 
       
   209     /**
       
   210      * <p>Get soft notification identification of this instance. Soft notifications are
       
   211      * persistent, so client should store the identification, if it wants to access the
       
   212      * soft notification from another instance e.g. after device is restarted.</p>
       
   213      *
       
   214      * <p><b>Notice</b> that identification might change during the lifetime of the
       
   215      * instance if {@link #post()} is called.</p>
       
   216      *
       
   217      * @return Soft notification identification.
       
   218      * @see #newInstance(int notificationId)
       
   219      */
       
   220     public abstract int getId();
       
   221 
       
   222     /**
       
   223      * <p>Shows a new soft notification or updates the content of previously posted
       
   224      * notification. If the application did not specify a valid identification in
       
   225      * the notification construction,
       
   226      * then new identification will be generated on first call to post(). If
       
   227      * previously posted soft notification does not exist, a new one is created
       
   228      * and a new identification will be generated. Client needs to set the attributes
       
   229      * shown in the soft notification before calling post or the default values
       
   230      * will be used. This call does not reset assigned text, image or softkeys.</p>
       
   231      *
       
   232      * <p><b>Notice</b> that identification of the soft notification may change every
       
   233      * time the function is called. So when needed, it is recommended to ask
       
   234      * the latest id from the notification by using {@link #getId()} function.</p>
       
   235      *
       
   236      * @throws SoftNotificationException Thrown if posting operation fails.
       
   237      */
       
   238     public abstract void post() throws SoftNotificationException;
       
   239 
       
   240     /**
       
   241      * <p>Removes the posted soft notification from the device home screen. If the
       
   242      * soft notification has not been posted or it has already been removed,
       
   243      * this call does nothing. Remove is automatically called when
       
   244      * the user accepts or dismisses the soft notification.</p>
       
   245      *
       
   246      * @throws SoftNotificationException Thrown if removing the note fails.
       
   247      */
       
   248     public abstract void remove() throws SoftNotificationException;
       
   249 
       
   250     /**
       
   251      * <p>Sets listener for the soft notification. Listener is notified when the
       
   252      * user accepts or dismisses the soft notification. Setting a new listener will
       
   253      * overwrite the previous listener. Listener can be removed by setting it to
       
   254      * <code>null</code>.</p>
       
   255      *
       
   256      * @param listener Listener for the soft notification.
       
   257      */
       
   258     public abstract void setListener(SoftNotificationListener listener);
       
   259 
       
   260     /**
       
   261      * <p>Sets the text field to be displayed in the soft notification. The
       
   262      * platform may truncate the text to a suitable size to fit into the soft
       
   263      * notification. If the text is not set before calling {@link #post()}, the
       
   264      * default value "" is used. An ungrouped soft notification has multiple lines
       
   265      * for the text, but a grouped notification has only one line.
       
   266      * Therefore the text assigned for a grouped notification should be
       
   267      * shorter. Multiple lines can be created by adding a line break
       
   268      * (character <code>'\n'</code>) inside the string.</p>
       
   269      *
       
   270      * <p>A notification supports grouping if <code>groupText</code> is
       
   271      * other than <code>null</code> or an empty string (<code>""</code>).</p>
       
   272      *
       
   273      * <p><b>Notice</b> that the modified soft notification data is not visible for
       
   274      * the user until {@link #post()} is called.</p>
       
   275      *
       
   276      * @param text A text to be displayed in the soft notification when ungrouped
       
   277      *        soft notification is shown.
       
   278      * @param groupText A text be to displayed in the soft notification when there are
       
   279      *        also other notifications visible (grouped form). The notification
       
   280      *        will not support grouping if <code>null</code> or an empty string
       
   281      *        (<code>""</code>) is used.
       
   282      *
       
   283      * @throws SoftNotificationException Thrown if setting text fails.
       
   284      */
       
   285     public abstract void setText(String text, String groupText)
       
   286     throws SoftNotificationException;
       
   287 
       
   288     /**
       
   289      * <p>Sets the textual labels for the softkeys. The platform may truncate the
       
   290      * text to a suitable size to fit to the softkey labels, so a relatively
       
   291      * short texts are preferred for the labels. If labels are not provided,
       
   292      * then the default softkeys will be used (Show & Exit).</p>
       
   293      *
       
   294      * <p>The functionalities of these softkeys remain always the same even if
       
   295      * their texts are changed. Softkey 1 (Show) opens
       
   296      * up the application and dismisses the soft notification. Softkey 2 (Exit)
       
   297      * dismisses the soft notification.</p>
       
   298      *
       
   299      * <p><b>Notice</b> that the modified soft notification data is not visible for
       
   300      * the user until {@link #post()} is called.</p>
       
   301      *
       
   302      * @param softkey1Label Text for the softkey 1.
       
   303      * @param softkey2Label Text for the softkey 2.
       
   304      *
       
   305      * @throws SoftNotificationException Thrown if setting softkeys fails.
       
   306      */
       
   307     public abstract void setSoftkeyLabels(String softkey1Label, String softkey2Label)
       
   308     throws SoftNotificationException;
       
   309 
       
   310     /**
       
   311      * <p>Sets an image to be displayed in the soft notification. The image can be
       
   312      * in any format that the platform's image decoder or SVG engine is able to
       
   313      * decode (for example, JPG, GIF, PNG, and SVG (even SVGS and MBM) images may
       
   314      * be supported in the platform). Image mask is used when available. </p>
       
   315      *
       
   316      * <p>The image is given as a byte array, for example the data may be read directly
       
   317      * from the file system or MIDlet JAR package.</p>
       
   318      *
       
   319      * <p>Images will be scaled to fit in the soft notification pop-up as the image
       
   320      * area of the notification is relatively small. For saving resources in scaling,
       
   321      * the size of the image should be kept small (preferably no larger than
       
   322      * 300x300 px).</p>
       
   323      *
       
   324      * <p>The function can also be used for removing an already set image from
       
   325      * the soft notification. This can be done by setting <code>null</code> instead
       
   326      * of any actual image.</p>
       
   327      *
       
   328      * <p><b>Notice</b> that the modified soft notification data is not visible for
       
   329      * the user until {@link #post()} is called.</p>
       
   330      *
       
   331      * @param image image to be displayed in the soft notification or
       
   332      *        <code>null</code> if any existing image should be removed
       
   333      *        from the notification.
       
   334      *
       
   335      * @throws SoftNotificationException Thrown if setting image fails.
       
   336      */
       
   337     public abstract void setImage(byte[] image) throws SoftNotificationException;
       
   338     
       
   339 }