📄 rmscontactmanager.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.cm.rms;
import com.funambol.mailclient.cm.ContactListFullException;
import com.funambol.mailclient.syncml.MailSyncSource;
import com.funambol.mailclient.syncml.PimItemEnumeration;
import com.funambol.mailclient.syncml.PimItemQueue;
import com.funambol.mailclient.syncml.PimItem;
import com.funambol.mailclient.cm.AlreadyExistentContactException;
import com.funambol.mailclient.cm.Contact;
import com.funambol.mailclient.cm.ContactManager;
import com.funambol.mailclient.cm.ContactManagerException;
import com.funambol.mailclient.Account;
import com.funambol.mailclient.syncml.ContactSyncSource;
import com.funambol.mailclient.config.ConfigException;
import com.funambol.mailclient.config.ConfigManager;
import com.funambol.syncml.spds.SourceConfig;
import com.funambol.syncml.spds.SyncException;
import com.funambol.syncml.spds.SyncSource;
import com.funambol.util.Log;
import com.funambol.util.StringUtil;
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.util.Enumeration;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;
import com.funambol.storage.ObjectStore;
import com.funambol.storage.ObjectFilter;
import com.funambol.storage.ObjectComparator;
import com.funambol.storage.ObjectEnumeration;
import com.funambol.mailclient.sm.SyncClient;
/**
* Contact Manager is useful to store contact information on the device's
* RMS persistent storage
*/
public class RMSContactManager implements ContactManager{
public static final String CONTACT_STORE = "ContactStore";
public static String SYNC_ITEM_STORE = "SyncItemStore";
private static long MIN_STORE_SIZE = 512*1024;
private PimItemQueue syncItemList = null;
private ObjectStore contactStore = null;
private String storeName = null;
private Contact existentContact = null;
private int defaultContactNumber = -1;
/**
* Creates a new instance of RMSContactManager
*/
public RMSContactManager(String contactStoreName) {
storeName = contactStoreName;
try {
contactStore = new ObjectStore();
contactStore.create(storeName);
syncItemList = new PimItemQueue();
if (ConfigManager.getConfig().getRmsStoreSize()<=MIN_STORE_SIZE) {
setDefaultContactNumber(CONTACT_LIST_MAX_SIZE);
} else {
setDefaultContactNumber(CONTACT_LIST_MAX_SIZE*10);
}
Log.debug("Number of contact to be stored: " +defaultContactNumber);
Log.debug("Maximum Space available on device's RMS: "
+ ConfigManager.getConfig().getRmsStoreSize());
Log.debug("Maximum Space Expected: " + MIN_STORE_SIZE);
} catch (RecordStoreException ex) {
Log.error("[ContactManager - Default Constructor]" + ex.toString());
} catch (Exception e) {
Log.error("[ContactManager - Default Constructor]" + e.toString());
}
}
/**
* Search the contact store for contacts matching with a given String
* @param filter is the contact filter
* @return RecordEnumeration sorted and filtered
*/
public Enumeration searchContacts(String filter)
throws ContactManagerException {
Enumeration e = null;
if (!StringUtil.isNullOrEmpty(filter)) {
contactStore.setObjectFilter(new ContactFilter(filter.getBytes()));
}
contactStore.setObjectComparator(new ContactComparator());
e = contactStore.getObjects(new SerializableContact());
ContactEnumeration ce = new ContactEnumeration((ObjectEnumeration) e);
contactStore.removeObjectComparator();
contactStore.removeObjectFilter();
return ce;
}
/**
* Record Comparator useful to sort record in ascending order
*/
private class ContactComparator implements ObjectComparator {
/**
* RecordComparator method - see javax.microedition.rms.RecordComparator
* javadoc for details
*/
public int compare(byte[] b, byte[] b0) {
ByteArrayInputStream bais1 = new ByteArrayInputStream(b);
ByteArrayInputStream bais2 = new ByteArrayInputStream(b0);
int result = 0;
try {
String visiblename1 =
getVisiblenameFromStream(new DataInputStream(bais1));
String visiblename2 =
getVisiblenameFromStream(new DataInputStream(bais2));
result = compareVisibleName(visiblename1, visiblename2);
} catch (IOException ex) {
ex.printStackTrace();
}
return result;
}
/**
* Search a suitable visibleName on the contact record stream
* @param in the stream where the visible name must be found
* @throw IOException
*/
private String getVisiblenameFromStream(DataInputStream in) throws IOException {
String visibleName = null;
String nickName = null;
String firstName = null;
String lastName = null;
String email = null;
//wonder if first name is stored
if (in.readBoolean()) {
nickName = in.readUTF();
}
if (in.readBoolean()) {
firstName = in.readUTF();
}
if (in.readBoolean()) {
lastName = in.readUTF();
}
if (in.readBoolean()) {
email = in.readUTF();
}
return buildVisibleName(nickName, firstName, lastName, email);
}
/**
* Returns a suitable visiblename given nickName, firstName, lastName,
* and email
* @param nickName is the nickName of the serialized contact;
* @param firstName is the firstName of the serialized contact;
* @param lastName is the lastName of the serialized contact;
* @param email is the email of the serialized contact;
*/
private String buildVisibleName(String nickName, String firstName,
String lastName, String email) {
if (!StringUtil.isNullOrEmpty(nickName)) {
return nickName;
}
if (!StringUtil.isNullOrEmpty(firstName)) {
if (!StringUtil.isNullOrEmpty(lastName)) {
return firstName + " " + lastName;
}
return firstName;
} else if (!StringUtil.isNullOrEmpty(lastName)&&
StringUtil.isNullOrEmpty(firstName)) {
return lastName;
}
return email;
}
/**
* Return an int that is the result of comparing 2 visibleName string
* @param visibleName1
* @param visibleName1
*/
private int compareVisibleName(String visibleName1, String visibleName2) {
visibleName1 = visibleName1.toLowerCase();
visibleName2 = visibleName2.toLowerCase();
if(visibleName1.compareTo(visibleName2)<0) {
return PRECEDES;
} else if(visibleName1.equals(visibleName2)) {
return EQUIVALENT;
} else {
return FOLLOWS;
}
}
}
/**
* Retrieve a contact from the contact store
* @param index is the contactId of the stored contact
* (it's the record number)
* @return Contact Object retrieved at the give index
*/
public Contact getContact(int index) throws ContactManagerException {
SerializableContact sc = new SerializableContact();
try {
sc = (SerializableContact) contactStore.retrieve(index, sc);
} catch (RecordStoreException ex) {
ex.printStackTrace();
Log.error(this, "getContact recordstore exception retrieving " +
"contact with index " + index);
} catch (IOException ex) {
Log.error(this, "getContact ioException retrieving contact +" +
"with index " + index);
ex.printStackTrace();
}
sc.setContactId(index);
return (Contact) sc;
}
/**
* Removes one contact from the contact store
* @param index is the index of the record to be removed (same as ContactId)
* @throw ContactManagerException when the contact cannot be retrieved
*/
public void removeContact(int index) throws ContactManagerException{
try {
SerializableContact sc = (SerializableContact) getContact(index);
addSyncItem(String.valueOf(sc.getContactId()), 'D');
contactStore.remove(index);
} catch (RecordStoreException ex) {
Log.error(this, "removeContact , recordstore exception removing " +
"contact with index" + index);
ex.printStackTrace();
throw new ContactManagerException("Contact manager error: " +
"unable to remove contact at index " + index);
}
}
/**
* Gets full Contact Store contacts
* @return Enumeration of contacts objects
*/
public Enumeration getContactList() throws ContactManagerException {
return searchContacts(null);
}
/**
* Update contact's information on the recordstore
* @param contact to be updated
* @throws ContactManagerException
*/
public void updateContact(Contact contact) throws ContactManagerException {
SerializableContact sc = new SerializableContact(
//contact.getContactId());
contact.getNickName(),
contact.getFirstName(),
contact.getLastName(),
contact.getDefaultEmail(),
contact.getEmail_2(),
contact.getEmail_3(),
contact.getHomePhone(),
contact.getJobPhone(),
contact.getMobilePhone());
try {
sc.setContactId(contact.getContactId());
contactStore.store(contact.getContactId(), sc);
addSyncItem(String.valueOf(contact.getContactId()), 'U');
} catch (RecordStoreException ex) {
ex.printStackTrace();
Log.error(this, "updateContact() recordstore exception while " +
"updating contact " + contact.getVisibleName() + ", throwing" +
" ContactManagerException");
throw new ContactManagerException("Contact manager error: " +
" unable to update contact");
} catch (IOException ex) {
Log.error(this, "IOException while " +
"updating contact " + contact.getVisibleName());
ex.printStackTrace();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -