2014-10-22 16:38:44 +00:00
|
|
|
package eu.siacs.conversations.utils;
|
|
|
|
|
2015-12-06 23:33:50 +00:00
|
|
|
import android.Manifest;
|
2014-10-22 16:38:44 +00:00
|
|
|
import android.content.Context;
|
|
|
|
import android.content.CursorLoader;
|
|
|
|
import android.content.Loader;
|
|
|
|
import android.content.Loader.OnLoadCompleteListener;
|
2015-02-16 09:06:09 +00:00
|
|
|
import android.content.pm.PackageManager;
|
2014-10-22 16:38:44 +00:00
|
|
|
import android.database.Cursor;
|
|
|
|
import android.net.Uri;
|
2015-12-06 23:33:50 +00:00
|
|
|
import android.os.Build;
|
2014-10-22 16:38:44 +00:00
|
|
|
import android.os.Bundle;
|
|
|
|
import android.provider.ContactsContract;
|
|
|
|
import android.provider.ContactsContract.Profile;
|
|
|
|
|
2015-07-20 12:26:29 +00:00
|
|
|
import java.util.List;
|
|
|
|
import java.util.concurrent.RejectedExecutionException;
|
|
|
|
|
2014-10-22 16:38:44 +00:00
|
|
|
public class PhoneHelper {
|
|
|
|
|
2014-12-13 21:17:27 +00:00
|
|
|
public static void loadPhoneContacts(Context context,final List<Bundle> phoneContacts, final OnPhoneContactsLoadedListener listener) {
|
2015-12-06 23:33:50 +00:00
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
|
|
|
|
&& context.checkSelfPermission(Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) {
|
|
|
|
listener.onPhoneContactsLoaded(phoneContacts);
|
|
|
|
return;
|
|
|
|
}
|
2014-10-22 16:38:44 +00:00
|
|
|
final String[] PROJECTION = new String[] { ContactsContract.Data._ID,
|
|
|
|
ContactsContract.Data.DISPLAY_NAME,
|
|
|
|
ContactsContract.Data.PHOTO_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
|
|
|
|
+ "\")";
|
|
|
|
|
|
|
|
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) {
|
|
|
|
if (cursor == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
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_URI)));
|
|
|
|
contact.putString("lookup", cursor.getString(cursor
|
|
|
|
.getColumnIndex(ContactsContract.Data.LOOKUP_KEY)));
|
|
|
|
|
|
|
|
contact.putString(
|
|
|
|
"jid",
|
|
|
|
cursor.getString(cursor
|
|
|
|
.getColumnIndex(ContactsContract.CommonDataKinds.Im.DATA)));
|
|
|
|
phoneContacts.add(contact);
|
|
|
|
}
|
|
|
|
if (listener != null) {
|
|
|
|
listener.onPhoneContactsLoaded(phoneContacts);
|
|
|
|
}
|
2014-12-03 22:29:35 +00:00
|
|
|
cursor.close();
|
2014-10-22 16:38:44 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
try {
|
|
|
|
mCursorLoader.startLoading();
|
|
|
|
} catch (RejectedExecutionException e) {
|
|
|
|
if (listener != null) {
|
|
|
|
listener.onPhoneContactsLoaded(phoneContacts);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Uri getSefliUri(Context context) {
|
2015-12-06 23:33:50 +00:00
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
|
|
|
|
&& context.checkSelfPermission(Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) {
|
|
|
|
return null;
|
|
|
|
}
|
2014-10-22 16:38:44 +00:00
|
|
|
String[] mProjection = new String[] { Profile._ID, Profile.PHOTO_URI };
|
|
|
|
Cursor mProfileCursor = context.getContentResolver().query(
|
|
|
|
Profile.CONTENT_URI, mProjection, null, null, null);
|
|
|
|
|
|
|
|
if (mProfileCursor == null || mProfileCursor.getCount() == 0) {
|
|
|
|
return null;
|
|
|
|
} else {
|
|
|
|
mProfileCursor.moveToFirst();
|
|
|
|
String uri = mProfileCursor.getString(1);
|
2014-12-03 22:29:35 +00:00
|
|
|
mProfileCursor.close();
|
2014-10-22 16:38:44 +00:00
|
|
|
if (uri == null) {
|
|
|
|
return null;
|
|
|
|
} else {
|
|
|
|
return Uri.parse(uri);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-02-16 09:06:09 +00:00
|
|
|
|
|
|
|
public static String getVersionName(Context context) {
|
|
|
|
final String packageName = context == null ? null : context.getPackageName();
|
|
|
|
if (packageName != null) {
|
|
|
|
try {
|
|
|
|
return context.getPackageManager().getPackageInfo(packageName, 0).versionName;
|
2015-08-19 10:24:42 +00:00
|
|
|
} catch (final PackageManager.NameNotFoundException | RuntimeException e) {
|
2015-02-16 09:06:09 +00:00
|
|
|
return "unknown";
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return "unknown";
|
|
|
|
}
|
|
|
|
}
|
2014-10-22 16:38:44 +00:00
|
|
|
}
|