Fixed ANR because of presence check in async task

This commit is contained in:
Sylvain Berfini 2020-05-19 20:52:58 +02:00
parent 265b000758
commit de7a2877c8
3 changed files with 36 additions and 30 deletions

View file

@ -24,9 +24,11 @@ import android.os.AsyncTask;
public class AsyncContactPresence extends AsyncTask<Void, AndroidContact, Void> {
private LinphoneContact mLinphoneContact;
private String mAlias;
public AsyncContactPresence(LinphoneContact linphoneContact) {
public AsyncContactPresence(LinphoneContact linphoneContact, String alias) {
mLinphoneContact = linphoneContact;
mAlias = alias;
}
@Override
@ -36,7 +38,7 @@ public class AsyncContactPresence extends AsyncTask<Void, AndroidContact, Void>
@Override
protected Void doInBackground(Void... voids) {
mLinphoneContact.updateNativeContactWithPresenceInfo();
mLinphoneContact.addPresenceInfoToNativeContact(mAlias);
return null;
}
}

View file

@ -51,6 +51,8 @@ import org.linphone.core.Friend;
import org.linphone.core.FriendList;
import org.linphone.core.FriendListListener;
import org.linphone.core.MagicSearch;
import org.linphone.core.PresenceBasicStatus;
import org.linphone.core.PresenceModel;
import org.linphone.core.ProxyConfig;
import org.linphone.core.tools.Log;
import org.linphone.settings.LinphonePreferences;
@ -485,14 +487,33 @@ public class ContactsManager extends ContentObserver
.getApplicationContext()
.getResources()
.getBoolean(R.bool.use_linphone_tag)) {
// Inserting Linphone information in Android contact if the parameter is enabled
if (LinphonePreferences.instance()
.isPresenceStorageInNativeAndroidContactEnabled()) {
// add presence to native contact
AsyncContactPresence asyncContactPresence = new AsyncContactPresence(contact);
// Inserting information in Android contact if the parameter is enabled
for (LinphoneNumberOrAddress noa : contact.getNumbersOrAddresses()) {
if (noa.isSIPAddress()) {
// We are only interested in phone numbers
continue;
}
String value = noa.getValue();
if (value == null || value.isEmpty()) {
continue;
}
// Test presence of the value
PresenceModel pm = contact.getFriend().getPresenceModelForUriOrTel(value);
// If presence is not null
if (pm != null
&& pm.getBasicStatus() != null
&& pm.getBasicStatus().equals(PresenceBasicStatus.Open)) {
// Add presence to native contact
AsyncContactPresence asyncContactPresence =
new AsyncContactPresence(contact, value);
asyncContactPresence.execute();
}
}
}
}
if (!mSipContacts.contains(contact)) {
mSipContacts.add(contact);

View file

@ -618,35 +618,18 @@ public class LinphoneContact extends AndroidContact
}
}
public synchronized void updateNativeContactWithPresenceInfo() {
Log.d("[Contact] Trying to update native contact with presence information");
public synchronized void addPresenceInfoToNativeContact(String value) {
Log.d(
"[Contact] Trying to update native contact with presence information for phone number ",
value);
// Creation of the raw contact with the presence information (tablet)
createRawLinphoneContactFromExistingAndroidContactIfNeeded();
for (LinphoneNumberOrAddress noa : getNumbersOrAddresses()) {
if (noa.isSIPAddress()) {
// We are only interested in SIP addresses
continue;
}
String value = noa.getValue();
if (value == null || value.isEmpty()) {
return;
}
// Test presence of the value
PresenceModel pm = getFriend().getPresenceModelForUriOrTel(value);
// If presence is not null
if (pm != null
&& pm.getBasicStatus() != null
&& pm.getBasicStatus().equals(PresenceBasicStatus.Open)) {
Log.d("[Contact] Found presence information for phone number " + value);
if (!isLinphoneAddressMimeEntryAlreadyExisting(value)) {
// Do the action on the contact only once if it has not been done yet
updateNativeContactWithPresenceInfo(value);
}
}
}
saveChangesCommited();
}