⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 usernotificationmanager.java

📁 moblie syncml mail javame
💻 JAVA
字号:
/*
 * Funambol is a mobile platform developed by Funambol, Inc. 
 * Copyright (C) 2003 - 2007 Funambol, Inc.
 * 
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Affero General Public License version 3 as published by
 * the Free Software Foundation with the addition of the following permission 
 * added to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED
 * WORK IN WHICH THE COPYRIGHT IS OWNED BY FUNAMBOL, FUNAMBOL DISCLAIMS THE 
 * WARRANTY OF NON INFRINGEMENT  OF THIRD PARTY RIGHTS.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU Affero General Public License 
 * along with this program; if not, see http://www.gnu.org/licenses or write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301 USA.
 * 
 * You can contact Funambol, Inc. headquarters at 643 Bair Island Road, Suite 
 * 305, Redwood City, CA 94063, USA, or at email address info@funambol.com.
 * 
 * The interactive user interfaces in modified source and object code versions
 * of this program must display Appropriate Legal Notices, as required under
 * Section 5 of the GNU Affero General Public License version 3.
 * 
 * In accordance with Section 7(b) of the GNU Affero General Public License
 * version 3, these Appropriate Legal Notices must retain the display of the
 * "Powered by Funambol" logo. If the display of the logo is not reasonably 
 * feasible for technical reasons, the Appropriate Legal Notices must display
 * the words "Powered by Funambol".
 *
 *
 */


package com.funambol.mailclient.ui.utils;

import com.funambol.mailclient.ui.controller.UIController;
import com.funambol.mailclient.config.MailClientConfig;
import com.funambol.util.Log;

import java.io.InputStream;
import java.io.IOException;
import javax.microedition.media.Manager;
import javax.microedition.media.MediaException;
import javax.microedition.media.Player;
import javax.microedition.media.control.VolumeControl;
import javax.microedition.midlet.MIDlet;

/**
 * This class implements different mechanisms to notify the user about events.
 * At the moment the only event that we notify is the presence of new emails.
 * This can be signaled in two different ways:
 *
 * 1) sound notification (tone and or vibration)
 * 2) visual notification (a change on the application icon or in the system
 * tray)
 *
 * Sound notification may be available or not, depending if playing sounds is
 * permitted on the device (some carriers restrict the use of this feature).
 * Visual notification is more rare, and it is not supported by standard MIDP
 * apis. For this reason this class allows the generation of sounds (provided
 * they are permitted on the device) but for the visual notification it only
 * provides an interface.
 *
 * Classes that extend UserNotificationManager (e.g.
 * @see BlackBerryUserNotificationManager) can implement this feature relying
 * on specific and non portable APIs.
 *
 */
public class UserNotificationManager {

    private static UserNotificationManager manager = null;
    private static boolean ableToSound             = false;

    protected static String newEmailTone = "/res/newmessage.midi";

    // We want this to be a singleton
    protected UserNotificationManager() {}

    public static UserNotificationManager getInstance() {
        if (manager == null) {
//#ifdef isBlackberry
//#         manager = new BlackBerryUserNotificationManager();
//#else
            manager = new UserNotificationManager();
//#endif
            // This check must be done only once
            checkSoundCapabilities();
        }
        return manager;
    }

    /**
     * Plays the new email tone. The tone is generated if the application is
     * allowed to play and if the user configured sound notification.
     * If the user enabled vibration notification, then this is also generated
     * by this method.
     */
    public void playNewEmailTone() {

        MailClientConfig mailClientConfig = UIController.getConfig();
                
        if (ableToSound && mailClientConfig.isSoundNotificationEnabled()) {
            Log.info("[UserNotificationManager] Playing sound...");
            // check if MessageMEdiaManager instance has been correctly created
            // i.e. devices restricting sound capabilities
            SoundPlayer sp = new SoundPlayer(newEmailTone);
            UIController.getThreadPool().startThread(sp);
        }
        if (mailClientConfig.isVibrateNotificationEnabled()) {
            Log.info("[UserNotificationManager] Vibrating");
            UIController.display.vibrate(1000);
        }
    }

    /**
     * Returns true iff the application can play sounds on this device.
     */
    public boolean isAbleToPlayTones() {
        return ableToSound;
    }

    /**
     * Show a new generic icon to indicate the presence of new messages.
     * This implementation is intentionally empty (see class description).
     */
    public void showNewEmailIcon() {
    }

    /**
     * Show a new icon to indicate the presence of a certain number of new messages.
     * This implementation is intentionally empty (see class description).
     *
     * @param newMessages number of new messages
     */
    public void showNewEmailIcon(int newMessages) {
    }

    /**
     * Hide the new email icon and restore the default application one.
     * This implementation is intentionally empty (see class description).
     */
    public void hideNewEmailIcon() {
    }

    /**
     * This method checks if playing sounds is permitted on this device. The
     * effect of the method is to set the ableToSound variable.
     * 
     */
    private static void checkSoundCapabilities() {
        // Testing ability to play sound
        InputStream is = UIController.midlet.getClass().getResourceAsStream(newEmailTone);
        Player player = null;
        try {
            player = Manager.createPlayer(is, "audio/midi");
            ableToSound = true;
        } catch (SecurityException se) {
            Log.error("[UserNotificationManager] The device carrier restricts " +
                    "Multi Media API usage to trusted applications only: " + se);
        } catch (IOException ex) {
            Log.error("[UserNotificationManager] IOException in playSound: " + ex);
            ex.printStackTrace();
        } catch (MediaException ex) {
            Log.error("[UserNotificationManager] MediaException in playSound: " + ex);
            ex.printStackTrace();
        } catch (IllegalArgumentException iae) {
            Log.error("[UserNotificationManager] Exception " +
                      "loading sound stream: " + iae);
            iae.printStackTrace();
        } finally {
            is = null;
            player = null;
        }
    }

    /**
     * This class implements a simple SoundPlayer. It is capable of playing
     * sounds if this operation is permitted.
     * The sound can be played in a stand alone thread to avoid having blocking
     * operations.
     */
    private class SoundPlayer implements Runnable {

        private Player player;
        private String midiResource;

        /**
         * Constructor.
         *
         * @param name of the midi file to be played
         */
        public SoundPlayer(String midiResource) {
            this.midiResource = midiResource;
        }
        
        /**
         * Plays a sound (midi file from resources)
         */
        public void playSound() {
            InputStream is = null;
            try {
                is = UIController.midlet.getClass().getResourceAsStream(midiResource);
                player = Manager.createPlayer(is, "audio/midi");
                Log.info("[SoundPlayer] play sound...");
                try {
                    player.realize();
                    
                    //Sound too loud for Nokia S60 3rd devices
                    //#ifdef volumecontrol
                    //# VolumeControl volume = (VolumeControl) player.getControl("VolumeControl");
                    //# volume.setLevel(60);
                    //#endif
                    Log.debug("[SoundPlayer] start sound...");
                    player.start();
                    Thread.sleep(3000);
                } catch (MediaException ex) {
                    Log.error("[SoundPlayer] MediaException in playSound: " + ex);
                    ex.printStackTrace();
                } catch (InterruptedException ex) {
                    Log.error("[SoundPlayer] InterruptedException in playSound: " + ex);
                    ex.printStackTrace();
                } catch (IllegalArgumentException iae) {
                    Log.error("[SoundPlayer] Exception loading sound stream: " + iae);
                    iae.printStackTrace();
                } finally {
                    if (player != null) {
                        try {
                            player.stop();
                        } catch (Exception e) {
                            // Nothing to do
                        }
                        player.close();
                        player = null;
                    }
                }
            } catch (Exception e) {
                Log.error("[SoundPlayer] Cannot create player " + e);
            } finally {
                if (is != null) {
                    try {
                        is.close();
                    } catch (IOException ioe) {
                        // Nothing to do
                    }
                }
            }
        }
        
        /**
         * Run it in separate thread if needed
         */
        public void run() {
            playSound();
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -