@Override
 public void loadMenuConversation(String conversationId) {
   menuConversation = conversationsList.getConversation(conversationId);
   menuConversation.removeUpdateListener(menuConversationUpdateListener);
   menuConversation.addUpdateListener(menuConversationUpdateListener);
   menuConversationUpdateListener.updated();
 }
 @Override
 public void sendMessage(final byte[] jpegData) {
   IConversation current = getCurrentConversation();
   if (current != null) {
     current.sendMessage(new MessageContent.Image(ImageAssetFactory.getImageAsset(jpegData)));
   }
 }
  private void identifyCurrentConversation(IConversation previousSelectedConversation) {
    if (selectedConversation == null) {
      if (previousSelectedConversation != null
          && previousSelectedConversation.getType() == IConversation.Type.INCOMING_CONNECTION) {
        // Switch to another incoming connect request.
        // Previous (ignored) conversation might still be included in list of incoming
        // conversations, find another one
        for (int i = 0; i < inboxList.size(); i++) {
          IConversation incomingConnectRequest = inboxList.get(i);
          if (!incomingConnectRequest.getId().equals(previousSelectedConversation.getId())) {
            setCurrentConversation(incomingConnectRequest, ConversationChangeRequester.FIRST_LOAD);
            return;
          }
        }
      }

      // TODO: AN-2974
      if (conversationsList.size() > 0) {
        setCurrentConversation(conversationsList.get(0), ConversationChangeRequester.FIRST_LOAD);
        return;
      }
    }

    setCurrentConversation(selectedConversation, ConversationChangeRequester.FIRST_LOAD);
  }
  @Override
  public void onOptionsItemClicked(IConversation conversation, User user, OptionsMenuItem item) {
    switch (item) {
      case BLOCK:
        showBlockUserConfirmation(user);
        break;
      case UNBLOCK:
        user.unblock();
        break;
      case ARCHIVE:
        getStoreFactory().getConversationStore().archive(conversation, true);
        getControllerFactory()
            .getTrackingController()
            .tagEvent(new ArchivedConversationEvent(conversation.getType().toString()));
        break;
      case UNARCHIVE:
        getStoreFactory().getConversationStore().archive(conversation, false);
        getControllerFactory()
            .getTrackingController()
            .tagEvent(new UnarchivedConversationEvent(conversation.getType().toString()));
        break;
      case SILENCE:
        conversation.setMuted(true);
        break;
      case UNSILENCE:
        conversation.setMuted(false);
        break;
    }

    optionsMenuControl.close();
  }
  @Override
  public void setCurrentConversation(
      IConversation conversation, ConversationChangeRequester conversationChangerSender) {
    if (conversation instanceof InboxLinkConversation) {
      conversation = inboxList.get(0);
    }

    if (conversation != null) {
      conversation.setArchived(false);
    }
    if (conversation != null) {
      Timber.i(
          "Set current conversation to %s, requester %s",
          conversation.getName(), conversationChangerSender);
    } else {
      Timber.i("Set current conversation to null, requester %s", conversationChangerSender);
    }
    this.conversationChangeRequester = conversationChangerSender;
    IConversation oldConversation =
        conversationChangerSender == ConversationChangeRequester.FIRST_LOAD
            ? null
            : selectedConversation;
    conversationsList.setSelectedConversation(conversation);

    if (oldConversation == null
        || (oldConversation != null
            && conversation != null
            && oldConversation.getId().equals(conversation.getId()))) {
      // Notify explicitly if the conversation doesn't change, the UiSginal notifies only when the
      // conversation changes
      notifyCurrentConversationHasChanged(oldConversation, conversation, conversationChangerSender);
    }
  }
  @Override
  public void archive(IConversation conversation, boolean archive) {
    if (conversation.isSelected()) {
      final IConversation nextConversation = getNextConversation();
      if (nextConversation != null) {
        // don't want to change selected item immediately
        new Handler()
            .postDelayed(
                new Runnable() {
                  @Override
                  public void run() {
                    if (conversationsList != null) {
                      setCurrentConversation(
                          nextConversation, ConversationChangeRequester.ARCHIVED_RESULT);
                    }
                  }
                },
                ARCHIVE_DELAY);
      }
    }

    conversation.setArchived(archive);

    // Set current conversation to unarchived
    if (!archive) {
      setCurrentConversation(
          conversation, ConversationChangeRequester.CONVERSATION_LIST_UNARCHIVED_CONVERSATION);
    }
  }
 @Override
 public void deleteConversation(IConversation conversation, boolean leaveConversation) {
   if (leaveConversation) {
     conversation.leave();
   } else {
     conversation.clear();
   }
 }
 private boolean isPendingIncomingConnectRequest(IConversation conversation) {
   if (conversation.isMe() || conversation.getType() == IConversation.Type.GROUP) {
     return false;
   }
   if (conversation.getType() == IConversation.Type.INCOMING_CONNECTION) {
     return true;
   }
   return false;
 }
 @Override
 public void sendMessage(
     IConversation conversation,
     AudioAssetForUpload audioAssetForUpload,
     MessageContent.Asset.ErrorHandler errorHandler) {
   if (conversation != null) {
     Timber.i("Send audio file to %s", conversation.getName());
     conversation.sendMessage(new MessageContent.Asset(audioAssetForUpload, errorHandler));
   }
 }
  @Override
  public void tearDown() {
    if (selectedConvSubscription != null) {
      selectedConvSubscription.cancel();
    }

    if (syncIndicator != null) {
      syncIndicator.removeUpdateListener(syncStateUpdateListener);
      syncIndicator = null;
    }

    if (conversationsList != null) {
      conversationsList.removeUpdateListener(conversationListUpdateListener);
      conversationsList = null;
    }

    if (inboxList != null) {
      inboxList.removeUpdateListener(inboxListUpdateListener);
      inboxList = null;
    }
    if (menuConversation != null) {
      menuConversation.removeUpdateListener(menuConversationUpdateListener);
      menuConversation = null;
    }

    establishedConversationsList = null;
    selectedConvSubscription = null;
    selectedConversation = null;
    conversationUiSignal = null;
  }
 @Override
 public void onConversationLoaded(IConversation conversation) {
   currentNextMessageConversationType = conversation.getType();
   getStoreFactory()
       .getConversationStore()
       .loadConversation(
           newIncomingMessage.getConversation().getId(), newMessageConversationListener);
 }
 @Override
 public void onConversationLoaded(IConversation conversation) {
   IConversation.Type newIncomingMessageConversationType = conversation.getType();
   if (currentNextMessageConversationType != IConversation.Type.ONE_TO_ONE
       || newIncomingMessageConversationType != IConversation.Type.GROUP) {
     currentNextMessage = newIncomingMessage;
   }
 }
 @Override
 public boolean hasOngoingCallInCurrentConversation() {
   if (selectedConversation == null) {
     return false;
   }
   VoiceChannel voiceChannel = selectedConversation.getVoiceChannel();
   if (voiceChannel == null) {
     return false;
   }
   VoiceChannelState state = voiceChannel.getState();
   return state != VoiceChannelState.NO_ACTIVE_USERS && state != VoiceChannelState.UNKNOWN;
 }
 @Override
 public IConversation getNextConversation() {
   if (conversationsList == null || conversationsList.size() == 0) {
     return null;
   }
   for (int i = 0; i < conversationsList.size(); i++) {
     IConversation previousConversation = i >= 1 ? conversationsList.get(i - 1) : null;
     IConversation conversation = conversationsList.get(i);
     IConversation nextConversation =
         i == (conversationsList.size() - 1) ? null : conversationsList.get(i + 1);
     if (selectedConversation.equals(conversation)) {
       if (nextConversation != null) {
         return nextConversation;
       }
       return previousConversation;
     }
   }
   return null;
 }
 @Override
 public String getCurrentConversationId() {
   return (selectedConversation == null) ? null : selectedConversation.getId();
 }
 @Override
 public void onConversationUpdated(IConversation conversation) {
   if (conversation != null && conversation.getType() == IConversation.Type.ONE_TO_ONE) {
     getContainer().onAcceptedPendingOutgoingConnectRequest(conversation);
   }
 }
 @Override
 public void sendMessage(IConversation conversation, String message) {
   if (conversation != null) {
     conversation.sendMessage(new MessageContent.Text(message));
   }
 }
 @Override
 public void leave(IConversation conversation) {
   conversation.leave();
 }
 @Override
 public void sendMessage(IConversation conversation, ImageAsset imageAsset) {
   if (conversation != null) {
     conversation.sendMessage(new MessageContent.Image(imageAsset));
   }
 }
 @Override
 public void mute(IConversation conversation, boolean mute) {
   conversation.setMuted(mute);
 }
 @Override
 public int getPositionInList(IConversation conversation) {
   return conversationsList.getConversationIndex(conversation.getId());
 }