private void closeAllChats() {
   for (AbstractChat chat : MessageManager.getInstance().getActiveChats()) {
     MessageManager.getInstance().closeChat(chat.getAccount(), chat.getUser());
     NotificationManager.getInstance()
         .removeMessageNotification(chat.getAccount(), chat.getUser());
   }
   getContactListFragment().getAdapter().onChange();
 }
Exemplo n.º 2
0
 private void onLoaded(Collection<RoomChat> roomChats, Collection<RoomChat> needJoins) {
   for (RoomChat roomChat : roomChats) {
     AbstractChat abstractChat =
         MessageManager.getInstance().getChat(roomChat.getAccount(), roomChat.getUser());
     if (abstractChat != null) MessageManager.getInstance().removeChat(abstractChat);
     MessageManager.getInstance().addChat(roomChat);
     if (needJoins.contains(roomChat)) roomChat.setState(RoomState.waiting);
   }
   NotificationManager.getInstance().registerNotificationProvider(inviteProvider);
   NotificationManager.getInstance().registerNotificationProvider(authorizationErrorProvider);
 }
  private void setUpOptionsMenu(Menu menu) {
    AbstractChat abstractChat = MessageManager.getInstance().getChat(account, user);

    if (abstractChat instanceof RoomChat) {
      RoomState chatState = ((RoomChat) abstractChat).getState();

      if (chatState == RoomState.available) {
        menu.findItem(R.id.action_list_of_occupants).setVisible(true);
      }

      if (chatState == RoomState.unavailable) {
        menu.findItem(R.id.action_join_conference).setVisible(true);

      } else {
        menu.findItem(R.id.action_invite_to_chat).setVisible(true);

        if (chatState == RoomState.error) {
          menu.findItem(R.id.action_authorization_settings).setVisible(true);
        } else {
          menu.findItem(R.id.action_leave_conference).setVisible(true);
        }
      }
    }

    if (abstractChat instanceof RegularChat) {
      menu.findItem(R.id.action_view_contact).setVisible(true);
      menu.findItem(R.id.action_close_chat).setVisible(true);
      menu.findItem(R.id.action_block_contact).setVisible(true);
    }
  }
Exemplo n.º 4
0
 @Override
 public void onClick(View view) {
   switch (view.getId()) {
     case R.id.ok:
       String user = userView.getText().toString();
       if ("".equals(user)) {
         Toast.makeText(this, getString(R.string.EMPTY_USER_NAME), Toast.LENGTH_LONG).show();
         return;
       }
       String account = (String) accountView.getSelectedItem();
       if (account == null) {
         Toast.makeText(this, getString(R.string.EMPTY_ACCOUNT), Toast.LENGTH_LONG).show();
         return;
       }
       try {
         RosterManager.getInstance()
             .createContact(account, user, nameView.getText().toString(), getSelected());
         PresenceManager.getInstance().requestSubscription(account, user);
       } catch (NetworkException e) {
         Application.getInstance().onError(e);
         finish();
         return;
       }
       MessageManager.getInstance().openChat(account, user);
       finish();
       break;
     default:
       break;
   }
 }
 /**
  * Open chat with specified contact.
  *
  * <p>Show dialog to choose account if necessary.
  *
  * @param user
  * @param text can be <code>null</code>.
  */
 private void openChat(String user, String text) {
   String bareAddress = Jid.getBareAddress(user);
   ArrayList<BaseEntity> entities = new ArrayList<>();
   for (AbstractChat check : MessageManager.getInstance().getChats()) {
     if (check.isActive() && check.getUser().equals(bareAddress)) {
       entities.add(check);
     }
   }
   if (entities.size() == 1) {
     openChat(entities.get(0), text);
     return;
   }
   entities.clear();
   for (RosterContact check : RosterManager.getInstance().getContacts()) {
     if (check.isEnabled() && check.getUser().equals(bareAddress)) {
       entities.add(check);
     }
   }
   if (entities.size() == 1) {
     openChat(entities.get(0), text);
     return;
   }
   Collection<String> accounts = AccountManager.getInstance().getAccounts();
   if (accounts.isEmpty()) {
     return;
   }
   if (accounts.size() == 1) {
     openChat(new BaseEntity(accounts.iterator().next(), bareAddress), text);
     return;
   }
   AccountChooseDialogFragment.newInstance(bareAddress, text)
       .show(getFragmentManager(), "OPEN_WITH_ACCOUNT");
 }
Exemplo n.º 6
0
 /**
  * Creates or updates existed room.
  *
  * @param account
  * @param room
  * @param nickname
  * @param password
  */
 public void createRoom(
     String account, String room, String nickname, String password, boolean join, String subject) {
   removeInvite(getInvite(account, room));
   AbstractChat chat = MessageManager.getInstance().getChat(account, room);
   RoomChat roomChat;
   if (chat == null || !(chat instanceof RoomChat)) {
     if (chat != null) MessageManager.getInstance().removeChat(chat);
     roomChat = new RoomChat(account, room, nickname, password, subject);
     MessageManager.getInstance().addChat(roomChat);
   } else {
     roomChat = (RoomChat) chat;
     roomChat.setNickname(nickname);
     roomChat.setPassword(password);
     roomChat.setSubject(subject);
   }
   requestToWriteRoom(account, room, nickname, password, join);
   if (join) joinRoom(account, room, true);
 }
Exemplo n.º 7
0
 public void removeRoom(final String account, final String room) {
   removeInvite(getInvite(account, room));
   RoomChat roomChat = getRoomChat(account, room);
   if (roomChat == null) return;
   leaveRoom(account, room);
   MessageManager.getInstance().removeChat(roomChat);
   RosterManager.getInstance().onContactChanged(account, room);
   Application.getInstance()
       .runInBackground(
           new Runnable() {
             @Override
             public void run() {
               RoomTable.getInstance().remove(account, room);
             }
           });
 }
 private void clearHistory(String account, String user) {
   MessageManager.getInstance().clearHistory(account, user);
 }
 private void closeChat(String account, String user) {
   MessageManager.getInstance().closeChat(account, user);
   NotificationManager.getInstance().removeMessageNotification(account, user);
   listener.onCloseChat();
 }
 private void showHistory(String account, String user) {
   MessageManager.getInstance().requestToLoadLocalHistory(account, user);
   MessageArchiveManager.getInstance().requestHistory(account, user, MINIMUM_MESSAGES_TO_LOAD, 0);
 }
  @Override
  public boolean onMenuItemClick(MenuItem item) {

    switch (item.getItemId()) {
        /* security menu */

      case R.id.action_start_encryption:
        startEncryption(account, user);
        return true;

      case R.id.action_restart_encryption:
        restartEncryption(account, user);
        return true;

      case R.id.action_stop_encryption:
        stopEncryption(account, user);
        return true;

      case R.id.action_verify_with_fingerprint:
        startActivity(FingerprintViewer.createIntent(getActivity(), account, user));
        return true;

      case R.id.action_verify_with_question:
        startActivity(QuestionViewer.createIntent(getActivity(), account, user, true, false, null));
        return true;

      case R.id.action_verify_with_shared_secret:
        startActivity(
            QuestionViewer.createIntent(getActivity(), account, user, false, false, null));
        return true;

        /* regular chat options menu */

      case R.id.action_view_contact:
        showContactInfo();
        return true;

      case R.id.action_chat_settings:
        startActivity(ChatContactSettings.createIntent(getActivity(), account, user));
        return true;

      case R.id.action_show_history:
        showHistory(account, user);
        return true;

      case R.id.action_authorization_settings:
        startActivity(ConferenceAdd.createIntent(getActivity(), account, user));
        return true;

      case R.id.action_close_chat:
        closeChat(account, user);
        return true;

      case R.id.action_clear_history:
        clearHistory(account, user);
        return true;

      case R.id.action_export_chat:
        onExportChatClick();
        return true;

      case R.id.action_call_attention:
        callAttention();
        return true;

      case R.id.action_block_contact:
        BlockContactDialog.newInstance(account, user)
            .show(getFragmentManager(), BlockContactDialog.class.getName());
        return true;

        /* conference specific options menu */

      case R.id.action_join_conference:
        MUCManager.getInstance().joinRoom(account, user, true);
        return true;

      case R.id.action_invite_to_chat:
        startActivity(ContactList.createRoomInviteIntent(getActivity(), account, user));
        return true;

      case R.id.action_leave_conference:
        leaveConference(account, user);
        return true;

      case R.id.action_list_of_occupants:
        startActivity(OccupantList.createIntent(getActivity(), account, user));
        return true;

        /* message popup menu */

      case R.id.action_message_repeat:
        if (clickedMessageItem.isUploadFileMessage()) {
          uploadFile(clickedMessageItem.getFile().getPath());
        } else {
          sendMessage(clickedMessageItem.getText());
        }
        return true;

      case R.id.action_message_copy:
        ((ClipboardManager) getActivity().getSystemService(Context.CLIPBOARD_SERVICE))
            .setPrimaryClip(
                ClipData.newPlainText(
                    clickedMessageItem.getSpannable(), clickedMessageItem.getSpannable()));
        return true;

      case R.id.action_message_quote:
        setInputText("> " + clickedMessageItem.getText() + "\n");
        return true;

      case R.id.action_message_remove:
        MessageManager.getInstance().removeMessage(clickedMessageItem);
        updateChat();
        return true;

      case R.id.action_message_open_file:
        FileManager.openFile(getActivity(), clickedMessageItem.getFile());
        return true;

      case R.id.action_message_save_file:
        OnSaveFileToDownloadsClick();
        return true;

      case R.id.action_message_open_muc_private_chat:
        String occupantFullJid = user + "/" + clickedMessageItem.getResource();
        MessageManager.getInstance().openChat(account, occupantFullJid);
        startActivity(ChatViewer.createSpecificChatIntent(getActivity(), account, occupantFullJid));
        return true;

      default:
        return false;
    }
  }
 private void sendMessage(String text) {
   MessageManager.getInstance().sendMessage(account, user, text);
   updateChat();
 }
  @Override
  public View onCreateView(
      LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    super.onCreateView(inflater, container, savedInstanceState);

    View view = inflater.inflate(R.layout.chat_viewer_fragment, container, false);

    contactTitleView = view.findViewById(R.id.contact_title);

    abstractContact = RosterManager.getInstance().getBestContact(account, user);
    contactTitleView.findViewById(R.id.avatar).setOnClickListener(this);

    toolbar = (Toolbar) view.findViewById(R.id.toolbar_default);
    toolbar.inflateMenu(R.menu.chat);
    toolbar.setOnMenuItemClickListener(this);
    toolbar.setNavigationIcon(R.drawable.ic_arrow_left_white_24dp);
    toolbar.setNavigationOnClickListener(
        new View.OnClickListener() {
          @Override
          public void onClick(View v) {
            NavUtils.navigateUpFromSameTask(getActivity());
          }
        });

    setHasOptionsMenu(true);

    accountPainter = new AccountPainter(getActivity());

    sendButton = (ImageButton) view.findViewById(R.id.button_send_message);
    sendButton.setImageResource(R.drawable.ic_button_send_inactive_24dp);

    AbstractChat abstractChat = MessageManager.getInstance().getChat(account, user);

    securityButton = (ImageButton) view.findViewById(R.id.button_security);

    if (abstractChat instanceof RegularChat) {
      securityButton.setOnClickListener(
          new View.OnClickListener() {
            @Override
            public void onClick(View v) {
              showSecurityMenu();
            }
          });
    } else {
      securityButton.setVisibility(View.GONE);
    }

    chatMessageAdapter = new ChatMessageAdapter(getActivity(), account, user, this, this);

    recyclerView = (RecyclerView) view.findViewById(R.id.chat_messages_recycler_view);
    recyclerView.setAdapter(chatMessageAdapter);

    layoutManager = new LinearLayoutManager(getActivity());
    layoutManager.setStackFromEnd(true);
    recyclerView.setLayoutManager(layoutManager);

    inputView = (EditText) view.findViewById(R.id.chat_input);

    view.findViewById(R.id.button_send_message)
        .setOnClickListener(
            new View.OnClickListener() {

              @Override
              public void onClick(View v) {
                sendMessage();
              }
            });

    inputView.setOnKeyListener(
        new View.OnKeyListener() {
          @Override
          public boolean onKey(View view, int keyCode, KeyEvent event) {
            if (SettingsManager.chatsSendByEnter()
                && event.getAction() == KeyEvent.ACTION_DOWN
                && keyCode == KeyEvent.KEYCODE_ENTER) {
              sendMessage();
              return true;
            }
            return false;
          }
        });

    inputView.addTextChangedListener(
        new TextWatcher() {

          @Override
          public void onTextChanged(CharSequence s, int start, int before, int count) {
            if (!skipOnTextChanges && stopTypingTimer != null) {
              stopTypingTimer.cancel();
            }
          }

          @Override
          public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

          @Override
          public void afterTextChanged(Editable text) {
            setUpInputViewButtons();

            if (skipOnTextChanges) {
              return;
            }

            ChatStateManager.getInstance().onComposing(account, user, text);

            stopTypingTimer = new Timer();
            stopTypingTimer.schedule(
                new TimerTask() {
                  @Override
                  public void run() {
                    Application.getInstance()
                        .runOnUiThread(
                            new Runnable() {
                              @Override
                              public void run() {
                                ChatStateManager.getInstance().onPaused(account, user);
                              }
                            });
                  }
                },
                STOP_TYPING_DELAY);
          }
        });

    final ImageButton emojiButton = (ImageButton) view.findViewById(R.id.button_emoticon);
    final View rootView = view.findViewById(R.id.root_view);

    // Give the topmost view of your activity layout hierarchy. This will be used to measure soft
    // keyboard height
    final EmojiconsPopup popup = new EmojiconsPopup(rootView, getActivity());

    // Will automatically set size according to the soft keyboard size
    popup.setSizeForSoftKeyboard();

    // If the emoji popup is dismissed, change emojiButton to smiley icon
    popup.setOnDismissListener(
        new PopupWindow.OnDismissListener() {

          @Override
          public void onDismiss() {
            changeEmojiKeyboardIcon(emojiButton, R.drawable.ic_emoticon_grey600_24dp);
          }
        });

    // If the text keyboard closes, also dismiss the emoji popup
    popup.setOnSoftKeyboardOpenCloseListener(
        new EmojiconsPopup.OnSoftKeyboardOpenCloseListener() {

          @Override
          public void onKeyboardOpen(int keyBoardHeight) {}

          @Override
          public void onKeyboardClose() {
            if (popup.isShowing()) popup.dismiss();
          }
        });

    // On emoji clicked, add it to edittext
    popup.setOnEmojiconClickedListener(
        new EmojiconGridView.OnEmojiconClickedListener() {

          @Override
          public void onEmojiconClicked(Emojicon emojicon) {
            if (inputView == null || emojicon == null) {
              return;
            }

            int start = inputView.getSelectionStart();
            int end = inputView.getSelectionEnd();
            if (start < 0) {
              inputView.append(emojicon.getEmoji());
            } else {
              inputView
                  .getText()
                  .replace(
                      Math.min(start, end),
                      Math.max(start, end),
                      emojicon.getEmoji(),
                      0,
                      emojicon.getEmoji().length());
            }
          }
        });

    // On backspace clicked, emulate the KEYCODE_DEL key event
    popup.setOnEmojiconBackspaceClickedListener(
        new EmojiconsPopup.OnEmojiconBackspaceClickedListener() {

          @Override
          public void onEmojiconBackspaceClicked(View v) {
            KeyEvent event =
                new KeyEvent(0, 0, 0, KeyEvent.KEYCODE_DEL, 0, 0, 0, 0, KeyEvent.KEYCODE_ENDCALL);
            inputView.dispatchKeyEvent(event);
          }
        });

    // To toggle between text keyboard and emoji keyboard keyboard(Popup)
    emojiButton.setOnClickListener(
        new View.OnClickListener() {

          @Override
          public void onClick(View v) {

            // If popup is not showing => emoji keyboard is not visible, we need to show it
            if (!popup.isShowing()) {

              // If keyboard is visible, simply show the emoji popup
              if (popup.isKeyBoardOpen()) {
                popup.showAtBottom();
                changeEmojiKeyboardIcon(emojiButton, R.drawable.ic_keyboard_variant_grey600_24dp);
              }

              // else, open the text keyboard first and immediately after that show the emoji popup
              else {
                inputView.setFocusableInTouchMode(true);
                inputView.requestFocus();
                popup.showAtBottomPending();
                final InputMethodManager inputMethodManager =
                    (InputMethodManager)
                        getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
                inputMethodManager.showSoftInput(inputView, InputMethodManager.SHOW_IMPLICIT);
                changeEmojiKeyboardIcon(emojiButton, R.drawable.ic_keyboard_variant_grey600_24dp);
              }
            }

            // If popup is showing, simply dismiss it to show the undelying text keyboard
            else {
              popup.dismiss();
            }
          }
        });

    attachButton = (ImageButton) view.findViewById(R.id.button_attach);
    attachButton.setOnClickListener(
        new View.OnClickListener() {
          @Override
          public void onClick(View v) {
            onAttachButtonPressed();
          }
        });

    return view;
  }
Exemplo n.º 14
0
 /**
  * @param account
  * @param room
  * @return <code>null</code> if does not exists.
  */
 public RoomChat getRoomChat(String account, String room) {
   AbstractChat chat = MessageManager.getInstance().getChat(account, room);
   if (chat != null && chat instanceof RoomChat) return (RoomChat) chat;
   return null;
 }