generalsettingsform.java

来自「moblie syncml mail javame」· Java 代码 · 共 631 行 · 第 1/2 页

JAVA
631
字号
/*
 * 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.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.UIController;
import com.funambol.mailclient.ui.utils.UserNotificationManager;
import com.funambol.mailclient.loc.Localization;
import com.funambol.push.CTPService;
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 GeneralSettingsForm 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;
    //#ifdef RadioChoice
//#     private ChoiceGroup deletePropagationChoice = new ChoiceGroup(
//#             Localization.getMessages().MFSF_DELETE_PROPAGATION,
//#             Choice.EXCLUSIVE,OPTION_PROPAGATE,null);
    //#else
    private ChoiceGroup deletePropagationChoice = new ChoiceGroup(
            Localization.getMessages().MFSF_DELETE_PROPAGATION,
            Choice.POPUP, OPTION_PROPAGATE, null);
    //#endif
    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;
    private ModalPopup popup;
    private ChoiceGroup soundSettingsChoice;
    private String soundSettingsLabel;
    private ChoiceGroup suppressNotificationChoice;
    private DateField suppressToField;
    private DateField suppressFromField;
    private ChoiceGroup syncOnStartupChoice;
    private ChoiceGroup deleteConfirmationChoice;
    private boolean settedScheduler;
    
    // if in ctp but no ctp is received we'll go to scheduling mode 
    private static final int FALLBACK_SCHEDULING_INTERVAL = 10;
    
    private static final int NUMBER_OF_NON_SCHEDULED_OPTIONS = 2; //push and manual

    /**
     * Creates a new instance of GeneralSettingsForm
     */
    public GeneralSettingsForm() {
        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);
        
        //#ifndef forceSaveOnBackCommand
        addCommand(UIController.saveCommand);
        //#endif
        
        //#ifdef isBlackberry
//#     addCommand(UIController.blackberryExitCommand);
        //#endif

        //allows the user to reset the inbox only if SyncClient is not busy
        if (UIController.isSyncPermitted()) {
            addCommand(resetInboxCommand);
        }
        setItemStateListener(this);
        setCommandListener(this);
    }

    private void initFormItems() {
        appendSyncOnStartupChoice();
        if (mailClientConfig.isEnableDeletePropagation()) {
            deletePropagationChoice.setSelectedIndex(1, true);
        }
        appendSyncSetChoice();
        appendDeleteConfirmationChoice();
        append(deletePropagationChoice);
        appendSoundChoiche();
    }

    public void commandAction(Command command, Displayable displayable) {
        if (command == UIController.backCommand) {
            
            //Automatically saves the configuration 
            //when the back command is pressed on nokia phone 
            //#ifdef forceSaveOnBackCommand
            //# saveSettings();
            //#endif
            
            UIController.showBackScreen();
            
            UIController.getMailSettingsScreen = null;
        } else if (command == resetInboxCommand) {
            //reset Inbox
            popup =
                    new ModalPopup("Reset Client",
                    Localization.getMessages().RESETINBOX_MESSAGE,
                    new ResetPopupAction());
            UIController.showModalPopup(popup, this);
        } else if (command == UIController.blackberryExitCommand) {
            UIController.midlet.destroyApp(true);
        } else if (command == UIController.saveCommand) {
            saveSettings();
            UIController.showBackScreen();
        }
    }

    private void saveSchedulerSettings(int minutes) {
        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);
    }

    private void saveSettings() {

        boolean deletePropagation = deletePropagationChoice.isSelected(1);
        //   Log.debug("Delete Propagation: " + deletePropagation);
        mailClientConfig.enableDeletePropagation(deletePropagation);
        alarmManager = AlarmManager.getInstance();


        boolean[] startupOption = new boolean[1];

        setConfigSound();

        mailClientConfig.setSyncOnStartup(syncOnStartupChoice.isSelected(0));
        mailClientConfig.setDeleteConfirmation(deleteConfirmationChoice.isSelected(0));

        int syncChoice = syncSettingsChoice.getSelectedIndex();

        Log.debug("[SettingsForm] selected " + syncChoice);

        switch (syncChoice) {


            case 0:
                // Push Service
                if (mailClientConfig.getCtpPushStatus()==MailClientConfig.CTP_DISABLED) {
                    mailClientConfig.setCtpPushStatus(MailClientConfig.CTP_ENABLED);
                    CTPService.setOfflineMode(false);
                    CTPService.getInstance().restartService();


                    // turning on scheduling with fallback interval,
                    // since if ctp is not able to connect or receive
                    // messages, we wanna keep on scheduling!
                    saveSchedulerSettings(FALLBACK_SCHEDULING_INTERVAL);
                }                
                break;
            case 1:
                // Manual : disabling scheduler and ctp
                mailClientConfig.enableScheduler(false);
                alarmManager.cancelTimerTask();
                mailClientConfig.setNextTimeAlarm(0L);
                mailClientConfig.setCtpPushStatus(MailClientConfig.CTP_DISABLED);
                CTPService.setOfflineMode(true);
                break;
            default:
                // scheduling
                int minutes = getMinutes(syncChoice);
                //if (mailClientConfig.getPollInterval() != minutes) {
                mailClientConfig.setCtpPushStatus(MailClientConfig.CTP_DISABLED);
                CTPService.setOfflineMode(true);
                if (settedScheduler) {
                    saveSchedulerSettings(minutes);
                }
                break;
        }
        
        UIController.saveNewConfig(mailClientConfig);
            
    }

    private void setConfigSound() {
        // check if sound capabilities are allowed by jvm before getting settings
        boolean[] notificationOptions = new boolean[soundSettingsChoice.size()];
        soundSettingsChoice.getSelectedFlags(notificationOptions);
        if (notificationOptions.length == 1) {
            mailClientConfig.setVibrateNotification(notificationOptions[0]);
        } else {
            mailClientConfig.setSoundNotification(notificationOptions[0]);
            mailClientConfig.setVibrateNotification(notificationOptions[1]);
        }
    }

    /**
     * enable or disable the resetInboxCommand command
     * @param enable if true enable the command, if false disable the command
     */
    public void enableResetInboxCommand(boolean enable) {
        Log.debug(this, "Enable sync commands: " + enable);
        if (enable) {
            addCommand(resetInboxCommand);
        } else {
            removeCommand(resetInboxCommand);
        }
    }

    private int getMinutes(int syncChoice) {
        int minutes = 0;
        switch (syncChoice) {

            // TODO: use NUMBER_OF_NON_SCHEDULED_OPTIONS constants to get the
            // right choice
            
            
            // 0 push service    
            // 1 manually
            case 2:
                // Every 5 minutes
                minutes = 5;
                break;
            case 3:
                // Every 10 minutes
                minutes = 10;
                break;
            case 4:
                // Every 15 minutes
                minutes = 15;
                break;
            case 5:

⌨️ 快捷键说明

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