2014-11-16 23:14:06 -08:00
|
|
|
/*
|
|
|
|
* Copyright 2014 Albert Vaca Cintora <albertvaka@gmail.com>
|
Add contacts-reading plugin (Android side)
Summary:
Add a plugin to the KDE Connect Android application which supports reading the Android contacts databases and sending the requested data as vcards
- Android automatically has support for exporting vcards with all the fields you would expect (phone, email, photo, etc.)
- I add two custom fields, one for the modification timestamp and another for the NAME_RAW_CONTACT_ID so that the contacts can be correlated back to the Android database
This does not (yet) support writing contacts back to the phone nor does it automatically listen to the phone's contacts database to change
BUG: 367999
Test Plan:
Connect the device to the desktop and verify that vcards are created in QStandardPaths::GenericDataLocation / kpeoplevcard". On my system this is ~/.local/share/kpeoplevcard
Create a dummy contact on the device and verify it is synchronized (Currently not automatic, have to disconnect and reconnect or use dbus)
Modify the dummy contact and verify the modifications are synchronized (Currently not automatic, have to disconnect and reconnect or use dbus)
Delete the dummy contact and verify the deletion is synchronized (Currently not automatic, have to disconnect and reconnect or use dbus)
Reviewers: #kde_connect, mtijink, nicolasfella
Reviewed By: #kde_connect, nicolasfella
Subscribers: MatMaul, philipc, kdeconnect, nicolasfella, andyholmes, mtijink
Tags: #kde_connect
Maniphest Tasks: T8283
Differential Revision: https://phabricator.kde.org/D9690
2018-05-30 18:33:23 -06:00
|
|
|
* Copyright 2018 Simon Redman <simon@ergotech.com>
|
2014-11-16 23:14:06 -08:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
2016-06-21 16:44:21 +02:00
|
|
|
import android.annotation.TargetApi;
|
2013-08-01 02:30:58 +02:00
|
|
|
import android.content.Context;
|
|
|
|
import android.database.Cursor;
|
|
|
|
import android.net.Uri;
|
2016-06-21 16:44:21 +02:00
|
|
|
import android.os.Build;
|
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;
|
|
|
|
|
2019-08-31 11:11:58 +02:00
|
|
|
import androidx.annotation.NonNull;
|
2019-10-27 21:23:52 +00:00
|
|
|
import androidx.annotation.Nullable;
|
2019-08-31 11:11:58 +02:00
|
|
|
import androidx.annotation.RequiresApi;
|
|
|
|
|
2020-07-10 10:36:11 +05:30
|
|
|
import org.apache.commons.io.IOUtils;
|
|
|
|
|
2016-03-06 21:50:58 +01:00
|
|
|
import java.io.ByteArrayOutputStream;
|
Add contacts-reading plugin (Android side)
Summary:
Add a plugin to the KDE Connect Android application which supports reading the Android contacts databases and sending the requested data as vcards
- Android automatically has support for exporting vcards with all the fields you would expect (phone, email, photo, etc.)
- I add two custom fields, one for the modification timestamp and another for the NAME_RAW_CONTACT_ID so that the contacts can be correlated back to the Android database
This does not (yet) support writing contacts back to the phone nor does it automatically listen to the phone's contacts database to change
BUG: 367999
Test Plan:
Connect the device to the desktop and verify that vcards are created in QStandardPaths::GenericDataLocation / kpeoplevcard". On my system this is ~/.local/share/kpeoplevcard
Create a dummy contact on the device and verify it is synchronized (Currently not automatic, have to disconnect and reconnect or use dbus)
Modify the dummy contact and verify the modifications are synchronized (Currently not automatic, have to disconnect and reconnect or use dbus)
Delete the dummy contact and verify the deletion is synchronized (Currently not automatic, have to disconnect and reconnect or use dbus)
Reviewers: #kde_connect, mtijink, nicolasfella
Reviewed By: #kde_connect, nicolasfella
Subscribers: MatMaul, philipc, kdeconnect, nicolasfella, andyholmes, mtijink
Tags: #kde_connect
Maniphest Tasks: T8283
Differential Revision: https://phabricator.kde.org/D9690
2018-05-30 18:33:23 -06:00
|
|
|
import java.io.IOException;
|
2016-03-06 21:50:58 +01:00
|
|
|
import java.io.InputStream;
|
Add contacts-reading plugin (Android side)
Summary:
Add a plugin to the KDE Connect Android application which supports reading the Android contacts databases and sending the requested data as vcards
- Android automatically has support for exporting vcards with all the fields you would expect (phone, email, photo, etc.)
- I add two custom fields, one for the modification timestamp and another for the NAME_RAW_CONTACT_ID so that the contacts can be correlated back to the Android database
This does not (yet) support writing contacts back to the phone nor does it automatically listen to the phone's contacts database to change
BUG: 367999
Test Plan:
Connect the device to the desktop and verify that vcards are created in QStandardPaths::GenericDataLocation / kpeoplevcard". On my system this is ~/.local/share/kpeoplevcard
Create a dummy contact on the device and verify it is synchronized (Currently not automatic, have to disconnect and reconnect or use dbus)
Modify the dummy contact and verify the modifications are synchronized (Currently not automatic, have to disconnect and reconnect or use dbus)
Delete the dummy contact and verify the deletion is synchronized (Currently not automatic, have to disconnect and reconnect or use dbus)
Reviewers: #kde_connect, mtijink, nicolasfella
Reviewed By: #kde_connect, nicolasfella
Subscribers: MatMaul, philipc, kdeconnect, nicolasfella, andyholmes, mtijink
Tags: #kde_connect
Maniphest Tasks: T8283
Differential Revision: https://phabricator.kde.org/D9690
2018-05-30 18:33:23 -06:00
|
|
|
import java.io.InputStreamReader;
|
2020-07-10 10:36:11 +05:30
|
|
|
import java.io.Reader;
|
Add contacts-reading plugin (Android side)
Summary:
Add a plugin to the KDE Connect Android application which supports reading the Android contacts databases and sending the requested data as vcards
- Android automatically has support for exporting vcards with all the fields you would expect (phone, email, photo, etc.)
- I add two custom fields, one for the modification timestamp and another for the NAME_RAW_CONTACT_ID so that the contacts can be correlated back to the Android database
This does not (yet) support writing contacts back to the phone nor does it automatically listen to the phone's contacts database to change
BUG: 367999
Test Plan:
Connect the device to the desktop and verify that vcards are created in QStandardPaths::GenericDataLocation / kpeoplevcard". On my system this is ~/.local/share/kpeoplevcard
Create a dummy contact on the device and verify it is synchronized (Currently not automatic, have to disconnect and reconnect or use dbus)
Modify the dummy contact and verify the modifications are synchronized (Currently not automatic, have to disconnect and reconnect or use dbus)
Delete the dummy contact and verify the deletion is synchronized (Currently not automatic, have to disconnect and reconnect or use dbus)
Reviewers: #kde_connect, mtijink, nicolasfella
Reviewed By: #kde_connect, nicolasfella
Subscribers: MatMaul, philipc, kdeconnect, nicolasfella, andyholmes, mtijink
Tags: #kde_connect
Maniphest Tasks: T8283
Differential Revision: https://phabricator.kde.org/D9690
2018-05-30 18:33:23 -06:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Collection;
|
2016-03-06 21:39:52 +01:00
|
|
|
import java.util.HashMap;
|
Add contacts-reading plugin (Android side)
Summary:
Add a plugin to the KDE Connect Android application which supports reading the Android contacts databases and sending the requested data as vcards
- Android automatically has support for exporting vcards with all the fields you would expect (phone, email, photo, etc.)
- I add two custom fields, one for the modification timestamp and another for the NAME_RAW_CONTACT_ID so that the contacts can be correlated back to the Android database
This does not (yet) support writing contacts back to the phone nor does it automatically listen to the phone's contacts database to change
BUG: 367999
Test Plan:
Connect the device to the desktop and verify that vcards are created in QStandardPaths::GenericDataLocation / kpeoplevcard". On my system this is ~/.local/share/kpeoplevcard
Create a dummy contact on the device and verify it is synchronized (Currently not automatic, have to disconnect and reconnect or use dbus)
Modify the dummy contact and verify the modifications are synchronized (Currently not automatic, have to disconnect and reconnect or use dbus)
Delete the dummy contact and verify the deletion is synchronized (Currently not automatic, have to disconnect and reconnect or use dbus)
Reviewers: #kde_connect, mtijink, nicolasfella
Reviewed By: #kde_connect, nicolasfella
Subscribers: MatMaul, philipc, kdeconnect, nicolasfella, andyholmes, mtijink
Tags: #kde_connect
Maniphest Tasks: T8283
Differential Revision: https://phabricator.kde.org/D9690
2018-05-30 18:33:23 -06:00
|
|
|
import java.util.List;
|
2016-03-06 21:39:52 +01:00
|
|
|
import java.util.Map;
|
2020-07-10 10:36:11 +05:30
|
|
|
import java.util.stream.Collectors;
|
2013-08-01 02:30:58 +02:00
|
|
|
|
|
|
|
public class ContactsHelper {
|
|
|
|
|
2019-10-27 21:23:52 +00:00
|
|
|
static final String LOG_TAG = "ContactsHelper";
|
2016-06-21 16:44:21 +02:00
|
|
|
|
Add contacts-reading plugin (Android side)
Summary:
Add a plugin to the KDE Connect Android application which supports reading the Android contacts databases and sending the requested data as vcards
- Android automatically has support for exporting vcards with all the fields you would expect (phone, email, photo, etc.)
- I add two custom fields, one for the modification timestamp and another for the NAME_RAW_CONTACT_ID so that the contacts can be correlated back to the Android database
This does not (yet) support writing contacts back to the phone nor does it automatically listen to the phone's contacts database to change
BUG: 367999
Test Plan:
Connect the device to the desktop and verify that vcards are created in QStandardPaths::GenericDataLocation / kpeoplevcard". On my system this is ~/.local/share/kpeoplevcard
Create a dummy contact on the device and verify it is synchronized (Currently not automatic, have to disconnect and reconnect or use dbus)
Modify the dummy contact and verify the modifications are synchronized (Currently not automatic, have to disconnect and reconnect or use dbus)
Delete the dummy contact and verify the deletion is synchronized (Currently not automatic, have to disconnect and reconnect or use dbus)
Reviewers: #kde_connect, mtijink, nicolasfella
Reviewed By: #kde_connect, nicolasfella
Subscribers: MatMaul, philipc, kdeconnect, nicolasfella, andyholmes, mtijink
Tags: #kde_connect
Maniphest Tasks: T8283
Differential Revision: https://phabricator.kde.org/D9690
2018-05-30 18:33:23 -06:00
|
|
|
/**
|
|
|
|
* Lookup the name and photoID of a contact given a phone number
|
|
|
|
*/
|
2018-10-26 23:08:32 +02:00
|
|
|
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
|
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
|
|
|
|
2016-06-21 16:44:21 +02:00
|
|
|
Map<String, String> contactInfo = new HashMap<>();
|
2016-03-06 21:39:52 +01:00
|
|
|
|
2013-08-01 02:30:58 +02:00
|
|
|
Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));
|
2018-10-30 12:30:21 +01:00
|
|
|
String[] columns = new String[]{
|
|
|
|
PhoneLookup.DISPLAY_NAME,
|
|
|
|
PhoneLookup.PHOTO_URI
|
|
|
|
/*, PhoneLookup.TYPE
|
|
|
|
, PhoneLookup.LABEL
|
|
|
|
, PhoneLookup.ID */
|
|
|
|
};
|
|
|
|
try (Cursor cursor = context.getContentResolver().query(uri, columns,null, null, null)) {
|
|
|
|
// Take the first match only
|
|
|
|
if (cursor != null && cursor.moveToFirst()) {
|
|
|
|
int nameIndex = cursor.getColumnIndex(PhoneLookup.DISPLAY_NAME);
|
|
|
|
if (nameIndex != -1) {
|
|
|
|
contactInfo.put("name", cursor.getString(nameIndex));
|
|
|
|
}
|
2016-07-07 17:45:04 +02:00
|
|
|
|
2018-10-30 12:30:21 +01:00
|
|
|
nameIndex = cursor.getColumnIndex(PhoneLookup.PHOTO_URI);
|
|
|
|
if (nameIndex != -1) {
|
|
|
|
contactInfo.put("photoID", cursor.getString(nameIndex));
|
|
|
|
}
|
2016-03-06 21:39:52 +01:00
|
|
|
}
|
2018-10-30 12:30:21 +01:00
|
|
|
} catch (Exception ignored) {
|
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) {
|
2016-06-21 16:44:21 +02:00
|
|
|
return "";
|
2016-06-03 14:51:31 +02:00
|
|
|
}
|
2016-03-06 21:50:58 +01:00
|
|
|
Uri photoUri = Uri.parse(photoId);
|
|
|
|
|
2018-12-24 17:50:49 +01:00
|
|
|
ByteArrayOutputStream encodedPhoto = new ByteArrayOutputStream();
|
|
|
|
try (InputStream input = context.getContentResolver().openInputStream(photoUri); Base64OutputStream output = new Base64OutputStream(encodedPhoto, Base64.DEFAULT)) {
|
2016-06-21 16:44:21 +02:00
|
|
|
byte[] buffer = new byte[1024];
|
2016-03-06 21:50:58 +01:00
|
|
|
int len;
|
Add contacts-reading plugin (Android side)
Summary:
Add a plugin to the KDE Connect Android application which supports reading the Android contacts databases and sending the requested data as vcards
- Android automatically has support for exporting vcards with all the fields you would expect (phone, email, photo, etc.)
- I add two custom fields, one for the modification timestamp and another for the NAME_RAW_CONTACT_ID so that the contacts can be correlated back to the Android database
This does not (yet) support writing contacts back to the phone nor does it automatically listen to the phone's contacts database to change
BUG: 367999
Test Plan:
Connect the device to the desktop and verify that vcards are created in QStandardPaths::GenericDataLocation / kpeoplevcard". On my system this is ~/.local/share/kpeoplevcard
Create a dummy contact on the device and verify it is synchronized (Currently not automatic, have to disconnect and reconnect or use dbus)
Modify the dummy contact and verify the modifications are synchronized (Currently not automatic, have to disconnect and reconnect or use dbus)
Delete the dummy contact and verify the deletion is synchronized (Currently not automatic, have to disconnect and reconnect or use dbus)
Reviewers: #kde_connect, mtijink, nicolasfella
Reviewed By: #kde_connect, nicolasfella
Subscribers: MatMaul, philipc, kdeconnect, nicolasfella, andyholmes, mtijink
Tags: #kde_connect
Maniphest Tasks: T8283
Differential Revision: https://phabricator.kde.org/D9690
2018-05-30 18:33:23 -06:00
|
|
|
//noinspection ConstantConditions
|
2016-06-21 16:44:21 +02:00
|
|
|
while ((len = input.read(buffer)) != -1) {
|
|
|
|
output.write(buffer, 0, len);
|
2016-03-06 21:50:58 +01:00
|
|
|
}
|
|
|
|
return encodedPhoto.toString();
|
|
|
|
} catch (Exception ex) {
|
2019-10-27 21:23:52 +00:00
|
|
|
Log.e(LOG_TAG, ex.toString());
|
2016-06-21 16:44:21 +02:00
|
|
|
return "";
|
2016-03-06 21:50:58 +01:00
|
|
|
}
|
|
|
|
}
|
2016-03-06 21:39:52 +01:00
|
|
|
|
Add contacts-reading plugin (Android side)
Summary:
Add a plugin to the KDE Connect Android application which supports reading the Android contacts databases and sending the requested data as vcards
- Android automatically has support for exporting vcards with all the fields you would expect (phone, email, photo, etc.)
- I add two custom fields, one for the modification timestamp and another for the NAME_RAW_CONTACT_ID so that the contacts can be correlated back to the Android database
This does not (yet) support writing contacts back to the phone nor does it automatically listen to the phone's contacts database to change
BUG: 367999
Test Plan:
Connect the device to the desktop and verify that vcards are created in QStandardPaths::GenericDataLocation / kpeoplevcard". On my system this is ~/.local/share/kpeoplevcard
Create a dummy contact on the device and verify it is synchronized (Currently not automatic, have to disconnect and reconnect or use dbus)
Modify the dummy contact and verify the modifications are synchronized (Currently not automatic, have to disconnect and reconnect or use dbus)
Delete the dummy contact and verify the deletion is synchronized (Currently not automatic, have to disconnect and reconnect or use dbus)
Reviewers: #kde_connect, mtijink, nicolasfella
Reviewed By: #kde_connect, nicolasfella
Subscribers: MatMaul, philipc, kdeconnect, nicolasfella, andyholmes, mtijink
Tags: #kde_connect
Maniphest Tasks: T8283
Differential Revision: https://phabricator.kde.org/D9690
2018-05-30 18:33:23 -06:00
|
|
|
/**
|
|
|
|
* Return all the NAME_RAW_CONTACT_IDS which contribute an entry to a Contact in the database
|
|
|
|
* <p>
|
|
|
|
* If the user has, for example, joined several contacts, on the phone, the IDs returned will
|
|
|
|
* be representative of the joined contact
|
|
|
|
* <p>
|
|
|
|
* See here: https://developer.android.com/reference/android/provider/ContactsContract.Contacts.html
|
|
|
|
* for more information about the connection between contacts and raw contacts
|
|
|
|
*
|
|
|
|
* @param context android.content.Context running the request
|
|
|
|
* @return List of each NAME_RAW_CONTACT_ID in the Contacts database
|
|
|
|
*/
|
|
|
|
public static List<uID> getAllContactContactIDs(Context context) {
|
|
|
|
ArrayList<uID> toReturn = new ArrayList<>();
|
|
|
|
|
|
|
|
// Define the columns we want to read from the Contacts database
|
2018-10-30 12:30:21 +01:00
|
|
|
final String[] columns = new String[]{
|
Add contacts-reading plugin (Android side)
Summary:
Add a plugin to the KDE Connect Android application which supports reading the Android contacts databases and sending the requested data as vcards
- Android automatically has support for exporting vcards with all the fields you would expect (phone, email, photo, etc.)
- I add two custom fields, one for the modification timestamp and another for the NAME_RAW_CONTACT_ID so that the contacts can be correlated back to the Android database
This does not (yet) support writing contacts back to the phone nor does it automatically listen to the phone's contacts database to change
BUG: 367999
Test Plan:
Connect the device to the desktop and verify that vcards are created in QStandardPaths::GenericDataLocation / kpeoplevcard". On my system this is ~/.local/share/kpeoplevcard
Create a dummy contact on the device and verify it is synchronized (Currently not automatic, have to disconnect and reconnect or use dbus)
Modify the dummy contact and verify the modifications are synchronized (Currently not automatic, have to disconnect and reconnect or use dbus)
Delete the dummy contact and verify the deletion is synchronized (Currently not automatic, have to disconnect and reconnect or use dbus)
Reviewers: #kde_connect, mtijink, nicolasfella
Reviewed By: #kde_connect, nicolasfella
Subscribers: MatMaul, philipc, kdeconnect, nicolasfella, andyholmes, mtijink
Tags: #kde_connect
Maniphest Tasks: T8283
Differential Revision: https://phabricator.kde.org/D9690
2018-05-30 18:33:23 -06:00
|
|
|
ContactsContract.Contacts.LOOKUP_KEY
|
|
|
|
};
|
|
|
|
|
|
|
|
Uri contactsUri = ContactsContract.Contacts.CONTENT_URI;
|
2018-10-30 12:30:21 +01:00
|
|
|
try (Cursor contactsCursor = context.getContentResolver().query(contactsUri, columns, null, null, null)) {
|
|
|
|
if (contactsCursor != null && contactsCursor.moveToFirst()) {
|
|
|
|
do {
|
|
|
|
uID contactID;
|
|
|
|
|
|
|
|
int idIndex = contactsCursor.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY);
|
|
|
|
if (idIndex != -1) {
|
|
|
|
contactID = new uID(contactsCursor.getString(idIndex));
|
|
|
|
} else {
|
|
|
|
// Something went wrong with this contact
|
|
|
|
// If you are experiencing this, please open a bug report indicating how you got here
|
2019-10-27 21:23:52 +00:00
|
|
|
Log.e(LOG_TAG, "Got a contact which does not have a LOOKUP_KEY");
|
2018-10-30 12:30:21 +01:00
|
|
|
continue;
|
|
|
|
}
|
Add contacts-reading plugin (Android side)
Summary:
Add a plugin to the KDE Connect Android application which supports reading the Android contacts databases and sending the requested data as vcards
- Android automatically has support for exporting vcards with all the fields you would expect (phone, email, photo, etc.)
- I add two custom fields, one for the modification timestamp and another for the NAME_RAW_CONTACT_ID so that the contacts can be correlated back to the Android database
This does not (yet) support writing contacts back to the phone nor does it automatically listen to the phone's contacts database to change
BUG: 367999
Test Plan:
Connect the device to the desktop and verify that vcards are created in QStandardPaths::GenericDataLocation / kpeoplevcard". On my system this is ~/.local/share/kpeoplevcard
Create a dummy contact on the device and verify it is synchronized (Currently not automatic, have to disconnect and reconnect or use dbus)
Modify the dummy contact and verify the modifications are synchronized (Currently not automatic, have to disconnect and reconnect or use dbus)
Delete the dummy contact and verify the deletion is synchronized (Currently not automatic, have to disconnect and reconnect or use dbus)
Reviewers: #kde_connect, mtijink, nicolasfella
Reviewed By: #kde_connect, nicolasfella
Subscribers: MatMaul, philipc, kdeconnect, nicolasfella, andyholmes, mtijink
Tags: #kde_connect
Maniphest Tasks: T8283
Differential Revision: https://phabricator.kde.org/D9690
2018-05-30 18:33:23 -06:00
|
|
|
|
2019-08-31 11:11:58 +02:00
|
|
|
if (!toReturn.contains(contactID)) {
|
|
|
|
toReturn.add(contactID);
|
|
|
|
}
|
2018-10-30 12:30:21 +01:00
|
|
|
} while (contactsCursor.moveToNext());
|
Add contacts-reading plugin (Android side)
Summary:
Add a plugin to the KDE Connect Android application which supports reading the Android contacts databases and sending the requested data as vcards
- Android automatically has support for exporting vcards with all the fields you would expect (phone, email, photo, etc.)
- I add two custom fields, one for the modification timestamp and another for the NAME_RAW_CONTACT_ID so that the contacts can be correlated back to the Android database
This does not (yet) support writing contacts back to the phone nor does it automatically listen to the phone's contacts database to change
BUG: 367999
Test Plan:
Connect the device to the desktop and verify that vcards are created in QStandardPaths::GenericDataLocation / kpeoplevcard". On my system this is ~/.local/share/kpeoplevcard
Create a dummy contact on the device and verify it is synchronized (Currently not automatic, have to disconnect and reconnect or use dbus)
Modify the dummy contact and verify the modifications are synchronized (Currently not automatic, have to disconnect and reconnect or use dbus)
Delete the dummy contact and verify the deletion is synchronized (Currently not automatic, have to disconnect and reconnect or use dbus)
Reviewers: #kde_connect, mtijink, nicolasfella
Reviewed By: #kde_connect, nicolasfella
Subscribers: MatMaul, philipc, kdeconnect, nicolasfella, andyholmes, mtijink
Tags: #kde_connect
Maniphest Tasks: T8283
Differential Revision: https://phabricator.kde.org/D9690
2018-05-30 18:33:23 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return toReturn;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-10-19 09:00:28 -07:00
|
|
|
* Get VCards using serial database lookups. This is tragically slow, so call only when needed.
|
Add contacts-reading plugin (Android side)
Summary:
Add a plugin to the KDE Connect Android application which supports reading the Android contacts databases and sending the requested data as vcards
- Android automatically has support for exporting vcards with all the fields you would expect (phone, email, photo, etc.)
- I add two custom fields, one for the modification timestamp and another for the NAME_RAW_CONTACT_ID so that the contacts can be correlated back to the Android database
This does not (yet) support writing contacts back to the phone nor does it automatically listen to the phone's contacts database to change
BUG: 367999
Test Plan:
Connect the device to the desktop and verify that vcards are created in QStandardPaths::GenericDataLocation / kpeoplevcard". On my system this is ~/.local/share/kpeoplevcard
Create a dummy contact on the device and verify it is synchronized (Currently not automatic, have to disconnect and reconnect or use dbus)
Modify the dummy contact and verify the modifications are synchronized (Currently not automatic, have to disconnect and reconnect or use dbus)
Delete the dummy contact and verify the deletion is synchronized (Currently not automatic, have to disconnect and reconnect or use dbus)
Reviewers: #kde_connect, mtijink, nicolasfella
Reviewed By: #kde_connect, nicolasfella
Subscribers: MatMaul, philipc, kdeconnect, nicolasfella, andyholmes, mtijink
Tags: #kde_connect
Maniphest Tasks: T8283
Differential Revision: https://phabricator.kde.org/D9690
2018-05-30 18:33:23 -06:00
|
|
|
*
|
|
|
|
* There is a faster API specified using ContactsContract.Contacts.CONTENT_MULTI_VCARD_URI,
|
|
|
|
* but there does not seem to be a way to figure out which ID resulted in which VCard using that API
|
|
|
|
*
|
|
|
|
* @param context android.content.Context running the request
|
|
|
|
* @param IDs collection of uIDs to look up
|
|
|
|
* @return Mapping of uIDs to the corresponding VCard
|
|
|
|
*/
|
2018-10-26 23:53:58 +02:00
|
|
|
private static Map<uID, VCardBuilder> getVCardsSlow(Context context, Collection<uID> IDs) {
|
Add contacts-reading plugin (Android side)
Summary:
Add a plugin to the KDE Connect Android application which supports reading the Android contacts databases and sending the requested data as vcards
- Android automatically has support for exporting vcards with all the fields you would expect (phone, email, photo, etc.)
- I add two custom fields, one for the modification timestamp and another for the NAME_RAW_CONTACT_ID so that the contacts can be correlated back to the Android database
This does not (yet) support writing contacts back to the phone nor does it automatically listen to the phone's contacts database to change
BUG: 367999
Test Plan:
Connect the device to the desktop and verify that vcards are created in QStandardPaths::GenericDataLocation / kpeoplevcard". On my system this is ~/.local/share/kpeoplevcard
Create a dummy contact on the device and verify it is synchronized (Currently not automatic, have to disconnect and reconnect or use dbus)
Modify the dummy contact and verify the modifications are synchronized (Currently not automatic, have to disconnect and reconnect or use dbus)
Delete the dummy contact and verify the deletion is synchronized (Currently not automatic, have to disconnect and reconnect or use dbus)
Reviewers: #kde_connect, mtijink, nicolasfella
Reviewed By: #kde_connect, nicolasfella
Subscribers: MatMaul, philipc, kdeconnect, nicolasfella, andyholmes, mtijink
Tags: #kde_connect
Maniphest Tasks: T8283
Differential Revision: https://phabricator.kde.org/D9690
2018-05-30 18:33:23 -06:00
|
|
|
Map<uID, VCardBuilder> toReturn = new HashMap<>();
|
|
|
|
|
|
|
|
for (uID ID : IDs) {
|
|
|
|
String lookupKey = ID.toString();
|
|
|
|
Uri vcardURI = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_VCARD_URI, lookupKey);
|
2018-10-30 12:30:21 +01:00
|
|
|
|
|
|
|
try (InputStream input = context.getContentResolver().openInputStream(vcardURI)) {
|
Add contacts-reading plugin (Android side)
Summary:
Add a plugin to the KDE Connect Android application which supports reading the Android contacts databases and sending the requested data as vcards
- Android automatically has support for exporting vcards with all the fields you would expect (phone, email, photo, etc.)
- I add two custom fields, one for the modification timestamp and another for the NAME_RAW_CONTACT_ID so that the contacts can be correlated back to the Android database
This does not (yet) support writing contacts back to the phone nor does it automatically listen to the phone's contacts database to change
BUG: 367999
Test Plan:
Connect the device to the desktop and verify that vcards are created in QStandardPaths::GenericDataLocation / kpeoplevcard". On my system this is ~/.local/share/kpeoplevcard
Create a dummy contact on the device and verify it is synchronized (Currently not automatic, have to disconnect and reconnect or use dbus)
Modify the dummy contact and verify the modifications are synchronized (Currently not automatic, have to disconnect and reconnect or use dbus)
Delete the dummy contact and verify the deletion is synchronized (Currently not automatic, have to disconnect and reconnect or use dbus)
Reviewers: #kde_connect, mtijink, nicolasfella
Reviewed By: #kde_connect, nicolasfella
Subscribers: MatMaul, philipc, kdeconnect, nicolasfella, andyholmes, mtijink
Tags: #kde_connect
Maniphest Tasks: T8283
Differential Revision: https://phabricator.kde.org/D9690
2018-05-30 18:33:23 -06:00
|
|
|
|
|
|
|
if (input == null)
|
|
|
|
{
|
|
|
|
throw new NullPointerException("ContentResolver did not give us a stream for the VCard for uID " + ID);
|
|
|
|
}
|
|
|
|
|
2020-07-10 10:36:11 +05:30
|
|
|
final Reader reader = new InputStreamReader(input);
|
|
|
|
final List<String> lines = IOUtils.readLines(reader);
|
|
|
|
reader.close();
|
|
|
|
toReturn.put(ID, new VCardBuilder(lines.stream().collect(Collectors.joining("\n"))));
|
Add contacts-reading plugin (Android side)
Summary:
Add a plugin to the KDE Connect Android application which supports reading the Android contacts databases and sending the requested data as vcards
- Android automatically has support for exporting vcards with all the fields you would expect (phone, email, photo, etc.)
- I add two custom fields, one for the modification timestamp and another for the NAME_RAW_CONTACT_ID so that the contacts can be correlated back to the Android database
This does not (yet) support writing contacts back to the phone nor does it automatically listen to the phone's contacts database to change
BUG: 367999
Test Plan:
Connect the device to the desktop and verify that vcards are created in QStandardPaths::GenericDataLocation / kpeoplevcard". On my system this is ~/.local/share/kpeoplevcard
Create a dummy contact on the device and verify it is synchronized (Currently not automatic, have to disconnect and reconnect or use dbus)
Modify the dummy contact and verify the modifications are synchronized (Currently not automatic, have to disconnect and reconnect or use dbus)
Delete the dummy contact and verify the deletion is synchronized (Currently not automatic, have to disconnect and reconnect or use dbus)
Reviewers: #kde_connect, mtijink, nicolasfella
Reviewed By: #kde_connect, nicolasfella
Subscribers: MatMaul, philipc, kdeconnect, nicolasfella, andyholmes, mtijink
Tags: #kde_connect
Maniphest Tasks: T8283
Differential Revision: https://phabricator.kde.org/D9690
2018-05-30 18:33:23 -06:00
|
|
|
} catch (IOException e) {
|
|
|
|
// If you are experiencing this, please open a bug report indicating how you got here
|
2019-03-31 20:09:44 +02:00
|
|
|
Log.e("Contacts", "Exception while fetching vcards", e);
|
2018-10-30 12:30:21 +01:00
|
|
|
} catch (NullPointerException e) {
|
Add contacts-reading plugin (Android side)
Summary:
Add a plugin to the KDE Connect Android application which supports reading the Android contacts databases and sending the requested data as vcards
- Android automatically has support for exporting vcards with all the fields you would expect (phone, email, photo, etc.)
- I add two custom fields, one for the modification timestamp and another for the NAME_RAW_CONTACT_ID so that the contacts can be correlated back to the Android database
This does not (yet) support writing contacts back to the phone nor does it automatically listen to the phone's contacts database to change
BUG: 367999
Test Plan:
Connect the device to the desktop and verify that vcards are created in QStandardPaths::GenericDataLocation / kpeoplevcard". On my system this is ~/.local/share/kpeoplevcard
Create a dummy contact on the device and verify it is synchronized (Currently not automatic, have to disconnect and reconnect or use dbus)
Modify the dummy contact and verify the modifications are synchronized (Currently not automatic, have to disconnect and reconnect or use dbus)
Delete the dummy contact and verify the deletion is synchronized (Currently not automatic, have to disconnect and reconnect or use dbus)
Reviewers: #kde_connect, mtijink, nicolasfella
Reviewed By: #kde_connect, nicolasfella
Subscribers: MatMaul, philipc, kdeconnect, nicolasfella, andyholmes, mtijink
Tags: #kde_connect
Maniphest Tasks: T8283
Differential Revision: https://phabricator.kde.org/D9690
2018-05-30 18:33:23 -06:00
|
|
|
// If you are experiencing this, please open a bug report indicating how you got here
|
2019-03-31 20:09:44 +02:00
|
|
|
Log.e("Contacts", "Exception while fetching vcards", e);
|
Add contacts-reading plugin (Android side)
Summary:
Add a plugin to the KDE Connect Android application which supports reading the Android contacts databases and sending the requested data as vcards
- Android automatically has support for exporting vcards with all the fields you would expect (phone, email, photo, etc.)
- I add two custom fields, one for the modification timestamp and another for the NAME_RAW_CONTACT_ID so that the contacts can be correlated back to the Android database
This does not (yet) support writing contacts back to the phone nor does it automatically listen to the phone's contacts database to change
BUG: 367999
Test Plan:
Connect the device to the desktop and verify that vcards are created in QStandardPaths::GenericDataLocation / kpeoplevcard". On my system this is ~/.local/share/kpeoplevcard
Create a dummy contact on the device and verify it is synchronized (Currently not automatic, have to disconnect and reconnect or use dbus)
Modify the dummy contact and verify the modifications are synchronized (Currently not automatic, have to disconnect and reconnect or use dbus)
Delete the dummy contact and verify the deletion is synchronized (Currently not automatic, have to disconnect and reconnect or use dbus)
Reviewers: #kde_connect, mtijink, nicolasfella
Reviewed By: #kde_connect, nicolasfella
Subscribers: MatMaul, philipc, kdeconnect, nicolasfella, andyholmes, mtijink
Tags: #kde_connect
Maniphest Tasks: T8283
Differential Revision: https://phabricator.kde.org/D9690
2018-05-30 18:33:23 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return toReturn;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the VCard for every specified raw contact ID
|
|
|
|
*
|
|
|
|
* @param context android.content.Context running the request
|
|
|
|
* @param IDs collection of raw contact IDs to look up
|
|
|
|
* @return Mapping of raw contact IDs to the corresponding VCard
|
|
|
|
*/
|
|
|
|
public static Map<uID, VCardBuilder> getVCardsForContactIDs(Context context, Collection<uID> IDs) {
|
|
|
|
return getVCardsSlow(context, IDs);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-10-27 21:23:52 +00:00
|
|
|
* Get the last-modified timestamp for every contact in the database
|
Add contacts-reading plugin (Android side)
Summary:
Add a plugin to the KDE Connect Android application which supports reading the Android contacts databases and sending the requested data as vcards
- Android automatically has support for exporting vcards with all the fields you would expect (phone, email, photo, etc.)
- I add two custom fields, one for the modification timestamp and another for the NAME_RAW_CONTACT_ID so that the contacts can be correlated back to the Android database
This does not (yet) support writing contacts back to the phone nor does it automatically listen to the phone's contacts database to change
BUG: 367999
Test Plan:
Connect the device to the desktop and verify that vcards are created in QStandardPaths::GenericDataLocation / kpeoplevcard". On my system this is ~/.local/share/kpeoplevcard
Create a dummy contact on the device and verify it is synchronized (Currently not automatic, have to disconnect and reconnect or use dbus)
Modify the dummy contact and verify the modifications are synchronized (Currently not automatic, have to disconnect and reconnect or use dbus)
Delete the dummy contact and verify the deletion is synchronized (Currently not automatic, have to disconnect and reconnect or use dbus)
Reviewers: #kde_connect, mtijink, nicolasfella
Reviewed By: #kde_connect, nicolasfella
Subscribers: MatMaul, philipc, kdeconnect, nicolasfella, andyholmes, mtijink
Tags: #kde_connect
Maniphest Tasks: T8283
Differential Revision: https://phabricator.kde.org/D9690
2018-05-30 18:33:23 -06:00
|
|
|
*
|
2019-10-27 21:23:52 +00:00
|
|
|
* @param context android.content.Context running the request
|
|
|
|
* @return Mapping of contact uID to last-modified timestamp
|
Add contacts-reading plugin (Android side)
Summary:
Add a plugin to the KDE Connect Android application which supports reading the Android contacts databases and sending the requested data as vcards
- Android automatically has support for exporting vcards with all the fields you would expect (phone, email, photo, etc.)
- I add two custom fields, one for the modification timestamp and another for the NAME_RAW_CONTACT_ID so that the contacts can be correlated back to the Android database
This does not (yet) support writing contacts back to the phone nor does it automatically listen to the phone's contacts database to change
BUG: 367999
Test Plan:
Connect the device to the desktop and verify that vcards are created in QStandardPaths::GenericDataLocation / kpeoplevcard". On my system this is ~/.local/share/kpeoplevcard
Create a dummy contact on the device and verify it is synchronized (Currently not automatic, have to disconnect and reconnect or use dbus)
Modify the dummy contact and verify the modifications are synchronized (Currently not automatic, have to disconnect and reconnect or use dbus)
Delete the dummy contact and verify the deletion is synchronized (Currently not automatic, have to disconnect and reconnect or use dbus)
Reviewers: #kde_connect, mtijink, nicolasfella
Reviewed By: #kde_connect, nicolasfella
Subscribers: MatMaul, philipc, kdeconnect, nicolasfella, andyholmes, mtijink
Tags: #kde_connect
Maniphest Tasks: T8283
Differential Revision: https://phabricator.kde.org/D9690
2018-05-30 18:33:23 -06:00
|
|
|
*/
|
2019-10-27 21:23:52 +00:00
|
|
|
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2) // Need API 18 for contact timestamps
|
|
|
|
public static Map<uID, Long> getAllContactTimestamps(Context context) {
|
|
|
|
String[] projection = { uID.COLUMN, ContactsContract.Contacts.CONTACT_LAST_UPDATED_TIMESTAMP };
|
|
|
|
|
|
|
|
Map<uID, Map<String, String>> databaseValues = accessContactsDatabase(context, projection, null, null, null);
|
|
|
|
|
|
|
|
Map<uID, Long> timestamps = new HashMap<>();
|
|
|
|
for (uID contactID : databaseValues.keySet()) {
|
|
|
|
Map<String, String> data = databaseValues.get(contactID);
|
|
|
|
timestamps.put(
|
|
|
|
contactID,
|
|
|
|
Long.parseLong(data.get(ContactsContract.Contacts.CONTACT_LAST_UPDATED_TIMESTAMP))
|
|
|
|
);
|
2019-01-27 17:33:27 +01:00
|
|
|
}
|
|
|
|
|
2019-10-27 21:23:52 +00:00
|
|
|
return timestamps;
|
|
|
|
}
|
Add contacts-reading plugin (Android side)
Summary:
Add a plugin to the KDE Connect Android application which supports reading the Android contacts databases and sending the requested data as vcards
- Android automatically has support for exporting vcards with all the fields you would expect (phone, email, photo, etc.)
- I add two custom fields, one for the modification timestamp and another for the NAME_RAW_CONTACT_ID so that the contacts can be correlated back to the Android database
This does not (yet) support writing contacts back to the phone nor does it automatically listen to the phone's contacts database to change
BUG: 367999
Test Plan:
Connect the device to the desktop and verify that vcards are created in QStandardPaths::GenericDataLocation / kpeoplevcard". On my system this is ~/.local/share/kpeoplevcard
Create a dummy contact on the device and verify it is synchronized (Currently not automatic, have to disconnect and reconnect or use dbus)
Modify the dummy contact and verify the modifications are synchronized (Currently not automatic, have to disconnect and reconnect or use dbus)
Delete the dummy contact and verify the deletion is synchronized (Currently not automatic, have to disconnect and reconnect or use dbus)
Reviewers: #kde_connect, mtijink, nicolasfella
Reviewed By: #kde_connect, nicolasfella
Subscribers: MatMaul, philipc, kdeconnect, nicolasfella, andyholmes, mtijink
Tags: #kde_connect
Maniphest Tasks: T8283
Differential Revision: https://phabricator.kde.org/D9690
2018-05-30 18:33:23 -06:00
|
|
|
|
2019-10-27 21:23:52 +00:00
|
|
|
/**
|
|
|
|
* Get the last-modified timestamp for the specified contact
|
|
|
|
*
|
|
|
|
* @param context android.content.Context running the request
|
|
|
|
* @param contactID Contact uID to read
|
|
|
|
* @throws ContactNotFoundException If the given ID for some reason does not match a contact
|
|
|
|
* @return Last-modified timestamp of the contact
|
|
|
|
*/
|
|
|
|
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2) // Need API 18 for contact timestamps
|
|
|
|
public static Long getContactTimestamp(Context context, uID contactID) throws ContactNotFoundException {
|
|
|
|
String[] projection = { uID.COLUMN, ContactsContract.Contacts.CONTACT_LAST_UPDATED_TIMESTAMP };
|
|
|
|
String selection = uID.COLUMN + " = ?";
|
|
|
|
String[] selectionArgs = { contactID.toString() };
|
Add contacts-reading plugin (Android side)
Summary:
Add a plugin to the KDE Connect Android application which supports reading the Android contacts databases and sending the requested data as vcards
- Android automatically has support for exporting vcards with all the fields you would expect (phone, email, photo, etc.)
- I add two custom fields, one for the modification timestamp and another for the NAME_RAW_CONTACT_ID so that the contacts can be correlated back to the Android database
This does not (yet) support writing contacts back to the phone nor does it automatically listen to the phone's contacts database to change
BUG: 367999
Test Plan:
Connect the device to the desktop and verify that vcards are created in QStandardPaths::GenericDataLocation / kpeoplevcard". On my system this is ~/.local/share/kpeoplevcard
Create a dummy contact on the device and verify it is synchronized (Currently not automatic, have to disconnect and reconnect or use dbus)
Modify the dummy contact and verify the modifications are synchronized (Currently not automatic, have to disconnect and reconnect or use dbus)
Delete the dummy contact and verify the deletion is synchronized (Currently not automatic, have to disconnect and reconnect or use dbus)
Reviewers: #kde_connect, mtijink, nicolasfella
Reviewed By: #kde_connect, nicolasfella
Subscribers: MatMaul, philipc, kdeconnect, nicolasfella, andyholmes, mtijink
Tags: #kde_connect
Maniphest Tasks: T8283
Differential Revision: https://phabricator.kde.org/D9690
2018-05-30 18:33:23 -06:00
|
|
|
|
2019-10-27 21:23:52 +00:00
|
|
|
Map<uID, Map<String, String>> databaseValue = accessContactsDatabase(context, projection, selection, selectionArgs, null);
|
Add contacts-reading plugin (Android side)
Summary:
Add a plugin to the KDE Connect Android application which supports reading the Android contacts databases and sending the requested data as vcards
- Android automatically has support for exporting vcards with all the fields you would expect (phone, email, photo, etc.)
- I add two custom fields, one for the modification timestamp and another for the NAME_RAW_CONTACT_ID so that the contacts can be correlated back to the Android database
This does not (yet) support writing contacts back to the phone nor does it automatically listen to the phone's contacts database to change
BUG: 367999
Test Plan:
Connect the device to the desktop and verify that vcards are created in QStandardPaths::GenericDataLocation / kpeoplevcard". On my system this is ~/.local/share/kpeoplevcard
Create a dummy contact on the device and verify it is synchronized (Currently not automatic, have to disconnect and reconnect or use dbus)
Modify the dummy contact and verify the modifications are synchronized (Currently not automatic, have to disconnect and reconnect or use dbus)
Delete the dummy contact and verify the deletion is synchronized (Currently not automatic, have to disconnect and reconnect or use dbus)
Reviewers: #kde_connect, mtijink, nicolasfella
Reviewed By: #kde_connect, nicolasfella
Subscribers: MatMaul, philipc, kdeconnect, nicolasfella, andyholmes, mtijink
Tags: #kde_connect
Maniphest Tasks: T8283
Differential Revision: https://phabricator.kde.org/D9690
2018-05-30 18:33:23 -06:00
|
|
|
|
2019-10-27 21:23:52 +00:00
|
|
|
if (databaseValue.size() == 0) {
|
|
|
|
throw new ContactNotFoundException("Querying for contact with id " + contactID + " returned no results.");
|
Add contacts-reading plugin (Android side)
Summary:
Add a plugin to the KDE Connect Android application which supports reading the Android contacts databases and sending the requested data as vcards
- Android automatically has support for exporting vcards with all the fields you would expect (phone, email, photo, etc.)
- I add two custom fields, one for the modification timestamp and another for the NAME_RAW_CONTACT_ID so that the contacts can be correlated back to the Android database
This does not (yet) support writing contacts back to the phone nor does it automatically listen to the phone's contacts database to change
BUG: 367999
Test Plan:
Connect the device to the desktop and verify that vcards are created in QStandardPaths::GenericDataLocation / kpeoplevcard". On my system this is ~/.local/share/kpeoplevcard
Create a dummy contact on the device and verify it is synchronized (Currently not automatic, have to disconnect and reconnect or use dbus)
Modify the dummy contact and verify the modifications are synchronized (Currently not automatic, have to disconnect and reconnect or use dbus)
Delete the dummy contact and verify the deletion is synchronized (Currently not automatic, have to disconnect and reconnect or use dbus)
Reviewers: #kde_connect, mtijink, nicolasfella
Reviewed By: #kde_connect, nicolasfella
Subscribers: MatMaul, philipc, kdeconnect, nicolasfella, andyholmes, mtijink
Tags: #kde_connect
Maniphest Tasks: T8283
Differential Revision: https://phabricator.kde.org/D9690
2018-05-30 18:33:23 -06:00
|
|
|
}
|
|
|
|
|
2019-10-27 21:23:52 +00:00
|
|
|
if (databaseValue.size() != 1) {
|
|
|
|
Log.w(LOG_TAG, "Received an improper number of return values from the database in getContactTimestamp: " + databaseValue.size());
|
Add contacts-reading plugin (Android side)
Summary:
Add a plugin to the KDE Connect Android application which supports reading the Android contacts databases and sending the requested data as vcards
- Android automatically has support for exporting vcards with all the fields you would expect (phone, email, photo, etc.)
- I add two custom fields, one for the modification timestamp and another for the NAME_RAW_CONTACT_ID so that the contacts can be correlated back to the Android database
This does not (yet) support writing contacts back to the phone nor does it automatically listen to the phone's contacts database to change
BUG: 367999
Test Plan:
Connect the device to the desktop and verify that vcards are created in QStandardPaths::GenericDataLocation / kpeoplevcard". On my system this is ~/.local/share/kpeoplevcard
Create a dummy contact on the device and verify it is synchronized (Currently not automatic, have to disconnect and reconnect or use dbus)
Modify the dummy contact and verify the modifications are synchronized (Currently not automatic, have to disconnect and reconnect or use dbus)
Delete the dummy contact and verify the deletion is synchronized (Currently not automatic, have to disconnect and reconnect or use dbus)
Reviewers: #kde_connect, mtijink, nicolasfella
Reviewed By: #kde_connect, nicolasfella
Subscribers: MatMaul, philipc, kdeconnect, nicolasfella, andyholmes, mtijink
Tags: #kde_connect
Maniphest Tasks: T8283
Differential Revision: https://phabricator.kde.org/D9690
2018-05-30 18:33:23 -06:00
|
|
|
}
|
|
|
|
|
2019-10-27 21:23:52 +00:00
|
|
|
Long timestamp = Long.parseLong(databaseValue.get(contactID).get(ContactsContract.Contacts.CONTACT_LAST_UPDATED_TIMESTAMP));
|
|
|
|
|
|
|
|
return timestamp;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a mapping of contact IDs to a map of the requested data from the Contacts database.
|
|
|
|
*
|
|
|
|
* @param context android.content.Context running the request
|
|
|
|
* @param projection List of column names to extract, defined in ContactsContract.Contacts. Must contain uID.COLUMN
|
|
|
|
* @param selection Parameterizable filter to use with the ContentResolver query. May be null.
|
|
|
|
* @param selectionArgs Parameters for selection. May be null.
|
|
|
|
* @param sortOrder Sort order to request from the ContentResolver query. May be null.
|
|
|
|
* @return mapping of contact uIDs to desired values, which are a mapping of column names to the data contained there
|
|
|
|
*/
|
|
|
|
private static Map<uID, Map<String, String>> accessContactsDatabase(
|
|
|
|
@NonNull Context context,
|
|
|
|
@NonNull String[] projection,
|
|
|
|
@Nullable String selection,
|
|
|
|
@Nullable String[] selectionArgs,
|
|
|
|
@Nullable String sortOrder
|
|
|
|
) {
|
|
|
|
Uri contactsUri = ContactsContract.Contacts.CONTENT_URI;
|
|
|
|
|
|
|
|
HashMap<uID, Map<String, String>> toReturn = new HashMap<>();
|
|
|
|
|
2018-10-30 12:30:21 +01:00
|
|
|
try (Cursor contactsCursor = context.getContentResolver().query(
|
Add contacts-reading plugin (Android side)
Summary:
Add a plugin to the KDE Connect Android application which supports reading the Android contacts databases and sending the requested data as vcards
- Android automatically has support for exporting vcards with all the fields you would expect (phone, email, photo, etc.)
- I add two custom fields, one for the modification timestamp and another for the NAME_RAW_CONTACT_ID so that the contacts can be correlated back to the Android database
This does not (yet) support writing contacts back to the phone nor does it automatically listen to the phone's contacts database to change
BUG: 367999
Test Plan:
Connect the device to the desktop and verify that vcards are created in QStandardPaths::GenericDataLocation / kpeoplevcard". On my system this is ~/.local/share/kpeoplevcard
Create a dummy contact on the device and verify it is synchronized (Currently not automatic, have to disconnect and reconnect or use dbus)
Modify the dummy contact and verify the modifications are synchronized (Currently not automatic, have to disconnect and reconnect or use dbus)
Delete the dummy contact and verify the deletion is synchronized (Currently not automatic, have to disconnect and reconnect or use dbus)
Reviewers: #kde_connect, mtijink, nicolasfella
Reviewed By: #kde_connect, nicolasfella
Subscribers: MatMaul, philipc, kdeconnect, nicolasfella, andyholmes, mtijink
Tags: #kde_connect
Maniphest Tasks: T8283
Differential Revision: https://phabricator.kde.org/D9690
2018-05-30 18:33:23 -06:00
|
|
|
contactsUri,
|
2019-10-27 21:23:52 +00:00
|
|
|
projection,
|
|
|
|
selection,
|
|
|
|
selectionArgs,
|
|
|
|
sortOrder
|
2018-10-30 12:30:21 +01:00
|
|
|
)) {
|
|
|
|
if (contactsCursor != null && contactsCursor.moveToFirst()) {
|
|
|
|
do {
|
2019-10-27 21:23:52 +00:00
|
|
|
Map<String, String> requestedData = new HashMap<>();
|
2018-10-30 12:30:21 +01:00
|
|
|
|
2019-10-27 21:23:52 +00:00
|
|
|
int uIDIndex = contactsCursor.getColumnIndexOrThrow(uID.COLUMN);
|
|
|
|
uID uID = new uID(contactsCursor.getString(uIDIndex));
|
2018-10-30 12:30:21 +01:00
|
|
|
|
|
|
|
// For each column, collect the data from that column
|
2019-10-27 21:23:52 +00:00
|
|
|
for (String column : projection) {
|
2018-10-30 12:30:21 +01:00
|
|
|
int index = contactsCursor.getColumnIndex(column);
|
|
|
|
// Since we might be getting various kinds of data, Object is the best we can do
|
2019-10-27 21:23:52 +00:00
|
|
|
String data;
|
2018-10-30 12:30:21 +01:00
|
|
|
if (index == -1) {
|
|
|
|
// This contact didn't have the requested column? Something is very wrong.
|
|
|
|
// If you are experiencing this, please open a bug report indicating how you got here
|
2019-10-27 21:23:52 +00:00
|
|
|
Log.e(LOG_TAG, "Got a contact which does not have a requested column");
|
Add contacts-reading plugin (Android side)
Summary:
Add a plugin to the KDE Connect Android application which supports reading the Android contacts databases and sending the requested data as vcards
- Android automatically has support for exporting vcards with all the fields you would expect (phone, email, photo, etc.)
- I add two custom fields, one for the modification timestamp and another for the NAME_RAW_CONTACT_ID so that the contacts can be correlated back to the Android database
This does not (yet) support writing contacts back to the phone nor does it automatically listen to the phone's contacts database to change
BUG: 367999
Test Plan:
Connect the device to the desktop and verify that vcards are created in QStandardPaths::GenericDataLocation / kpeoplevcard". On my system this is ~/.local/share/kpeoplevcard
Create a dummy contact on the device and verify it is synchronized (Currently not automatic, have to disconnect and reconnect or use dbus)
Modify the dummy contact and verify the modifications are synchronized (Currently not automatic, have to disconnect and reconnect or use dbus)
Delete the dummy contact and verify the deletion is synchronized (Currently not automatic, have to disconnect and reconnect or use dbus)
Reviewers: #kde_connect, mtijink, nicolasfella
Reviewed By: #kde_connect, nicolasfella
Subscribers: MatMaul, philipc, kdeconnect, nicolasfella, andyholmes, mtijink
Tags: #kde_connect
Maniphest Tasks: T8283
Differential Revision: https://phabricator.kde.org/D9690
2018-05-30 18:33:23 -06:00
|
|
|
continue;
|
2018-10-30 12:30:21 +01:00
|
|
|
}
|
2019-10-27 21:23:52 +00:00
|
|
|
data = contactsCursor.getString(index);
|
2018-10-30 12:30:21 +01:00
|
|
|
|
|
|
|
requestedData.put(column, data);
|
Add contacts-reading plugin (Android side)
Summary:
Add a plugin to the KDE Connect Android application which supports reading the Android contacts databases and sending the requested data as vcards
- Android automatically has support for exporting vcards with all the fields you would expect (phone, email, photo, etc.)
- I add two custom fields, one for the modification timestamp and another for the NAME_RAW_CONTACT_ID so that the contacts can be correlated back to the Android database
This does not (yet) support writing contacts back to the phone nor does it automatically listen to the phone's contacts database to change
BUG: 367999
Test Plan:
Connect the device to the desktop and verify that vcards are created in QStandardPaths::GenericDataLocation / kpeoplevcard". On my system this is ~/.local/share/kpeoplevcard
Create a dummy contact on the device and verify it is synchronized (Currently not automatic, have to disconnect and reconnect or use dbus)
Modify the dummy contact and verify the modifications are synchronized (Currently not automatic, have to disconnect and reconnect or use dbus)
Delete the dummy contact and verify the deletion is synchronized (Currently not automatic, have to disconnect and reconnect or use dbus)
Reviewers: #kde_connect, mtijink, nicolasfella
Reviewed By: #kde_connect, nicolasfella
Subscribers: MatMaul, philipc, kdeconnect, nicolasfella, andyholmes, mtijink
Tags: #kde_connect
Maniphest Tasks: T8283
Differential Revision: https://phabricator.kde.org/D9690
2018-05-30 18:33:23 -06:00
|
|
|
}
|
|
|
|
|
2019-10-27 21:23:52 +00:00
|
|
|
toReturn.put(uID, requestedData);
|
2018-10-30 12:30:21 +01:00
|
|
|
} while (contactsCursor.moveToNext());
|
Add contacts-reading plugin (Android side)
Summary:
Add a plugin to the KDE Connect Android application which supports reading the Android contacts databases and sending the requested data as vcards
- Android automatically has support for exporting vcards with all the fields you would expect (phone, email, photo, etc.)
- I add two custom fields, one for the modification timestamp and another for the NAME_RAW_CONTACT_ID so that the contacts can be correlated back to the Android database
This does not (yet) support writing contacts back to the phone nor does it automatically listen to the phone's contacts database to change
BUG: 367999
Test Plan:
Connect the device to the desktop and verify that vcards are created in QStandardPaths::GenericDataLocation / kpeoplevcard". On my system this is ~/.local/share/kpeoplevcard
Create a dummy contact on the device and verify it is synchronized (Currently not automatic, have to disconnect and reconnect or use dbus)
Modify the dummy contact and verify the modifications are synchronized (Currently not automatic, have to disconnect and reconnect or use dbus)
Delete the dummy contact and verify the deletion is synchronized (Currently not automatic, have to disconnect and reconnect or use dbus)
Reviewers: #kde_connect, mtijink, nicolasfella
Reviewed By: #kde_connect, nicolasfella
Subscribers: MatMaul, philipc, kdeconnect, nicolasfella, andyholmes, mtijink
Tags: #kde_connect
Maniphest Tasks: T8283
Differential Revision: https://phabricator.kde.org/D9690
2018-05-30 18:33:23 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return toReturn;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This is a cheap ripoff of com.android.vcard.VCardBuilder
|
|
|
|
* <p>
|
|
|
|
* Maybe in the future that library will be made public and we can switch to using that!
|
|
|
|
* <p>
|
|
|
|
* The main similarity is the usage of .toString() to produce the finalized VCard and the
|
|
|
|
* usage of .appendLine(String, String) to add stuff to the vcard
|
|
|
|
*/
|
|
|
|
public static class VCardBuilder {
|
2018-10-26 23:53:58 +02:00
|
|
|
static final String VCARD_END = "END:VCARD"; // Written to terminate the vcard
|
|
|
|
static final String VCARD_DATA_SEPARATOR = ":";
|
Add contacts-reading plugin (Android side)
Summary:
Add a plugin to the KDE Connect Android application which supports reading the Android contacts databases and sending the requested data as vcards
- Android automatically has support for exporting vcards with all the fields you would expect (phone, email, photo, etc.)
- I add two custom fields, one for the modification timestamp and another for the NAME_RAW_CONTACT_ID so that the contacts can be correlated back to the Android database
This does not (yet) support writing contacts back to the phone nor does it automatically listen to the phone's contacts database to change
BUG: 367999
Test Plan:
Connect the device to the desktop and verify that vcards are created in QStandardPaths::GenericDataLocation / kpeoplevcard". On my system this is ~/.local/share/kpeoplevcard
Create a dummy contact on the device and verify it is synchronized (Currently not automatic, have to disconnect and reconnect or use dbus)
Modify the dummy contact and verify the modifications are synchronized (Currently not automatic, have to disconnect and reconnect or use dbus)
Delete the dummy contact and verify the deletion is synchronized (Currently not automatic, have to disconnect and reconnect or use dbus)
Reviewers: #kde_connect, mtijink, nicolasfella
Reviewed By: #kde_connect, nicolasfella
Subscribers: MatMaul, philipc, kdeconnect, nicolasfella, andyholmes, mtijink
Tags: #kde_connect
Maniphest Tasks: T8283
Differential Revision: https://phabricator.kde.org/D9690
2018-05-30 18:33:23 -06:00
|
|
|
|
|
|
|
final StringBuilder vcardBody;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Take a partial vcard as a string and make a VCardBuilder
|
|
|
|
*
|
|
|
|
* @param vcard vcard to build upon
|
|
|
|
*/
|
2018-10-26 23:53:58 +02:00
|
|
|
VCardBuilder(String vcard) {
|
Add contacts-reading plugin (Android side)
Summary:
Add a plugin to the KDE Connect Android application which supports reading the Android contacts databases and sending the requested data as vcards
- Android automatically has support for exporting vcards with all the fields you would expect (phone, email, photo, etc.)
- I add two custom fields, one for the modification timestamp and another for the NAME_RAW_CONTACT_ID so that the contacts can be correlated back to the Android database
This does not (yet) support writing contacts back to the phone nor does it automatically listen to the phone's contacts database to change
BUG: 367999
Test Plan:
Connect the device to the desktop and verify that vcards are created in QStandardPaths::GenericDataLocation / kpeoplevcard". On my system this is ~/.local/share/kpeoplevcard
Create a dummy contact on the device and verify it is synchronized (Currently not automatic, have to disconnect and reconnect or use dbus)
Modify the dummy contact and verify the modifications are synchronized (Currently not automatic, have to disconnect and reconnect or use dbus)
Delete the dummy contact and verify the deletion is synchronized (Currently not automatic, have to disconnect and reconnect or use dbus)
Reviewers: #kde_connect, mtijink, nicolasfella
Reviewed By: #kde_connect, nicolasfella
Subscribers: MatMaul, philipc, kdeconnect, nicolasfella, andyholmes, mtijink
Tags: #kde_connect
Maniphest Tasks: T8283
Differential Revision: https://phabricator.kde.org/D9690
2018-05-30 18:33:23 -06:00
|
|
|
// Remove the end tag. We will add it back on in .toString()
|
|
|
|
vcard = vcard.substring(0, vcard.indexOf(VCARD_END));
|
|
|
|
|
|
|
|
vcardBody = new StringBuilder(vcard);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Appends one line with a given property name and value.
|
|
|
|
*/
|
|
|
|
public void appendLine(final String propertyName, final String rawValue) {
|
|
|
|
vcardBody.append(propertyName)
|
|
|
|
.append(VCARD_DATA_SEPARATOR)
|
|
|
|
.append(rawValue)
|
|
|
|
.append("\n");
|
|
|
|
}
|
|
|
|
|
2019-04-19 01:31:19 +02:00
|
|
|
@NonNull
|
Add contacts-reading plugin (Android side)
Summary:
Add a plugin to the KDE Connect Android application which supports reading the Android contacts databases and sending the requested data as vcards
- Android automatically has support for exporting vcards with all the fields you would expect (phone, email, photo, etc.)
- I add two custom fields, one for the modification timestamp and another for the NAME_RAW_CONTACT_ID so that the contacts can be correlated back to the Android database
This does not (yet) support writing contacts back to the phone nor does it automatically listen to the phone's contacts database to change
BUG: 367999
Test Plan:
Connect the device to the desktop and verify that vcards are created in QStandardPaths::GenericDataLocation / kpeoplevcard". On my system this is ~/.local/share/kpeoplevcard
Create a dummy contact on the device and verify it is synchronized (Currently not automatic, have to disconnect and reconnect or use dbus)
Modify the dummy contact and verify the modifications are synchronized (Currently not automatic, have to disconnect and reconnect or use dbus)
Delete the dummy contact and verify the deletion is synchronized (Currently not automatic, have to disconnect and reconnect or use dbus)
Reviewers: #kde_connect, mtijink, nicolasfella
Reviewed By: #kde_connect, nicolasfella
Subscribers: MatMaul, philipc, kdeconnect, nicolasfella, andyholmes, mtijink
Tags: #kde_connect
Maniphest Tasks: T8283
Differential Revision: https://phabricator.kde.org/D9690
2018-05-30 18:33:23 -06:00
|
|
|
public String toString() {
|
|
|
|
return vcardBody.toString() + VCARD_END;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Essentially a typedef of the type used for a unique identifier
|
|
|
|
*/
|
|
|
|
public static class uID {
|
|
|
|
/**
|
|
|
|
* We use the LOOKUP_KEY column of the Contacts table as a unique ID, since that's what it's
|
|
|
|
* for
|
|
|
|
*/
|
|
|
|
final String contactLookupKey;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Which Contacts column this uID is pulled from
|
|
|
|
*/
|
|
|
|
static final String COLUMN = ContactsContract.Contacts.LOOKUP_KEY;
|
|
|
|
|
|
|
|
public uID(String lookupKey) {
|
2019-04-19 01:31:19 +02:00
|
|
|
|
|
|
|
if (lookupKey == null)
|
|
|
|
throw new IllegalArgumentException("lookUpKey should not be null");
|
|
|
|
|
Add contacts-reading plugin (Android side)
Summary:
Add a plugin to the KDE Connect Android application which supports reading the Android contacts databases and sending the requested data as vcards
- Android automatically has support for exporting vcards with all the fields you would expect (phone, email, photo, etc.)
- I add two custom fields, one for the modification timestamp and another for the NAME_RAW_CONTACT_ID so that the contacts can be correlated back to the Android database
This does not (yet) support writing contacts back to the phone nor does it automatically listen to the phone's contacts database to change
BUG: 367999
Test Plan:
Connect the device to the desktop and verify that vcards are created in QStandardPaths::GenericDataLocation / kpeoplevcard". On my system this is ~/.local/share/kpeoplevcard
Create a dummy contact on the device and verify it is synchronized (Currently not automatic, have to disconnect and reconnect or use dbus)
Modify the dummy contact and verify the modifications are synchronized (Currently not automatic, have to disconnect and reconnect or use dbus)
Delete the dummy contact and verify the deletion is synchronized (Currently not automatic, have to disconnect and reconnect or use dbus)
Reviewers: #kde_connect, mtijink, nicolasfella
Reviewed By: #kde_connect, nicolasfella
Subscribers: MatMaul, philipc, kdeconnect, nicolasfella, andyholmes, mtijink
Tags: #kde_connect
Maniphest Tasks: T8283
Differential Revision: https://phabricator.kde.org/D9690
2018-05-30 18:33:23 -06:00
|
|
|
contactLookupKey = lookupKey;
|
|
|
|
}
|
|
|
|
|
2019-04-19 01:31:19 +02:00
|
|
|
@NonNull
|
Add contacts-reading plugin (Android side)
Summary:
Add a plugin to the KDE Connect Android application which supports reading the Android contacts databases and sending the requested data as vcards
- Android automatically has support for exporting vcards with all the fields you would expect (phone, email, photo, etc.)
- I add two custom fields, one for the modification timestamp and another for the NAME_RAW_CONTACT_ID so that the contacts can be correlated back to the Android database
This does not (yet) support writing contacts back to the phone nor does it automatically listen to the phone's contacts database to change
BUG: 367999
Test Plan:
Connect the device to the desktop and verify that vcards are created in QStandardPaths::GenericDataLocation / kpeoplevcard". On my system this is ~/.local/share/kpeoplevcard
Create a dummy contact on the device and verify it is synchronized (Currently not automatic, have to disconnect and reconnect or use dbus)
Modify the dummy contact and verify the modifications are synchronized (Currently not automatic, have to disconnect and reconnect or use dbus)
Delete the dummy contact and verify the deletion is synchronized (Currently not automatic, have to disconnect and reconnect or use dbus)
Reviewers: #kde_connect, mtijink, nicolasfella
Reviewed By: #kde_connect, nicolasfella
Subscribers: MatMaul, philipc, kdeconnect, nicolasfella, andyholmes, mtijink
Tags: #kde_connect
Maniphest Tasks: T8283
Differential Revision: https://phabricator.kde.org/D9690
2018-05-30 18:33:23 -06:00
|
|
|
public String toString() {
|
|
|
|
return this.contactLookupKey;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int hashCode() {
|
|
|
|
return contactLookupKey.hashCode();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean equals(Object other) {
|
|
|
|
if (other instanceof uID) {
|
|
|
|
return contactLookupKey.equals(((uID) other).contactLookupKey);
|
|
|
|
}
|
|
|
|
return contactLookupKey.equals(other);
|
|
|
|
}
|
|
|
|
}
|
2019-10-27 21:23:52 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Exception to indicate that a specified contact was not found
|
|
|
|
*/
|
|
|
|
public static class ContactNotFoundException extends Exception {
|
|
|
|
public ContactNotFoundException(uID contactID) {
|
|
|
|
super("Unable to find contact with ID " + contactID);
|
|
|
|
}
|
|
|
|
|
|
|
|
public ContactNotFoundException(String message) {
|
|
|
|
super(message);
|
|
|
|
}
|
|
|
|
}
|
Add contacts-reading plugin (Android side)
Summary:
Add a plugin to the KDE Connect Android application which supports reading the Android contacts databases and sending the requested data as vcards
- Android automatically has support for exporting vcards with all the fields you would expect (phone, email, photo, etc.)
- I add two custom fields, one for the modification timestamp and another for the NAME_RAW_CONTACT_ID so that the contacts can be correlated back to the Android database
This does not (yet) support writing contacts back to the phone nor does it automatically listen to the phone's contacts database to change
BUG: 367999
Test Plan:
Connect the device to the desktop and verify that vcards are created in QStandardPaths::GenericDataLocation / kpeoplevcard". On my system this is ~/.local/share/kpeoplevcard
Create a dummy contact on the device and verify it is synchronized (Currently not automatic, have to disconnect and reconnect or use dbus)
Modify the dummy contact and verify the modifications are synchronized (Currently not automatic, have to disconnect and reconnect or use dbus)
Delete the dummy contact and verify the deletion is synchronized (Currently not automatic, have to disconnect and reconnect or use dbus)
Reviewers: #kde_connect, mtijink, nicolasfella
Reviewed By: #kde_connect, nicolasfella
Subscribers: MatMaul, philipc, kdeconnect, nicolasfella, andyholmes, mtijink
Tags: #kde_connect
Maniphest Tasks: T8283
Differential Revision: https://phabricator.kde.org/D9690
2018-05-30 18:33:23 -06:00
|
|
|
}
|