use of net.iGap.structs.MessageObject in project iGap-Android by KianIranian-STDG.
the class MusicPlayer method previousMusic.
public static void previousMusic() {
try {
if (MusicPlayer.mp != null) {
if (MusicPlayer.mp.getCurrentPosition() > 10000) {
musicProgress = 0;
MusicPlayer.mp.seekTo(0);
time = MusicPlayer.mp.getCurrentPosition();
updatePlayerTime();
return;
}
}
} catch (IllegalStateException e) {
e.printStackTrace();
}
try {
String beforeMessageId = MusicPlayer.messageId;
selectedMedia++;
if (selectedMedia >= mediaList.size()) {
selectedMedia = 0;
}
MessageObject messageObject = null;
boolean _continue = true;
while (_continue) {
messageObject = RealmRoomMessage.getFinalMessage(mediaList.get(selectedMedia));
if (!messageObject.getAttachment().isFileExistsOnLocal(messageObject)) {
selectedMedia++;
if (selectedMedia >= mediaList.size()) {
if (isVoice) {
// avoid from return to first voice
if (btnPlayMusic != null) {
btnPlayMusic.setText(context.getString(R.string.icon_play));
}
stopSound();
closeLayoutMediaPlayer();
return;
}
selectedMedia = 0;
}
} else {
_continue = false;
}
}
startPlayer(messageObject.attachment.name, messageObject.attachment.filePath, roomName, roomId, false, mediaList.get(selectedMedia).id + "");
if (FragmentChat.onMusicListener != null) {
FragmentChat.onMusicListener.complete(true, MusicPlayer.messageId, beforeMessageId);
}
} catch (Exception e) {
e.printStackTrace();
}
}
use of net.iGap.structs.MessageObject in project iGap-Android by KianIranian-STDG.
the class ResendMessage method resend.
private void resend(final boolean all) {
if (!RequestManager.getInstance(AccountManager.selectedAccount).isUserLogin()) {
return;
}
DbManager.getInstance().doRealmTask(realm -> {
realm.executeTransactionAsync(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
for (MessageObject message : mMessages) {
if (all) {
RealmRoomMessage.setStatus(realm, message.id, ProtoGlobal.RoomMessageStatus.SENDING);
} else {
if (message.id == mSelectedMessageID) {
RealmRoomMessage.setStatus(realm, message.id, ProtoGlobal.RoomMessageStatus.SENDING);
break;
}
}
}
}
}, () -> {
if (all) {
mListener.resendAllMessages();
} else {
mListener.resendMessage();
}
for (int i = 0; i < mMessages.size(); i++) {
final int j = i;
if (all) {
if (FragmentChat.allowResendMessage(mMessages.get(j).id)) {
G.handler.postDelayed(new Runnable() {
@Override
public void run() {
DbManager.getInstance().doRealmTask(realm1 -> {
RealmRoomMessage roomMessage = realm1.where(RealmRoomMessage.class).equalTo("messageId", mMessages.get(j).id).findFirst();
MessageObject resendMessageObject = MessageObject.create(roomMessage);
if (resendMessageObject != null) {
RealmRoom realmRoom = realm1.where(RealmRoom.class).equalTo("id", resendMessageObject.roomId).findFirst();
if (realmRoom != null) {
if (resendMessageObject.attachment == null) {
ProtoGlobal.Room.Type roomType = realmRoom.getType();
ChatSendMessageUtil.getInstance(AccountManager.selectedAccount).build(roomType, resendMessageObject.roomId, resendMessageObject);
} else {
if (resendMessageObject.additional != null && resendMessageObject.additional.type == 4) {
ChatSendMessageUtil.getInstance(AccountManager.selectedAccount).build(realmRoom.getType(), resendMessageObject.roomId, resendMessageObject);
} else {
RealmRoomMessage message = realm.copyFromRealm(roomMessage);
UploadObject fileObject = UploadObject.createForMessage(message, realmRoom.getType());
if (fileObject != null) {
Uploader.getInstance().upload(fileObject);
}
}
}
}
}
});
}
}, 1000 * j);
}
} else {
if (mMessages.get(j).id == mSelectedMessageID) {
if (FragmentChat.allowResendMessage(mSelectedMessageID)) {
RealmRoomMessage roomMessage = realm.where(RealmRoomMessage.class).equalTo("messageId", mMessages.get(j).id).findFirst();
MessageObject resendMessageObject = MessageObject.create(roomMessage);
if (resendMessageObject != null) {
RealmRoom realmRoom = realm.where(RealmRoom.class).equalTo("id", resendMessageObject.roomId).findFirst();
if (realmRoom != null) {
ProtoGlobal.Room.Type roomType = realmRoom.getType();
if (resendMessageObject.attachment == null) {
ChatSendMessageUtil.getInstance(AccountManager.selectedAccount).build(roomType, resendMessageObject.roomId, resendMessageObject);
} else {
if (resendMessageObject.additional != null && resendMessageObject.additional.type == 4) {
ChatSendMessageUtil.getInstance(AccountManager.selectedAccount).build(realmRoom.getType(), resendMessageObject.roomId, resendMessageObject);
} else {
RealmRoomMessage message = realm.copyFromRealm(roomMessage);
UploadObject fileObject = UploadObject.createForMessage(message, realmRoom.getType());
if (fileObject != null) {
Uploader.getInstance().upload(fileObject);
}
}
}
}
}
break;
}
}
}
}
});
});
}
use of net.iGap.structs.MessageObject in project iGap-Android by KianIranian-STDG.
the class MessageDataStorage method createForwardMessage.
public void createForwardMessage(final long destinationRoomId, final long newMessageId, MessageObject sourceMessage, boolean isMessage, DatabaseDelegate databaseDelegate) {
storageQueue.postRunnable(() -> {
FileLog.i(TAG, "createForwardMessage: " + destinationRoomId + " " + newMessageId);
try {
final RealmRoomMessage[] forwardedMessage = new RealmRoomMessage[1];
MessageObject messageObject = null;
RealmRoomMessage copyMessage = null;
database.beginTransaction();
if (isMessage && sourceMessage.forwardedMessage == null) {
forwardedMessage[0] = database.createObject(RealmRoomMessage.class, newMessageId);
forwardedMessage[0].setCreateTime(TimeUtils.currentLocalTime());
forwardedMessage[0].setRoomId(destinationRoomId);
forwardedMessage[0].setStatus(ProtoGlobal.RoomMessageStatus.SENDING.toString());
forwardedMessage[0].setMessageType(ProtoGlobal.RoomMessageType.forNumber(sourceMessage.messageType));
forwardedMessage[0].setMessage(sourceMessage.message);
if (sourceMessage.attachment != null) {
AttachmentObject attObject = sourceMessage.attachment;
LocalFileType type = attObject.filePath == null ? LocalFileType.THUMBNAIL : LocalFileType.FILE;
String filePath = attObject.filePath != null ? attObject.filePath : attObject.thumbnailPath;
forwardedMessage[0].setAttachment(newMessageId, filePath, attObject.width, attObject.height, attObject.size, attObject.name, attObject.duration, attObject.token, type);
}
if (sourceMessage.contact != null) {
RoomContactObject contactObject = sourceMessage.contact;
ProtoGlobal.RoomMessageContact.Builder builder = ProtoGlobal.RoomMessageContact.newBuilder();
builder.setPhone(0, contactObject.phones.get(0));
builder.setFirstName(contactObject.firstName);
builder.setLastName(contactObject.lastName);
builder.setEmail(0, contactObject.emails.get(0));
RealmRoomMessageContact roomMessageContact = RealmRoomMessageContact.put(database, builder.build());
forwardedMessage[0].setRoomMessageContact(roomMessageContact);
}
forwardedMessage[0].setUserId(AccountManager.getInstance().getCurrentUser().getId());
database.copyToRealmOrUpdate(forwardedMessage[0]);
copyMessage = database.copyFromRealm(forwardedMessage[0]);
} else {
RealmRoomMessage roomMessage = database.where(RealmRoomMessage.class).equalTo("messageId", sourceMessage.id).findFirst();
if (roomMessage != null) {
forwardedMessage[0] = database.createObject(RealmRoomMessage.class, newMessageId);
if (roomMessage.getForwardMessage() != null) {
forwardedMessage[0].setForwardMessage(roomMessage.getForwardMessage());
forwardedMessage[0].setHasMessageLink(roomMessage.getForwardMessage().getHasMessageLink());
} else {
forwardedMessage[0].setForwardMessage(roomMessage);
forwardedMessage[0].setHasMessageLink(roomMessage.getHasMessageLink());
}
forwardedMessage[0].setCreateTime(TimeUtils.currentLocalTime());
forwardedMessage[0].setMessageType(ProtoGlobal.RoomMessageType.TEXT);
forwardedMessage[0].setRoomId(destinationRoomId);
forwardedMessage[0].setStatus(ProtoGlobal.RoomMessageStatus.SENDING.toString());
forwardedMessage[0].setUserId(AccountManager.getInstance().getCurrentUser().getId());
copyMessage = database.copyFromRealm(forwardedMessage[0]);
}
}
messageObject = MessageObject.create(copyMessage);
RealmRoomMessage realmSourceMessage = database.where(RealmRoomMessage.class).equalTo("messageId", sourceMessage.id).findFirst();
assert realmSourceMessage != null;
RealmRoomMessage copyOfSource = database.copyFromRealm(realmSourceMessage);
database.commitTransaction();
MessageObject finalMessageObject = messageObject;
RealmRoomMessage finalCopyMessage = copyMessage;
G.runOnUiThread(() -> {
databaseDelegate.run(finalMessageObject, finalCopyMessage, copyOfSource.getRoomId(), copyOfSource.getMessageId());
});
} catch (Exception e) {
FileLog.e(e);
}
});
}
use of net.iGap.structs.MessageObject in project iGap-Android by KianIranian-STDG.
the class BotInit method onClick.
@Override
public void onClick(View v, ButtonEntity buttonEntity) {
DbManager.getInstance().doRealmTask(realm -> {
try {
if (v.getId() == ButtonActionType.USERNAME_LINK) {
// TODO: fixed this and do not use G.currentActivity
HelperUrl.checkUsernameAndGoToRoomWithMessageId(G.currentActivity, ((ArrayList<String>) v.getTag()).get(0).substring(1), HelperUrl.ChatEntry.chat, 0, 0);
} else if (v.getId() == ButtonActionType.BOT_ACTION) {
try {
Long identity = System.currentTimeMillis();
realm.executeTransaction(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
RealmRoomMessage realmRoomMessage = RealmRoomMessage.makeAdditionalData(roomId, identity, ((ArrayList<String>) v.getTag()).get(1), ((ArrayList<String>) v.getTag()).get(2), 3, realm, ProtoGlobal.RoomMessageType.TEXT);
MessageObject botMessage = MessageObject.create(realmRoomMessage);
ChatSendMessageUtil.getInstance(AccountManager.selectedAccount).build(ProtoGlobal.Room.Type.CHAT, roomId, botMessage);
if (G.onBotClick != null) {
G.onBotClick.onBotCommandText(realmRoomMessage, ButtonActionType.BOT_ACTION);
}
}
});
} catch (Exception e) {
}
} else if (v.getId() == ButtonActionType.JOIN_LINK) {
HelperUrl.checkAndJoinToRoom(G.currentActivity, ((ArrayList<String>) v.getTag()).get(0).substring(14));
} else if (v.getId() == ButtonActionType.WEB_LINK) {
HelperUrl.openBrowser(((ArrayList<String>) v.getTag()).get(0));
} else if (v.getId() == ButtonActionType.WEBVIEW_LINK) {
G.onBotClick.onBotCommandText(((ArrayList<String>) v.getTag()).get(0), ButtonActionType.WEBVIEW_LINK);
} else if (v.getId() == ButtonActionType.REQUEST_PHONE) {
try {
new MaterialDialog.Builder(G.currentActivity).title(R.string.access_phone_number).positiveText(R.string.ok).negativeText(R.string.cancel).onPositive(new MaterialDialog.SingleButtonCallback() {
@Override
public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
Long identity = System.currentTimeMillis();
realm.executeTransaction(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
RealmUserInfo realmUserInfo = RealmUserInfo.getRealmUserInfo(realm);
RealmRoomMessage realmRoomMessage = RealmRoomMessage.makeAdditionalData(roomId, identity, realmUserInfo.getUserInfo().getPhoneNumber(), realmUserInfo.getUserInfo().getPhoneNumber(), 0, realm, ProtoGlobal.RoomMessageType.TEXT);
MessageObject botMessage = MessageObject.create(realmRoomMessage);
ChatSendMessageUtil.getInstance(AccountManager.selectedAccount).build(ProtoGlobal.Room.Type.CHAT, roomId, botMessage);
if (G.onBotClick != null) {
G.onBotClick.onBotCommandText(realmRoomMessage, ButtonActionType.BOT_ACTION);
}
}
});
}
}).show();
} catch (Exception e) {
}
} else if (v.getId() == ButtonActionType.REQUEST_LOCATION) {
try {
new MaterialDialog.Builder(G.currentActivity).title(R.string.access_location).positiveText(R.string.ok).negativeText(R.string.cancel).onPositive(new MaterialDialog.SingleButtonCallback() {
@Override
public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
Boolean response = false;
if (G.locationListener != null) {
isLocationFromBot = true;
G.locationListener.requestLocation();
}
/* G.locationListenerResponse = new LocationListenerResponse() {
@Override
public void setLocationResponse(Double latitude, Double longitude) {
Long identity = System.currentTimeMillis();
realm.executeTransaction(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
RealmRoomMessage realmRoomMessage = RealmRoomMessage.makeAdditionalData(roomId, identity, latitude + "," + longitude, ((ArrayList<String>) v.getTag()).get(2).toString(), 3, realm, ProtoGlobal.RoomMessageType.TEXT);
G.chatSendMessageUtil.build(ProtoGlobal.Room.Type.CHAT, roomId, realmRoomMessage).sendMessage(identity + "");
if (G.onBotClick != null) {
G.onBotClick.onBotCommandText(realmRoomMessage, ButtonActionType.BOT_ACTION);
}
}
});
}
};*/
}
}).show();
} catch (Exception e) {
}
}
} catch (Exception e) {
Toast.makeText(G.context, "دستور با خطا مواجه شد", Toast.LENGTH_LONG).show();
}
});
}
use of net.iGap.structs.MessageObject in project iGap-Android by KianIranian-STDG.
the class UploadManager method uploadMessageAndSend.
public void uploadMessageAndSend(ProtoGlobal.Room.Type roomType, RealmRoomMessage message, boolean ignoreCompress) {
Log.d("bagi", "uploadMessageAndSend" + message.getMessageId());
if (message.isManaged()) {
uploadMessageAndSend(roomType, DbManager.getInstance().doRealmTask(realm -> {
return realm.copyFromRealm(message);
}), ignoreCompress);
return;
}
if (message.getForwardMessage() != null || message.getAttachment() == null || message.getMessageType() == ProtoGlobal.RoomMessageType.STICKER || message.getMessageType() == ProtoGlobal.RoomMessageType.CONTACT) {
MessageObject uploadMessage = MessageObject.create(message);
ChatSendMessageUtil.getInstance(AccountManager.selectedAccount).build(roomType, message.getRoomId(), uploadMessage);
return;
}
Log.d("bagi", "uploadMessageAndSend222");
String savePathVideoCompress = G.DIR_TEMP + "/VIDEO_" + message.getMessageId() + ".mp4";
File compressFile = new File(savePathVideoCompress);
File CompletedCompressFile = new File(savePathVideoCompress.replace(".mp4", "_finish.mp4"));
if (!CompletedCompressFile.exists() && !ignoreCompress && (message.getMessageType() == ProtoGlobal.RoomMessageType.VIDEO || message.getMessageType() == ProtoGlobal.RoomMessageType.VIDEO_TEXT)) {
if (pendingCompressTasks.containsKey(message.getMessageId() + ""))
return;
Log.d("bagi", "uploadMessageAndSend33");
CompressTask compressTask = new CompressTask(message.getMessageId() + "", message.getAttachment().getLocalFilePath(), savePathVideoCompress, new OnCompress() {
@Override
public void onCompressProgress(String id, int percent) {
Log.d("bagi", "onCompressProgress" + percent);
G.runOnUiThread(() -> EventManager.getInstance(AccountManager.selectedAccount).postEvent(EventManager.ON_UPLOAD_COMPRESS, id, percent));
}
@Override
public void onCompressFinish(String id, boolean compress, boolean isCancel) {
Log.d("bagi", "onCompressFinish" + message.getMessageId());
if (compress && compressFile.exists() && compressFile.length() < (new File(message.getAttachment().getLocalFilePath())).length()) {
compressFile.renameTo(CompletedCompressFile);
message.attachment.size = CompletedCompressFile.length();
G.runOnUiThread(() -> EventManager.getInstance(AccountManager.selectedAccount).postEvent(EventManager.ON_UPLOAD_COMPRESS, id, 100, CompletedCompressFile.length()));
uploadMessageAndSend(roomType, message, ignoreCompress);
} else {
if (compressFile.exists()) {
compressFile.delete();
}
G.runOnUiThread(() -> EventManager.getInstance(AccountManager.selectedAccount).postEvent(EventManager.ON_UPLOAD_COMPRESS, id, 100));
uploadMessageAndSend(roomType, message, true);
}
}
});
if (compressFile.exists()) {
compressFile.delete();
}
pendingCompressTasks.put(message.getMessageId() + "", compressTask);
mThreadPoolExecutor.execute(compressTask);
return;
}
CompressTask compressTask = pendingCompressTasks.remove(message.getMessageId() + "");
if ((message.getMessageType() == ProtoGlobal.RoomMessageType.VIDEO || message.getMessageType() == ProtoGlobal.RoomMessageType.VIDEO_TEXT) && !ignoreCompress && (compressTask == null || !CompletedCompressFile.exists()))
return;
Log.d("bagi", "after Compress");
OnUploadListener onUploadListener = new OnUploadListener() {
@Override
public void onProgress(String id, int progress) {
Log.d("bagi", progress + "uploadMessageAndSend2");
G.runOnUiThread(() -> EventManager.getInstance(AccountManager.selectedAccount).postEvent(EventManager.ON_UPLOAD_PROGRESS, id, progress, message.attachment.size));
}
@Override
public void onFinish(String id, String token) {
Log.d("bagi", "uploadMessageAndSendonFinish");
if (CompletedCompressFile.exists()) {
CompletedCompressFile.delete();
}
HelperSetAction.sendCancel(message.getMessageId());
DbManager.getInstance().doRealmTransaction(realm -> {
RealmAttachment.updateToken(message.getMessageId(), token);
});
/**
* this code should exist in under of other codes in this block
*/
if (message.getReplyTo() == null) {
ChatSendMessageUtil.getInstance(AccountManager.selectedAccount).newBuilder(roomType, message.getMessageType(), message.getRoomId()).attachment(token).message(message.getMessage()).sendMessage(message.getMessageId() + "");
} else {
ChatSendMessageUtil.getInstance(AccountManager.selectedAccount).newBuilder(roomType, message.getMessageType(), message.getRoomId()).replyMessage(message.getReplyTo().getMessageId()).attachment(token).message(message.getMessage()).sendMessage(message.getMessageId() + "");
}
pendingUploadTasks.remove(id);
}
@Override
public void onError(String id) {
Log.d("bagi", "uploadMessageAndSendError");
pendingUploadTasks.remove(id);
HelperSetAction.sendCancel(Long.parseLong(id));
makeFailed(id);
}
};
UploadTask uploadTask;
if (CompletedCompressFile.exists()) {
uploadTask = new UploadTask(message, CompletedCompressFile.getAbsolutePath(), onUploadListener);
} else {
uploadTask = new UploadTask(message, onUploadListener);
}
if (!pendingUploadTasks.containsKey(uploadTask.identity)) {
pendingUploadTasks.put(uploadTask.identity, uploadTask);
mThreadPoolExecutor.execute(uploadTask);
HelperSetAction.setActionFiles(message.getRoomId(), message.getMessageId(), HelperSetAction.getAction(message.getMessageType()), roomType);
G.runOnUiThread(() -> EventManager.getInstance(AccountManager.selectedAccount).postEvent(EventManager.ON_UPLOAD_PROGRESS, message.getMessageId() + "", 1));
}
}
Aggregations