use of com.fanap.podchat.util.PodThreadManager in project pod-chat-android-sdk by FanapSoft.
the class Generator method generateFakeContact.
public static void generateFakeContact(int count, Context context) {
new PodThreadManager().doThisAndGo(() -> {
Faker faker = new Faker();
ArrayList<ContentProviderOperation> ops = new ArrayList<>();
for (int i = 0; i < count; i++) {
String DisplayName = faker.artist().name();
String MobileNumber = faker.phoneNumber().cellPhone();
String HomeNumber = faker.phoneNumber().phoneNumber();
String WorkNumber = faker.phoneNumber().phoneNumber();
String emailID = null;
String company = faker.company().name();
String jobTitle = faker.company().bs();
ops.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI).withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, null).withValue(ContactsContract.RawContacts.ACCOUNT_NAME, null).build());
// ------------------------------------------------------ Names
if (DisplayName != null) {
ops.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, DisplayName).build());
}
// ------------------------------------------------------ Mobile Number
if (MobileNumber != null) {
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI).withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0).withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE).withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, MobileNumber).withValue(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE).build());
}
// ------------------------------------------------------ Home Numbers
if (HomeNumber != null) {
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI).withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0).withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE).withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, HomeNumber).withValue(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_HOME).build());
}
// ------------------------------------------------------ Work Numbers
if (WorkNumber != null) {
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI).withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0).withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE).withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, WorkNumber).withValue(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_WORK).build());
}
// ------------------------------------------------------ Email
if (emailID != null) {
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI).withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0).withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE).withValue(ContactsContract.CommonDataKinds.Email.DATA, emailID).withValue(ContactsContract.CommonDataKinds.Email.TYPE, ContactsContract.CommonDataKinds.Email.TYPE_WORK).build());
}
// ------------------------------------------------------ Organization
if (!company.equals("") && !jobTitle.equals("")) {
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI).withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0).withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE).withValue(ContactsContract.CommonDataKinds.Organization.COMPANY, company).withValue(ContactsContract.CommonDataKinds.Organization.TYPE, ContactsContract.CommonDataKinds.Organization.TYPE_WORK).withValue(ContactsContract.CommonDataKinds.Organization.TITLE, jobTitle).withValue(ContactsContract.CommonDataKinds.Organization.TYPE, ContactsContract.CommonDataKinds.Organization.TYPE_WORK).build());
}
try {
// Asking the Contact provider to create a new contact
context.getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
} catch (Exception e) {
e.printStackTrace();
Log.e("MTAG", "Exception Generating Contact " + e.getMessage());
}
}
Log.e("MTAG", "Generator added " + ops.size() + " Contact");
});
}
use of com.fanap.podchat.util.PodThreadManager in project pod-chat-android-sdk by FanapSoft.
the class ChatCore method getThreadAdminsMain.
private String getThreadAdminsMain(int count, int offset, long threadId, String typeCode, boolean useCache, ChatHandler handler) {
String uniqueId = generateUniqueId();
final int mCount = count != 0 ? count : 50;
PodThreadManager podThreadManager = new PodThreadManager();
podThreadManager.addTask(() -> {
if (cache && useCache) {
loadAdminsFromCache(uniqueId, mCount, offset, threadId);
}
});
podThreadManager.addTask(() -> {
if (chatReady) {
JsonObject content = new JsonObject();
content.addProperty("count", mCount);
content.addProperty("offset", offset);
content.addProperty("admin", true);
AsyncMessage chatMessage = new AsyncMessage();
chatMessage.setContent(content.toString());
chatMessage.setType(Constants.THREAD_PARTICIPANTS);
chatMessage.setTokenIssuer(String.valueOf(TOKEN_ISSUER));
chatMessage.setToken(getToken());
chatMessage.setUniqueId(uniqueId);
chatMessage.setSubjectId(threadId);
chatMessage.setTypeCode(typeCode != null ? typeCode : getTypeCode());
JsonObject jsonObject = (JsonObject) gson.toJsonTree(chatMessage);
String asyncContent = jsonObject.toString();
setCallBacks(null, null, null, true, Constants.THREAD_PARTICIPANTS, (long) offset, uniqueId);
sendAsyncMessage(asyncContent, AsyncAckType.Constants.WITHOUT_ACK, "SEND_GET_THREAD_ADMINS");
if (handler != null) {
handler.onGetThreadParticipant(uniqueId);
}
} else {
captureError(ChatConstant.ERROR_CHAT_READY, ChatConstant.ERROR_CODE_CHAT_READY, uniqueId);
}
});
podThreadManager.runTasksSynced();
return uniqueId;
}
use of com.fanap.podchat.util.PodThreadManager 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.util.PodThreadManager in project pod-chat-android-sdk by FanapSoft.
the class ChatCore method findAndUpdateGaps.
private void findAndUpdateGaps(List<MessageVO> newMessagesFromServer, long threadId) {
Runnable jobFindAndInsertGap = () -> {
if (newMessagesFromServer.size() == 0)
return;
MessageVO lastMessage = newMessagesFromServer.get(newMessagesFromServer.size() - 1);
if (lastMessage.getPreviousId() == 0)
return;
List<CacheMessageVO> messages = messageDatabaseHelper.getMessageById(lastMessage.getPreviousId());
if (Util.isNullOrEmpty(messages)) {
GapMessageVO gapMessageVO = new GapMessageVO();
gapMessageVO.setId(lastMessage.getId());
gapMessageVO.setPreviousId(lastMessage.getPreviousId());
gapMessageVO.setThreadId(threadId);
gapMessageVO.setTime(lastMessage.getTime());
gapMessageVO.setUniqueId(lastMessage.getUniqueId());
messageDatabaseHelper.insertGap(gapMessageVO);
lastMessage.setHasGap(true);
dataSource.updateMessage(lastMessage, threadId);
// messageDatabaseHelper.updateMessage(lastMessage, threadId);
}
};
Runnable jobUpdateGaps = () -> {
List<GapMessageVO> gaps = messageDatabaseHelper.getAllGaps(threadId);
if (!Util.isNullOrEmpty(gaps)) {
Map<Long, Long> msgIdAndPreviousId = new HashMap<>();
for (GapMessageVO gapMessage : gaps) {
msgIdAndPreviousId.put(gapMessage.getPreviousId(), gapMessage.getId());
}
for (MessageVO newMessage : newMessagesFromServer) {
if (msgIdAndPreviousId.containsKey(newMessage.getId())) {
// delete gap that produced by this message
messageDatabaseHelper.deleteGapForMessageId(msgIdAndPreviousId.get(newMessage.getId()));
// set message gap field to false
messageDatabaseHelper.updateMessageGapState(msgIdAndPreviousId.get(newMessage.getId()), false);
}
}
}
};
PodThreadManager podThreadManager = new PodThreadManager();
podThreadManager.addTask(jobFindAndInsertGap);
podThreadManager.addTask(jobUpdateGaps);
podThreadManager.runTasksSynced();
}
use of com.fanap.podchat.util.PodThreadManager in project pod-chat-android-sdk by FanapSoft.
the class ChatCore method searchContact.
// public String searchContact(RequestSearchContact requestSearchContact) {
//
// String uniqueId = generateUniqueId();
//
// Runnable cacheLoading = () -> {
//
// if (cache && requestSearchContact.canUseCache()) {
// List<Contact> contacts = new ArrayList<>();
// if (requestSearchContact.getId() != null) {
// Contact contact = messageDatabaseHelper.getContactById(Long.parseLong(requestSearchContact.getId()));
// contacts.add(contact);
// } else if (requestSearchContact.getFirstName() != null) {
// contacts = messageDatabaseHelper.getContactsByFirst(requestSearchContact.getFirstName());
// } else if (requestSearchContact.getFirstName() != null && requestSearchContact.getLastName() != null && !requestSearchContact.getFirstName().isEmpty() && !requestSearchContact.getLastName().isEmpty()) {
// contacts = messageDatabaseHelper.getContactsByFirstAndLast(requestSearchContact.getFirstName(), requestSearchContact.getLastName());
// } else if (requestSearchContact.getEmail() != null && !requestSearchContact.getEmail().isEmpty()) {
// contacts = messageDatabaseHelper.getContactsByEmail(requestSearchContact.getEmail());
// } else if (requestSearchContact.getCellphoneNumber() != null && !requestSearchContact.getCellphoneNumber().isEmpty()) {
// contacts = messageDatabaseHelper.getContactByCell(requestSearchContact.getCellphoneNumber());
// }
//
// ChatResponse<ResultContact> chatResponse = new ChatResponse<>();
// chatResponse.setCache(true);
//
// ResultContact resultContact = new ResultContact();
// ArrayList<Contact> listContact = new ArrayList<>(contacts);
// resultContact.setContacts(listContact);
// chatResponse.setUniqueId(uniqueId);
// chatResponse.setHasError(false);
// chatResponse.setErrorCode(0);
// chatResponse.setErrorMessage("");
// chatResponse.setResult(resultContact);
//
// String jsonContact = gson.toJson(chatResponse);
// listenerManager.callOnSearchContact(jsonContact, chatResponse);
// showLog("CACHE_SEARCH_CONTACT", jsonContact);
//
//
// }
// };
//
// Runnable requestServer = () -> {
// if (chatReady) {
//
// JsonObject jsonObject = (JsonObject) gson.toJsonTree(requestSearchContact);
//
// jsonObject.addProperty("uniqueId", uniqueId);
//
// jsonObject.addProperty("tokenIssuer", 1);
//
// showLog("SEND_SEARCH_CONTACT", getJsonForLog(jsonObject));
//
//
// Observable<Response<SearchContactVO>> observable = contactApi.searchContact(
// getToken(),
// TOKEN_ISSUER,
// requestSearchContact.getId()
// , requestSearchContact.getFirstName()
// , requestSearchContact.getLastName()
// , requestSearchContact.getEmail()
// , null
// , requestSearchContact.getOffset()
// , requestSearchContact.getSize()
// , null
// , requestSearchContact.getQuery()
// , requestSearchContact.getCellphoneNumber());
//
// observable
// .subscribeOn(Schedulers.io())
// .observeOn(AndroidSchedulers.mainThread())
// .subscribe(contactResponse -> {
//
//
// if (contactResponse.isSuccessful()) {
//
// if (contactResponse.body() != null && contactResponse.body().getResult() != null) {
//
// ArrayList<Contact> contacts = new ArrayList<>(contactResponse.body().getResult());
//
// ResultContact resultContacts = new ResultContact();
// resultContacts.setContacts(contacts);
//
// ChatResponse<ResultContact> chatResponse = new ChatResponse<>();
// chatResponse.setUniqueId(uniqueId);
// chatResponse.setResult(resultContacts);
//
// String content = gson.toJson(chatResponse);
//
//
// showLog("RECEIVE_SEARCH_CONTACT", content);
//
// listenerManager.callOnSearchContact(content, chatResponse);
//
// }
//
// } else {
//
// if (contactResponse.body() != null) {
// String errorMessage = contactResponse.body().getMessage() != null ? contactResponse.body().getMessage() : "";
// int errorCode = contactResponse.body().getErrorCode() != null ? contactResponse.body().getErrorCode() : 0;
// getErrorOutPut(errorMessage, errorCode, uniqueId);
// }
// }
//
// }, (Throwable throwable) -> Log.e(TAG, throwable.getMessage()));
// } else {
// getErrorOutPut(ChatConstant.ERROR_CHAT_READY, ChatConstant.ERROR_CODE_CHAT_READY, uniqueId);
// }
// };
//
//
// new PodThreadManager()
// .addNewTask(cacheLoading)
// .addNewTask(requestServer)
// .runTasksSynced();
//
// return uniqueId;
// }
public String searchContact(RequestSearchContact requestSearchContact) {
String uniqueId = generateUniqueId();
String size = !Util.isNullOrEmpty(requestSearchContact.getSize()) && !requestSearchContact.getSize().equals("0") ? requestSearchContact.getSize() : "50";
String offset = !Util.isNullOrEmpty(requestSearchContact.getOffset()) ? requestSearchContact.getOffset() : "0";
String order = !Util.isNullOrEmpty(requestSearchContact.getOrder()) ? requestSearchContact.getOrder() : "asc";
Runnable cacheLoading = () -> {
if (cache && requestSearchContact.canUseCache()) {
ChatResponse<ResultContact> chatResponse = messageDatabaseHelper.searchContacts(requestSearchContact, size, offset);
chatResponse.setUniqueId(uniqueId);
String jsonContact = gson.toJson(chatResponse);
listenerManager.callOnSearchContact(jsonContact, chatResponse);
showLog("CACHE_SEARCH_CONTACT", jsonContact);
}
};
Runnable requestServer = () -> {
if (chatReady) {
JsonObject jObj = new JsonObject();
String query = requestSearchContact.getQuery();
if (Util.isNullOrEmpty(query)) {
query = "";
if (!Util.isNullOrEmpty(requestSearchContact.getFirstName())) {
query += requestSearchContact.getFirstName() + " ";
}
if (!Util.isNullOrEmpty(requestSearchContact.getLastName())) {
query += requestSearchContact.getLastName();
}
}
if (!Util.isNullOrEmpty(query)) {
jObj.addProperty("query", query);
}
if (!Util.isNullOrEmpty(requestSearchContact.getEmail())) {
jObj.addProperty("email", requestSearchContact.getEmail());
}
if (!Util.isNullOrEmpty(requestSearchContact.getCellphoneNumber())) {
jObj.addProperty("cellphoneNumber", requestSearchContact.getCellphoneNumber());
}
if (!Util.isNullOrEmpty(requestSearchContact.getId())) {
jObj.addProperty("id", requestSearchContact.getId());
}
jObj.addProperty("size", size);
jObj.addProperty("offset", offset);
jObj.addProperty("order", order);
AsyncMessage chatMessage = new AsyncMessage();
chatMessage.setContent(jObj.toString());
chatMessage.setType(Constants.GET_CONTACTS);
chatMessage.setToken(getToken());
chatMessage.setUniqueId(uniqueId);
chatMessage.setTypeCode(!Util.isNullOrEmpty(typeCode) ? typeCode : getTypeCode());
chatMessage.setTokenIssuer(String.valueOf(TOKEN_ISSUER));
JsonObject jsonObject = (JsonObject) gson.toJsonTree(chatMessage);
String asyncContent = jsonObject.toString();
setCallBacks(null, null, null, true, Constants.GET_CONTACTS, Long.valueOf(offset), uniqueId);
handlerSend.put(uniqueId, new ChatHandler() {
@Override
public void onGetContact(String contactJson, ChatResponse<ResultContact> chatResponse) {
super.onGetContact(contactJson, chatResponse);
showLog("RECEIVE_SEARCH_CONTACT", contactJson);
listenerManager.callOnSearchContact(contactJson, chatResponse);
handlerSend.remove(uniqueId);
}
});
sendAsyncMessage(asyncContent, AsyncAckType.Constants.WITHOUT_ACK, "SEND_SEARCH_CONTACT");
} else {
captureError(ChatConstant.ERROR_CHAT_READY, ChatConstant.ERROR_CODE_CHAT_READY, uniqueId);
}
};
new PodThreadManager().addNewTask(cacheLoading).addNewTask(requestServer).runTasksSynced();
return uniqueId;
}
Aggregations