Merge branch 'async-events-refactoring' into 'master'

Refactor UI event listeners

See merge request briar/briar!1074
This commit is contained in:
Torsten Grote
2019-04-05 16:41:19 +00:00
27 changed files with 192 additions and 290 deletions

View File

@@ -104,9 +104,7 @@ class AndroidNotificationManagerImpl implements AndroidNotificationManager,
private ContactId blockedContact = null;
private GroupId blockedGroup = null;
private boolean blockSignInReminder = false;
private boolean blockContacts = false, blockGroups = false;
private boolean blockForums = false, blockBlogs = false;
private boolean blockIntroductions = false;
private boolean blockBlogs = false;
private long lastSound = 0;
private volatile Settings settings = new Settings();
@@ -237,7 +235,6 @@ class AndroidNotificationManagerImpl implements AndroidNotificationManager,
}
}
@UiThread
@Override
public Notification getForegroundNotification() {
@@ -278,13 +275,11 @@ class AndroidNotificationManagerImpl implements AndroidNotificationManager,
notificationManager.notify(ONGOING_NOTIFICATION_ID, n);
}
@UiThread
private void showContactNotification(ContactId c) {
androidExecutor.runOnUiThread(() -> {
if (blockContacts) return;
if (c.equals(blockedContact)) return;
contactCounts.add(c);
updateContactNotification(true);
});
if (c.equals(blockedContact)) return;
contactCounts.add(c);
updateContactNotification(true);
}
@Override
@@ -381,12 +376,9 @@ class AndroidNotificationManagerImpl implements AndroidNotificationManager,
@UiThread
private void showGroupMessageNotification(GroupId g) {
androidExecutor.runOnUiThread(() -> {
if (blockGroups) return;
if (g.equals(blockedGroup)) return;
groupCounts.add(g);
updateGroupMessageNotification(true);
});
if (g.equals(blockedGroup)) return;
groupCounts.add(g);
updateGroupMessageNotification(true);
}
@Override
@@ -452,12 +444,9 @@ class AndroidNotificationManagerImpl implements AndroidNotificationManager,
@UiThread
private void showForumPostNotification(GroupId g) {
androidExecutor.runOnUiThread(() -> {
if (blockForums) return;
if (g.equals(blockedGroup)) return;
forumCounts.add(g);
updateForumPostNotification(true);
});
if (g.equals(blockedGroup)) return;
forumCounts.add(g);
updateForumPostNotification(true);
}
@Override
@@ -522,12 +511,10 @@ class AndroidNotificationManagerImpl implements AndroidNotificationManager,
@UiThread
private void showBlogPostNotification(GroupId g) {
androidExecutor.runOnUiThread(() -> {
if (blockBlogs) return;
if (g.equals(blockedGroup)) return;
blogCounts.add(g);
updateBlogPostNotification(true);
});
if (blockBlogs) return;
if (g.equals(blockedGroup)) return;
blogCounts.add(g);
updateBlogPostNotification(true);
}
@Override
@@ -575,12 +562,10 @@ class AndroidNotificationManagerImpl implements AndroidNotificationManager,
(Runnable) this::clearBlogPostNotification);
}
@UiThread
private void showIntroductionNotification() {
androidExecutor.runOnUiThread(() -> {
if (blockIntroductions) return;
introductionTotal++;
updateIntroductionNotification();
});
introductionTotal++;
updateIntroductionNotification();
}
@UiThread

View File

@@ -6,7 +6,6 @@ import org.briarproject.bramble.api.db.DbException;
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
import org.briarproject.bramble.api.sync.GroupId;
import org.briarproject.bramble.api.sync.MessageId;
import org.briarproject.briar.android.DestroyableContext;
import org.briarproject.briar.android.controller.handler.ExceptionHandler;
import org.briarproject.briar.android.controller.handler.ResultExceptionHandler;
import org.briarproject.briar.api.blog.BlogPostHeader;
@@ -36,10 +35,11 @@ interface BaseController {
void repeatPost(BlogPostItem item, @Nullable String comment,
ExceptionHandler<DbException> handler);
@UiThread
void setBlogListener(BlogListener listener);
@NotNullByDefault
interface BlogListener extends DestroyableContext {
interface BlogListener {
@UiThread
void onBlogPostAdded(BlogPostHeader header, boolean local);

View File

@@ -86,15 +86,6 @@ abstract class BaseControllerImpl extends DbControllerImpl
this.listener = listener;
}
void onBlogPostAdded(BlogPostHeader h, boolean local) {
listener.runOnUiThreadUnlessDestroyed(
() -> listener.onBlogPostAdded(h, local));
}
void onBlogRemoved() {
listener.runOnUiThreadUnlessDestroyed(() -> listener.onBlogRemoved());
}
@Override
public void loadBlogPosts(GroupId groupId,
ResultExceptionHandler<Collection<BlogPostItem>, DbException> handler) {

View File

@@ -16,6 +16,7 @@ public interface BlogController extends BaseController {
void setGroupId(GroupId g);
@UiThread
void setBlogSharingListener(BlogSharingListener listener);
void loadBlogPosts(

View File

@@ -49,8 +49,11 @@ class BlogControllerImpl extends BaseControllerImpl
Logger.getLogger(BlogControllerImpl.class.getName());
private final BlogSharingManager blogSharingManager;
// UI thread
private BlogSharingListener listener;
private volatile GroupId groupId = null;
private volatile BlogSharingListener listener;
@Inject
BlogControllerImpl(@DatabaseExecutor Executor dbExecutor,
@@ -102,7 +105,7 @@ class BlogControllerImpl extends BaseControllerImpl
BlogPostAddedEvent b = (BlogPostAddedEvent) e;
if (b.getGroupId().equals(groupId)) {
LOG.info("Blog post added");
onBlogPostAdded(b.getHeader(), b.isLocal());
listener.onBlogPostAdded(b.getHeader(), b.isLocal());
}
} else if (e instanceof BlogInvitationResponseReceivedEvent) {
BlogInvitationResponseReceivedEvent b =
@@ -110,32 +113,23 @@ class BlogControllerImpl extends BaseControllerImpl
BlogInvitationResponse r = b.getMessageHeader();
if (r.getShareableId().equals(groupId) && r.wasAccepted()) {
LOG.info("Blog invitation accepted");
onBlogInvitationAccepted(b.getContactId());
listener.onBlogInvitationAccepted(b.getContactId());
}
} else if (e instanceof ContactLeftShareableEvent) {
ContactLeftShareableEvent s = (ContactLeftShareableEvent) e;
if (s.getGroupId().equals(groupId)) {
LOG.info("Blog left by contact");
onBlogLeft(s.getContactId());
listener.onBlogLeft(s.getContactId());
}
} else if (e instanceof GroupRemovedEvent) {
GroupRemovedEvent g = (GroupRemovedEvent) e;
if (g.getGroup().getId().equals(groupId)) {
LOG.info("Blog removed");
onBlogRemoved();
listener.onBlogRemoved();
}
}
}
private void onBlogInvitationAccepted(ContactId c) {
listener.runOnUiThreadUnlessDestroyed(
() -> listener.onBlogInvitationAccepted(c));
}
private void onBlogLeft(ContactId c) {
listener.runOnUiThreadUnlessDestroyed(() -> listener.onBlogLeft(c));
}
@Override
public void loadBlogPosts(
ResultExceptionHandler<Collection<BlogPostItem>, DbException> handler) {

View File

@@ -17,6 +17,7 @@ public interface FeedController extends BaseController {
void loadPersonalBlog(ResultExceptionHandler<Blog, DbException> handler);
@UiThread
void setFeedListener(FeedListener listener);
@NotNullByDefault

View File

@@ -34,13 +34,13 @@ import static org.briarproject.briar.api.blog.BlogManager.CLIENT_ID;
@MethodsNotNullByDefault
@ParametersNotNullByDefault
class FeedControllerImpl extends BaseControllerImpl
implements FeedController {
class FeedControllerImpl extends BaseControllerImpl implements FeedController {
private static final Logger LOG =
Logger.getLogger(FeedControllerImpl.class.getName());
private volatile FeedListener listener;
// UI thread
private FeedListener listener;
@Inject
FeedControllerImpl(@DatabaseExecutor Executor dbExecutor,
@@ -76,26 +76,22 @@ class FeedControllerImpl extends BaseControllerImpl
if (e instanceof BlogPostAddedEvent) {
BlogPostAddedEvent b = (BlogPostAddedEvent) e;
LOG.info("Blog post added");
onBlogPostAdded(b.getHeader(), b.isLocal());
listener.onBlogPostAdded(b.getHeader(), b.isLocal());
} else if (e instanceof GroupAddedEvent) {
GroupAddedEvent g = (GroupAddedEvent) e;
if (g.getGroup().getClientId().equals(CLIENT_ID)) {
LOG.info("Blog added");
onBlogAdded();
listener.onBlogAdded();
}
} else if (e instanceof GroupRemovedEvent) {
GroupRemovedEvent g = (GroupRemovedEvent) e;
if (g.getGroup().getClientId().equals(CLIENT_ID)) {
LOG.info("Blog removed");
onBlogRemoved();
listener.onBlogRemoved();
}
}
}
private void onBlogAdded() {
listener.runOnUiThreadUnlessDestroyed(() -> listener.onBlogAdded());
}
@Override
public void loadBlogPosts(
ResultExceptionHandler<Collection<BlogPostItem>, DbException> handler) {

View File

@@ -2,6 +2,7 @@ package org.briarproject.briar.android.contact;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.UiThread;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.ActivityOptionsCompat;
import android.support.v4.util.Pair;
@@ -253,37 +254,34 @@ public class ContactListFragment extends BaseFragment implements EventListener {
}
}
@UiThread
private void updateItem(ContactId c, ConversationMessageHeader h) {
runOnUiThreadUnlessDestroyed(() -> {
adapter.incrementRevision();
int position = adapter.findItemPosition(c);
ContactListItem item = adapter.getItemAt(position);
if (item != null) {
item.addMessage(h);
adapter.updateItemAt(position, item);
}
});
adapter.incrementRevision();
int position = adapter.findItemPosition(c);
ContactListItem item = adapter.getItemAt(position);
if (item != null) {
item.addMessage(h);
adapter.updateItemAt(position, item);
}
}
@UiThread
private void removeItem(ContactId c) {
runOnUiThreadUnlessDestroyed(() -> {
adapter.incrementRevision();
int position = adapter.findItemPosition(c);
ContactListItem item = adapter.getItemAt(position);
if (item != null) adapter.remove(item);
});
adapter.incrementRevision();
int position = adapter.findItemPosition(c);
ContactListItem item = adapter.getItemAt(position);
if (item != null) adapter.remove(item);
}
@UiThread
private void setConnected(ContactId c, boolean connected) {
runOnUiThreadUnlessDestroyed(() -> {
adapter.incrementRevision();
int position = adapter.findItemPosition(c);
ContactListItem item = adapter.getItemAt(position);
if (item != null) {
item.setConnected(connected);
adapter.updateItemAt(position, item);
}
});
adapter.incrementRevision();
int position = adapter.findItemPosition(c);
ContactListItem item = adapter.getItemAt(position);
if (item != null) {
item.setConnected(connected);
adapter.updateItemAt(position, item);
}
}
}

View File

@@ -4,12 +4,11 @@ import android.support.annotation.UiThread;
import org.briarproject.bramble.api.contact.ContactId;
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
import org.briarproject.briar.android.DestroyableContext;
import java.util.Collection;
@NotNullByDefault
public interface ContactSelectorListener extends DestroyableContext {
public interface ContactSelectorListener {
@UiThread
void contactsSelected(Collection<ContactId> contacts);

View File

@@ -4,7 +4,6 @@ import android.support.annotation.UiThread;
import org.briarproject.bramble.api.contact.ContactId;
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
import org.briarproject.briar.android.DestroyableContext;
import java.util.Collection;
@@ -62,7 +61,8 @@ public interface SharingController {
@UiThread
int getTotalCount();
interface SharingListener extends DestroyableContext {
interface SharingListener {
@UiThread
void onSharingInfoUpdated(int total, int online);
}

View File

@@ -1,5 +1,7 @@
package org.briarproject.briar.android.controller;
import android.support.annotation.UiThread;
import org.briarproject.bramble.api.contact.ContactId;
import org.briarproject.bramble.api.event.Event;
import org.briarproject.bramble.api.event.EventBus;
@@ -22,11 +24,13 @@ public class SharingControllerImpl implements SharingController, EventListener {
private final EventBus eventBus;
private final ConnectionRegistry connectionRegistry;
@Nullable
private volatile SharingListener listener;
// only access on @UiThread
// UI thread
private final Set<ContactId> contacts = new HashSet<>();
// UI thread
@Nullable
private SharingListener listener;
@Inject
SharingControllerImpl(EventBus eventBus,
ConnectionRegistry connectionRegistry) {
@@ -58,14 +62,13 @@ public class SharingControllerImpl implements SharingController, EventListener {
}
}
@UiThread
private void setConnected(ContactId c) {
if (listener == null) return;
listener.runOnUiThreadUnlessDestroyed(() -> {
if (contacts.contains(c)) {
int online = getOnlineCount();
listener.onSharingInfoUpdated(contacts.size(), online);
}
});
if (listener == null) throw new IllegalStateException();
if (contacts.contains(c)) {
int online = getOnlineCount();
listener.onSharingInfoUpdated(contacts.size(), online);
}
}
@Override

View File

@@ -398,22 +398,17 @@ public class ConversationActivity extends BriarActivity
}
}
@UiThread
private void displayContactOnlineStatus() {
runOnUiThreadUnlessDestroyed(() -> {
if (connectionRegistry.isConnected(contactId)) {
toolbarStatus.setImageDrawable(ContextCompat
.getDrawable(ConversationActivity.this,
R.drawable.contact_online));
toolbarStatus
.setContentDescription(getString(R.string.online));
} else {
toolbarStatus.setImageDrawable(ContextCompat
.getDrawable(ConversationActivity.this,
R.drawable.contact_offline));
toolbarStatus
.setContentDescription(getString(R.string.offline));
}
});
if (connectionRegistry.isConnected(contactId)) {
toolbarStatus.setImageDrawable(ContextCompat.getDrawable(
ConversationActivity.this, R.drawable.contact_online));
toolbarStatus.setContentDescription(getString(R.string.online));
} else {
toolbarStatus.setImageDrawable(ContextCompat.getDrawable(
ConversationActivity.this, R.drawable.contact_offline));
toolbarStatus.setContentDescription(getString(R.string.offline));
}
}
private void loadMessages() {
@@ -583,7 +578,7 @@ public class ConversationActivity extends BriarActivity
ContactRemovedEvent c = (ContactRemovedEvent) e;
if (c.getContactId().equals(contactId)) {
LOG.info("Contact removed");
finishOnUiThread();
supportFinishAfterTransition();
}
} else if (e instanceof ConversationMessageReceivedEvent) {
ConversationMessageReceivedEvent p =
@@ -619,47 +614,43 @@ public class ConversationActivity extends BriarActivity
}
}
@UiThread
private void addConversationItem(ConversationItem item) {
runOnUiThreadUnlessDestroyed(() -> {
adapter.incrementRevision();
adapter.add(item);
// When adding a new message, scroll to the bottom if the
// conversation is visible, even if we're not currently at
// the bottom
if (getLifecycle().getCurrentState().isAtLeast(STARTED))
scrollToBottom();
});
adapter.incrementRevision();
adapter.add(item);
// When adding a new message, scroll to the bottom if the conversation
// is visible, even if we're not currently at the bottom
if (getLifecycle().getCurrentState().isAtLeast(STARTED))
scrollToBottom();
}
@UiThread
private void onNewConversationMessage(ConversationMessageHeader h) {
runOnUiThreadUnlessDestroyed(() -> {
if (h instanceof ConversationRequest ||
h instanceof ConversationResponse) {
// contact name might not have been loaded
observeOnce(viewModel.getContactDisplayName(), this,
name -> addConversationItem(h.accept(visitor)));
} else {
// visitor also loads message text (if existing)
addConversationItem(h.accept(visitor));
}
});
if (h instanceof ConversationRequest ||
h instanceof ConversationResponse) {
// contact name might not have been loaded
observeOnce(viewModel.getContactDisplayName(), this,
name -> addConversationItem(h.accept(visitor)));
} else {
// visitor also loads message text (if existing)
addConversationItem(h.accept(visitor));
}
}
@UiThread
private void markMessages(Collection<MessageId> messageIds, boolean sent,
boolean seen) {
runOnUiThreadUnlessDestroyed(() -> {
adapter.incrementRevision();
Set<MessageId> messages = new HashSet<>(messageIds);
SparseArray<ConversationItem> list = adapter.getOutgoingMessages();
for (int i = 0; i < list.size(); i++) {
ConversationItem item = list.valueAt(i);
if (messages.contains(item.getId())) {
item.setSent(sent);
item.setSeen(seen);
adapter.notifyItemChanged(list.keyAt(i));
}
adapter.incrementRevision();
Set<MessageId> messages = new HashSet<>(messageIds);
SparseArray<ConversationItem> list = adapter.getOutgoingMessages();
for (int i = 0; i < list.size(); i++) {
ConversationItem item = list.valueAt(i);
if (messages.contains(item.getId())) {
item.setSent(sent);
item.setSeen(seen);
adapter.notifyItemChanged(list.keyAt(i));
}
});
}
}
@Override

View File

@@ -79,7 +79,7 @@ class ForumControllerImpl extends
ForumPostReceivedEvent f = (ForumPostReceivedEvent) e;
if (f.getGroupId().equals(getGroupId())) {
LOG.info("Forum post received, adding...");
onForumPostReceived(f.getHeader(), f.getText());
listener.onItemReceived(buildItem(f.getHeader(), f.getText()));
}
} else if (e instanceof ForumInvitationResponseReceivedEvent) {
ForumInvitationResponseReceivedEvent f =
@@ -87,13 +87,13 @@ class ForumControllerImpl extends
ForumInvitationResponse r = f.getMessageHeader();
if (r.getShareableId().equals(getGroupId()) && r.wasAccepted()) {
LOG.info("Forum invitation was accepted");
onForumInvitationAccepted(f.getContactId());
listener.onInvitationAccepted(f.getContactId());
}
} else if (e instanceof ContactLeftShareableEvent) {
ContactLeftShareableEvent c = (ContactLeftShareableEvent) e;
if (c.getGroupId().equals(getGroupId())) {
LOG.info("Forum left by contact");
onForumLeft(c.getContactId());
listener.onForumLeft(c.getContactId());
}
}
}
@@ -168,8 +168,7 @@ class ForumControllerImpl extends
}
@Override
protected ForumPostHeader addLocalMessage(ForumPost p)
throws DbException {
protected ForumPostHeader addLocalMessage(ForumPost p) throws DbException {
return forumManager.addLocalPost(p);
}
@@ -183,19 +182,4 @@ class ForumControllerImpl extends
return new ForumItem(header, text);
}
private void onForumPostReceived(ForumPostHeader h, String text) {
ForumItem item = buildItem(h, text);
listener.runOnUiThreadUnlessDestroyed(
() -> listener.onItemReceived(item));
}
private void onForumInvitationAccepted(ContactId c) {
listener.runOnUiThreadUnlessDestroyed(
() -> listener.onInvitationAccepted(c));
}
private void onForumLeft(ContactId c) {
listener.runOnUiThreadUnlessDestroyed(() -> listener.onForumLeft(c));
}
}

View File

@@ -2,6 +2,7 @@ package org.briarproject.briar.android.forum;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.UiThread;
import android.support.design.widget.Snackbar;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
@@ -244,25 +245,23 @@ public class ForumListFragment extends BaseEventFragment implements
}
}
@UiThread
private void updateItem(GroupId g, ForumPostHeader m) {
runOnUiThreadUnlessDestroyed(() -> {
adapter.incrementRevision();
int position = adapter.findItemPosition(g);
ForumListItem item = adapter.getItemAt(position);
if (item != null) {
item.addHeader(m);
adapter.updateItemAt(position, item);
}
});
adapter.incrementRevision();
int position = adapter.findItemPosition(g);
ForumListItem item = adapter.getItemAt(position);
if (item != null) {
item.addHeader(m);
adapter.updateItemAt(position, item);
}
}
@UiThread
private void removeForum(GroupId g) {
runOnUiThreadUnlessDestroyed(() -> {
adapter.incrementRevision();
int position = adapter.findItemPosition(g);
ForumListItem item = adapter.getItemAt(position);
if (item != null) adapter.remove(item);
});
adapter.incrementRevision();
int position = adapter.findItemPosition(g);
ForumListItem item = adapter.getItemAt(position);
if (item != null) adapter.remove(item);
}
@Override

View File

@@ -285,38 +285,34 @@ public class KeyAgreementFragment extends BaseEventFragment
}
}
@UiThread
private void keyAgreementFailed() {
runOnUiThreadUnlessDestroyed(() -> {
reset();
listener.keyAgreementFailed();
});
reset();
listener.keyAgreementFailed();
}
@UiThread
private void keyAgreementWaiting() {
runOnUiThreadUnlessDestroyed(
() -> status.setText(listener.keyAgreementWaiting()));
status.setText(listener.keyAgreementWaiting());
}
@UiThread
private void keyAgreementStarted() {
runOnUiThreadUnlessDestroyed(() -> {
qrCodeView.setVisibility(INVISIBLE);
statusView.setVisibility(VISIBLE);
status.setText(listener.keyAgreementStarted());
});
qrCodeView.setVisibility(INVISIBLE);
statusView.setVisibility(VISIBLE);
status.setText(listener.keyAgreementStarted());
}
@UiThread
private void keyAgreementAborted(boolean remoteAborted) {
runOnUiThreadUnlessDestroyed(() -> {
reset();
listener.keyAgreementAborted(remoteAborted);
});
reset();
listener.keyAgreementAborted(remoteAborted);
}
@UiThread
private void keyAgreementFinished(KeyAgreementResult result) {
runOnUiThreadUnlessDestroyed(() -> {
statusView.setVisibility(VISIBLE);
status.setText(listener.keyAgreementFinished(result));
});
statusView.setVisibility(VISIBLE);
status.setText(listener.keyAgreementFinished(result));
}
private void setQrCode(Payload localPayload) {

View File

@@ -73,12 +73,9 @@ public class OpenDatabaseActivity extends BriarActivity
public void eventOccurred(Event e) {
if (e instanceof LifecycleEvent) {
LifecycleState state = ((LifecycleEvent) e).getLifecycleState();
if (state.isAfter(STARTING_SERVICES))
runOnUiThreadUnlessDestroyed(this::finishAndStartApp);
else if (state == MIGRATING_DATABASE)
runOnUiThreadUnlessDestroyed(this::showMigration);
else if (state == COMPACTING_DATABASE)
runOnUiThreadUnlessDestroyed(this::showCompaction);
if (state.isAfter(STARTING_SERVICES)) finishAndStartApp();
else if (state == MIGRATING_DATABASE) showMigration();
else if (state == COMPACTING_DATABASE) showCompaction();
}
}

View File

@@ -6,6 +6,7 @@ import android.content.res.Configuration;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.UiThread;
import android.support.design.widget.NavigationView;
import android.support.design.widget.NavigationView.OnNavigationItemSelectedListener;
import android.support.v4.app.ActivityCompat;
@@ -435,17 +436,16 @@ public class NavDrawerActivity extends BriarActivity implements
};
}
@UiThread
private void setTransport(TransportId id, boolean enabled) {
runOnUiThreadUnlessDestroyed(() -> {
if (transports == null || transportsAdapter == null) return;
for (Transport t : transports) {
if (t.id.equals(id)) {
t.enabled = enabled;
transportsAdapter.notifyDataSetChanged();
break;
}
if (transports == null || transportsAdapter == null) return;
for (Transport t : transports) {
if (t.id.equals(id)) {
t.enabled = enabled;
transportsAdapter.notifyDataSetChanged();
break;
}
});
}
}
private void updateTransports() {

View File

@@ -53,7 +53,8 @@ public class NavDrawerControllerImpl extends DbControllerImpl
private final SettingsManager settingsManager;
private final EventBus eventBus;
private volatile TransportStateListener listener;
// UI thread
private TransportStateListener listener;
@Inject
NavDrawerControllerImpl(@DatabaseExecutor Executor dbExecutor,
@@ -82,7 +83,6 @@ public class NavDrawerControllerImpl extends DbControllerImpl
@Override
public void onActivityDestroy() {
}
@Override
@@ -92,21 +92,16 @@ public class NavDrawerControllerImpl extends DbControllerImpl
if (LOG.isLoggable(INFO)) {
LOG.info("TransportEnabledEvent: " + id.getString());
}
transportStateUpdate(id, true);
listener.stateUpdate(id, true);
} else if (e instanceof TransportDisabledEvent) {
TransportId id = ((TransportDisabledEvent) e).getTransportId();
if (LOG.isLoggable(INFO)) {
LOG.info("TransportDisabledEvent: " + id.getString());
}
transportStateUpdate(id, false);
listener.stateUpdate(id, false);
}
}
private void transportStateUpdate(TransportId id, boolean enabled) {
listener.runOnUiThreadUnlessDestroyed(
() -> listener.stateUpdate(id, enabled));
}
@Override
public void showExpiryWarning(ResultHandler<ExpiryWarning> handler) {
if (!IS_DEBUG_BUILD && !IS_BETA_BUILD) {
@@ -188,7 +183,6 @@ public class NavDrawerControllerImpl extends DbControllerImpl
@Override
public boolean isTransportRunning(TransportId transportId) {
Plugin plugin = pluginManager.getPlugin(transportId);
return plugin != null && plugin.isRunning();
}

View File

@@ -1,9 +1,11 @@
package org.briarproject.briar.android.navdrawer;
import android.support.annotation.UiThread;
import org.briarproject.bramble.api.plugin.TransportId;
import org.briarproject.briar.android.DestroyableContext;
interface TransportStateListener extends DestroyableContext {
interface TransportStateListener {
@UiThread
void stateUpdate(TransportId id, boolean enabled);
}

View File

@@ -84,31 +84,26 @@ class GroupControllerImpl extends
GroupMessageAddedEvent g = (GroupMessageAddedEvent) e;
if (!g.isLocal() && g.getGroupId().equals(getGroupId())) {
LOG.info("Group message received, adding...");
GroupMessageItem item = buildItem(g.getHeader(), g.getText());
listener.runOnUiThreadUnlessDestroyed(
() -> listener.onItemReceived(item));
listener.onItemReceived(buildItem(g.getHeader(), g.getText()));
}
} else if (e instanceof ContactRelationshipRevealedEvent) {
ContactRelationshipRevealedEvent c =
(ContactRelationshipRevealedEvent) e;
if (getGroupId().equals(c.getGroupId())) {
listener.runOnUiThreadUnlessDestroyed(() ->
listener.onContactRelationshipRevealed(c.getMemberId(),
c.getContactId(), c.getVisibility()));
listener.onContactRelationshipRevealed(c.getMemberId(),
c.getContactId(), c.getVisibility());
}
} else if (e instanceof GroupInvitationResponseReceivedEvent) {
GroupInvitationResponseReceivedEvent g =
(GroupInvitationResponseReceivedEvent) e;
GroupInvitationResponse r = g.getMessageHeader();
if (getGroupId().equals(r.getShareableId()) && r.wasAccepted()) {
listener.runOnUiThreadUnlessDestroyed(
() -> listener.onInvitationAccepted(g.getContactId()));
listener.onInvitationAccepted(g.getContactId());
}
} else if (e instanceof GroupDissolvedEvent) {
GroupDissolvedEvent g = (GroupDissolvedEvent) e;
if (getGroupId().equals(g.getGroupId())) {
listener.runOnUiThreadUnlessDestroyed(
() -> listener.onGroupDissolved());
listener.onGroupDissolved();
}
}
}

View File

@@ -5,7 +5,6 @@ import android.support.annotation.UiThread;
import org.briarproject.bramble.api.db.DbException;
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
import org.briarproject.bramble.api.sync.GroupId;
import org.briarproject.briar.android.DestroyableContext;
import org.briarproject.briar.android.controller.DbController;
import org.briarproject.briar.android.controller.handler.ExceptionHandler;
import org.briarproject.briar.android.controller.handler.ResultExceptionHandler;
@@ -19,6 +18,7 @@ interface GroupListController extends DbController {
/**
* The listener must be set right after the controller was injected
*/
@UiThread
void setGroupListListener(GroupListListener listener);
@UiThread
@@ -35,7 +35,7 @@ interface GroupListController extends DbController {
void loadAvailableGroups(
ResultExceptionHandler<Integer, DbException> result);
interface GroupListListener extends DestroyableContext {
interface GroupListListener {
@UiThread
void onGroupMessageAdded(GroupMessageHeader header);

View File

@@ -23,7 +23,6 @@ import org.briarproject.briar.android.controller.handler.ExceptionHandler;
import org.briarproject.briar.android.controller.handler.ResultExceptionHandler;
import org.briarproject.briar.api.android.AndroidNotificationManager;
import org.briarproject.briar.api.client.MessageTracker.GroupCount;
import org.briarproject.briar.api.privategroup.GroupMessageHeader;
import org.briarproject.briar.api.privategroup.PrivateGroup;
import org.briarproject.briar.api.privategroup.PrivateGroupManager;
import org.briarproject.briar.api.privategroup.event.GroupDissolvedEvent;
@@ -61,7 +60,8 @@ class GroupListControllerImpl extends DbControllerImpl
private final AndroidNotificationManager notificationManager;
private final EventBus eventBus;
protected volatile GroupListListener listener;
// UI thread
private GroupListListener listener;
@Inject
GroupListControllerImpl(@DatabaseExecutor Executor dbExecutor,
@@ -85,9 +85,7 @@ class GroupListControllerImpl extends DbControllerImpl
@Override
@CallSuper
public void onStart() {
if (listener == null)
throw new IllegalStateException(
"GroupListListener needs to be attached");
if (listener == null) throw new IllegalStateException();
eventBus.addListener(this);
notificationManager.clearAllGroupMessageNotifications();
}
@@ -104,54 +102,31 @@ class GroupListControllerImpl extends DbControllerImpl
if (e instanceof GroupMessageAddedEvent) {
GroupMessageAddedEvent g = (GroupMessageAddedEvent) e;
LOG.info("Private group message added");
onGroupMessageAdded(g.getHeader());
listener.onGroupMessageAdded(g.getHeader());
} else if (e instanceof GroupInvitationRequestReceivedEvent) {
LOG.info("Private group invitation received");
onGroupInvitationReceived();
listener.onGroupInvitationReceived();
} else if (e instanceof GroupAddedEvent) {
GroupAddedEvent g = (GroupAddedEvent) e;
ClientId id = g.getGroup().getClientId();
if (id.equals(CLIENT_ID)) {
LOG.info("Private group added");
onGroupAdded(g.getGroup().getId());
listener.onGroupAdded(g.getGroup().getId());
}
} else if (e instanceof GroupRemovedEvent) {
GroupRemovedEvent g = (GroupRemovedEvent) e;
ClientId id = g.getGroup().getClientId();
if (id.equals(CLIENT_ID)) {
LOG.info("Private group removed");
onGroupRemoved(g.getGroup().getId());
listener.onGroupRemoved(g.getGroup().getId());
}
} else if (e instanceof GroupDissolvedEvent) {
GroupDissolvedEvent g = (GroupDissolvedEvent) e;
LOG.info("Private group dissolved");
onGroupDissolved(g.getGroupId());
listener.onGroupDissolved(g.getGroupId());
}
}
private void onGroupMessageAdded(GroupMessageHeader h) {
listener.runOnUiThreadUnlessDestroyed(
() -> listener.onGroupMessageAdded(h));
}
private void onGroupInvitationReceived() {
listener.runOnUiThreadUnlessDestroyed(
() -> listener.onGroupInvitationReceived());
}
private void onGroupAdded(GroupId g) {
listener.runOnUiThreadUnlessDestroyed(() -> listener.onGroupAdded(g));
}
private void onGroupRemoved(GroupId g) {
listener.runOnUiThreadUnlessDestroyed(() -> listener.onGroupRemoved(g));
}
private void onGroupDissolved(GroupId g) {
listener.runOnUiThreadUnlessDestroyed(
() -> listener.onGroupDissolved(g));
}
@Override
public void loadGroups(
ResultExceptionHandler<Collection<GroupItem>, DbException> handler) {

View File

@@ -95,8 +95,7 @@ public class GroupMemberListActivity extends BriarActivity
} else if (e instanceof GroupRemovedEvent) {
GroupRemovedEvent g = (GroupRemovedEvent) e;
if (g.getGroup().getId().equals(groupId)) {
runOnUiThreadUnlessDestroyed(
this::supportFinishAfterTransition);
supportFinishAfterTransition();
}
}
// TODO ContactConnectedEvent and ContactDisconnectedEvent

View File

@@ -39,6 +39,8 @@ public abstract class InvitationControllerImpl<I extends InvitationItem>
Logger.getLogger(InvitationControllerImpl.class.getName());
private final EventBus eventBus;
// UI thread
protected InvitationListener listener;
public InvitationControllerImpl(@DatabaseExecutor Executor dbExecutor,

View File

@@ -100,8 +100,7 @@ abstract class SharingStatusActivity extends BriarActivity
} else if (e instanceof GroupRemovedEvent) {
GroupRemovedEvent g = (GroupRemovedEvent) e;
if (g.getGroup().getId().equals(getGroupId())) {
runOnUiThreadUnlessDestroyed(
this::supportFinishAfterTransition);
supportFinishAfterTransition();
}
}
// TODO ContactConnectedEvent and ContactDisconnectedEvent

View File

@@ -7,7 +7,6 @@ import org.briarproject.bramble.api.db.DbException;
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
import org.briarproject.bramble.api.sync.GroupId;
import org.briarproject.bramble.api.sync.MessageId;
import org.briarproject.briar.android.DestroyableContext;
import org.briarproject.briar.android.controller.ActivityLifecycleController;
import org.briarproject.briar.android.controller.handler.ExceptionHandler;
import org.briarproject.briar.android.controller.handler.ResultExceptionHandler;
@@ -52,7 +51,7 @@ public interface ThreadListController<G extends NamedGroup, I extends ThreadItem
void onInvitationAccepted(ContactId c);
}
interface ThreadListDataSource extends DestroyableContext {
interface ThreadListDataSource {
@UiThread @Nullable
MessageId getFirstVisibleMessageId();

View File

@@ -50,15 +50,18 @@ public abstract class ThreadListControllerImpl<G extends NamedGroup, I extends T
Logger.getLogger(ThreadListControllerImpl.class.getName());
private final EventBus eventBus;
private final MessageTracker messageTracker;
private final Map<MessageId, String> textCache = new ConcurrentHashMap<>();
private volatile GroupId groupId;
protected final IdentityManager identityManager;
protected final AndroidNotificationManager notificationManager;
protected final Executor cryptoExecutor;
protected final Clock clock;
private final MessageTracker messageTracker;
protected volatile L listener;
// UI thread
protected L listener;
protected ThreadListControllerImpl(@DatabaseExecutor Executor dbExecutor,
LifecycleManager lifecycleManager, IdentityManager identityManager,
@@ -121,8 +124,7 @@ public abstract class ThreadListControllerImpl<G extends NamedGroup, I extends T
GroupRemovedEvent s = (GroupRemovedEvent) e;
if (s.getGroup().getId().equals(getGroupId())) {
LOG.info("Group removed");
listener.runOnUiThreadUnlessDestroyed(
() -> listener.onGroupRemoved());
listener.onGroupRemoved();
}
}
}