mirror of
https://github.com/ArcaneChat/android.git
synced 2026-07-03 14:05:24 +02:00
add DcEventCenter and start using it, tackles #11
This commit is contained in:
@@ -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. */
|
||||
|
||||
@@ -142,11 +142,13 @@ public class DcContext {
|
||||
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);
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -10,12 +10,15 @@ import android.support.annotation.NonNull;
|
||||
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;
|
||||
@@ -28,7 +31,7 @@ import org.thoughtcrime.securesms.util.Dialogs;
|
||||
* @author Moxie Marlinspike
|
||||
* @author Daniel Böhrs
|
||||
*/
|
||||
public class RegistrationActivity extends BaseActionBarActivity {
|
||||
public class RegistrationActivity extends BaseActionBarActivity implements DcEventCenter.DcEventDelegate {
|
||||
|
||||
private enum VerificationType {
|
||||
EMAIL,
|
||||
@@ -57,6 +60,13 @@ public class RegistrationActivity extends BaseActionBarActivity {
|
||||
|
||||
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
|
||||
@@ -225,9 +235,21 @@ public class RegistrationActivity extends BaseActionBarActivity {
|
||||
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*/) {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -22,6 +22,7 @@ import android.util.Log;
|
||||
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;
|
||||
@@ -179,13 +180,14 @@ public class ApplicationDcContext extends DcContext {
|
||||
* 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));
|
||||
@@ -216,18 +218,6 @@ public class ApplicationDcContext extends DcContext {
|
||||
});
|
||||
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 {
|
||||
@@ -253,6 +243,22 @@ public class ApplicationDcContext extends DcContext {
|
||||
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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user