use of im.actor.core.network.RpcCallback in project actor-platform by actorapp.
the class Authentication method requestStartEmailAuth.
@Deprecated
public Command<AuthState> requestStartEmailAuth(final String email) {
return callback -> {
ArrayList<String> langs1 = new ArrayList<>();
for (String s : modules.getConfiguration().getPreferredLanguages()) {
langs1.add(s);
}
request(new RequestStartEmailAuth(email, apiConfiguration.getAppId(), apiConfiguration.getAppKey(), deviceHash, apiConfiguration.getDeviceTitle(), modules.getConfiguration().getTimeZone(), langs1), new RpcCallback<ResponseStartEmailAuth>() {
@Override
public void onResult(ResponseStartEmailAuth response) {
modules.getPreferences().putString(KEY_EMAIL, email);
modules.getPreferences().putString(KEY_TRANSACTION_HASH, response.getTransactionHash());
ApiEmailActivationType emailActivationType = response.getActivationType();
if (emailActivationType.equals(ApiEmailActivationType.OAUTH2)) {
state = AuthState.GET_OAUTH_PARAMS;
} else if (emailActivationType.equals(ApiEmailActivationType.CODE)) {
state = AuthState.CODE_VALIDATION_EMAIL;
} else if (emailActivationType.equals(ApiEmailActivationType.PASSWORD)) {
state = AuthState.PASSWORD_VALIDATION;
} else {
state = AuthState.CODE_VALIDATION_EMAIL;
}
Runtime.postToMainThread(() -> callback.onResult(state));
}
@Override
public void onError(final RpcException e) {
Runtime.postToMainThread(() -> {
Log.e(TAG, e);
callback.onError(e);
});
}
});
};
}
use of im.actor.core.network.RpcCallback in project actor-platform by actorapp.
the class BookImportActor method performImportIfRequired.
private void performImportIfRequired() {
if (ENABLE_LOG) {
Log.d(TAG, "performImportIfRequired called");
}
if (isUploadingContacts) {
if (ENABLE_LOG) {
Log.d(TAG, "performImportIfRequired:exiting:already importing");
}
return;
}
if (importQueue.size() == 0) {
if (ENABLE_LOG) {
Log.d(TAG, "performImportIfRequired:exiting:nothing to import");
}
// Marking as everything is imported
context().getConductor().getConductor().onPhoneBookImported();
return;
}
//
// Performing import
//
isUploadingContacts = true;
final ArrayList<ApiPhoneToImport> phoneToImports = new ArrayList<>();
final ArrayList<ApiEmailToImport> emailToImports = new ArrayList<>();
for (int i = 0; i < MAX_IMPORT_SIZE && importQueue.size() > 0; i++) {
ImportQueueItem importQueueItem = importQueue.remove(0);
if (importQueueItem instanceof ImportEmailQueueItem) {
emailToImports.add(new ApiEmailToImport(((ImportEmailQueueItem) importQueueItem).getEmail(), ((ImportEmailQueueItem) importQueueItem).getName()));
} else if (importQueueItem instanceof ImportPhoneQueueItem) {
phoneToImports.add(new ApiPhoneToImport(((ImportPhoneQueueItem) importQueueItem).getPhoneNumber(), ((ImportPhoneQueueItem) importQueueItem).getName()));
} else {
throw new RuntimeException();
}
}
request(new RequestImportContacts(phoneToImports, emailToImports, ApiSupportConfiguration.OPTIMIZATIONS), new RpcCallback<ResponseImportContacts>() {
@Override
public void onResult(ResponseImportContacts response) {
for (ApiPhoneToImport phoneToImport : phoneToImports) {
storage.markAsImported(phoneToImport.getPhoneNumber());
importingPhones.remove(phoneToImport.getPhoneNumber());
}
for (ApiEmailToImport emailToImport : emailToImports) {
storage.markAsImported(emailToImport.getEmail());
importingEmails.remove(emailToImport.getEmail());
}
context().getContactsModule().getBookImportState().put(0, storage.toByteArray());
//
if (response.getUsers().size() != 0 || response.getUserPeers().size() != 0) {
if (ENABLE_LOG) {
Log.d(TAG, "Import success with " + (response.getUsers().size() + response.getUserPeers().size()) + " new contacts");
}
if (response.getUserPeers().size() != 0) {
// Optimized version
ArrayList<Integer> uids = new ArrayList<>();
for (ApiUserOutPeer u : response.getUserPeers()) {
uids.add(u.getUid());
}
updates().loadRequiredPeers(response.getUserPeers(), new ArrayList<>()).flatMap(v -> updates().applyUpdate(response.getSeq(), response.getState(), new UpdateContactsAdded(uids)));
} else {
// Old version
ArrayList<Integer> uids = new ArrayList<>();
for (ApiUser u : response.getUsers()) {
uids.add(u.getId());
}
updates().onUpdateReceived(new FatSeqUpdate(response.getSeq(), response.getState(), UpdateContactsAdded.HEADER, new UpdateContactsAdded(uids).toByteArray(), response.getUsers(), new ArrayList<>()));
}
} else {
if (ENABLE_LOG) {
Log.d(TAG, "Import success, but no new contacts found");
}
}
//
// Launching next iteration
//
isUploadingContacts = false;
performImportIfRequired();
}
@Override
public void onError(RpcException e) {
// TODO: Better error handling
if (ENABLE_LOG) {
Log.d(TAG, "Import failure");
}
e.printStackTrace();
//
// Launching next iteration
//
isUploadingContacts = false;
performImportIfRequired();
}
});
}
use of im.actor.core.network.RpcCallback in project actor-platform by actorapp.
the class ContactsSyncActor method performSync.
public void performSync() {
if (ENABLE_LOG) {
Log.d(TAG, "Checking sync");
}
if (isInProgress) {
if (ENABLE_LOG) {
Log.d(TAG, "Sync in progress, invalidating current sync");
}
isInvalidated = true;
return;
}
isInProgress = true;
isInvalidated = false;
if (ENABLE_LOG) {
Log.d(TAG, "Starting sync");
}
Integer[] uids = contacts.toArray(new Integer[contacts.size()]);
Arrays.sort(uids);
String hash = "";
for (long u : uids) {
if (hash.length() != 0) {
hash += ",";
}
hash += u;
}
byte[] hashData;
try {
hashData = hash.getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return;
}
String hashValue = Crypto.hex(Crypto.SHA256(hashData));
if (ENABLE_LOG) {
Log.d(TAG, "Performing sync with hash: " + hashValue);
Log.d(TAG, "Performing sync with uids: " + hash);
}
request(new RequestGetContacts(hashValue, ApiSupportConfiguration.OPTIMIZATIONS), new RpcCallback<ResponseGetContacts>() {
@Override
public void onResult(ResponseGetContacts response) {
if (ENABLE_LOG) {
Log.d(TAG, "Sync received " + (response.getUsers().size() + response.getUserPeers().size()) + " contacts");
}
if (response.getUserPeers().size() > 0) {
updates().loadRequiredPeers(response.getUserPeers(), new ArrayList<>()).then(v -> onContactsLoaded(response));
} else {
updates().applyRelatedData(response.getUsers()).then(v -> onContactsLoaded(response));
}
}
@Override
public void onError(RpcException e) {
isInProgress = false;
e.printStackTrace();
}
});
}
use of im.actor.core.network.RpcCallback in project actor-platform by actorapp.
the class ArchivedDialogsActor method onLoadMore.
private void onLoadMore(boolean init, RpcCallback<ResponseLoadArchived> callback) {
if (init || isLoading) {
//
if (lastCallback != null) {
lastCallback.onError(new RpcException(TAG, 0, "callback replaced", false, null));
}
}
lastCallback = callback;
if (isLoading && !init) {
return;
}
if (init) {
if (lastRequest != -1) {
cancelRequest(lastRequest);
}
nextOffset = null;
}
isLoading = true;
Log.d(TAG, "Loading archived dialogs");
api(new RequestLoadArchived(nextOffset, LIMIT, ApiSupportConfiguration.OPTIMIZATIONS)).chain(r -> updates().applyRelatedData(r.getUsers(), r.getGroups())).chain(r -> updates().loadRequiredPeers(r.getUserPeers(), r.getGroupPeers())).then(r -> onLoadedMore(r)).failure(e -> lastCallback.onError((RpcException) e));
}
use of im.actor.core.network.RpcCallback in project actor-platform by actorapp.
the class Authentication method requestCompleteOauth.
@Deprecated
public Command<AuthState> requestCompleteOauth(final String code) {
return callback -> request(new RequestCompleteOAuth2(modules.getPreferences().getString(KEY_TRANSACTION_HASH), code), new RpcCallback<ResponseAuth>() {
@Override
public void onResult(ResponseAuth response) {
onLoggedIn(callback, response);
}
@Override
public void onError(final RpcException e) {
if ("EMAIL_EXPIRED".equals(e.getTag())) {
resetAuth();
} else if ("EMAIL_UNOCCUPIED".equals(e.getTag())) {
state = AuthState.SIGN_UP;
callback.onResult(AuthState.SIGN_UP);
return;
}
Runtime.postToMainThread(() -> {
Log.e(TAG, e);
callback.onError(e);
});
}
});
}
Aggregations