📄 uicontroller.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.mailclient.Cache;
import com.funambol.mailclient.Funambol;
import com.funambol.mailclient.cm.AlreadyExistentContactException;
import com.funambol.mailclient.cm.ContactManagerFactory;
import com.funambol.mailclient.sm.SyncClient;
import com.funambol.mailclient.ui.utils.UiUtils;
import com.funambol.mailclient.ui.view.AboutScreen;
import com.funambol.mailclient.ui.view.AccountSettingsForm;
import com.funambol.mailclient.ui.view.AddContactForm;
import com.funambol.mailclient.ui.view.AdvancedContactForm;
import com.funambol.mailclient.ui.view.ComposeMessageForm;
import com.funambol.mailclient.ui.view.FolderList;
import com.funambol.mailclient.ui.view.FunCanvasContactList;
import com.funambol.mailclient.ui.view.FunCanvasViewMessage;
import com.funambol.mailclient.ui.view.FunambolAlert;
import com.funambol.mailclient.ui.view.GetMailSettingsForm;
import com.funambol.mailclient.ui.view.LinkList;
import com.funambol.mailclient.ui.view.LogSettingsForm;
import com.funambol.mailclient.ui.view.LogViewerForm;
import com.funambol.mailclient.ui.view.RoundedDrawer;
import com.funambol.mailclient.ui.view.Drawer;
//import com.funambol.mailclient.ui.view.PhoneNumberList;
import com.funambol.mailclient.ui.view.SettingsList;
import com.funambol.mailclient.ui.view.FunCanvasDraftMessageList;
import com.funambol.mailclient.ui.view.FunCanvasInboxMessageList;
import com.funambol.mailclient.ui.view.FunCanvasMessageList;
import com.funambol.mailclient.ui.view.FunCanvasOutboxMessageList;
import com.funambol.mailclient.ui.view.FunCanvasSentMessageList;
import com.funambol.mailclient.ui.view.FunCanvasRecipientList;
import com.funambol.mailclient.ui.view.FunModalPopup;
import com.funambol.mailclient.loc.Localization;
import com.funambol.mailclient.cm.Contact;
import com.funambol.mailclient.cm.ContactManager;
import com.funambol.mailclient.cm.ContactManagerException;
import com.funambol.mailclient.mm.FolderInfo;
import com.funambol.mailclient.mm.MessageInfo;
import com.funambol.mailclient.mm.MessageManager;
import com.funambol.mailclient.mm.SyncTaskManager;
import com.funambol.mailclient.config.*;
import com.funambol.mail.Address;
import com.funambol.mail.MailException;
import com.funambol.mail.Message;
import com.funambol.mail.MessageFlags;
import com.funambol.mail.Store;
import com.funambol.mail.StoreFactory;
import com.funambol.syncml.spds.SyncException;
import com.funambol.syncml.spds.SourceConfig;
import com.funambol.syncml.protocol.SyncML;
import com.funambol.util.MailDateFormatter;
import com.funambol.util.StringUtil;
import com.funambol.util.ThreadPool;
import com.funambol.util.Log;
import com.funambol.util.ThreadPoolMonitor;
import java.io.IOException;
import java.io.InputStream;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
import javax.microedition.io.ConnectionNotFoundException;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Item;
import javax.microedition.midlet.MIDlet;
import javax.microedition.rms.RecordStoreException;
/**
* It is the Controller class that controls all interactions
* between screens and the model
* It maintains a pool of shared UI instances
*/
public class UIController {
private static final String RE=Localization.getMessages().RE_LABEL;
private static final String FW=Localization.getMessages().FW_LABEL;
public static final int SENT_MESSAGE=0;
public static final int OUTGOING_MESSAGE=1;
public static final int INCOMING_MESSAGE=2;
public static final int COMMAND_TYPE = Command.OK;
public static Funambol midlet;
public static Display display;
// The command pool
public static Command backCommand =
new Command(Localization.getMessages().BACK_COMMAND,
Command.BACK, 0);
public static Command selectCommand =
new Command(Localization.getMessages().SELECT_COMMAND,
UIController.COMMAND_TYPE, 0);
public static Command cancelCommand =
new Command(Localization.getMessages().CANCEL_COMMAND,
Command.BACK, 10);
public static Command saveCommand =
new Command(Localization.getMessages().SAVE_COMMAND,
UIController.COMMAND_TYPE, 1);
public static Command inboxCommand =
new Command(Localization.getMessages().INBOX_COMMAND,
UIController.COMMAND_TYPE, 1);
// The contact manager
private static ContactManager contactManager;
// The screen pool
public static ComposeMessageForm composeMessageScreen;
private static FunCanvasInboxMessageList inboxMessageList;
private static FunCanvasOutboxMessageList outboxMessageList;
private static FunCanvasSentMessageList sentMessageList;
private static FunCanvasDraftMessageList draftMessageList;
protected static FunCanvasContactList contactList;
// The screen stack
public static Stack backStack = new Stack();
// Thread pool
private static ThreadPool threadPool;
private ThreadPoolMonitor uiThreadMonitor;
private static final int MAX_NUM_THREADS = 4;
public static MessageManager messageManager;
public static MailClientConfig mailClientConfig;
// Folder informations
private static FolderInfo[] folderInfo;
public static boolean otaStarted;
//public static long startAppProbe;
public static long startupFreeMem;
//private static long heapSize = Runtime.getRuntime().totalMemory();
private static ContactListLoader contactListLoader;
private static MessageWriter messageWriter;
private static SyncTaskManager syncTaskManager;
private static Drawer drawer;
private UIController() {
}
public static void initThreadPool() {
ThreadPoolMonitor uiThreadMonitor = new UIThreadPoolMonitor();
threadPool=new ThreadPool(uiThreadMonitor, MAX_NUM_THREADS);
}
public static ThreadPool getThreadPool() {
return threadPool;
}
/**
* enable or disable the sync and reset commands from inbox and contact lists,
* the send command from compose and the
* if needed
*
* @param enable enable the commands if true, disable the commands if false
*/
public static void enableSyncCommands( boolean enable ) {
// TODO: add the remove for send log command
if (contactList != null) {
contactList.enableSyncCommand(enable);
}
if (inboxMessageList != null) {
getInboxMessageList().enableSyncCommand(enable);
}
if (composeMessageScreen!=null) {
getComposeMessageScreen().enableSendCommand(enable);
}
}
/**
* init the inbox: calls initmessagemanager, create the cache and display
* the inbox with the cached messages, then starts the thread that loads the
* messages from the rms
*
* @return true if the account has been set, false otherwise.
* The account is considered set if the url is different from
* Account.DEFAULT_URL and if the username and password are not empty.
*
*/
public static boolean initInbox() {
boolean accountSet = false;
try {
initMessageManager();
//long startInbox = System.currentTimeMillis();//XXX:time
//long getCache = System.currentTimeMillis();//XXX:time
MessageInfo[] cacheList = Cache.getMsgSnapShot(Cache.INBOX);
//long gotCache = System.currentTimeMillis();//XXX:time
// Log.debug("Got Snapshot");
// Log.debug("Cache list size = " + cacheList.length);
//create the inbox using the cache
inboxMessageList = new FunCanvasInboxMessageList(cacheList);
outboxMessageList = null;
draftMessageList = null;
sentMessageList = null;
accountSet = mailClientConfig.getMailAccount().isSet();
// If the account information has been already set
if (accountSet) {
display.setCurrent(inboxMessageList);
// this starts the thread that load messages from rms
getThreadPool().startThread(inboxMessageList);
} else {
Log.info("initInbox: go to account settings.");
// Some devices, like the RAZR, don't show two display
// if the calls to display.setcurrent are too close.
//sleep(10);
showAlert(Localization.getMessages().CONFIG_NOT_SET,
getAccountSettingsForm(inboxMessageList));
}
/*
long endInbox = System.currentTimeMillis();//XXX:time
Log.addProbe("\n_probe_start_cache = " + getCache);//XXX:time
Log.addProbe("\n_probe_end_cache = " + gotCache);//XXX:time
Log.addProbe("\n_probe_start_createInbox = " + startInbox);//XXX:time
Log.addProbe("\n_probe_end_createInbox = " + endInbox);//XXX:time
Log.addProbe("\n_probe_start_showInbox = " + startAppProbe);//XXX:time
Log.addProbe("\n_probe_end_showInbox = " + System.currentTimeMillis());
*/
} catch (Exception ex) {
Log.error("UIController: error starting sync or getting cache" + ex);
ex.printStackTrace();
//if (display.getCurrent() != inboxMessageList) {
// Log.error("inboxmessagelist not shown, resetting it");
// resetInboxMessageList();
// display.setCurrent(getInboxMessageList());
//}
}
return accountSet;
}
/**
* Suspend the current thread for the given number of seconds
*
* @param millis the time to wait (milliseconds)
*/
public static void sleep(int millis) {
try {
Thread.sleep(millis);
} catch (Exception e) {
Log.error("sleep interrupted.");
};
}
/**
* mesure the currently used memory
*
* @return current used memory in bytes
*/
public static long startMemMeasure() {
//Runtime.getRuntime().gc();
long totalb4 = Runtime.getRuntime().totalMemory();
long freeb4 = Runtime.getRuntime().freeMemory();
long usedb4 = totalb4 - freeb4;
return usedb4;
}
/**
* calculate the difference between current used memory and the given used memory,
*and print a log.error message with the given string
*
* @param msg a string that will be used to identify the element
* that use the memory
* @param usedb4 the memory used before the element was activated
*
*/
public static void calcMem(String msg, final long usedb4) {
//Runtime.getRuntime().gc();
long totalAfter = Runtime.getRuntime().totalMemory();
long freeAfter = Runtime.getRuntime().freeMemory();
long usedAfter = totalAfter - freeAfter;
Log.error("Memory used by " + msg + " = " + (usedAfter - usedb4) );
}
/**
* init the messagemanager and the store
*/
public static void initMessageManager() {
try {
//Log.debug("MessageManager init");
messageManager = MessageManager.getInstance();
initStore(false);
} catch (Exception ex) {
Log.error("UIController: error init MessageManager " + ex);
ex.printStackTrace();
}
}
/**
* init the message store
* @param reset if true the folders will be erased and recreated
*/
public static void initStore(boolean reset) throws MailException {
messageManager.initStore(reset);
if (reset) {
folderInfo=null;
}
}
/**
*
* showXXX methods managing the views
*
*/
/**
* This method retrieves the screen to go back from the Stack
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -