Exemplo n.º 1
0
 private String getChatKey(Peer peer) {
   if (peer.getPeerType() == PeerType.PRIVATE) {
     return "PRIVATE_" + peer.getPeerId();
   } else if (peer.getPeerType() == PeerType.GROUP) {
     return "GROUP_" + peer.getPeerId();
   } else {
     throw new RuntimeException("Unsupported peer");
   }
 }
Exemplo n.º 2
0
  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    int i = item.getItemId();
    if (i == android.R.id.home) {
      finish();

    } else if (i == R.id.clear) {
      new AlertDialog.Builder(this)
          .setMessage(R.string.alert_delete_all_messages_text)
          .setPositiveButton(
              R.string.alert_delete_all_messages_yes,
              new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                  execute(messenger().clearChat(peer));
                }
              })
          .setNegativeButton(R.string.dialog_cancel, null)
          .show()
          .setCanceledOnTouchOutside(true);

    } else if (i == R.id.leaveGroup) {
      new AlertDialog.Builder(this)
          .setMessage(
              getString(R.string.alert_leave_group_message)
                  .replace("%1$s", groups().get(peer.getPeerId()).getName().get()))
          .setPositiveButton(
              R.string.alert_leave_group_yes,
              new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog2, int which) {
                  execute(messenger().leaveGroup(peer.getPeerId()));
                  finish();
                }
              })
          .setNegativeButton(R.string.dialog_cancel, null)
          .show()
          .setCanceledOnTouchOutside(true);

    } else if (i == R.id.contact) {
      startActivity(Intents.openProfile(peer.getPeerId(), ChatActivity.this));

    } else if (i == R.id.groupInfo) {
      startActivity(Intents.openGroup(peer.getPeerId(), ChatActivity.this));

    } else if (i
        == R.id.files) { // startActivity(Intents.openDocs(chatType, chatId, ChatActivity.this));

    }
    return super.onOptionsItemSelected(item);
  }
Exemplo n.º 3
0
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {

    // Inflating menu
    getMenuInflater().inflate(R.menu.chat_menu, menu);

    // Show menu for opening chat contact
    if (peer.getPeerType() == PeerType.PRIVATE) {
      menu.findItem(R.id.contact).setVisible(true);
    } else {
      menu.findItem(R.id.contact).setVisible(false);
    }

    // Show menus for leave group and group info view
    if (peer.getPeerType() == PeerType.GROUP) {
      if (groups().get(peer.getPeerId()).isMember().get()) {
        menu.findItem(R.id.leaveGroup).setVisible(true);
        menu.findItem(R.id.groupInfo).setVisible(true);
      } else {
        menu.findItem(R.id.leaveGroup).setVisible(false);
        menu.findItem(R.id.groupInfo).setVisible(false);
      }
    } else {
      menu.findItem(R.id.groupInfo).setVisible(false);
      menu.findItem(R.id.leaveGroup).setVisible(false);
    }

    // Hide unsupported files menu
    menu.findItem(R.id.files).setVisible(false);

    return super.onCreateOptionsMenu(menu);
  }
Exemplo n.º 4
0
  private void showMentions(boolean initEmpty) {
    if (isMentionsVisible) {
      return;
    }
    isMentionsVisible = true;

    GroupVM groupInfo = groups().get(peer.getPeerId());
    mentionsAdapter =
        new MentionsAdapter(
            groupInfo.getId(),
            this,
            new MentionsAdapter.MentionsUpdatedCallback() {

              @Override
              public void onMentionsUpdated(int oldRowsCount, int newRowsCount) {
                onMentionsChanged(oldRowsCount, newRowsCount);
              }
            },
            initEmpty);

    mentionsList.setAdapter(mentionsAdapter);
    mentionsList.setOnItemClickListener(
        new AdapterView.OnItemClickListener() {
          @Override
          public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Object item = parent.getItemAtPosition(position);
            if (item != null && item instanceof MentionFilterResult) {

              String origMention = ((MentionFilterResult) item).getMentionString();

              if (mentionStart != -1
                  && mentionStart + mentionSearchString.length() + 1
                      <= messageEditText.getText().length()) {

                String mentionString = origMention + ": ";

                Editable text = messageEditText.getText();

                int cursorPosition = mentionStart + mentionString.length();

                text.replace(
                    mentionStart, mentionStart + mentionSearchString.length() + 1, mentionString);

                messageEditText.setSelection(cursorPosition, cursorPosition);
              }
              hideMentions();
            }
          }
        });

    expandMentions(mentionsList, 0, mentionsList.getCount());
  }
Exemplo n.º 5
0
  public void doSendText(
      @NotNull Peer peer,
      @NotNull String text,
      @Nullable ArrayList<Integer> mentions, /*Ignored*/
      @Nullable String markDownText,
      boolean autoDetect) {

    text = text.trim();

    long rid = RandomUtils.nextRid();
    long date = createPendingDate();
    long sortDate = date + 365 * 24 * 60 * 60 * 1000L;

    if (autoDetect) {
      mentions = new ArrayList<>();
      if (peer.getPeerType() == PeerType.GROUP) {
        Group group = getGroup(peer.getPeerId());
        String lowText = text.toLowerCase();
        if (group.getMembers() != null) {
          for (GroupMember member : group.getMembers()) {
            User user = getUser(member.getUid());
            if (user.getNick() != null) {
              String nick = "@" + user.getNick().toLowerCase();
              // TODO: Better filtering
              if (lowText.contains(nick + ":")
                  || lowText.contains(nick + " ")
                  || lowText.contains(" " + nick)
                  || lowText.endsWith(nick)
                  || lowText.equals(nick)) {
                mentions.add(user.getUid());
              }
            }
          }
        }
      }
    }

    TextContent content = TextContent.create(text, null, mentions);

    Message message = new Message(rid, sortDate, date, myUid(), MessageState.PENDING, content);

    context().getMessagesModule().getRouter().onOutgoingMessage(peer, message);

    pendingMessages.getPendingMessages().add(new PendingMessage(peer, rid, content));
    savePending();

    performSendContent(peer, rid, content);
  }
Exemplo n.º 6
0
  @Override
  protected void onPerformBind() {
    super.onPerformBind();

    // Performing all required Data Binding here

    if (peer.getPeerType() == PeerType.PRIVATE) {

      // Loading user
      final UserVM user = users().get(peer.getPeerId());
      if (user == null) {
        finish();
        return;
      }

      // Binding User Avatar to Toolbar
      bind(barAvatar, user.getId(), user.getAvatar(), user.getName());

      // Binding User name to Toolbar
      bind(barTitle, user.getName());

      // Binding User presence to Toolbar
      bind(barSubtitle, barSubtitleContainer, user);

      // Binding User typing to Toolbar
      bindPrivateTyping(
          barTyping, barTypingContainer, barSubtitle, messenger().getTyping(user.getId()));

      // Hide removedFromGroup panel as we are not in group
      removedFromGroup.setVisibility(View.GONE);

    } else if (peer.getPeerType() == PeerType.GROUP) {

      // Loading group
      GroupVM group = groups().get(peer.getPeerId());
      if (group == null) {
        finish();
        return;
      }

      // Binding Group avatar to Toolbar
      bind(barAvatar, group.getId(), group.getAvatar(), group.getName());

      // Binding Group title to Toolbar
      bind(barTitle, group.getName());

      // Subtitle is always visible for Groups
      barSubtitleContainer.setVisibility(View.VISIBLE);

      // Binding group members
      bind(barSubtitle, barSubtitleContainer, group);

      // Binding group typing
      bindGroupTyping(
          barTyping, barTypingContainer, barSubtitle, messenger().getGroupTyping(group.getId()));

      // Binding membership flag to removedFromGroup panel
      bind(
          messenger().getGroups().get(peer.getPeerId()).isMember(),
          new ValueChangedListener<Boolean>() {
            @Override
            public void onChanged(Boolean val, Value<Boolean> Value) {
              removedFromGroup.setVisibility(val ? View.GONE : View.VISIBLE);
            }
          });
    }
  }
  @Override
  public void onPeerClicked(Peer peer) {
    Activity activity = getActivity();
    String name;
    if (peer.getPeerType() == PeerType.PRIVATE) {
      name = messenger().getUser(peer.getPeerId()).getName().get();
    } else if (peer.getPeerType() == PeerType.GROUP) {
      name = messenger().getGroup(peer.getPeerId()).getName().get();
    } else {
      activity.finish();
      return;
    }
    new AlertDialog.Builder(getActivity())
        .setMessage(getActivity().getString(R.string.confirm_share) + " " + name + "?")
        .setPositiveButton(
            R.string.dialog_ok,
            (dialog, which) -> {
              Intent intent = Intents.openDialog(peer, false, activity);

              if (shareAction.getForwardText() != null) {
                intent.putExtra(Intents.EXTRA_FORWARD_TEXT, shareAction.getForwardText());
              }
              if (shareAction.getForwardTextRaw() != null) {
                intent.putExtra(Intents.EXTRA_FORWARD_TEXT_RAW, shareAction.getForwardTextRaw());
              }
              if (shareAction.getForwardTextRaw() != null) {
                intent.putExtra(Intents.EXTRA_FORWARD_CONTENT, shareAction.getForwardTextRaw());
              }

              if (shareAction.getText() != null) {
                messenger().sendMessage(peer, shareAction.getText());
              } else if (shareAction.getUris().size() > 0) {
                for (String sendUri : shareAction.getUris()) {
                  executeSilent(messenger().sendUri(peer, Uri.parse(sendUri)));
                }
              } else if (shareAction.getUserId() != null) {
                String userName = users().get(shareAction.getUserId()).getName().get();
                String mentionTitle = "@".concat(userName);
                ArrayList<Integer> mention = new ArrayList<>();
                mention.add(shareAction.getUserId());
                messenger()
                    .sendMessage(
                        peer,
                        mentionTitle,
                        "["
                            .concat(mentionTitle)
                            .concat(
                                "](people://"
                                    .concat(Integer.toString(shareAction.getUserId()))
                                    .concat(")")),
                        mention);
              } else if (shareAction.getDocContent() != null) {
                try {
                  messenger().forwardContent(peer, AbsContent.parse(shareAction.getDocContent()));
                } catch (IOException e) {
                  e.printStackTrace();
                }
              }

              startActivity(intent);
              activity.finish();
              shareAction = null;
            })
        .setNegativeButton(
            R.string.dialog_cancel,
            (dialog, which) -> {
              dialog.dismiss();
            })
        .show();
  }