2
0
mirror of https://github.com/KDE/kdeconnect-android synced 2025-08-31 06:05:12 +00:00

Adapt phoneNumberLookup to return more information

It returns now photoID plus the name and it can be easily expanded in
the future if required.
This commit is contained in:
Àlex Fiestas
2016-03-06 21:39:52 +01:00
parent 9f2999de23
commit 708bcf9928
2 changed files with 28 additions and 12 deletions

View File

@@ -23,44 +23,57 @@ package org.kde.kdeconnect.Helpers;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.ContactsContract;
import android.provider.ContactsContract.PhoneLookup;
import android.util.Log;
import java.util.HashMap;
import java.util.Map;
public class ContactsHelper {
public static String phoneNumberLookup(Context context, String number) {
public static Map<String, String> phoneNumberLookup(Context context, String number) {
//Log.e("PhoneNumberLookup", number);
Map<String, String> contactInfo = new HashMap<String, String>();
Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));
Cursor cursor = null;
try {
cursor = context.getContentResolver().query(
uri,
new String[] {
PhoneLookup.DISPLAY_NAME
PhoneLookup.DISPLAY_NAME,
ContactsContract.PhoneLookup.PHOTO_URI
/*, PhoneLookup.TYPE
, PhoneLookup.LABEL
, PhoneLookup.ID */
},
null, null, null);
} catch (IllegalArgumentException e) {
return number;
return contactInfo;
}
// Take the first match only
if (cursor != null && cursor.moveToFirst()) {
int nameIndex = cursor.getColumnIndex(PhoneLookup.DISPLAY_NAME);
if (nameIndex != -1) {
String name = cursor.getString(nameIndex);
//Log.e("PhoneNumberLookup", "success: " + name);
contactInfo.put("name", cursor.getString(nameIndex));
}
nameIndex = cursor.getColumnIndex(PhoneLookup.PHOTO_URI);
if (nameIndex != -1) {
contactInfo.put("photoID", cursor.getString(nameIndex));
}
if (!contactInfo.isEmpty()) {
cursor.close();
return name;
return contactInfo;
}
}
if (cursor != null) cursor.close();
return number;
return contactInfo;
}
}

View File

@@ -35,6 +35,7 @@ import org.kde.kdeconnect.NetworkPackage;
import org.kde.kdeconnect.Plugins.Plugin;
import org.kde.kdeconnect_tp.R;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;
@@ -97,10 +98,12 @@ public class TelephonyPlugin extends Plugin {
//Log.e("TelephonyPlugin", "callBroadcastReceived");
Map<String, String> contactInfo = ContactsHelper.phoneNumberLookup(context, phoneNumber);
NetworkPackage np = new NetworkPackage(NetworkPackage.PACKAGE_TYPE_TELEPHONY);
if (phoneNumber != null) {
np.set("phoneNumber", phoneNumber);
np.set("contactName", ContactsHelper.phoneNumberLookup(context, phoneNumber));
np.set("contactName", contactInfo.get("name"));
}
}
switch (state) {
@@ -175,7 +178,7 @@ public class TelephonyPlugin extends Plugin {
String phoneNumber = message.getOriginatingAddress();
if (phoneNumber != null) {
np.set("phoneNumber", phoneNumber);
np.set("contactName", ContactsHelper.phoneNumberLookup(context, phoneNumber));
np.set("contactName", ContactsHelper.phoneNumberLookup(context, phoneNumber).get("name"));
}
device.sendPackage(np);