2014-11-16 23:14:06 -08:00
|
|
|
/*
|
|
|
|
* Copyright 2014 Albert Vaca Cintora <albertvaka@gmail.com>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License as
|
|
|
|
* published by the Free Software Foundation; either version 2 of
|
|
|
|
* the License or (at your option) version 3 or any later version
|
|
|
|
* accepted by the membership of KDE e.V. (or its successor approved
|
|
|
|
* by the membership of KDE e.V.), which shall act as a proxy
|
|
|
|
* defined in Section 14 of version 3 of the license.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2013-09-05 01:37:59 +02:00
|
|
|
package org.kde.kdeconnect.Helpers;
|
2013-08-01 02:30:58 +02:00
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.database.Cursor;
|
|
|
|
import android.net.Uri;
|
2016-03-06 21:39:52 +01:00
|
|
|
import android.provider.ContactsContract;
|
2013-08-01 02:30:58 +02:00
|
|
|
import android.provider.ContactsContract.PhoneLookup;
|
2016-03-06 21:50:58 +01:00
|
|
|
import android.util.Base64;
|
|
|
|
import android.util.Base64OutputStream;
|
2016-03-06 21:39:52 +01:00
|
|
|
import android.util.Log;
|
|
|
|
|
2016-03-06 21:50:58 +01:00
|
|
|
import java.io.ByteArrayOutputStream;
|
|
|
|
import java.io.InputStream;
|
2016-03-06 21:39:52 +01:00
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
2013-08-01 02:30:58 +02:00
|
|
|
|
|
|
|
public class ContactsHelper {
|
|
|
|
|
2016-03-06 21:39:52 +01:00
|
|
|
public static Map<String, String> phoneNumberLookup(Context context, String number) {
|
2013-08-01 02:30:58 +02:00
|
|
|
|
2013-09-05 10:48:42 +02:00
|
|
|
//Log.e("PhoneNumberLookup", number);
|
2013-08-01 02:30:58 +02:00
|
|
|
|
2016-03-06 21:39:52 +01:00
|
|
|
Map<String, String> contactInfo = new HashMap<String, String>();
|
|
|
|
|
2013-08-01 02:30:58 +02:00
|
|
|
Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));
|
2013-08-08 18:04:43 +02:00
|
|
|
Cursor cursor = null;
|
2013-08-01 02:30:58 +02:00
|
|
|
try {
|
|
|
|
cursor = context.getContentResolver().query(
|
|
|
|
uri,
|
|
|
|
new String[] {
|
2016-03-06 21:39:52 +01:00
|
|
|
PhoneLookup.DISPLAY_NAME,
|
|
|
|
ContactsContract.PhoneLookup.PHOTO_URI
|
2013-08-01 02:30:58 +02:00
|
|
|
/*, PhoneLookup.TYPE
|
|
|
|
, PhoneLookup.LABEL
|
|
|
|
, PhoneLookup.ID */
|
|
|
|
},
|
|
|
|
null, null, null);
|
|
|
|
} catch (IllegalArgumentException e) {
|
2016-03-06 21:39:52 +01:00
|
|
|
return contactInfo;
|
2013-08-01 02:30:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Take the first match only
|
|
|
|
if (cursor != null && cursor.moveToFirst()) {
|
2014-03-29 01:47:15 +01:00
|
|
|
int nameIndex = cursor.getColumnIndex(PhoneLookup.DISPLAY_NAME);
|
2013-08-01 02:30:58 +02:00
|
|
|
if (nameIndex != -1) {
|
2016-03-06 21:39:52 +01:00
|
|
|
contactInfo.put("name", cursor.getString(nameIndex));
|
2013-08-01 02:30:58 +02:00
|
|
|
}
|
|
|
|
|
2016-03-06 21:39:52 +01:00
|
|
|
nameIndex = cursor.getColumnIndex(PhoneLookup.PHOTO_URI);
|
|
|
|
if (nameIndex != -1) {
|
|
|
|
contactInfo.put("photoID", cursor.getString(nameIndex));
|
|
|
|
}
|
2013-10-05 17:23:27 +02:00
|
|
|
|
2016-03-06 21:39:52 +01:00
|
|
|
if (!contactInfo.isEmpty()) {
|
|
|
|
cursor.close();
|
|
|
|
return contactInfo;
|
|
|
|
}
|
|
|
|
}
|
2013-08-01 02:30:58 +02:00
|
|
|
|
2016-03-06 21:39:52 +01:00
|
|
|
return contactInfo;
|
2013-08-01 02:30:58 +02:00
|
|
|
}
|
2016-03-06 21:50:58 +01:00
|
|
|
|
|
|
|
public static String photoId64Encoded(Context context, String photoId) {
|
2016-06-03 14:51:31 +02:00
|
|
|
if (photoId == null) {
|
|
|
|
return new String();
|
|
|
|
}
|
2016-03-06 21:50:58 +01:00
|
|
|
Uri photoUri = Uri.parse(photoId);
|
|
|
|
Uri displayPhotoUri = Uri.withAppendedPath(photoUri, ContactsContract.Contacts.Photo.DISPLAY_PHOTO);
|
|
|
|
|
|
|
|
byte[] buffer = null;
|
|
|
|
Base64OutputStream out = null;
|
|
|
|
ByteArrayOutputStream encodedPhoto = null;
|
|
|
|
try {
|
|
|
|
encodedPhoto = new ByteArrayOutputStream();
|
|
|
|
out = new Base64OutputStream(encodedPhoto, Base64.DEFAULT);
|
|
|
|
InputStream fd2 = context.getContentResolver().openInputStream(photoUri);
|
|
|
|
buffer = new byte[1024];
|
|
|
|
int len;
|
|
|
|
while ((len = fd2.read(buffer)) != -1) {
|
|
|
|
out.write(buffer, 0, len);
|
|
|
|
}
|
|
|
|
return encodedPhoto.toString();
|
|
|
|
} catch (Exception ex) {
|
|
|
|
Log.e("ContactsHelper", ex.toString());
|
|
|
|
return new String();
|
|
|
|
}
|
|
|
|
}
|
2014-11-16 23:14:06 -08:00
|
|
|
}
|
2016-03-06 21:39:52 +01:00
|
|
|
|