Android Open Source - CloudyPhone Contacts Manager






From Project

Back to project page CloudyPhone.

License

The source code is released under:

MIT License

If you think the Android project CloudyPhone listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.cloudyphone.android.model.contact;
/* w w w  . j  av a2 s .c  o  m*/
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collection;

import android.content.ContentResolver;
import android.content.ContentUris;
import android.database.Cursor;
import android.net.Uri;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.Phone;

public class ContactsManager {

  /**
   * Gets all Contacts which has phone numbers
   * 
   * @param cr
   * @returns contacts if there is any contact.
   */
  public static ParseContacts getAllContacts(ContentResolver cr) {
    Collection<JSONContact> contacts = new ArrayList<JSONContact>();

    // Contacts cursor
    Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
        null, null, null);

    Cursor phonesCursor;
    String contactId, name;
    String selection;
    String equal = " = ";
    Collection<String> numbers;

    while (cursor.moveToNext()) {
      // get contact id
      contactId = cursor.getString(cursor
          .getColumnIndex(ContactsContract.Contacts._ID));

      // Construct the selection for phone cursor
      selection = new StringBuilder(Phone.CONTACT_ID).append(equal)
          .append(contactId).toString();

      // the cursor to get phone numbers
      phonesCursor = cr.query(Phone.CONTENT_URI, null, selection, null,
          null);

      // get phone numbers
      numbers = new ArrayList<String>();
      while (phonesCursor.moveToNext()) {
        numbers.add(phonesCursor.getString(phonesCursor
            .getColumnIndex(Phone.NUMBER)));
      }
      phonesCursor.close();

      // Only add contacts which have at least phone number
      if (numbers.size() > 0) {
        name = cursor
            .getString(cursor
                .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

        contacts.add(new JSONContact(Long.parseLong(contactId), name,
            numbers));
      }
    }

    cursor.close();

    return new ParseContacts(contacts);
  }

  public static Collection<ParseContactImg> getAllContactImages(
      ContentResolver cr, Collection<JSONContact> contacts) {

    Collection<ParseContactImg> images = new ArrayList<ParseContactImg>();
    for (JSONContact c : contacts) {
      try {
        images.add(getContactImg(cr, c.getId()));
        // if exception is thrown, ignore this image
      } catch (FileNotFoundException e) {
      } catch (IOException e) {
      }
    }

    return images;
  }

  private static ParseContactImg getContactImg(ContentResolver cr, long id)
      throws FileNotFoundException, IOException {

    Uri auri = getPhotoUri(id);
    InputStream is = cr.openInputStream(auri);
    int fileLength = is.available();

    if (fileLength > 0) {
      byte[] data = ParseContactImg.convertFileToByte(is, fileLength);
      return new ParseContactImg(id, data);
    }

    return null;
  }

  private static Uri getPhotoUri(long contactId) {
    Uri contactUri = ContentUris.withAppendedId(
        android.provider.ContactsContract.Contacts.CONTENT_URI,
        contactId);
    Uri photoUri = Uri
        .withAppendedPath(
            contactUri,
            android.provider.ContactsContract.Contacts.Photo.CONTENT_DIRECTORY);
    return photoUri;
  }
}




Java Source Code List

com.cloudyphone.android.Constants.java
com.cloudyphone.android.controller.activities.CloudyPhoneActivity.java
com.cloudyphone.android.controller.activities.LoginActivity.java
com.cloudyphone.android.controller.activities.ResetPasswordActivity.java
com.cloudyphone.android.controller.activities.SignupActivity.java
com.cloudyphone.android.controller.activities.WelcomeActivity.java
com.cloudyphone.android.controller.callbacks.MyLoginCallback.java
com.cloudyphone.android.controller.callbacks.MyResetPasswordCallback.java
com.cloudyphone.android.controller.callbacks.MySignUpCallback.java
com.cloudyphone.android.controller.commands.Command.java
com.cloudyphone.android.controller.commands.CommandsAsyncTask.java
com.cloudyphone.android.controller.commands.SyncContactsCommand.java
com.cloudyphone.android.controller.commands.SyncContactsImagesCommand.java
com.cloudyphone.android.controller.commands.SyncPhoneInforCommand.java
com.cloudyphone.android.controller.commands.SyncSmsThreadsCommand.java
com.cloudyphone.android.controller.listeners.LoginClickListener.java
com.cloudyphone.android.controller.listeners.ResetPasswordClickListener.java
com.cloudyphone.android.controller.listeners.SignupClickListener.java
com.cloudyphone.android.controller.network.ServerConnector.java
com.cloudyphone.android.controller.push.PushManager.java
com.cloudyphone.android.controller.receivers.CopyClipboardReceiver.java
com.cloudyphone.android.controller.receivers.DenyCallReceiver.java
com.cloudyphone.android.controller.receivers.MakeCallReceiver.java
com.cloudyphone.android.controller.receivers.OpenWebReceiver.java
com.cloudyphone.android.controller.receivers.PushReceiver.java
com.cloudyphone.android.controller.receivers.SendSmsReceiver.java
com.cloudyphone.android.controller.receivers.SyncReceiver.java
com.cloudyphone.android.controller.sync.SyncThread.java
com.cloudyphone.android.controller.sync.UpdateThread.java
com.cloudyphone.android.model.InputValidator.java
com.cloudyphone.android.model.MyParseObject.java
com.cloudyphone.android.model.contact.ContactsManager.java
com.cloudyphone.android.model.contact.JSONContact.java
com.cloudyphone.android.model.contact.ParseContactImg.java
com.cloudyphone.android.model.contact.ParseContacts.java
com.cloudyphone.android.model.infor.ParsePhoneInfor.java
com.cloudyphone.android.model.sms.JSONSmsMessage.java
com.cloudyphone.android.model.sms.JSONSmsThread.java
com.cloudyphone.android.model.sms.ParseSmsThreads.java
com.cloudyphone.android.model.sms.SmsManager.java
com.cloudyphone.android.utils.Logger.java