public class AlarmTimer {
static Logger log=Logger.getLogger(AlarmTimer.class);
private Timer timer = new Timer();
private int minutes;
private int type = VIRGIN;
private String id;
private Object o;
private static Map timerQuery;
public static final int VIRGIN = 0;
public static final int CALL_STATUS = 1;
public static final int CALL_SMS = 2;
public static final int CALL_MMS = 3;
public static final int UPDATE_LOCATION = 4;
public static final int DUR_LOCATION = 5;
static {
timerQuery = new ConcurrentHashMap();
}
public AlarmTimer() {}
public AlarmTimer(int minutes, int type, Object o, String id) {
this.minutes = minutes;
this.type = type;
this.o = o;
this.id = id;
}
@SuppressWarnings("unchecked")
public void start() {
timerQuery.put(id, timer);
timer.schedule(new TimerTask() {
public void run() {
if (type == 1) {
delStatus((ConcurrentHashMap) o, id);
timer.cancel();
} else if (type == 2) {
delSms((SmsBean) o, id);
timer.cancel();
} else if (type == 3) {
delMms((MmsBean) o, id);
timer.cancel();
} else if (type == 4) {
updateLocation((ConcurrentHashMap) o, id);
timer.cancel();
} else if (type == 5){
duraLocation ((ConcurrentHashMap) o, id);
timer.cancel();
}
}
private void delStatus(ConcurrentHashMap hMap, String RegId) {
hMap.remove(RegId);
log.info(RegId+ " status removed!");
}
private void delSms(SmsBean smsBean, String RegId) {
try {
Set keys = timerQuery.keySet();
Iterator e = keys.iterator();
while (e.hasNext()) {
String temp = (String) e.next();
if (temp.equals(RegId)) {
smsBean.deleteBysmsId(RegId);
timerQuery.remove(id);
log.info(RegId+ " sms removed!");
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
private void delMms(MmsBean mmsBean, String RegId) {
try {
Set keys = timerQuery.keySet();
Iterator e = keys.iterator();
while (e.hasNext()) {
String temp = (String) e.next();
if (temp.equals(RegId)) {
mmsBean.deleteBymmsId(RegId);
timerQuery.remove(id);
log.info(RegId+ " mms removed!");
}
}
String TMP_PATH = Messages.getString("FILE_PATH");
String userName = mmsBean.getRegId();
String fileFullName = mmsBean.getFileUri();
String toDel = TMP_PATH + "/" + userName + "/"
+ fileFullName;
File file = new File(toDel);
if (file.delete()) {
log.info(RegId+ " mms file removed!");
} else {
log.warn(RegId+ " mms file does not exist!");
}
} catch (Exception e) {
e.printStackTrace();
}
}
private void updateLocation(ConcurrentHashMap hMap, String RegId) {
Notification notiEvent = null;
if (hMap.containsKey(RegId)) {
notiEvent = (Notification) hMap.get(RegId);
String num = notiEvent.getAddress();
int frequency = notiEvent.getFrequency();
Location loc = new Location();
try {
double latitude = loc.queryLatitude(num);
double longitude = loc.queryLongtitude(num);
DateFormater dFormater = new DateFormater(new Date());
String timeStamp = dFormater.dateFormat();
notiEvent.setLatitude(latitude);
notiEvent.setLongitude(longitude);
notiEvent.setTimeStamp(timeStamp);
log.info(RegId+ " location update!");
AlarmTimer aTimer = new AlarmTimer(frequency,
AlarmTimer.UPDATE_LOCATION, hMap, RegId);
aTimer.start();
// ---just for test show---
log.info(RegId+ " latitude update:" + latitude + " at:"
+ timeStamp);
log.info(RegId+ " longitude update:" + longitude + " at:"
+ timeStamp);
// ------------------------
} catch (Exception e) {
e.printStackTrace();
}
}
}
private void duraLocation(ConcurrentHashMap hMap,String RegId){
hMap.remove(RegId);
AlarmTimer aTimer=new AlarmTimer();
aTimer.delTimer(RegId);
LocationBean lBean=new LocationBean();
try {
lBean.delete(RegId);
} catch (Exception e) {
e.printStackTrace();
}
log.info(RegId+ " location service removed!");
}
}, minutes * 60 * 1000);
}
public void delTimer(String regId) {
this.id = regId;
Set keys = timerQuery.keySet();
Iterator e = keys.iterator();
while (e.hasNext()) {
String temp = (String) e.next();
if (temp.equals(id)) {
timer = (Timer) timerQuery.get(temp);
timerQuery.remove(id);
timer.cancel();
log.info(id+"Timer removed!");
}
}
}
}
|