ContactProviderSdk5.java :  » Contact » fastcall-android » com » sfeir » fastcall » access » Android Open Source

Android Open Source » Contact » fastcall android 
fastcall android » com » sfeir » fastcall » access » ContactProviderSdk5.java
package com.sfeir.fastcall.access;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;
import java.util.TreeMap;

import android.database.Cursor;
import android.provider.ContactsContract.Contacts;
import android.provider.ContactsContract.Data;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.provider.ContactsContract.CommonDataKinds.StructuredName;

import com.sfeir.fastcall.model.PhoneEntry;

/**
 * Cette classe permet de recuperer les donnees relative au contact, depuis le repertoire du smartphone.
 * 
 * Created by :
 * 
 *     @author SFEIR
 */
public class ContactProviderSdk5 extends ContactProviderCommons {
  
  private final static String ESPACE_REGEX = "\\s";
  
  
  @Override
  protected void loadFromContacts(TreeMap<String, TreeMap<String, HashSet<Integer>>> indexContactLetter) {
    final Cursor cur = cr.query(Contacts.CONTENT_URI, 
        new String[] { Contacts._ID, Contacts.DISPLAY_NAME },
        Contacts.HAS_PHONE_NUMBER + " = 1", 
        null,
        null);
    if (cur.getCount() > 0) {
      String displayName, token;
      String[] displayNameTokens;

      while (cur.moveToNext()) {
        displayName = cur.getString(cur.getColumnIndex(Contacts.DISPLAY_NAME));

        displayNameTokens = displayName.split(ESPACE_REGEX);
        for (int i = 0; i < displayNameTokens.length; i++) {
          token = displayNameTokens[i].toUpperCase();
          while(token.length()<2){
            token = token.concat(" ");
          }
          TreeMap<String, HashSet<Integer>> firstLevel = indexContactLetter.get(token.substring(0, 1));
          if(firstLevel == null){
            firstLevel = new TreeMap<String,HashSet<Integer>>();
            indexContactLetter.put(token.substring(0, 1), firstLevel);
          } 
          HashSet<Integer> contactIds = firstLevel.get(token.substring(0, 2));
          if(contactIds == null){
            contactIds = new HashSet<Integer>();
            firstLevel.put(token.substring(0, 2), contactIds);
          }
          contactIds.add(cur.getInt(cur.getColumnIndex(Contacts._ID)));

        }
      }
    }
    cur.close();
  }    

  public ArrayList<PhoneEntry> filtreContacts(String filtre) {
    final ArrayList<PhoneEntry> contactEntries = new ArrayList<PhoneEntry>();
    
    final Set<Integer> contactIds = filtreContactIds(filtre);
    final StringBuilder where = new StringBuilder();
    where.append(StructuredName.CONTACT_ID);
    where.append(" IN ( ");
    int i=0;
    for (int contactId : contactIds) {
      where.append(contactId);
      if (i < contactIds.size() - 1)
        where.append(" , ");
      i++;
    }    
    where.append(" ) AND ").append( Data.MIMETYPE).append(" IN ( ?, ? )");
    where.trimToSize();

    final Cursor cur = this.cr.query(Data.CONTENT_URI,
                  new String[] {StructuredName.CONTACT_ID,Data.MIMETYPE, Phone.NUMBER, Phone.TYPE, Contacts.DISPLAY_NAME },
                  where.toString(), 
                  new String[] {StructuredName.CONTENT_ITEM_TYPE, Phone.CONTENT_ITEM_TYPE },
                  Data.CONTACT_ID);    
    int currentContactId = -1;
    PhoneEntry phoneEntry = null;
    ArrayList<PhoneEntry>  phoneEntryTmp = null;
    String currentDisplayName = null;

    while (cur.moveToNext()) {
      final int contactId = cur.getInt(cur.getColumnIndex(StructuredName.CONTACT_ID));
      if (contactId != currentContactId) {
        // Ce n'est plus le mme contacts, on lui assigne le nom trouv
        if(phoneEntryTmp!=null){
          for(int j=0;j<phoneEntryTmp.size(); j++){
            phoneEntryTmp.get(j).setDisplayName(currentDisplayName);
          }
          contactEntries.addAll(phoneEntryTmp);
        }
        // On va traiter une nouvel ensemble de contact
        phoneEntryTmp  = new ArrayList<PhoneEntry>();
        currentContactId = contactId;
      }
      final String mimeType = cur.getString(cur.getColumnIndex(Data.MIMETYPE));
      if(mimeType.equals(StructuredName.CONTENT_ITEM_TYPE)){
        currentDisplayName = cur.getString(cur.getColumnIndex(Contacts.DISPLAY_NAME));
      } else if(mimeType.equals(Phone.CONTENT_ITEM_TYPE)){
        phoneEntry = new PhoneEntry();
        phoneEntryTmp.add(phoneEntry);
        phoneEntry.id = contactId;
        phoneEntry.phoneNumber = cur.getString(cur.getColumnIndex(Phone.NUMBER));
        phoneEntry.isMobile =  cur.getInt(cur.getColumnIndex(Phone.TYPE)) == Phone.TYPE_MOBILE;
      }
    }
    //   
    if(phoneEntryTmp!=null){
      for(int j=0;j<phoneEntryTmp.size(); j++){
        phoneEntryTmp.get(j).setDisplayName(currentDisplayName);
      }
      contactEntries.addAll(phoneEntryTmp);
    }    

    
    cur.close();
    
    return contactEntries;
  }


}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.