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

📄 logviewerform.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.view;

import com.funambol.mail.Address;
import com.funambol.mail.MailException;
import com.funambol.mail.Message;
import com.funambol.mailclient.config.ConfigManager;
import com.funambol.mailclient.sm.SyncClientConfig;
import com.funambol.mailclient.sm.SyncClient;
import com.funambol.mailclient.ui.controller.ContactsLoader;
import com.funambol.mailclient.ui.controller.UIController;
import com.funambol.mailclient.loc.Localization;
import com.funambol.mailclient.ui.controller.AppProperties;
import com.funambol.push.CTPService;
//#ifdef isBlackberry
//# import com.funambol.util.BlackberryHelper;
//#endif
import com.funambol.util.Log;
import com.funambol.util.LogViewer;
import com.funambol.util.MailDateFormatter;
import java.util.Date;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.StringItem;

/**
 * This form shows the log content collected by using the
 * application
 */
public class LogViewerForm extends Form implements CommandListener {
    
    private Command backCommand;
    private Command sendCommand;
    private Command enableCommand;
    private Command disableCommand;
    private Command clearCommand;
    
    String[] logEntries;
    String messageContent;
    StringBuffer sb;
    Message message;
    
    private String deviceId;
   private int logLevel;
    
    private String disabledLabel = Localization.getMessages().DISABLE_LOG_COMMAND;
    private String enabledLabel = Localization.getMessages().ENABLE_LOG_COMMAND;
    
    private String SUPPORT_ADDRESS = "fjm_logs@funambol.com";
    
    
    private StringItem endPage;
    
    private Command endPageCommand;
    
    /** Creates a new instance of LogViewerForm */
    public LogViewerForm() {
        super(Localization.getMessages().LOG_VIEWER_FORM_TITLE);
        messageContent = createContent();
        append(messageContent);
        
        backCommand = UIController.backCommand;
        sendCommand = new Command(Localization.getMessages().SEND_LOG_COMMAND, Command.OK, 10);
        enableCommand = new Command(enabledLabel, Command.OK, 0);
        disableCommand = new Command(disabledLabel, Command.OK, 0);
        clearCommand = new Command(Localization.getMessages().CLEAR_LOG_COMMAND, Command.OK, 20);
        endPageCommand = new Command(Localization.getMessages().END_LOG_COMMAND, Command.OK, 30);
        
        addCommand(backCommand);
        addCommand(clearCommand);
        //#ifdef isBlackberry
        //# addCommand(UIController.blackberryExitCommand);
        //#endif
        
        /*Got to end command removed because not working on Motorla devices:
         *focus doesn't mean scrolling...
         */
        //addCommand(endPageCommand);
        logLevel = UIController.mailClientConfig.getLogLevel();
        addCommand(getEnabledCommandFromLogLevel(logLevel));
        
        // allows to send log message if the SyncClient is not running
        if (UIController.isSyncPermitted()){
            addCommand(sendCommand);
        }
        endPage = new StringItem(null, "");
        setCommandListener(this);
    }
    
    /**
     * enable or disable the send command
     * @param enable if true enable the command, if false disable the command
     */
    public void enableSendCommand(boolean enable) {
        Log.debug(this, "Enable sync commands: " + enable);
        if(enable) {
            addCommand(sendCommand);
        } else {
            removeCommand(sendCommand);
        }
    }
    
    public void commandAction(Command command, Displayable displayable) {
        if (command == backCommand) {
            showSettingsScreen();
        } else if (command == endPageCommand) {
            this.append(endPage);
            UIController.display.setCurrentItem(endPage);
            
        } else if (command == clearCommand) {
            clearLogEntries();
        } else if (command == sendCommand) {
            message = new Message();
            message.setFrom(UIController.getMyFrom());
            message.setSubject(createSubject());
            
            try {
                message.setContent(messageContent);
                
                message.setTo(new Address[]
                {new Address(Address.TO, SUPPORT_ADDRESS)}
                );
            } catch (MailException ex) {
                ex.printStackTrace();
                Log.error(this, "Mail exception creating address for support email");
            }
            ModalPopup popup =
                    new ModalPopup(Localization.getMessages().SEND_LOG_POPUP_TITLE,
                    Localization.getMessages().SEND_LOG,
                    new SendLogPopupAction());
            UIController.showModalPopup(popup, this);
        } else if (command == enableCommand) {
            manageEnableCommand();
        } else if (command == disableCommand) {
            manageDisableCommand();
        } else if (command == UIController.blackberryExitCommand) {
            UIController.midlet.destroyApp( true );
        }
    }
    
    private void manageDisableCommand() {
        UIController.updateLogLevel(Log.DISABLED);
        removeCommand(disableCommand);
        addCommand(enableCommand);
        showSettingsScreen();
    }
    
    private void manageEnableCommand() {
        UIController.updateLogLevel(Log.ERROR);
        removeCommand(enableCommand);
        addCommand(disableCommand);
        showSettingsScreen();
    }
    
    private void showSettingsScreen() {
        UIController.showSettings(UIController.getInboxMessageList());
        UIController.logViewerScreen=null;
    }
    
    private void clearLogEntries() {
        Log.deleteLog();
        delete(0);
        sb = new StringBuffer();
        //writeClientStats();
        append(sb.toString());
    }
    
    private String createContent() {
        sb = new StringBuffer();
        writeClientStats();
        writeLogEntries();
        String content = sb.toString();
        return content;
    }
    
    private void writeLogEntries() {
        sb.append("\n\nLog\n");
        //append the log content
        LogViewer lv = new LogViewer();
        logEntries = lv.getLogEntries(lv.RMSLOG);
        for (int i=0; i<logEntries.length; i++) {
            sb.append(logEntries[i] + "\n");
        }
        sb.append("\n");
        sb.append(Localization.getMessages().END_OF_LOG_LABEL);
    }
    
    private void writeClientStats() {
        getDeviceID();
        sb.append("DeviceId: " + deviceId);
        long totalMemory = Runtime.getRuntime().totalMemory();
        long freeMemory = Runtime.getRuntime().freeMemory();
        sb.append("\n-Memory Stats-");
        sb.append("\nTotal Memory: ").append(totalMemory);
        sb.append("\nStartup FreeMem: ").append(UIController.startupFreeMem);
        sb.append("\nFree Memory: ").append(freeMemory);
        sb.append("\nThreads Running: ").append(UIController.getThreadPool().getRunnableCount());
        sb.append("\n-Client Config.-\n");
        sb.append(UIController.getConfig().toString());
        sb.append("\nInbox messages: " + UIController.getInboxMessageList().getNumMessages());
        
        if (!UIController.getContactsLoader().isLocked()) {
            sb.append("\nContacts: " + UIController.getContactManager().getContactCount());
        } else {
            sb.append("\nContacts: ");
        }
        
        sb.append("\nDevice UA: " + SyncClient.getSyncClient().getDeviceUA());
        sb.append("\nClient Version: " + UIController.getVersion());
        sb.append("\nClient Bundle: " + ConfigManager.appInfoAttr);
        
        //String ctp = UIController.midlet.getAppProperty("CTP");
        String ctp = UIController.appProperties.get(AppProperties.CTP);
        sb.append("\nCTP from jad: " + ctp);
        //#ifdef CtpPushRuntime
        //# sb.append("\nCTP enabled from ifdef" );
        //#endif

        sb.append("\nCTP status: " + CTPService.getInstance().getState());
        //#ifdef isBlackberry
        //# sb.append("\n" + BlackberryHelper.getConfigsDescription());
        //# sb.append("\nsaved config: " + BlackberryHelper.getSavedConfigDescription());
        //#endif
    }
    
    private String createSubject() {
        String dev = System.getProperty("microedition.platform");
        if (dev == null) {
            dev = "";
        }
        String subject = "FJM_Log: [" + ConfigManager.appInfoAttr + "]-[" +
                dev + "]-[" +
                MailDateFormatter.dateToRfc2822(
                new Date()) +
                "]";
        return subject;
    }
    
    private void getDeviceID() {
        
        SyncClientConfig conf = new SyncClientConfig();
        try {
            ConfigManager.load("syncml.config", conf);
        } catch (Exception ce) {
            //ce.printStackTrace();
            //Log.error(this, "error loading config");
        }
        
        deviceId = conf.getDeviceId();
    }
    
    /**
     * get the index of the item to be selected in
     * loglevelchoice from the given log level
     * @param loglevel the  log level
     * @return the index of loglevelchoice corresponding
     *  to the given log level
     */
    private Command getEnabledCommandFromLogLevel(int logLevel) {
        if (logLevel == Log.DISABLED) {
            return enableCommand;
        } else {
            return disableCommand;
        }
    }
    
    private class SendLogPopupAction implements PopupAction {
        public void cancel() {
            UIController.showBackScreen();
        }
        
        public void confirm() {
            UIController.sendMessage(message);
            
        }
        
    }
    
}

⌨️ 快捷键说明

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