mirror of
https://github.com/deltachat/deltachat-android.git
synced 2025-10-06 03:49:58 +02:00
handle text drafts, tackles #73
This commit is contained in:
parent
983ee6bb80
commit
1750c07f4d
4 changed files with 74 additions and 78 deletions
|
@ -452,11 +452,15 @@ JNIEXPORT jint Java_com_b44t_messenger_DcContext_removeContactFromChat(JNIEnv *e
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
JNIEXPORT void Java_com_b44t_messenger_DcContext_setDraft(JNIEnv *env, jobject obj, jint chat_id, jstring draft /* NULL=delete */)
|
JNIEXPORT void Java_com_b44t_messenger_DcContext_setDraft(JNIEnv *env, jobject obj, jint chat_id, jobject msg /* NULL=delete */)
|
||||||
{
|
{
|
||||||
CHAR_REF(draft);
|
dc_set_draft(get_dc_context(env, obj), chat_id, get_dc_msg(env, msg));
|
||||||
dc_set_text_draft(get_dc_context(env, obj), chat_id, draftPtr /* NULL=delete */);
|
}
|
||||||
CHAR_UNREF(draft);
|
|
||||||
|
|
||||||
|
JNIEXPORT jlong Java_com_b44t_messenger_DcContext_getDraftCPtr(JNIEnv *env, jobject obj, jint chat_id)
|
||||||
|
{
|
||||||
|
return (jlong)dc_get_draft(get_dc_context(env, obj), chat_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -834,21 +838,6 @@ JNIEXPORT jboolean Java_com_b44t_messenger_DcChat_isVerified(JNIEnv *env, jobjec
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
JNIEXPORT jstring Java_com_b44t_messenger_DcChat_getDraft(JNIEnv *env, jobject obj) /* returns NULL for "no draft" */
|
|
||||||
{
|
|
||||||
const char* temp = dc_chat_get_text_draft(get_dc_chat(env, obj));
|
|
||||||
jstring ret = temp? JSTRING_NEW(temp) : NULL;
|
|
||||||
free(temp);
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
JNIEXPORT jlong Java_com_b44t_messenger_DcChat_getDraftTimestamp(JNIEnv *env, jobject obj)
|
|
||||||
{
|
|
||||||
return JTIMESTAMP(dc_chat_get_draft_timestamp(get_dc_chat(env, obj)));
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
JNIEXPORT jintArray Java_com_b44t_messenger_DcContext_getChatMedia(JNIEnv *env, jobject obj, jint chat_id, jint msg_type, jint or_msg_type)
|
JNIEXPORT jintArray Java_com_b44t_messenger_DcContext_getChatMedia(JNIEnv *env, jobject obj, jint chat_id, jint msg_type, jint or_msg_type)
|
||||||
{
|
{
|
||||||
dc_array_t* ca = dc_get_chat_media(get_dc_context(env, obj), chat_id, msg_type, or_msg_type);
|
dc_array_t* ca = dc_get_chat_media(get_dc_context(env, obj), chat_id, msg_type, or_msg_type);
|
||||||
|
@ -900,12 +889,14 @@ JNIEXPORT jintArray Java_com_b44t_messenger_DcContext_getChatContacts(JNIEnv *en
|
||||||
static dc_msg_t* get_dc_msg(JNIEnv *env, jobject obj)
|
static dc_msg_t* get_dc_msg(JNIEnv *env, jobject obj)
|
||||||
{
|
{
|
||||||
static jfieldID fid = 0;
|
static jfieldID fid = 0;
|
||||||
if (fid==0) {
|
if (env && obj) {
|
||||||
jclass cls = (*env)->GetObjectClass(env, obj);
|
if (fid==0) {
|
||||||
fid = (*env)->GetFieldID(env, cls, "msgCPtr", "J" /*Signature, J=long*/);
|
jclass cls = (*env)->GetObjectClass(env, obj);
|
||||||
}
|
fid = (*env)->GetFieldID(env, cls, "msgCPtr", "J" /*Signature, J=long*/);
|
||||||
if (fid) {
|
}
|
||||||
return (dc_msg_t*)(*env)->GetLongField(env, obj, fid);
|
if (fid) {
|
||||||
|
return (dc_msg_t*)(*env)->GetLongField(env, obj, fid);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
|
@ -28,8 +28,6 @@ public class DcChat {
|
||||||
public native boolean isUnpromoted ();
|
public native boolean isUnpromoted ();
|
||||||
public native boolean isSelfTalk ();
|
public native boolean isSelfTalk ();
|
||||||
public native boolean isVerified ();
|
public native boolean isVerified ();
|
||||||
public native String getDraft ();
|
|
||||||
public native long getDraftTimestamp ();
|
|
||||||
|
|
||||||
// working with raw c-data
|
// working with raw c-data
|
||||||
private long chatCPtr; // CAVE: the name is referenced in the JNI
|
private long chatCPtr; // CAVE: the name is referenced in the JNI
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package com.b44t.messenger;
|
package com.b44t.messenger;
|
||||||
|
|
||||||
import android.support.annotation.NonNull;
|
import android.support.annotation.NonNull;
|
||||||
|
import android.support.annotation.Nullable;
|
||||||
|
|
||||||
public class DcContext {
|
public class DcContext {
|
||||||
|
|
||||||
|
@ -106,7 +107,8 @@ public class DcContext {
|
||||||
public native boolean isContactInChat (int chat_id, int contact_id);
|
public native boolean isContactInChat (int chat_id, int contact_id);
|
||||||
public native int addContactToChat (int chat_id, int contact_id);
|
public native int addContactToChat (int chat_id, int contact_id);
|
||||||
public native int removeContactFromChat(int chat_id, int contact_id);
|
public native int removeContactFromChat(int chat_id, int contact_id);
|
||||||
public native void setDraft (int chat_id, String draft/*null=delete*/);
|
public native void setDraft (int chat_id, DcMsg msg/*null=delete*/);
|
||||||
|
public @Nullable DcMsg getDraft (int chat_id) { return new DcMsg(getDraftCPtr(chat_id)); }
|
||||||
public native int setChatName (int chat_id, String name);
|
public native int setChatName (int chat_id, String name);
|
||||||
public native int setChatProfileImage (int chat_id, String name);
|
public native int setChatProfileImage (int chat_id, String name);
|
||||||
public native int[] getChatMsgs (int chat_id, int flags, int marker1before);
|
public native int[] getChatMsgs (int chat_id, int flags, int marker1before);
|
||||||
|
@ -146,5 +148,6 @@ public class DcContext {
|
||||||
private native long getChatlistCPtr (int listflags, String query, int queryId);
|
private native long getChatlistCPtr (int listflags, String query, int queryId);
|
||||||
private native long getChatCPtr (int chat_id);
|
private native long getChatCPtr (int chat_id);
|
||||||
private native long getMsgCPtr (int id);
|
private native long getMsgCPtr (int id);
|
||||||
|
private native long getDraftCPtr (int id);
|
||||||
private native long getContactCPtr (int id);
|
private native long getContactCPtr (int id);
|
||||||
}
|
}
|
||||||
|
|
|
@ -655,61 +655,63 @@ public class ConversationActivity extends PassphraseRequiredActionBarActivity
|
||||||
private ListenableFuture<Boolean> initializeDraftFromDatabase() {
|
private ListenableFuture<Boolean> initializeDraftFromDatabase() {
|
||||||
SettableFuture<Boolean> future = new SettableFuture<>();
|
SettableFuture<Boolean> future = new SettableFuture<>();
|
||||||
|
|
||||||
new AsyncTask<Void, Void, List<Draft>>() {
|
new AsyncTask<Void, Void, DcMsg>() {
|
||||||
@Override
|
@Override
|
||||||
protected List<Draft> doInBackground(Void... params) {
|
protected DcMsg doInBackground(Void... params) {
|
||||||
DraftDatabase draftDatabase = DatabaseFactory.getDraftDatabase(ConversationActivity.this);
|
return dcContext.getDraft(threadId);
|
||||||
List<Draft> results = draftDatabase.getDrafts(threadId);
|
|
||||||
|
|
||||||
draftDatabase.clearDrafts(threadId);
|
|
||||||
|
|
||||||
return results;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onPostExecute(List<Draft> drafts) {
|
protected void onPostExecute(DcMsg draft) {
|
||||||
AtomicInteger draftsRemaining = new AtomicInteger(drafts.size());
|
if(draft!=null) {
|
||||||
AtomicBoolean success = new AtomicBoolean(false);
|
String text = draft.getText();
|
||||||
ListenableFuture.Listener<Boolean> listener = new AssertedSuccessListener<Boolean>() {
|
if(!text.isEmpty()) {
|
||||||
@Override
|
composeText.setText(text);
|
||||||
public void onSuccess(Boolean result) {
|
composeText.setSelection(composeText.getText().length());
|
||||||
success.compareAndSet(false, result);
|
|
||||||
|
|
||||||
if (draftsRemaining.decrementAndGet() <= 0) {
|
|
||||||
future.set(success.get());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
for (Draft draft : drafts) {
|
|
||||||
try {
|
|
||||||
switch (draft.getType()) {
|
|
||||||
case Draft.TEXT:
|
|
||||||
composeText.setText(draft.getValue());
|
|
||||||
listener.onSuccess(true);
|
|
||||||
break;
|
|
||||||
case Draft.LOCATION:
|
|
||||||
attachmentManager.setLocation(SignalPlace.deserialize(draft.getValue()), getCurrentMediaConstraints()).addListener(listener);
|
|
||||||
break;
|
|
||||||
case Draft.IMAGE:
|
|
||||||
setMedia(Uri.parse(draft.getValue()), MediaType.IMAGE).addListener(listener);
|
|
||||||
break;
|
|
||||||
case Draft.AUDIO:
|
|
||||||
setMedia(Uri.parse(draft.getValue()), MediaType.AUDIO).addListener(listener);
|
|
||||||
break;
|
|
||||||
case Draft.VIDEO:
|
|
||||||
setMedia(Uri.parse(draft.getValue()), MediaType.VIDEO).addListener(listener);
|
|
||||||
break;
|
|
||||||
case Draft.QUOTE:
|
|
||||||
SettableFuture<Boolean> quoteResult = new SettableFuture<>();
|
|
||||||
new QuoteRestorationTask(draft.getValue(), quoteResult).execute();
|
|
||||||
quoteResult.addListener(listener);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
} catch (IOException e) {
|
|
||||||
Log.w(TAG, e);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// AtomicInteger draftsRemaining = new AtomicInteger(drafts.size());
|
||||||
|
// AtomicBoolean success = new AtomicBoolean(false);
|
||||||
|
// ListenableFuture.Listener<Boolean> listener = new AssertedSuccessListener<Boolean>() {
|
||||||
|
// @Override
|
||||||
|
// public void onSuccess(Boolean result) {
|
||||||
|
// success.compareAndSet(false, result);
|
||||||
|
//
|
||||||
|
// if (draftsRemaining.decrementAndGet() <= 0) {
|
||||||
|
// future.set(success.get());
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// };
|
||||||
|
//
|
||||||
|
// for (Draft draft : drafts) {
|
||||||
|
// try {
|
||||||
|
// switch (draft.getType()) {
|
||||||
|
// case Draft.TEXT:
|
||||||
|
// composeText.setText(draft.getValue());
|
||||||
|
// listener.onSuccess(true);
|
||||||
|
// break;
|
||||||
|
// case Draft.LOCATION:
|
||||||
|
// attachmentManager.setLocation(SignalPlace.deserialize(draft.getValue()), getCurrentMediaConstraints()).addListener(listener);
|
||||||
|
// break;
|
||||||
|
// case Draft.IMAGE:
|
||||||
|
// setMedia(Uri.parse(draft.getValue()), MediaType.IMAGE).addListener(listener);
|
||||||
|
// break;
|
||||||
|
// case Draft.AUDIO:
|
||||||
|
// setMedia(Uri.parse(draft.getValue()), MediaType.AUDIO).addListener(listener);
|
||||||
|
// break;
|
||||||
|
// case Draft.VIDEO:
|
||||||
|
// setMedia(Uri.parse(draft.getValue()), MediaType.VIDEO).addListener(listener);
|
||||||
|
// break;
|
||||||
|
// case Draft.QUOTE:
|
||||||
|
// SettableFuture<Boolean> quoteResult = new SettableFuture<>();
|
||||||
|
// new QuoteRestorationTask(draft.getValue(), quoteResult).execute();
|
||||||
|
// quoteResult.addListener(listener);
|
||||||
|
// break;
|
||||||
|
// }
|
||||||
|
// } catch (IOException e) {
|
||||||
|
// Log.w(TAG, e);
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
updateToggleButtonState();
|
updateToggleButtonState();
|
||||||
}
|
}
|
||||||
|
@ -963,7 +965,9 @@ public class ConversationActivity extends PassphraseRequiredActionBarActivity
|
||||||
|
|
||||||
if (drafts.size() > 0 && threadId>0) {
|
if (drafts.size() > 0 && threadId>0) {
|
||||||
// todo: the following line only works for text drafts
|
// todo: the following line only works for text drafts
|
||||||
dcContext.setDraft(dcChat.getId(), drafts.getSnippet(dcContext.context));
|
DcMsg msg = new DcMsg(dcContext, DcMsg.DC_MSG_TEXT);
|
||||||
|
msg.setText(drafts.getSnippet(dcContext.context));
|
||||||
|
dcContext.setDraft(dcChat.getId(), msg);
|
||||||
} else if (threadId > 0) {
|
} else if (threadId > 0) {
|
||||||
dcContext.setDraft(dcChat.getId(), null);
|
dcContext.setDraft(dcChat.getId(), null);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue