@Override
  public View getSectionHeaderView(int section, View convertView, ViewGroup parent) {
    if (usersAsSections) {
      if (section < ContactsController.getInstance().sortedUsersSectionsArray.size()) {
        if (convertView == null) {
          convertView = new SettingsSectionLayout(mContext);
          convertView.setBackgroundColor(0xffffffff);
        }
        ((SettingsSectionLayout) convertView)
            .setText(ContactsController.getInstance().sortedUsersSectionsArray.get(section));
        return convertView;
      }
    } else {
      if (section == 0) {
        if (convertView == null) {
          LayoutInflater li =
              (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
          convertView = li.inflate(R.layout.empty_layout, parent, false);
        }
        return convertView;
      }
    }

    if (convertView == null) {
      convertView = new SettingsSectionLayout(mContext);
      convertView.setBackgroundColor(0xffffffff);
    }
    ((SettingsSectionLayout) convertView)
        .setText(ContactsController.getInstance().sortedContactsSectionsArray.get(section - 1));
    return convertView;
  }
 @Override
 public int getItemViewType(int section, int position) {
   if (onlyUsers && !isAdmin) {
     ArrayList<TLRPC.TL_contact> arr =
         ContactsController.getInstance()
             .usersSectionsDict
             .get(ContactsController.getInstance().sortedUsersSectionsArray.get(section));
     return position < arr.size() ? 0 : 4;
   } else {
     if (section == 0) {
       if (needPhonebook || isAdmin) {
         if (position == 1) {
           return 3;
         }
       } else {
         if (position == 3) {
           return 3;
         }
       }
       return 2;
     } else if (section - 1 < ContactsController.getInstance().sortedUsersSectionsArray.size()) {
       ArrayList<TLRPC.TL_contact> arr =
           ContactsController.getInstance()
               .usersSectionsDict
               .get(ContactsController.getInstance().sortedUsersSectionsArray.get(section - 1));
       return position < arr.size() ? 0 : 4;
     }
   }
   return 1;
 }
 @Override
 public boolean isRowEnabled(int section, int row) {
   if (onlyUsers && !isAdmin) {
     ArrayList<TLRPC.TL_contact> arr =
         ContactsController.getInstance()
             .usersSectionsDict
             .get(ContactsController.getInstance().sortedUsersSectionsArray.get(section));
     return row < arr.size();
   } else {
     if (section == 0) {
       if (needPhonebook || isAdmin) {
         if (row == 1) {
           return false;
         }
       } else {
         if (row == 3) {
           return false;
         }
       }
       return true;
     } else if (section - 1 < ContactsController.getInstance().sortedUsersSectionsArray.size()) {
       ArrayList<TLRPC.TL_contact> arr =
           ContactsController.getInstance()
               .usersSectionsDict
               .get(ContactsController.getInstance().sortedUsersSectionsArray.get(section - 1));
       return row < arr.size();
     }
   }
   return true;
 }
 @Override
 public int getCountForSection(int section) {
   if (searching && searchWas) {
     return searchResult == null ? 0 : searchResult.size();
   }
   ArrayList<TLRPC.TL_contact> arr =
       ContactsController.getInstance()
           .usersSectionsDict
           .get(ContactsController.getInstance().sortedUsersSectionsArray.get(section));
   return arr.size();
 }
 @Override
 public int getSectionCount() {
   int count = 0;
   if (usersAsSections) {
     count += ContactsController.getInstance().sortedUsersSectionsArray.size();
   } else {
     count++;
   }
   if (!onlyUsers) {
     count += ContactsController.getInstance().sortedContactsSectionsArray.size();
   }
   return count;
 }
 @Override
 public int getSectionCount() {
   if (searching && searchWas) {
     return searchResult == null || searchResult.isEmpty() ? 0 : 1;
   }
   return ContactsController.getInstance().sortedUsersSectionsArray.size();
 }
  private void updateInterfaceForCurrentMessage(int move) {
    if (actionBar == null) {
      return;
    }
    currentChat = null;
    currentUser = null;
    long dialog_id = currentMessageObject.getDialogId();
    chatActivityEnterView.setDialogId(dialog_id);
    if ((int) dialog_id != 0) {
      int lower_id = (int) dialog_id;
      if (lower_id > 0) {
        currentUser = MessagesController.getInstance().getUser(lower_id);
      } else {
        currentChat = MessagesController.getInstance().getChat(-lower_id);
        currentUser =
            MessagesController.getInstance().getUser(currentMessageObject.messageOwner.from_id);
      }
    } else {
      TLRPC.EncryptedChat encryptedChat =
          MessagesController.getInstance().getEncryptedChat((int) (dialog_id >> 32));
      currentUser = MessagesController.getInstance().getUser(encryptedChat.user_id);
    }

    if (currentChat != null && currentUser != null) {
      nameTextView.setText(currentChat.title);
      onlineTextView.setText(
          ContactsController.formatName(currentUser.first_name, currentUser.last_name));
      nameTextView.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
      nameTextView.setCompoundDrawablePadding(0);
    } else if (currentUser != null) {
      nameTextView.setText(
          ContactsController.formatName(currentUser.first_name, currentUser.last_name));
      if ((int) dialog_id == 0) {
        nameTextView.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_lock_white, 0, 0, 0);
        nameTextView.setCompoundDrawablePadding(AndroidUtilities.dp(4));
      } else {
        nameTextView.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
        nameTextView.setCompoundDrawablePadding(0);
      }
    }

    prepareLayouts(move);
    updateSubtitle();
    checkAndUpdateAvatar();
    applyViewsLayoutParams(0);
  }
 @Override
 public Object getItem(int section, int position) {
   if (onlyUsers && !isAdmin) {
     if (section < ContactsController.getInstance().sortedUsersSectionsArray.size()) {
       ArrayList<TLRPC.TL_contact> arr =
           ContactsController.getInstance()
               .usersSectionsDict
               .get(ContactsController.getInstance().sortedUsersSectionsArray.get(section));
       if (position < arr.size()) {
         return MessagesController.getInstance().getUser(arr.get(position).user_id);
       }
     }
     return null;
   } else {
     if (section == 0) {
       return null;
     } else {
       if (section - 1 < ContactsController.getInstance().sortedUsersSectionsArray.size()) {
         ArrayList<TLRPC.TL_contact> arr =
             ContactsController.getInstance()
                 .usersSectionsDict
                 .get(ContactsController.getInstance().sortedUsersSectionsArray.get(section - 1));
         if (position < arr.size()) {
           return MessagesController.getInstance().getUser(arr.get(position).user_id);
         }
         return null;
       }
     }
   }
   if (needPhonebook) {
     return ContactsController.getInstance().phoneBookContacts.get(position);
   }
   return null;
 }
 @Override
 public int getSectionHeaderViewType(int section) {
   if (usersAsSections) {
     if (section < ContactsController.getInstance().sortedUsersSectionsArray.size()) {
       return 1;
     }
   } else if (section == 0) {
     return 0;
   }
   return 1;
 }
 private void updateSubtitle() {
   if (actionBar == null) {
     return;
   }
   if (currentChat != null || currentUser == null) {
     return;
   }
   if (currentUser.id / 1000 != 777
       && currentUser.id / 1000 != 333
       && ContactsController.getInstance().contactsDict.get(currentUser.id) == null
       && (ContactsController.getInstance().contactsDict.size() != 0
           || !ContactsController.getInstance().isLoadingContacts())) {
     if (currentUser.phone != null && currentUser.phone.length() != 0) {
       nameTextView.setText(PhoneFormat.getInstance().format("+" + currentUser.phone));
     } else {
       nameTextView.setText(
           ContactsController.formatName(currentUser.first_name, currentUser.last_name));
     }
   } else {
     nameTextView.setText(
         ContactsController.formatName(currentUser.first_name, currentUser.last_name));
   }
   CharSequence printString =
       MessagesController.getInstance().printingStrings.get(currentMessageObject.getDialogId());
   if (printString == null || printString.length() == 0) {
     lastPrintString = null;
     setTypingAnimation(false);
     TLRPC.User user = MessagesController.getInstance().getUser(currentUser.id);
     if (user != null) {
       currentUser = user;
     }
     onlineTextView.setText(LocaleController.formatUserStatus(currentUser));
   } else {
     lastPrintString = printString;
     onlineTextView.setText(printString);
     setTypingAnimation(true);
   }
 }
 @Override
 public int getSectionCount() {
   int count = ContactsController.getInstance().sortedUsersSectionsArray.size();
   if (!onlyUsers) {
     count++;
   }
   if (isAdmin) {
     count++;
   }
   if (needPhonebook) {
     count++;
   }
   return count;
 }
 @Override
 public View getSectionHeaderView(int section, View convertView, ViewGroup parent) {
   if (convertView == null) {
     convertView = new SettingsSectionLayout(mContext);
     convertView.setBackgroundColor(0xffffffff);
   }
   if (searching && searchWas) {
     ((SettingsSectionLayout) convertView)
         .setText(LocaleController.getString("AllContacts", R.string.AllContacts));
   } else {
     ((SettingsSectionLayout) convertView)
         .setText(ContactsController.getInstance().sortedUsersSectionsArray.get(section));
   }
   return convertView;
 }
 @Override
 public View getSectionHeaderView(int section, View convertView, ViewGroup parent) {
   if (convertView == null) {
     convertView = new LetterSectionCell(mContext);
   }
   if (onlyUsers && !isAdmin) {
     if (section < ContactsController.getInstance().sortedUsersSectionsArray.size()) {
       ((LetterSectionCell) convertView)
           .setLetter(ContactsController.getInstance().sortedUsersSectionsArray.get(section));
     } else {
       ((LetterSectionCell) convertView).setLetter("");
     }
   } else {
     if (section == 0) {
       ((LetterSectionCell) convertView).setLetter("");
     } else if (section - 1 < ContactsController.getInstance().sortedUsersSectionsArray.size()) {
       ((LetterSectionCell) convertView)
           .setLetter(ContactsController.getInstance().sortedUsersSectionsArray.get(section - 1));
     } else {
       ((LetterSectionCell) convertView).setLetter("");
     }
   }
   return convertView;
 }
 @Override
 public int getCountForSection(int section) {
   if (onlyUsers && !isAdmin) {
     if (section < ContactsController.getInstance().sortedUsersSectionsArray.size()) {
       ArrayList<TLRPC.TL_contact> arr =
           ContactsController.getInstance()
               .usersSectionsDict
               .get(ContactsController.getInstance().sortedUsersSectionsArray.get(section));
       int count = arr.size();
       if (section != (ContactsController.getInstance().sortedUsersSectionsArray.size() - 1)
           || needPhonebook) {
         count++;
       }
       return count;
     }
   } else {
     if (section == 0) {
       if (needPhonebook || isAdmin) {
         return 2;
       } else {
         return 4;
       }
     } else if (section - 1 < ContactsController.getInstance().sortedUsersSectionsArray.size()) {
       ArrayList<TLRPC.TL_contact> arr =
           ContactsController.getInstance()
               .usersSectionsDict
               .get(ContactsController.getInstance().sortedUsersSectionsArray.get(section - 1));
       int count = arr.size();
       if (section - 1 != (ContactsController.getInstance().sortedUsersSectionsArray.size() - 1)
           || needPhonebook) {
         count++;
       }
       return count;
     }
   }
   if (needPhonebook) {
     return ContactsController.getInstance().phoneBookContacts.size();
   }
   return 0;
 }
  public XImageSpan createAndPutChipForUser(TLRPC.User user) {
    LayoutInflater lf =
        (LayoutInflater)
            ApplicationLoader.applicationContext.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    View textView = lf.inflate(R.layout.group_create_bubble, null);
    TextView text = (TextView) textView.findViewById(R.id.bubble_text_view);
    String name = ContactsController.formatName(user.first_name, user.last_name);
    if (name.length() == 0 && user.phone != null && user.phone.length() != 0) {
      name = PhoneFormat.getInstance().format("+" + user.phone);
    }
    text.setText(name + ", ");

    int spec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
    textView.measure(spec, spec);
    textView.layout(0, 0, textView.getMeasuredWidth(), textView.getMeasuredHeight());
    Bitmap b =
        Bitmap.createBitmap(textView.getWidth(), textView.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(b);
    canvas.translate(-textView.getScrollX(), -textView.getScrollY());
    textView.draw(canvas);
    textView.setDrawingCacheEnabled(true);
    Bitmap cacheBmp = textView.getDrawingCache();
    Bitmap viewBmp = cacheBmp.copy(Bitmap.Config.ARGB_8888, true);
    textView.destroyDrawingCache();

    final BitmapDrawable bmpDrawable = new BitmapDrawable(b);
    bmpDrawable.setBounds(0, 0, b.getWidth(), b.getHeight());

    SpannableStringBuilder ssb = new SpannableStringBuilder("");
    XImageSpan span = new XImageSpan(bmpDrawable, ImageSpan.ALIGN_BASELINE);
    allSpans.add(span);
    selectedContacts.put(user.id, span);
    for (ImageSpan sp : allSpans) {
      ssb.append("<<");
      ssb.setSpan(
          sp, ssb.length() - 2, ssb.length(), SpannableStringBuilder.SPAN_EXCLUSIVE_EXCLUSIVE);
    }
    userSelectEditText.setText(ssb);
    userSelectEditText.setSelection(ssb.length());
    return span;
  }
 @Override
 public int getCountForSection(int section) {
   if (usersAsSections) {
     if (section < ContactsController.getInstance().sortedUsersSectionsArray.size()) {
       ArrayList<TLRPC.TL_contact> arr =
           ContactsController.getInstance()
               .usersSectionsDict
               .get(ContactsController.getInstance().sortedUsersSectionsArray.get(section));
       return arr.size();
     }
   } else {
     if (section == 0) {
       return ContactsController.getInstance().contacts.size() + 1;
     }
   }
   ArrayList<ContactsController.Contact> arr =
       ContactsController.getInstance()
           .contactsSectionsDict
           .get(ContactsController.getInstance().sortedContactsSectionsArray.get(section - 1));
   return arr.size();
 }
    @Override
    public View getItemView(int section, int position, View convertView, ViewGroup parent) {
      TLRPC.User user;
      int size;

      if (searchWas && searching) {
        user = MessagesController.getInstance().getUser(searchResult.get(position).id);
        size = searchResult.size();
      } else {
        ArrayList<TLRPC.TL_contact> arr =
            ContactsController.getInstance()
                .usersSectionsDict
                .get(ContactsController.getInstance().sortedUsersSectionsArray.get(section));
        user = MessagesController.getInstance().getUser(arr.get(position).user_id);
        size = arr.size();
      }

      if (convertView == null) {
        LayoutInflater li =
            (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = li.inflate(R.layout.group_create_row_layout, parent, false);
      }
      ContactListRowHolder holder = (ContactListRowHolder) convertView.getTag();
      if (holder == null) {
        holder = new ContactListRowHolder(convertView);
        convertView.setTag(holder);
      }

      ImageView checkButton = (ImageView) convertView.findViewById(R.id.settings_row_check_button);
      if (selectedContacts.containsKey(user.id)) {
        checkButton.setImageResource(R.drawable.btn_check_on_holo_light);
      } else {
        checkButton.setImageResource(R.drawable.btn_check_off_holo_light);
      }

      View divider = convertView.findViewById(R.id.settings_row_divider);
      if (position == size - 1) {
        divider.setVisibility(View.INVISIBLE);
      } else {
        divider.setVisibility(View.VISIBLE);
      }

      if (searchWas && searching) {
        holder.nameTextView.setText(searchResultNames.get(position));
      } else {
        String name = ContactsController.formatName(user.first_name, user.last_name);
        if (name.length() == 0) {
          if (user.phone != null && user.phone.length() != 0) {
            name = PhoneFormat.getInstance().format("+" + user.phone);
          } else {
            name = LocaleController.getString("HiddenName", R.string.HiddenName);
          }
        }
        holder.nameTextView.setText(name);
      }

      TLRPC.FileLocation photo = null;
      if (user.photo != null) {
        photo = user.photo.photo_small;
      }
      int placeHolderId = AndroidUtilities.getUserAvatarForId(user.id);
      holder.avatarImage.setImage(photo, "50_50", placeHolderId);

      holder.messageTextView.setText(LocaleController.formatUserStatus(user));
      if (user.status != null
          && user.status.expires > ConnectionsManager.getInstance().getCurrentTime()) {
        holder.messageTextView.setTextColor(0xff357aa8);
      } else {
        holder.messageTextView.setTextColor(0xff808080);
      }

      return convertView;
    }
  @Override
  public View getItemView(int section, int position, View convertView, ViewGroup parent) {

    TLRPC.User user = null;
    int count = 0;
    if (usersAsSections) {
      if (section < ContactsController.getInstance().sortedUsersSectionsArray.size()) {
        ArrayList<TLRPC.TL_contact> arr =
            ContactsController.getInstance()
                .usersSectionsDict
                .get(ContactsController.getInstance().sortedUsersSectionsArray.get(section));
        user = MessagesController.getInstance().getUser(arr.get(position).user_id);
        count = arr.size();
      }
    } else {
      if (section == 0) {
        if (position == 0) {
          if (convertView == null) {
            LayoutInflater li =
                (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = li.inflate(R.layout.contacts_invite_row_layout, parent, false);
            TextView textView = (TextView) convertView.findViewById(R.id.messages_list_row_name);
            textView.setText(LocaleController.getString("InviteFriends", R.string.InviteFriends));
          }
          View divider = convertView.findViewById(R.id.settings_row_divider);
          if (ContactsController.getInstance().contacts.isEmpty()) {
            divider.setVisibility(View.INVISIBLE);
          } else {
            divider.setVisibility(View.VISIBLE);
          }
          return convertView;
        }
        user =
            MessagesController.getInstance()
                .getUser(ContactsController.getInstance().contacts.get(position - 1).user_id);
        count = ContactsController.getInstance().contacts.size();
      }
    }
    if (user != null) {
      if (convertView == null) {
        convertView = new ChatOrUserCell(mContext);
        ((ChatOrUserCell) convertView).usePadding = false;
      }

      ((ChatOrUserCell) convertView).setData(user, null, null, null, null);

      if (ignoreUsers != null) {
        if (ignoreUsers.containsKey(user.id)) {
          ((ChatOrUserCell) convertView).drawAlpha = 0.5f;
        } else {
          ((ChatOrUserCell) convertView).drawAlpha = 1.0f;
        }
      }

      ((ChatOrUserCell) convertView).useSeparator = position != count - 1;

      return convertView;
    }

    TextView textView;
    if (convertView == null) {
      LayoutInflater li =
          (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
      convertView = li.inflate(R.layout.settings_row_button_layout, parent, false);
      textView = (TextView) convertView.findViewById(R.id.settings_row_text);
    } else {
      textView = (TextView) convertView.findViewById(R.id.settings_row_text);
    }

    View divider = convertView.findViewById(R.id.settings_row_divider);
    ArrayList<ContactsController.Contact> arr =
        ContactsController.getInstance()
            .contactsSectionsDict
            .get(ContactsController.getInstance().sortedContactsSectionsArray.get(section - 1));
    ContactsController.Contact contact = arr.get(position);
    if (divider != null) {
      if (position == arr.size() - 1) {
        divider.setVisibility(View.INVISIBLE);
      } else {
        divider.setVisibility(View.VISIBLE);
      }
    }
    if (contact.first_name != null && contact.last_name != null) {
      textView.setText(contact.first_name + " " + contact.last_name);
    } else if (contact.first_name != null && contact.last_name == null) {
      textView.setText(contact.first_name);
    } else {
      textView.setText(contact.last_name);
    }
    return convertView;
  }
  @Override
  public View getItemView(int section, int position, View convertView, ViewGroup parent) {
    int type = getItemViewType(section, position);
    if (type == 4) {
      if (convertView == null) {
        convertView = new DividerCell(mContext);
        convertView.setPadding(
            AndroidUtilities.dp(LocaleController.isRTL ? 28 : 72),
            0,
            AndroidUtilities.dp(LocaleController.isRTL ? 72 : 28),
            0);
      }
    } else if (type == 3) {
      if (convertView == null) {
        convertView = new GreySectionCell(mContext);
        ((GreySectionCell) convertView)
            .setText(LocaleController.getString("Contacts", R.string.Contacts).toUpperCase());
      }
    } else if (type == 2) {
      if (convertView == null) {
        convertView = new TextCell(mContext);
      }
      TextCell actionCell = (TextCell) convertView;
      if (needPhonebook) {
        actionCell.setTextAndIcon(
            LocaleController.getString("InviteFriends", R.string.InviteFriends),
            R.drawable.menu_invite);
      } else if (isAdmin) {
        actionCell.setTextAndIcon(
            LocaleController.getString("InviteToGroupByLink", R.string.InviteToGroupByLink),
            R.drawable.menu_invite);
      } else {
        if (position == 0) {
          actionCell.setTextAndIcon(
              LocaleController.getString("NewGroup", R.string.NewGroup), R.drawable.menu_newgroup);
        } else if (position == 1) {
          actionCell.setTextAndIcon(
              LocaleController.getString("NewSecretChat", R.string.NewSecretChat),
              R.drawable.menu_secret);
        } else if (position == 2) {
          actionCell.setTextAndIcon(
              LocaleController.getString("NewBroadcastList", R.string.NewBroadcastList),
              R.drawable.menu_broadcast);
        }
      }
    } else if (type == 1) {
      if (convertView == null) {
        convertView = new TextCell(mContext);
      }
      ContactsController.Contact contact =
          ContactsController.getInstance().phoneBookContacts.get(position);
      if (contact.first_name != null && contact.last_name != null) {
        if (isContainUTF8(contact.display_name)) {
          ((TextCell) convertView).setText(contact.display_name);
        } else {
          ((TextCell) convertView).setText(contact.first_name + " " + contact.last_name);
        }
      } else if (contact.first_name != null && contact.last_name == null) {
        ((TextCell) convertView).setText(contact.first_name);
      } else {
        ((TextCell) convertView).setText(contact.last_name);
      }
    } else if (type == 0) {
      if (convertView == null) {
        convertView = new UserCell(mContext, 58);
        ((UserCell) convertView).setStatusColors(0xffa8a8a8, 0xff3b84c0);
      }

      ArrayList<TLRPC.TL_contact> arr =
          ContactsController.getInstance()
              .usersSectionsDict
              .get(
                  ContactsController.getInstance()
                      .sortedUsersSectionsArray
                      .get(section - (onlyUsers && !isAdmin ? 0 : 1)));
      TLRPC.User user = MessagesController.getInstance().getUser(arr.get(position).user_id);
      if (isContainUTF8(user.first_name) || isContainUTF8(user.last_name)) {
        ((UserCell) convertView).setData(user, user.last_name + user.first_name, null, 0);
      } else {
        ((UserCell) convertView).setData(user, null, null, 0);
      }

      if (checkedMap != null) {
        ((UserCell) convertView)
            .setChecked(checkedMap.containsKey(user.id), !scrolling && Build.VERSION.SDK_INT > 10);
      }
      if (ignoreUsers != null) {
        if (ignoreUsers.containsKey(user.id)) {
          ViewProxy.setAlpha(convertView, 0.5f);
        } else {
          ViewProxy.setAlpha(convertView, 1.0f);
        }
      }
    }
    return convertView;
  }