Skip to content

Commit

Permalink
add DcEventCenter and start using it, tackles #11
Browse files Browse the repository at this point in the history
  • Loading branch information
r10s committed Aug 29, 2018
1 parent 0b00ec2 commit 0b9e129
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 18 deletions.
12 changes: 12 additions & 0 deletions jni/dc_wrapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -1244,6 +1244,18 @@ JNIEXPORT void Java_com_b44t_messenger_DcLot_DcLotUnref(JNIEnv *env, jclass cls,
******************************************************************************/


JNIEXPORT jboolean Java_com_b44t_messenger_DcContext_data1IsString(JNIEnv *env, jclass cls, jint event)
{
return DC_EVENT_DATA1_IS_STRING(event);
}


JNIEXPORT jboolean Java_com_b44t_messenger_DcContext_data2IsString(JNIEnv *env, jclass cls, jint event)
{
return DC_EVENT_DATA2_IS_STRING(event);
}


JNIEXPORT jstring Java_com_b44t_messenger_DcContext_dataToString(JNIEnv *env, jclass cls, jlong hStr)
{
/* the callback may return a long that represents a pointer to a C-String; this function creates a Java-string from such values. */
Expand Down
4 changes: 3 additions & 1 deletion src/com/b44t/messenger/DcContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,13 @@ public DcContext(String osName) {
public native int joinSecurejoin(String qr);

// event handling - you should @Override this function in derived classes
public long handleEvent(final int event, final long data1, final long data2) {
public long handleEvent(int event, long data1, long data2) {
return 0;
}

// helper to get/return strings from/to handleEvent()
public native static boolean data1IsString(int event);
public native static boolean data2IsString(int event);
public native static String dataToString(long hString);
public native static long stringToData(String str);

Expand Down
81 changes: 81 additions & 0 deletions src/com/b44t/messenger/DcEventCenter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*******************************************************************************
*
* Delta Chat Java Adapter
* (C) 2017 Björn Petersen
* 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/ .
*
******************************************************************************/


package com.b44t.messenger;


import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Hashtable;

public class DcEventCenter {
private Hashtable<Integer, ArrayList<Object>> allObservers = new Hashtable<>();
private final Object LOCK = new Object();

public interface DcEventDelegate {
void handleEvent(int eventId, Object data1, Object data2);
}

public void addObserver(Object observer, int eventId) {
synchronized (LOCK) {
ArrayList<Object> idObservers = allObservers.get(eventId);
if (idObservers == null) {
allObservers.put(eventId, (idObservers = new ArrayList<>()));
}
idObservers.add(observer);
}
}

public void removeObserver(Object observer, int eventId) {
synchronized (LOCK) {
ArrayList<Object> idObservers = allObservers.get(eventId);
if (idObservers != null) {
idObservers.remove(observer);
}
}
}

public void removeObservers(Object observer) {
synchronized (LOCK) {
Enumeration<Integer> enumKey = allObservers.keys();
while(enumKey.hasMoreElements()) {
Integer eventId = enumKey.nextElement();
ArrayList<Object> idObservers = allObservers.get(eventId);
if (idObservers != null) {
idObservers.remove(observer);
}
}
}
}

public void sendToObservers(int eventId, Object data1, Object data2) {
synchronized (LOCK) {
ArrayList<Object> idObservers = allObservers.get(eventId);
if (idObservers != null) {
for (int i = 0; i < idObservers.size(); i++) {
Object observer = idObservers.get(i);
((DcEventDelegate) observer).handleEvent(eventId, data1, data2);
}
}
}
}
}
30 changes: 26 additions & 4 deletions src/org/thoughtcrime/securesms/RegistrationActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,15 @@
import android.support.constraint.Group;
import android.support.design.widget.TextInputEditText;
import android.text.TextUtils;
import android.util.Log;
import android.util.Patterns;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.b44t.messenger.DcContext;
import com.b44t.messenger.DcEventCenter;
import com.dd.CircularProgressButton;

import org.thoughtcrime.securesms.permissions.Permissions;
Expand All @@ -28,7 +31,7 @@
* @author Moxie Marlinspike
* @author Daniel Böhrs
*/
public class RegistrationActivity extends BaseActionBarActivity {
public class RegistrationActivity extends BaseActionBarActivity implements DcEventCenter.DcEventDelegate {

private enum VerificationType {
EMAIL,
Expand Down Expand Up @@ -57,6 +60,13 @@ public void onCreate(Bundle bundle) {

initializeResources();
initializePermissions();
ApplicationContext.getInstance(getApplicationContext()).dcContext.eventCenter.addObserver(this, DcContext.DC_EVENT_CONFIGURE_PROGRESS);
}

@Override
public void onDestroy() {
super.onDestroy();
ApplicationContext.getInstance(getApplicationContext()).dcContext.eventCenter.removeObservers(this);
}

@Override
Expand Down Expand Up @@ -225,9 +235,21 @@ private void stopLoginProcess() {
ApplicationContext.getInstance(getApplicationContext()).dcContext.stopOngoingProcess();
}

//@Override
public void didReceivedNotification(int id, Object... args) {
// TODO react to notifications sent via NotificationCenter
@Override
public void handleEvent(int eventId, Object data1, Object data2) {
if (eventId==DcContext.DC_EVENT_CONFIGURE_PROGRESS) {
long progress = (Long)data1;
Log.i("DeltaChat", String.format("configure-progress=%d", (int)progress));
if (progress==0/*error/aborted*/) {

}
else if (progress<1000/*progress in permille*/) {

}
else if (progress==1000/*done*/) {

}
}
}


Expand Down
32 changes: 19 additions & 13 deletions src/org/thoughtcrime/securesms/connect/ApplicationDcContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import android.widget.Toast;

import com.b44t.messenger.DcContext;
import com.b44t.messenger.DcEventCenter;

import org.thoughtcrime.securesms.ApplicationContext;
import org.thoughtcrime.securesms.util.Util;
Expand Down Expand Up @@ -179,13 +180,14 @@ public void waitForThreadsRunning()
* Event Handling
**********************************************************************************************/

public DcEventCenter eventCenter = new DcEventCenter();

public final Object lastErrorLock = new Object();
public int lastErrorCode = 0;
public String lastErrorString = "";
public boolean showNextErrorAsToast = true;

@Override public long handleEvent(final int event, final long data1, final long data2) {
@Override public long handleEvent(final int event, long data1, long data2) {
switch(event) {
case DC_EVENT_INFO:
Log.i("DeltaChat", dataToString(data2));
Expand Down Expand Up @@ -216,18 +218,6 @@ public void run() {
});
break;

case DC_EVENT_CONFIGURE_PROGRESS:
if (data1==0/*error/aborted*/) {
// TODO: send this event to RegistrationActivity, take care, we're not in the main thread!
}
else if (data1<1000/*progress in permille*/) {
// TODO: send this event to RegistrationActivity, take care, we're not in the main thread!
}
else if (data1==1000/*done*/) {
// TODO: send this event to RegistrationActivity, take care, we're not in the main thread!
}
break;

case DC_EVENT_HTTP_GET:
String httpContent = null;
try {
Expand All @@ -253,6 +243,22 @@ else if (data1==1000/*done*/) {
e.printStackTrace();
}
return stringToData(httpContent);

default:
{
final Object data1obj = data1IsString(event)? dataToString(data1) : data1;
final Object data2obj = data2IsString(event)? dataToString(data2) : data2;
Util.runOnMain(new Runnable() {
@Override
public void run() {
if(eventCenter!=null) {
eventCenter.sendToObservers(event, data1obj, data2obj);
}
}
});

}
break;
}
return 0;
}
Expand Down

0 comments on commit 0b9e129

Please sign in to comment.