mirror of
https://github.com/ArcaneChat/android.git
synced 2026-07-03 14:05:24 +02:00
close #113. the notification event is running on the background thread now, the default for event handlers is the main thread, though.
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
/*******************************************************************************
|
||||
/* *****************************************************************************
|
||||
*
|
||||
* Delta Chat Java Adapter
|
||||
* (C) 2017 Björn Petersen
|
||||
@@ -23,8 +23,12 @@
|
||||
package com.b44t.messenger;
|
||||
|
||||
|
||||
import android.os.AsyncTask;
|
||||
|
||||
import org.thoughtcrime.securesms.util.Util;
|
||||
|
||||
import java.lang.ref.WeakReference;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Enumeration;
|
||||
import java.util.Hashtable;
|
||||
|
||||
public class DcEventCenter {
|
||||
@@ -33,9 +37,20 @@ public class DcEventCenter {
|
||||
|
||||
public interface DcEventDelegate {
|
||||
void handleEvent(int eventId, Object data1, Object data2);
|
||||
default boolean runOnMain() {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated use addObserver(int, DcEventDelegate) instead.
|
||||
*/
|
||||
@Deprecated
|
||||
public void addObserver(DcEventDelegate observer, int eventId) {
|
||||
addObserver(eventId, observer);
|
||||
}
|
||||
|
||||
public void addObserver(int eventId, DcEventDelegate observer) {
|
||||
synchronized (LOCK) {
|
||||
ArrayList<DcEventDelegate> idObservers = allObservers.get(eventId);
|
||||
if (idObservers == null) {
|
||||
@@ -45,7 +60,7 @@ public class DcEventCenter {
|
||||
}
|
||||
}
|
||||
|
||||
public void removeObserver(DcEventDelegate observer, int eventId) {
|
||||
public void removeObserver(int eventId, DcEventDelegate observer) {
|
||||
synchronized (LOCK) {
|
||||
ArrayList<DcEventDelegate> idObservers = allObservers.get(eventId);
|
||||
if (idObservers != null) {
|
||||
@@ -54,11 +69,9 @@ public class DcEventCenter {
|
||||
}
|
||||
}
|
||||
|
||||
public void removeObservers(Object observer) {
|
||||
public void removeObservers(DcEventDelegate observer) {
|
||||
synchronized (LOCK) {
|
||||
Enumeration<Integer> enumKey = allObservers.keys();
|
||||
while(enumKey.hasMoreElements()) {
|
||||
Integer eventId = enumKey.nextElement();
|
||||
for(Integer eventId : allObservers.keySet()) {
|
||||
ArrayList<DcEventDelegate> idObservers = allObservers.get(eventId);
|
||||
if (idObservers != null) {
|
||||
idObservers.remove(observer);
|
||||
@@ -71,11 +84,32 @@ public class DcEventCenter {
|
||||
synchronized (LOCK) {
|
||||
ArrayList<DcEventDelegate> idObservers = allObservers.get(eventId);
|
||||
if (idObservers != null) {
|
||||
for (int i = 0; i < idObservers.size(); i++) {
|
||||
DcEventDelegate observer = idObservers.get(i);
|
||||
observer.handleEvent(eventId, data1, data2);
|
||||
for (DcEventDelegate observer : idObservers) {
|
||||
if(observer.runOnMain()) {
|
||||
Util.runOnMain(() -> observer.handleEvent(eventId, data1, data2));
|
||||
} else {
|
||||
new BackgroundEventHandler(observer, eventId)
|
||||
.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, data1, data2);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static class BackgroundEventHandler extends AsyncTask<Object, Void, Void> {
|
||||
private final WeakReference<DcEventDelegate> asyncDelegate;
|
||||
private final int eventId;
|
||||
BackgroundEventHandler(DcEventDelegate delegate, int eventId) {
|
||||
asyncDelegate = new WeakReference<>(delegate);
|
||||
this.eventId = eventId;
|
||||
}
|
||||
@Override
|
||||
protected Void doInBackground(Object... data) {
|
||||
DcEventDelegate delegate = asyncDelegate.get();
|
||||
if(delegate != null) {
|
||||
delegate.handleEvent(eventId, data[0], data[1]);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -152,23 +152,17 @@ public class ApplicationContext extends MultiDexApplication implements Dependenc
|
||||
private void initializeIncomingMessageNotifier() {
|
||||
|
||||
DcEventCenter dcEventCenter = dcContext.eventCenter;
|
||||
dcEventCenter.addObserver((eventId, data1, data2)
|
||||
-> new AsyncTask<Long, Void, Void>() {
|
||||
dcEventCenter.addObserver(DcContext.DC_EVENT_INCOMING_MSG, new DcEventCenter.DcEventDelegate() {
|
||||
@Override
|
||||
protected Void doInBackground(Long... params) {
|
||||
if (data1 == null)
|
||||
MessageNotifier.updateNotification(dcContext.context);
|
||||
else {
|
||||
if (data1 instanceof Long)
|
||||
MessageNotifier.updateNotification(dcContext.context, ((Long)data1).intValue());
|
||||
else {
|
||||
Log.w(TAG, "The type of chat was unclear. " + data1.getClass());
|
||||
MessageNotifier.updateNotification(dcContext.context);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR), DcContext.DC_EVENT_INCOMING_MSG);
|
||||
public void handleEvent(int eventId, Object data1, Object data2) {
|
||||
MessageNotifier.updateNotification(dcContext.context, ((Long) data1).intValue());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean runOnMain() {
|
||||
return false;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void initializeJobManager() {
|
||||
|
||||
@@ -92,13 +92,13 @@ public class QrShowActivity extends AppCompatActivity implements DcEventCenter.D
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
dcEventCenter.addObserver(this, DcContext.DC_EVENT_SECUREJOIN_INVITER_PROGRESS);
|
||||
dcEventCenter.addObserver(DcContext.DC_EVENT_SECUREJOIN_INVITER_PROGRESS, this);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onDestroy() {
|
||||
super.onDestroy();
|
||||
dcEventCenter.removeObserver(this, DcContext.DC_EVENT_SECUREJOIN_INVITER_PROGRESS);
|
||||
dcEventCenter.removeObservers(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user