Advertisement
OmarRajeev

Untitled

Jun 6th, 2017
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 159.92 KB | None | 0 0
  1. /*
  2. * This is the source code of Glabbr for Android v. 3.x.x.
  3. * It is licensed under GNU GPL v. 2 or later.
  4. * You should have received a copy of the license in this archive (see LICENSE).
  5. *
  6. * Author Rajeev Omar rajeev@glabbr.com, 2017.
  7. */
  8.  
  9. package com.mazak.ui;
  10.  
  11. import android.Manifest;
  12. import android.animation.Animator;
  13. import android.animation.ObjectAnimator;
  14. import android.animation.StateListAnimator;
  15. import android.annotation.SuppressLint;
  16. import android.app.Activity;
  17. import android.app.AlertDialog;
  18. import android.app.Dialog;
  19. import android.app.ProgressDialog;
  20. import android.content.Context;
  21. import android.content.DialogInterface;
  22. import android.content.Intent;
  23. import android.content.SharedPreferences;
  24. import android.content.pm.PackageInfo;
  25. import android.content.pm.PackageManager;
  26. import android.graphics.Canvas;
  27. import android.graphics.Paint;
  28. import android.graphics.Typeface;
  29. import android.os.Build;
  30. import android.os.Bundle;
  31. import android.os.Vibrator;
  32. import android.telephony.TelephonyManager;
  33. import android.text.Editable;
  34. import android.text.InputFilter;
  35. import android.text.InputType;
  36. import android.text.TextUtils;
  37. import android.text.TextWatcher;
  38. import android.text.method.PasswordTransformationMethod;
  39. import android.util.TypedValue;
  40. import android.view.Gravity;
  41. import android.view.KeyEvent;
  42. import android.view.View;
  43. import android.view.animation.AccelerateDecelerateInterpolator;
  44. import android.view.inputmethod.EditorInfo;
  45. import android.widget.AdapterView;
  46. import android.widget.EditText;
  47. import android.widget.FrameLayout;
  48. import android.widget.ImageView;
  49. import android.widget.LinearLayout;
  50. import android.widget.ScrollView;
  51. import android.widget.TextView;
  52.  
  53. import com.mazak.PhoneFormat.Country;
  54. import com.mazak.PhoneFormat.PhoneFormat;
  55. import com.mazak.messenger.AndroidUtilities;
  56. import com.mazak.messenger.ApplicationLoader;
  57. import com.mazak.messenger.BuildVars;
  58. import com.mazak.messenger.ContactsController;
  59. import com.mazak.messenger.FileLog;
  60. import com.mazak.messenger.LocaleController;
  61. import com.mazak.messenger.MessagesController;
  62. import com.mazak.messenger.MessagesStorage;
  63. import com.mazak.messenger.NotificationCenter;
  64. import com.mazak.messenger.R;
  65. import com.mazak.messenger.UserConfig;
  66. import com.mazak.messenger.Utilities;
  67. import com.mazak.tgnet.TLObject;
  68. import com.mazak.tgnet.TLRPC;
  69. import com.mazak.ui.ActionBar.ActionBarMenu;
  70. import com.mazak.ui.ActionBar.BaseFragment;
  71. import com.mazak.ui.Components.LayoutHelper;
  72. import com.mazak.ui.Components.SlideView;
  73. import com.mazak.tgnet.ConnectionsManager;
  74. import com.mazak.tgnet.RequestDelegate;
  75. import com.mazak.ui.ActionBar.ActionBar;
  76. import com.mazak.ui.Components.HintEditText;
  77.  
  78. import java.io.BufferedReader;
  79. import java.io.InputStreamReader;
  80. import java.util.ArrayList;
  81. import java.util.Collections;
  82. import java.util.Comparator;
  83. import java.util.HashMap;
  84. import java.util.Locale;
  85. import java.util.Map;
  86. import java.util.Set;
  87. import java.util.Timer;
  88. import java.util.TimerTask;
  89.  
  90. public class LoginActivity extends BaseFragment {
  91.  
  92. private int currentViewNum = 0;
  93. private SlideView[] views = new SlideView[7];
  94. private ProgressDialog progressDialog;
  95. private Dialog permissionsDialog;
  96. private ArrayList<String> permissionsItems = new ArrayList<>();
  97. private boolean checkPermissions = true;
  98. private View doneButton;
  99.  
  100. private final static int done_button = 1;
  101.  
  102. @Override
  103. public void onFragmentDestroy() {
  104. super.onFragmentDestroy();
  105. for (int a = 0; a < views.length; a++) {
  106. if (views[a] != null) {
  107. views[a].onDestroyActivity();
  108. }
  109. }
  110. if (progressDialog != null) {
  111. try {
  112. progressDialog.dismiss();
  113. } catch (Exception e) {
  114. FileLog.e("Glabbr", e);
  115. }
  116. progressDialog = null;
  117. }
  118. }
  119.  
  120. @Override
  121. public View createView(Context context) {
  122. actionBar.setTitle(LocaleController.getString("AppName", R.string.AppName));
  123.  
  124. actionBar.setActionBarMenuOnItemClick(new ActionBar.ActionBarMenuOnItemClick() {
  125. @Override
  126. public void onItemClick(int id) {
  127. if (id == done_button) {
  128. views[currentViewNum].onNextPressed();
  129. } else if (id == -1) {
  130. onBackPressed();
  131. }
  132. }
  133. });
  134.  
  135. ActionBarMenu menu = actionBar.createMenu();
  136. doneButton = menu.addItemWithWidth(done_button, R.drawable.ic_done, AndroidUtilities.dp(56));
  137.  
  138. fragmentView = new ScrollView(context);
  139. ScrollView scrollView = (ScrollView) fragmentView;
  140. scrollView.setFillViewport(true);
  141.  
  142. FrameLayout frameLayout = new FrameLayout(context);
  143. scrollView.addView(frameLayout, LayoutHelper.createScroll(LayoutHelper.MATCH_PARENT, LayoutHelper.WRAP_CONTENT, Gravity.TOP | Gravity.LEFT));
  144.  
  145. views[0] = new PhoneView(context);
  146. views[1] = new LoginActivitySmsView(context, 1);
  147. views[2] = new LoginActivitySmsView(context, 2);
  148. views[3] = new LoginActivitySmsView(context, 3);
  149. views[4] = new LoginActivitySmsView(context, 4);
  150. views[5] = new LoginActivityRegisterView(context);
  151. views[6] = new LoginActivityInviteView(context);
  152. /* views[6] = new LoginActivityPasswordView(context);
  153. views[7] = new LoginActivityRecoverView(context);*/
  154.  
  155. for (int a = 0; a < views.length; a++) {
  156. views[a].setVisibility(a == 0 ? View.VISIBLE : View.GONE);
  157. 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));
  158. }
  159.  
  160. Bundle savedInstanceState = loadCurrentState();
  161. if (savedInstanceState != null) {
  162. currentViewNum = savedInstanceState.getInt("currentViewNum", 0);
  163. if (currentViewNum >= 1 && currentViewNum <= 4) {
  164. int time = savedInstanceState.getInt("open");
  165. if (time != 0 && Math.abs(System.currentTimeMillis() / 1000 - time) >= 24 * 60 * 60) {
  166. currentViewNum = 0;
  167. savedInstanceState = null;
  168. clearCurrentState();
  169. }
  170. }
  171. }
  172. actionBar.setTitle(views[currentViewNum].getHeaderName());
  173. for (int a = 0; a < views.length; a++) {
  174. if (savedInstanceState != null) {
  175. if (a >= 1 && a <= 4) {
  176. if (a == currentViewNum) {
  177. views[a].restoreStateParams(savedInstanceState);
  178. }
  179. } else {
  180. views[a].restoreStateParams(savedInstanceState);
  181. }
  182. }
  183. if (currentViewNum == a) {
  184. actionBar.setBackButtonImage(views[a].needBackButton() ? R.drawable.ic_ab_back : 0);
  185. views[a].setVisibility(View.VISIBLE);
  186. views[a].onShow();
  187. if (a == 3 || a == 6) {
  188. doneButton.setVisibility(View.GONE);
  189. }
  190. } else {
  191. views[a].setVisibility(View.GONE);
  192. }
  193. }
  194.  
  195. return fragmentView;
  196. }
  197.  
  198. @Override
  199. public void onPause() {
  200. super.onPause();
  201. AndroidUtilities.removeAdjustResize(getParentActivity(), classGuid);
  202. }
  203.  
  204. @Override
  205. public void onResume() {
  206. super.onResume();
  207. AndroidUtilities.requestAdjustResize(getParentActivity(), classGuid);
  208. /*try {
  209. if (currentViewNum >= 1 && currentViewNum <= 4 && views[currentViewNum] instanceof LoginActivitySmsView) {
  210. int time = ((LoginActivitySmsView) views[currentViewNum]).openTime;
  211. if (time != 0 && Math.abs(System.currentTimeMillis() / 1000 - time) >= 24 * 60 * 60) {
  212. views[currentViewNum].onBackPressed();
  213. setPage(0, false, null, true);
  214. }
  215. }
  216. } catch (Exception e) {
  217. FileLog.e("Glabbr", e);
  218. }*/
  219. }
  220.  
  221. @Override
  222. public void onRequestPermissionsResultFragment(int requestCode, String[] permissions, int[] grantResults) {
  223. if (requestCode == 6) {
  224. checkPermissions = false;
  225. if (currentViewNum == 0) {
  226. views[currentViewNum].onNextPressed();
  227. }
  228. }
  229. }
  230.  
  231. private Bundle loadCurrentState() {
  232. try {
  233. Bundle bundle = new Bundle();
  234. ArrayList<String> phones = new ArrayList();
  235. SharedPreferences preferences = ApplicationLoader.applicationContext.getSharedPreferences("logininfo2", Context.MODE_PRIVATE);
  236. Map<String, ?> params = preferences.getAll();
  237. for (Map.Entry<String, ?> entry : params.entrySet()) {
  238. String key = entry.getKey();
  239. Object value = entry.getValue();
  240. String[] args = key.split("_\\|_");
  241. if (args.length == 1) {
  242. if (value instanceof String) {
  243. bundle.putString(key, (String) value);
  244. } else if (value instanceof Integer) {
  245. bundle.putInt(key, (Integer) value);
  246. }
  247. } else if (args.length == 2) {
  248. Bundle inner = bundle.getBundle(args[0]);
  249. if (inner == null) {
  250. inner = new Bundle();
  251. bundle.putBundle(args[0], inner);
  252. }
  253. String[] array = args[1].split("_");
  254. if(array.length == 2){
  255. phones.add((String)value);
  256. inner.putStringArrayList(array[0],phones);
  257. continue;
  258. }
  259. if (value instanceof String) {
  260. inner.putString(args[1], (String) value);
  261. } else if (value instanceof Integer) {
  262. inner.putInt(args[1], (Integer) value);
  263. }
  264. }
  265. }
  266. return bundle;
  267. } catch (Exception e) {
  268. FileLog.e("Glabbr", e);
  269. }
  270. return null;
  271. }
  272.  
  273. private void clearCurrentState() {
  274. SharedPreferences preferences = ApplicationLoader.applicationContext.getSharedPreferences("logininfo2", Context.MODE_PRIVATE);
  275. SharedPreferences.Editor editor = preferences.edit();
  276. editor.clear();
  277. editor.commit();
  278.  
  279. SharedPreferences preferences2 = ApplicationLoader.applicationContext.getSharedPreferences("mainconfig", Context.MODE_PRIVATE);
  280. SharedPreferences.Editor editor2 = preferences2.edit();
  281. editor2.remove("reason").commit();
  282.  
  283.  
  284. }
  285.  
  286. private void putBundleToEditor(Bundle bundle, SharedPreferences.Editor editor, String prefix) {
  287. Set<String> keys = bundle.keySet();
  288. for (String key : keys) {
  289. Object obj = bundle.get(key);
  290. if (obj instanceof String) {
  291. if (prefix != null) {
  292. editor.putString(prefix + "_|_" + key, (String) obj);
  293. } else {
  294. editor.putString(key, (String) obj);
  295. }
  296. } else if (obj instanceof Integer) {
  297. if (prefix != null) {
  298. editor.putInt(prefix + "_|_" + key, (Integer) obj);
  299. } else {
  300. editor.putInt(key, (Integer) obj);
  301. }
  302. } else if (obj instanceof ArrayList) {
  303. int size = ((ArrayList) obj).size();
  304. if (prefix != null) {
  305. //editor.putInt(prefix + "_|_" + key+"_size", size);
  306. for(int i=0;i<size;i++){
  307. editor.remove(prefix + "_|_" + key+"_" + i);
  308. editor.putString(prefix + "_|_" + key+"_" + i,(String) ((ArrayList) obj).get(i));
  309. }
  310. } else {
  311. editor.putInt(key+"_size", size);
  312. for(int i=0;i<size;i++){
  313. editor.remove(key+"_" + i);
  314. editor.putString(key+"_" + i,(String) ((ArrayList) obj).get(i));
  315. }
  316. }
  317. } else if (obj instanceof Bundle) {
  318. putBundleToEditor((Bundle) obj, editor, key);
  319. }
  320. }
  321. }
  322.  
  323. @Override
  324. protected void onDialogDismiss(Dialog dialog) {
  325. if (Build.VERSION.SDK_INT >= 23 && dialog == permissionsDialog && !permissionsItems.isEmpty() && getParentActivity() != null) {
  326. getParentActivity().requestPermissions(permissionsItems.toArray(new String[permissionsItems.size()]), 6);
  327. }
  328. }
  329.  
  330. @Override
  331. public boolean onBackPressed() {
  332. if (currentViewNum == 0) {
  333. for (int a = 0; a < views.length; a++) {
  334. if (views[a] != null) {
  335. views[a].onDestroyActivity();
  336. }
  337. }
  338. clearCurrentState();
  339. return true;
  340. }/* else if (currentViewNum == 6) {
  341. views[currentViewNum].onBackPressed();
  342. setPage(0, true, null, true);
  343. } else if (currentViewNum == 7) {
  344. views[currentViewNum].onBackPressed();
  345. setPage(6, true, null, true);
  346. }*/
  347. return false;
  348. }
  349.  
  350. private void needShowAlert(String title, String text) {
  351. if (text == null || getParentActivity() == null) {
  352. return;
  353. }
  354. AlertDialog.Builder builder = new AlertDialog.Builder(getParentActivity());
  355. builder.setTitle(title);
  356. builder.setMessage(text);
  357. builder.setPositiveButton(LocaleController.getString("OK", R.string.OK), null);
  358. showDialog(builder.create());
  359. }
  360.  
  361. private void needShowInvalidAlert(final String phoneNumber, final boolean banned) {
  362. if (getParentActivity() == null) {
  363. return;
  364. }
  365. AlertDialog.Builder builder = new AlertDialog.Builder(getParentActivity());
  366. builder.setTitle(LocaleController.getString("AppName", R.string.AppName));
  367. if (banned) {
  368. builder.setMessage(LocaleController.getString("BannedPhoneNumber", R.string.BannedPhoneNumber));
  369. } else {
  370. builder.setMessage(LocaleController.getString("InvalidPhoneNumber", R.string.InvalidPhoneNumber));
  371. }
  372. builder.setNeutralButton(LocaleController.getString("BotHelp", R.string.BotHelp), new DialogInterface.OnClickListener() {
  373. @Override
  374. public void onClick(DialogInterface dialog, int which) {
  375. try {
  376. PackageInfo pInfo = ApplicationLoader.applicationContext.getPackageManager().getPackageInfo(ApplicationLoader.applicationContext.getPackageName(), 0);
  377. String version = String.format(Locale.US, "%s (%d)", pInfo.versionName, pInfo.versionCode);
  378.  
  379. Intent mailer = new Intent(Intent.ACTION_SEND);
  380. mailer.setType("message/rfc822");
  381. mailer.putExtra(Intent.EXTRA_EMAIL, new String[]{"login@stel.com"});
  382. if (banned) {
  383. mailer.putExtra(Intent.EXTRA_SUBJECT, "Banned phone number: " + phoneNumber);
  384. mailer.putExtra(Intent.EXTRA_TEXT, "I'm trying to use my mobile phone number: " + phoneNumber + "\nBut Glabbr says it's banned. Please help.\n\nApp version: " + version + "\nOS version: SDK " + Build.VERSION.SDK_INT + "\nDevice Name: " + Build.MANUFACTURER + Build.MODEL + "\nLocale: " + Locale.getDefault());
  385. } else {
  386. mailer.putExtra(Intent.EXTRA_SUBJECT, "Invalid phone number: " + phoneNumber);
  387. mailer.putExtra(Intent.EXTRA_TEXT, "I'm trying to use my mobile phone number: " + phoneNumber + "\nBut Glabbr says it's invalid. Please help.\n\nApp version: " + version + "\nOS version: SDK " + Build.VERSION.SDK_INT + "\nDevice Name: " + Build.MANUFACTURER + Build.MODEL + "\nLocale: " + Locale.getDefault());
  388. }
  389. getParentActivity().startActivity(Intent.createChooser(mailer, "Send email..."));
  390. } catch (Exception e) {
  391. needShowAlert(LocaleController.getString("AppName", R.string.AppName), LocaleController.getString("NoMailInstalled", R.string.NoMailInstalled));
  392. }
  393. }
  394. });
  395. builder.setPositiveButton(LocaleController.getString("OK", R.string.OK), null);
  396. showDialog(builder.create());
  397. }
  398.  
  399. private void needShowProgress() {
  400. if (getParentActivity() == null || getParentActivity().isFinishing() || progressDialog != null) {
  401. return;
  402. }
  403. progressDialog = new ProgressDialog(getParentActivity());
  404. progressDialog.setMessage(LocaleController.getString("Loading", R.string.Loading));
  405. progressDialog.setCanceledOnTouchOutside(false);
  406. progressDialog.setCancelable(false);
  407. progressDialog.show();
  408. }
  409.  
  410. public void needHideProgress() {
  411. if (progressDialog == null) {
  412. return;
  413. }
  414. try {
  415. progressDialog.dismiss();
  416. } catch (Exception e) {
  417. FileLog.e("Glabbr", e);
  418. }
  419. progressDialog = null;
  420. }
  421.  
  422. public void setPage(int page, boolean animated, Bundle params, boolean back) {
  423. if (page == 3 || page == 6) {
  424. doneButton.setVisibility(View.GONE);
  425. } else {
  426. if (page == 0) {
  427. checkPermissions = true;
  428. }
  429. doneButton.setVisibility(View.VISIBLE);
  430. }
  431. if (animated) {
  432. final SlideView outView = views[currentViewNum];
  433. final SlideView newView = views[page];
  434. currentViewNum = page;
  435. actionBar.setBackButtonImage(newView.needBackButton() ? R.drawable.ic_ab_back : 0);
  436.  
  437. newView.setParams(params);
  438. actionBar.setTitle(newView.getHeaderName());
  439. newView.onShow();
  440. newView.setX(back ? -AndroidUtilities.displaySize.x : AndroidUtilities.displaySize.x);
  441. outView.animate().setInterpolator(new AccelerateDecelerateInterpolator()).setListener(new Animator.AnimatorListener() {
  442. @Override
  443. public void onAnimationStart(Animator animator) {
  444. }
  445.  
  446. @SuppressLint("NewApi")
  447. @Override
  448. public void onAnimationEnd(Animator animator) {
  449. outView.setVisibility(View.GONE);
  450. outView.setX(0);
  451. }
  452.  
  453. @Override
  454. public void onAnimationCancel(Animator animator) {
  455. }
  456.  
  457. @Override
  458. public void onAnimationRepeat(Animator animator) {
  459. }
  460. }).setDuration(300).translationX(back ? AndroidUtilities.displaySize.x : -AndroidUtilities.displaySize.x).start();
  461. newView.animate().setInterpolator(new AccelerateDecelerateInterpolator()).setListener(new Animator.AnimatorListener() {
  462. @Override
  463. public void onAnimationStart(Animator animator) {
  464. newView.setVisibility(View.VISIBLE);
  465. }
  466.  
  467. @Override
  468. public void onAnimationEnd(Animator animator) {
  469. }
  470.  
  471. @Override
  472. public void onAnimationCancel(Animator animator) {
  473. }
  474.  
  475. @Override
  476. public void onAnimationRepeat(Animator animator) {
  477. }
  478. }).setDuration(300).translationX(0).start();
  479. } else {
  480. actionBar.setBackButtonImage(views[page].needBackButton() ? R.drawable.ic_ab_back : 0);
  481. views[currentViewNum].setVisibility(View.GONE);
  482. currentViewNum = page;
  483. views[page].setParams(params);
  484. views[page].setVisibility(View.VISIBLE);
  485. actionBar.setTitle(views[page].getHeaderName());
  486. views[page].onShow();
  487. }
  488. }
  489.  
  490. @Override
  491. public void saveSelfArgs(Bundle outState) {
  492. try {
  493. Bundle bundle = new Bundle();
  494. bundle.putInt("currentViewNum", currentViewNum);
  495. for (int a = 0; a <= currentViewNum; a++) {
  496. SlideView v = views[a];
  497. if (v != null) {
  498. v.saveStateParams(bundle);
  499. }
  500. }
  501. SharedPreferences preferences = ApplicationLoader.applicationContext.getSharedPreferences("logininfo2", Context.MODE_PRIVATE);
  502. SharedPreferences.Editor editor = preferences.edit();
  503. editor.clear();
  504. putBundleToEditor(bundle, editor, null);
  505. editor.commit();
  506. SharedPreferences preferences2 = ApplicationLoader.applicationContext.getSharedPreferences("mainconfig", Context.MODE_PRIVATE);
  507. SharedPreferences.Editor editor2 = preferences2.edit();
  508. editor2.remove("reason").commit();
  509. } catch (Exception e) {
  510. FileLog.e("Glabbr", e);
  511. }
  512. }
  513.  
  514. private void needFinishActivity() {
  515. clearCurrentState();
  516. presentFragment(new DialogsActivity(null), true);
  517. NotificationCenter.getInstance().postNotificationName(NotificationCenter.mainUserInfoChanged);
  518. }
  519.  
  520. private void fillNextCodeParams(Bundle params, TLRPC.TL_auth_sentCode res) {
  521. params.putString("phoneHash", res.phone_code_hash);
  522. if (res.timeout == 0) {
  523. res.timeout = 60;
  524. }
  525. if (res.next_type == 4) {
  526. params.putInt("nextType", 4);
  527. params.putInt("type", 4);
  528. params.putInt("length", 6);
  529. setPage(4, true, params, false);
  530. } else if (res.next_type == 3) {
  531. params.putInt("nextType", 3);
  532. params.putInt("type", 3);
  533. params.putString("pattern", "*");
  534. setPage(3, true, params, false);
  535. } else if (res.next_type == 2) {
  536. params.putInt("nextType", 4);
  537. params.putInt("type", 2);
  538. params.putInt("length", 6);
  539. params.putInt("timeout",res.timeout * 1000);
  540. setPage(2, true, params, false);
  541. }else if (res.next_type == 6) {
  542. params.putInt("nextType", 6);
  543. params.putInt("type", 6);
  544. params.putStringArrayList("phones",res.phones);
  545. setPage(6, true, params, false);
  546. }
  547. }
  548.  
  549. public class PhoneView extends SlideView implements AdapterView.OnItemSelectedListener {
  550.  
  551. // private EditText codeField;
  552. private HintEditText phoneField;
  553. //private TextView countryButton;
  554.  
  555. //private int countryState = 0;
  556.  
  557. private ArrayList<String> countriesArray = new ArrayList<>();
  558. private HashMap<String, String> countriesMap = new HashMap<>();
  559. private HashMap<String, String> codesMap = new HashMap<>();
  560. private HashMap<String, String> phoneFormatMap = new HashMap<>();
  561.  
  562. private boolean ignoreSelection = false;
  563. private boolean ignoreOnTextChange = false;
  564. private boolean ignoreOnPhoneChange = false;
  565. private boolean nextPressed = false;
  566.  
  567. public PhoneView(Context context) {
  568. super(context);
  569.  
  570. setOrientation(VERTICAL);
  571.  
  572. /*countryButton = new TextView(context);
  573. countryButton.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 18);
  574. countryButton.setPadding(AndroidUtilities.dp(12), AndroidUtilities.dp(10), AndroidUtilities.dp(12), 0);
  575. countryButton.setTextColor(0xff212121);
  576. countryButton.setMaxLines(1);
  577. countryButton.setSingleLine(true);
  578. countryButton.setEllipsize(TextUtils.TruncateAt.END);
  579. countryButton.setGravity((LocaleController.isRTL ? Gravity.RIGHT : Gravity.LEFT) | Gravity.CENTER_HORIZONTAL);
  580. countryButton.setBackgroundResource(R.drawable.spinner_states);
  581. addView(countryButton, LayoutHelper.createLinear(LayoutHelper.MATCH_PARENT, 36, 0, 100, 0, 14));
  582. countryButton.setOnClickListener(new OnClickListener() {
  583. @Override
  584. public void onClick(View view) {
  585. CountrySelectActivity fragment = new CountrySelectActivity();
  586. fragment.setCountrySelectActivityDelegate(new CountrySelectActivity.CountrySelectActivityDelegate() {
  587. @Override
  588. public void didSelectCountry(String name) {
  589. selectCountry(name);
  590. AndroidUtilities.runOnUIThread(new Runnable() {
  591. @Override
  592. public void run() {
  593. AndroidUtilities.showKeyboard(phoneField);
  594. }
  595. }, 300);
  596. phoneField.requestFocus();
  597. phoneField.setSelection(phoneField.length());
  598. }
  599. });
  600. presentFragment(fragment);
  601. }
  602. });
  603. */
  604. /*View view = new View(context);
  605. view.setPadding(AndroidUtilities.dp(12), 0, AndroidUtilities.dp(12), 0);
  606. view.setBackgroundColor(0xffdbdbdb);
  607. addView(view, LayoutHelper.createLinear(LayoutHelper.MATCH_PARENT, 1, 4, -17.5f, 4, 0));
  608. */
  609. LinearLayout linearLayout = new LinearLayout(context);
  610. linearLayout.setOrientation(HORIZONTAL);
  611. addView(linearLayout, LayoutHelper.createLinear(LayoutHelper.MATCH_PARENT, LayoutHelper.WRAP_CONTENT, 0, 40, 0, 0));
  612.  
  613. /*TextView textView = new TextView(context);
  614. textView.setText("+");
  615. textView.setTextColor(0xff212121);
  616. textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 18);
  617. linearLayout.addView(textView, LayoutHelper.createLinear(LayoutHelper.WRAP_CONTENT, LayoutHelper.WRAP_CONTENT));
  618. */
  619. /* codeField = new EditText(context);
  620. codeField.setInputType(InputType.TYPE_CLASS_PHONE);
  621. codeField.setTextColor(0xff212121);
  622. AndroidUtilities.clearCursorDrawable(codeField);
  623. codeField.setPadding(AndroidUtilities.dp(10), 0, 0, 0);
  624. codeField.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 18);
  625. codeField.setMaxLines(1);
  626. codeField.setGravity(Gravity.LEFT | Gravity.CENTER_VERTICAL);
  627. codeField.setImeOptions(EditorInfo.IME_ACTION_NEXT | EditorInfo.IME_FLAG_NO_EXTRACT_UI);
  628. InputFilter[] inputFilters = new InputFilter[1];
  629. inputFilters[0] = new InputFilter.LengthFilter(5);
  630. codeField.setFilters(inputFilters);
  631. linearLayout.addView(codeField, LayoutHelper.createLinear(55, 36, -9, 0, 16, 0));
  632. codeField.addTextChangedListener(new TextWatcher() {
  633. @Override
  634. public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
  635.  
  636. }
  637.  
  638. @Override
  639. public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
  640.  
  641. }
  642.  
  643. @Override
  644. public void afterTextChanged(Editable editable) {
  645. if (ignoreOnTextChange) {
  646. return;
  647. }
  648. ignoreOnTextChange = true;
  649. String text = PhoneFormat.stripExceptNumbers(codeField.getText().toString());
  650. codeField.setText(text);
  651. if (text.length() == 0) {
  652. countryButton.setText(LocaleController.getString("ChooseCountry", R.string.ChooseCountry));
  653. phoneField.setHintText(null);
  654. countryState = 1;
  655. } else {
  656. String country;
  657. boolean ok = false;
  658. String textToSet = null;
  659. if (text.length() > 4) {
  660. ignoreOnTextChange = true;
  661. for (int a = 4; a >= 1; a--) {
  662. String sub = text.substring(0, a);
  663. country = codesMap.get(sub);
  664. if (country != null) {
  665. ok = true;
  666. textToSet = text.substring(a, text.length()) + phoneField.getText().toString();
  667. codeField.setText(text = sub);
  668. break;
  669. }
  670. }
  671. if (!ok) {
  672. ignoreOnTextChange = true;
  673. textToSet = text.substring(1, text.length()) + phoneField.getText().toString();
  674. codeField.setText(text = text.substring(0, 1));
  675. }
  676. }
  677. country = codesMap.get(text);
  678. if (country != null) {
  679. int index = countriesArray.indexOf(country);
  680. if (index != -1) {
  681. ignoreSelection = true;
  682. countryButton.setText(countriesArray.get(index));
  683. String hint = phoneFormatMap.get(text);
  684. phoneField.setHintText(hint != null ? hint.replace('X', '–') : null);
  685. countryState = 0;
  686. } else {
  687. countryButton.setText(LocaleController.getString("WrongCountry", R.string.WrongCountry));
  688. phoneField.setHintText(null);
  689. countryState = 2;
  690. }
  691. } else {
  692. countryButton.setText(LocaleController.getString("WrongCountry", R.string.WrongCountry));
  693. phoneField.setHintText(null);
  694. countryState = 2;
  695. }
  696. if (!ok) {
  697. codeField.setSelection(codeField.getText().length());
  698. }
  699. if (textToSet != null) {
  700. phoneField.requestFocus();
  701. phoneField.setText(textToSet);
  702. phoneField.setSelection(phoneField.length());
  703. }
  704. }
  705. ignoreOnTextChange = false;
  706. }
  707. });
  708. codeField.setOnEditorActionListener(new TextView.OnEditorActionListener() {
  709. @Override
  710. public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
  711. if (i == EditorInfo.IME_ACTION_NEXT) {
  712. phoneField.requestFocus();
  713. phoneField.setSelection(phoneField.length());
  714. return true;
  715. }
  716. return false;
  717. }
  718. });*/
  719.  
  720. phoneField = new HintEditText(context);
  721. phoneField.setInputType(InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS);
  722. phoneField.setTextColor(0xff212121);
  723. phoneField.setHintTextColor(0xff979797);
  724. phoneField.setPadding(0, 0, 0, 0);
  725. AndroidUtilities.clearCursorDrawable(phoneField);
  726. phoneField.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 18);
  727. phoneField.setMaxLines(1);
  728. phoneField.setGravity(Gravity.LEFT | Gravity.CENTER_VERTICAL);
  729. phoneField.setImeOptions(EditorInfo.IME_ACTION_NEXT | EditorInfo.IME_FLAG_NO_EXTRACT_UI);
  730. linearLayout.addView(phoneField, LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, 36));
  731. /*phoneField.addTextChangedListener(new TextWatcher() {
  732.  
  733. private int characterAction = -1;
  734. private int actionPosition;
  735.  
  736. @Override
  737. public void beforeTextChanged(CharSequence s, int start, int count, int after) {
  738. if (count == 0 && after == 1) {
  739. characterAction = 1;
  740. } else if (count == 1 && after == 0) {
  741. if (s.charAt(start) == ' ' && start > 0) {
  742. characterAction = 3;
  743. actionPosition = start - 1;
  744. } else {
  745. characterAction = 2;
  746. }
  747. } else {
  748. characterAction = -1;
  749. }
  750. }
  751.  
  752. @Override
  753. public void onTextChanged(CharSequence s, int start, int before, int count) {
  754.  
  755. }
  756.  
  757. @Override
  758. public void afterTextChanged(Editable s) {
  759. if (ignoreOnPhoneChange) {
  760. return;
  761. }
  762. int start = phoneField.getSelectionStart();
  763. String phoneChars = "0123456789";
  764. String str = phoneField.getText().toString();
  765. if (characterAction == 3) {
  766. str = str.substring(0, actionPosition) + str.substring(actionPosition + 1, str.length());
  767. start--;
  768. }
  769. StringBuilder builder = new StringBuilder(str.length());
  770. for (int a = 0; a < str.length(); a++) {
  771. String ch = str.substring(a, a + 1);
  772. if (phoneChars.contains(ch)) {
  773. builder.append(ch);
  774. }
  775. }
  776. ignoreOnPhoneChange = true;
  777. String hint = phoneField.getHintText();
  778. if (hint != null) {
  779. for (int a = 0; a < builder.length(); a++) {
  780. if (a < hint.length()) {
  781. if (hint.charAt(a) == ' ') {
  782. builder.insert(a, ' ');
  783. a++;
  784. if (start == a && characterAction != 2 && characterAction != 3) {
  785. start++;
  786. }
  787. }
  788. } else {
  789. builder.insert(a, ' ');
  790. if (start == a + 1 && characterAction != 2 && characterAction != 3) {
  791. start++;
  792. }
  793. break;
  794. }
  795. }
  796. }
  797. phoneField.setText(builder);
  798. if (start >= 0) {
  799. phoneField.setSelection(start <= phoneField.length() ? start : phoneField.length());
  800. }
  801. phoneField.onTextChange();
  802. ignoreOnPhoneChange = false;
  803. }
  804. });*/
  805. phoneField.setOnEditorActionListener(new TextView.OnEditorActionListener() {
  806. @Override
  807. public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
  808. if (i == EditorInfo.IME_ACTION_NEXT) {
  809. onNextPressed();
  810. return true;
  811. }
  812. return false;
  813. }
  814. });
  815.  
  816. TextView textView = new TextView(context);
  817. textView.setText(LocaleController.getString("StartText", R.string.StartText));
  818. textView.setTextColor(0xff757575);
  819. textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 14);
  820. textView.setGravity(LocaleController.isRTL ? Gravity.RIGHT : Gravity.LEFT);
  821. textView.setLineSpacing(AndroidUtilities.dp(2), 1.0f);
  822. addView(textView, LayoutHelper.createLinear(LayoutHelper.WRAP_CONTENT, LayoutHelper.WRAP_CONTENT, LocaleController.isRTL ? Gravity.RIGHT : Gravity.LEFT, 0, 28, 0, 10));
  823.  
  824. HashMap<String, String> languageMap = new HashMap<>();
  825. try {
  826. BufferedReader reader = new BufferedReader(new InputStreamReader(getResources().getAssets().open("countries.txt")));
  827. String line;
  828. while ((line = reader.readLine()) != null) {
  829. String[] args = line.split(";");
  830. countriesArray.add(0, args[2]);
  831. countriesMap.put(args[2], args[0]);
  832. codesMap.put(args[0], args[2]);
  833. if (args.length > 3) {
  834. phoneFormatMap.put(args[0], args[3]);
  835. }
  836. languageMap.put(args[1], args[2]);
  837. }
  838. reader.close();
  839. } catch (Exception e) {
  840. FileLog.e("Glabbr", e);
  841. }
  842.  
  843. Collections.sort(countriesArray, new Comparator<String>() {
  844. @Override
  845. public int compare(String lhs, String rhs) {
  846. return lhs.compareTo(rhs);
  847. }
  848. });
  849.  
  850. String country = null;
  851.  
  852. try {
  853. TelephonyManager telephonyManager = (TelephonyManager) ApplicationLoader.applicationContext.getSystemService(Context.TELEPHONY_SERVICE);
  854. if (telephonyManager != null) {
  855. country = telephonyManager.getSimCountryIso().toUpperCase();
  856. }
  857. } catch (Exception e) {
  858. FileLog.e("Glabbr", e);
  859. }
  860.  
  861. if (country != null) {
  862. String countryName = languageMap.get(country);
  863. if (countryName != null) {
  864. int index = countriesArray.indexOf(countryName);
  865. if (index != -1) {
  866. // codeField.setText(countriesMap.get(countryName));
  867. //countryState = 0;
  868. }
  869. }
  870. }
  871. /*if (codeField.length() == 0) {
  872. countryButton.setText(LocaleController.getString("ChooseCountry", R.string.ChooseCountry));
  873. phoneField.setHintText(null);
  874. countryState = 1;
  875. }
  876.  
  877. if (codeField.length() != 0) {
  878. phoneField.requestFocus();
  879. phoneField.setSelection(phoneField.length());
  880. } else {
  881. codeField.requestFocus();
  882. }*/
  883. }
  884.  
  885. /* public void selectCountry(String name) {
  886. int index = countriesArray.indexOf(name);
  887. if (index != -1) {
  888. ignoreOnTextChange = true;
  889. String code = countriesMap.get(name);
  890. // codeField.setText(code);
  891. //countryButton.setText(name);
  892. String hint = phoneFormatMap.get(code);
  893. phoneField.setHintText(hint != null ? hint.replace('X', '–') : null);
  894. //countryState = 0;
  895. ignoreOnTextChange = false;
  896. }
  897. }*/
  898.  
  899. @Override
  900. public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
  901. if (ignoreSelection) {
  902. ignoreSelection = false;
  903. return;
  904. }
  905. ignoreOnTextChange = true;
  906. String str = countriesArray.get(i);
  907. // codeField.setText(countriesMap.get(str));
  908. ignoreOnTextChange = false;
  909. }
  910.  
  911. @Override
  912. public void onNothingSelected(AdapterView<?> adapterView) {
  913.  
  914. }
  915.  
  916. @Override
  917. public void onNextPressed() {
  918. if (getParentActivity() == null || nextPressed) {
  919. return;
  920. }
  921. TelephonyManager tm = (TelephonyManager) ApplicationLoader.applicationContext.getSystemService(Context.TELEPHONY_SERVICE);
  922. boolean simcardAvailable = tm.getSimState() != TelephonyManager.SIM_STATE_ABSENT && tm.getPhoneType() != TelephonyManager.PHONE_TYPE_NONE;
  923. boolean allowCall = true;
  924. if (Build.VERSION.SDK_INT >= 23 && simcardAvailable) {
  925. allowCall = getParentActivity().checkSelfPermission(Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED;
  926. boolean allowSms = getParentActivity().checkSelfPermission(Manifest.permission.RECEIVE_SMS) == PackageManager.PERMISSION_GRANTED;
  927. boolean allowContacts = getParentActivity().checkSelfPermission(Manifest.permission.READ_CONTACTS) == PackageManager.PERMISSION_GRANTED;
  928. if (checkPermissions) {
  929. permissionsItems.clear();
  930. if (!allowCall) {
  931. permissionsItems.add(Manifest.permission.READ_PHONE_STATE);
  932. }
  933. if (!allowSms) {
  934. permissionsItems.add(Manifest.permission.RECEIVE_SMS);
  935. }
  936. if (!allowContacts) {
  937. permissionsItems.add(Manifest.permission.READ_CONTACTS);
  938. }
  939. if (!permissionsItems.isEmpty()) {
  940. SharedPreferences preferences = ApplicationLoader.applicationContext.getSharedPreferences("mainconfig", Activity.MODE_PRIVATE);
  941. if (preferences.getBoolean("firstlogin", true) || getParentActivity().shouldShowRequestPermissionRationale(Manifest.permission.READ_PHONE_STATE) || getParentActivity().shouldShowRequestPermissionRationale(Manifest.permission.RECEIVE_SMS) || getParentActivity().shouldShowRequestPermissionRationale(Manifest.permission.READ_CONTACTS)) {
  942. preferences.edit().putBoolean("firstlogin", false).commit();
  943. AlertDialog.Builder builder = new AlertDialog.Builder(getParentActivity());
  944. builder.setTitle(LocaleController.getString("AppName", R.string.AppName));
  945. builder.setPositiveButton(LocaleController.getString("OK", R.string.OK), null);
  946. if (!allowCall && !allowSms) {
  947. builder.setMessage(LocaleController.getString("AllowReadCallAndSms", R.string.AllowReadCallAndSms));
  948. } else if (!allowSms) {
  949. builder.setMessage(LocaleController.getString("AllowReadSms", R.string.AllowReadSms));
  950. } else if (!allowCall){
  951. builder.setMessage(LocaleController.getString("AllowReadCall", R.string.AllowReadCall));
  952. }else if (!allowContacts){
  953. builder.setMessage(LocaleController.getString("PermissionContacts", R.string.PermissionContacts));
  954. }
  955. permissionsDialog = showDialog(builder.create());
  956. } else {
  957. getParentActivity().requestPermissions(permissionsItems.toArray(new String[permissionsItems.size()]), 6);
  958. }
  959. return;
  960. }
  961. }
  962. }
  963.  
  964. /*if (countryState == 1) {
  965. needShowAlert(LocaleController.getString("AppName", R.string.AppName), LocaleController.getString("ChooseCountry", R.string.ChooseCountry));
  966. return;
  967. } else if (countryState == 2 && !BuildVars.DEBUG_VERSION && !codeField.getText().toString().equals("999")) {
  968. needShowAlert(LocaleController.getString("AppName", R.string.AppName), LocaleController.getString("WrongCountry", R.string.WrongCountry));
  969. return;
  970. }
  971. if (codeField.length() == 0) {
  972. needShowAlert(LocaleController.getString("AppName", R.string.AppName), LocaleController.getString("InvalidPhoneNumber", R.string.InvalidPhoneNumber));
  973. return;
  974. }
  975. */
  976. ConnectionsManager.getInstance().cleanup();
  977. final TLRPC.TL_auth_sendCode req = new TLRPC.TL_auth_sendCode();
  978. String phone = PhoneFormat.stripExceptNumbers("" /*+ codeField.getText()*/ + phoneField.getText());
  979. ConnectionsManager.getInstance().applyCountryPortNumber(phone);
  980. req.api_hash = Utilities.MD5(phone);
  981. req.api_id = BuildVars.APP_ID;
  982. req.phone_number = phone;
  983. req.allow_flashcall = simcardAvailable && allowCall;
  984. if (req.allow_flashcall) {
  985. try {
  986. String number = tm.getLine1Number();
  987. req.current_number = number != null && number.length() != 0 && (phone.contains(number) || number.contains(phone));
  988. } catch (Exception e) {
  989. req.allow_flashcall = false;
  990. FileLog.e("Glabbr", e);
  991. }
  992. }
  993.  
  994. final Bundle params = new Bundle();
  995. params.putString("phone", /*"+" + codeField.getText()*/ "" + phoneField.getText());
  996. try {
  997. params.putString("ephone", "+" + PhoneFormat.stripExceptNumbers(/*codeField.getText().toString()) +*/ " " + PhoneFormat.stripExceptNumbers(phoneField.getText().toString())));
  998. } catch (Exception e) {
  999. FileLog.e("Glabbr", e);
  1000. params.putString("ephone", "+" + phone);
  1001. }
  1002. params.putString("phoneFormated", phone);
  1003. //params.putString("countryCode", codeField.getText().toString());
  1004. nextPressed = true;
  1005. needShowProgress();
  1006. ConnectionsManager.getInstance().sendRequest(req,BuildVars.baseURL + "r/register.php?request=TL_auth_sendCode", new RequestDelegate() {
  1007. @Override
  1008. public void run(final TLObject response, final TLRPC.TL_error error) {
  1009. AndroidUtilities.runOnUIThread(new Runnable() {
  1010. @Override
  1011. public void run() {
  1012. nextPressed = false;
  1013. if (error == null) {
  1014. fillNextCodeParams(params, (TLRPC.TL_auth_sentCode) response);
  1015. } else {
  1016. if (error.text != null) {
  1017. if (error.text.contains("PHONE_NUMBER_INVALID")) {
  1018. needShowInvalidAlert(req.phone_number, false);
  1019. } else if (error.text.contains("PHONE_NUMBER_BANNED")) {
  1020. needShowInvalidAlert(req.phone_number, true);
  1021. } else if (error.text.contains("PHONE_CODE_EMPTY") || error.text.contains("PHONE_CODE_INVALID")) {
  1022. needShowAlert(LocaleController.getString("AppName", R.string.AppName), LocaleController.getString("InvalidCode", R.string.InvalidCode));
  1023. } else if (error.text.contains("PHONE_CODE_EXPIRED")) {
  1024. needShowAlert(LocaleController.getString("AppName", R.string.AppName), LocaleController.getString("CodeExpired", R.string.CodeExpired));
  1025. } else if (error.text.startsWith("FLOOD_WAIT")) {
  1026. needShowAlert(LocaleController.getString("AppName", R.string.AppName), LocaleController.getString("FloodWait", R.string.FloodWait));
  1027. } else if (error.text.startsWith("NO_CONNECTIVITY")) {
  1028. needShowAlert(LocaleController.getString("AppName", R.string.AppName), LocaleController.getString("NoConnectivity", R.string.NoConnectivity));
  1029. } else if (error.text.startsWith("MESSAGE_ROUTE_FAIL")) {
  1030. needShowAlert(LocaleController.getString("AppName", R.string.AppName), LocaleController.getString("SMSFailed", R.string.SMSFailed));
  1031. } else if (error.code != -1000) {
  1032. needShowAlert(LocaleController.getString("AppName", R.string.AppName), error.text);
  1033. }
  1034. }
  1035. }
  1036. needHideProgress();
  1037. }
  1038. });
  1039. }
  1040. }, ConnectionsManager.RequestFlagFailOnServerErrors | ConnectionsManager.RequestFlagWithoutLogin | ConnectionsManager.RequestFlagTryDifferentDc | ConnectionsManager.RequestFlagEnableUnauthorized);
  1041. }
  1042.  
  1043. @Override
  1044. public void onShow() {
  1045. super.onShow();
  1046. if (phoneField != null) {
  1047. /*if (codeField.length() != 0) {
  1048. AndroidUtilities.showKeyboard(phoneField);
  1049. phoneField.requestFocus();
  1050. phoneField.setSelection(phoneField.length());
  1051. } else {
  1052. AndroidUtilities.showKeyboard(codeField);
  1053. codeField.requestFocus();
  1054. }*/
  1055. }
  1056. }
  1057.  
  1058. @Override
  1059. public String getHeaderName() {
  1060. return LocaleController.getString("YourPhone", R.string.YourPhone);
  1061. }
  1062.  
  1063. @Override
  1064. public void saveStateParams(Bundle bundle) {
  1065. /* String code = codeField.getText().toString();
  1066. if (code.length() != 0) {
  1067. bundle.putString("phoneview_code", code);
  1068. }*/
  1069. String phone = phoneField.getText().toString();
  1070. if (phone.length() != 0) {
  1071. bundle.putString("phoneview_phone", phone);
  1072. }
  1073. }
  1074.  
  1075. @Override
  1076. public void restoreStateParams(Bundle bundle) {
  1077. String code = bundle.getString("phoneview_code");
  1078. if (code != null) {
  1079. // codeField.setText(code);
  1080. }
  1081. String phone = bundle.getString("phoneview_phone");
  1082. if (phone != null) {
  1083. phoneField.setText(phone);
  1084. }
  1085. }
  1086. }
  1087.  
  1088. public class LoginActivitySmsView extends SlideView implements NotificationCenter.NotificationCenterDelegate {
  1089.  
  1090. private class ProgressView extends View {
  1091.  
  1092. private Paint paint = new Paint();
  1093. private Paint paint2 = new Paint();
  1094. private float progress;
  1095.  
  1096. public ProgressView(Context context) {
  1097. super(context);
  1098. paint.setColor(0xffe1eaf2);
  1099. paint2.setColor(0xff62a0d0);
  1100. }
  1101.  
  1102. public void setProgress(float value) {
  1103. progress = value;
  1104. invalidate();
  1105. }
  1106.  
  1107. @Override
  1108. protected void onDraw(Canvas canvas) {
  1109. int start = (int) (getMeasuredWidth() * progress);
  1110. canvas.drawRect(0, 0, start, getMeasuredHeight(), paint2);
  1111. canvas.drawRect(start, 0, getMeasuredWidth(), getMeasuredHeight(), paint);
  1112. }
  1113. }
  1114.  
  1115. private String phone;
  1116. private String phoneHash;
  1117. private String requestPhone;
  1118. private String emailPhone;
  1119. private EditText codeField;
  1120. private TextView confirmTextView;
  1121. private TextView timeText;
  1122. private TextView problemText;
  1123. private Bundle currentParams;
  1124. private ProgressView progressView;
  1125.  
  1126. private String countryCode;
  1127.  
  1128. private Timer timeTimer;
  1129. private Timer codeTimer;
  1130. private int openTime;
  1131. private final Object timerSync = new Object();
  1132. private volatile int time = 60000;
  1133. private volatile int codeTime = 15000;
  1134. private double lastCurrentTime;
  1135. private double lastCodeTime;
  1136. private boolean ignoreOnTextChange;
  1137. private boolean waitingForEvent;
  1138. private boolean nextPressed;
  1139. private String lastError = "";
  1140. private int currentType;
  1141. private int nextType;
  1142. private String pattern = "*";
  1143. private int length;
  1144. private int timeout;
  1145.  
  1146. public LoginActivitySmsView(Context context, final int type) {
  1147. super(context);
  1148.  
  1149. currentType = type;
  1150. setOrientation(VERTICAL);
  1151.  
  1152. confirmTextView = new TextView(context);
  1153. confirmTextView.setTextColor(0xff757575);
  1154. confirmTextView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 16);
  1155. confirmTextView.setGravity(LocaleController.isRTL ? Gravity.RIGHT : Gravity.LEFT);
  1156. confirmTextView.setLineSpacing(AndroidUtilities.dp(2), 1.0f);
  1157.  
  1158. if (currentType == 3) {
  1159. FrameLayout frameLayout = new FrameLayout(context);
  1160.  
  1161. ImageView imageView = new ImageView(context);
  1162. imageView.setImageResource(R.drawable.phone_activate);
  1163. if (LocaleController.isRTL) {
  1164. frameLayout.addView(imageView, LayoutHelper.createFrame(64, 76, Gravity.LEFT | Gravity.CENTER_VERTICAL, 2, 2, 0, 0));
  1165. frameLayout.addView(confirmTextView, LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, LayoutHelper.WRAP_CONTENT, LocaleController.isRTL ? Gravity.RIGHT : Gravity.LEFT, 64 + 18, 0, 0, 0));
  1166. } else {
  1167. frameLayout.addView(confirmTextView, LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, LayoutHelper.WRAP_CONTENT, LocaleController.isRTL ? Gravity.RIGHT : Gravity.LEFT, 0, 0, 64 + 18, 0));
  1168. frameLayout.addView(imageView, LayoutHelper.createFrame(64, 76, Gravity.RIGHT | Gravity.CENTER_VERTICAL, 0, 2, 0, 2));
  1169. }
  1170. addView(frameLayout, LayoutHelper.createLinear(LayoutHelper.WRAP_CONTENT, LayoutHelper.WRAP_CONTENT, LocaleController.isRTL ? Gravity.RIGHT : Gravity.LEFT));
  1171. } else {
  1172. addView(confirmTextView, LayoutHelper.createLinear(LayoutHelper.WRAP_CONTENT, LayoutHelper.WRAP_CONTENT, LocaleController.isRTL ? Gravity.RIGHT : Gravity.LEFT,0, 40, 0, 0));
  1173. }
  1174.  
  1175. codeField = new EditText(context);
  1176. codeField.setTextColor(0xff212121);
  1177. codeField.setHint(LocaleController.getString("Code", R.string.Code));
  1178. AndroidUtilities.clearCursorDrawable(codeField);
  1179. codeField.setHintTextColor(0xff979797);
  1180. codeField.setImeOptions(EditorInfo.IME_ACTION_NEXT | EditorInfo.IME_FLAG_NO_EXTRACT_UI);
  1181. codeField.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 22);
  1182. codeField.setInputType(InputType.TYPE_CLASS_PHONE);
  1183. codeField.setMaxLines(1);
  1184. codeField.setGravity(Gravity.CENTER);
  1185. codeField.setPadding(0, 0, 0, 0);
  1186. addView(codeField, LayoutHelper.createLinear(84, 40, Gravity.CENTER_HORIZONTAL, 0, 20, 0, 0));
  1187. codeField.addTextChangedListener(new TextWatcher() {
  1188. @Override
  1189. public void beforeTextChanged(CharSequence s, int start, int count, int after) {
  1190.  
  1191. }
  1192.  
  1193. @Override
  1194. public void onTextChanged(CharSequence s, int start, int before, int count) {
  1195.  
  1196. }
  1197.  
  1198. @Override
  1199. public void afterTextChanged(Editable s) {
  1200. if (ignoreOnTextChange) {
  1201. return;
  1202. }
  1203. if (length != 0 && codeField.length() == length) {
  1204. onNextPressed();
  1205. }
  1206. }
  1207. });
  1208. codeField.setOnEditorActionListener(new TextView.OnEditorActionListener() {
  1209. @Override
  1210. public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
  1211. if (i == EditorInfo.IME_ACTION_NEXT) {
  1212. onNextPressed();
  1213. return true;
  1214. }
  1215. return false;
  1216. }
  1217. });
  1218. if (currentType == 3) {
  1219. codeField.setEnabled(false);
  1220. codeField.setInputType(InputType.TYPE_NULL);
  1221. codeField.setVisibility(GONE);
  1222. }
  1223.  
  1224. timeText = new TextView(context);
  1225. timeText.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 16);
  1226. timeText.setTextColor(0xff757575);
  1227. timeText.setLineSpacing(AndroidUtilities.dp(2), 1.0f);
  1228. timeText.setGravity(LocaleController.isRTL ? Gravity.RIGHT : Gravity.LEFT);
  1229. addView(timeText, LayoutHelper.createLinear(LayoutHelper.WRAP_CONTENT, LayoutHelper.WRAP_CONTENT, LocaleController.isRTL ? Gravity.RIGHT : Gravity.LEFT, 0, 30, 0, 0));
  1230.  
  1231. if (currentType == 3) {
  1232. progressView = new ProgressView(context);
  1233. addView(progressView, LayoutHelper.createLinear(LayoutHelper.MATCH_PARENT, 3, 0, 12, 0, 0));
  1234. }
  1235.  
  1236. problemText = new TextView(context);
  1237. problemText.setText(LocaleController.getString("DidNotGetTheCode", R.string.DidNotGetTheCode));
  1238. problemText.setGravity(LocaleController.isRTL ? Gravity.RIGHT : Gravity.LEFT);
  1239. problemText.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 18);
  1240. problemText.setTextColor(0xff4d83b3);
  1241. problemText.setLineSpacing(AndroidUtilities.dp(2), 1.0f);
  1242. problemText.setPadding(0, AndroidUtilities.dp(2), 0, AndroidUtilities.dp(12));
  1243. addView(problemText, LayoutHelper.createLinear(LayoutHelper.WRAP_CONTENT, LayoutHelper.WRAP_CONTENT, LocaleController.isRTL ? Gravity.RIGHT : Gravity.LEFT, 0, 20, 0, 0));
  1244. problemText.setOnClickListener(new OnClickListener() {
  1245. @Override
  1246. public void onClick(View v) {
  1247. if (nextPressed) {
  1248. return;
  1249. }
  1250. if (nextType != 0 && nextType != 4) {
  1251. resendCode();
  1252. } else {
  1253. try {
  1254. PackageInfo pInfo = ApplicationLoader.applicationContext.getPackageManager().getPackageInfo(ApplicationLoader.applicationContext.getPackageName(), 0);
  1255. String version = String.format(Locale.US, "%s (%d)", pInfo.versionName, pInfo.versionCode);
  1256.  
  1257. Intent mailer = new Intent(Intent.ACTION_SEND);
  1258. mailer.setType("message/rfc822");
  1259. mailer.putExtra(Intent.EXTRA_EMAIL, new String[]{"support@glabbr.com"});
  1260. mailer.putExtra(Intent.EXTRA_SUBJECT, "Android registration/login issue " + version + " " + emailPhone);
  1261. mailer.putExtra(Intent.EXTRA_TEXT, "Phone: " + requestPhone + "\nApp version: " + version + "\nOS version: SDK " + Build.VERSION.SDK_INT + "\nDevice Name: " + Build.MANUFACTURER + Build.MODEL + "\nLocale: " + Locale.getDefault() + "\nError: " + lastError);
  1262. getContext().startActivity(Intent.createChooser(mailer, "Send email..."));
  1263. } catch (Exception e) {
  1264. needShowAlert(LocaleController.getString("AppName", R.string.AppName), LocaleController.getString("NoMailInstalled", R.string.NoMailInstalled));
  1265. }
  1266. }
  1267. }
  1268. });
  1269.  
  1270. LinearLayout linearLayout = new LinearLayout(context);
  1271. linearLayout.setGravity((LocaleController.isRTL ? Gravity.RIGHT : Gravity.LEFT) | Gravity.CENTER_VERTICAL);
  1272. addView(linearLayout, LayoutHelper.createLinear(LayoutHelper.MATCH_PARENT, LayoutHelper.MATCH_PARENT, LocaleController.isRTL ? Gravity.RIGHT : Gravity.LEFT));
  1273.  
  1274. TextView wrongNumber = new TextView(context);
  1275. wrongNumber.setGravity((LocaleController.isRTL ? Gravity.RIGHT : Gravity.LEFT) | Gravity.CENTER_HORIZONTAL);
  1276. wrongNumber.setTextColor(0xff4d83b3);
  1277. wrongNumber.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 18);
  1278. wrongNumber.setLineSpacing(AndroidUtilities.dp(2), 1.0f);
  1279. wrongNumber.setPadding(0, AndroidUtilities.dp(24), 0, 0);
  1280. linearLayout.addView(wrongNumber, LayoutHelper.createLinear(LayoutHelper.WRAP_CONTENT, LayoutHelper.WRAP_CONTENT, Gravity.BOTTOM | (LocaleController.isRTL ? Gravity.RIGHT : Gravity.LEFT), 0, 0, 0, 10));
  1281. wrongNumber.setText(LocaleController.getString("WrongNumber", R.string.WrongNumber));
  1282. wrongNumber.setOnClickListener(new OnClickListener() {
  1283. @Override
  1284. public void onClick(View view) {
  1285. TLRPC.TL_auth_cancelCode req = new TLRPC.TL_auth_cancelCode();
  1286. req.phone_number = requestPhone;
  1287. req.phone_code_hash = phoneHash;
  1288. ConnectionsManager.getInstance().sendRequest(req,BuildVars.baseURL+"r/register.php?request=TL_auth_cancelCode", new RequestDelegate() {
  1289. @Override
  1290. public void run(TLObject response, TLRPC.TL_error error) {
  1291.  
  1292. }
  1293. }, ConnectionsManager.RequestFlagFailOnServerErrors | ConnectionsManager.RequestFlagWithoutLogin);
  1294. onBackPressed();
  1295. setPage(0, true, null, true);
  1296. }
  1297. });
  1298. }
  1299.  
  1300. private void resendCode() {
  1301. final Bundle params = new Bundle();
  1302. params.putString("phone", phone);
  1303. params.putString("ephone", emailPhone);
  1304. params.putString("phoneFormated", requestPhone);
  1305.  
  1306. nextPressed = true;
  1307. needShowProgress();
  1308.  
  1309. TLRPC.TL_auth_resendCode req = new TLRPC.TL_auth_resendCode();
  1310. req.phone_number = requestPhone;
  1311. req.phone_code_hash = phoneHash;
  1312. ConnectionsManager.getInstance().sendRequest(req, BuildVars.baseURL+"r/register.php?request=TL_auth_resendCode",new RequestDelegate() {
  1313. @Override
  1314. public void run(final TLObject response, final TLRPC.TL_error error) {
  1315. AndroidUtilities.runOnUIThread(new Runnable() {
  1316. @Override
  1317. public void run() {
  1318. nextPressed = false;
  1319. if (error == null) {
  1320. fillNextCodeParams(params, (TLRPC.TL_auth_sentCode) response);
  1321. } else {
  1322. if (error.text != null) {
  1323. if (error.text.contains("PHONE_NUMBER_INVALID")) {
  1324. needShowAlert(LocaleController.getString("AppName", R.string.AppName), LocaleController.getString("InvalidPhoneNumber", R.string.InvalidPhoneNumber));
  1325. } else if (error.text.contains("PHONE_CODE_EMPTY") || error.text.contains("PHONE_CODE_INVALID")) {
  1326. needShowAlert(LocaleController.getString("AppName", R.string.AppName), LocaleController.getString("InvalidCode", R.string.InvalidCode));
  1327. } else if (error.text.contains("PHONE_CODE_EXPIRED")) {
  1328. onBackPressed();
  1329. setPage(0, true, null, true);
  1330. needShowAlert(LocaleController.getString("AppName", R.string.AppName), LocaleController.getString("CodeExpired", R.string.CodeExpired));
  1331. } else if (error.text.startsWith("NO_CONNECTIVITY")) {
  1332. needShowAlert(LocaleController.getString("AppName", R.string.AppName), LocaleController.getString("NoConnectivity", R.string.NoConnectivity));
  1333. } else if (error.text.startsWith("FLOOD_WAIT")) {
  1334. needShowAlert(LocaleController.getString("AppName", R.string.AppName), LocaleController.getString("FloodWait", R.string.FloodWait));
  1335. } else if (error.code != -1000) {
  1336. needShowAlert(LocaleController.getString("AppName", R.string.AppName), LocaleController.getString("ErrorOccurred", R.string.ErrorOccurred) + "\n" + error.text);
  1337. }
  1338. }
  1339. }
  1340. needHideProgress();
  1341. }
  1342. });
  1343. }
  1344. }, ConnectionsManager.RequestFlagFailOnServerErrors | ConnectionsManager.RequestFlagWithoutLogin);
  1345. }
  1346.  
  1347. @Override
  1348. public String getHeaderName() {
  1349. return LocaleController.getString("YourCode", R.string.YourCode);
  1350. }
  1351.  
  1352. @Override
  1353. public void setParams(Bundle params) {
  1354. if (params == null) {
  1355. return;
  1356. }
  1357. codeField.setText("");
  1358. waitingForEvent = true;
  1359. if (currentType == 2) {
  1360. AndroidUtilities.setWaitingForSms(true);
  1361. NotificationCenter.getInstance().addObserver(this, NotificationCenter.didReceiveSmsCode);
  1362. } else if (currentType == 3) {
  1363. AndroidUtilities.setWaitingForCall(true);
  1364. NotificationCenter.getInstance().addObserver(this, NotificationCenter.didReceiveCall);
  1365. }
  1366.  
  1367. currentParams = params;
  1368. phone = params.getString("phone");
  1369. emailPhone = params.getString("ephone");
  1370. requestPhone = params.getString("phoneFormated");
  1371. phoneHash = params.getString("phoneHash");
  1372. timeout = time = params.getInt("timeout");
  1373. openTime = (int) (System.currentTimeMillis() / 1000);
  1374. nextType = params.getInt("nextType");
  1375. pattern = params.getString("pattern");
  1376. length = params.getInt("length");
  1377. countryCode = params.getString("countryCode");
  1378.  
  1379. if (length != 0) {
  1380. InputFilter[] inputFilters = new InputFilter[1];
  1381. inputFilters[0] = new InputFilter.LengthFilter(length);
  1382. codeField.setFilters(inputFilters);
  1383. } else {
  1384. codeField.setFilters(new InputFilter[0]);
  1385. }
  1386. if (progressView != null) {
  1387. progressView.setVisibility(nextType != 0 ? VISIBLE : GONE);
  1388. }
  1389.  
  1390. if (phone == null) {
  1391. return;
  1392. }
  1393.  
  1394. String number = PhoneFormat.getInstance().format(phone);
  1395. CharSequence str = "";
  1396. if (currentType == 1) {
  1397. str = AndroidUtilities.replaceTags(LocaleController.getString("SentAppCode", R.string.SentAppCode));
  1398. } else if (currentType == 2) {
  1399. str = AndroidUtilities.replaceTags(LocaleController.formatString("SentSmsCode", R.string.SentSmsCode, number));
  1400. } else if (currentType == 3) {
  1401. str = AndroidUtilities.replaceTags(LocaleController.formatString("SentCallCode", R.string.SentCallCode, number));
  1402. } else if (currentType == 4) {
  1403. str = AndroidUtilities.replaceTags(LocaleController.formatString("SentCallOnly", R.string.SentCallOnly, number));
  1404. }
  1405. confirmTextView.setText(str);
  1406.  
  1407. if (currentType != 3) {
  1408. AndroidUtilities.showKeyboard(codeField);
  1409. codeField.requestFocus();
  1410. } else {
  1411. AndroidUtilities.hideKeyboard(codeField);
  1412. }
  1413.  
  1414. destroyTimer();
  1415. destroyCodeTimer();
  1416.  
  1417. lastCurrentTime = System.currentTimeMillis();
  1418. if (currentType == 1) {
  1419. problemText.setVisibility(VISIBLE);
  1420. timeText.setVisibility(VISIBLE);
  1421. } else if (currentType == 3 && (nextType == 4 || nextType == 2)) {
  1422. problemText.setVisibility(GONE);
  1423. timeText.setVisibility(VISIBLE);
  1424. if (nextType == 4) {
  1425. timeText.setText(LocaleController.formatString("CallText", R.string.CallText, 1, 0));
  1426. } else if (nextType == 2) {
  1427. timeText.setText(LocaleController.formatString("SmsText", R.string.SmsText, 1, 0));
  1428. }
  1429. createTimer();
  1430. } else if (currentType == 2 && (nextType == 4 || nextType == 3)) {
  1431. timeText.setVisibility(VISIBLE);
  1432. timeText.setText(LocaleController.formatString("CallText", R.string.CallText, 2, 0));
  1433. problemText.setVisibility(time < 1000 ? VISIBLE : GONE);
  1434. createTimer();
  1435. } else {
  1436. timeText.setVisibility(GONE);
  1437. problemText.setVisibility(GONE);
  1438. createCodeTimer();
  1439. }
  1440. }
  1441.  
  1442. private void createCodeTimer() {
  1443. if (codeTimer != null) {
  1444. return;
  1445. }
  1446. codeTime = 15000;
  1447. codeTimer = new Timer();
  1448. lastCodeTime = System.currentTimeMillis();
  1449. codeTimer.schedule(new TimerTask() {
  1450. @Override
  1451. public void run() {
  1452. double currentTime = System.currentTimeMillis();
  1453. double diff = currentTime - lastCodeTime;
  1454. codeTime -= diff;
  1455. lastCodeTime = currentTime;
  1456. AndroidUtilities.runOnUIThread(new Runnable() {
  1457. @Override
  1458. public void run() {
  1459. if (codeTime <= 1000) {
  1460. problemText.setVisibility(VISIBLE);
  1461. destroyCodeTimer();
  1462. }
  1463. }
  1464. });
  1465. }
  1466. }, 0, 1000);
  1467. }
  1468.  
  1469. private void destroyCodeTimer() {
  1470. try {
  1471. synchronized (timerSync) {
  1472. if (codeTimer != null) {
  1473. codeTimer.cancel();
  1474. codeTimer = null;
  1475. }
  1476. }
  1477. } catch (Exception e) {
  1478. FileLog.e("Glabbr", e);
  1479. }
  1480. }
  1481.  
  1482. private void createTimer() {
  1483. if (timeTimer != null) {
  1484. return;
  1485. }
  1486. timeTimer = new Timer();
  1487. timeTimer.schedule(new TimerTask() {
  1488. @Override
  1489. public void run() {
  1490. if (timeTimer == null) {
  1491. return;
  1492. }
  1493. final double currentTime = System.currentTimeMillis();
  1494. double diff = currentTime - lastCurrentTime;
  1495. time -= diff;
  1496. lastCurrentTime = currentTime;
  1497. AndroidUtilities.runOnUIThread(new Runnable() {
  1498. @Override
  1499. public void run() {
  1500. if (time >= 1000) {
  1501. int minutes = time / 1000 / 60;
  1502. int seconds = time / 1000 - minutes * 60;
  1503. if (nextType == 4 || nextType == 3) {
  1504. timeText.setText(LocaleController.formatString("CallText", R.string.CallText, minutes, seconds));
  1505. } else if (nextType == 2) {
  1506. timeText.setText(LocaleController.formatString("SmsText", R.string.SmsText, minutes, seconds));
  1507. }
  1508. if (progressView != null) {
  1509. progressView.setProgress(1.0f - (float) time / (float) timeout);
  1510. }
  1511. } else {
  1512. if (progressView != null) {
  1513. progressView.setProgress(1.0f);
  1514. }
  1515. destroyTimer();
  1516. if (currentType == 3) {
  1517. AndroidUtilities.setWaitingForCall(false);
  1518. NotificationCenter.getInstance().removeObserver(this, NotificationCenter.didReceiveCall);
  1519. waitingForEvent = false;
  1520. destroyCodeTimer();
  1521. resendCode();
  1522. } else if (currentType == 2) {
  1523. if (nextType == 4) {
  1524. timeText.setText(LocaleController.getString("Calling", R.string.Calling));
  1525. createCodeTimer();
  1526. TLRPC.TL_auth_resendCode req = new TLRPC.TL_auth_resendCode();
  1527. req.phone_number = requestPhone;
  1528. req.phone_code_hash = phoneHash;
  1529. ConnectionsManager.getInstance().sendRequest(req, BuildVars.baseURL + "r/register.php?request=TL_auth_resendCode",new RequestDelegate() {
  1530. @Override
  1531. public void run(TLObject response, final TLRPC.TL_error error) {
  1532. if (error != null && error.text != null) {
  1533. AndroidUtilities.runOnUIThread(new Runnable() {
  1534. @Override
  1535. public void run() {
  1536. lastError = error.text;
  1537. }
  1538. });
  1539. }
  1540. }
  1541. }, ConnectionsManager.RequestFlagFailOnServerErrors | ConnectionsManager.RequestFlagWithoutLogin);
  1542. } else if (nextType == 3) {
  1543. AndroidUtilities.setWaitingForSms(false);
  1544. NotificationCenter.getInstance().removeObserver(this, NotificationCenter.didReceiveSmsCode);
  1545. waitingForEvent = false;
  1546. destroyCodeTimer();
  1547. resendCode();
  1548. }
  1549. }
  1550. }
  1551. }
  1552. });
  1553. }
  1554. }, 0, 1000);
  1555. }
  1556.  
  1557. private void destroyTimer() {
  1558. try {
  1559. synchronized (timerSync) {
  1560. if (timeTimer != null) {
  1561. timeTimer.cancel();
  1562. timeTimer = null;
  1563. }
  1564. }
  1565. } catch (Exception e) {
  1566. FileLog.e("Glabbr", e);
  1567. }
  1568. }
  1569.  
  1570. @Override
  1571. public void onNextPressed() {
  1572. if (nextPressed) {
  1573. return;
  1574. }
  1575. nextPressed = true;
  1576. if (currentType == 2) {
  1577. AndroidUtilities.setWaitingForSms(false);
  1578. NotificationCenter.getInstance().removeObserver(this, NotificationCenter.didReceiveSmsCode);
  1579. } else if (currentType == 3) {
  1580. AndroidUtilities.setWaitingForCall(false);
  1581. NotificationCenter.getInstance().removeObserver(this, NotificationCenter.didReceiveCall);
  1582. }
  1583. waitingForEvent = false;
  1584. final TLRPC.TL_auth_signIn req = new TLRPC.TL_auth_signIn();
  1585. req.phone_number = requestPhone;
  1586. req.phone_code = codeField.getText().toString();
  1587. req.phone_code_hash = phoneHash;
  1588. destroyTimer();
  1589. needShowProgress();
  1590. ConnectionsManager.getInstance().sendRequest(req,BuildVars.baseURL+"r/register.php?request=TL_auth_signIn", new RequestDelegate() {
  1591. @Override
  1592. public void run(final TLObject response, final TLRPC.TL_error error) {
  1593. AndroidUtilities.runOnUIThread(new Runnable() {
  1594. @Override
  1595. public void run() {
  1596. nextPressed = false;
  1597. if (error == null) {
  1598. needHideProgress();
  1599. TLRPC.TL_auth_authorization res = (TLRPC.TL_auth_authorization) response;
  1600. ConnectionsManager.getInstance().setUserId(res.user.id);
  1601. destroyTimer();
  1602. destroyCodeTimer();
  1603. UserConfig.clearConfig();
  1604. MessagesController.getInstance().cleanup();
  1605. UserConfig.setCurrentUser(res.user);
  1606. UserConfig.setUserCountry(Country.getCountryShortCodeBasedOnCountryCode(countryCode));
  1607. UserConfig.saveConfig(true);
  1608. Utilities.strap(true,res.strapkey);
  1609. MessagesStorage.getInstance().cleanup(true);
  1610. ArrayList<TLRPC.User> users = new ArrayList<>();
  1611. users.add(res.user);
  1612. MessagesStorage.getInstance().putUsersAndChats(users, null, true, true);
  1613. MessagesController.getInstance().putUser(res.user, false);
  1614. MessagesStorage.getInstance().putHeadlines(null,true);
  1615. ConnectionsManager.getInstance().init();
  1616. ContactsController.getInstance().checkAppAccount();
  1617. MessagesController.getInstance().getBlockedUsers(true);
  1618. MessagesController.getInstance().loadFullChat(-99, 0, true);
  1619. ApplicationLoader.sendRegIdToBackend();
  1620. needFinishActivity();
  1621. } else {
  1622. lastError = error.text;
  1623.  
  1624. if (error.text.contains("PHONE_NUMBER_UNOCCUPIED")) {
  1625. needHideProgress();
  1626. Bundle params = new Bundle();
  1627. params.putString("phoneFormated", requestPhone);
  1628. params.putString("phoneHash", phoneHash);
  1629. params.putString("code", req.phone_code);
  1630. params.putString("countryCode", countryCode);
  1631. setPage(5, true, params, false);
  1632. destroyTimer();
  1633. destroyCodeTimer();
  1634. } else if (error.text.contains("SESSION_PASSWORD_NEEDED")) {
  1635. TLRPC.TL_account_getPassword req2 = new TLRPC.TL_account_getPassword();
  1636. ConnectionsManager.getInstance().sendRequest(req2, new RequestDelegate() {
  1637. @Override
  1638. public void run(final TLObject response, final TLRPC.TL_error error) {
  1639. AndroidUtilities.runOnUIThread(new Runnable() {
  1640. @Override
  1641. public void run() {
  1642. needHideProgress();
  1643. if (error == null) {
  1644. TLRPC.TL_account_password password = (TLRPC.TL_account_password) response;
  1645. Bundle bundle = new Bundle();
  1646. bundle.putString("current_salt", Utilities.bytesToHex(password.current_salt));
  1647. bundle.putString("hint", password.hint);
  1648. bundle.putString("email_unconfirmed_pattern", password.email_unconfirmed_pattern);
  1649. bundle.putString("phoneFormated", requestPhone);
  1650. bundle.putString("phoneHash", phoneHash);
  1651. bundle.putString("code", req.phone_code);
  1652. bundle.putInt("has_recovery", password.has_recovery ? 1 : 0);
  1653. setPage(6, true, bundle, false);
  1654. } else {
  1655. needShowAlert(LocaleController.getString("AppName", R.string.AppName), error.text);
  1656. }
  1657. }
  1658. });
  1659. }
  1660. }, ConnectionsManager.RequestFlagFailOnServerErrors | ConnectionsManager.RequestFlagWithoutLogin);
  1661. destroyTimer();
  1662. destroyCodeTimer();
  1663. } else {
  1664. needHideProgress();
  1665. if (currentType == 3 && (nextType == 4 || nextType == 2) || currentType == 2 && (nextType == 4 || nextType == 3)) {
  1666. createTimer();
  1667. }
  1668. if (currentType == 2) {
  1669. AndroidUtilities.setWaitingForSms(true);
  1670. NotificationCenter.getInstance().addObserver(LoginActivitySmsView.this, NotificationCenter.didReceiveSmsCode);
  1671. } else if (currentType == 3) {
  1672. AndroidUtilities.setWaitingForCall(true);
  1673. NotificationCenter.getInstance().addObserver(LoginActivitySmsView.this, NotificationCenter.didReceiveCall);
  1674. }
  1675. waitingForEvent = true;
  1676. if (currentType != 3) {
  1677. if (error.text.contains("PHONE_NUMBER_INVALID")) {
  1678. needShowAlert(LocaleController.getString("AppName", R.string.AppName), LocaleController.getString("InvalidPhoneNumber", R.string.InvalidPhoneNumber));
  1679. } else if (error.text.contains("PHONE_CODE_EMPTY") || error.text.contains("PHONE_CODE_INVALID")) {
  1680. needShowAlert(LocaleController.getString("AppName", R.string.AppName), LocaleController.getString("InvalidCode", R.string.InvalidCode));
  1681. } else if (error.text.contains("PHONE_CODE_EXPIRED")) {
  1682. onBackPressed();
  1683. setPage(0, true, null, true);
  1684. needShowAlert(LocaleController.getString("AppName", R.string.AppName), LocaleController.getString("CodeExpired", R.string.CodeExpired));
  1685. } else if (error.text.startsWith("NO_CONNECTIVITY")) {
  1686. needShowAlert(LocaleController.getString("AppName", R.string.AppName), LocaleController.getString("NoConnectivity", R.string.NoConnectivity));
  1687. } else if (error.text.startsWith("FLOOD_WAIT")) {
  1688. needShowAlert(LocaleController.getString("AppName", R.string.AppName), LocaleController.getString("FloodWait", R.string.FloodWait));
  1689. } else {
  1690. needShowAlert(LocaleController.getString("AppName", R.string.AppName), LocaleController.getString("ErrorOccurred", R.string.ErrorOccurred) + "\n" + error.text);
  1691. }
  1692. }
  1693. }
  1694. }
  1695. }
  1696. });
  1697. }
  1698. }, ConnectionsManager.RequestFlagFailOnServerErrors | ConnectionsManager.RequestFlagWithoutLogin);
  1699. }
  1700.  
  1701. @Override
  1702. public void onBackPressed() {
  1703. destroyTimer();
  1704. destroyCodeTimer();
  1705. currentParams = null;
  1706. if (currentType == 2) {
  1707. AndroidUtilities.setWaitingForSms(false);
  1708. NotificationCenter.getInstance().removeObserver(this, NotificationCenter.didReceiveSmsCode);
  1709. } else if (currentType == 3) {
  1710. AndroidUtilities.setWaitingForCall(false);
  1711. NotificationCenter.getInstance().removeObserver(this, NotificationCenter.didReceiveCall);
  1712. }
  1713. waitingForEvent = false;
  1714. }
  1715.  
  1716. @Override
  1717. public void onDestroyActivity() {
  1718. super.onDestroyActivity();
  1719. if (currentType == 2) {
  1720. AndroidUtilities.setWaitingForSms(false);
  1721. NotificationCenter.getInstance().removeObserver(this, NotificationCenter.didReceiveSmsCode);
  1722. } else if (currentType == 3) {
  1723. AndroidUtilities.setWaitingForCall(false);
  1724. NotificationCenter.getInstance().removeObserver(this, NotificationCenter.didReceiveCall);
  1725. }
  1726. waitingForEvent = false;
  1727. destroyTimer();
  1728. destroyCodeTimer();
  1729. }
  1730.  
  1731. @Override
  1732. public void onShow() {
  1733. super.onShow();
  1734. if (codeField != null) {
  1735. codeField.requestFocus();
  1736. codeField.setSelection(codeField.length());
  1737. }
  1738. }
  1739.  
  1740. @Override
  1741. public void didReceivedNotification(int id, final Object... args) {
  1742. if (!waitingForEvent || codeField == null) {
  1743. return;
  1744. }
  1745. if (id == NotificationCenter.didReceiveSmsCode) {
  1746. ignoreOnTextChange = true;
  1747. codeField.setText("" + args[0]);
  1748. ignoreOnTextChange = false;
  1749. onNextPressed();
  1750. } else if (id == NotificationCenter.didReceiveCall) {
  1751. String num = "" + args[0];
  1752. if (!pattern.equals("*")) {
  1753. String patternNumbers = pattern.replace("*", "");
  1754. if (!num.contains(patternNumbers)) {
  1755. return;
  1756. }
  1757. }
  1758. ignoreOnTextChange = true;
  1759. codeField.setText(num);
  1760. ignoreOnTextChange = false;
  1761. onNextPressed();
  1762. }
  1763. }
  1764.  
  1765. @Override
  1766. public void saveStateParams(Bundle bundle) {
  1767. String code = codeField.getText().toString();
  1768. if (code.length() != 0) {
  1769. bundle.putString("smsview_code_" + currentType, code);
  1770. }
  1771. if (currentParams != null) {
  1772. bundle.putBundle("smsview_params_" + currentType, currentParams);
  1773. }
  1774. if (time != 0) {
  1775. bundle.putInt("time", time);
  1776. }
  1777. if (openTime != 0) {
  1778. bundle.putInt("open", openTime);
  1779. }
  1780. }
  1781.  
  1782. @Override
  1783. public void restoreStateParams(Bundle bundle) {
  1784. currentParams = bundle.getBundle("smsview_params_" + currentType);
  1785. if (currentParams != null) {
  1786. setParams(currentParams);
  1787. }
  1788. String code = bundle.getString("smsview_code_" + currentType);
  1789. if (code != null) {
  1790. codeField.setText(code);
  1791. }
  1792. int t = bundle.getInt("time");
  1793. if (t != 0) {
  1794. time = t;
  1795. }
  1796. int t2 = bundle.getInt("open");
  1797. if (t2 != 0) {
  1798. openTime = t2;
  1799. }
  1800. }
  1801. }
  1802.  
  1803. public class LoginActivityPasswordView extends SlideView {
  1804.  
  1805. private EditText codeField;
  1806. private TextView confirmTextView;
  1807. private TextView resetAccountButton;
  1808. private TextView resetAccountText;
  1809.  
  1810. private Bundle currentParams;
  1811. private boolean nextPressed;
  1812. private byte[] current_salt;
  1813. private String hint;
  1814. private String email_unconfirmed_pattern;
  1815. private boolean has_recovery;
  1816. private String requestPhone;
  1817. private String phoneHash;
  1818. private String phoneCode;
  1819.  
  1820. public LoginActivityPasswordView(Context context) {
  1821. super(context);
  1822.  
  1823. setOrientation(VERTICAL);
  1824.  
  1825. confirmTextView = new TextView(context);
  1826. confirmTextView.setTextColor(0xff757575);
  1827. confirmTextView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 14);
  1828. confirmTextView.setGravity(LocaleController.isRTL ? Gravity.RIGHT : Gravity.LEFT);
  1829. confirmTextView.setLineSpacing(AndroidUtilities.dp(2), 1.0f);
  1830. confirmTextView.setText(LocaleController.getString("LoginPasswordText", R.string.LoginPasswordText));
  1831. addView(confirmTextView, LayoutHelper.createLinear(LayoutHelper.WRAP_CONTENT, LayoutHelper.WRAP_CONTENT, LocaleController.isRTL ? Gravity.RIGHT : Gravity.LEFT));
  1832.  
  1833. codeField = new EditText(context);
  1834. codeField.setTextColor(0xff212121);
  1835. AndroidUtilities.clearCursorDrawable(codeField);
  1836. codeField.setHintTextColor(0xff979797);
  1837. codeField.setHint(LocaleController.getString("LoginPassword", R.string.LoginPassword));
  1838. codeField.setImeOptions(EditorInfo.IME_ACTION_NEXT | EditorInfo.IME_FLAG_NO_EXTRACT_UI);
  1839. codeField.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 18);
  1840. codeField.setMaxLines(1);
  1841. codeField.setPadding(0, 0, 0, 0);
  1842. codeField.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
  1843. codeField.setTransformationMethod(PasswordTransformationMethod.getInstance());
  1844. codeField.setTypeface(Typeface.DEFAULT);
  1845. codeField.setGravity(LocaleController.isRTL ? Gravity.RIGHT : Gravity.LEFT);
  1846. addView(codeField, LayoutHelper.createLinear(LayoutHelper.MATCH_PARENT, 36, Gravity.CENTER_HORIZONTAL, 0, 20, 0, 0));
  1847. codeField.setOnEditorActionListener(new TextView.OnEditorActionListener() {
  1848. @Override
  1849. public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
  1850. if (i == EditorInfo.IME_ACTION_NEXT) {
  1851. onNextPressed();
  1852. return true;
  1853. }
  1854. return false;
  1855. }
  1856. });
  1857.  
  1858. TextView cancelButton = new TextView(context);
  1859. cancelButton.setGravity((LocaleController.isRTL ? Gravity.RIGHT : Gravity.LEFT) | Gravity.TOP);
  1860. cancelButton.setTextColor(0xff4d83b3);
  1861. cancelButton.setText(LocaleController.getString("ForgotPassword", R.string.ForgotPassword));
  1862. cancelButton.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 14);
  1863. cancelButton.setLineSpacing(AndroidUtilities.dp(2), 1.0f);
  1864. cancelButton.setPadding(0, AndroidUtilities.dp(14), 0, 0);
  1865. addView(cancelButton, LayoutHelper.createLinear(LayoutHelper.WRAP_CONTENT, LayoutHelper.WRAP_CONTENT, Gravity.TOP | (LocaleController.isRTL ? Gravity.RIGHT : Gravity.LEFT)));
  1866. cancelButton.setOnClickListener(new OnClickListener() {
  1867. @Override
  1868. public void onClick(View view) {
  1869. if (has_recovery) {
  1870. needShowProgress();
  1871. TLRPC.TL_auth_requestPasswordRecovery req = new TLRPC.TL_auth_requestPasswordRecovery();
  1872. ConnectionsManager.getInstance().sendRequest(req, new RequestDelegate() {
  1873. @Override
  1874. public void run(final TLObject response, final TLRPC.TL_error error) {
  1875. AndroidUtilities.runOnUIThread(new Runnable() {
  1876. @Override
  1877. public void run() {
  1878. needHideProgress();
  1879. if (error == null) {
  1880. final TLRPC.TL_auth_passwordRecovery res = (TLRPC.TL_auth_passwordRecovery) response;
  1881. AlertDialog.Builder builder = new AlertDialog.Builder(getParentActivity());
  1882. builder.setMessage(LocaleController.formatString("RestoreEmailSent", R.string.RestoreEmailSent, res.email_pattern));
  1883. builder.setTitle(LocaleController.getString("AppName", R.string.AppName));
  1884. builder.setPositiveButton(LocaleController.getString("OK", R.string.OK), new DialogInterface.OnClickListener() {
  1885. @Override
  1886. public void onClick(DialogInterface dialogInterface, int i) {
  1887. Bundle bundle = new Bundle();
  1888. bundle.putString("email_unconfirmed_pattern", res.email_pattern);
  1889. setPage(7, true, bundle, false);
  1890. }
  1891. });
  1892. Dialog dialog = showDialog(builder.create());
  1893. if (dialog != null) {
  1894. dialog.setCanceledOnTouchOutside(false);
  1895. dialog.setCancelable(false);
  1896. }
  1897. } else {
  1898. if (error.text.startsWith("FLOOD_WAIT")) {
  1899. int time = Utilities.parseInt(error.text);
  1900. String timeString;
  1901. if (time < 60) {
  1902. timeString = LocaleController.formatPluralString("Seconds", time);
  1903. } else {
  1904. timeString = LocaleController.formatPluralString("Minutes", time / 60);
  1905. }
  1906. needShowAlert(LocaleController.getString("AppName", R.string.AppName), LocaleController.formatString("FloodWaitTime", R.string.FloodWaitTime, timeString));
  1907. } else {
  1908. needShowAlert(LocaleController.getString("AppName", R.string.AppName), error.text);
  1909. }
  1910. }
  1911. }
  1912. });
  1913. }
  1914. }, ConnectionsManager.RequestFlagFailOnServerErrors | ConnectionsManager.RequestFlagWithoutLogin);
  1915. } else {
  1916. resetAccountText.setVisibility(VISIBLE);
  1917. resetAccountButton.setVisibility(VISIBLE);
  1918. AndroidUtilities.hideKeyboard(codeField);
  1919. needShowAlert(LocaleController.getString("RestorePasswordNoEmailTitle", R.string.RestorePasswordNoEmailTitle), LocaleController.getString("RestorePasswordNoEmailText", R.string.RestorePasswordNoEmailText));
  1920. }
  1921. }
  1922. });
  1923.  
  1924. resetAccountButton = new TextView(context);
  1925. resetAccountButton.setGravity((LocaleController.isRTL ? Gravity.RIGHT : Gravity.LEFT) | Gravity.TOP);
  1926. resetAccountButton.setTextColor(0xffff6666);
  1927. resetAccountButton.setVisibility(GONE);
  1928. resetAccountButton.setText(LocaleController.getString("ResetMyAccount", R.string.ResetMyAccount));
  1929. resetAccountButton.setTypeface(AndroidUtilities.getTypeface("fonts/rmedium.ttf"));
  1930. resetAccountButton.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 14);
  1931. resetAccountButton.setLineSpacing(AndroidUtilities.dp(2), 1.0f);
  1932. resetAccountButton.setPadding(0, AndroidUtilities.dp(14), 0, 0);
  1933. addView(resetAccountButton, LayoutHelper.createLinear(LayoutHelper.WRAP_CONTENT, LayoutHelper.WRAP_CONTENT, Gravity.TOP | (LocaleController.isRTL ? Gravity.RIGHT : Gravity.LEFT), 0, 34, 0, 0));
  1934. resetAccountButton.setOnClickListener(new OnClickListener() {
  1935. @Override
  1936. public void onClick(View view) {
  1937. AlertDialog.Builder builder = new AlertDialog.Builder(getParentActivity());
  1938. builder.setMessage(LocaleController.getString("ResetMyAccountWarningText", R.string.ResetMyAccountWarningText));
  1939. builder.setTitle(LocaleController.getString("ResetMyAccountWarning", R.string.ResetMyAccountWarning));
  1940. builder.setPositiveButton(LocaleController.getString("ResetMyAccountWarningReset", R.string.ResetMyAccountWarningReset), new DialogInterface.OnClickListener() {
  1941. @Override
  1942. public void onClick(DialogInterface dialogInterface, int i) {
  1943. needShowProgress();
  1944. TLRPC.TL_account_deleteAccount req = new TLRPC.TL_account_deleteAccount();
  1945. req.reason = "Forgot password";
  1946. ConnectionsManager.getInstance().sendRequest(req, new RequestDelegate() {
  1947. @Override
  1948. public void run(TLObject response, final TLRPC.TL_error error) {
  1949. AndroidUtilities.runOnUIThread(new Runnable() {
  1950. @Override
  1951. public void run() {
  1952. needHideProgress();
  1953. if (error == null) {
  1954. Bundle params = new Bundle();
  1955. params.putString("phoneFormated", requestPhone);
  1956. params.putString("phoneHash", phoneHash);
  1957. params.putString("code", phoneCode);
  1958. setPage(5, true, params, false);
  1959. } else {
  1960. if (error.text.equals("2FA_RECENT_CONFIRM")) {
  1961. needShowAlert(LocaleController.getString("AppName", R.string.AppName), LocaleController.getString("ResetAccountCancelledAlert", R.string.ResetAccountCancelledAlert));
  1962. } else if (error.text.startsWith("2FA_CONFIRM_WAIT_")) {
  1963. Bundle params = new Bundle();
  1964. params.putString("phoneFormated", requestPhone);
  1965. params.putString("phoneHash", phoneHash);
  1966. params.putString("code", phoneCode);
  1967. params.putInt("startTime", ConnectionsManager.getInstance().getCurrentTime());
  1968. params.putInt("waitTime", Utilities.parseInt(error.text.replace("2FA_CONFIRM_WAIT_", "")));
  1969. setPage(8, true, params, false);
  1970. } else {
  1971. needShowAlert(LocaleController.getString("AppName", R.string.AppName), error.text);
  1972. }
  1973. }
  1974. }
  1975. });
  1976. }
  1977. }, ConnectionsManager.RequestFlagWithoutLogin | ConnectionsManager.RequestFlagFailOnServerErrors);
  1978. }
  1979. });
  1980. builder.setNegativeButton(LocaleController.getString("Cancel", R.string.Cancel), null);
  1981. showDialog(builder.create());
  1982. }
  1983. });
  1984.  
  1985. resetAccountText = new TextView(context);
  1986. resetAccountText.setGravity((LocaleController.isRTL ? Gravity.RIGHT : Gravity.LEFT) | Gravity.TOP);
  1987. resetAccountText.setVisibility(GONE);
  1988. resetAccountText.setTextColor(0xff757575);
  1989. resetAccountText.setText(LocaleController.getString("ResetMyAccountText", R.string.ResetMyAccountText));
  1990. resetAccountText.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 14);
  1991. resetAccountText.setLineSpacing(AndroidUtilities.dp(2), 1.0f);
  1992. addView(resetAccountText, LayoutHelper.createLinear(LayoutHelper.WRAP_CONTENT, LayoutHelper.WRAP_CONTENT, Gravity.TOP | (LocaleController.isRTL ? Gravity.RIGHT : Gravity.LEFT), 0, 7, 0, 14));
  1993. }
  1994.  
  1995. @Override
  1996. public String getHeaderName() {
  1997. return LocaleController.getString("LoginPassword", R.string.LoginPassword);
  1998. }
  1999.  
  2000. @Override
  2001. public void setParams(Bundle params) {
  2002. if (params == null) {
  2003. return;
  2004. }
  2005. if (params.isEmpty()) {
  2006. resetAccountButton.setVisibility(VISIBLE);
  2007. resetAccountText.setVisibility(VISIBLE);
  2008. AndroidUtilities.hideKeyboard(codeField);
  2009. return;
  2010. }
  2011. resetAccountButton.setVisibility(GONE);
  2012. resetAccountText.setVisibility(GONE);
  2013. codeField.setText("");
  2014. currentParams = params;
  2015. current_salt = Utilities.hexToBytes(currentParams.getString("current_salt"));
  2016. hint = currentParams.getString("hint");
  2017. has_recovery = currentParams.getInt("has_recovery") == 1;
  2018. email_unconfirmed_pattern = currentParams.getString("email_unconfirmed_pattern");
  2019. requestPhone = params.getString("phoneFormated");
  2020. phoneHash = params.getString("phoneHash");
  2021. phoneCode = params.getString("code");
  2022.  
  2023. if (hint != null && hint.length() > 0) {
  2024. codeField.setHint(hint);
  2025. } else {
  2026. codeField.setHint(LocaleController.getString("LoginPassword", R.string.LoginPassword));
  2027. }
  2028. }
  2029.  
  2030. private void onPasscodeError(boolean clear) {
  2031. if (getParentActivity() == null) {
  2032. return;
  2033. }
  2034. Vibrator v = (Vibrator) getParentActivity().getSystemService(Context.VIBRATOR_SERVICE);
  2035. if (v != null) {
  2036. v.vibrate(200);
  2037. }
  2038. if (clear) {
  2039. codeField.setText("");
  2040. }
  2041. AndroidUtilities.shakeView(confirmTextView, 2, 0);
  2042. }
  2043.  
  2044. @Override
  2045. public void onNextPressed() {
  2046. if (nextPressed) {
  2047. return;
  2048. }
  2049.  
  2050. String oldPassword = codeField.getText().toString();
  2051. if (oldPassword.length() == 0) {
  2052. onPasscodeError(false);
  2053. return;
  2054. }
  2055. nextPressed = true;
  2056. byte[] oldPasswordBytes = null;
  2057. try {
  2058. oldPasswordBytes = oldPassword.getBytes("UTF-8");
  2059. } catch (Exception e) {
  2060. FileLog.e("Glabbr", e);
  2061. }
  2062.  
  2063. needShowProgress();
  2064. byte[] hash = new byte[current_salt.length * 2 + oldPasswordBytes.length];
  2065. System.arraycopy(current_salt, 0, hash, 0, current_salt.length);
  2066. System.arraycopy(oldPasswordBytes, 0, hash, current_salt.length, oldPasswordBytes.length);
  2067. System.arraycopy(current_salt, 0, hash, hash.length - current_salt.length, current_salt.length);
  2068.  
  2069. final TLRPC.TL_auth_checkPassword req = new TLRPC.TL_auth_checkPassword();
  2070. req.password_hash = Utilities.computeSHA256(hash, 0, hash.length);
  2071. ConnectionsManager.getInstance().sendRequest(req, new RequestDelegate() {
  2072. @Override
  2073. public void run(final TLObject response, final TLRPC.TL_error error) {
  2074. AndroidUtilities.runOnUIThread(new Runnable() {
  2075. @Override
  2076. public void run() {
  2077. needHideProgress();
  2078. nextPressed = false;
  2079. if (error == null) {
  2080. TLRPC.TL_auth_authorization res = (TLRPC.TL_auth_authorization) response;
  2081. ConnectionsManager.getInstance().setUserId(res.user.id);
  2082. UserConfig.clearConfig();
  2083. MessagesController.getInstance().cleanup();
  2084. UserConfig.setCurrentUser(res.user);
  2085. UserConfig.saveConfig(true);
  2086. MessagesStorage.getInstance().cleanup(true);
  2087. ArrayList<TLRPC.User> users = new ArrayList<>();
  2088. users.add(res.user);
  2089. MessagesStorage.getInstance().putUsersAndChats(users, null, true, true);
  2090. MessagesController.getInstance().putUser(res.user, false);
  2091. ContactsController.getInstance().checkAppAccount();
  2092. MessagesController.getInstance().getBlockedUsers(true);
  2093. needFinishActivity();
  2094. } else {
  2095. if (error.text.equals("PASSWORD_HASH_INVALID")) {
  2096. onPasscodeError(true);
  2097. } else if (error.text.startsWith("FLOOD_WAIT")) {
  2098. int time = Utilities.parseInt(error.text);
  2099. String timeString;
  2100. if (time < 60) {
  2101. timeString = LocaleController.formatPluralString("Seconds", time);
  2102. } else {
  2103. timeString = LocaleController.formatPluralString("Minutes", time / 60);
  2104. }
  2105. needShowAlert(LocaleController.getString("AppName", R.string.AppName), LocaleController.formatString("FloodWaitTime", R.string.FloodWaitTime, timeString));
  2106. } else {
  2107. needShowAlert(LocaleController.getString("AppName", R.string.AppName), error.text);
  2108. }
  2109. }
  2110. }
  2111. });
  2112. }
  2113. }, ConnectionsManager.RequestFlagFailOnServerErrors | ConnectionsManager.RequestFlagWithoutLogin);
  2114. }
  2115.  
  2116. @Override
  2117. public boolean needBackButton() {
  2118. return true;
  2119. }
  2120.  
  2121. @Override
  2122. public void onBackPressed() {
  2123. currentParams = null;
  2124. }
  2125.  
  2126. @Override
  2127. public void onShow() {
  2128. super.onShow();
  2129. if (codeField != null) {
  2130. codeField.requestFocus();
  2131. codeField.setSelection(codeField.length());
  2132. AndroidUtilities.showKeyboard(codeField);
  2133. }
  2134. }
  2135.  
  2136. @Override
  2137. public void saveStateParams(Bundle bundle) {
  2138. String code = codeField.getText().toString();
  2139. if (code.length() != 0) {
  2140. bundle.putString("passview_code", code);
  2141. }
  2142. if (currentParams != null) {
  2143. bundle.putBundle("passview_params", currentParams);
  2144. }
  2145. }
  2146.  
  2147. @Override
  2148. public void restoreStateParams(Bundle bundle) {
  2149. currentParams = bundle.getBundle("passview_params");
  2150. if (currentParams != null) {
  2151. setParams(currentParams);
  2152. }
  2153. String code = bundle.getString("passview_code");
  2154. if (code != null) {
  2155. codeField.setText(code);
  2156. }
  2157. }
  2158. }
  2159.  
  2160. public class LoginActivityResetWaitView extends SlideView {
  2161.  
  2162. private TextView confirmTextView;
  2163. private TextView resetAccountButton;
  2164. private TextView resetAccountTime;
  2165. private Runnable timeRunnable;
  2166.  
  2167. private Bundle currentParams;
  2168. private String requestPhone;
  2169. private String phoneHash;
  2170. private String phoneCode;
  2171. private int startTime;
  2172. private int waitTime;
  2173.  
  2174. public LoginActivityResetWaitView(Context context) {
  2175. super(context);
  2176.  
  2177. setOrientation(VERTICAL);
  2178.  
  2179. confirmTextView = new TextView(context);
  2180. confirmTextView.setTextColor(0xff757575);
  2181. confirmTextView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 14);
  2182. confirmTextView.setGravity(LocaleController.isRTL ? Gravity.RIGHT : Gravity.LEFT);
  2183. confirmTextView.setLineSpacing(AndroidUtilities.dp(2), 1.0f);
  2184. addView(confirmTextView, LayoutHelper.createLinear(LayoutHelper.WRAP_CONTENT, LayoutHelper.WRAP_CONTENT, LocaleController.isRTL ? Gravity.RIGHT : Gravity.LEFT));
  2185.  
  2186. TextView resetAccountText = new TextView(context);
  2187. resetAccountText.setGravity((LocaleController.isRTL ? Gravity.RIGHT : Gravity.LEFT) | Gravity.TOP);
  2188. resetAccountText.setTextColor(0xff757575);
  2189. resetAccountText.setText(LocaleController.getString("ResetAccountStatus", R.string.ResetAccountStatus));
  2190. resetAccountText.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 14);
  2191. resetAccountText.setLineSpacing(AndroidUtilities.dp(2), 1.0f);
  2192. addView(resetAccountText, LayoutHelper.createLinear(LayoutHelper.WRAP_CONTENT, LayoutHelper.WRAP_CONTENT, Gravity.TOP | (LocaleController.isRTL ? Gravity.RIGHT : Gravity.LEFT), 0, 24, 0, 0));
  2193.  
  2194. resetAccountTime = new TextView(context);
  2195. resetAccountTime.setGravity((LocaleController.isRTL ? Gravity.RIGHT : Gravity.LEFT) | Gravity.TOP);
  2196. resetAccountTime.setTextColor(0xff757575);
  2197. resetAccountTime.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 14);
  2198. resetAccountTime.setLineSpacing(AndroidUtilities.dp(2), 1.0f);
  2199. addView(resetAccountTime, LayoutHelper.createLinear(LayoutHelper.WRAP_CONTENT, LayoutHelper.WRAP_CONTENT, Gravity.TOP | (LocaleController.isRTL ? Gravity.RIGHT : Gravity.LEFT), 0, 2, 0, 0));
  2200.  
  2201. resetAccountButton = new TextView(context);
  2202. resetAccountButton.setGravity((LocaleController.isRTL ? Gravity.RIGHT : Gravity.LEFT) | Gravity.TOP);
  2203. resetAccountButton.setText(LocaleController.getString("ResetAccountButton", R.string.ResetAccountButton));
  2204. resetAccountButton.setTypeface(AndroidUtilities.getTypeface("fonts/rmedium.ttf"));
  2205. resetAccountButton.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 14);
  2206. resetAccountButton.setLineSpacing(AndroidUtilities.dp(2), 1.0f);
  2207. resetAccountButton.setPadding(0, AndroidUtilities.dp(14), 0, 0);
  2208. addView(resetAccountButton, LayoutHelper.createLinear(LayoutHelper.WRAP_CONTENT, LayoutHelper.WRAP_CONTENT, Gravity.TOP | (LocaleController.isRTL ? Gravity.RIGHT : Gravity.LEFT), 0, 7, 0, 0));
  2209. resetAccountButton.setOnClickListener(new OnClickListener() {
  2210. @Override
  2211. public void onClick(View view) {
  2212. if (Math.abs(ConnectionsManager.getInstance().getCurrentTime() - startTime) < waitTime) {
  2213. return;
  2214. }
  2215. AlertDialog.Builder builder = new AlertDialog.Builder(getParentActivity());
  2216. builder.setMessage(LocaleController.getString("ResetMyAccountWarningText", R.string.ResetMyAccountWarningText));
  2217. builder.setTitle(LocaleController.getString("ResetMyAccountWarning", R.string.ResetMyAccountWarning));
  2218. builder.setPositiveButton(LocaleController.getString("ResetMyAccountWarningReset", R.string.ResetMyAccountWarningReset), new DialogInterface.OnClickListener() {
  2219. @Override
  2220. public void onClick(DialogInterface dialogInterface, int i) {
  2221. needShowProgress();
  2222. TLRPC.TL_account_deleteAccount req = new TLRPC.TL_account_deleteAccount();
  2223. req.reason = "Forgot password";
  2224. ConnectionsManager.getInstance().sendRequest(req, new RequestDelegate() {
  2225. @Override
  2226. public void run(TLObject response, final TLRPC.TL_error error) {
  2227. AndroidUtilities.runOnUIThread(new Runnable() {
  2228. @Override
  2229. public void run() {
  2230. needHideProgress();
  2231. if (error == null) {
  2232. Bundle params = new Bundle();
  2233. params.putString("phoneFormated", requestPhone);
  2234. params.putString("phoneHash", phoneHash);
  2235. params.putString("code", phoneCode);
  2236. setPage(5, true, params, false);
  2237. } else {
  2238. if (error.text.equals("2FA_RECENT_CONFIRM")) {
  2239. needShowAlert(LocaleController.getString("AppName", R.string.AppName), LocaleController.getString("ResetAccountCancelledAlert", R.string.ResetAccountCancelledAlert));
  2240. } else {
  2241. needShowAlert(LocaleController.getString("AppName", R.string.AppName), error.text);
  2242. }
  2243. }
  2244. }
  2245. });
  2246. }
  2247. }, ConnectionsManager.RequestFlagWithoutLogin | ConnectionsManager.RequestFlagFailOnServerErrors);
  2248. }
  2249. });
  2250. builder.setNegativeButton(LocaleController.getString("Cancel", R.string.Cancel), null);
  2251. showDialog(builder.create());
  2252. }
  2253. });
  2254. }
  2255.  
  2256. @Override
  2257. public String getHeaderName() {
  2258. return LocaleController.getString("ResetAccount", R.string.ResetAccount);
  2259. }
  2260.  
  2261. private void updateTimeText() {
  2262. int timeLeft = Math.max(0, waitTime - (ConnectionsManager.getInstance().getCurrentTime() - startTime));
  2263. int days = timeLeft / 86400;
  2264. int hours = (timeLeft - days * 86400) / 3600;
  2265. int minutes = (timeLeft - days * 86400 - hours * 3600) / 60;
  2266. int seconds = timeLeft % 60;
  2267. if (days != 0) {
  2268. resetAccountTime.setText(AndroidUtilities.replaceTags(LocaleController.formatPluralString("DaysBold", days) + " " + LocaleController.formatPluralString("HoursBold", hours) + " " + LocaleController.formatPluralString("MinutesBold", minutes)));
  2269. } else {
  2270. resetAccountTime.setText(AndroidUtilities.replaceTags(LocaleController.formatPluralString("HoursBold", hours) + " " + LocaleController.formatPluralString("MinutesBold", minutes) + " " + LocaleController.formatPluralString("SecondsBold", seconds)));
  2271. }
  2272. if (timeLeft > 0) {
  2273. resetAccountButton.setTextColor(0x88888888);
  2274. } else {
  2275. resetAccountButton.setTextColor(0xffff6666);
  2276. }
  2277. }
  2278.  
  2279. @Override
  2280. public void setParams(Bundle params) {
  2281. if (params == null) {
  2282. return;
  2283. }
  2284. currentParams = params;
  2285. requestPhone = params.getString("phoneFormated");
  2286. phoneHash = params.getString("phoneHash");
  2287. phoneCode = params.getString("code");
  2288. startTime = params.getInt("startTime");
  2289. waitTime = params.getInt("waitTime");
  2290. confirmTextView.setText(AndroidUtilities.replaceTags(LocaleController.formatString("ResetAccountInfo", R.string.ResetAccountInfo, PhoneFormat.getInstance().format("+" + requestPhone))));
  2291. updateTimeText();
  2292. timeRunnable = new Runnable() {
  2293. @Override
  2294. public void run() {
  2295. if (timeRunnable != this) {
  2296. return;
  2297. }
  2298. updateTimeText();
  2299. AndroidUtilities.runOnUIThread(timeRunnable, 1000);
  2300. }
  2301. };
  2302. AndroidUtilities.runOnUIThread(timeRunnable, 1000);
  2303. }
  2304.  
  2305. @Override
  2306. public boolean needBackButton() {
  2307. return true;
  2308. }
  2309.  
  2310. @Override
  2311. public void onBackPressed() {
  2312. AndroidUtilities.cancelRunOnUIThread(timeRunnable);
  2313. timeRunnable = null;
  2314. currentParams = null;
  2315. }
  2316.  
  2317. @Override
  2318. public void saveStateParams(Bundle bundle) {
  2319. if (currentParams != null) {
  2320. bundle.putBundle("resetview_params", currentParams);
  2321. }
  2322. }
  2323.  
  2324. @Override
  2325. public void restoreStateParams(Bundle bundle) {
  2326. currentParams = bundle.getBundle("resetview_params");
  2327. if (currentParams != null) {
  2328. setParams(currentParams);
  2329. }
  2330. }
  2331. }
  2332.  
  2333. public class LoginActivityRecoverView extends SlideView {
  2334.  
  2335. private EditText codeField;
  2336. private TextView confirmTextView;
  2337. private TextView cancelButton;
  2338.  
  2339. private Bundle currentParams;
  2340. private boolean nextPressed;
  2341. private String email_unconfirmed_pattern;
  2342.  
  2343. public LoginActivityRecoverView(Context context) {
  2344. super(context);
  2345.  
  2346. setOrientation(VERTICAL);
  2347.  
  2348. confirmTextView = new TextView(context);
  2349. confirmTextView.setTextColor(0xff757575);
  2350. confirmTextView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 14);
  2351. confirmTextView.setGravity((LocaleController.isRTL ? Gravity.RIGHT : Gravity.LEFT));
  2352. confirmTextView.setLineSpacing(AndroidUtilities.dp(2), 1.0f);
  2353. confirmTextView.setText(LocaleController.getString("RestoreEmailSentInfo", R.string.RestoreEmailSentInfo));
  2354. addView(confirmTextView, LayoutHelper.createLinear(LayoutHelper.WRAP_CONTENT, LayoutHelper.WRAP_CONTENT, (LocaleController.isRTL ? Gravity.RIGHT : Gravity.LEFT)));
  2355.  
  2356. codeField = new EditText(context);
  2357. codeField.setTextColor(0xff212121);
  2358. AndroidUtilities.clearCursorDrawable(codeField);
  2359. codeField.setHintTextColor(0xff979797);
  2360. codeField.setHint(LocaleController.getString("PasswordCode", R.string.PasswordCode));
  2361. codeField.setImeOptions(EditorInfo.IME_ACTION_NEXT | EditorInfo.IME_FLAG_NO_EXTRACT_UI);
  2362. codeField.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 18);
  2363. codeField.setMaxLines(1);
  2364. codeField.setPadding(0, 0, 0, 0);
  2365. codeField.setInputType(InputType.TYPE_CLASS_PHONE);
  2366. codeField.setTransformationMethod(PasswordTransformationMethod.getInstance());
  2367. codeField.setTypeface(Typeface.DEFAULT);
  2368. codeField.setGravity(LocaleController.isRTL ? Gravity.RIGHT : Gravity.LEFT);
  2369. addView(codeField, LayoutHelper.createLinear(LayoutHelper.MATCH_PARENT, 36, Gravity.CENTER_HORIZONTAL, 0, 20, 0, 0));
  2370. codeField.setOnEditorActionListener(new TextView.OnEditorActionListener() {
  2371. @Override
  2372. public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
  2373. if (i == EditorInfo.IME_ACTION_NEXT) {
  2374. onNextPressed();
  2375. return true;
  2376. }
  2377. return false;
  2378. }
  2379. });
  2380.  
  2381. cancelButton = new TextView(context);
  2382. cancelButton.setGravity((LocaleController.isRTL ? Gravity.RIGHT : Gravity.LEFT) | Gravity.BOTTOM);
  2383. cancelButton.setTextColor(0xff4d83b3);
  2384. cancelButton.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 14);
  2385. cancelButton.setLineSpacing(AndroidUtilities.dp(2), 1.0f);
  2386. cancelButton.setPadding(0, AndroidUtilities.dp(14), 0, 0);
  2387. addView(cancelButton, LayoutHelper.createLinear(LayoutHelper.WRAP_CONTENT, LayoutHelper.WRAP_CONTENT, Gravity.BOTTOM | (LocaleController.isRTL ? Gravity.RIGHT : Gravity.LEFT), 0, 0, 0, 14));
  2388. cancelButton.setOnClickListener(new OnClickListener() {
  2389. @Override
  2390. public void onClick(View view) {
  2391. AlertDialog.Builder builder = new AlertDialog.Builder(getParentActivity());
  2392. builder.setMessage(LocaleController.getString("RestoreEmailTroubleText", R.string.RestoreEmailTroubleText));
  2393. builder.setTitle(LocaleController.getString("RestorePasswordNoEmailTitle", R.string.RestorePasswordNoEmailTitle));
  2394. builder.setPositiveButton(LocaleController.getString("OK", R.string.OK), new DialogInterface.OnClickListener() {
  2395. @Override
  2396. public void onClick(DialogInterface dialogInterface, int i) {
  2397. setPage(6, true, new Bundle(), true);
  2398. }
  2399. });
  2400. Dialog dialog = showDialog(builder.create());
  2401. if (dialog != null) {
  2402. dialog.setCanceledOnTouchOutside(false);
  2403. dialog.setCancelable(false);
  2404. }
  2405. }
  2406. });
  2407. }
  2408.  
  2409. @Override
  2410. public boolean needBackButton() {
  2411. return true;
  2412. }
  2413.  
  2414. @Override
  2415. public String getHeaderName() {
  2416. return LocaleController.getString("LoginPassword", R.string.LoginPassword);
  2417. }
  2418.  
  2419. @Override
  2420. public void setParams(Bundle params) {
  2421. if (params == null) {
  2422. return;
  2423. }
  2424. codeField.setText("");
  2425. currentParams = params;
  2426. email_unconfirmed_pattern = currentParams.getString("email_unconfirmed_pattern");
  2427. cancelButton.setText(LocaleController.formatString("RestoreEmailTrouble", R.string.RestoreEmailTrouble, email_unconfirmed_pattern));
  2428.  
  2429. AndroidUtilities.showKeyboard(codeField);
  2430. codeField.requestFocus();
  2431. }
  2432.  
  2433. private void onPasscodeError(boolean clear) {
  2434. if (getParentActivity() == null) {
  2435. return;
  2436. }
  2437. Vibrator v = (Vibrator) getParentActivity().getSystemService(Context.VIBRATOR_SERVICE);
  2438. if (v != null) {
  2439. v.vibrate(200);
  2440. }
  2441. if (clear) {
  2442. codeField.setText("");
  2443. }
  2444. AndroidUtilities.shakeView(confirmTextView, 2, 0);
  2445. }
  2446.  
  2447. @Override
  2448. public void onNextPressed() {
  2449. if (nextPressed) {
  2450. return;
  2451. }
  2452.  
  2453. String oldPassword = codeField.getText().toString();
  2454. if (oldPassword.length() == 0) {
  2455. onPasscodeError(false);
  2456. return;
  2457. }
  2458. nextPressed = true;
  2459.  
  2460. String code = codeField.getText().toString();
  2461. if (code.length() == 0) {
  2462. onPasscodeError(false);
  2463. return;
  2464. }
  2465. needShowProgress();
  2466. TLRPC.TL_auth_recoverPassword req = new TLRPC.TL_auth_recoverPassword();
  2467. req.code = code;
  2468. ConnectionsManager.getInstance().sendRequest(req, new RequestDelegate() {
  2469. @Override
  2470. public void run(final TLObject response, final TLRPC.TL_error error) {
  2471. AndroidUtilities.runOnUIThread(new Runnable() {
  2472. @Override
  2473. public void run() {
  2474. needHideProgress();
  2475. nextPressed = false;
  2476. if (error == null) {
  2477. TLRPC.TL_auth_authorization res = (TLRPC.TL_auth_authorization) response;
  2478. ConnectionsManager.getInstance().setUserId(res.user.id);
  2479. UserConfig.clearConfig();
  2480. MessagesController.getInstance().cleanup();
  2481. UserConfig.setCurrentUser(res.user);
  2482. UserConfig.saveConfig(true);
  2483. MessagesStorage.getInstance().cleanup(true);
  2484. ArrayList<TLRPC.User> users = new ArrayList<>();
  2485. users.add(res.user);
  2486. MessagesStorage.getInstance().putUsersAndChats(users, null, true, true);
  2487. MessagesController.getInstance().putUser(res.user, false);
  2488. ContactsController.getInstance().checkAppAccount();
  2489. MessagesController.getInstance().getBlockedUsers(true);
  2490. needFinishActivity();
  2491. } else {
  2492. if (error.text.startsWith("CODE_INVALID")) {
  2493. onPasscodeError(true);
  2494. } else if (error.text.startsWith("FLOOD_WAIT")) {
  2495. int time = Utilities.parseInt(error.text);
  2496. String timeString;
  2497. if (time < 60) {
  2498. timeString = LocaleController.formatPluralString("Seconds", time);
  2499. } else {
  2500. timeString = LocaleController.formatPluralString("Minutes", time / 60);
  2501. }
  2502. needShowAlert(LocaleController.getString("AppName", R.string.AppName), LocaleController.formatString("FloodWaitTime", R.string.FloodWaitTime, timeString));
  2503. } else {
  2504. needShowAlert(LocaleController.getString("AppName", R.string.AppName), error.text);
  2505. }
  2506. }
  2507. }
  2508. });
  2509. }
  2510. }, ConnectionsManager.RequestFlagFailOnServerErrors | ConnectionsManager.RequestFlagWithoutLogin);
  2511. }
  2512.  
  2513. @Override
  2514. public void onBackPressed() {
  2515. currentParams = null;
  2516. }
  2517.  
  2518. @Override
  2519. public void onShow() {
  2520. super.onShow();
  2521. if (codeField != null) {
  2522. codeField.requestFocus();
  2523. codeField.setSelection(codeField.length());
  2524. }
  2525. }
  2526.  
  2527. @Override
  2528. public void saveStateParams(Bundle bundle) {
  2529. String code = codeField.getText().toString();
  2530. if (code != null && code.length() != 0) {
  2531. bundle.putString("recoveryview_code", code);
  2532. }
  2533. if (currentParams != null) {
  2534. bundle.putBundle("recoveryview_params", currentParams);
  2535. }
  2536. }
  2537.  
  2538. @Override
  2539. public void restoreStateParams(Bundle bundle) {
  2540. currentParams = bundle.getBundle("recoveryview_params");
  2541. if (currentParams != null) {
  2542. setParams(currentParams);
  2543. }
  2544. String code = bundle.getString("recoveryview_code");
  2545. if (code != null) {
  2546. codeField.setText(code);
  2547. }
  2548. }
  2549. }
  2550. public class LoginActivityRegisterView extends SlideView {
  2551.  
  2552. private EditText firstNameField;
  2553. private EditText lastNameField;
  2554. private String requestPhone;
  2555. private String phoneHash;
  2556. private String phoneCode;
  2557. private Bundle currentParams;
  2558. private boolean nextPressed = false;
  2559. private String countryCode;
  2560. private EditText userNameField;
  2561. private TextView checkTextView;
  2562. private String lastCheckName = null;
  2563. private Runnable checkRunnable = null;
  2564. private boolean lastNameAvailable = false;
  2565. private int checkReqId = 0;
  2566.  
  2567. public LoginActivityRegisterView(Context context) {
  2568. super(context);
  2569.  
  2570. setOrientation(VERTICAL);
  2571.  
  2572. TextView textView = new TextView(context);
  2573. textView.setText(LocaleController.getString("RegisterText", R.string.RegisterText));
  2574. textView.setTextColor(0xff757575);
  2575. textView.setGravity(LocaleController.isRTL ? Gravity.RIGHT : Gravity.LEFT);
  2576. textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 14);
  2577. addView(textView, LayoutHelper.createLinear(LayoutHelper.WRAP_CONTENT, LayoutHelper.WRAP_CONTENT, LocaleController.isRTL ? Gravity.RIGHT : Gravity.LEFT, 24, 8, 24, 0));
  2578.  
  2579. firstNameField = new EditText(context);
  2580. firstNameField.setHintTextColor(0xff979797);
  2581. firstNameField.setTextColor(0xff212121);
  2582. AndroidUtilities.clearCursorDrawable(firstNameField);
  2583. firstNameField.setHint(LocaleController.getString("FirstName", R.string.FirstName));
  2584. firstNameField.setImeOptions(EditorInfo.IME_ACTION_NEXT | EditorInfo.IME_FLAG_NO_EXTRACT_UI);
  2585. firstNameField.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 18);
  2586. firstNameField.setMaxLines(1);
  2587. firstNameField.setInputType(InputType.TYPE_TEXT_FLAG_CAP_WORDS);
  2588. addView(firstNameField,LayoutHelper.createLinear(LayoutHelper.MATCH_PARENT, 36, 24, 18, 24, 0));
  2589. firstNameField.setOnEditorActionListener(new TextView.OnEditorActionListener() {
  2590. @Override
  2591. public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
  2592. if (i == EditorInfo.IME_ACTION_NEXT) {
  2593. lastNameField.requestFocus();
  2594. return true;
  2595. }
  2596. return false;
  2597. }
  2598. });
  2599.  
  2600. lastNameField = new EditText(context);
  2601. lastNameField.setHint(LocaleController.getString("LastName", R.string.LastName));
  2602. lastNameField.setHintTextColor(0xff979797);
  2603. lastNameField.setTextColor(0xff212121);
  2604. AndroidUtilities.clearCursorDrawable(lastNameField);
  2605. lastNameField.setImeOptions(EditorInfo.IME_ACTION_NEXT | EditorInfo.IME_FLAG_NO_EXTRACT_UI);
  2606. lastNameField.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 18);
  2607. lastNameField.setMaxLines(1);
  2608. lastNameField.setInputType(InputType.TYPE_TEXT_FLAG_CAP_WORDS);
  2609. addView(lastNameField, LayoutHelper.createLinear(LayoutHelper.MATCH_PARENT, 36, 24, 24, 24, 12));
  2610.  
  2611. TextView utextView = new TextView(context);
  2612. utextView.setText(LocaleController.getString("UsernameRegisterText", R.string.UsernameRegisterText));
  2613. utextView.setTextColor(0xff757575);
  2614. utextView.setGravity(LocaleController.isRTL ? Gravity.RIGHT : Gravity.LEFT);
  2615. utextView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 14);
  2616. addView(utextView, LayoutHelper.createLinear(LayoutHelper.WRAP_CONTENT, LayoutHelper.WRAP_CONTENT, LocaleController.isRTL ? Gravity.RIGHT : Gravity.LEFT, 24, 8, 24, 8));
  2617.  
  2618.  
  2619. checkTextView = new TextView(context);
  2620. checkTextView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 15);
  2621. checkTextView.setGravity(LocaleController.isRTL ? Gravity.RIGHT : Gravity.LEFT);
  2622. addView(checkTextView, LayoutHelper.createLinear(LayoutHelper.WRAP_CONTENT, LayoutHelper.WRAP_CONTENT, LocaleController.isRTL ? Gravity.RIGHT : Gravity.LEFT, 24, 12, 24, 0));
  2623.  
  2624.  
  2625. userNameField = new EditText(context);
  2626. userNameField.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 18);
  2627. userNameField.setHintTextColor(0xff979797);
  2628. userNameField.setTextColor(0xff212121);
  2629. userNameField.setMaxLines(1);
  2630. userNameField.setLines(1);
  2631. userNameField.setPadding(0, 0, 0, 0);
  2632. userNameField.setSingleLine(true);
  2633. userNameField.setGravity(LocaleController.isRTL ? Gravity.RIGHT : Gravity.LEFT);
  2634. userNameField.setInputType(InputType.TYPE_TEXT_FLAG_CAP_SENTENCES | InputType.TYPE_TEXT_FLAG_MULTI_LINE | InputType.TYPE_TEXT_FLAG_AUTO_CORRECT);
  2635. userNameField.setImeOptions(EditorInfo.IME_ACTION_DONE);
  2636. userNameField.setHint(LocaleController.getString("UsernamePlaceholder", R.string.UsernamePlaceholder));
  2637. AndroidUtilities.clearCursorDrawable(userNameField);
  2638. userNameField.setOnEditorActionListener(new TextView.OnEditorActionListener() {
  2639. @Override
  2640. public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
  2641. if (i == EditorInfo.IME_ACTION_DONE && doneButton != null) {
  2642. doneButton.performClick();
  2643. return true;
  2644. }
  2645. return false;
  2646. }
  2647. });
  2648.  
  2649. addView(userNameField, LayoutHelper.createLinear(LayoutHelper.MATCH_PARENT, 36, 24, 18, 24, 0));
  2650.  
  2651. TextView helpTextView = new TextView(context);
  2652. helpTextView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 15);
  2653. helpTextView.setTextColor(0xff6d6d72);
  2654. helpTextView.setGravity(LocaleController.isRTL ? Gravity.RIGHT : Gravity.LEFT);
  2655. helpTextView.setText(AndroidUtilities.replaceTags(LocaleController.getString("UsernameHelp", R.string.UsernameHelp)));
  2656. addView(helpTextView, LayoutHelper.createLinear(LayoutHelper.WRAP_CONTENT, LayoutHelper.WRAP_CONTENT, LocaleController.isRTL ? Gravity.RIGHT : Gravity.LEFT, 24, 10, 24, 0));
  2657.  
  2658.  
  2659. LinearLayout linearLayout = new LinearLayout(context);
  2660. linearLayout.setGravity(Gravity.BOTTOM | Gravity.CENTER_VERTICAL);
  2661. addView(linearLayout, LayoutHelper.createLinear(LayoutHelper.MATCH_PARENT, LayoutHelper.MATCH_PARENT));
  2662.  
  2663.  
  2664.  
  2665.  
  2666. userNameField.addTextChangedListener(new TextWatcher() {
  2667. @Override
  2668. public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
  2669.  
  2670. }
  2671.  
  2672. @Override
  2673. public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
  2674. checkUserName(userNameField.getText().toString(), false);
  2675. }
  2676.  
  2677. @Override
  2678. public void afterTextChanged(Editable editable) {
  2679.  
  2680. }
  2681. });
  2682.  
  2683. checkTextView.setVisibility(View.GONE);
  2684.  
  2685. TextView wrongNumber = new TextView(context);
  2686. wrongNumber.setText(LocaleController.getString("CancelRegistration", R.string.CancelRegistration));
  2687. wrongNumber.setGravity((LocaleController.isRTL ? Gravity.RIGHT : Gravity.LEFT) | Gravity.CENTER_HORIZONTAL);
  2688. wrongNumber.setTextColor(0xff4d83b3);
  2689. wrongNumber.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 14);
  2690. wrongNumber.setLineSpacing(AndroidUtilities.dp(2), 1.0f);
  2691. wrongNumber.setPadding(0, AndroidUtilities.dp(24), 0, 0);
  2692. linearLayout.addView(wrongNumber, LayoutHelper.createLinear(LayoutHelper.WRAP_CONTENT, LayoutHelper.WRAP_CONTENT, Gravity.BOTTOM | (LocaleController.isRTL ? Gravity.RIGHT : Gravity.LEFT), 0, 0, 0, 10));
  2693. wrongNumber.setOnClickListener(new OnClickListener() {
  2694. @Override
  2695. public void onClick(View view) {
  2696. AlertDialog.Builder builder = new AlertDialog.Builder(getParentActivity());
  2697. builder.setTitle(LocaleController.getString("AppName", R.string.AppName));
  2698. builder.setMessage(LocaleController.getString("AreYouSureRegistration", R.string.AreYouSureRegistration));
  2699. builder.setPositiveButton(LocaleController.getString("OK", R.string.OK), new DialogInterface.OnClickListener() {
  2700. @Override
  2701. public void onClick(DialogInterface dialogInterface, int i) {
  2702. onBackPressed();
  2703. setPage(0, true, null, true);
  2704. }
  2705. });
  2706. builder.setNegativeButton(LocaleController.getString("Cancel", R.string.Cancel), null);
  2707. showDialog(builder.create());
  2708. }
  2709. });
  2710. }
  2711.  
  2712. @Override
  2713. public void onBackPressed() {
  2714. currentParams = null;
  2715. }
  2716.  
  2717. @Override
  2718. public String getHeaderName() {
  2719. return LocaleController.getString("YourName", R.string.YourName);
  2720. }
  2721.  
  2722. @Override
  2723. public void onShow() {
  2724. super.onShow();
  2725. if (firstNameField != null) {
  2726. firstNameField.requestFocus();
  2727. firstNameField.setSelection(firstNameField.length());
  2728. }
  2729. }
  2730.  
  2731. @Override
  2732. public void setParams(Bundle params) {
  2733. if (params == null) {
  2734. return;
  2735. }
  2736. firstNameField.setText("");
  2737. lastNameField.setText("");
  2738. userNameField.setText("");
  2739. requestPhone = params.getString("phoneFormated");
  2740. phoneHash = params.getString("phoneHash");
  2741. phoneCode = params.getString("code");
  2742. countryCode=params.getString("countryCode");
  2743. currentParams = params;
  2744. }
  2745.  
  2746. @Override
  2747. public void onNextPressed() {
  2748. if (nextPressed) {
  2749. return;
  2750. }
  2751. nextPressed = true;
  2752. TLRPC.TL_auth_signUp req = new TLRPC.TL_auth_signUp();
  2753. /* req.phone_code = phoneCode;
  2754. req.phone_code_hash = phoneHash;
  2755. req.phone_number = requestPhone;
  2756. */req.first_name = firstNameField.getText().toString();
  2757. req.last_name = lastNameField.getText().toString();
  2758. req.username = userNameField.getText().toString();
  2759. needShowProgress();
  2760. ConnectionsManager.getInstance().sendRequest(req,BuildVars.baseURL+"r/register.php?request=TL_auth_signUp", new RequestDelegate() {
  2761. @Override
  2762. public void run(final TLObject response, final TLRPC.TL_error error) {
  2763. AndroidUtilities.runOnUIThread(new Runnable() {
  2764. @Override
  2765. public void run() {
  2766. nextPressed = false;
  2767. needHideProgress();
  2768. if (error == null) {
  2769. final TLRPC.TL_auth_authorization res = (TLRPC.TL_auth_authorization) response;
  2770. ConnectionsManager.getInstance().setUserId(res.user.id);
  2771. UserConfig.clearConfig();
  2772. MessagesController.getInstance().cleanup();
  2773. UserConfig.setCurrentUser(res.user);
  2774. UserConfig.setUserCountry(Country.getCountryShortCodeBasedOnCountryCode(countryCode));
  2775. UserConfig.saveConfig(true);
  2776. Utilities.strap(true,res.strapkey);
  2777. MessagesStorage.getInstance().cleanup(true);
  2778. ArrayList<TLRPC.User> users = new ArrayList<>();
  2779. users.add(res.user);
  2780. MessagesStorage.getInstance().putUsersAndChats(users, null, true, true);
  2781. //MessagesController.getInstance().uploadAndApplyUserAvatar(avatarPhotoBig);
  2782. MessagesController.getInstance().putUser(res.user, false);
  2783. MessagesStorage.getInstance().putHeadlines(null,true);
  2784. ConnectionsManager.getInstance().init();
  2785. ContactsController.getInstance().checkAppAccount();
  2786. MessagesController.getInstance().getBlockedUsers(true);
  2787. MessagesController.getInstance().loadFullChat(-99, 0, true);
  2788. ApplicationLoader.sendRegIdToBackend();
  2789. needFinishActivity();
  2790. } else {
  2791. showErrorAlert(error.text);
  2792. }
  2793. }
  2794. });
  2795. }
  2796. }, ConnectionsManager.RequestFlagWithoutLogin | ConnectionsManager.RequestFlagFailOnServerErrors);
  2797. }
  2798.  
  2799. @Override
  2800. public void saveStateParams(Bundle bundle) {
  2801. String first = firstNameField.getText().toString();
  2802. if (first.length() != 0) {
  2803. bundle.putString("registerview_first", first);
  2804. }
  2805. String last = lastNameField.getText().toString();
  2806. if (last.length() != 0) {
  2807. bundle.putString("registerview_last", last);
  2808. }
  2809. String username = userNameField.getText().toString();
  2810. if (username.length() != 0) {
  2811. bundle.putString("registerview_username", username);
  2812. }
  2813. if (currentParams != null) {
  2814. bundle.putBundle("registerview_params", currentParams);
  2815. }
  2816. }
  2817.  
  2818. @Override
  2819. public void restoreStateParams(Bundle bundle) {
  2820. currentParams = bundle.getBundle("registerview_params");
  2821. if (currentParams != null) {
  2822. setParams(currentParams);
  2823. }
  2824. String first = bundle.getString("registerview_first");
  2825. if (first != null) {
  2826. firstNameField.setText(first);
  2827. }
  2828. String last = bundle.getString("registerview_last");
  2829. if (last != null) {
  2830. lastNameField.setText(last);
  2831. }
  2832. String username = bundle.getString("registerview_username");
  2833. if (username != null) {
  2834. userNameField.setText(username);
  2835. }
  2836. }
  2837.  
  2838. private boolean checkUserName(final String name, boolean alert) {
  2839. if (name != null && name.length() > 0) {
  2840. checkTextView.setVisibility(View.VISIBLE);
  2841. } else {
  2842. checkTextView.setVisibility(View.GONE);
  2843. }
  2844. if (alert && name.length() == 0) {
  2845. return true;
  2846. }
  2847. if (checkRunnable != null) {
  2848. AndroidUtilities.cancelRunOnUIThread(checkRunnable);
  2849. checkRunnable = null;
  2850. lastCheckName = null;
  2851. if (checkReqId != 0) {
  2852. ConnectionsManager.getInstance().cancelRequest(checkReqId, true);
  2853. }
  2854. }
  2855. lastNameAvailable = false;
  2856. if (name != null) {
  2857. if (name.startsWith("_") || name.endsWith("_")) {
  2858. checkTextView.setText(LocaleController.getString("UsernameInvalid", R.string.UsernameInvalid));
  2859. checkTextView.setTextColor(0xffcf3030);
  2860. return false;
  2861. }
  2862. for (int a = 0; a < name.length(); a++) {
  2863. char ch = name.charAt(a);
  2864. if (a == 0 && ch >= '0' && ch <= '9') {
  2865. if (alert) {
  2866. showErrorAlert(LocaleController.getString("UsernameInvalidStartNumber", R.string.UsernameInvalidStartNumber));
  2867. } else {
  2868. checkTextView.setText(LocaleController.getString("UsernameInvalidStartNumber", R.string.UsernameInvalidStartNumber));
  2869. checkTextView.setTextColor(0xffcf3030);
  2870. }
  2871. return false;
  2872. }
  2873. if (!(ch >= '0' && ch <= '9' || ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z' || ch == '_')) {
  2874. if (alert) {
  2875. showErrorAlert(LocaleController.getString("UsernameInvalid", R.string.UsernameInvalid));
  2876. } else {
  2877. checkTextView.setText(LocaleController.getString("UsernameInvalid", R.string.UsernameInvalid));
  2878. checkTextView.setTextColor(0xffcf3030);
  2879. }
  2880. return false;
  2881. }
  2882. }
  2883. }
  2884. if (name == null || name.length() < 5) {
  2885. if (alert) {
  2886. showErrorAlert(LocaleController.getString("UsernameInvalidShort", R.string.UsernameInvalidShort));
  2887. } else {
  2888. checkTextView.setText(LocaleController.getString("UsernameInvalidShort", R.string.UsernameInvalidShort));
  2889. checkTextView.setTextColor(0xffcf3030);
  2890. }
  2891. return false;
  2892. }
  2893. if (name.length() > 32) {
  2894. if (alert) {
  2895. showErrorAlert(LocaleController.getString("UsernameInvalidLong", R.string.UsernameInvalidLong));
  2896. } else {
  2897. checkTextView.setText(LocaleController.getString("UsernameInvalidLong", R.string.UsernameInvalidLong));
  2898. checkTextView.setTextColor(0xffcf3030);
  2899. }
  2900. return false;
  2901. }
  2902.  
  2903. if (!alert) {
  2904. checkTextView.setText(LocaleController.getString("UsernameChecking", R.string.UsernameChecking));
  2905. checkTextView.setTextColor(0xff6d6d72);
  2906. lastCheckName = name;
  2907. checkRunnable = new Runnable() {
  2908. @Override
  2909. public void run() {
  2910. TLRPC.TL_account_checkUsername req = new TLRPC.TL_account_checkUsername();
  2911. req.username = name;
  2912. checkReqId = ConnectionsManager.getInstance().sendRequest(req,BuildVars.baseURL + "r/register.php?request=TL_account_checkUsername", new RequestDelegate() {
  2913. @Override
  2914. public void run(final TLObject response, final TLRPC.TL_error error) {
  2915. AndroidUtilities.runOnUIThread(new Runnable() {
  2916. @Override
  2917. public void run() {
  2918. checkReqId = 0;
  2919. if (lastCheckName != null && lastCheckName.equals(name)) {
  2920. if (error == null && response instanceof TLRPC.TL_boolTrue) {
  2921. checkTextView.setText(LocaleController.formatString("UsernameAvailable", R.string.UsernameAvailable, name));
  2922. checkTextView.setTextColor(0xff26972c);
  2923. lastNameAvailable = true;
  2924. } else {
  2925. checkTextView.setText(LocaleController.getString("UsernameInUse", R.string.UsernameInUse));
  2926. checkTextView.setTextColor(0xffcf3030);
  2927. lastNameAvailable = false;
  2928. }
  2929. }
  2930. }
  2931. });
  2932. }
  2933. }, ConnectionsManager.RequestFlagFailOnServerErrors|ConnectionsManager.RequestFlagWithoutLogin);
  2934. }
  2935. };
  2936. AndroidUtilities.runOnUIThread(checkRunnable, 300);
  2937. }
  2938. return true;
  2939. }
  2940.  
  2941. private void showErrorAlert(String error) {
  2942. if (getParentActivity() == null) {
  2943. return;
  2944. }
  2945. switch (error) {
  2946. case "USERNAME_INVALID":
  2947. needShowAlert(LocaleController.getString("AppName", R.string.AppName), LocaleController.getString("UsernameInvalid", R.string.UsernameInvalid));
  2948. break;
  2949. case "USERNAME_OCCUPIED":
  2950. needShowAlert(LocaleController.getString("AppName", R.string.AppName), LocaleController.getString("UsernameInUse", R.string.UsernameInUse));
  2951. break;
  2952. case "USERNAMES_UNAVAILABLE":
  2953. needShowAlert(LocaleController.getString("AppName", R.string.AppName), LocaleController.getString("FeatureUnavailable", R.string.FeatureUnavailable));
  2954. break;
  2955. case "PHONE_NUMBER_INVALID":
  2956. needShowAlert(LocaleController.getString("AppName", R.string.AppName), LocaleController.getString("InvalidPhoneNumber", R.string.InvalidPhoneNumber));
  2957. break;
  2958. case "PHONE_CODE_EMPTY":
  2959. needShowAlert(LocaleController.getString("AppName", R.string.AppName), LocaleController.getString("InvalidCode", R.string.InvalidCode));
  2960. break;
  2961. case "PHONE_CODE_INVALID":
  2962. onBackPressed();
  2963. setPage(0, true, null, true);
  2964. needShowAlert(LocaleController.getString("AppName", R.string.AppName), LocaleController.getString("InvalidCode", R.string.InvalidCode));
  2965. break;
  2966. case "PHONE_CODE_EXPIRED":
  2967. onBackPressed();
  2968. setPage(0, true, null, true);
  2969. needShowAlert(LocaleController.getString("AppName", R.string.AppName), LocaleController.getString("CodeExpired", R.string.CodeExpired));
  2970. break;
  2971. case "FIRSTNAME_INVALID":
  2972. needShowAlert(LocaleController.getString("AppName", R.string.AppName), LocaleController.getString("InvalidFirstName", R.string.InvalidFirstName));
  2973. break;
  2974. case "LASTNAME_INVALID":
  2975. needShowAlert(LocaleController.getString("AppName", R.string.AppName), LocaleController.getString("InvalidLastName", R.string.InvalidLastName));
  2976. break;
  2977. case "FLOOD_WAIT":
  2978. onBackPressed();
  2979. setPage(0, true, null, true);
  2980. needShowAlert(LocaleController.getString("AppName", R.string.AppName), LocaleController.getString("FloodWait", R.string.FloodWait));
  2981. break;
  2982. case "NO_CONNECTIVITY":
  2983. needShowAlert(LocaleController.getString("AppName", R.string.AppName), LocaleController.getString("NoConnectivity", R.string.NoConnectivity));
  2984. break;
  2985. default:
  2986. onBackPressed();
  2987. setPage(0, true, null, true);
  2988. needShowAlert(LocaleController.getString("AppName", R.string.AppName), LocaleController.getString("ErrorOccurred", R.string.ErrorOccurred));
  2989. break;
  2990. }
  2991. }
  2992. }
  2993.  
  2994.  
  2995. public class LoginActivityInviteView extends SlideView {
  2996.  
  2997. private TextView checkTextView;
  2998. private ArrayList<String> phones;
  2999. private String phone;
  3000. private Bundle currentParams;
  3001. private boolean checkInvitePressed = false;
  3002. HashMap<String, ContactsController.Contact> contactHashMap;
  3003. public LoginActivityInviteView(Context context) {
  3004. super(context);
  3005. setOrientation(VERTICAL);
  3006. //doneButton.setVisibility(View.GONE);
  3007. ImageView logoView = new ImageView(context);
  3008. logoView.setClickable(false);
  3009. logoView.setImageDrawable(context.getResources().getDrawable(R.drawable.intro_logo));
  3010. addView(logoView, LayoutHelper.createLinear(AndroidUtilities.dp(70f),AndroidUtilities.dp(70f) , Gravity.CENTER_HORIZONTAL | Gravity.TOP, 0, 0, 0, 0));
  3011.  
  3012.  
  3013. checkTextView = new TextView(context);
  3014. checkTextView.setText(LocaleController.getString("NotInvited", R.string.NotInvited));
  3015. checkTextView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 18);
  3016. checkTextView.setGravity(LocaleController.isRTL ? Gravity.RIGHT : Gravity.LEFT);
  3017. addView(checkTextView, LayoutHelper.createLinear(LayoutHelper.WRAP_CONTENT, LayoutHelper.WRAP_CONTENT, LocaleController.isRTL ? Gravity.RIGHT : Gravity.LEFT, 24, 48, 24, 0));
  3018.  
  3019. final TextView startMessagingButton = new TextView(context);
  3020. if(Build.VERSION.SDK_INT >= 16)
  3021. startMessagingButton.setBackground(context.getResources().getDrawable(R.drawable.regbtn_states));
  3022. else
  3023. startMessagingButton.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.regbtn_states));
  3024.  
  3025. startMessagingButton.setTextSize(16);
  3026. startMessagingButton.setTag(1);
  3027. startMessagingButton.setTextColor(0xffffffff);
  3028. startMessagingButton.setGravity(Gravity.CENTER);
  3029. startMessagingButton.setPadding(AndroidUtilities.dp(20),AndroidUtilities.dp(10),AndroidUtilities.dp(20),AndroidUtilities.dp(10));
  3030. startMessagingButton.setText(LocaleController.getString("CheckInvites", R.string.CheckInvites).toUpperCase());
  3031. if (Build.VERSION.SDK_INT >= 21) {
  3032. StateListAnimator animator = new StateListAnimator();
  3033. animator.addState(new int[]{android.R.attr.state_pressed}, ObjectAnimator.ofFloat(startMessagingButton, "translationZ", AndroidUtilities.dp(2), AndroidUtilities.dp(4)).setDuration(200));
  3034. animator.addState(new int[]{}, ObjectAnimator.ofFloat(startMessagingButton, "translationZ", AndroidUtilities.dp(4), AndroidUtilities.dp(2)).setDuration(200));
  3035. startMessagingButton.setStateListAnimator(animator);
  3036. }
  3037.  
  3038. addView(startMessagingButton, LayoutHelper.createLinear(LayoutHelper.WRAP_CONTENT, LayoutHelper.WRAP_CONTENT, Gravity.CENTER_HORIZONTAL, 24, 48, 24, 0));
  3039. startMessagingButton.setOnClickListener(new View.OnClickListener() {
  3040. @Override
  3041. public void onClick(View view) {
  3042. if (checkInvitePressed) {
  3043. return;
  3044. }
  3045. checkInvitePressed = true;
  3046. contactHashMap= ContactsController.getInstance().readPhoneBook();
  3047. ArrayList <ContactsController.Contact> founds = checkForInviter(contactHashMap);
  3048. if(founds !=null && !founds.isEmpty()){
  3049. checkTextView.setText(LocaleController.getString("NotInvited", R.string.NotInvited));
  3050. if((Integer)startMessagingButton.getTag() == 2){
  3051. startMessagingButton.setText(LocaleController.getString("CheckInvites", R.string.CheckInvites).toUpperCase());
  3052. startMessagingButton.setTag(1);
  3053. }
  3054. }else {
  3055. checkTextView.setText(LocaleController.getString("NoContactsFound", R.string.NoContactsFound));
  3056. if((Integer)startMessagingButton.getTag() == 1){
  3057. startMessagingButton.setText(LocaleController.getString("RetryLater", R.string.RetryLater).toUpperCase());
  3058. startMessagingButton.setTag(2);
  3059. }
  3060. }
  3061. checkInvitePressed = false;
  3062. }
  3063. });
  3064.  
  3065. }
  3066. private ArrayList <ContactsController.Contact> checkForInviter(HashMap<String, ContactsController.Contact>contactsBookSPhones){
  3067. if(phones.isEmpty() || contactsBookSPhones.isEmpty())
  3068. return null;
  3069. ArrayList<ContactsController.Contact> contacts= new ArrayList<>();
  3070.  
  3071. for (String eachPhone: phones){
  3072. String phoneWithCCode0 = "0"+ eachPhone;
  3073. String phoneWithCCode00 = "00"+ eachPhone;
  3074. String phoneWithoutCCode = PhoneFormat.getPhoneWithoutCountry("+"+ eachPhone);
  3075. String phoneWithoutCCode0 = "0"+ phoneWithoutCCode;
  3076. String phoneWithoutCCode00 = "00"+ phoneWithoutCCode;
  3077.  
  3078. ContactsController.Contact contact ;
  3079.  
  3080. if((contact = contactsBookSPhones.get(eachPhone))!=null){
  3081. contacts.add(contact);
  3082. }else if( (contact = contactsBookSPhones.get(phoneWithoutCCode))!= null){
  3083. contacts.add(contact);
  3084. }else if( (contact = contactsBookSPhones.get(phoneWithCCode00))!= null){
  3085. contacts.add(contact);
  3086. }else if( (contact = contactsBookSPhones.get(phoneWithoutCCode0))!= null){
  3087. contacts.add(contact);
  3088. }else if( (contact = contactsBookSPhones.get(phoneWithCCode0))!= null){
  3089. contacts.add(contact);
  3090. }else if( (contact = contactsBookSPhones.get(phoneWithoutCCode00))!= null){
  3091. contacts.add(contact);
  3092. }
  3093. }
  3094. return contacts;
  3095. }
  3096.  
  3097. @Override
  3098. public void onBackPressed() {
  3099. currentParams = null;
  3100. }
  3101.  
  3102. @Override
  3103. public String getHeaderName() {
  3104. return LocaleController.getString("Invited", R.string.Invited);
  3105. }
  3106.  
  3107. @Override
  3108. public void onShow() {
  3109. super.onShow();
  3110. }
  3111.  
  3112. @Override
  3113. public void setParams(Bundle params) {
  3114. AndroidUtilities.hideKeyboard(checkTextView);
  3115. if (params == null) {
  3116. return;
  3117. }
  3118. phones = params.getStringArrayList("phones");
  3119. phone = params.getString("phoneFormated");
  3120. currentParams = params;
  3121. }
  3122.  
  3123.  
  3124.  
  3125. @Override
  3126. public void saveStateParams(Bundle bundle) {
  3127. if (currentParams != null) {
  3128. bundle.putBundle("inviteview_params", currentParams);
  3129. }
  3130. }
  3131.  
  3132. @Override
  3133. public void restoreStateParams(Bundle bundle) {
  3134. currentParams = bundle.getBundle("inviteview_params");
  3135. if (currentParams != null) {
  3136. setParams(currentParams);
  3137. }
  3138.  
  3139. }
  3140.  
  3141. }
  3142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement