use of com.fanap.podchat.cachemodel.PhoneContact in project pod-chat-android-sdk by FanapSoft.
the class ChatCore method addGroupContacts.
private PublishSubject<List<PhoneContact>> addGroupContacts(List<PhoneContact> phoneContacts, String uniqueId) {
PublishSubject<List<PhoneContact>> subject = PublishSubject.create();
ArrayList<String> firstNames = new ArrayList<>();
ArrayList<String> cellphoneNumbers = new ArrayList<>();
ArrayList<String> lastNames = new ArrayList<>();
ArrayList<String> typeCodes = new ArrayList<>();
ArrayList<String> uniqueIds = new ArrayList<>();
ArrayList<String> emails = new ArrayList<>();
for (PhoneContact contact : phoneContacts) {
firstNames.add(contact.getName());
lastNames.add(contact.getLastName());
uniqueIds.add(generateUniqueId());
String phoneNum = String.valueOf(contact.getPhoneNumber());
if (phoneNum.startsWith("9")) {
phoneNum = phoneNum.replaceFirst("9", "09");
}
cellphoneNumbers.add(phoneNum);
emails.add("");
typeCodes.add(getTypeCode());
}
Observable<Response<Contacts>> addContactsObservable;
if (getPlatformHost() != null) {
if (!Util.isNullOrEmpty(getTypeCode())) {
addContactsObservable = contactApi.addContacts(getToken(), TOKEN_ISSUER, firstNames, lastNames, emails, uniqueIds, cellphoneNumbers, typeCodes);
} else {
addContactsObservable = contactApi.addContacts(getToken(), TOKEN_ISSUER, firstNames, lastNames, emails, uniqueIds, cellphoneNumbers);
}
showLog("Call add contacts " + new Date());
addContactsObservable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(contactsResponse -> {
showLog(">>> Server Respond " + new Date());
boolean error = false;
if (contactsResponse.body() != null) {
error = contactsResponse.body().getHasError();
showLog(">>>Response: " + contactsResponse.code());
showLog(">>>ReferenceNumber: " + contactsResponse.body().getReferenceNumber());
showLog(">>>Ott: " + contactsResponse.body().getOtt());
}
if (contactsResponse.isSuccessful()) {
if (error) {
captureError(contactsResponse.body().getMessage(), contactsResponse.body().getErrorCode(), uniqueId);
showLog("Error add Contacts: " + contactsResponse.body().getMessage());
subject.onError(new Throwable(contactsResponse.body().getMessage()));
} else {
// successful response
Contacts contacts = contactsResponse.body();
ChatResponse<Contacts> chatResponse = new ChatResponse<>();
chatResponse.setResult(contacts);
chatResponse.setUniqueId(uniqueId);
Runnable updatePhoneContactsDBTask = () -> {
try {
boolean result = phoneContactDbHelper.addPhoneContacts(phoneContacts);
if (!result) {
disableCache(() -> phoneContactDbHelper.addPhoneContacts(phoneContacts));
}
} catch (Exception e) {
showErrorLog("Updating Contacts cache failed: " + e.getMessage());
onUnknownException(uniqueId, e);
}
};
Runnable updateCachedContactsTask = () -> {
if (cache) {
try {
// messageDatabaseHelper.saveContacts(chatResponse.getResult().getResult(), getExpireAmount());
dataSource.saveContactsResultFromServer(chatResponse.getResult().getResult());
} catch (Exception e) {
showErrorLog("Saving Contacts Failed: " + e.getMessage());
onUnknownException(uniqueId, e);
}
}
};
new PodThreadManager().addNewTask(updatePhoneContactsDBTask).addNewTask(updateCachedContactsTask).addNewTask(() -> subject.onNext(phoneContacts)).runTasksSynced();
}
} else {
captureError(contactsResponse.message(), contactsResponse.code(), uniqueId);
showLog("Error add Contacts: " + contactsResponse.raw());
subject.onError(new Throwable(contactsResponse.message()));
}
}, throwable -> {
captureError(throwable.getMessage(), ChatConstant.ERROR_CODE_UNKNOWN_EXCEPTION, uniqueId);
subject.onError(throwable);
});
}
return subject;
}
use of com.fanap.podchat.cachemodel.PhoneContact in project pod-chat-android-sdk by FanapSoft.
the class ChatCore method addContacts.
/**
* Add list of contacts with their mobile numbers and their cellphoneNumbers
*/
private void addContacts(List<PhoneContact> phoneContacts, String uniqueId) {
ArrayList<String> firstNames = new ArrayList<>();
ArrayList<String> cellphoneNumbers = new ArrayList<>();
ArrayList<String> lastNames = new ArrayList<>();
ArrayList<String> typeCodes = new ArrayList<>();
ArrayList<String> uniqueIds = new ArrayList<>();
ArrayList<String> emails = new ArrayList<>();
for (PhoneContact contact : phoneContacts) {
firstNames.add(contact.getName());
lastNames.add(contact.getLastName());
uniqueIds.add(generateUniqueId());
String phoneNum = String.valueOf(contact.getPhoneNumber());
if (phoneNum.startsWith("9")) {
phoneNum = phoneNum.replaceFirst("9", "09");
}
cellphoneNumbers.add(phoneNum);
emails.add("");
typeCodes.add(getTypeCode());
}
Observable<Response<Contacts>> addContactsObservable;
if (getPlatformHost() != null) {
if (!Util.isNullOrEmpty(getTypeCode())) {
addContactsObservable = contactApi.addContacts(getToken(), TOKEN_ISSUER, firstNames, lastNames, emails, uniqueIds, cellphoneNumbers, typeCodes);
} else {
addContactsObservable = contactApi.addContacts(getToken(), TOKEN_ISSUER, firstNames, lastNames, emails, uniqueIds, cellphoneNumbers);
}
showLog("Call add contacts " + new Date());
addContactsObservable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(contactsResponse -> {
showLog(">>> Server Respond " + new Date());
boolean error = false;
if (contactsResponse.body() != null) {
error = contactsResponse.body().getHasError();
showLog(">>>Response: " + contactsResponse.code());
showLog(">>>ReferenceNumber: " + contactsResponse.body().getReferenceNumber());
showLog(">>>Ott: " + contactsResponse.body().getOtt());
}
if (contactsResponse.isSuccessful()) {
if (error) {
captureError(contactsResponse.body().getMessage(), contactsResponse.body().getErrorCode(), uniqueId);
showLog("Error add Contacts: " + contactsResponse.body().getMessage());
} else {
// successful response
Contacts contacts = contactsResponse.body();
ChatResponse<Contacts> chatResponse = new ChatResponse<>();
chatResponse.setResult(contacts);
chatResponse.setUniqueId(uniqueId);
Runnable updatePhoneContactsDBTask = () -> {
try {
boolean result = phoneContactDbHelper.addPhoneContacts(phoneContacts);
if (!result) {
disableCache(() -> phoneContactDbHelper.addPhoneContacts(phoneContacts));
}
} catch (Exception e) {
showErrorLog("Updating Contacts cache failed: " + e.getMessage());
onUnknownException(uniqueId, e);
}
};
Runnable updateCachedContactsTask = () -> {
if (cache) {
try {
// messageDatabaseHelper.saveContacts(chatResponse.getResult().getResult(), getExpireAmount());
dataSource.saveContactsResultFromServer(chatResponse.getResult().getResult());
} catch (Exception e) {
showErrorLog("Saving Contacts Failed: " + e.getMessage());
onUnknownException(uniqueId, e);
}
}
};
new PodThreadManager().addNewTask(updatePhoneContactsDBTask).addNewTask(updateCachedContactsTask).runTasksSynced();
String contactsJson = gson.toJson(chatResponse);
listenerManager.callOnSyncContact(contactsJson, chatResponse);
showLog("SYNC_CONTACT_COMPLETED", contactsJson);
}
} else {
captureError(contactsResponse.message(), contactsResponse.code(), uniqueId);
showLog("Error add Contacts: " + contactsResponse.raw());
}
}, throwable -> captureError(throwable.getMessage(), ChatConstant.ERROR_CODE_UNKNOWN_EXCEPTION, uniqueId));
}
}
use of com.fanap.podchat.cachemodel.PhoneContact in project pod-chat-android-sdk by FanapSoft.
the class ChatCore method getPhoneContact.
/**
* Get the list of the Device Contact
*/
private void getPhoneContact(Context context, String uniqueId, OnContactLoaded listener) {
try {
showLog(">>> Getting phone contacts ");
List<PhoneContact> cachePhoneContacts = new ArrayList<>();
PhoneContactAsyncTask task = new PhoneContactAsyncTask(phoneContactDbHelper, contacts -> {
String firstName;
String phoneNumber;
String lastName;
String empty = "";
int version;
ArrayList<PhoneContact> newPhoneContact = new ArrayList<>();
HashMap<String, PhoneContact> newContactsMap = new HashMap<>();
showLog("#" + contacts.size() + " Contacts Loaded From Cache");
cachePhoneContacts.addAll(contacts);
HashMap<String, PhoneContact> mapCacheContactKeeper = new HashMap<>();
if (cachePhoneContacts.size() > 0) {
for (PhoneContact contact : cachePhoneContacts) {
mapCacheContactKeeper.put(contact.getPhoneNumber(), contact);
}
}
Cursor cursor = context.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
if (cursor == null) {
showLog("Contacts loader cursor is null");
listener.onLoad(newPhoneContact);
return;
}
while (cursor.moveToNext()) {
firstName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
lastName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME));
version = cursor.getInt(cursor.getColumnIndex(ContactsContract.RawContacts.VERSION));
phoneNumber = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
PhoneContact phoneContact = new PhoneContact();
if (!Util.isNullOrEmpty(phoneNumber)) {
phoneContact.setPhoneNumber(phoneNumber);
if (!Util.isNullOrEmpty(firstName)) {
phoneContact.setName(firstName);
} else {
phoneContact.setName(empty);
}
if (!Util.isNullOrEmpty(lastName)) {
phoneContact.setLastName(lastName);
} else {
phoneContact.setLastName(empty);
}
if (!Util.isNullOrEmpty(version)) {
phoneContact.setVersion(version);
}
if (cachePhoneContacts.size() > 0) {
// if its not in PhoneContactCache and its a contact that added recently
if (mapCacheContactKeeper.get(phoneNumber) != null) {
if (version != mapCacheContactKeeper.get(phoneNumber).getVersion()) {
newContactsMap.put(phoneNumber, phoneContact);
}
} else {
newContactsMap.put(phoneNumber, phoneContact);
}
} else {
newContactsMap.put(phoneNumber, phoneContact);
}
}
}
cursor.close();
// retrieve unique contacts
for (String key : newContactsMap.keySet()) {
if (!mapCacheContactKeeper.containsKey(key))
newPhoneContact.add(newContactsMap.get(key));
}
showLog("#" + newPhoneContact.size() + " New Contact Found");
listener.onLoad(newPhoneContact);
});
task.execute();
} catch (Exception e) {
showErrorLog(e.getMessage());
onUnknownException(uniqueId, e);
}
}
use of com.fanap.podchat.cachemodel.PhoneContact in project pod-chat-android-sdk by FanapSoft.
the class ExampleUnitTest method contactsEquality.
@Test
public void contactsEquality() {
PhoneContact a = new PhoneContact();
a.setName("a contact");
a.setLastName("a lastname");
a.setPhoneNumber("+98 915 777 0684");
a.setVersion(150);
PhoneContact b = new PhoneContact();
b.setName("b contact");
b.setLastName("b lastname");
b.setPhoneNumber("+98 915 777 0684");
b.setVersion(750);
boolean e = a.equals(b);
System.out.println(e);
assertEquals(a, b);
}
use of com.fanap.podchat.cachemodel.PhoneContact in project pod-chat-android-sdk by FanapSoft.
the class ExampleUnitTest method contactsEqualityInList.
@Test
public void contactsEqualityInList() {
HashMap<String, PhoneContact> phoneContacts = new HashMap<>();
List<PhoneContact> list = new ArrayList<>();
PhoneContact a = new PhoneContact();
a.setName("a contact");
a.setLastName("a lastname");
a.setPhoneNumber("+98 915 777 0684");
a.setVersion(150);
PhoneContact b = new PhoneContact();
b.setName("b contact");
b.setLastName("b lastname");
b.setPhoneNumber("+98 915 777 0684");
b.setVersion(750);
phoneContacts.put(a.getPhoneNumber(), a);
list.add(b);
phoneContacts.put(b.getPhoneNumber(), b);
list.add(a);
System.out.println(phoneContacts.size());
assertEquals(phoneContacts.size(), 1);
assertEquals(phoneContacts.get("+98 915 777 0684").getName(), "b contact");
System.out.println("list + " + list);
Collections.sort(list, (o1, o2) -> Long.compare(o1.getVersion(), o2.getVersion()));
System.out.println("list + " + list);
}
Aggregations