📄 alarmmanager.java
字号:
/*
* Copyright (C) 2006-2007 Funambol
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* 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 General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
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) {
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 periodic alarm in next 24 hours
*/
public Date startSyncTimerTask(Date startDate, int days) {
Date newDate = updateConfig(startDate);
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.info("Previous SyncTimerTask cancelling...");
if (syncTimerTask!=null) {
syncTimerTask.cancel();
}
}
private Date updateConfig(final Date alarmDate) {
Date now = new Date();
DateUtil dateUtil = new DateUtil(alarmDate);
boolean isAfter = dateUtil.isAfter(now);
if (!isAfter) {
Log.info("Alarm is before now");
Date newDate = calcNewAlarmDate(alarmDate);
UIController.mailClientConfig.setNextTimeAlarm(newDate.getTime());
return newDate;
} else {
Log.info("Alarm is after now");
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 + -