@Override
 protected boolean onPositiveClick() {
   String newName = nameView.getText().toString();
   if ("".equals(newName)) {
     Toast.makeText(getActivity(), getString(R.string.group_is_empty), Toast.LENGTH_LONG).show();
     return false;
   }
   try {
     if (account == null) RosterManager.getInstance().renameGroup(group, newName);
     else RosterManager.getInstance().renameGroup(account, group, newName);
   } catch (NetworkException e) {
     Application.getInstance().onError(e);
   }
   return true;
 }
Exemplo n.º 2
0
  public CommonState getCommonState() {
    boolean disabled = false;
    boolean offline = false;
    boolean waiting = false;
    boolean connecting = false;
    boolean roster = false;
    boolean online = false;

    for (AccountItem accountItem : accountItems.values()) {
      ConnectionState state = accountItem.getState();
      if (state == ConnectionState.connected) online = true;
      if (RosterManager.getInstance().isRosterReceived(accountItem.getAccount())) roster = true;
      if (state == ConnectionState.connecting || state == ConnectionState.authentication)
        connecting = true;
      if (state == ConnectionState.waiting) waiting = true;
      if (accountItem.isEnabled()) offline = true;
      disabled = true;
    }
    if (online) return CommonState.online;
    else if (roster) return CommonState.roster;
    else if (connecting) return CommonState.connecting;
    if (waiting) return CommonState.waiting;
    else if (offline) return CommonState.offline;
    else if (disabled) return CommonState.disabled;
    else return CommonState.empty;
  }
Exemplo n.º 3
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.º 5
0
 public void leaveRoom(String account, String room) {
   final MultiUserChat multiUserChat;
   RoomChat roomChat = getRoomChat(account, room);
   if (roomChat == null) return;
   multiUserChat = roomChat.getMultiUserChat();
   roomChat.setState(RoomState.unavailable);
   roomChat.setRequested(false);
   roomChat.newAction(roomChat.getNickname(), null, ChatAction.leave);
   requestToWriteRoom(account, room, roomChat.getNickname(), roomChat.getPassword(), false);
   if (multiUserChat != null) {
     Thread thread =
         new Thread("Leave to room " + room + " from " + account) {
           @Override
           public void run() {
             try {
               multiUserChat.leave();
             } catch (IllegalStateException e) {
               // Do nothing
             }
           }
         };
     thread.setDaemon(true);
     thread.start();
   }
   RosterManager.getInstance().onContactChanged(account, room);
 }
Exemplo n.º 6
0
 @Override
 public View getView(int position, View convertView, ViewGroup parent) {
   final AbstractChat abstractChat = (AbstractChat) getItem(position);
   final AbstractContact abstractContact =
       RosterManager.getInstance()
           .getBestContact(abstractChat.getAccount(), abstractChat.getUser());
   return contactItemInflater.setUpContactView(convertView, parent, abstractContact);
 }
Exemplo n.º 7
0
  @Override
  protected void onInflate(Bundle savedInstanceState) {
    setContentView(R.layout.contact_add);

    ListView listView = getListView();
    LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
    View view = inflater.inflate(R.layout.contact_add_header, listView, false);
    listView.addHeaderView(view, null, false);

    accountView = (Spinner) view.findViewById(R.id.contact_account);
    accountView.setAdapter(new AccountChooseAdapter(this));
    accountView.setOnItemSelectedListener(this);
    userView = (EditText) view.findViewById(R.id.contact_user);
    nameView = (EditText) view.findViewById(R.id.contact_name);
    ((Button) view.findViewById(R.id.ok)).setOnClickListener(this);

    String name;
    Intent intent = getIntent();
    if (savedInstanceState != null) {
      account = savedInstanceState.getString(SAVED_ACCOUNT);
      user = savedInstanceState.getString(SAVED_USER);
      name = savedInstanceState.getString(SAVED_NAME);
    } else {
      account = getAccount(intent);
      user = getUser(intent);
      if (account == null || user == null) name = null;
      else {
        name = RosterManager.getInstance().getName(account, user);
        if (user.equals(name)) name = null;
      }
    }
    if (account == null) {
      Collection<String> accounts = AccountManager.getInstance().getAccounts();
      if (accounts.size() == 1) account = accounts.iterator().next();
    }
    if (account != null) {
      for (int position = 0; position < accountView.getCount(); position++)
        if (account.equals(accountView.getItemAtPosition(position))) {
          accountView.setSelection(position);
          break;
        }
    }
    if (user != null) userView.setText(user);
    if (name != null) nameView.setText(name);
    if (ACTION_SUBSCRIPTION_REQUEST.equals(intent.getAction())) {
      subscriptionRequest = PresenceManager.getInstance().getSubscriptionRequest(account, user);
      if (subscriptionRequest == null) {
        Application.getInstance().onError(R.string.ENTRY_IS_NOT_FOUND);
        finish();
        return;
      }
    } else {
      subscriptionRequest = null;
    }
  }
Exemplo n.º 8
0
 @Override
 public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
   String account = (String) accountView.getSelectedItem();
   if (account == null) {
     onNothingSelected(parent);
   } else {
     HashSet<String> groups = new HashSet<String>(RosterManager.getInstance().getGroups(account));
     groups.addAll(getSelected());
     setGroups(groups, getSelected());
   }
 }
Exemplo n.º 9
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);
             }
           });
 }
Exemplo n.º 10
0
 private void update() {
   isUpdating = true;
   AbstractContact abstractContact = RosterManager.getInstance().getBestContact(account, user);
   ContactTitleInflater.updateTitle(findViewById(R.id.title), this, abstractContact);
   verifiedView.setChecked(OTRManager.getInstance().isVerified(account, user));
   scanView.setEnabled(remoteFingerprint != null);
   verifiedView.setEnabled(remoteFingerprint != null);
   ((TextView) findViewById(R.id.otr_remote_fingerprint))
       .setText(
           remoteFingerprint == null
               ? getString(R.string.unknown)
               : CertificateManager.showFingerprint(remoteFingerprint));
   showView.setEnabled(localFingerprint != null);
   copyView.setEnabled(localFingerprint != null);
   ((TextView) findViewById(R.id.otr_local_fingerprint))
       .setText(
           localFingerprint == null
               ? getString(R.string.unknown)
               : CertificateManager.showFingerprint(localFingerprint));
   isUpdating = false;
 }
  @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.º 12
0
 @Override
 Collection<String> getInitialGroups() {
   String account = (String) accountView.getSelectedItem();
   if (account == null) return Collections.emptyList();
   return RosterManager.getInstance().getGroups(account);
 }
Exemplo n.º 13
0
 @Override
 public String getText() {
   return RosterManager.getInstance().getName(account, user);
 }