use of im.actor.core.viewmodel.Command 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.viewmodel.Command in project actor-platform by actorapp.
the class AndroidMessenger method sendUri.
public Command<Boolean> sendUri(final Peer peer, final Uri uri, String appName) {
return callback -> fileDownloader.execute(() -> {
String[] filePathColumn = { MediaStore.Images.Media.DATA, MediaStore.Video.Media.MIME_TYPE, MediaStore.Video.Media.TITLE };
String picturePath;
String mimeType;
String fileName;
String ext = "";
Cursor cursor = context.getContentResolver().query(uri, filePathColumn, null, null, null);
if (cursor != null) {
cursor.moveToFirst();
picturePath = cursor.getString(cursor.getColumnIndex(filePathColumn[0]));
mimeType = cursor.getString(cursor.getColumnIndex(filePathColumn[1]));
fileName = cursor.getString(cursor.getColumnIndex(filePathColumn[2]));
cursor.close();
} else {
picturePath = uri.getPath();
fileName = new File(uri.getPath()).getName();
int index = fileName.lastIndexOf(".");
if (index > 0) {
ext = fileName.substring(index + 1);
mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(ext);
} else {
mimeType = "?/?";
}
}
if (mimeType == null) {
mimeType = "?/?";
}
if (picturePath == null || !uri.getScheme().equals("file")) {
File externalFile = context.getExternalFilesDir(null);
if (externalFile == null) {
callback.onError(new NullPointerException());
return;
}
String externalPath = externalFile.getAbsolutePath();
File dest = new File(externalPath + "/" + appName + "/");
dest.mkdirs();
if (ext.isEmpty() && picturePath != null) {
int index = picturePath.lastIndexOf(".");
ext = picturePath.substring(index + 1);
}
File outputFile = new File(dest, "upload_" + random.nextLong() + "." + ext);
picturePath = outputFile.getAbsolutePath();
try {
IOUtils.copy(context.getContentResolver().openInputStream(uri), new File(picturePath));
} catch (IOException e) {
e.printStackTrace();
callback.onError(e);
return;
}
}
if (fileName == null) {
fileName = picturePath;
}
if (!ext.isEmpty() && !fileName.endsWith(ext))
fileName += "." + ext;
if (mimeType.startsWith("video/")) {
sendVideo(peer, picturePath, fileName);
} else if (mimeType.startsWith("image/")) {
sendPhoto(peer, picturePath, new File(fileName).getName());
} else {
sendDocument(peer, picturePath, new File(fileName).getName());
}
callback.onResult(true);
});
}
use of im.actor.core.viewmodel.Command 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);
});
}
});
}
use of im.actor.core.viewmodel.Command in project actor-platform by actorapp.
the class ContactsModule method removeContact.
public Command<Boolean> removeContact(final int uid) {
return callback -> {
User user = users().getValue(uid);
if (user == null) {
runOnUiThread(() -> callback.onError(new RpcInternalException()));
return;
}
request(new RequestRemoveContact(uid, user.getAccessHash()), new RpcCallback<ResponseSeq>() {
@Override
public void onResult(ResponseSeq response) {
ArrayList<Integer> uids = new ArrayList<>();
uids.add(uid);
SeqUpdate update = new SeqUpdate(response.getSeq(), response.getState(), UpdateContactsRemoved.HEADER, new UpdateContactsRemoved(uids).toByteArray());
updates().onUpdateReceived(update);
runOnUiThread(() -> callback.onResult(true));
}
@Override
public void onError(RpcException e) {
runOnUiThread(() -> callback.onError(new RpcInternalException()));
}
});
};
}
use of im.actor.core.viewmodel.Command in project actor-platform by actorapp.
the class ContactsModule method addContact.
public Command<Boolean> addContact(final int uid) {
return callback -> {
User user = users().getValue(uid);
if (user == null) {
runOnUiThread(() -> callback.onError(new RpcInternalException()));
return;
}
request(new RequestAddContact(uid, user.getAccessHash()), new RpcCallback<ResponseSeq>() {
@Override
public void onResult(ResponseSeq response) {
ArrayList<Integer> uids = new ArrayList<>();
uids.add(uid);
SeqUpdate update = new SeqUpdate(response.getSeq(), response.getState(), UpdateContactsAdded.HEADER, new UpdateContactsAdded(uids).toByteArray());
updates().onUpdateReceived(update);
runOnUiThread(() -> callback.onResult(true));
}
@Override
public void onError(RpcException e) {
runOnUiThread(() -> callback.onError(new RpcInternalException()));
}
});
};
}
Aggregations