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

📄 alarmmanager.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.controller;

import com.funambol.util.DateUtil;
import com.funambol.util.Log;
import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
import javax.microedition.io.ConnectionNotFoundException;
import javax.microedition.io.PushRegistry;


/**
 * Singleton class to register/unregister alarms for polling
 */
public class AlarmManager {
    private static AlarmManager alarmManager;
    private static SyncTimerTask syncTimerTask;
    
    private static long alarmMillisecs;
    
    private static Timer aTimer;
    
    private static long alTime;
    
    
    /** Creates a new instance of OTAMessagesManager */
    private AlarmManager() {
    }
    
    /**
     * Unique instance for this class
     */
    public static AlarmManager getInstance() {
        if (alarmManager==null) {
            alarmManager = new AlarmManager();
        }
        return alarmManager;
    }
    
    /**
     * Set method for alarm milliseconds in case of registering alarm
     */
    public void setAlarm(long millisecs) {
        alarmMillisecs = millisecs;
    }
    
    /**
     * Schedule a new Alarm on PushRegistry for polling
     */
    public void scheduleMIDletAlarm() throws ClassNotFoundException, 
            ConnectionNotFoundException,
            SecurityException {
        cancelTimerTask();
        Date alarmDate = new Date(alarmMillisecs);
        Date newDate = updateConfig(alarmDate);
        long t = PushRegistry.registerAlarm(UIController.midlet.getClass().getName(),
                newDate.getTime());
        Log.info("New Alarm registered for " + newDate);
    }
    
    /**
     * Method to start a TimerTask when the midlet is running.
     * To be used in case of alarm within next 24 hours
     */
    public void startSyncTimerTask(int minutes) {
        cancelTimerTask();
        syncTimerTask = new SyncTimerTask();
        Log.info("Scheduled SyncTimerTask in " + minutes + " minutes");
        aTimer = new Timer();
        long time = minutes*60000;
        setTimeOfAlarmSetting();
        aTimer.schedule(syncTimerTask, time, time);
    }
    
    /**
     * Method to start a TimerTask when the midlet is running.
     * To be used in case of not repeating alarm 
     */
    public void startOneShotSyncTimerTask(int minutes) {
        Log.info("Scheduled One Shoot SyncTimerTask in " + minutes + " minutes");
        Timer t = new Timer();
        long time = minutes*60000;
        t.schedule(new SyncTimerTask(false), time);
    }
    
    
    /**
     * Method to start a TimerTask when the midlet is running.
     * To be used in case of periodic alarm in next 24 hours
     */
    public Date startSyncTimerTask(Date startDate, int days) {
        Date newDate = updateConfig(startDate);
        cancelTimerTask();
        syncTimerTask = new SyncTimerTask();
        aTimer = new Timer();
        long time = days*24*60*60000;
        setTimeOfAlarmSetting();
        aTimer.schedule(syncTimerTask, newDate, time);
        Log.info("Scheduled SyncTimerTask in days: " + days + " starting since " + newDate);
        return newDate;
    }
    
    /**
     * Method to cancel a scheduled timertask
     */
    public void cancelTimerTask() {
        Log.debug("[AlarmManager] checking if there are timer to cancel");
        if (syncTimerTask!=null) {
            Log.info("[AlarmManager] dropping previous SyncTimerTask...");
            syncTimerTask.cancel();
        } else {
            Log.debug("[AlarmManager] no SyncTimerTask to cancel");
        }
        if (aTimer != null) {
            Log.info("[AlarmManager] dropping aTimer");
            aTimer.cancel();
        } else {
            Log.debug("[AlarmManager] aTimer already null");
        }
    }
    
    private Date updateConfig(final Date alarmDate) {
        Date now = new Date();
        DateUtil dateUtil = new DateUtil(alarmDate);
        boolean isAfter = dateUtil.isAfter(now);
        if (!isAfter) {
            Log.info("Alarmdate is set in the past!");
            Date newDate = calcNewAlarmDate(alarmDate);
            UIController.mailClientConfig.setNextTimeAlarm(newDate.getTime());
            return newDate;
        } else {
            Log.info("Alarm is in the future");
            UIController.mailClientConfig.setNextTimeAlarm(alarmDate.getTime());
            return alarmDate;
        }
    }
    
    private Date calcNewAlarmDate(final Date alarmDate) {
        long newMillisecs = 1440*60000 + alarmDate.getTime();
        Date newDate = new Date(newMillisecs);
        return newDate;
    }
    
    
    public void setTimeOfAlarmSetting() {
        Date date = new Date();
        long time = date.getTime();
        Log.info("New Alarm configured");
        alTime = time;
    }
    
    public long getTimeOfAlarmSetting() {
        return alTime;
    }
    
    public Date getAlarmStartDate(Date date) {
        Calendar calendarNow = Calendar.getInstance();
        Calendar calendarAlarm = Calendar.getInstance();
        calendarAlarm.setTime(date);
        calendarAlarm.set(
                Calendar.DAY_OF_MONTH, calendarNow.get(Calendar.DAY_OF_MONTH));
        calendarAlarm.set(Calendar.MONTH, calendarNow.get(Calendar.MONTH));
        calendarAlarm.set(Calendar.YEAR, calendarNow.get(Calendar.YEAR));
        return calendarAlarm.getTime();
    }
    
    public Date getDateFromMinutes(int minutes) {
        DateUtil dateUtil = new DateUtil();
        return dateUtil.addMinutes(minutes).getTime();
    }
}

⌨️ 快捷键说明

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