gt:                             codeField[num - 1].setSelection(codeField[num - 1].length());
context: /*
   * This is the source code of Telegram for Android v. 5.x.x.
   * It is licensed under GNU GPL v. 2 or later.
   * You should have received a copy of the license in this archive (see LICENSE).
   *
   * Copyright Nikolai Kudashov, 2013-2018.
   */
  
  package org.telegram.ui;
  
  import android.animation.Animator;
  import android.animation.AnimatorListenerAdapter;
  import android.animation.AnimatorSet;
  import android.animation.ObjectAnimator;
  import android.annotation.SuppressLint;
  import android.app.Activity;
  import android.app.Dialog;
  import android.content.Context;
  import android.content.Intent;
  import android.content.SharedPreferences;
  import android.content.pm.PackageInfo;
  import android.graphics.Canvas;
  import android.graphics.Paint;
  import android.graphics.PorterDuff;
  import android.graphics.PorterDuffColorFilter;
  import android.graphics.Rect;
  import android.graphics.drawable.Drawable;
  import android.os.Build;
  import android.os.Bundle;
  import android.telephony.PhoneNumberUtils;
  import android.telephony.TelephonyManager;
  import android.text.Editable;
  import android.text.InputType;
  import android.text.TextUtils;
  import android.text.TextWatcher;
  import android.util.TypedValue;
  import android.view.Gravity;
  import android.view.KeyEvent;
  import android.view.View;
  import android.view.animation.AccelerateDecelerateInterpolator;
  import android.view.inputmethod.EditorInfo;
  import android.widget.FrameLayout;
  import android.widget.ImageView;
  import android.widget.LinearLayout;
  import android.widget.ScrollView;
  import android.widget.TextView;
  
  import org.telegram.PhoneFormat.PhoneFormat;
  import org.telegram.messenger.AndroidUtilities;
  import org.telegram.messenger.ApplicationLoader;
  import org.telegram.messenger.BuildVars;
  import org.telegram.messenger.FileLog;
  import org.telegram.messenger.LocaleController;
  import org.telegram.messenger.NotificationCenter;
  import org.telegram.messenger.R;
  import org.telegram.tgnet.ConnectionsManager;
  import org.telegram.tgnet.TLRPC;
  import org.telegram.ui.ActionBar.ActionBar;
  import org.telegram.ui.ActionBar.ActionBarMenu;
  import org.telegram.ui.ActionBar.AlertDialog;
  import org.telegram.ui.ActionBar.BaseFragment;
  import org.telegram.ui.ActionBar.Theme;
  import org.telegram.ui.ActionBar.ThemeDescription;
  import org.telegram.ui.Components.AlertsCreator;
  import org.telegram.ui.Components.EditTextBoldCursor;
  import org.telegram.ui.Components.LayoutHelper;
  import org.telegram.ui.Components.RadialProgressView;
  import org.telegram.ui.Components.SlideView;
  
  import java.util.ArrayList;
  import java.util.Locale;
  import java.util.Timer;
  import java.util.TimerTask;
  
  public class CancelAccountDeletionActivity extends BaseFragment {
  
      private int currentViewNum = 0;
      private SlideView[] views = new SlideView[5];
      private AlertDialog progressDialog;
      private Dialog permissionsDialog;
      private ArrayList<String> permissionsItems = new ArrayList<>();
      private boolean checkPermissions = false; //true;
      private View doneButton;
      private String hash;
      private String phone;
      private Dialog errorDialog;
  
      private int scrollHeight;
  
      private final static int done_button = 1;
  
      private class ProgressView extends View {
  
          private Paint paint = new Paint();
          private Paint paint2 = new Paint();
          private float progress;
  
          public ProgressView(Context context) {
              super(context);
              paint.setColor(Theme.getColor(Theme.key_login_progressInner));
              paint2.setColor(Theme.getColor(Theme.key_login_progressOuter));
          }
  
          public void setProgress(float value) {
              progress = value;
              invalidate();
          }
  
          @Override
          protected void onDraw(Canvas canvas) {
              int start = (int) (getMeasuredWidth() * progress);
              canvas.drawRect(0, 0, start, getMeasuredHeight(), paint2);
              canvas.drawRect(start, 0, getMeasuredWidth(), getMeasuredHeight(), paint);
          }
      }
  
      public CancelAccountDeletionActivity(Bundle args) {
          super(args);
          hash = args.getString("hash");
          phone = args.getString("phone");
      }
  
      @Override
      public void onFragmentDestroy() {
          super.onFragmentDestroy();
          for (int a = 0; a < views.length; a++) {
              if (views[a] != null) {
                  views[a].onDestroyActivity();
              }
          }
          if (progressDialog != null) {
              try {
                  progressDialog.dismiss();
              } catch (Exception e) {
                  FileLog.e(e);
              }
              progressDialog = null;
          }
          AndroidUtilities.removeAdjustResize(getParentActivity(), classGuid);
      }
  
      @Override
      public View createView(Context context) {
          actionBar.setTitle(LocaleController.getString("AppName", R.string.AppName));
          actionBar.setBackButtonImage(R.drawable.ic_ab_back);
          actionBar.setActionBarMenuOnItemClick(new ActionBar.ActionBarMenuOnItemClick() {
              @Override
              public void onItemClick(int id) {
                  if (id == done_button) {
                      views[currentViewNum].onNextPressed();
                  } else if (id == -1) {
                      finishFragment();
                  }
              }
          });
  
          ActionBarMenu menu = actionBar.createMenu();
          doneButton = menu.addItemWithWidth(done_button, R.drawable.ic_done, AndroidUtilities.dp(56));
          doneButton.setVisibility(View.GONE);
  
          ScrollView scrollView = new ScrollView(context) {
              @Override
              public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
                  if (currentViewNum == 1 || currentViewNum == 2 || currentViewNum == 4) {
                      rectangle.bottom += AndroidUtilities.dp(40);
                  }
                  return super.requestChildRectangleOnScreen(child, rectangle, immediate);
              }
  
              @Override
              protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
                  scrollHeight = MeasureSpec.getSize(heightMeasureSpec) - AndroidUtilities.dp(30);
                  super.onMeasure(widthMeasureSpec, heightMeasureSpec);
              }
          };
          scrollView.setFillViewport(true);
          fragmentView = scrollView;
  
          FrameLayout frameLayout = new FrameLayout(context);
          scrollView.addView(frameLayout, LayoutHelper.createScroll(LayoutHelper.MATCH_PARENT, LayoutHelper.WRAP_CONTENT, Gravity.TOP | Gravity.LEFT));
  
          views[0] = new PhoneView(context);
          views[1] = new LoginActivitySmsView(context, 1);
          views[2] = new LoginActivitySmsView(context, 2);
          views[3] = new LoginActivitySmsView(context, 3);
          views[4] = new LoginActivitySmsView(context, 4);
  
          for (int a = 0; a < views.length; a++) {
              views[a].setVisibility(a == 0 ? View.VISIBLE : View.GONE);
              frameLayout.addView(views[a], LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, a == 0 ? LayoutHelper.WRAP_CONTENT : LayoutHelper.MATCH_PARENT, Gravity.TOP | Gravity.LEFT, AndroidUtilities.isTablet() ? 26 : 18, 30, AndroidUtilities.isTablet() ? 26 : 18, 0));
          }
  
          actionBar.setTitle(views[0].getHeaderName());
  
          return fragmentView;
      }
  
      @Override
      public void onResume() {
          super.onResume();
          AndroidUtilities.requestAdjustResize(getParentActivity(), classGuid);
      }
  
      @Override
      public void onRequestPermissionsResultFragment(int requestCode, String[] permissions, int[] grantResults) {
          if (requestCode == 6) {
              checkPermissions = false;
              if (currentViewNum == 0) {
                  views[currentViewNum].onNextPressed();
              }
          }
      }
  
      @Override
      protected void onDialogDismiss(Dialog dialog) {
          if (Build.VERSION.SDK_INT >= 23 && dialog == permissionsDialog && !permissionsItems.isEmpty()) {
              getParentActivity().requestPermissions(permissionsItems.toArray(new String[0]), 6);
          }
          if (dialog == errorDialog) {
              finishFragment();
          }
      }
  
      @Override
      public boolean onBackPressed() {
          for (int a = 0; a < views.length; a++) {
              if (views[a] != null) {
                  views[a].onDestroyActivity();
              }
          }
          return true;
      }
  
      @Override
      public void onTransitionAnimationEnd(boolean isOpen, boolean backward) {
          if (isOpen) {
              views[currentViewNum].onShow();
          }
      }
  
      public void needShowProgress() {
          if (getParentActivity() == null || getParentActivity().isFinishing() || progressDialog != null) {
              return;
          }
          progressDialog = new AlertDialog(getParentActivity(), 3);
          progressDialog.setCanCacnel(false);
          progressDialog.show();
      }
  
      public void needHideProgress() {
          if (progressDialog == null) {
              return;
          }
          try {
              progressDialog.dismiss();
          } catch (Exception e) {
              FileLog.e(e);
          }
          progressDialog = null;
      }
  
      public void setPage(int page, boolean animated, Bundle params, boolean back) {
          if (page == 3 || page == 0) {
              if (page == 0) {
                  //checkPermissions = true;
              }
              doneButton.setVisibility(View.GONE);
          } else {
              doneButton.setVisibility(View.VISIBLE);
          }
          final SlideView outView = views[currentViewNum];
          final SlideView newView = views[page];
          currentViewNum = page;
  
          newView.setParams(params, false);
          actionBar.setTitle(newView.getHeaderName());
          newView.onShow();
          newView.setX(back ? -AndroidUtilities.displaySize.x : AndroidUtilities.displaySize.x);
  
          AnimatorSet animatorSet = new AnimatorSet();
          animatorSet.setInterpolator(new AccelerateDecelerateInterpolator());
          animatorSet.setDuration(300);
          animatorSet.playTogether(
                  ObjectAnimator.ofFloat(outView, "translationX", back ? AndroidUtilities.displaySize.x : -AndroidUtilities.displaySize.x),
                  ObjectAnimator.ofFloat(newView, "translationX", 0));
          animatorSet.addListener(new AnimatorListenerAdapter() {
              @Override
              public void onAnimationStart(Animator animation) {
                  newView.setVisibility(View.VISIBLE);
              }
  
              @Override
              public void onAnimationEnd(Animator animation) {
                  outView.setVisibility(View.GONE);
                  outView.setX(0);
              }
          });
          animatorSet.start();
      }
  
      private void fillNextCodeParams(Bundle params, TLRPC.TL_auth_sentCode res) {
          params.putString("phoneHash", res.phone_code_hash);
          if (res.next_type instanceof TLRPC.TL_auth_codeTypeCall) {
              params.putInt("nextType", 4);
          } else if (res.next_type instanceof TLRPC.TL_auth_codeTypeFlashCall) {
              params.putInt("nextType", 3);
          } else if (res.next_type instanceof TLRPC.TL_auth_codeTypeSms) {
              params.putInt("nextType", 2);
          }
          if (res.type instanceof TLRPC.TL_auth_sentCodeTypeApp) {
              params.putInt("type", 1);
              params.putInt("length", res.type.length);
              setPage(1, true, params, false);
          } else {
              if (res.timeout == 0) {
                  res.timeout = 60;
              }
              params.putInt("timeout", res.timeout * 1000);
              if (res.type instanceof TLRPC.TL_auth_sentCodeTypeCall) {
                  params.putInt("type", 4);
                  params.putInt("length", res.type.length);
                  setPage(4, true, params, false);
              } else if (res.type instanceof TLRPC.TL_auth_sentCodeTypeFlashCall) {
                  params.putInt("type", 3);
                  params.putString("pattern", res.type.pattern);
                  setPage(3, true, params, false);
              } else if (res.type instanceof TLRPC.TL_auth_sentCodeTypeSms) {
                  params.putInt("type", 2);
                  params.putInt("length", res.type.length);
                  setPage(2, true, params, false);
              }
          }
      }
  
      public class PhoneView extends SlideView {
  
          private boolean nextPressed = false;
          private RadialProgressView progressBar;
  
          public PhoneView(Context context) {
              super(context);
  
              setOrientation(VERTICAL);
  
              FrameLayout frameLayout = new FrameLayout(context);
              addView(frameLayout, LayoutHelper.createLinear(LayoutHelper.MATCH_PARENT, 200));
  
              progressBar = new RadialProgressView(context);
              frameLayout.addView(progressBar, LayoutHelper.createFrame(LayoutHelper.WRAP_CONTENT, LayoutHelper.WRAP_CONTENT, Gravity.CENTER));
          }
  
          @Override
          public void onNextPressed() {
              if (getParentActivity() == null || nextPressed) {
                  return;
              }
              TelephonyManager tm = (TelephonyManager) ApplicationLoader.applicationContext.getSystemService(Context.TELEPHONY_SERVICE);
              boolean simcardAvailable = tm.getSimState() != TelephonyManager.SIM_STATE_ABSENT && tm.getPhoneType() != TelephonyManager.PHONE_TYPE_NONE;
              boolean allowCall = true;
              if (Build.VERSION.SDK_INT >= 23 && simcardAvailable) {
                  //allowCall = getParentActivity().checkSelfPermission(Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED;
                  /*if (checkPermissions) {
                      permissionsItems.clear();
                      if (!allowCall) {
                          permissionsItems.add(Manifest.permission.READ_PHONE_STATE);
                      }
                      if (!permissionsItems.isEmpty()) {
                          SharedPreferences preferences = MessagesController.getGlobalMainSettings();
                          if (preferences.getBoolean("firstlogin", true) || getParentActivity().shouldShowRequestPermissionRationale(Manifest.permission.READ_PHONE_STATE)) {
                              preferences.edit().putBoolean("firstlogin", false).commit();
                              AlertDialog.Builder builder = new AlertDialog.Builder(getParentActivity());
                              builder.setTitle(LocaleController.getString("AppName", R.string.AppName));
                              builder.setPositiveButton(LocaleController.getString("OK", R.string.OK), null);
                              builder.setMessage(LocaleController.getString("AllowReadCall", R.string.AllowReadCall));
                              permissionsDialog = showDialog(builder.create());
                          } else {
                              getParentActivity().requestPermissions(permissionsItems.toArray(new String[permissionsItems.size()]), 6);
                          }
                          return;
                      }
                  }*/
              }
  
              final TLRPC.TL_account_sendConfirmPhoneCode req = new TLRPC.TL_account_sendConfirmPhoneCode();
              req.hash = hash;
              req.settings = new TLRPC.TL_codeSettings();
              req.settings.allow_flashcall = false;//simcardAvailable && allowCall;
              req.settings.allow_app_hash = ApplicationLoader.hasPlayServices;
              SharedPreferences preferences = ApplicationLoader.applicationContext.getSharedPreferences("mainconfig", Activity.MODE_PRIVATE);
              if (req.settings.allow_app_hash) {
                  preferences.edit().putString("sms_hash", BuildVars.SMS_HASH).commit();
              } else {
                  preferences.edit().remove("sms_hash").commit();
              }
              if (req.settings.allow_flashcall) {
                  try {
                      @SuppressLint("HardwareIds") String number = tm.getLine1Number();
                      if (!TextUtils.isEmpty(number)) {
                          req.settings.current_number = PhoneNumberUtils.compare(phone, number);
                          if (!req.settings.current_number) {
                              req.settings.allow_flashcall = false;
                          }
                      } else {
                          req.settings.current_number = false;
                      }
                  } catch (Exception e) {
                      req.settings.allow_flashcall = false;
                      FileLog.e(e);
                  }
              }
  
              final Bundle params = new Bundle();
              params.putString("phone", phone);
              nextPressed = true;
              ConnectionsManager.getInstance(currentAccount).sendRequest(req, (response, error) -> AndroidUtilities.runOnUIThread(() -> {
                  nextPressed = false;
                  if (error == null) {
                      fillNextCodeParams(params, (TLRPC.TL_auth_sentCode) response);
                  } else {
                      errorDialog = AlertsCreator.processError(currentAccount, error, CancelAccountDeletionActivity.this, req);
                  }
              }), ConnectionsManager.RequestFlagFailOnServerErrors);
          }
  
          @Override
          public String getHeaderName() {
              return LocaleController.getString("CancelAccountReset", R.string.CancelAccountReset);
          }
  
          @Override
          public void onShow() {
              super.onShow();
              onNextPressed();
          }
      }
  
      public class LoginActivitySmsView extends SlideView implements NotificationCenter.NotificationCenterDelegate {
  
          private String phone;
          private String phoneHash;
          private LinearLayout codeFieldContainer;
          private EditTextBoldCursor[] codeField;
          private TextView confirmTextView;
          private TextView titleTextView;
          private ImageView blackImageView;
          private ImageView blueImageView;
          private TextView timeText;
          private TextView problemText;
          private Bundle currentParams;
          private ProgressView progressView;
  
          private Timer timeTimer;
          private Timer codeTimer;
          private int openTime;
          private final Object timerSync = new Object();
          private int time = 60000;
          private int codeTime = 15000;
          private double lastCurrentTime;
          private double lastCodeTime;
          private boolean ignoreOnTextChange;
          private boolean waitingForEvent;
          private boolean nextPressed;
          private String lastError = "";
          private int currentType;
          private int nextType;
          private String pattern = "*";
          private int length;
          private int timeout;
  
          public LoginActivitySmsView(Context context, final int type) {
              super(context);
  
              currentType = type;
              setOrientation(VERTICAL);
  
              confirmTextView = new TextView(context);
              confirmTextView.setTextColor(Theme.getColor(Theme.key_windowBackgroundWhiteGrayText6));
              confirmTextView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 14);
              confirmTextView.setLineSpacing(AndroidUtilities.dp(2), 1.0f);
  
              titleTextView = new TextView(context);
              titleTextView.setTextColor(Theme.getColor(Theme.key_windowBackgroundWhiteBlackText));
              titleTextView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 18);
              titleTextView.setTypeface(AndroidUtilities.getTypeface("fonts/rmedium.ttf"));
              titleTextView.setGravity(LocaleController.isRTL ? Gravity.RIGHT : Gravity.LEFT);
              titleTextView.setLineSpacing(AndroidUtilities.dp(2), 1.0f);
              titleTextView.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL);
  
              if (currentType == 3) {
                  confirmTextView.setGravity(Gravity.TOP | (LocaleController.isRTL ? Gravity.RIGHT : Gravity.LEFT));
                  FrameLayout frameLayout = new FrameLayout(context);
                  addView(frameLayout, LayoutHelper.createLinear(LayoutHelper.WRAP_CONTENT, LayoutHelper.WRAP_CONTENT, LocaleController.isRTL ? Gravity.RIGHT : Gravity.LEFT));
  
                  ImageView imageView = new ImageView(context);
                  imageView.setImageResource(R.drawable.phone_activate);
                  if (LocaleController.isRTL) {
                      frameLayout.addView(imageView, LayoutHelper.createFrame(64, 76, Gravity.LEFT | Gravity.CENTER_VERTICAL, 2, 2, 0, 0));
                      frameLayout.addView(confirmTextView, LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, LayoutHelper.WRAP_CONTENT, LocaleController.isRTL ? Gravity.RIGHT : Gravity.LEFT, 64 + 18, 0, 0, 0));
                  } else {
                      frameLayout.addView(confirmTextView, LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, LayoutHelper.WRAP_CONTENT, LocaleController.isRTL ? Gravity.RIGHT : Gravity.LEFT, 0, 0, 64 + 18, 0));
                      frameLayout.addView(imageView, LayoutHelper.createFrame(64, 76, Gravity.RIGHT | Gravity.CENTER_VERTICAL, 0, 2, 0, 2));
                  }
              } else {
                  confirmTextView.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL);
  
                  FrameLayout frameLayout = new FrameLayout(context);
                  addView(frameLayout, LayoutHelper.createLinear(LayoutHelper.WRAP_CONTENT, LayoutHelper.WRAP_CONTENT, Gravity.TOP | Gravity.CENTER_HORIZONTAL));
  
                  if (currentType == 1) {
                      blackImageView = new ImageView(context);
                      blackImageView.setImageResource(R.drawable.sms_devices);
                      blackImageView.setColorFilter(new PorterDuffColorFilter(Theme.getColor(Theme.key_windowBackgroundWhiteBlackText), PorterDuff.Mode.MULTIPLY));
                      frameLayout.addView(blackImageView, LayoutHelper.createFrame(LayoutHelper.WRAP_CONTENT, LayoutHelper.WRAP_CONTENT, Gravity.LEFT | Gravity.TOP, 0, 0, 0, 0));
  
                      blueImageView = new ImageView(context);
                      blueImageView.setImageResource(R.drawable.sms_bubble);
                      blueImageView.setColorFilter(new PorterDuffColorFilter(Theme.getColor(Theme.key_chats_actionBackground), PorterDuff.Mode.MULTIPLY));
                      frameLayout.addView(blueImageView, LayoutHelper.createFrame(LayoutHelper.WRAP_CONTENT, LayoutHelper.WRAP_CONTENT, Gravity.LEFT | Gravity.TOP, 0, 0, 0, 0));
  
                      titleTextView.setText(LocaleController.getString("SentAppCodeTitle", R.string.SentAppCodeTitle));
                  } else {
                      blueImageView = new ImageView(context);
                      blueImageView.setImageResource(R.drawable.sms_code);
                      blueImageView.setColorFilter(new PorterDuffColorFilter(Theme.getColor(Theme.key_chats_actionBackground), PorterDuff.Mode.MULTIPLY));
                      frameLayout.addView(blueImageView, LayoutHelper.createFrame(LayoutHelper.WRAP_CONTENT, LayoutHelper.WRAP_CONTENT, Gravity.LEFT | Gravity.TOP, 0, 0, 0, 0));
  
                      titleTextView.setText(LocaleController.getString("SentSmsCodeTitle", R.string.SentSmsCodeTitle));
                  }
                  addView(titleTextView, LayoutHelper.createLinear(LayoutHelper.WRAP_CONTENT, LayoutHelper.WRAP_CONTENT, Gravity.CENTER_HORIZONTAL | Gravity.TOP, 0, 18, 0, 0));
                  addView(confirmTextView, LayoutHelper.createLinear(LayoutHelper.WRAP_CONTENT, LayoutHelper.WRAP_CONTENT, Gravity.CENTER_HORIZONTAL | Gravity.TOP, 0, 17, 0, 0));
              }
  
              codeFieldContainer = new LinearLayout(context);
              codeFieldContainer.setOrientation(HORIZONTAL);
              addView(codeFieldContainer, LayoutHelper.createLinear(LayoutHelper.WRAP_CONTENT, 36, Gravity.CENTER_HORIZONTAL));
              if (currentType == 3) {
                  codeFieldContainer.setVisibility(GONE);
              }
  
              timeText = new TextView(context) {
                  @Override
                  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
                      super.onMeasure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(AndroidUtilities.dp(100), MeasureSpec.AT_MOST));
                  }
              };
              timeText.setTextColor(Theme.getColor(Theme.key_windowBackgroundWhiteGrayText6));
              timeText.setLineSpacing(AndroidUtilities.dp(2), 1.0f);
              if (currentType == 3) {
                  timeText.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 14);
                  addView(timeText, LayoutHelper.createLinear(LayoutHelper.WRAP_CONTENT, LayoutHelper.WRAP_CONTENT, LocaleController.isRTL ? Gravity.RIGHT : Gravity.LEFT));
  
                  progressView = new ProgressView(context);
                  timeText.setGravity(LocaleController.isRTL ? Gravity.RIGHT : Gravity.LEFT);
                  addView(progressView, LayoutHelper.createLinear(LayoutHelper.MATCH_PARENT, 3, 0, 12, 0, 0));
              } else {
                  timeText.setPadding(0, AndroidUtilities.dp(2), 0, AndroidUtilities.dp(10));
                  timeText.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 15);
                  timeText.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL);
                  addView(timeText, LayoutHelper.createLinear(LayoutHelper.WRAP_CONTENT, LayoutHelper.WRAP_CONTENT, Gravity.TOP | Gravity.CENTER_HORIZONTAL));
              }
  
              problemText = new TextView(context) {
                  @Override
                  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
                      super.onMeasure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(AndroidUtilities.dp(100), MeasureSpec.AT_MOST));
                  }
              };
              problemText.setTextColor(Theme.getColor(Theme.key_windowBackgroundWhiteBlueText4));
              problemText.setLineSpacing(AndroidUtilities.dp(2), 1.0f);
              problemText.setPadding(0, AndroidUtilities.dp(2), 0, AndroidUtilities.dp(10));
              problemText.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 15);
              problemText.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.TOP);
              if (currentType == 1) {
                  problemText.setText(LocaleController.getString("DidNotGetTheCodeSms", R.string.DidNotGetTheCodeSms));
              } else {
                  problemText.setText(LocaleController.getString("DidNotGetTheCode", R.string.DidNotGetTheCode));
              }
              addView(problemText, LayoutHelper.createLinear(LayoutHelper.WRAP_CONTENT, LayoutHelper.WRAP_CONTENT, Gravity.CENTER_HORIZONTAL | Gravity.TOP));
              problemText.setOnClickListener(v -> {
                  if (nextPressed) {
                      return;
                  }
                  boolean email = nextType == 4 && currentType == 2 || nextType == 0;
                  if (!email) {
                      resendCode();
                  } else {
                      try {
                          PackageInfo pInfo = ApplicationLoader.applicationContext.getPackageManager().getPackageInfo(ApplicationLoader.applicationContext.getPackageName(), 0);
                          String version = String.format(Locale.US, "%s (%d)", pInfo.versionName, pInfo.versionCode);
  
                          Intent mailer = new Intent(Intent.ACTION_SEND);
                          mailer.setType("message/rfc822");
                          mailer.putExtra(Intent.EXTRA_EMAIL, new String[]{"sms@stel.com"});
                          mailer.putExtra(Intent.EXTRA_SUBJECT, "Android cancel account deletion issue " + version + " " + phone);
                          mailer.putExtra(Intent.EXTRA_TEXT, "Phone: " + phone + "\nApp version: " + version + "\nOS version: SDK " + Build.VERSION.SDK_INT + "\nDevice Name: " + Build.MANUFACTURER + Build.MODEL + "\nLocale: " + Locale.getDefault() + "\nError: " + lastError);
                          getContext().startActivity(Intent.createChooser(mailer, "Send email..."));
                      } catch (Exception e) {
                          AlertsCreator.showSimpleAlert(CancelAccountDeletionActivity.this, LocaleController.getString("NoMailInstalled", R.string.NoMailInstalled));
                      }
                  }
              });
          }
  
          @Override
          protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
              super.onMeasure(widthMeasureSpec, heightMeasureSpec);
              if (currentType != 3 && blueImageView != null) {
                  int innerHeight = blueImageView.getMeasuredHeight() + titleTextView.getMeasuredHeight() + confirmTextView.getMeasuredHeight() + AndroidUtilities.dp(18 + 17);
                  int requiredHeight = AndroidUtilities.dp(80);
                  int maxHeight = AndroidUtilities.dp(291);
                  if (scrollHeight - innerHeight < requiredHeight) {
                      setMeasuredDimension(getMeasuredWidth(), innerHeight + requiredHeight);
                  } else if (scrollHeight > maxHeight) {
                      setMeasuredDimension(getMeasuredWidth(), maxHeight);
                  } else {
                      setMeasuredDimension(getMeasuredWidth(), scrollHeight);
                  }
              }
          }
  
          @Override
          protected void onLayout(boolean changed, int l, int t, int r, int b) {
              super.onLayout(changed, l, t, r, b);
              if (currentType != 3 && blueImageView != null) {
                  int bottom = confirmTextView.getBottom();
                  int height = getMeasuredHeight() - bottom;
  
                  int h;
                  if (problemText.getVisibility() == VISIBLE) {
                      h = problemText.getMeasuredHeight();
                      t = bottom + height - h;
                      problemText.layout(problemText.getLeft(), t, problemText.getRight(), t + h);
                  } else if (timeText.getVisibility() == VISIBLE) {
                      h = timeText.getMeasuredHeight();
                      t = bottom + height - h;
                      timeText.layout(timeText.getLeft(), t, timeText.getRight(), t + h);
                  } else {
                      t = bottom + height;
                  }
  
                  height = t - bottom;
                  h = codeFieldContainer.getMeasuredHeight();
                  t = (height - h) / 2 + bottom;
                  codeFieldContainer.layout(codeFieldContainer.getLeft(), t, codeFieldContainer.getRight(), t + h);
              }
          }
  
          private void resendCode() {
              final Bundle params = new Bundle();
              params.putString("phone", phone);
  
              nextPressed = true;
              needShowProgress();
  
              final TLRPC.TL_auth_resendCode req = new TLRPC.TL_auth_resendCode();
              req.phone_number = phone;
              req.phone_code_hash = phoneHash;
              ConnectionsManager.getInstance(currentAccount).sendRequest(req, (response, error) -> AndroidUtilities.runOnUIThread(() -> {
                  nextPressed = false;
                  if (error == null) {
                      fillNextCodeParams(params, (TLRPC.TL_auth_sentCode) response);
                  } else {
                      if (error.text != null) {
                          AlertDialog dialog = (AlertDialog) AlertsCreator.processError(currentAccount, error, CancelAccountDeletionActivity.this, req);
                          if (dialog != null && error.text.contains("PHONE_CODE_EXPIRED")) {
                              dialog.setPositiveButtonListener((dialog1, which) -> {
                                  onBackPressed(true);
                                  finishFragment();
                              });
                          }
                      }
                  }
                  needHideProgress();
              }), ConnectionsManager.RequestFlagFailOnServerErrors);
          }
  
          @Override
          public String getHeaderName() {
              if (currentType == 1) {
                  return phone;
              } else {
                  return LocaleController.getString("CancelAccountReset", R.string.CancelAccountReset);
              }
          }
  
          @Override
          public boolean needBackButton() {
              return true;
          }
  
          @Override
          public void setParams(Bundle params, boolean restore) {
              if (params == null) {
                  return;
              }
              waitingForEvent = true;
              if (currentType == 2) {
                  AndroidUtilities.setWaitingForSms(true);
                  NotificationCenter.getGlobalInstance().addObserver(this, NotificationCenter.didReceiveSmsCode);
              } else if (currentType == 3) {
                  AndroidUtilities.setWaitingForCall(true);
                  NotificationCenter.getGlobalInstance().addObserver(this, NotificationCenter.didReceiveCall);
              }
  
              currentParams = params;
              phone = params.getString("phone");
              phoneHash = params.getString("phoneHash");
              timeout = time = params.getInt("timeout");
              openTime = (int) (System.currentTimeMillis() / 1000);
              nextType = params.getInt("nextType");
              pattern = params.getString("pattern");
              length = params.getInt("length");
              if (length == 0) {
                  length = 5;
              }
  
              if (codeField == null || codeField.length != length) {
                  codeField = new EditTextBoldCursor[length];
                  for (int a = 0; a < length; a++) {
                      final int num = a;
                      codeField[a] = new EditTextBoldCursor(getContext());
                      codeField[a].setTextColor(Theme.getColor(Theme.key_windowBackgroundWhiteBlackText));
                      codeField[a].setCursorColor(Theme.getColor(Theme.key_windowBackgroundWhiteBlackText));
                      codeField[a].setCursorSize(AndroidUtilities.dp(20));
                      codeField[a].setCursorWidth(1.5f);
  
                      Drawable pressedDrawable = getResources().getDrawable(R.drawable.search_dark_activated).mutate();
                      pressedDrawable.setColorFilter(new PorterDuffColorFilter(Theme.getColor(Theme.key_windowBackgroundWhiteInputFieldActivated), PorterDuff.Mode.MULTIPLY));
  
                      codeField[a].setBackgroundDrawable(pressedDrawable);
                      codeField[a].setImeOptions(EditorInfo.IME_ACTION_NEXT | EditorInfo.IME_FLAG_NO_EXTRACT_UI);
                      codeField[a].setTextSize(TypedValue.COMPLEX_UNIT_DIP, 20);
                      codeField[a].setMaxLines(1);
                      codeField[a].setTypeface(AndroidUtilities.getTypeface("fonts/rmedium.ttf"));
                      codeField[a].setPadding(0, 0, 0, 0);
                      codeField[a].setGravity(Gravity.CENTER_HORIZONTAL | Gravity.TOP);
                      if (currentType == 3) {
                          codeField[a].setEnabled(false);
                          codeField[a].setInputType(InputType.TYPE_NULL);
                          codeField[a].setVisibility(GONE);
                      } else {
                          codeField[a].setInputType(InputType.TYPE_CLASS_PHONE);
                      }
                      codeFieldContainer.addView(codeField[a], LayoutHelper.createLinear(34, 36, Gravity.CENTER_HORIZONTAL, 0, 0, a != length - 1 ? 7 : 0, 0));
                      codeField[a].addTextChangedListener(new TextWatcher() {
  
                          @Override
                          public void beforeTextChanged(CharSequence s, int start, int count, int after) {
  
                          }
  
                          @Override
                          public void onTextChanged(CharSequence s, int start, int before, int count) {
  
                          }
  
                          @Override
                          public void afterTextChanged(Editable s) {
                              if (ignoreOnTextChange) {
                                  return;
                              }
                              int len = s.length();
                              if (len >= 1) {
                                  if (len > 1) {
                                      String text = s.toString();
                                      ignoreOnTextChange = true;
                                      for (int a = 0; a < Math.min(length - num, len); a++) {
                                          if (a == 0) {
                                              s.replace(0, len, text.substring(a, a + 1));
                                          } else {
                                              codeField[num + a].setText(text.substring(a, a + 1));
                                          }
                                      }
                                      ignoreOnTextChange = false;
                                  }
  
                                  if (num != length - 1) {
                                      codeField[num + 1].setSelection(codeField[num + 1].length());
                                      codeField[num + 1].requestFocus();
                                  }
                                  if ((num == length - 1 || num == length - 2 && len >= 2) && getCode().length() == length) {
                                      onNextPressed();
                                  }
                              }
                          }
                      });
                      codeField[a].setOnKeyListener((v, keyCode, event) -> {
                          if (keyCode == KeyEvent.KEYCODE_DEL && codeField[num].length() == 0 && num > 0) {
  