2014-02-28 12:46:01 -05:00
|
|
|
package eu.siacs.conversations.utils;
|
2014-02-09 21:34:00 -05:00
|
|
|
|
|
|
|
import java.util.Hashtable;
|
|
|
|
|
2014-02-10 09:24:34 -05:00
|
|
|
import android.app.Activity;
|
2014-02-09 21:34:00 -05:00
|
|
|
import android.content.Context;
|
|
|
|
import android.content.CursorLoader;
|
|
|
|
import android.content.Loader;
|
|
|
|
import android.content.Loader.OnLoadCompleteListener;
|
|
|
|
import android.database.Cursor;
|
2014-02-10 09:24:34 -05:00
|
|
|
import android.net.Uri;
|
2014-02-09 21:34:00 -05:00
|
|
|
import android.os.Bundle;
|
2014-02-18 19:35:23 -05:00
|
|
|
import android.os.Looper;
|
2014-02-09 21:34:00 -05:00
|
|
|
import android.provider.ContactsContract;
|
2014-02-10 09:24:34 -05:00
|
|
|
import android.provider.ContactsContract.Profile;
|
2014-04-05 15:06:10 -04:00
|
|
|
import android.provider.MediaStore;
|
2014-02-09 21:34:00 -05:00
|
|
|
|
|
|
|
public class PhoneHelper {
|
2014-04-05 15:06:10 -04:00
|
|
|
|
|
|
|
public static void loadPhoneContacts(Context context,
|
|
|
|
final OnPhoneContactsLoadedListener listener) {
|
|
|
|
if (Looper.myLooper() == null) {
|
2014-02-18 19:35:23 -05:00
|
|
|
Looper.prepare();
|
|
|
|
}
|
|
|
|
final Looper mLooper = Looper.myLooper();
|
2014-02-09 21:34:00 -05:00
|
|
|
final Hashtable<String, Bundle> phoneContacts = new Hashtable<String, Bundle>();
|
2014-04-05 15:06:10 -04:00
|
|
|
|
|
|
|
final String[] PROJECTION = new String[] { ContactsContract.Data._ID,
|
2014-02-09 21:34:00 -05:00
|
|
|
ContactsContract.Data.DISPLAY_NAME,
|
|
|
|
ContactsContract.Data.PHOTO_THUMBNAIL_URI,
|
|
|
|
ContactsContract.Data.LOOKUP_KEY,
|
|
|
|
ContactsContract.CommonDataKinds.Im.DATA };
|
|
|
|
|
|
|
|
final String SELECTION = "(" + ContactsContract.Data.MIMETYPE + "=\""
|
|
|
|
+ ContactsContract.CommonDataKinds.Im.CONTENT_ITEM_TYPE
|
|
|
|
+ "\") AND (" + ContactsContract.CommonDataKinds.Im.PROTOCOL
|
|
|
|
+ "=\"" + ContactsContract.CommonDataKinds.Im.PROTOCOL_JABBER
|
|
|
|
+ "\")";
|
2014-04-05 15:06:10 -04:00
|
|
|
|
2014-02-09 21:34:00 -05:00
|
|
|
CursorLoader mCursorLoader = new CursorLoader(context,
|
|
|
|
ContactsContract.Data.CONTENT_URI, PROJECTION, SELECTION, null,
|
|
|
|
null);
|
|
|
|
mCursorLoader.registerListener(0, new OnLoadCompleteListener<Cursor>() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onLoadComplete(Loader<Cursor> arg0, Cursor cursor) {
|
|
|
|
while (cursor.moveToNext()) {
|
|
|
|
Bundle contact = new Bundle();
|
|
|
|
contact.putInt("phoneid", cursor.getInt(cursor
|
|
|
|
.getColumnIndex(ContactsContract.Data._ID)));
|
|
|
|
contact.putString(
|
|
|
|
"displayname",
|
|
|
|
cursor.getString(cursor
|
|
|
|
.getColumnIndex(ContactsContract.Data.DISPLAY_NAME)));
|
|
|
|
contact.putString(
|
|
|
|
"photouri",
|
|
|
|
cursor.getString(cursor
|
|
|
|
.getColumnIndex(ContactsContract.Data.PHOTO_THUMBNAIL_URI)));
|
2014-04-05 15:06:10 -04:00
|
|
|
contact.putString("lookup", cursor.getString(cursor
|
2014-02-09 21:34:00 -05:00
|
|
|
.getColumnIndex(ContactsContract.Data.LOOKUP_KEY)));
|
|
|
|
phoneContacts.put(
|
|
|
|
cursor.getString(cursor
|
|
|
|
.getColumnIndex(ContactsContract.CommonDataKinds.Im.DATA)),
|
|
|
|
contact);
|
|
|
|
}
|
2014-04-05 15:06:10 -04:00
|
|
|
if (listener != null) {
|
2014-02-09 21:34:00 -05:00
|
|
|
listener.onPhoneContactsLoaded(phoneContacts);
|
|
|
|
}
|
2014-02-18 19:35:23 -05:00
|
|
|
mLooper.quit();
|
2014-02-09 21:34:00 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
mCursorLoader.startLoading();
|
|
|
|
}
|
2014-02-10 09:24:34 -05:00
|
|
|
|
|
|
|
public static Uri getSefliUri(Activity activity) {
|
|
|
|
String[] mProjection = new String[] { Profile._ID,
|
|
|
|
Profile.PHOTO_THUMBNAIL_URI };
|
|
|
|
Cursor mProfileCursor = activity.getContentResolver().query(
|
|
|
|
Profile.CONTENT_URI, mProjection, null, null, null);
|
|
|
|
|
2014-04-05 15:06:10 -04:00
|
|
|
if (mProfileCursor.getCount() == 0) {
|
2014-02-10 09:24:34 -05:00
|
|
|
return null;
|
|
|
|
} else {
|
|
|
|
mProfileCursor.moveToFirst();
|
2014-03-05 15:04:11 -05:00
|
|
|
String uri = mProfileCursor.getString(1);
|
2014-04-05 15:06:10 -04:00
|
|
|
if (uri == null) {
|
2014-03-05 15:04:11 -05:00
|
|
|
return null;
|
|
|
|
} else {
|
|
|
|
return Uri.parse(uri);
|
|
|
|
}
|
2014-02-10 09:24:34 -05:00
|
|
|
}
|
|
|
|
}
|
2014-02-09 21:34:00 -05:00
|
|
|
}
|