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

📄 getmailsettingsform.java

📁 The Funambol J2ME Mail Client aims to be a light, easy to use, free email client for J2ME devices.
💻 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.view;

import com.funambol.mail.MailException;
import com.funambol.mailclient.config.MailClientConfig;
import com.funambol.mailclient.ui.controller.AlarmManager;
import com.funambol.mailclient.ui.controller.OTAMessagesListener;
import com.funambol.mailclient.ui.controller.UIController;
import com.funambol.mailclient.loc.Localization;
import com.funambol.util.Log;
import com.funambol.util.MailDateFormatter;


import javax.microedition.lcdui.Choice;
import javax.microedition.lcdui.ChoiceGroup;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.DateField;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.Item;

import java.util.Date;
import javax.microedition.lcdui.ItemStateListener;
import javax.microedition.rms.RecordStoreException;

/**
 * This form shows all the settings related to the
 * Auto Synchronization functions
 * The uesr will be able to configure scheduled
 * automated synchronizations
 */
public class GetMailSettingsForm extends Form implements CommandListener,
        ItemStateListener {
    private final String [] OPTION_PROPAGATE={
        Localization.getMessages().MFSF_DELETE_ON_DEVICE,
        Localization.getMessages().MFSF_DELETE_ON_SERVER};
    private ChoiceGroup syncSettingsChoice;
    private DateField scheduledTimeField;
    private ChoiceGroup scheduledSyncTime;
    private ChoiceGroup deletePropagationChoice = new ChoiceGroup(
            Localization.getMessages().MFSF_DELETE_PROPAGATION,
            Choice.POPUP,OPTION_PROPAGATE,null);
    private int scheduledMinutes[] = {5, 10, 15, 30};
    private int scheduledHours[] = {1, 2, 4};
    private int scheduledDays[] = {1};
    
    private String syncSettingsLabel;
    
    private final int MANUAL = 0;
    private final int SCHEDULED = 1;
    
    private String scheduledTimeFieldLabel;
    private int DAILY;
    
    private String scheduledSyncTimeLabel;
    private int minutesInADay = 1440;
    
    private MailClientConfig mailClientConfig;
    
    private AlarmManager alarmManager;
    private Command resetInboxCommand;
    
    /**
     * Creates a new instance of GetMailSettingsForm
     */
    public GetMailSettingsForm() {
        super(Localization.getMessages().AUTO_SYNC_SETTINGS_FORM_TITLE);
        this.mailClientConfig = UIController.getConfig();
        
        initFormItems();
        resetInboxCommand =
                new Command(Localization.getMessages().RESETINBOX_COMMAND,
                UIController.COMMAND_TYPE, 55);
        
        addCommand(UIController.backCommand);
        addCommand(UIController.saveCommand);
        addCommand(resetInboxCommand);
        setItemStateListener(this);
        setCommandListener(this);
    }
    
    private void initFormItems() {
        if (mailClientConfig.isEnableDeletePropagation()) {
            deletePropagationChoice.setSelectedIndex(1, true);
        }
        
        appendSyncSetChoice();
        append(deletePropagationChoice);
    }
    
    public void commandAction(Command command, Displayable displayable) {
        String dateStr = "";
        if (command == UIController.backCommand) {
            UIController.showBackScreen();
        } else if (command == resetInboxCommand) {
            //reset Inbox
            FunModalPopup popup =
                    new FunModalPopup("Reset Client",
                    Localization.getMessages().RESETINBOX_MESSAGE,
                    new ResetPopupAction());
            UIController.showModalPopup(popup, this);
        } else if (command == UIController.saveCommand) {
            boolean deletePropagation = deletePropagationChoice.isSelected(1);
            //   Log.debug("Delete Propagation: " + deletePropagation);
            mailClientConfig.enableDeletePropagation(deletePropagation);
            alarmManager = AlarmManager.getInstance();
            int syncChoice = syncSettingsChoice.getSelectedIndex();
            
            if (syncChoice==0) {
                // Manual
                mailClientConfig.enableScheduler(false);
                alarmManager.cancelTimerTask();
                mailClientConfig.setNextTimeAlarm(0L);
                UIController.saveNewConfig(mailClientConfig);
            } else {
                // Scheduled
                int minutes = getMinutes(syncChoice);
                mailClientConfig.setPollInterval(minutes);
                //   Log.debug("saving minutes: " + minutes);
                alarmManager.cancelTimerTask();
                
                if (minutes >= minutesInADay) {
                    Date date = scheduledTimeField.getDate();
                    Date alarmDate = null;
                    int days = minutes/minutesInADay;
                    if (date!=null) {
                        alarmDate = alarmManager.getAlarmStartDate(date);
                    } else {
                        alarmDate = new Date();
                        Log.info("Next Alarm Time is null. Configured in 24 h");
                    }
                    Date newDate = alarmManager.startSyncTimerTask(alarmDate, days);
                    mailClientConfig.setNextTimeAlarm(newDate.getTime());
                } else {
                    alarmManager.startSyncTimerTask(minutes);
                    mailClientConfig.setNextTimeAlarm(
                            alarmManager.getDateFromMinutes(minutes).getTime());
                }
                mailClientConfig.enableScheduler(true);
                UIController.saveNewConfig(mailClientConfig);
                dateStr = "\nNext Alarm: " + MailDateFormatter.dateToRfc2822(
                        new Date(mailClientConfig.getNextTimeAlarm()));
            }
            UIController.showBackScreen();
            //UIController.showAlert(
            //Localization.getMessages().ASSF_SETTINGS_SAVED_MESSAGE + " " +
            //dateStr, UIController.backStack.pop());
        }
    }
    
    private int getMinutes(int syncChoice) {
        int minutes = 0;
        switch (syncChoice) {
            case 1:
                // Every 5 minutes
                minutes = 5;
                break;
            case 2:
                // Every 10 minutes
                minutes = 10;
                break;
            case 3:
                // Every 15 minutes
                minutes = 15;
                break;
            case 4:
                // Every 30 minutes
                minutes = 30;
                break;
            case 5:
                // Every 1 hour
                minutes = 60;
                break;
            case 6:
                // Every 2 hours
                minutes = 120;
                break;
            case 7:
                // Every 4 hours
                minutes = 240;
                break;
            case 8:
                // Every day
                minutes = 24*60;
                break;
            default:
                break;
        }
        return minutes;
    }
    
    
    private void appendSyncSetChoice() {
        boolean showTime = false;
        if (syncSettingsChoice == null) {
            syncSettingsLabel = Localization.getMessages().ASSF_SYNC_SETTINGS_CHOICE_LABEL;
            syncSettingsChoice = new ChoiceGroup(syncSettingsLabel, Choice.POPUP, new String[] {
                Localization.getMessages().ASSF_SYNC_SETTINGS_CHOICE_MAN,
                Localization.getMessages().ASSF_SYNC_SETTINGS_CHOICE_5M,
                Localization.getMessages().ASSF_SYNC_SETTINGS_CHOICE_10M,
                Localization.getMessages().ASSF_SYNC_SETTINGS_CHOICE_15M,
                Localization.getMessages().ASSF_SYNC_SETTINGS_CHOICE_30M,
                Localization.getMessages().ASSF_SYNC_SETTINGS_CHOICE_1H,
                Localization.getMessages().ASSF_SYNC_SETTINGS_CHOICE_2H,
                Localization.getMessages().ASSF_SYNC_SETTINGS_CHOICE_4H,
                Localization.getMessages().ASSF_SYNC_SETTINGS_CHOICE_1D
            }, new Image[] {
                null,null,null,null,null,null,null,null,null
            });
            int selectedIndex = 0;
            if (UIController.getConfig().isSchedulerEnabled()) {
                Log.info("Scheduling enabled");
                int minutes = UIController.getConfig().getPollInterval();
                selectedIndex = getScheduledIndex(minutes)+1;
                if (minutes >= minutesInADay) {
                    getScheduledTimeField();
                    Date nextAlarmDate = new Date(mailClientConfig.getNextTimeAlarm());
                    Log.info("Next Alarm Date: " + MailDateFormatter.dateToUTC(nextAlarmDate));
                    scheduledTimeField.setDate(nextAlarmDate);
                    showTime=true;
                }
            }
            
            syncSettingsChoice.setSelectedIndex(selectedIndex, true);
            
            
        }
        append(syncSettingsChoice);
        if (showTime) {
            append(scheduledTimeField);
        }
    }
    
    private Item getScheduledTimeField() {
        if (scheduledTimeField == null) {
            scheduledTimeFieldLabel = Localization.getMessages().ASSF_SCHEDULED_TIME_FIELD_LABEL;
            scheduledTimeField =
                    new DateField(scheduledTimeFieldLabel, DateField.TIME);
        }
        return scheduledTimeField;
    }
    
    
    public void itemStateChanged(Item item) {
        if (item == syncSettingsChoice) {
            int index = syncSettingsChoice.getSelectedIndex();
            if (index==8) {
                addTimeChoice();
            } else {
                removeTimeChoice();
            }
        }
    }
    
    private void removeTimeChoice() {
        while (size()>2) {
            delete(1);
        }
    }
    /**
     * Method to get the index for the scheduling choice to be set
     * based on the pollInterval property of the mailClientConfig
     * @return Index to set the choice of scheduling interval
     */
    public int getScheduledIndex(int minutes) {
        Log.info("PollInterval minutes found: " + minutes);
        for (int i=0; i<scheduledMinutes.length;i++) {
            if (minutes==scheduledMinutes[i]) {
                return i;
            }
        }
        int hours = minutes/60;
        for (int i=0; i<scheduledHours.length;i++) {
            if (hours==scheduledHours[i]) {
                return scheduledMinutes.length+i;
            }
        }
        int days = hours/24;
        for (int i=0; i<scheduledDays.length;i++) {
            if (days==scheduledDays[i]) {
                return scheduledMinutes.length+scheduledHours.length+i;
            }
        }
        Log.error("Wrong pollInterval setting, no setting.");
        return 0;
    }
    
    private void addTimeChoice() {
        insert(1,getScheduledTimeField());
    }
    
    public class ResetPopupAction implements PopupAction {
        public ResetPopupAction() {
        }
        
        public void cancel() {
            UIController.showBackScreen();
        }
        
        public void confirm() {
            try {
                UIController.showAlert("Resetting Inbox...");
                Log.info("Resetting inbox...");
                UIController.resetInboxFromServer();
            } catch (MailException e) {
                Log.error("Unable to resetWithSlow: " + e.getMessage());
                e.printStackTrace();
            } catch (RecordStoreException e) {
                Log.error("Unable to resetWithSlow: " + e.getMessage());
                e.printStackTrace();
            } finally {
                UIController.getInboxMessageList().saveSnapshot();
            }
        }
        
        
    }
}

⌨️ 快捷键说明

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