Contact creation is almost OK, still missing visible linphone action in native app

This commit is contained in:
Sylvain Berfini 2018-12-07 16:11:51 +01:00
parent 998640fadd
commit ce6e5fd714
3 changed files with 126 additions and 71 deletions

View file

@ -42,13 +42,13 @@ import org.linphone.mediastream.Log;
class AndroidContact implements Serializable { class AndroidContact implements Serializable {
protected String mAndroidId, mAndroidRawId, mAndroidLookupKey; protected String mAndroidId, mAndroidRawId, mAndroidLookupKey;
protected boolean isAndroidIdLinphone; protected boolean isAndroidRawIdLinphone;
private transient ArrayList<ContentProviderOperation> mChangesToCommit; private transient ArrayList<ContentProviderOperation> mChangesToCommit;
protected AndroidContact() { protected AndroidContact() {
mChangesToCommit = new ArrayList<>(); mChangesToCommit = new ArrayList<>();
isAndroidIdLinphone = false; isAndroidRawIdLinphone = false;
} }
protected String getAndroidId() { protected String getAndroidId() {
@ -88,8 +88,9 @@ class AndroidContact implements Serializable {
ContentProviderResult[] results = ContentProviderResult[] results =
contentResolver.applyBatch(ContactsContract.AUTHORITY, mChangesToCommit); contentResolver.applyBatch(ContactsContract.AUTHORITY, mChangesToCommit);
if (results != null && results.length > 0 && results[0] != null) { if (results != null && results.length > 0 && results[0] != null) {
mAndroidRawId = String.valueOf(ContentUris.parseId(results[0].uri)); String rawId = String.valueOf(ContentUris.parseId(results[0].uri));
Log.i("[Contact] Contact created with RAW ID " + mAndroidRawId); if (mAndroidId == null) {
Log.i("[Contact] Contact created with RAW ID " + rawId);
final String[] projection = final String[] projection =
new String[] {ContactsContract.RawContacts.CONTACT_ID}; new String[] {ContactsContract.RawContacts.CONTACT_ID};
@ -102,6 +103,18 @@ class AndroidContact implements Serializable {
cursor.close(); cursor.close();
Log.i("[Contact] Contact created with ID " + mAndroidId); Log.i("[Contact] Contact created with ID " + mAndroidId);
} }
} else {
if (rawId.equals(mAndroidRawId)) {
Log.i("[Contact] Contact with RAW ID " + rawId + " updated");
} else {
Log.i(
"[Contact] Linphone RAW ID "
+ rawId
+ " created from existing RAW ID "
+ mAndroidRawId);
}
}
mAndroidRawId = rawId;
} }
} catch (Exception e) { } catch (Exception e) {
Log.e("[Contact] Exception while saving changes: " + e); Log.e("[Contact] Exception while saving changes: " + e);
@ -131,7 +144,17 @@ class AndroidContact implements Serializable {
RawContacts.AGGREGATION_MODE, RawContacts.AGGREGATION_MODE,
RawContacts.AGGREGATION_MODE_DEFAULT) RawContacts.AGGREGATION_MODE_DEFAULT)
.build()); .build());
isAndroidIdLinphone = true; isAndroidRawIdLinphone = true;
} else {
Log.i("[Contact] Creating contact using default account type");
addChangesToCommit(
ContentProviderOperation.newInsert(RawContacts.CONTENT_URI)
.withValue(RawContacts.ACCOUNT_TYPE, null)
.withValue(RawContacts.ACCOUNT_NAME, null)
.withValue(
RawContacts.AGGREGATION_MODE,
RawContacts.AGGREGATION_MODE_DEFAULT)
.build());
} }
} }
@ -449,12 +472,64 @@ class AndroidContact implements Serializable {
.getContext() .getContext()
.getResources() .getResources()
.getBoolean(R.bool.use_linphone_tag)) { .getBoolean(R.bool.use_linphone_tag)) {
// TODO if (mAndroidId != null && (mAndroidRawId == null || !isAndroidRawIdLinphone)) {
/*if (mLinphoneAndroidContactId == null && findLinphoneRawContactId() == null) { String linphoneRawId = findLinphoneRawContactId();
if (linphoneRawId == null) {
Log.i("[Contact] Linphone RAW ID not found for contact " + mAndroidId);
createRawLinphoneContactFromExistingAndroidContact(fullName); createRawLinphoneContactFromExistingAndroidContact(fullName);
isAndroidIdLinphone = true; } else {
}*/ Log.i(
"[Contact] Linphone RAW ID found for contact "
+ mAndroidId
+ " : "
+ linphoneRawId);
mAndroidRawId = linphoneRawId;
} }
isAndroidRawIdLinphone = true;
}
}
}
private void createRawLinphoneContactFromExistingAndroidContact(String fullName) {
addChangesToCommit(
ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI)
.withValue(
ContactsContract.RawContacts.ACCOUNT_TYPE,
ContactsManager.getInstance().getString(R.string.sync_account_type))
.withValue(
ContactsContract.RawContacts.ACCOUNT_NAME,
ContactsManager.getInstance().getString(R.string.sync_account_name))
.withValue(
ContactsContract.RawContacts.AGGREGATION_MODE,
ContactsContract.RawContacts.AGGREGATION_MODE_DEFAULT)
.build());
addChangesToCommit(
ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
.withValue(
ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
.withValue(
ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME,
fullName)
.build());
addChangesToCommit(
ContentProviderOperation.newUpdate(
ContactsContract.AggregationExceptions.CONTENT_URI)
.withValue(
ContactsContract.AggregationExceptions.TYPE,
ContactsContract.AggregationExceptions.TYPE_KEEP_TOGETHER)
.withValue(
ContactsContract.AggregationExceptions.RAW_CONTACT_ID1,
mAndroidRawId)
.withValueBackReference(
ContactsContract.AggregationExceptions.RAW_CONTACT_ID2, 0)
.build());
Log.i("[Contact] Creating linphone contact");
saveChangesCommited();
} }
private String findLinphoneRawContactId() { private String findLinphoneRawContactId() {
@ -485,54 +560,4 @@ class AndroidContact implements Serializable {
} }
return result; return result;
} }
/*private void createRawLinphoneContactFromExistingAndroidContact(String fullName) {
ArrayList<ContentProviderOperation> batch = new ArrayList<>();
batch.add(
ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI)
.withValue(
ContactsContract.RawContacts.ACCOUNT_TYPE,
ContactsManager.getInstance().getString(R.string.sync_account_type))
.withValue(
ContactsContract.RawContacts.ACCOUNT_NAME,
ContactsManager.getInstance().getString(R.string.sync_account_name))
.withValue(
ContactsContract.RawContacts.AGGREGATION_MODE,
ContactsContract.RawContacts.AGGREGATION_MODE_DEFAULT)
.build());
batch.add(
ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
.withValue(
ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
.withValue(
ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME,
fullName)
.build());
batch.add(
ContentProviderOperation.newUpdate(
ContactsContract.AggregationExceptions.CONTENT_URI)
.withValue(
ContactsContract.AggregationExceptions.TYPE,
ContactsContract.AggregationExceptions.TYPE_KEEP_TOGETHER)
.withValue(
ContactsContract.AggregationExceptions.RAW_CONTACT_ID1,
mAndroidRawId)
.withValueBackReference(
ContactsContract.AggregationExceptions.RAW_CONTACT_ID2, 0)
.build());
Log.i("[Contact] Creating linphone contact");
try {
LinphoneService.instance()
.getContentResolver()
.applyBatch(ContactsContract.AUTHORITY, batch);
} catch (Exception e) {
Log.e(e);
}
}*/
} }

View file

@ -24,8 +24,10 @@ import static android.os.AsyncTask.THREAD_POOL_EXECUTOR;
import android.accounts.Account; import android.accounts.Account;
import android.accounts.AccountManager; import android.accounts.AccountManager;
import android.app.Activity; import android.app.Activity;
import android.content.ContentProviderClient;
import android.content.ContentProviderOperation; import android.content.ContentProviderOperation;
import android.content.ContentResolver; import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
@ -34,6 +36,7 @@ import android.database.Cursor;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.graphics.BitmapFactory; import android.graphics.BitmapFactory;
import android.net.Uri; import android.net.Uri;
import android.os.RemoteException;
import android.provider.ContactsContract; import android.provider.ContactsContract;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
@ -262,6 +265,31 @@ public class ContactsManager extends ContentObserver implements FriendListListen
} }
} }
private void makeContactAccountVisible() {
ContentProviderClient client =
mContext.getContentResolver()
.acquireContentProviderClient(ContactsContract.AUTHORITY_URI);
ContentValues values = new ContentValues();
values.put(
ContactsContract.Settings.ACCOUNT_NAME,
mContext.getString(R.string.sync_account_name));
values.put(
ContactsContract.Settings.ACCOUNT_TYPE,
mContext.getString(R.string.sync_account_type));
values.put(ContactsContract.Settings.UNGROUPED_VISIBLE, true);
try {
client.insert(
ContactsContract.Settings.CONTENT_URI
.buildUpon()
.appendQueryParameter(ContactsContract.CALLER_IS_SYNCADAPTER, "true")
.build(),
values);
Log.i("[Contacts Manager] Contacts account made visible");
} catch (RemoteException e) {
Log.e("[Contacts Manager] Couldn't make contacts account visible: " + e);
}
}
public void initializeSyncAccount(Activity activity) { public void initializeSyncAccount(Activity activity) {
initializeContactManager(activity); initializeContactManager(activity);
AccountManager accountManager = AccountManager accountManager =
@ -277,10 +305,12 @@ public class ContactsManager extends ContentObserver implements FriendListListen
mContext.getString(R.string.sync_account_type)); mContext.getString(R.string.sync_account_type));
try { try {
accountManager.addAccountExplicitly(newAccount, null, null); accountManager.addAccountExplicitly(newAccount, null, null);
Log.i("[Contacts Manager] Contact account added");
} catch (Exception e) { } catch (Exception e) {
Log.e("[Contacts Manager] Couldn't initialize sync account: " + e); Log.e("[Contacts Manager] Couldn't initialize sync account: " + e);
} }
} }
makeContactAccountVisible();
} }
public synchronized LinphoneContact findContactFromAddress(Address address) { public synchronized LinphoneContact findContactFromAddress(Address address) {

View file

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<sync-adapter xmlns:android="http://schemas.android.com/apk/res/android" <sync-adapter xmlns:android="http://schemas.android.com/apk/res/android"
android:accountType="@string/sync_account_name" android:accountType="@string/sync_account_type"
android:contentAuthority="com.android.contacts" android:contentAuthority="com.android.contacts"
android:supportsUploading="false" android:supportsUploading="false"
android:userVisible="false" /> android:userVisible="false" />