/*
* Copyright (C) 2009 Show SMS open source project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.bydavy.model.showsms;
//~--- non-JDK imports --------------------------------------------------------
import android.content.ContentUris;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Parcel;
import android.os.Parcelable;
import android.provider.Contacts.People;
import android.provider.Contacts.Phones;
import com.bydavy.android.showsms.R;
import com.bydavy.util.showsms.AppLog;
/**
* Class to describe a contact
* A Contact have a name, a phone number, a photo and an id (Id in the phone contacts)
* The contact can be in the phone contact or not
*
* @version 1.0, 09/06/21
* @author Davy L.
*/
public class Contact implements Parcelable {
//~--- static fields ------------------------------------------------------
/** Key to bundle the id */
public static final String EXTRAS_PERSON_ID = "EXTRAS_PERSON_ID";
/** Key to bundle the name */
public static final String EXTRAS_PERSON_NAME = "EXTRAS_PERSON_NAME";
/** Key to bundle the phone number */
public static final String EXTRAS_PERSON_PHONENUMBER = "EXTRAS_PERSON_PHONENUMBER";
/** Key to bundle the photo */
public static final String EXTRAS_PERSON_PHOTO = "EXTRAS_PERSON_PHOTO";
/** ID value if not founded in the phone contacts */
private static final long NO_ID_FOUNDED = -1;
/** Creator of a contact or persons from a Parcel */
public static final Parcelable.Creator<Contact> CREATOR = new Parcelable.Creator<Contact>() {
public Contact createFromParcel(Parcel in) {
return new Contact(in);
}
public Contact[] newArray(int size) {
return new Contact[size];
}
};
//~--- fields -------------------------------------------------------------
/** contact photo */
private Bitmap photo = null;
/** id of contact in the phone contacts if exist, or NO_ID_FOUNDED value */
private long id = NO_ID_FOUNDED;
/** contact name */
private String name;
/** contact phone number */
private String phoneNumber;
//~--- constructors -------------------------------------------------------
/**
* Construct a <Contact> from a parcel
*
* @param in parcel containing a <Contact>
*/
public Contact(Parcel in) {
id = in.readLong();
name = in.readString();
phoneNumber = in.readString();
boolean photo_exist = (Boolean) in.readValue(boolean.class.getClassLoader());
if (photo_exist == true) {
photo = in.readParcelable(Bitmap.class.getClassLoader());
}
}
/**
* Constructs contact from her phone number
* Alll informations like photo, name and id while by exctract from the phone contacts if exist
*
* @param context context application
* @param _phoneNumber phone number of the contact (can't be null)
*/
public Contact(String _phoneNumber, Context context) {
phoneNumber = _phoneNumber;
id = Contact.findId(context, phoneNumber);
if (id == NO_ID_FOUNDED) {
name = null;
photo = null;
} else {
name = Contact.findName(context, id);
photo = Contact.findPhoto(context, id);
}
}
/**
* Constructs a contact from attributes
* All attributes must be defined
* @param _id id in the phone contacts
* @param _name name of the contact
* @param _phoneNumber phone number of the contact
* @param _photo photo of the contact
*/
public Contact(long _id, String _name, String _phoneNumber, Bitmap _photo) {
id = _id;
name = _name;
phoneNumber = _phoneNumber;
photo = _photo;
}
//~--- get methods --------------------------------------------------------
/**
* Getter : the Name if exist or phoneNumber
*
* @return Name if exist or the phone number
*/
public String getNameOrPhoneNumber() {
if (name != null) {
return name;
} else {
return phoneNumber;
}
}
//~--- methods ------------------------------------------------------------
/**
* Constructs a contact from a bundle
*
* @param bundle bundle containing a <Contact>
*
* @return <contact> or Null if no contact bundled
*/
static public Contact constructFromBundle(Bundle bundle) {
long id = bundle.getLong(EXTRAS_PERSON_ID);
// No contact object bundled
if (id == 0L) {
return null;
}
String name = bundle.getString(EXTRAS_PERSON_NAME);
String phoneNumber = bundle.getString(EXTRAS_PERSON_PHONENUMBER);
Bitmap photo = bundle.getParcelable(EXTRAS_PERSON_PHOTO);
return new Contact(id, name, phoneNumber, photo);
}
/**
* Bundle a contact object
*
* @return <Contact> bundled
*/
public Bundle toBundle() {
return toBundle(new Bundle());
}
/**
* Add <contact> bundle to an existing bundle
* /!\ One bundle object can't contains more that one <Contact>
*
* @param b existing bundle
* @return <Contact> bundled
*/
public Bundle toBundle(Bundle b) {
b.putLong(EXTRAS_PERSON_ID, id);
b.putString(EXTRAS_PERSON_NAME, name);
b.putString(EXTRAS_PERSON_PHONENUMBER, phoneNumber);
b.putParcelable(EXTRAS_PERSON_PHOTO, photo);
return b;
}
/**
* test if the Contact exist in the phone contacts
*
* @return true if the contact exist in the phone contacts, in the others case false
*/
public boolean existInPhoneContacts() {
if (id == NO_ID_FOUNDED) {
return false;
} else {
return true;
}
}
//~--- get methods --------------------------------------------------------
/**
* Getter : the contact photo retrieved from the phone contact
* /!\ Can by call even if the contact don't have a photo or isn't in the phone contacts, in these two cases a particular photo will be returned
*
* @param context
* @return the contact photo (96 x 96px)
*/
public Bitmap getPhoto(Context context) {
// Unknown contact
if (!existInPhoneContacts()) {
AppLog.v("This contact is unknow");
return BitmapFactory.decodeResource(context.getResources(), R.drawable.no_photo);
}
// Knowned contact
else {
if (!hasPhoto()) {
AppLog.v("This contact is know but don't have photo");
return BitmapFactory.decodeResource(context.getResources(), R.drawable.no_photo);
}
}
AppLog.v("This contact have a photo");
return photo;
}
/**
* Get the contact id (id) in the phone contacts
* If the contact doesn't exist in the phone contacts return Contact.NO_ID_FOUNDED
* @return contact id or Contact.NO_ID_FOUNDED if the contact exist or not in the phone contacts
*/
public long getId() {
return id;
}
/**
* Get the contact name retrieved from the phone contact
*
* @param context application context
* @return contact name if exist in the phone contacts or a default string (person_unknow_name) definied in string.xml
*/
public String getName(Context context) {
if (name == null) {
return context.getString(R.string.person_unknow_name);
} else {
return name;
}
}
/**
* Getter : the contact phone number
*
* @return contact phone number
*/
public String getPhoneNumber() {
return phoneNumber;
}
//~--- methods ------------------------------------------------------------
/**
* Find contact id from phoneNumber in the phone contacts
*
* @param context application context
* @param phoneNumber contact phone number
*
* @return contact id if contact founded in the phone contacts otherwise Contact.NO_ID_FOUNDED
*/
private static long findId(Context context, String phoneNumber) {
// Get the base URI
Uri myPerson = Uri.withAppendedPath(Phones.CONTENT_FILTER_URL, Uri.encode(phoneNumber));
// Column to return
String[] projection = new String[] { Phones.PERSON_ID };
Cursor cursor = context.getContentResolver().query(myPerson, projection, null, null, null);
if (cursor.moveToFirst()) {
int idColumn = cursor.getColumnIndex(Phones.PERSON_ID);
Long id = cursor.getLong(idColumn);
AppLog.v("Found id contact: " + id);
cursor.close();
return id;
}
cursor.close();
AppLog.v("Not Found id contact");
return NO_ID_FOUNDED;
}
/**
* Find contact name from contact id in the phone contacts
*
* @param context application context
* @param id contact id
*
* @return contact name if contact id founded in the phone contacts otherwise return null
*/
private static String findName(Context context, Long id) {
// Get the base URI
Uri myPerson = ContentUris.withAppendedId(People.CONTENT_URI, id);
// Column to return
String[] projection = new String[] { People.NAME };
Cursor cursor = context.getContentResolver().query(myPerson, projection, null, null, null);
if (cursor != null) {
try {
if (cursor.getCount() > 0) {
cursor.moveToFirst();
int nameColumn = cursor.getColumnIndex(People.NAME);
String name = cursor.getString(nameColumn);
AppLog.v("Found name contact: " + name);
cursor.close();
return name;
}
} finally {
cursor.close();
}
}
AppLog.v("Not Found name contact");
return null;
}
/**
* Find contact photo from contact id in the phone contacts
*
*
* @param context application context
* @param id contact id in the phone contacts
*
* @return contact photo if contact id founded in the phone contacts otherwise return null
*/
private static Bitmap findPhoto(Context context, Long id) {
Uri uri = Uri.parse("content://contacts/people/" + id);
return People.loadContactPhoto(context, uri, 0, null);
}
//~--- get methods --------------------------------------------------------
/**
* test if the contact has a photo in the phone contacts
* /!\ Not all persons in the phone contacts have a photo
* @return true if the contact has photo, otherwise return false
*/
public boolean hasPhoto() {
if (photo == null) {
return false;
}
return true;
}
//~--- methods ------------------------------------------------------------
/**
* Method description
*
*
* @return
*/
@Override
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}
/**
* Save the <Contact> in a parcel
*
* @param dest parcel'll contains the <contact>
* @param flags
*/
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeLong(id);
dest.writeString(name);
dest.writeString(phoneNumber);
if (hasPhoto()) {
dest.writeValue(true);
dest.writeParcelable(photo, Bitmap.PARCELABLE_WRITE_RETURN_VALUE);
} else {
dest.writeValue(false);
}
}
/**
* All contact informations in a String (phone number, id and name)
*
* With this Layout:
* Contact phone number : XXXX \n
* Contact id : XXXX \n
* Contact name : XXXX \n
* @return
*/
@Override
public String toString() {
return "Contact phone number : " + phoneNumber + "\n" + "Contact id : " + id + "\n" + "Contact name : " + name;
}
}
|