Skip to content

Commit

Permalink
avatar URL must be absolute in contacts synching
Browse files Browse the repository at this point in the history
  • Loading branch information
or-else committed Nov 9, 2022
1 parent d46c5fa commit 323035f
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 21 deletions.
8 changes: 4 additions & 4 deletions app/src/main/java/co/tinode/tindroid/ChatsActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -272,14 +272,14 @@ public void onMetaSub(final Subscription<VxCard, PrivateType> sub) {
return;
}

Tinode tinode = Cache.getTinode();
if (mAccount == null) {
mAccount = Utils.getSavedAccount(AccountManager.get(ChatsActivity.this),
Cache.getTinode().getMyId());
mAccount = Utils.getSavedAccount(AccountManager.get(ChatsActivity.this), tinode.getMyId());
}
if (Topic.isP2PType(sub.topic)) {
ContactsManager.processContact(ChatsActivity.this,
ChatsActivity.this.getContentResolver(),
mAccount, sub.pub, null, sub.getUnique(), sub.deleted != null,
ChatsActivity.this.getContentResolver(), mAccount, tinode,
sub.pub, null, sub.getUnique(), sub.deleted != null,
null, false);
}
}
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/co/tinode/tindroid/MessageActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -932,7 +932,7 @@ public void onSubsUpdated() {
if (UiUtils.isPermissionGranted(context, Manifest.permission.WRITE_CONTACTS)) {
Account acc = Utils.getSavedAccount(AccountManager.get(context), Cache.getTinode().getMyId());
if (acc != null) {
ContactsManager.updateContacts(context, acc, mTopic.getSubscriptions(),
ContactsManager.updateContacts(context, acc, Cache.getTinode(), mTopic.getSubscriptions(),
null, false);
}
}
Expand Down
5 changes: 3 additions & 2 deletions app/src/main/java/co/tinode/tindroid/UiUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -399,8 +399,9 @@ static void onContactsPermissionsGranted(Activity activity) {
if (acc == null) {
return;
}
Collection<ComTopic<VxCard>> topics = Cache.getTinode().getFilteredTopics(Topic::isP2PType);
ContactsManager.updateContacts(activity, acc, topics);
Tinode tinode = Cache.getTinode();
Collection<ComTopic<VxCard>> topics = tinode.getFilteredTopics(Topic::isP2PType);
ContactsManager.updateContacts(activity, acc, tinode, topics);
TindroidApp.startWatchingContacts(activity, acc);
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import co.tinode.tindroid.R;
import co.tinode.tindroid.UiUtils;
import co.tinode.tinodesdk.Tinode;

/**
* Helper class for storing data in the platform content providers.
Expand Down Expand Up @@ -217,12 +218,12 @@ ContactOperations addIm(final String tinode_id) {
* @param avatar avatar image serialized into byte array
* @return instance of ContactOperations
*/
ContactOperations addAvatar(byte[] avatar, final String ref, final String mimeType) {
ContactOperations addAvatar(byte[] avatar, final Tinode tinode, final String ref, final String mimeType) {
mValues.clear();
if (ref != null) {
try {
avatar = UiUtils.bitmapToBytes(Picasso.get()
.load(Uri.decode(ref))
.load(tinode.toAbsoluteURL(ref).toString())
.resize(UiUtils.MAX_AVATAR_SIZE, UiUtils.MAX_AVATAR_SIZE).centerCrop()
.get(), mimeType);
} catch (IOException ex) {
Expand Down
22 changes: 12 additions & 10 deletions app/src/main/java/co/tinode/tindroid/account/ContactsManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import co.tinode.tindroid.R;
import co.tinode.tindroid.media.VxCard;
import co.tinode.tinodesdk.ComTopic;
import co.tinode.tinodesdk.Tinode;
import co.tinode.tinodesdk.model.Subscription;
import co.tinode.tinodesdk.model.TheCard;

Expand All @@ -46,6 +47,7 @@ public class ContactsManager {
* sync request.
*/
public static synchronized <K> Date updateContacts(Context context, Account account,
Tinode tinode,
Collection<Subscription<VxCard, K>> subscriptions,
Date lastSyncMarker,
// It's a false positive.
Expand All @@ -60,7 +62,7 @@ public static synchronized <K> Date updateContacts(Context context, Account acco
currentSyncMarker = sub.updated;

// Send updated contact to database.
processContact(context, resolver, account,
processContact(context, resolver, account, tinode,
sub.pub, sub.priv, sub.user, sub.deleted != null,
batchOperation, isSyncContext);

Expand All @@ -85,14 +87,14 @@ public static synchronized <K> Date updateContacts(Context context, Account acco
* @param account The username for the account
* @param topics The list of contacts to update
*/
public static synchronized void updateContacts(Context context, Account account,
public static synchronized void updateContacts(Context context, Account account, Tinode tinode,
Collection<ComTopic<VxCard>> topics) {
final ContentResolver resolver = context.getContentResolver();
final BatchOperation batchOperation = new BatchOperation(resolver);

for (ComTopic<VxCard> topic : topics) {
// Save topic as contact to database.
processContact(context, resolver, account,
processContact(context, resolver, account, tinode,
topic.getPub(), null, topic.getName(), false,
batchOperation, false);

Expand Down Expand Up @@ -122,7 +124,7 @@ public static synchronized void updateContacts(Context context, Account account,
*/
public static synchronized void processContact(Context context,
ContentResolver resolver,
Account account,
Account account, Tinode tinode,
VxCard pub, Object priv,
String userId, boolean deleted,
BatchOperation batchOperation,
Expand All @@ -145,7 +147,7 @@ public static synchronized void processContact(Context context,
if (rawContactId > 0) {
// Contact already exists
if (pub != null) {
updateContact(context, resolver, pub, userId, rawContactId,
updateContact(context, resolver, tinode, pub, userId, rawContactId,
batchOperation, isSyncContext);
}
} else {
Expand All @@ -155,7 +157,7 @@ public static synchronized void processContact(Context context,
pub.fn = context.getString(R.string.default_contact_name, userId);
}

addContact(context, account, pub, userId, batchOperation, isSyncContext);
addContact(context, account, tinode, pub, userId, batchOperation, isSyncContext);
}
}

Expand All @@ -178,7 +180,7 @@ public static synchronized void processContact(Context context,
* into a single provider call
*/
private static void addContact(Context context, Account account,
VxCard pub, String userId,
Tinode tinode, VxCard pub, String userId,
BatchOperation batchOperation, boolean isSyncContext) {

// Initiate adding data to contacts provider.
Expand All @@ -189,7 +191,7 @@ private static void addContact(Context context, Account account,

contactOp.addName(pub.fn, pub.n != null ? pub.n.given : null,
pub.n != null ? pub.n.surname : null)
.addAvatar(pub.getPhotoBits(), pub.getPhotoRef(), pub.getPhotoMimeType());
.addAvatar(pub.getPhotoBits(), tinode, pub.getPhotoRef(), pub.getPhotoMimeType());

if (pub.email != null) {
for (TheCard.Contact email : pub.email) {
Expand Down Expand Up @@ -229,7 +231,7 @@ private static void addContact(Context context, Account account,
* into a single provider call
*/
private static void updateContact(Context context, ContentResolver resolver,
VxCard pub, String unique,
Tinode tinode, VxCard pub, String unique,
long rawContactId, BatchOperation batchOperation, boolean isSyncContext) {

boolean existingCellPhone = false;
Expand Down Expand Up @@ -316,7 +318,7 @@ private static void updateContact(Context context, ContentResolver resolver,
}
// Add the avatar if we didn't update the existing avatar
if (!existingAvatar) {
contactOp.addAvatar(pub.getPhotoBits(), pub.getPhotoRef(), pub.getPhotoMimeType());
contactOp.addAvatar(pub.getPhotoBits(), tinode, pub.getPhotoRef(), pub.getPhotoMimeType());
}

// If we don't have a status profile, then create one. This could
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -285,8 +285,8 @@ public void onPerformSync(final Account account, final Bundle extras, String aut
updated.add(sub);
}
}
newSyncMarker = ContactsManager.updateContacts(mContext, account, updated,
lastSyncMarker, true);
newSyncMarker = ContactsManager.updateContacts(mContext, account, tinode,
updated, lastSyncMarker, true);
}
}
tinode.disconnect(true);
Expand Down

0 comments on commit 323035f

Please sign in to comment.