@Override
  public View createView(Context context, LayoutInflater inflater) {
    searching = false;
    searchWas = false;

    actionBar.setBackButtonImage(R.drawable.ic_ab_back);
    actionBar.setAllowOverlayTitle(true);
    actionBar.setTitle(LocaleController.getString("Language", R.string.Language));

    actionBar.setActionBarMenuOnItemClick(
        new ActionBar.ActionBarMenuOnItemClick() {
          @Override
          public void onItemClick(int id) {
            if (id == -1) {
              finishFragment();
            }
          }
        });

    ActionBarMenu menu = actionBar.createMenu();
    ActionBarMenuItem item =
        menu.addItem(0, R.drawable.ic_ab_search)
            .setIsSearchField(true)
            .setActionBarMenuItemSearchListener(
                new ActionBarMenuItem.ActionBarMenuItemSearchListener() {
                  @Override
                  public void onSearchExpand() {
                    searching = true;
                  }

                  @Override
                  public void onSearchCollapse() {
                    search(null);
                    searching = false;
                    searchWas = false;
                    if (listView != null) {
                      emptyTextView.setVisibility(View.GONE);
                      listView.setAdapter(listAdapter);
                    }
                  }

                  @Override
                  public void onTextChanged(EditText editText) {
                    String text = editText.getText().toString();
                    search(text);
                    if (text.length() != 0) {
                      searchWas = true;
                      if (listView != null) {
                        listView.setAdapter(searchListViewAdapter);
                      }
                    }
                  }
                });
    item.getSearchField().setHint(LocaleController.getString("Search", R.string.Search));

    listAdapter = new ListAdapter(context);
    searchListViewAdapter = new SearchAdapter(context);

    fragmentView = new FrameLayout(context);

    LinearLayout emptyTextLayout = new LinearLayout(context);
    emptyTextLayout.setVisibility(View.INVISIBLE);
    emptyTextLayout.setOrientation(LinearLayout.VERTICAL);
    ((FrameLayout) fragmentView).addView(emptyTextLayout);
    FrameLayout.LayoutParams layoutParams =
        (FrameLayout.LayoutParams) emptyTextLayout.getLayoutParams();
    layoutParams.width = LayoutHelper.MATCH_PARENT;
    layoutParams.height = LayoutHelper.MATCH_PARENT;
    layoutParams.gravity = Gravity.TOP;
    emptyTextLayout.setLayoutParams(layoutParams);
    emptyTextLayout.setOnTouchListener(
        new View.OnTouchListener() {
          @Override
          public boolean onTouch(View v, MotionEvent event) {
            return true;
          }
        });

    emptyTextView = new TextView(context);
    emptyTextView.setTextColor(0xff808080);
    emptyTextView.setTextSize(20);
    emptyTextView.setGravity(Gravity.CENTER);
    emptyTextView.setText(LocaleController.getString("NoResult", R.string.NoResult));
    emptyTextLayout.addView(emptyTextView);
    LinearLayout.LayoutParams layoutParams1 =
        (LinearLayout.LayoutParams) emptyTextView.getLayoutParams();
    layoutParams1.width = LayoutHelper.MATCH_PARENT;
    layoutParams1.height = LayoutHelper.MATCH_PARENT;
    layoutParams1.weight = 0.5f;
    emptyTextView.setLayoutParams(layoutParams1);

    FrameLayout frameLayout = new FrameLayout(context);
    emptyTextLayout.addView(frameLayout);
    layoutParams1 = (LinearLayout.LayoutParams) frameLayout.getLayoutParams();
    layoutParams1.width = LayoutHelper.MATCH_PARENT;
    layoutParams1.height = LayoutHelper.MATCH_PARENT;
    layoutParams1.weight = 0.5f;
    frameLayout.setLayoutParams(layoutParams1);

    listView = new ListView(context);
    listView.setEmptyView(emptyTextLayout);
    listView.setVerticalScrollBarEnabled(false);
    listView.setDivider(null);
    listView.setDividerHeight(0);
    listView.setAdapter(listAdapter);
    ((FrameLayout) fragmentView).addView(listView);
    layoutParams = (FrameLayout.LayoutParams) listView.getLayoutParams();
    layoutParams.width = LayoutHelper.MATCH_PARENT;
    layoutParams.height = LayoutHelper.MATCH_PARENT;
    listView.setLayoutParams(layoutParams);

    listView.setOnItemClickListener(
        new AdapterView.OnItemClickListener() {
          @Override
          public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            LocaleController.LocaleInfo localeInfo = null;
            if (searching && searchWas) {
              if (i >= 0 && i < searchResult.size()) {
                localeInfo = searchResult.get(i);
              }
            } else {
              if (i >= 0 && i < LocaleController.getInstance().sortedLanguages.size()) {
                localeInfo = LocaleController.getInstance().sortedLanguages.get(i);
              }
            }
            if (localeInfo != null) {
              LocaleController.getInstance().applyLanguage(localeInfo, true);
              parentLayout.rebuildAllFragmentViews(false);
            }
            finishFragment();
          }
        });

    listView.setOnItemLongClickListener(
        new AdapterView.OnItemLongClickListener() {
          @Override
          public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
            LocaleController.LocaleInfo localeInfo = null;
            if (searching && searchWas) {
              if (i >= 0 && i < searchResult.size()) {
                localeInfo = searchResult.get(i);
              }
            } else {
              if (i >= 0 && i < LocaleController.getInstance().sortedLanguages.size()) {
                localeInfo = LocaleController.getInstance().sortedLanguages.get(i);
              }
            }
            if (localeInfo == null
                || localeInfo.pathToFile == null
                || getParentActivity() == null) {
              return false;
            }
            final LocaleController.LocaleInfo finalLocaleInfo = localeInfo;
            AlertDialog.Builder builder = new AlertDialog.Builder(getParentActivity());
            builder.setMessage(
                LocaleController.getString("DeleteLocalization", R.string.DeleteLocalization));
            builder.setTitle(LocaleController.getString("AppName", R.string.AppName));
            builder.setPositiveButton(
                LocaleController.getString("Delete", R.string.Delete),
                new DialogInterface.OnClickListener() {
                  @Override
                  public void onClick(DialogInterface dialogInterface, int i) {
                    if (LocaleController.getInstance().deleteLanguage(finalLocaleInfo)) {
                      if (searchResult != null) {
                        searchResult.remove(finalLocaleInfo);
                      }
                      if (listAdapter != null) {
                        listAdapter.notifyDataSetChanged();
                      }
                      if (searchListViewAdapter != null) {
                        searchListViewAdapter.notifyDataSetChanged();
                      }
                    }
                  }
                });
            builder.setNegativeButton(LocaleController.getString("Cancel", R.string.Cancel), null);
            showDialog(builder.create());
            return true;
          }
        });

    listView.setOnScrollListener(
        new AbsListView.OnScrollListener() {
          @Override
          public void onScrollStateChanged(AbsListView absListView, int i) {
            if (i == SCROLL_STATE_TOUCH_SCROLL && searching && searchWas) {
              AndroidUtilities.hideKeyboard(getParentActivity().getCurrentFocus());
            }
          }

          @Override
          public void onScroll(
              AbsListView absListView,
              int firstVisibleItem,
              int visibleItemCount,
              int totalItemCount) {}
        });

    return fragmentView;
  }
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    classGuid = ConnectionsManager.getInstance().generateClassGuid();
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.appDidLogout);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.pushMessagesUpdated);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.updateInterfaces);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.audioProgressDidChanged);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.audioDidReset);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.contactsDidLoaded);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.emojiDidLoaded);

    typingDotsDrawable = new TypingDotsDrawable();
    recordStatusDrawable = new RecordStatusDrawable();

    SizeNotifierFrameLayout contentView =
        new SizeNotifierFrameLayout(this) {
          @Override
          protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            int widthMode = MeasureSpec.getMode(widthMeasureSpec);
            int heightMode = MeasureSpec.getMode(heightMeasureSpec);
            int widthSize = MeasureSpec.getSize(widthMeasureSpec);
            int heightSize = MeasureSpec.getSize(heightMeasureSpec);

            setMeasuredDimension(widthSize, heightSize);

            int keyboardSize = getKeyboardHeight();

            if (keyboardSize <= AndroidUtilities.dp(20)) {
              heightSize -= chatActivityEnterView.getEmojiPadding();
            }

            int childCount = getChildCount();
            for (int i = 0; i < childCount; i++) {
              View child = getChildAt(i);
              if (child.getVisibility() == GONE) {
                continue;
              }
              if (chatActivityEnterView.isPopupView(child)) {
                child.measure(
                    MeasureSpec.makeMeasureSpec(widthSize, MeasureSpec.EXACTLY),
                    MeasureSpec.makeMeasureSpec(
                        child.getLayoutParams().height, MeasureSpec.EXACTLY));
              } else {
                child.measure(
                    MeasureSpec.makeMeasureSpec(widthSize, MeasureSpec.EXACTLY),
                    MeasureSpec.makeMeasureSpec(
                        Math.max(AndroidUtilities.dp(10), heightSize + AndroidUtilities.dp(2)),
                        MeasureSpec.EXACTLY));
              }
            }
          }

          @Override
          protected void onLayout(boolean changed, int l, int t, int r, int b) {
            final int count = getChildCount();

            int paddingBottom =
                getKeyboardHeight() <= AndroidUtilities.dp(20)
                    ? chatActivityEnterView.getEmojiPadding()
                    : 0;

            for (int i = 0; i < count; i++) {
              final View child = getChildAt(i);
              if (child.getVisibility() == GONE) {
                continue;
              }
              final LayoutParams lp = (LayoutParams) child.getLayoutParams();

              int width = child.getMeasuredWidth();
              int height = child.getMeasuredHeight();

              int childLeft;
              int childTop;

              int gravity = lp.gravity;
              if (gravity == -1) {
                gravity = Gravity.TOP | Gravity.LEFT;
              }

              final int absoluteGravity = gravity & Gravity.HORIZONTAL_GRAVITY_MASK;
              final int verticalGravity = gravity & Gravity.VERTICAL_GRAVITY_MASK;

              switch (absoluteGravity & Gravity.HORIZONTAL_GRAVITY_MASK) {
                case Gravity.CENTER_HORIZONTAL:
                  childLeft = (r - l - width) / 2 + lp.leftMargin - lp.rightMargin;
                  break;
                case Gravity.RIGHT:
                  childLeft = r - width - lp.rightMargin;
                  break;
                case Gravity.LEFT:
                default:
                  childLeft = lp.leftMargin;
              }

              switch (verticalGravity) {
                case Gravity.TOP:
                  childTop = lp.topMargin;
                  break;
                case Gravity.CENTER_VERTICAL:
                  childTop =
                      ((b - paddingBottom) - t - height) / 2 + lp.topMargin - lp.bottomMargin;
                  break;
                case Gravity.BOTTOM:
                  childTop = ((b - paddingBottom) - t) - height - lp.bottomMargin;
                  break;
                default:
                  childTop = lp.topMargin;
              }
              if (chatActivityEnterView.isPopupView(child)) {
                childTop =
                    paddingBottom != 0 ? getMeasuredHeight() - paddingBottom : getMeasuredHeight();
              }
              child.layout(childLeft, childTop, childLeft + width, childTop + height);
            }

            notifyHeightChanged();
          }
        };
    setContentView(contentView);
    contentView.setBackgroundColor(0x99000000);

    RelativeLayout relativeLayout = new RelativeLayout(this);
    contentView.addView(
        relativeLayout,
        LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, LayoutHelper.MATCH_PARENT));

    RelativeLayout popupContainer = new RelativeLayout(this);
    popupContainer.setBackgroundColor(0xffffffff);
    relativeLayout.addView(
        popupContainer,
        LayoutHelper.createRelative(
            LayoutHelper.MATCH_PARENT, 240, 12, 0, 12, 0, RelativeLayout.CENTER_IN_PARENT));

    if (chatActivityEnterView != null) {
      chatActivityEnterView.onDestroy();
    }
    chatActivityEnterView = new ChatActivityEnterView(this, contentView, null, false);
    popupContainer.addView(
        chatActivityEnterView,
        LayoutHelper.createRelative(
            LayoutHelper.MATCH_PARENT,
            LayoutHelper.WRAP_CONTENT,
            RelativeLayout.ALIGN_PARENT_BOTTOM));
    chatActivityEnterView.setDelegate(
        new ChatActivityEnterView.ChatActivityEnterViewDelegate() {
          @Override
          public void onMessageSend(String message) {
            if (currentMessageObject == null) {
              return;
            }
            if (currentMessageNum >= 0
                && currentMessageNum < NotificationsController.getInstance().popupMessages.size()) {
              NotificationsController.getInstance().popupMessages.remove(currentMessageNum);
            }
            MessagesController.getInstance()
                .markDialogAsRead(
                    currentMessageObject.getDialogId(),
                    currentMessageObject.getId(),
                    Math.max(0, currentMessageObject.getId()),
                    0,
                    currentMessageObject.messageOwner.date,
                    true,
                    true);
            currentMessageObject = null;
            getNewMessage();
          }

          @Override
          public void onTextChanged(CharSequence text, boolean big) {}

          @Override
          public void needSendTyping() {
            if (currentMessageObject != null) {
              MessagesController.getInstance()
                  .sendTyping(currentMessageObject.getDialogId(), 0, classGuid);
            }
          }

          @Override
          public void onAttachButtonHidden() {}

          @Override
          public void onAttachButtonShow() {}

          @Override
          public void onWindowSizeChanged(int size) {}
        });

    messageContainer = new FrameLayoutTouch(this);
    popupContainer.addView(messageContainer, 0);

    actionBar = new ActionBar(this);
    actionBar.setOccupyStatusBar(false);
    actionBar.setBackButtonImage(R.drawable.ic_ab_back);
    actionBar.setBackgroundColor(0xff54759e);
    actionBar.setItemsBackground(R.drawable.bar_selector);
    popupContainer.addView(actionBar);
    ViewGroup.LayoutParams layoutParams = actionBar.getLayoutParams();
    layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
    actionBar.setLayoutParams(layoutParams);

    ActionBarMenu menu = actionBar.createMenu();
    View view = menu.addItemResource(2, R.layout.popup_count_layout);
    countText = (TextView) view.findViewById(R.id.count_text);

    avatarContainer = new FrameLayoutFixed(this);
    avatarContainer.setBackgroundResource(R.drawable.bar_selector);
    avatarContainer.setPadding(AndroidUtilities.dp(4), 0, AndroidUtilities.dp(4), 0);
    actionBar.addView(avatarContainer);
    FrameLayout.LayoutParams layoutParams2 =
        (FrameLayout.LayoutParams) avatarContainer.getLayoutParams();
    layoutParams2.height = LayoutHelper.MATCH_PARENT;
    layoutParams2.width = LayoutHelper.WRAP_CONTENT;
    layoutParams2.rightMargin = AndroidUtilities.dp(48);
    layoutParams2.leftMargin = AndroidUtilities.dp(60);
    layoutParams2.gravity = Gravity.TOP | Gravity.LEFT;
    avatarContainer.setLayoutParams(layoutParams2);

    avatarImageView = new BackupImageView(this);
    avatarImageView.setRoundRadius(AndroidUtilities.dp(21));
    avatarContainer.addView(avatarImageView);
    layoutParams2 = (FrameLayout.LayoutParams) avatarImageView.getLayoutParams();
    layoutParams2.width = AndroidUtilities.dp(42);
    layoutParams2.height = AndroidUtilities.dp(42);
    layoutParams2.topMargin = AndroidUtilities.dp(3);
    avatarImageView.setLayoutParams(layoutParams2);

    nameTextView = new TextView(this);
    nameTextView.setTextColor(0xffffffff);
    nameTextView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 18);
    nameTextView.setLines(1);
    nameTextView.setMaxLines(1);
    nameTextView.setSingleLine(true);
    nameTextView.setEllipsize(TextUtils.TruncateAt.END);
    nameTextView.setGravity(Gravity.LEFT);
    nameTextView.setTypeface(AndroidUtilities.getTypeface("fonts/rmedium.ttf"));
    avatarContainer.addView(nameTextView);
    layoutParams2 = (FrameLayout.LayoutParams) nameTextView.getLayoutParams();
    layoutParams2.width = LayoutHelper.WRAP_CONTENT;
    layoutParams2.height = LayoutHelper.WRAP_CONTENT;
    layoutParams2.leftMargin = AndroidUtilities.dp(54);
    layoutParams2.bottomMargin = AndroidUtilities.dp(22);
    layoutParams2.gravity = Gravity.BOTTOM;
    nameTextView.setLayoutParams(layoutParams2);

    onlineTextView = new TextView(this);
    onlineTextView.setTextColor(0xffd7e8f7);
    onlineTextView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 14);
    onlineTextView.setLines(1);
    onlineTextView.setMaxLines(1);
    onlineTextView.setSingleLine(true);
    onlineTextView.setEllipsize(TextUtils.TruncateAt.END);
    onlineTextView.setGravity(Gravity.LEFT);
    avatarContainer.addView(onlineTextView);
    layoutParams2 = (FrameLayout.LayoutParams) onlineTextView.getLayoutParams();
    layoutParams2.width = LayoutHelper.WRAP_CONTENT;
    layoutParams2.height = LayoutHelper.WRAP_CONTENT;
    layoutParams2.leftMargin = AndroidUtilities.dp(54);
    layoutParams2.bottomMargin = AndroidUtilities.dp(4);
    layoutParams2.gravity = Gravity.BOTTOM;
    onlineTextView.setLayoutParams(layoutParams2);

    actionBar.setActionBarMenuOnItemClick(
        new ActionBar.ActionBarMenuOnItemClick() {
          @Override
          public void onItemClick(int id) {
            if (id == -1) {
              onFinish();
              finish();
            } else if (id == 1) {
              openCurrentMessage();
            } else if (id == 2) {
              switchToNextMessage();
            }
          }
        });

    PowerManager pm =
        (PowerManager) ApplicationLoader.applicationContext.getSystemService(Context.POWER_SERVICE);
    wakeLock =
        pm.newWakeLock(
            PowerManager.SCREEN_DIM_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "screen");
    wakeLock.setReferenceCounted(false);

    handleIntent(getIntent());
  }