Few changes for contact manager
This commit is contained in:
parent
1de819e33a
commit
0e6b94241f
3 changed files with 55 additions and 49 deletions
|
@ -31,8 +31,6 @@ import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
import org.linphone.core.LinphoneAddress;
|
import org.linphone.core.LinphoneAddress;
|
||||||
import org.linphone.core.LinphoneCore;
|
import org.linphone.core.LinphoneCore;
|
||||||
import org.linphone.core.LinphoneCoreException;
|
|
||||||
import org.linphone.core.LinphoneCoreFactory;
|
|
||||||
import org.linphone.core.LinphoneFriend;
|
import org.linphone.core.LinphoneFriend;
|
||||||
import org.linphone.core.LinphoneFriendImpl;
|
import org.linphone.core.LinphoneFriendImpl;
|
||||||
import org.linphone.core.LinphoneProxyConfig;
|
import org.linphone.core.LinphoneProxyConfig;
|
||||||
|
@ -51,7 +49,6 @@ import android.graphics.Bitmap;
|
||||||
import android.graphics.BitmapFactory;
|
import android.graphics.BitmapFactory;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
import android.os.Handler;
|
import android.os.Handler;
|
||||||
import android.os.Message;
|
|
||||||
import android.provider.ContactsContract;
|
import android.provider.ContactsContract;
|
||||||
import android.provider.ContactsContract.CommonDataKinds;
|
import android.provider.ContactsContract.CommonDataKinds;
|
||||||
import android.provider.ContactsContract.Data;
|
import android.provider.ContactsContract.Data;
|
||||||
|
@ -64,11 +61,12 @@ public class ContactsManager extends ContentObserver {
|
||||||
private static ContactsManager instance;
|
private static ContactsManager instance;
|
||||||
|
|
||||||
private List<LinphoneContact> contacts, sipContacts;
|
private List<LinphoneContact> contacts, sipContacts;
|
||||||
private boolean preferLinphoneContacts = false, isContactPresenceDisabled = true, hasContactAccess = false;
|
private boolean preferLinphoneContacts = false, isContactPresenceDisabled = true;
|
||||||
private ContentResolver contentResolver;
|
private ContentResolver contentResolver;
|
||||||
private Context context;
|
private Context context;
|
||||||
private HashMap<String, LinphoneContact> androidContactsCache;
|
private HashMap<String, LinphoneContact> androidContactsCache;
|
||||||
private Bitmap defaultAvatar;
|
private Bitmap defaultAvatar;
|
||||||
|
private Handler handler;
|
||||||
|
|
||||||
private static ArrayList<ContactsUpdatedListener> contactsUpdatedListeners;
|
private static ArrayList<ContactsUpdatedListener> contactsUpdatedListeners;
|
||||||
public static void addContactsListener(ContactsUpdatedListener listener) {
|
public static void addContactsListener(ContactsUpdatedListener listener) {
|
||||||
|
@ -78,15 +76,9 @@ public class ContactsManager extends ContentObserver {
|
||||||
contactsUpdatedListeners.remove(listener);
|
contactsUpdatedListeners.remove(listener);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Handler handler = new Handler() {
|
|
||||||
@Override
|
|
||||||
public void handleMessage (Message msg) {
|
|
||||||
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
private ContactsManager(Handler handler) {
|
private ContactsManager(Handler handler) {
|
||||||
super(handler);
|
super(handler);
|
||||||
|
this.handler = handler;
|
||||||
defaultAvatar = BitmapFactory.decodeResource(LinphoneService.instance().getResources(), R.drawable.avatar);
|
defaultAvatar = BitmapFactory.decodeResource(LinphoneService.instance().getResources(), R.drawable.avatar);
|
||||||
androidContactsCache = new HashMap<String, LinphoneContact>();
|
androidContactsCache = new HashMap<String, LinphoneContact>();
|
||||||
contactsUpdatedListeners = new ArrayList<ContactsUpdatedListener>();
|
contactsUpdatedListeners = new ArrayList<ContactsUpdatedListener>();
|
||||||
|
@ -122,7 +114,7 @@ public class ContactsManager extends ContentObserver {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static final ContactsManager getInstance() {
|
public static final ContactsManager getInstance() {
|
||||||
if (instance == null) instance = new ContactsManager(handler);
|
if (instance == null) instance = new ContactsManager(LinphoneService.instance().mHandler);
|
||||||
return instance;
|
return instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -173,7 +165,6 @@ public class ContactsManager extends ContentObserver {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void enableContactsAccess() {
|
public void enableContactsAccess() {
|
||||||
hasContactAccess = true;
|
|
||||||
LinphonePreferences.instance().disableFriendsStorage();
|
LinphonePreferences.instance().disableFriendsStorage();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -266,11 +257,19 @@ public class ContactsManager extends ContentObserver {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public synchronized void fetchContactsSync() {
|
public void fetchContactsAsync() {
|
||||||
|
handler.post(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
fetchContactsSync();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private synchronized void fetchContactsSync() {
|
||||||
List<LinphoneContact> contacts = new ArrayList<LinphoneContact>();
|
List<LinphoneContact> contacts = new ArrayList<LinphoneContact>();
|
||||||
List<LinphoneContact> sipContacts = new ArrayList<LinphoneContact>();
|
List<LinphoneContact> sipContacts = new ArrayList<LinphoneContact>();
|
||||||
Date contactsTime = new Date();
|
Date contactsTime = new Date();
|
||||||
androidContactsCache.clear();
|
|
||||||
|
|
||||||
LinphoneCore lc = LinphoneManager.getLcIfManagerNotDestroyedOrNull();
|
LinphoneCore lc = LinphoneManager.getLcIfManagerNotDestroyedOrNull();
|
||||||
if (lc != null) {
|
if (lc != null) {
|
||||||
|
@ -299,7 +298,15 @@ public class ContactsManager extends ContentObserver {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
long timeElapsed = (new Date()).getTime() - contactsTime.getTime();
|
||||||
|
String time = String.format("%02d:%02d",
|
||||||
|
TimeUnit.MILLISECONDS.toMinutes(timeElapsed),
|
||||||
|
TimeUnit.MILLISECONDS.toSeconds(timeElapsed) -
|
||||||
|
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(timeElapsed)));
|
||||||
|
Log.i("[ContactsManager] Step 0 for " + contacts.size() + " contacts: " + time + " elapsed since starting");
|
||||||
|
|
||||||
if (hasContactsAccess()) {
|
if (hasContactsAccess()) {
|
||||||
|
androidContactsCache.clear();
|
||||||
List<String> nativeIds = new ArrayList<String>();
|
List<String> nativeIds = new ArrayList<String>();
|
||||||
Cursor c = getContactsCursor(contentResolver);
|
Cursor c = getContactsCursor(contentResolver);
|
||||||
if (c != null) {
|
if (c != null) {
|
||||||
|
@ -351,19 +358,15 @@ public class ContactsManager extends ContentObserver {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
nativeIds.clear();
|
nativeIds.clear();
|
||||||
} else {
|
|
||||||
Log.w("[Permission] Read contacts permission wasn't granted, only fetch LinphoneFriends");
|
|
||||||
}
|
|
||||||
|
|
||||||
long timeElapsed = (new Date()).getTime() - contactsTime.getTime();
|
timeElapsed = (new Date()).getTime() - contactsTime.getTime();
|
||||||
String time = String.format("%02d:%02d",
|
time = String.format("%02d:%02d",
|
||||||
TimeUnit.MILLISECONDS.toMinutes(timeElapsed),
|
TimeUnit.MILLISECONDS.toMinutes(timeElapsed),
|
||||||
TimeUnit.MILLISECONDS.toSeconds(timeElapsed) -
|
TimeUnit.MILLISECONDS.toSeconds(timeElapsed) -
|
||||||
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(timeElapsed)));
|
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(timeElapsed)));
|
||||||
Log.i("[ContactsManager] Step 1 for " + contacts.size() + " contacts: " + time + " elapsed since starting");
|
Log.i("[ContactsManager] Step 1 for " + contacts.size() + " contacts: " + time + " elapsed since starting");
|
||||||
|
|
||||||
if (hasContactsAccess()) {
|
c = getPhonesCursor(contentResolver);
|
||||||
Cursor c = getPhonesCursor(contentResolver);
|
|
||||||
if (c != null) {
|
if (c != null) {
|
||||||
while (c.moveToNext()) {
|
while (c.moveToNext()) {
|
||||||
String id = c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID));
|
String id = c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID));
|
||||||
|
@ -390,7 +393,6 @@ public class ContactsManager extends ContentObserver {
|
||||||
}
|
}
|
||||||
c.close();
|
c.close();
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
timeElapsed = (new Date()).getTime() - contactsTime.getTime();
|
timeElapsed = (new Date()).getTime() - contactsTime.getTime();
|
||||||
time = String.format("%02d:%02d",
|
time = String.format("%02d:%02d",
|
||||||
|
@ -411,6 +413,10 @@ public class ContactsManager extends ContentObserver {
|
||||||
Log.i("[ContactsManager] Step 3 for " + contacts.size() + " contacts: " + time + " elapsed since starting");
|
Log.i("[ContactsManager] Step 3 for " + contacts.size() + " contacts: " + time + " elapsed since starting");
|
||||||
|
|
||||||
androidContactsCache.clear();
|
androidContactsCache.clear();
|
||||||
|
} else {
|
||||||
|
Log.w("[Permission] Read contacts permission wasn't granted, only fetch LinphoneFriends");
|
||||||
|
}
|
||||||
|
|
||||||
Collections.sort(contacts);
|
Collections.sort(contacts);
|
||||||
Collections.sort(sipContacts);
|
Collections.sort(sipContacts);
|
||||||
setContacts(contacts);
|
setContacts(contacts);
|
||||||
|
|
|
@ -1268,7 +1268,7 @@ public class LinphoneActivity extends LinphoneGenericActivity implements OnClick
|
||||||
ContactsManager.getInstance().enableContactsAccess();
|
ContactsManager.getInstance().enableContactsAccess();
|
||||||
if (!ContactsManager.getInstance().contactsFetchedOnce()) {
|
if (!ContactsManager.getInstance().contactsFetchedOnce()) {
|
||||||
ContactsManager.getInstance().enableContactsAccess();
|
ContactsManager.getInstance().enableContactsAccess();
|
||||||
ContactsManager.getInstance().fetchContactsSync();
|
ContactsManager.getInstance().fetchContactsAsync();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1307,7 +1307,7 @@ public class LinphoneActivity extends LinphoneGenericActivity implements OnClick
|
||||||
} else {
|
} else {
|
||||||
if (!ContactsManager.getInstance().contactsFetchedOnce()) {
|
if (!ContactsManager.getInstance().contactsFetchedOnce()) {
|
||||||
ContactsManager.getInstance().enableContactsAccess();
|
ContactsManager.getInstance().enableContactsAccess();
|
||||||
ContactsManager.getInstance().fetchContactsSync();
|
ContactsManager.getInstance().fetchContactsAsync();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -463,7 +463,7 @@ public class LinphoneContact implements Serializable, Comparable<LinphoneContact
|
||||||
if (!ContactsManager.getInstance().hasContactsAccess()) {
|
if (!ContactsManager.getInstance().hasContactsAccess()) {
|
||||||
// This refresh is only needed if app has no contacts permission to refresh the list of LinphoneFriends.
|
// This refresh is only needed if app has no contacts permission to refresh the list of LinphoneFriends.
|
||||||
// Otherwise contacts will be refreshed due to changes in native contact and the handler in ContactsManager
|
// Otherwise contacts will be refreshed due to changes in native contact and the handler in ContactsManager
|
||||||
ContactsManager.getInstance().fetchContactsSync();
|
ContactsManager.getInstance().fetchContactsAsync();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue