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

📄 rmscontactmanager.java

📁 moblie syncml mail javame
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        }
        SerializableContact sc =
                new SerializableContact(
                contact.getNickName(),
                contact.getFirstName(),
                contact.getLastName(),
                contact.getDefaultEmail(),
                contact.getEmail_2(),
                contact.getEmail_3(),
                contact.getHomePhone(),
                contact.getJobPhone(),
                contact.getMobilePhone());
        
        
        try {
            int id = contactStore.store(sc);
            //FIXED FOR DUPLICATED CONTACTS:
            //Duplicated Contacts avoided by initCache invocation - see constructor
            contact.setContactId(id);
            
            int pos = getPosition(contact);
            getContactsVector().insertElementAt(contact, pos);
            if (addToJournal) {
                addSyncItem(String.valueOf(contact.getContactId()), SyncItem.STATE_NEW);
            }
        } catch (RecordStoreException ex) {
            ex.printStackTrace();
            
            Log.error("[addContact] RecordStoreException: " + ex.toString());
            
            throw new ContactManagerException("Contact manager error: " +
                    " unable to add contact");
        } catch (IOException ex) {
            
            Log.error("[addContact] IOException: " + ex.toString());
            
            ex.printStackTrace();
        }
        
        if(listener != null) {
            Log.debug("Sending update event (add) ");
            ContactManagerEvent event = new ContactManagerEvent(
                    ContactManagerEvent.EVENT_CODE_ADD_CONTACT, contact);
            listener.update(event);
        }
        
        return contact.getContactId();
        //}
        
    }
    
    /**
     * search contact list for contacts with given string as default email
     * @param email the string to be searched
     * @return the contact if found, null otherwise
     */
    public Contact exists(String email) throws ContactManagerException {
        
        Enumeration e = getContactsVector().elements();
        
        while (e.hasMoreElements()) {
            Contact c = (Contact) e.nextElement();
            //Log.debug(c.getDefaultEmail()+"\n"+contactField);
            if (c.getDefaultEmail().toLowerCase().equals(email.toLowerCase())) {
                return c;
            }
        }
        return null;
    }
    
    /**
     * Search contact with given email addres
     * @param email the string to be searched
     * @return the Contact with the give email address (default - second or third) , or null if
     * the contact is not present
     *
     */
    
    public Contact searchContactFromMail(String email) throws ContactManagerException {
        
        Enumeration e = getContactsVector().elements();
        
        while (e.hasMoreElements()) {
            Contact c = (Contact) e.nextElement();
            
            String defaultMail = c.getDefaultEmail();
            String secondMail = c.getEmail_2();
            String thirdMail = c.getEmail_3();
            
            if(!StringUtil.isNullOrEmpty(defaultMail) && StringUtil.equalsIgnoreCase(defaultMail, email)){
                return c;
            }
            
            if(!StringUtil.isNullOrEmpty(secondMail) && StringUtil.equalsIgnoreCase(secondMail,email)){
                return c;
            }
            
            if(!StringUtil.isNullOrEmpty(thirdMail) && StringUtil.equalsIgnoreCase(thirdMail,email)){
                return c;
            }
        }
        return null;
    }
    
    
    public void removeSyncItem(String key) {
        syncItemList.removeItemInfo(key);
    }
    
    
    
    /**
     * adds the given sync item with the given status
     * @param key the key to be used for storin the item
     * @status one of
     * <ul>
     * <li>SyncItem.STATE_UPDATED</li>
     * <li>SyncItem.STATE_NEW</li>
     * <li>SyncItem.STATE_DELETED</li>
     *</ul>
     */
    
    public void addSyncItem(String key, char status){
        Log.debug("Adding sync item " + key + "with status '" + status + "'");
        syncItemList.addItemInfo(key, status);
    }
    
    /**
     * @return the sync items with given state.
     * @param state ostate of the items to be returned, predefined states are
     * <ul>
     * <li>SyncItem.STATE_DELETED</li>
     * <li>SyncItem.STATE_NEW</li>
     * <li>SyncItem.STATE_UNDEF</li>
     * <li>SyncItem.STATE_UPDATED</li>
     * </ul>
     */
    public Enumeration getSyncItems(char state) {
        return syncItemList.getItemInfos(state);
        
    }
    public PimItem getSyncItemInfo(String key) {
        return (PimItem) syncItemList.getItemInfo(key);
    }
    /**
     * reset the contact list deleting the contact store and
     * recreating it. It also reset the list of the sync items
     */
    public void resetContactList() {
        try {
            contactStore.close();
            AbstractRecordStore.deleteRecordStore(CONTACT_STORE);
            contactStore.create(CONTACT_STORE);
            contactsVector=new Vector();
            contactStore.open(CONTACT_STORE);
            syncItemList.resetPimItemQueue();
        } catch (Exception ex) {
            Log.error(this, "resetContactList() exception: " + ex.toString());
            ex.printStackTrace();
        }
        
        if(listener != null) {
            Log.debug("Sending update event (reset) ");
            ContactManagerEvent event = new ContactManagerEvent(
                    ContactManagerEvent.EVENT_CODE_RESET_CONTACT_LIST,null);
            listener.update(event);
        }
        
    }
    
    private void removeSyncItem(char state) {
        PimItemEnumeration itemToRemove = (PimItemEnumeration) getSyncItems(state);
        while (itemToRemove.hasMoreElements()) {
            PimItem nextItem = (PimItem) itemToRemove.nextElement();
            syncItemList.removeItemInfo(nextItem.getKey());
        }
        itemToRemove=null;
    }
    /**
     * set the default contact number. the de
     * @param newDefaultContactNumber the new default contact number.
     * default is Integer.MAX_VALUE
     */
    public void setDefaultContactNumber(int newDefaultContactNumber) {
        this.defaultContactNumber = newDefaultContactNumber;
    }
    /**
     * @return the current default contact number
     */
    public int getDefaultContactNumber() {
        return this.defaultContactNumber;
    }
    /**
     * @return the total number of contacts
     */
    public int getContactCount() {
        return contactStore.size();
    }
    
    private Vector getContactsVector() {
        //initcache build the contactsvector
        if (contactsVector == null) {
            //cacheLoaded=false;
            initCache();
        }
        return contactsVector;
    }
    
    /**
     * set the current listener
     * @param listener the listener that needs to be notified of the
     * contactmanager events
     */
    public void setListener(ContactManagerListener listener) {
        this.listener = listener;
    }
    
    
    private int getPosition(Contact c) {
        
        int start = 0;
        int end =  getContactsVector().size();
      //  if (end < 0) end = 0;
        Contact middle;
       
        while (start!=end) {
             middle = (Contact) contactsVector.elementAt( (start  + end)/2 );
            if (comparator.compareVisibleName(c.getVisibleName(),middle.getVisibleName()) <= 0) {
                end = (start + end) /2;
            } else {
                if (start == ( (start + end) / 2)) {
                 start ++;
                } else {
                    start = (start + end) /2;
                }
             }
        }
        return start;
    }
    
    /**
     * 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) {
                Log.error("Error Comparing Visible names" + ex);
            }
            return result;
        }
        
        /**
         * BlackBerry compare method
         */
        public int compare( Object o1, Object o2 ) {

            compare((byte[]) o1,(byte[]) o2);
            return 0;
        }
        
        /**
         * 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
         */
        public int compareVisibleName(String visibleName1, String visibleName2) {
            visibleName1 = visibleName1.toLowerCase();
            visibleName2 = visibleName2.toLowerCase();
            
            //Use static methods to be compatable with preprocessing in BB builds.
            if(visibleName1.compareTo(visibleName2)<0) {
                return RecordComparator.PRECEDES;
            } else if(visibleName1.equals(visibleName2)) {
                return RecordComparator.EQUIVALENT;
            } else {
                return RecordComparator.FOLLOWS;
            }
        }
    }
    
    
}

⌨️ 快捷键说明

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