mirror of
https://github.com/ArcaneChat/android.git
synced 2026-07-03 14:05:24 +02:00
pass DcEvent object to handlers to avoid error-prone casts
This commit is contained in:
@@ -10,7 +10,7 @@ public class DcEventCenter {
|
||||
private final Object LOCK = new Object();
|
||||
|
||||
public interface DcEventDelegate {
|
||||
void handleEvent(int eventId, Object data1, Object data2);
|
||||
void handleEvent(DcEvent event);
|
||||
default boolean runOnMain() {
|
||||
return true;
|
||||
}
|
||||
@@ -54,9 +54,9 @@ public class DcEventCenter {
|
||||
}
|
||||
}
|
||||
|
||||
public void sendToObservers(int eventId, Object data1, Object data2) {
|
||||
public void sendToObservers(DcEvent event) {
|
||||
synchronized (LOCK) {
|
||||
ArrayList<DcEventDelegate> idObservers = allObservers.get(eventId);
|
||||
ArrayList<DcEventDelegate> idObservers = allObservers.get(event.getId());
|
||||
if (idObservers != null) {
|
||||
for (DcEventDelegate observer : idObservers) {
|
||||
// using try/catch blocks as under some circumstances eg. getContext() may return NULL -
|
||||
@@ -65,7 +65,7 @@ public class DcEventCenter {
|
||||
if(observer.runOnMain()) {
|
||||
Util.runOnMain(() -> {
|
||||
try {
|
||||
observer.handleEvent(eventId, data1, data2);
|
||||
observer.handleEvent(event);
|
||||
}
|
||||
catch(Exception e) {
|
||||
e.printStackTrace();
|
||||
@@ -74,7 +74,7 @@ public class DcEventCenter {
|
||||
} else {
|
||||
Util.runOnBackground(() -> {
|
||||
try {
|
||||
observer.handleEvent(eventId, data1, data2);
|
||||
observer.handleEvent(event);
|
||||
}
|
||||
catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
|
||||
@@ -16,6 +16,7 @@ import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
|
||||
import com.b44t.messenger.DcContext;
|
||||
import com.b44t.messenger.DcEvent;
|
||||
import com.b44t.messenger.DcEventCenter;
|
||||
|
||||
import org.thoughtcrime.securesms.connect.ApplicationDcContext;
|
||||
@@ -130,8 +131,8 @@ public class BlockedAndShareContactsActivity extends PassphraseRequiredActionBar
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleEvent(int eventId, Object data1, Object data2) {
|
||||
if (eventId==DcContext.DC_EVENT_CONTACTS_CHANGED) {
|
||||
public void handleEvent(DcEvent event) {
|
||||
if (event.getId()==DcContext.DC_EVENT_CONTACTS_CHANGED) {
|
||||
restartLoader();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -47,6 +47,7 @@ import android.widget.Toast;
|
||||
|
||||
import com.b44t.messenger.DcContact;
|
||||
import com.b44t.messenger.DcContext;
|
||||
import com.b44t.messenger.DcEvent;
|
||||
import com.b44t.messenger.DcEventCenter;
|
||||
|
||||
import org.thoughtcrime.securesms.components.RecyclerViewFastScroller;
|
||||
@@ -426,8 +427,8 @@ public class ContactSelectionListFragment extends Fragment
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleEvent(int eventId, Object data1, Object data2) {
|
||||
if (eventId==DcContext.DC_EVENT_CONTACTS_CHANGED) {
|
||||
public void handleEvent(DcEvent event) {
|
||||
if (event.getId()==DcContext.DC_EVENT_CONTACTS_CHANGED) {
|
||||
getLoaderManager().restartLoader(0, null, ContactSelectionListFragment.this);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -63,6 +63,7 @@ import androidx.core.view.WindowCompat;
|
||||
import com.b44t.messenger.DcChat;
|
||||
import com.b44t.messenger.DcContact;
|
||||
import com.b44t.messenger.DcContext;
|
||||
import com.b44t.messenger.DcEvent;
|
||||
import com.b44t.messenger.DcEventCenter;
|
||||
import com.b44t.messenger.DcMsg;
|
||||
|
||||
@@ -1449,7 +1450,8 @@ public class ConversationActivity extends PassphraseRequiredActionBarActivity
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleEvent(int eventId, Object data1, Object data2) {
|
||||
public void handleEvent(DcEvent event) {
|
||||
int eventId = event.getId();
|
||||
if (eventId == DcContext.DC_EVENT_CHAT_MODIFIED
|
||||
|| eventId == DcContext.DC_EVENT_CHAT_EPHEMERAL_TIMER_MODIFIED
|
||||
|| eventId == DcContext.DC_EVENT_CONTACTS_CHANGED) {
|
||||
|
||||
@@ -57,6 +57,7 @@ import androidx.recyclerview.widget.RecyclerView.OnScrollListener;
|
||||
import com.b44t.messenger.DcChat;
|
||||
import com.b44t.messenger.DcContact;
|
||||
import com.b44t.messenger.DcContext;
|
||||
import com.b44t.messenger.DcEvent;
|
||||
import com.b44t.messenger.DcEventCenter;
|
||||
import com.b44t.messenger.DcMsg;
|
||||
|
||||
@@ -840,7 +841,9 @@ public class ConversationFragment extends Fragment
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleEvent(int eventId, Object data1, Object data2) {
|
||||
public void handleEvent(DcEvent event) {
|
||||
int eventId = event.getId();
|
||||
|
||||
if (eventId == DcContext.DC_EVENT_CHAT_MODIFIED) {
|
||||
updateLocationButton();
|
||||
}
|
||||
|
||||
@@ -28,6 +28,8 @@ import android.os.Bundle;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
|
||||
import com.b44t.messenger.DcEvent;
|
||||
import com.google.android.material.snackbar.Snackbar;
|
||||
|
||||
import androidx.core.content.ContextCompat;
|
||||
@@ -583,7 +585,7 @@ public class ConversationListFragment extends Fragment
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleEvent(int eventId, Object data1, Object data2) {
|
||||
public void handleEvent(DcEvent event) {
|
||||
getLoaderManager().restartLoader(0, null, this);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,6 +21,7 @@ import android.widget.Toast;
|
||||
|
||||
import com.b44t.messenger.DcContact;
|
||||
import com.b44t.messenger.DcContext;
|
||||
import com.b44t.messenger.DcEvent;
|
||||
import com.b44t.messenger.DcEventCenter;
|
||||
import com.bumptech.glide.load.engine.DiskCacheStrategy;
|
||||
import com.bumptech.glide.request.target.SimpleTarget;
|
||||
@@ -143,7 +144,8 @@ public class GroupCreateActivity extends PassphraseRequiredActionBarActivity
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleEvent(int eventId, Object data1, Object data2) {
|
||||
public void handleEvent(DcEvent event) {
|
||||
int eventId = event.getId();
|
||||
if (eventId== DcContext.DC_EVENT_CHAT_MODIFIED || eventId==DcContext.DC_EVENT_CONTACTS_CHANGED) {
|
||||
initializeExistingGroup();
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ import android.net.Uri;
|
||||
import android.os.Bundle;
|
||||
import android.provider.Settings;
|
||||
|
||||
import com.b44t.messenger.DcEvent;
|
||||
import com.google.android.material.tabs.TabLayout;
|
||||
import androidx.fragment.app.Fragment;
|
||||
import androidx.fragment.app.FragmentManager;
|
||||
@@ -177,7 +178,7 @@ public class ProfileActivity extends PassphraseRequiredActionBarActivity
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleEvent(int eventId, Object data1, Object data2) {
|
||||
public void handleEvent(DcEvent event) {
|
||||
updateToolbar();
|
||||
}
|
||||
|
||||
|
||||
@@ -24,6 +24,7 @@ import android.view.Window;
|
||||
import android.widget.TextView;
|
||||
|
||||
import com.b44t.messenger.DcContext;
|
||||
import com.b44t.messenger.DcEvent;
|
||||
import com.b44t.messenger.DcEventCenter;
|
||||
import com.b44t.messenger.DcMsg;
|
||||
import com.codewaves.stickyheadergrid.StickyHeaderGridLayoutManager;
|
||||
@@ -94,7 +95,7 @@ public class ProfileDocumentsFragment
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleEvent(int eventId, Object data1, Object data2) {
|
||||
public void handleEvent(DcEvent event) {
|
||||
getLoaderManager().restartLoader(0, null, this);
|
||||
}
|
||||
|
||||
|
||||
@@ -24,6 +24,7 @@ import android.view.Window;
|
||||
import android.widget.TextView;
|
||||
|
||||
import com.b44t.messenger.DcContext;
|
||||
import com.b44t.messenger.DcEvent;
|
||||
import com.b44t.messenger.DcEventCenter;
|
||||
import com.b44t.messenger.DcMsg;
|
||||
import com.codewaves.stickyheadergrid.StickyHeaderGridLayoutManager;
|
||||
@@ -97,7 +98,7 @@ public class ProfileGalleryFragment
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleEvent(int eventId, Object data1, Object data2) {
|
||||
public void handleEvent(DcEvent event) {
|
||||
getLoaderManager().restartLoader(0, null, this);
|
||||
}
|
||||
|
||||
|
||||
@@ -24,6 +24,7 @@ import com.b44t.messenger.DcChat;
|
||||
import com.b44t.messenger.DcChatlist;
|
||||
import com.b44t.messenger.DcContact;
|
||||
import com.b44t.messenger.DcContext;
|
||||
import com.b44t.messenger.DcEvent;
|
||||
import com.b44t.messenger.DcEventCenter;
|
||||
|
||||
import org.thoughtcrime.securesms.connect.ApplicationDcContext;
|
||||
@@ -104,7 +105,7 @@ public class ProfileSettingsFragment extends Fragment
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleEvent(int eventId, Object data1, Object data2) {
|
||||
public void handleEvent(DcEvent event) {
|
||||
update();
|
||||
}
|
||||
|
||||
|
||||
@@ -11,6 +11,7 @@ import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.constraintlayout.widget.Group;
|
||||
|
||||
import com.b44t.messenger.DcEvent;
|
||||
import com.b44t.messenger.DcProvider;
|
||||
import com.google.android.material.textfield.TextInputEditText;
|
||||
import com.google.android.material.textfield.TextInputLayout;
|
||||
@@ -546,15 +547,15 @@ public class RegistrationActivity extends BaseActionBarActivity implements DcEve
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleEvent(int eventId, Object data1, Object data2) {
|
||||
if (eventId==DcContext.DC_EVENT_CONFIGURE_PROGRESS) {
|
||||
public void handleEvent(DcEvent event) {
|
||||
if (event.getId()==DcContext.DC_EVENT_CONFIGURE_PROGRESS) {
|
||||
ApplicationDcContext dcContext = DcHelper.getContext(this);
|
||||
long progress = (Long)data1;
|
||||
long progress = event.getData1Int();
|
||||
if (progress==0/*error/aborted*/) {
|
||||
dcContext.maybeStartIo(); // start-io is also needed on errors to make previous config work in case of changes
|
||||
dcContext.endCaptureNextError();
|
||||
progressDialog.dismiss();
|
||||
WelcomeActivity.maybeShowConfigurationError(this, data2);
|
||||
WelcomeActivity.maybeShowConfigurationError(this, event.getData2Str());
|
||||
}
|
||||
else if (progress<1000/*progress in permille*/) {
|
||||
int percent = (int)progress / 10;
|
||||
|
||||
@@ -15,6 +15,7 @@ import androidx.annotation.NonNull;
|
||||
import androidx.appcompat.app.AlertDialog;
|
||||
|
||||
import com.b44t.messenger.DcContext;
|
||||
import com.b44t.messenger.DcEvent;
|
||||
import com.b44t.messenger.DcEventCenter;
|
||||
import com.b44t.messenger.DcLot;
|
||||
import com.google.zxing.integration.android.IntentIntegrator;
|
||||
@@ -171,7 +172,7 @@ public class WelcomeActivity extends BaseActionBarActivity implements DcEventCen
|
||||
dcContext.configure();
|
||||
}
|
||||
|
||||
private void progressError(Object data2) {
|
||||
private void progressError(String data2) {
|
||||
progressDialog.dismiss();
|
||||
maybeShowConfigurationError(this, data2);
|
||||
}
|
||||
@@ -195,10 +196,10 @@ public class WelcomeActivity extends BaseActionBarActivity implements DcEventCen
|
||||
finish();
|
||||
}
|
||||
|
||||
public static void maybeShowConfigurationError(Activity activity, Object data2) {
|
||||
if (data2 instanceof String && !((String) data2).isEmpty()) {
|
||||
public static void maybeShowConfigurationError(Activity activity, String data2) {
|
||||
if (data2 != null && data2.isEmpty()) {
|
||||
AlertDialog d = new AlertDialog.Builder(activity)
|
||||
.setMessage(((String) data2))
|
||||
.setMessage(data2)
|
||||
.setPositiveButton(android.R.string.ok, null)
|
||||
.create();
|
||||
d.show();
|
||||
@@ -212,9 +213,11 @@ public class WelcomeActivity extends BaseActionBarActivity implements DcEventCen
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleEvent(int eventId, Object data1, Object data2) {
|
||||
public void handleEvent(DcEvent event) {
|
||||
int eventId = event.getId();
|
||||
|
||||
if (eventId== DcContext.DC_EVENT_IMEX_PROGRESS ) {
|
||||
long progress = (Long)data1;
|
||||
long progress = event.getData1Int();
|
||||
if (progress==0/*error/aborted*/) {
|
||||
progressError(dcContext.getCapturedError());
|
||||
notificationController.close();
|
||||
@@ -230,16 +233,16 @@ public class WelcomeActivity extends BaseActionBarActivity implements DcEventCen
|
||||
}
|
||||
}
|
||||
else if (manualConfigure && eventId==DcContext.DC_EVENT_CONFIGURE_PROGRESS) {
|
||||
long progress = (Long)data1;
|
||||
long progress = event.getData1Int();
|
||||
if (progress==1000/*done*/) {
|
||||
dcContext.maybeStartIo();
|
||||
finish(); // remove ourself from the activity stack (finishAffinity is available in API 16, we're targeting API 14)
|
||||
}
|
||||
}
|
||||
else if (!manualConfigure && eventId==DcContext.DC_EVENT_CONFIGURE_PROGRESS) {
|
||||
long progress = (Long)data1;
|
||||
long progress = event.getData1Int();
|
||||
if (progress==0/*error/aborted*/) {
|
||||
progressError(data2);
|
||||
progressError(event.getData2Str());
|
||||
}
|
||||
else if (progress<1000/*progress in permille*/) {
|
||||
progressUpdate((int)progress);
|
||||
|
||||
@@ -446,15 +446,13 @@ public class ApplicationDcContext extends DcContext {
|
||||
case DC_EVENT_INCOMING_MSG:
|
||||
notificationCenter.addNotification(event.getData1Int(), event.getData2Int());
|
||||
if (eventCenter != null) {
|
||||
eventCenter.sendToObservers(id, (long)event.getData1Int(), (long)event.getData2Int());
|
||||
eventCenter.sendToObservers(event);
|
||||
}
|
||||
break;
|
||||
|
||||
default: {
|
||||
final Object data1obj = (long)event.getData1Int();
|
||||
final Object data2obj = data2IsString(id) ? event.getData2Str() : (long)event.getData2Int();
|
||||
if (eventCenter != null) {
|
||||
eventCenter.sendToObservers(id, data1obj, data2obj);
|
||||
eventCenter.sendToObservers(event);
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
@@ -5,6 +5,7 @@ import android.graphics.Bitmap;
|
||||
import androidx.annotation.NonNull;
|
||||
import android.util.Log;
|
||||
|
||||
import com.b44t.messenger.DcEvent;
|
||||
import com.b44t.messenger.DcEventCenter;
|
||||
import com.mapbox.android.core.permissions.PermissionsManager;
|
||||
import com.mapbox.geojson.Feature;
|
||||
@@ -183,9 +184,10 @@ public class MapDataManager implements DcEventCenter.DcEventDelegate, GenerateIn
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleEvent(int eventId, Object data1, Object data2) {
|
||||
public void handleEvent(DcEvent event) {
|
||||
int eventId = event.getId();
|
||||
Log.d(TAG, "updateEvent in MapDataManager called. eventId: " + eventId);
|
||||
int contactId = ((Long) data1).intValue();
|
||||
int contactId = event.getData1Int();
|
||||
if (contactMapSources.containsKey(contactId)) {
|
||||
DataCollector collector = new DataCollector(dcContext,
|
||||
contactMapSources,
|
||||
|
||||
@@ -10,6 +10,7 @@ import androidx.preference.ListPreference;
|
||||
import androidx.preference.Preference;
|
||||
|
||||
import com.b44t.messenger.DcContext;
|
||||
import com.b44t.messenger.DcEvent;
|
||||
import com.b44t.messenger.DcEventCenter;
|
||||
|
||||
import org.thoughtcrime.securesms.R;
|
||||
@@ -116,9 +117,9 @@ public abstract class ListSummaryPreferenceFragment extends CorrectedPreferenceF
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleEvent(int eventId, Object data1, Object data2) {
|
||||
if (eventId== DcContext.DC_EVENT_IMEX_PROGRESS) {
|
||||
long progress = (Long)data1;
|
||||
public void handleEvent(DcEvent event) {
|
||||
if (event.getId()== DcContext.DC_EVENT_IMEX_PROGRESS) {
|
||||
long progress = event.getData1Int();
|
||||
if (progress==0/*error/aborted*/) {
|
||||
dcContext.endCaptureNextError();
|
||||
progressDialog.dismiss();
|
||||
|
||||
@@ -9,6 +9,7 @@ import android.text.Html;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.b44t.messenger.DcContext;
|
||||
import com.b44t.messenger.DcEvent;
|
||||
import com.b44t.messenger.DcEventCenter;
|
||||
import com.b44t.messenger.DcLot;
|
||||
import com.google.zxing.integration.android.IntentResult;
|
||||
@@ -222,10 +223,10 @@ public class QrCodeHandler implements DcEventCenter.DcEventDelegate {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleEvent(int eventId, Object data1, Object data2) {
|
||||
if (eventId == DcContext.DC_EVENT_SECUREJOIN_JOINER_PROGRESS) {
|
||||
long contact_id = (Long)data1;
|
||||
long progress = (Long)data2;
|
||||
public void handleEvent(DcEvent event) {
|
||||
if (event.getId() == DcContext.DC_EVENT_SECUREJOIN_JOINER_PROGRESS) {
|
||||
long contact_id = event.getData1Int();
|
||||
long progress = event.getData2Int();
|
||||
String msg = null;
|
||||
if( progress == 400) {
|
||||
msg = activity.getString(R.string.qrscan_x_verified_introduce_myself, dcContext.getContact((int)contact_id).getNameNAddr());
|
||||
|
||||
@@ -23,6 +23,7 @@ import androidx.annotation.NonNull;
|
||||
import androidx.fragment.app.Fragment;
|
||||
|
||||
import com.b44t.messenger.DcContext;
|
||||
import com.b44t.messenger.DcEvent;
|
||||
import com.b44t.messenger.DcEventCenter;
|
||||
import com.google.zxing.BarcodeFormat;
|
||||
import com.google.zxing.MultiFormatWriter;
|
||||
@@ -184,11 +185,11 @@ public class QrShowFragment extends Fragment implements DcEventCenter.DcEventDel
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleEvent(int eventId, Object data1, Object data2) {
|
||||
if (eventId == DcContext.DC_EVENT_SECUREJOIN_INVITER_PROGRESS) {
|
||||
public void handleEvent(DcEvent event) {
|
||||
if (event.getId() == DcContext.DC_EVENT_SECUREJOIN_INVITER_PROGRESS) {
|
||||
DcContext dcContext = DcHelper.getContext(getActivity());
|
||||
int contact_id = ((Long) data1).intValue();
|
||||
long progress = (Long) data2;
|
||||
int contact_id = event.getData1Int();
|
||||
long progress = event.getData2Int();
|
||||
String msg = null;
|
||||
if (progress == 300) {
|
||||
msg = String.format(getString(R.string.qrshow_x_joining), dcContext.getContact(contact_id).getNameNAddr());
|
||||
|
||||
Reference in New Issue
Block a user