diff --git a/MessengerProj/jni/mrwrapper.c b/MessengerProj/jni/mrwrapper.c index 1ff4f5333..09c97c9ec 100644 --- a/MessengerProj/jni/mrwrapper.c +++ b/MessengerProj/jni/mrwrapper.c @@ -549,69 +549,69 @@ JNIEXPORT jintArray Java_com_b44t_messenger_MrMailbox_MrMailboxGetChatContacts(J ******************************************************************************/ -JNIEXPORT void Java_com_b44t_messenger_MrMailbox_MrMsgUnref(JNIEnv *env, jclass c, jlong hMsg) +JNIEXPORT void Java_com_b44t_messenger_MrMsg_MrMsgUnref(JNIEnv *env, jclass c, jlong hMsg) { mrmsg_unref((mrmsg_t*)hMsg); } -JNIEXPORT jint Java_com_b44t_messenger_MrMailbox_MrMsgGetId(JNIEnv *env, jclass c, jlong hMsg) +JNIEXPORT jint Java_com_b44t_messenger_MrMsg_MrMsgGetId(JNIEnv *env, jclass c, jlong hMsg) { mrmsg_t* ths = (mrmsg_t*)hMsg; if( ths == NULL ) { return 0; } return ths->m_id; } -JNIEXPORT jstring Java_com_b44t_messenger_MrMailbox_MrMsgGetText(JNIEnv *env, jclass c, jlong hMsg) +JNIEXPORT jstring Java_com_b44t_messenger_MrMsg_MrMsgGetText(JNIEnv *env, jclass c, jlong hMsg) { mrmsg_t* ths = (mrmsg_t*)hMsg; if( ths == NULL ) { return JSTRING_NEW(NULL); } return JSTRING_NEW(ths->m_text); } -JNIEXPORT jlong Java_com_b44t_messenger_MrMailbox_MrMsgGetTimestamp(JNIEnv *env, jclass c, jlong hMsg) +JNIEXPORT jlong Java_com_b44t_messenger_MrMsg_MrMsgGetTimestamp(JNIEnv *env, jclass c, jlong hMsg) { mrmsg_t* ths = (mrmsg_t*)hMsg; if( ths == NULL ) { return 0; } return ths->m_timestamp; } -JNIEXPORT jint Java_com_b44t_messenger_MrMailbox_MrMsgGetType(JNIEnv *env, jclass c, jlong hMsg) +JNIEXPORT jint Java_com_b44t_messenger_MrMsg_MrMsgGetType(JNIEnv *env, jclass c, jlong hMsg) { mrmsg_t* ths = (mrmsg_t*)hMsg; if( ths == NULL ) { return 0; } return ths->m_type; } -JNIEXPORT jint Java_com_b44t_messenger_MrMailbox_MrMsgGetState(JNIEnv *env, jclass c, jlong hMsg) +JNIEXPORT jint Java_com_b44t_messenger_MrMsg_MrMsgGetState(JNIEnv *env, jclass c, jlong hMsg) { mrmsg_t* ths = (mrmsg_t*)hMsg; if( ths == NULL ) { return 0; } return ths->m_state; } -JNIEXPORT jint Java_com_b44t_messenger_MrMailbox_MrMsgGetChatId(JNIEnv *env, jclass c, jlong hMsg) +JNIEXPORT jint Java_com_b44t_messenger_MrMsg_MrMsgGetChatId(JNIEnv *env, jclass c, jlong hMsg) { mrmsg_t* ths = (mrmsg_t*)hMsg; if( ths == NULL ) { return 0; } return ths->m_chat_id; } -JNIEXPORT jint Java_com_b44t_messenger_MrMailbox_MrMsgGetFromId(JNIEnv *env, jclass c, jlong hMsg) +JNIEXPORT jint Java_com_b44t_messenger_MrMsg_MrMsgGetFromId(JNIEnv *env, jclass c, jlong hMsg) { mrmsg_t* ths = (mrmsg_t*)hMsg; if( ths == NULL ) { return 0; } return ths->m_from_id; } -JNIEXPORT jint Java_com_b44t_messenger_MrMailbox_MrMsgGetToId(JNIEnv *env, jclass c, jlong hMsg) +JNIEXPORT jint Java_com_b44t_messenger_MrMsg_MrMsgGetToId(JNIEnv *env, jclass c, jlong hMsg) { mrmsg_t* ths = (mrmsg_t*)hMsg; if( ths == NULL ) { return 0; } return ths->m_to_id; } -JNIEXPORT jstring Java_com_b44t_messenger_MrMailbox_MrMsgGetParam(JNIEnv *env, jclass c, jlong hMsg, jint key, jstring def) +JNIEXPORT jstring Java_com_b44t_messenger_MrMsg_MrMsgGetParam(JNIEnv *env, jclass c, jlong hMsg, jint key, jstring def) { mrmsg_t* ths = (mrmsg_t*)hMsg; CHAR_REF(def); @@ -623,7 +623,7 @@ JNIEXPORT jstring Java_com_b44t_messenger_MrMailbox_MrMsgGetParam(JNIEnv *env, j } -JNIEXPORT jint Java_com_b44t_messenger_MrMailbox_MrMsgGetParamInt(JNIEnv *env, jclass c, jlong hMsg, jint key, jint def) +JNIEXPORT jint Java_com_b44t_messenger_MrMsg_MrMsgGetParamInt(JNIEnv *env, jclass c, jlong hMsg, jint key, jint def) { mrmsg_t* ths = (mrmsg_t*)hMsg; return mrparam_get_int(ths? ths->m_param:NULL, key, def); diff --git a/MessengerProj/src/main/java/com/b44t/messenger/MrMailbox.java b/MessengerProj/src/main/java/com/b44t/messenger/MrMailbox.java index f72ff4926..9d26899e6 100644 --- a/MessengerProj/src/main/java/com/b44t/messenger/MrMailbox.java +++ b/MessengerProj/src/main/java/com/b44t/messenger/MrMailbox.java @@ -21,7 +21,7 @@ * * File: MrMailbox.java * Authors: Björn Petersen - * Purpose: The Java part of the Java<->C Wrapper, see also mrwrapper.c + * Purpose: Wrap around mrmailbox_t * ******************************************************************************/ @@ -53,14 +53,14 @@ public class MrMailbox { return dlg; } - public static TLRPC.Message hMsg2Message(long hMsg) + public static TLRPC.Message MrMsg2Message(MrMsg mrMsg) { TLRPC.Message ret = new TLRPC.TL_message(); // the class derived from TLRPC.Message defines the basic type: // TLRPC.TL_messageService is used to display messages as "You joined the group" // TLRPC.TL_message is a normal message (also photos?) - int state = MrMsgGetState(hMsg); - int type = MrMsgGetType(hMsg); + int state = mrMsg.getState(); + int type = mrMsg.getType(); switch( state ) { case MR_OUT_DELIVERED: ret.send_state = MessageObject.MESSAGE_SEND_STATE_SENT; break; case MR_OUT_ERROR: ret.send_state = MessageObject.MESSAGE_SEND_STATE_SEND_ERROR; break; @@ -68,12 +68,12 @@ public class MrMailbox { case MR_OUT_READ: ret.send_state = MessageObject.MESSAGE_SEND_STATE_SENT; break; } - ret.id = MrMailbox.MrMsgGetId(hMsg); - ret.from_id = MrMailbox.MrMsgGetFromId(hMsg); + ret.id = mrMsg.getId(); + ret.from_id = mrMsg.getFromId(); ret.to_id = new TLRPC.TL_peerUser(); - ret.to_id.user_id = MrMailbox.MrMsgGetToId(hMsg); - ret.date = (int)MrMsgGetTimestamp(hMsg); - ret.dialog_id = MrMsgGetChatId(hMsg); + ret.to_id.user_id = mrMsg.getToId(); + ret.date = (int)mrMsg.getTimestamp(); + ret.dialog_id = mrMsg.getChatId(); ret.unread = state!=MR_OUT_READ; // the state of outgoing messages ret.media_unread = ret.unread; ret.flags = 0; // posible flags: MESSAGE_FLAG_HAS_FROM_ID, however, this seems to be read only @@ -82,16 +82,16 @@ public class MrMailbox { ret.created_by_mr = true; if( type == MrMailbox.MR_MSG_TEXT ) { - ret.message = MrMailbox.MrMsgGetText(hMsg); + ret.message = mrMsg.getText(); } else if( type == MrMailbox.MR_MSG_IMAGE ) { - String path = MrMailbox.MrMsgGetParam(hMsg, 'f', ""); + String path = mrMsg.getParam('f', ""); TLRPC.TL_photo photo = null; if( !path.isEmpty() ) { try { TLRPC.TL_photoSize photoSize = new TLRPC.TL_photoSize(); - photoSize.w = MrMailbox.MrMsgGetParamInt(hMsg, 'w', 800); - photoSize.h = MrMailbox.MrMsgGetParamInt(hMsg, 'h', 800); + photoSize.w = mrMsg.getParamInt('w', 800); + photoSize.h = mrMsg.getParamInt('h', 800); photoSize.size = 0; photoSize.location = new TLRPC.TL_fileLocation(); photoSize.location.mr_path = path; @@ -115,7 +115,7 @@ public class MrMailbox { } } else if( type == MrMailbox.MR_MSG_AUDIO || type == MrMailbox.MR_MSG_VIDEO ) { - String path = MrMailbox.MrMsgGetParam(hMsg, 'f', ""); + String path = mrMsg.getParam('f', ""); if( !path.isEmpty()) { ret.message = "-1"; // may be misused for video editing information ret.media = new TLRPC.TL_messageMediaDocument(); @@ -125,14 +125,14 @@ public class MrMailbox { if( type == MrMailbox.MR_MSG_AUDIO ) { TLRPC.TL_documentAttributeAudio attr = new TLRPC.TL_documentAttributeAudio(); attr.voice = true; // !voice = music - attr.duration = MrMailbox.MrMsgGetParamInt(hMsg, 'd', 0) / 1000; + attr.duration = mrMsg.getParamInt('d', 0) / 1000; ret.media.document.attributes.add(attr); } else if( type == MrMailbox.MR_MSG_VIDEO) { TLRPC.TL_documentAttributeVideo attr = new TLRPC.TL_documentAttributeVideo(); - attr.duration = MrMailbox.MrMsgGetParamInt(hMsg, 'd', 0) / 1000; - attr.w = MrMailbox.MrMsgGetParamInt(hMsg, 'w', 0); - attr.h = MrMailbox.MrMsgGetParamInt(hMsg, 'h', 0); + attr.duration = mrMsg.getParamInt('d', 0) / 1000; + attr.w = mrMsg.getParamInt('w', 0); + attr.h = mrMsg.getParamInt('h', 0); ret.media.document.attributes.add(attr); } @@ -214,6 +214,11 @@ public class MrMailbox { } // MrMailbox objects + public static MrMsg getMsg(long hMailbox, int id) + { + return new MrMsg(MrMailboxGetMsg(hMailbox, id)); + } + public native static long MrMailboxNew (); // returns hMailbox which must be unref'd after usage (Names as mrmailbox_new don't work due to the additional underscore) public native static int MrMailboxOpen (long hMailbox, String dbfile, String blobdir); public native static void MrMailboxClose (long hMailbox); @@ -240,7 +245,7 @@ public class MrMailbox { public native static int[] MrMailboxGetChatMedia (long hMailbox, int chat_id, int msg_type, int or_msg_type); public native static int[] MrMailboxGetChatContacts (long hMailbox, int chat_id); public native static int MrMailboxDeleteChat (long hMailbox, int chat_id); - public native static long MrMailboxGetMsg (long hMailbox, int id); // return hMsg which must be unref'd after usage + private native static long MrMailboxGetMsg (long hMailbox, int id); // return hMsg which must be unref'd after usage public native static String MrMailboxGetMsgInfo (long hMailbox, int id); public native static void MrMailboxDeleteMsg (long hMailbox, int id); public native static int MrMailboxSetConfig (long hMailbox, String key, String value); // value may be NULL @@ -272,19 +277,6 @@ public class MrMailbox { public native static int MrChatSendText (long hChat, String text); // returns message id public native static int MrChatSendMedia (long hChat, int type, String file, String mime, int w, int h, int time_ms); - // MrMsg objects - public native static void MrMsgUnref (long hMsg); - public native static int MrMsgGetId (long hMsg); - public native static String MrMsgGetText (long hMsg); - public native static long MrMsgGetTimestamp (long hMsg); - public native static int MrMsgGetType (long hMsg); - public native static int MrMsgGetState (long hMsg); - public native static int MrMsgGetChatId (long hMsg); - public native static int MrMsgGetFromId (long hMsg); - public native static int MrMsgGetToId (long hMsg); - public native static String MrMsgGetParam (long hMsg, int key, String def); - public native static int MrMsgGetParamInt (long hMsg, int key, int def); - // MrContact objects public native static void MrContactUnref (long hContact); public native static String MrContactGetName (long hContact); diff --git a/MessengerProj/src/main/java/com/b44t/messenger/MrMsg.java b/MessengerProj/src/main/java/com/b44t/messenger/MrMsg.java new file mode 100644 index 000000000..d20d11e1d --- /dev/null +++ b/MessengerProj/src/main/java/com/b44t/messenger/MrMsg.java @@ -0,0 +1,90 @@ +/******************************************************************************* + * + * Messenger Android Frontend + * Copyright (C) 2016 Björn Petersen Software Design and Development + * Contact: r10s@b44t.com, http://b44t.com + * + * This program is free software: you can redistribute it and/or modify it under + * the terms of the GNU General Public License as published by the Free Software + * Foundation, either version 3 of the License, or (at your option) any later + * version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License along with + * this program. If not, see http://www.gnu.org/licenses/ . + * + ******************************************************************************* + * + * File: MrMailbox.java + * Authors: Björn Petersen + * Purpose: Wrap around mrmsg_t + * + ******************************************************************************/ + + +package com.b44t.messenger; + +public class MrMsg { + + public MrMsg(long hMsg) { + m_hMsg = hMsg; + } + + @Override protected void finalize() throws Throwable { + super.finalize(); + MrMsgUnref(m_hMsg); + } + + public int getId() { + return MrMsgGetId(m_hMsg); + } + + public String getText() { + return MrMsgGetText(m_hMsg); + } + + public long getTimestamp() { + return MrMsgGetTimestamp(m_hMsg); + } + + public int getType() { + return MrMsgGetType(m_hMsg); + } + + public int getState() { + return MrMsgGetState(m_hMsg); + } + public int getChatId() { + return MrMsgGetChatId(m_hMsg); + } + public int getFromId() { + return MrMsgGetFromId(m_hMsg); + } + public int getToId() { + return MrMsgGetToId(m_hMsg); + } + + public String getParam (int key, String def) { + return MrMsgGetParam(m_hMsg, key, def); + } + public int getParamInt(int key, int def) { + return MrMsgGetParamInt(m_hMsg, key, def); + } + + private long m_hMsg; + private native static void MrMsgUnref (long hMsg); + private native static int MrMsgGetId (long hMsg); + private native static String MrMsgGetText (long hMsg); + private native static long MrMsgGetTimestamp (long hMsg); + private native static int MrMsgGetType (long hMsg); + private native static int MrMsgGetState (long hMsg); + private native static int MrMsgGetChatId (long hMsg); + private native static int MrMsgGetFromId (long hMsg); + private native static int MrMsgGetToId (long hMsg); + private native static String MrMsgGetParam (long hMsg, int key, String def); + private native static int MrMsgGetParamInt (long hMsg, int key, int def); +}; diff --git a/MessengerProj/src/main/java/com/b44t/ui/ChatActivity.java b/MessengerProj/src/main/java/com/b44t/ui/ChatActivity.java index 44c87ad59..c1c188920 100644 --- a/MessengerProj/src/main/java/com/b44t/ui/ChatActivity.java +++ b/MessengerProj/src/main/java/com/b44t/ui/ChatActivity.java @@ -64,6 +64,7 @@ import com.b44t.messenger.Emoji; import com.b44t.messenger.LocaleController; import com.b44t.messenger.MediaController; import com.b44t.messenger.MrMailbox; +import com.b44t.messenger.MrMsg; import com.b44t.messenger.NotificationsController; import com.b44t.messenger.SendMessagesHelper; import com.b44t.messenger.UserObject; @@ -2860,10 +2861,9 @@ public class ChatActivity extends BaseFragment implements NotificationCenter.Not chatActivityEnterView.closeKeyboard(); } - long hMsg = MrMailbox.MrMailboxGetMsg(MrMailbox.hMailbox, messageId); - if( hMsg==0 ) { return; } + MrMsg mrMsg = MrMailbox.getMsg(MrMailbox.hMailbox, messageId); - final int fromId = MrMailbox.MrMsgGetFromId(hMsg); + final int fromId =mrMsg.getFromId(); long hContact = MrMailbox.MrMailboxGetContact(MrMailbox.hMailbox, fromId); String name = MrMailbox.MrContactGetNameNAddr(hContact); MrMailbox.MrContactUnref(hContact); @@ -2887,8 +2887,6 @@ public class ChatActivity extends BaseFragment implements NotificationCenter.Not builder.setNegativeButton(LocaleController.getString("Cancel", R.string.Cancel), null); builder.setMessage(AndroidUtilities.replaceTags(LocaleController.formatString("AskStartChatWith", R.string.AskStartChatWith, name))); showDialog(builder.create()); - - MrMailbox.MrMsgUnref(hMsg); } private void scrollToMessageId(int id, int fromMessageId, boolean select, int loadIndex) { @@ -3643,12 +3641,11 @@ public class ChatActivity extends BaseFragment implements NotificationCenter.Not int mrCount = msglist.length; for (int a = mrCount - 1; a >= 0; a--) { - long hMsg = MrMailbox.MrMailboxGetMsg(MrMailbox.hMailbox, msglist[a]); - TLRPC.Message msg = MrMailbox.hMsg2Message(hMsg); + MrMsg mrMsg = MrMailbox.getMsg(MrMailbox.hMailbox, msglist[a]); + TLRPC.Message msg = MrMailbox.MrMsg2Message(mrMsg); MessageObject msgDrawObj = new MessageObject(msg, null, true); messages.add(0, msgDrawObj); messagesDict[loadIndex].put(msg.id, msgDrawObj); - MrMailbox.MrMsgUnref(hMsg); } if (MrMailbox.MrChatGetId(m_hChat) == MrMailbox.MR_CHAT_ID_DEADDROP) {