예제 #1
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);
  }
예제 #2
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");
   }
 }
예제 #3
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);
  }
예제 #4
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);
  }
예제 #5
0
  public static JsGroup fromGroupVM(GroupVM groupVM, JsMessenger messenger) {
    int online = groupVM.getPresence().get();
    String presence = messenger.getFormatter().formatGroupMembers(groupVM.getMembersCount());
    if (online > 0) {
      presence += ", " + messenger.getFormatter().formatGroupOnline(online);
    }
    String fileUrl = null;
    String bigFileUrl = null;
    Avatar avatar = groupVM.getAvatar().get();
    if (avatar != null) {
      if (avatar.getSmallImage() != null) {
        fileUrl = messenger.getFileUrl(avatar.getSmallImage().getFileReference());
      }
      if (avatar.getLargeImage() != null) {
        bigFileUrl = messenger.getFileUrl(avatar.getLargeImage().getFileReference());
      }
    }

    ArrayList<JsGroupMember> convertedMembers = new ArrayList<JsGroupMember>();
    HashSet<GroupMember> groupMembers = groupVM.getMembers().get();
    GroupMember[] members = groupMembers.toArray(new GroupMember[groupMembers.size()]);
    for (GroupMember g : members) {
      JsPeerInfo peerInfo = messenger.buildPeerInfo(Peer.user(g.getUid()));
      Log.d("JsGroup", "PeerInfo: " + peerInfo);
      convertedMembers.add(
          JsGroupMember.create(
              peerInfo,
              g.isAdministrator(),
              g.getInviterUid() == messenger.myUid()
                  || groupVM.getCreatorId() == messenger.myUid()));
    }
    Collections.sort(
        convertedMembers,
        new Comparator<JsGroupMember>() {
          @Override
          public int compare(JsGroupMember o1, JsGroupMember o2) {
            return o1.getPeerInfo().getTitle().compareToIgnoreCase(o2.getPeerInfo().getTitle());
          }
        });
    JsArray<JsGroupMember> jsMembers = JsArray.createArray().cast();
    for (JsGroupMember member : convertedMembers) {
      jsMembers.push(member);
    }
    return create(
        groupVM.getId(),
        groupVM.getName().get(),
        fileUrl,
        bigFileUrl,
        Placeholders.getPlaceholder(groupVM.getId()),
        groupVM.getCreatorId(),
        presence,
        jsMembers);
  }
예제 #6
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());
  }
예제 #7
0
  @Override
  public void onCreate(Bundle saveInstance) {
    // Reading peer of chat
    peer = Peer.fromUniqueId(getIntent().getExtras().getLong(EXTRA_CHAT_PEER));

    if (saveInstance == null) {
      // Set compose state for auto-showing menu
      isCompose = getIntent().getExtras().getBoolean(EXTRA_CHAT_COMPOSE, false);
    } else {
      // Activity restore
      pending_fileName = saveInstance.getString(STATE_FILE_NAME, null);
    }

    super.onCreate(saveInstance);

    messageEditText.addTextChangedListener(new TextWatcherImp());

    // Mentions
    mentionsList = (ListView) findViewById(R.id.mentionsList);

    // Quote
    quoteContainer = (FrameLayout) findViewById(R.id.quoteContainer);
    quoteText = (TextView) findViewById(R.id.quote_text);
    findViewById(R.id.ib_close_quote)
        .setOnClickListener(
            new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                goneView(quoteContainer);
                quoteText.setText("");
                currentQuote = "";
              }
            });

    // Sharing
    sendUri = getIntent().getStringExtra("send_uri");
    sendUriMultiple = getIntent().getStringArrayListExtra("send_uri_multiple");
    shareUser = getIntent().getIntExtra("share_user", 0);

    // Forwarding
    forwardText = getIntent().getStringExtra("forward_text");
    forwardTextRaw = getIntent().getStringExtra("forward_text_raw");
    sendText = getIntent().getStringExtra("send_text");
    forwardDocDescriptor = getIntent().getStringExtra("forward_doc_descriptor");
    forwardDocIsDoc = getIntent().getBooleanExtra("forward_doc_is_doc", true);
  }
예제 #8
0
 public static Intent build(Peer peer, boolean compose, Context context) {
   final Intent intent = new Intent(context, ChatActivity.class);
   intent.putExtra(EXTRA_CHAT_PEER, peer.getUnuqueId());
   intent.putExtra(EXTRA_CHAT_COMPOSE, compose);
   return intent;
 }
예제 #9
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);
            }
          });
    }
  }
예제 #10
0
  @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();
  }
예제 #11
0
 public JsPeer getGroupPeer(int gid) {
   return JsPeer.create(Peer.group(gid));
 }
예제 #12
0
 public JsPeer getUserPeer(int uid) {
   return JsPeer.create(Peer.user(uid));
 }