Improved contacts performances
This commit is contained in:
parent
1a29c5ac72
commit
35bbf884ab
3 changed files with 71 additions and 30 deletions
|
@ -20,6 +20,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|||
import java.io.Serializable;
|
||||
import java.util.List;
|
||||
|
||||
import android.graphics.Bitmap;
|
||||
import android.net.Uri;
|
||||
|
||||
/**
|
||||
|
@ -30,21 +31,31 @@ public class Contact implements Serializable {
|
|||
|
||||
private String id;
|
||||
private String name;
|
||||
private transient Uri photo;
|
||||
private transient Uri photoUri;
|
||||
private transient Bitmap photo;
|
||||
private List<String> numerosOrAddresses;
|
||||
|
||||
public Contact(String id, String name) {
|
||||
super();
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.photo = null;
|
||||
this.photoUri = null;
|
||||
}
|
||||
|
||||
public Contact(String id, String name, Uri photo) {
|
||||
super();
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.photo = photo;
|
||||
this.photoUri = photo;
|
||||
this.photo = null;
|
||||
}
|
||||
|
||||
public Contact(String id, String name, Uri photo, Bitmap picture) {
|
||||
super();
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.photoUri = photo;
|
||||
this.photo = picture;
|
||||
}
|
||||
|
||||
public String getID() {
|
||||
|
@ -55,7 +66,11 @@ public class Contact implements Serializable {
|
|||
return name;
|
||||
}
|
||||
|
||||
public Uri getPhoto() {
|
||||
public Uri getPhotoUri() {
|
||||
return photoUri;
|
||||
}
|
||||
|
||||
public Bitmap getPhoto() {
|
||||
return photo;
|
||||
}
|
||||
|
||||
|
|
|
@ -41,7 +41,7 @@ public class ContactFragment extends Fragment {
|
|||
|
||||
ImageView contactPicture = (ImageView) view.findViewById(R.id.contactPicture);
|
||||
if (contact.getPhoto() != null) {
|
||||
LinphoneUtils.setImagePictureFromUri(view.getContext(), contactPicture, contact.getPhoto(), R.drawable.unknown_small);
|
||||
LinphoneUtils.setImagePictureFromUri(view.getContext(), contactPicture, contact.getPhotoUri(), R.drawable.unknown_small);
|
||||
}
|
||||
|
||||
chatListener = getChatListener();
|
||||
|
@ -81,7 +81,7 @@ public class ContactFragment extends Fragment {
|
|||
return new OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
LinphoneActivity.instance().setAddressAndGoToDialer(v.getTag().toString(), contact.getName(), contact.getPhoto());
|
||||
LinphoneActivity.instance().setAddressAndGoToDialer(v.getTag().toString(), contact.getName(), contact.getPhotoUri());
|
||||
}
|
||||
};
|
||||
}
|
||||
|
|
|
@ -18,6 +18,8 @@ along with this program; if not, write to the Free Software
|
|||
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||
*/
|
||||
import java.io.InputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import android.content.ContentUris;
|
||||
import android.content.Intent;
|
||||
|
@ -53,6 +55,7 @@ public class ContactsFragment extends Fragment implements OnClickListener, OnIte
|
|||
private boolean onlyDisplayLinphoneCalls;
|
||||
private int lastKnownPosition;
|
||||
private Cursor cursor;
|
||||
private List<Contact> contacts;
|
||||
|
||||
@Override
|
||||
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
||||
|
@ -114,10 +117,45 @@ public class ContactsFragment extends Fragment implements OnClickListener, OnIte
|
|||
contactsList.setAdapter(new ContactsListAdapter());
|
||||
contactsList.setFastScrollEnabled(true);
|
||||
}
|
||||
contacts = new ArrayList<Contact>();
|
||||
new Thread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
for (int i = 0; i < cursor.getCount(); i++) {
|
||||
Contact contact = getContact(i);
|
||||
contacts.add(contact);
|
||||
}
|
||||
}
|
||||
}).start();
|
||||
|
||||
contactsList.setSelectionFromTop(lastKnownPosition, 0);
|
||||
}
|
||||
|
||||
private Contact getContact(int position) {
|
||||
cursor.moveToFirst();
|
||||
boolean success = cursor.move(position);
|
||||
if (!success)
|
||||
return null;
|
||||
|
||||
String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
|
||||
String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
|
||||
Uri person = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, Long.parseLong(id));
|
||||
Uri photo = Uri.withAppendedPath(person, ContactsContract.Contacts.Photo.CONTENT_DIRECTORY);
|
||||
|
||||
InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(getActivity().getContentResolver(), person);
|
||||
Contact contact;
|
||||
if (input == null) {
|
||||
contact = new Contact(id, name);
|
||||
}
|
||||
else {
|
||||
contact = new Contact(id, name, photo, BitmapFactory.decodeStream(input));
|
||||
}
|
||||
|
||||
contact.setNumerosOrAddresses(ContactHelper.extractContactNumbersAndAddresses(contact.getID(), getActivity().getContentResolver()));
|
||||
|
||||
return contact;
|
||||
}
|
||||
|
||||
class ContactsListAdapter extends BaseAdapter implements SectionIndexer {
|
||||
private AlphabetIndexer indexer;
|
||||
private int margin;
|
||||
|
@ -134,28 +172,11 @@ public class ContactsFragment extends Fragment implements OnClickListener, OnIte
|
|||
}
|
||||
|
||||
public Object getItem(int position) {
|
||||
cursor.moveToFirst();
|
||||
boolean success = cursor.move(position);
|
||||
if (!success)
|
||||
return null;
|
||||
|
||||
String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
|
||||
String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
|
||||
Uri person = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, Long.parseLong(id));
|
||||
Uri photo = Uri.withAppendedPath(person, ContactsContract.Contacts.Photo.CONTENT_DIRECTORY);
|
||||
|
||||
InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(getActivity().getContentResolver(), person);
|
||||
Contact contact;
|
||||
if (input == null) {
|
||||
contact = new Contact(id, name);
|
||||
if (position >= contacts.size()) {
|
||||
return getContact(position);
|
||||
} else {
|
||||
return contacts.get(position);
|
||||
}
|
||||
else {
|
||||
contact = new Contact(id, name, photo);
|
||||
}
|
||||
|
||||
contact.setNumerosOrAddresses(ContactHelper.extractContactNumbersAndAddresses(contact.getID(), getActivity().getContentResolver()));
|
||||
|
||||
return contact;
|
||||
}
|
||||
|
||||
public long getItemId(int position) {
|
||||
|
@ -171,7 +192,10 @@ public class ContactsFragment extends Fragment implements OnClickListener, OnIte
|
|||
view = mInflater.inflate(R.layout.contact_cell, parent, false);
|
||||
}
|
||||
|
||||
Contact contact = (Contact) getItem(position);
|
||||
Contact contact = null;
|
||||
do {
|
||||
contact = (Contact) getItem(position);
|
||||
} while (contact == null);
|
||||
|
||||
TextView name = (TextView) view.findViewById(R.id.name);
|
||||
name.setText(contact.getName());
|
||||
|
@ -189,7 +213,9 @@ public class ContactsFragment extends Fragment implements OnClickListener, OnIte
|
|||
|
||||
ImageView icon = (ImageView) view.findViewById(R.id.icon);
|
||||
if (contact.getPhoto() != null) {
|
||||
icon.setImageURI(contact.getPhoto());
|
||||
icon.setImageBitmap(contact.getPhoto());
|
||||
} else if (contact.getPhotoUri() != null) {
|
||||
icon.setImageURI(contact.getPhotoUri());
|
||||
} else {
|
||||
icon.setImageBitmap(bitmapUnknown);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue