NekoX/TMessagesProj/src/main/java/org/telegram/ui/TwoStepVerificationActivity...

1152 lines
60 KiB
Java
Raw Normal View History

2015-04-09 20:00:14 +02:00
/*
2015-10-29 18:10:07 +01:00
* This is the source code of Telegram for Android v. 3.x.x
2015-04-09 20:00:14 +02:00
* It is licensed under GNU GPL v. 2 or later.
* You should have received a copy of the license in this archive (see LICENSE).
*
2017-03-31 01:58:05 +02:00
* Copyright Nikolai Kudashov, 2013-2017.
2015-04-09 20:00:14 +02:00
*/
package org.telegram.ui;
import android.content.Context;
2019-12-31 14:08:08 +01:00
import android.content.DialogInterface;
2015-04-09 20:00:14 +02:00
import android.graphics.Typeface;
2021-12-07 14:02:02 +01:00
import android.os.Bundle;
2015-04-09 20:00:14 +02:00
import android.os.Vibrator;
import android.text.InputType;
2018-07-30 04:07:02 +02:00
import android.text.TextUtils;
2015-04-09 20:00:14 +02:00
import android.text.method.PasswordTransformationMethod;
import android.util.TypedValue;
import android.view.ActionMode;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
2015-09-24 22:52:02 +02:00
import org.telegram.messenger.AndroidUtilities;
import org.telegram.messenger.LocaleController;
import org.telegram.messenger.NotificationCenter;
2015-04-09 20:00:14 +02:00
import org.telegram.messenger.FileLog;
import org.telegram.messenger.R;
2018-08-27 10:33:11 +02:00
import org.telegram.messenger.SRPHelper;
2018-07-30 04:07:02 +02:00
import org.telegram.messenger.UserConfig;
2015-09-24 22:52:02 +02:00
import org.telegram.tgnet.ConnectionsManager;
import org.telegram.tgnet.RequestDelegate;
import org.telegram.tgnet.TLRPC;
2015-04-09 20:00:14 +02:00
import org.telegram.messenger.Utilities;
import org.telegram.ui.ActionBar.ActionBar;
import org.telegram.ui.ActionBar.ActionBarMenu;
import org.telegram.ui.ActionBar.ActionBarMenuItem;
2017-03-31 01:58:05 +02:00
import org.telegram.ui.ActionBar.AlertDialog;
2015-04-09 20:00:14 +02:00
import org.telegram.ui.ActionBar.BaseFragment;
2021-07-15 16:24:57 +02:00
import org.telegram.ui.ActionBar.SimpleTextView;
2017-03-31 01:58:05 +02:00
import org.telegram.ui.ActionBar.Theme;
import org.telegram.ui.ActionBar.ThemeDescription;
2019-01-23 18:03:33 +01:00
import org.telegram.ui.Cells.EditTextSettingsCell;
2015-04-09 20:00:14 +02:00
import org.telegram.ui.Cells.TextInfoPrivacyCell;
import org.telegram.ui.Cells.TextSettingsCell;
2018-08-27 10:33:11 +02:00
import org.telegram.ui.Components.AlertsCreator;
2017-12-08 18:35:59 +01:00
import org.telegram.ui.Components.EditTextBoldCursor;
2017-03-31 01:58:05 +02:00
import org.telegram.ui.Components.EmptyTextProgressView;
import org.telegram.ui.Components.LayoutHelper;
2017-03-31 01:58:05 +02:00
import org.telegram.ui.Components.RecyclerListView;
2015-04-09 20:00:14 +02:00
2020-04-24 11:21:58 +02:00
import java.util.ArrayList;
2021-07-15 16:24:57 +02:00
import java.util.Locale;
2018-08-27 10:33:11 +02:00
2019-05-14 14:08:05 +02:00
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
2015-04-09 20:00:14 +02:00
public class TwoStepVerificationActivity extends BaseFragment implements NotificationCenter.NotificationCenterDelegate {
private ListAdapter listAdapter;
2017-03-31 01:58:05 +02:00
private RecyclerListView listView;
2015-04-09 20:00:14 +02:00
private TextView titleTextView;
private TextView bottomTextView;
2021-07-15 16:24:57 +02:00
private SimpleTextView bottomButton;
private TextView cancelResetButton;
2017-12-08 18:35:59 +01:00
private EditTextBoldCursor passwordEditText;
2017-03-31 01:58:05 +02:00
private AlertDialog progressDialog;
private EmptyTextProgressView emptyView;
2015-04-09 20:00:14 +02:00
private ActionBarMenuItem doneItem;
private ScrollView scrollView;
2019-01-23 18:03:33 +01:00
2015-04-09 20:00:14 +02:00
private String firstPassword;
private String hint;
private String email;
private boolean emailOnly;
private boolean loading;
private boolean destroyed;
2018-07-30 04:07:02 +02:00
private boolean paused;
2018-08-27 10:33:11 +02:00
private TLRPC.TL_account_password currentPassword;
2015-04-09 20:00:14 +02:00
private boolean passwordEntered = true;
private byte[] currentPasswordHash = new byte[0];
2018-07-30 04:07:02 +02:00
private long currentSecretId;
private byte[] currentSecret;
2015-04-09 20:00:14 +02:00
2021-07-15 16:24:57 +02:00
private boolean resetPasswordOnShow;
2015-04-09 20:00:14 +02:00
private int setPasswordRow;
private int setPasswordDetailRow;
private int changePasswordRow;
private int turnPasswordOffRow;
private int setRecoveryEmailRow;
private int changeRecoveryEmailRow;
private int passwordEnabledDetailRow;
private int rowCount;
2021-07-15 16:24:57 +02:00
private boolean forgotPasswordOnShow;
2021-12-07 14:02:02 +01:00
int otherwiseReloginDays = -1;
2021-07-15 16:24:57 +02:00
2019-07-18 15:01:39 +02:00
private TwoStepVerificationActivityDelegate delegate;
public interface TwoStepVerificationActivityDelegate {
void didEnterPassword(TLRPC.InputCheckPasswordSRP password);
}
2015-04-09 20:00:14 +02:00
private final static int done_button = 1;
public TwoStepVerificationActivity() {
2015-04-09 20:00:14 +02:00
super();
2015-04-09 20:00:14 +02:00
}
public TwoStepVerificationActivity(int account) {
2018-07-30 04:07:02 +02:00
super();
currentAccount = account;
}
public void setPassword(TLRPC.TL_account_password password) {
2018-07-30 04:07:02 +02:00
currentPassword = password;
2021-07-15 16:24:57 +02:00
passwordEntered = false;
}
public void setCurrentPasswordParams(TLRPC.TL_account_password password, byte[] passwordHash, long secretId, byte[] secret) {
currentPassword = password;
currentPasswordHash = passwordHash;
currentSecret = secret;
currentSecretId = secretId;
passwordEntered = currentPasswordHash != null && currentPasswordHash.length > 0 || !currentPassword.has_password;
2018-07-30 04:07:02 +02:00
}
2015-04-09 20:00:14 +02:00
@Override
public boolean onFragmentCreate() {
super.onFragmentCreate();
if (currentPassword == null || currentPassword.current_algo == null || currentPasswordHash == null || currentPasswordHash.length <= 0) {
2021-07-15 16:24:57 +02:00
loadPasswordInfo(true, currentPassword != null);
2015-04-09 20:00:14 +02:00
}
updateRows();
NotificationCenter.getInstance(currentAccount).addObserver(this, NotificationCenter.twoStepPasswordChanged);
2015-04-09 20:00:14 +02:00
return true;
}
@Override
public void onFragmentDestroy() {
super.onFragmentDestroy();
2021-07-15 16:24:57 +02:00
AndroidUtilities.cancelRunOnUIThread(updateTimeRunnable);
NotificationCenter.getInstance(currentAccount).removeObserver(this, NotificationCenter.twoStepPasswordChanged);
destroyed = true;
2015-04-09 20:00:14 +02:00
if (progressDialog != null) {
try {
progressDialog.dismiss();
} catch (Exception e) {
2017-03-31 01:58:05 +02:00
FileLog.e(e);
2015-04-09 20:00:14 +02:00
}
progressDialog = null;
}
2015-09-24 22:52:02 +02:00
AndroidUtilities.removeAdjustResize(getParentActivity(), classGuid);
2015-04-09 20:00:14 +02:00
}
@Override
2015-07-22 20:56:37 +02:00
public View createView(Context context) {
2015-04-09 20:00:14 +02:00
actionBar.setBackButtonImage(R.drawable.ic_ab_back);
actionBar.setAllowOverlayTitle(false);
actionBar.setActionBarMenuOnItemClick(new ActionBar.ActionBarMenuOnItemClick() {
@Override
public void onItemClick(int id) {
if (id == -1) {
2021-12-07 14:02:02 +01:00
if (otherwiseReloginDays >= 0) {
showSetForcePasswordAlert();
} else {
finishFragment();
}
2015-04-09 20:00:14 +02:00
} else if (id == done_button) {
processDone();
}
}
});
fragmentView = new FrameLayout(context);
FrameLayout frameLayout = (FrameLayout) fragmentView;
2018-07-30 04:07:02 +02:00
frameLayout.setBackgroundColor(Theme.getColor(Theme.key_windowBackgroundWhite));
2015-04-09 20:00:14 +02:00
ActionBarMenu menu = actionBar.createMenu();
2020-12-24 06:36:01 +01:00
doneItem = menu.addItemWithWidth(done_button, R.drawable.ic_done, AndroidUtilities.dp(56), LocaleController.getString("Done", R.string.Done));
2015-04-09 20:00:14 +02:00
scrollView = new ScrollView(context);
scrollView.setFillViewport(true);
2017-03-31 01:58:05 +02:00
frameLayout.addView(scrollView, LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, LayoutHelper.MATCH_PARENT));
2015-04-09 20:00:14 +02:00
LinearLayout linearLayout = new LinearLayout(context);
linearLayout.setOrientation(LinearLayout.VERTICAL);
2017-03-31 01:58:05 +02:00
scrollView.addView(linearLayout, LayoutHelper.createScroll(LayoutHelper.MATCH_PARENT, LayoutHelper.WRAP_CONTENT, Gravity.LEFT | Gravity.TOP));
2015-04-09 20:00:14 +02:00
titleTextView = new TextView(context);
2017-03-31 01:58:05 +02:00
titleTextView.setTextColor(Theme.getColor(Theme.key_windowBackgroundWhiteGrayText6));
2015-04-09 20:00:14 +02:00
titleTextView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 18);
titleTextView.setGravity(Gravity.CENTER_HORIZONTAL);
2019-07-18 15:01:39 +02:00
titleTextView.setPadding(AndroidUtilities.dp(40), 0, AndroidUtilities.dp(40), 0);
2017-03-31 01:58:05 +02:00
linearLayout.addView(titleTextView, LayoutHelper.createLinear(LayoutHelper.WRAP_CONTENT, LayoutHelper.WRAP_CONTENT, Gravity.CENTER_HORIZONTAL, 0, 38, 0, 0));
2015-04-09 20:00:14 +02:00
2017-12-08 18:35:59 +01:00
passwordEditText = new EditTextBoldCursor(context);
2015-04-09 20:00:14 +02:00
passwordEditText.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 20);
2017-03-31 01:58:05 +02:00
passwordEditText.setTextColor(Theme.getColor(Theme.key_windowBackgroundWhiteBlackText));
passwordEditText.setHintTextColor(Theme.getColor(Theme.key_windowBackgroundWhiteHintText));
passwordEditText.setBackgroundDrawable(Theme.createEditTextDrawable(context, false));
2015-04-09 20:00:14 +02:00
passwordEditText.setMaxLines(1);
passwordEditText.setLines(1);
passwordEditText.setGravity(Gravity.CENTER_HORIZONTAL);
passwordEditText.setSingleLine(true);
passwordEditText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
passwordEditText.setTransformationMethod(PasswordTransformationMethod.getInstance());
passwordEditText.setTypeface(Typeface.DEFAULT);
2017-12-08 18:35:59 +01:00
passwordEditText.setCursorColor(Theme.getColor(Theme.key_windowBackgroundWhiteBlackText));
passwordEditText.setCursorWidth(1.5f);
2017-03-31 01:58:05 +02:00
linearLayout.addView(passwordEditText, LayoutHelper.createLinear(LayoutHelper.MATCH_PARENT, 36, Gravity.TOP | Gravity.LEFT, 40, 32, 40, 0));
2018-08-27 10:33:11 +02:00
passwordEditText.setOnEditorActionListener((textView, i, keyEvent) -> {
if (i == EditorInfo.IME_ACTION_NEXT || i == EditorInfo.IME_ACTION_DONE) {
processDone();
return true;
2015-04-09 20:00:14 +02:00
}
2018-08-27 10:33:11 +02:00
return false;
2015-04-09 20:00:14 +02:00
});
passwordEditText.setCustomSelectionActionModeCallback(new ActionMode.Callback() {
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
2015-04-09 20:00:14 +02:00
public void onDestroyActionMode(ActionMode mode) {
}
2015-04-09 20:00:14 +02:00
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
return false;
}
2015-04-09 20:00:14 +02:00
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
return false;
}
});
2015-04-09 20:00:14 +02:00
bottomTextView = new TextView(context);
2017-03-31 01:58:05 +02:00
bottomTextView.setTextColor(Theme.getColor(Theme.key_windowBackgroundWhiteGrayText6));
2015-04-09 20:00:14 +02:00
bottomTextView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 14);
bottomTextView.setGravity((LocaleController.isRTL ? Gravity.RIGHT : Gravity.LEFT) | Gravity.TOP);
bottomTextView.setText(LocaleController.getString("YourEmailInfo", R.string.YourEmailInfo));
2017-03-31 01:58:05 +02:00
linearLayout.addView(bottomTextView, LayoutHelper.createLinear(LayoutHelper.WRAP_CONTENT, LayoutHelper.WRAP_CONTENT, (LocaleController.isRTL ? Gravity.RIGHT : Gravity.LEFT) | Gravity.TOP, 40, 30, 40, 0));
2015-04-09 20:00:14 +02:00
LinearLayout linearLayout2 = new LinearLayout(context);
2021-07-15 16:24:57 +02:00
linearLayout2.setOrientation(LinearLayout.VERTICAL);
2015-04-09 20:00:14 +02:00
linearLayout2.setGravity(Gravity.BOTTOM | Gravity.CENTER_VERTICAL);
2021-07-15 16:24:57 +02:00
linearLayout2.setClipChildren(false);
2017-03-31 01:58:05 +02:00
linearLayout.addView(linearLayout2, LayoutHelper.createLinear(LayoutHelper.MATCH_PARENT, LayoutHelper.MATCH_PARENT));
2015-04-09 20:00:14 +02:00
2021-07-15 16:24:57 +02:00
bottomButton = new SimpleTextView(context);
bottomButton.setTextSize(14);
2015-04-09 20:00:14 +02:00
bottomButton.setGravity((LocaleController.isRTL ? Gravity.RIGHT : Gravity.LEFT) | Gravity.BOTTOM);
bottomButton.setPadding(0, AndroidUtilities.dp(10), 0, 0);
2021-07-15 16:24:57 +02:00
linearLayout2.addView(bottomButton, LayoutHelper.createLinear(LayoutHelper.MATCH_PARENT, 40, (LocaleController.isRTL ? Gravity.RIGHT : Gravity.LEFT) | Gravity.BOTTOM, 40, 0, 40, 14));
bottomButton.setOnClickListener(v -> onPasswordForgot());
cancelResetButton = new TextView(context);
cancelResetButton.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 14);
cancelResetButton.setGravity((LocaleController.isRTL ? Gravity.RIGHT : Gravity.LEFT) | Gravity.BOTTOM);
cancelResetButton.setPadding(0, AndroidUtilities.dp(10), 0, 0);
cancelResetButton.setText(LocaleController.getString("CancelReset", R.string.CancelReset));
cancelResetButton.setTextColor(Theme.getColor(Theme.key_windowBackgroundWhiteBlueText4));
linearLayout2.addView(cancelResetButton, LayoutHelper.createLinear(LayoutHelper.MATCH_PARENT, LayoutHelper.WRAP_CONTENT, (LocaleController.isRTL ? Gravity.RIGHT : Gravity.LEFT) | Gravity.BOTTOM, 40, 0, 40, 26));
cancelResetButton.setOnClickListener(v -> cancelPasswordReset());
2015-04-09 20:00:14 +02:00
emptyView = new EmptyTextProgressView(context);
emptyView.showProgress();
frameLayout.addView(emptyView, LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, LayoutHelper.MATCH_PARENT));
listView = new RecyclerListView(context);
listView.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false));
listView.setEmptyView(emptyView);
listView.setVerticalScrollBarEnabled(false);
frameLayout.addView(listView, LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, LayoutHelper.MATCH_PARENT));
listView.setAdapter(listAdapter = new ListAdapter(context));
listView.setOnItemClickListener((view, position) -> {
if (position == setPasswordRow || position == changePasswordRow) {
TwoStepVerificationSetupActivity fragment = new TwoStepVerificationSetupActivity(currentAccount, TwoStepVerificationSetupActivity.TYPE_ENTER_FIRST, currentPassword);
fragment.addFragmentToClose(this);
fragment.setCurrentPasswordParams(currentPasswordHash, currentSecretId, currentSecret, false);
presentFragment(fragment);
} else if (position == setRecoveryEmailRow || position == changeRecoveryEmailRow) {
TwoStepVerificationSetupActivity fragment = new TwoStepVerificationSetupActivity(currentAccount, TwoStepVerificationSetupActivity.TYPE_ENTER_EMAIL, currentPassword);
fragment.addFragmentToClose(this);
fragment.setCurrentPasswordParams(currentPasswordHash, currentSecretId, currentSecret, true);
presentFragment(fragment);
} else if (position == turnPasswordOffRow) {
AlertDialog.Builder builder = new AlertDialog.Builder(getParentActivity());
2019-01-23 18:03:33 +01:00
String text = LocaleController.getString("TurnPasswordOffQuestion", R.string.TurnPasswordOffQuestion);
if (currentPassword.has_secure_values) {
text += "\n\n" + LocaleController.getString("TurnPasswordOffPassport", R.string.TurnPasswordOffPassport);
2019-01-23 18:03:33 +01:00
}
String title = LocaleController.getString("TurnPasswordOffQuestionTitle", R.string.TurnPasswordOffQuestionTitle);
String buttonText = LocaleController.getString("Disable", R.string.Disable);
2019-01-23 18:03:33 +01:00
builder.setMessage(text);
builder.setTitle(title);
builder.setPositiveButton(buttonText, (dialogInterface, i) -> clearPassword());
builder.setNegativeButton(LocaleController.getString("Cancel", R.string.Cancel), null);
AlertDialog alertDialog = builder.create();
showDialog(alertDialog);
TextView button = (TextView) alertDialog.getButton(DialogInterface.BUTTON_POSITIVE);
if (button != null) {
button.setTextColor(Theme.getColor(Theme.key_dialogTextRed2));
2015-04-09 20:00:14 +02:00
}
}
});
2015-04-09 20:00:14 +02:00
updateRows();
2015-04-09 20:00:14 +02:00
actionBar.setTitle(LocaleController.getString("TwoStepVerificationTitle", R.string.TwoStepVerificationTitle));
if (delegate != null) {
titleTextView.setText(LocaleController.getString("PleaseEnterCurrentPasswordTransfer", R.string.PleaseEnterCurrentPasswordTransfer));
} else {
titleTextView.setText(LocaleController.getString("PleaseEnterCurrentPassword", R.string.PleaseEnterCurrentPassword));
2015-04-09 20:00:14 +02:00
}
if (passwordEntered) {
2018-08-27 10:33:11 +02:00
fragmentView.setBackgroundColor(Theme.getColor(Theme.key_windowBackgroundGray));
fragmentView.setTag(Theme.key_windowBackgroundGray);
} else {
fragmentView.setBackgroundColor(Theme.getColor(Theme.key_windowBackgroundWhite));
fragmentView.setTag(Theme.key_windowBackgroundWhite);
}
2015-04-09 20:00:14 +02:00
return fragmentView;
}
2021-07-15 16:24:57 +02:00
private Runnable updateTimeRunnable = this::updateBottomButton;
private void cancelPasswordReset() {
if (getParentActivity() == null) {
return;
}
AlertDialog.Builder builder = new AlertDialog.Builder(getParentActivity());
builder.setPositiveButton(LocaleController.getString("CancelPasswordResetYes", R.string.CancelPasswordResetYes), (dialog, which) -> {
TLRPC.TL_account_declinePasswordReset req = new TLRPC.TL_account_declinePasswordReset();
getConnectionsManager().sendRequest(req, (response, error) -> AndroidUtilities.runOnUIThread(() -> {
if (response instanceof TLRPC.TL_boolTrue) {
currentPassword.pending_reset_date = 0;
updateBottomButton();
}
}));
});
builder.setNegativeButton(LocaleController.getString("CancelPasswordResetNo", R.string.CancelPasswordResetNo), null);
builder.setTitle(LocaleController.getString("CancelReset", R.string.CancelReset));
builder.setMessage(LocaleController.getString("CancelPasswordReset", R.string.CancelPasswordReset));
showDialog(builder.create());
}
public void setForgotPasswordOnShow() {
forgotPasswordOnShow = true;
}
private void resetPassword() {
needShowProgress(true);
TLRPC.TL_account_resetPassword req = new TLRPC.TL_account_resetPassword();
getConnectionsManager().sendRequest(req, (response, error) -> AndroidUtilities.runOnUIThread(() -> {
needHideProgress();
if (response instanceof TLRPC.TL_account_resetPasswordOk) {
AlertDialog.Builder builder = new AlertDialog.Builder(getParentActivity());
builder.setNegativeButton(LocaleController.getString("OK", R.string.OK), null);
builder.setTitle(LocaleController.getString("ResetPassword", R.string.ResetPassword));
builder.setMessage(LocaleController.getString("RestorePasswordResetPasswordOk", R.string.RestorePasswordResetPasswordOk));
showDialog(builder.create(), dialog -> {
getNotificationCenter().postNotificationName(NotificationCenter.didSetOrRemoveTwoStepPassword);
finishFragment();
});
} else if (response instanceof TLRPC.TL_account_resetPasswordRequestedWait) {
TLRPC.TL_account_resetPasswordRequestedWait res = (TLRPC.TL_account_resetPasswordRequestedWait) response;
currentPassword.pending_reset_date = res.until_date;
updateBottomButton();
} else if (response instanceof TLRPC.TL_account_resetPasswordFailedWait) {
TLRPC.TL_account_resetPasswordFailedWait res = (TLRPC.TL_account_resetPasswordFailedWait) response;
int time = res.retry_date - getConnectionsManager().getCurrentTime();
String timeString;
if (time > 24 * 60 * 60) {
timeString = LocaleController.formatPluralString("Days", time / (24 * 60 * 60));
} else if (time > 60 * 60) {
timeString = LocaleController.formatPluralString("Hours", time / (24 * 60 * 60));
} else if (time > 60) {
timeString = LocaleController.formatPluralString("Minutes", time / 60);
} else {
timeString = LocaleController.formatPluralString("Seconds", Math.max(1, time));
}
showAlertWithText(LocaleController.getString("ResetPassword", R.string.ResetPassword), LocaleController.formatString("ResetPasswordWait", R.string.ResetPasswordWait, timeString));
}
}));
}
private void updateBottomButton() {
if (currentPassword == null || bottomButton == null || bottomButton.getVisibility() != View.VISIBLE) {
AndroidUtilities.cancelRunOnUIThread(updateTimeRunnable);
if (cancelResetButton != null) {
cancelResetButton.setVisibility(View.GONE);
}
return;
}
LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) bottomButton.getLayoutParams();
if (currentPassword.pending_reset_date == 0 || getConnectionsManager().getCurrentTime() > currentPassword.pending_reset_date) {
if (currentPassword.pending_reset_date == 0) {
bottomButton.setText(LocaleController.getString("ForgotPassword", R.string.ForgotPassword));
cancelResetButton.setVisibility(View.GONE);
layoutParams.bottomMargin = AndroidUtilities.dp(14);
layoutParams.height = AndroidUtilities.dp(40);
} else {
bottomButton.setText(LocaleController.getString("ResetPassword", R.string.ResetPassword));
cancelResetButton.setVisibility(View.VISIBLE);
layoutParams.bottomMargin = 0;
layoutParams.height = AndroidUtilities.dp(22);
}
bottomButton.setTextColor(Theme.getColor(Theme.key_windowBackgroundWhiteBlueText4));
AndroidUtilities.cancelRunOnUIThread(updateTimeRunnable);
} else {
int t = Math.max(1, currentPassword.pending_reset_date - getConnectionsManager().getCurrentTime());
String time;
if (t > 24 * 60 * 60) {
time = LocaleController.formatPluralString("Days", t / (24 * 60 * 60));
} else if (t >= 60 * 60) {
time = LocaleController.formatPluralString("Hours", t / (60 * 60));
} else {
time = String.format(Locale.US, "%02d:%02d", t / 60, t % 60);
}
bottomButton.setText(LocaleController.formatString("RestorePasswordResetIn", R.string.RestorePasswordResetIn, time));
bottomButton.setTextColor(Theme.getColor(Theme.key_windowBackgroundWhiteGrayText6));
cancelResetButton.setVisibility(View.VISIBLE);
layoutParams.bottomMargin = 0;
layoutParams.height = AndroidUtilities.dp(22);
AndroidUtilities.cancelRunOnUIThread(updateTimeRunnable);
AndroidUtilities.runOnUIThread(updateTimeRunnable, 1000);
}
bottomButton.setLayoutParams(layoutParams);
}
private void onPasswordForgot() {
if (currentPassword.pending_reset_date == 0 && currentPassword.has_recovery) {
needShowProgress(true);
TLRPC.TL_auth_requestPasswordRecovery req = new TLRPC.TL_auth_requestPasswordRecovery();
ConnectionsManager.getInstance(currentAccount).sendRequest(req, (response, error) -> AndroidUtilities.runOnUIThread(() -> {
needHideProgress();
if (error == null) {
final TLRPC.TL_auth_passwordRecovery res = (TLRPC.TL_auth_passwordRecovery) response;
currentPassword.email_unconfirmed_pattern = res.email_pattern;
TwoStepVerificationSetupActivity fragment = new TwoStepVerificationSetupActivity(currentAccount, TwoStepVerificationSetupActivity.TYPE_EMAIL_RECOVERY, currentPassword) {
@Override
protected void onReset() {
resetPasswordOnShow = true;
}
};
fragment.addFragmentToClose(this);
fragment.setCurrentPasswordParams(currentPasswordHash, currentSecretId, currentSecret, false);
presentFragment(fragment);
} else {
if (error.text.startsWith("FLOOD_WAIT")) {
int time = Utilities.parseInt(error.text);
String timeString;
if (time < 60) {
timeString = LocaleController.formatPluralString("Seconds", time);
} else {
timeString = LocaleController.formatPluralString("Minutes", time / 60);
}
showAlertWithText(LocaleController.getString("AppName", R.string.AppName), LocaleController.formatString("FloodWaitTime", R.string.FloodWaitTime, timeString));
} else {
showAlertWithText(LocaleController.getString("AppName", R.string.AppName), error.text);
}
}
}), ConnectionsManager.RequestFlagFailOnServerErrors | ConnectionsManager.RequestFlagWithoutLogin);
} else {
if (getParentActivity() == null) {
return;
}
if (currentPassword.pending_reset_date != 0) {
if (getConnectionsManager().getCurrentTime() > currentPassword.pending_reset_date) {
AlertDialog.Builder builder = new AlertDialog.Builder(getParentActivity());
builder.setPositiveButton(LocaleController.getString("Reset", R.string.Reset), (dialog, which) -> resetPassword());
builder.setNegativeButton(LocaleController.getString("Cancel", R.string.Cancel), null);
builder.setTitle(LocaleController.getString("ResetPassword", R.string.ResetPassword));
builder.setMessage(LocaleController.getString("RestorePasswordResetPasswordText", R.string.RestorePasswordResetPasswordText));
AlertDialog dialog = builder.create();
showDialog(dialog);
TextView button = (TextView) dialog.getButton(DialogInterface.BUTTON_POSITIVE);
if (button != null) {
button.setTextColor(Theme.getColor(Theme.key_dialogTextRed2));
}
} else {
cancelPasswordReset();
}
} else {
AlertDialog.Builder builder = new AlertDialog.Builder(getParentActivity());
builder.setPositiveButton(LocaleController.getString("Reset", R.string.Reset), (dialog, which) -> resetPassword());
builder.setNegativeButton(LocaleController.getString("Cancel", R.string.Cancel), null);
builder.setTitle(LocaleController.getString("ResetPassword", R.string.ResetPassword));
builder.setMessage(LocaleController.getString("RestorePasswordNoEmailText2", R.string.RestorePasswordNoEmailText2));
showDialog(builder.create());
}
}
}
2015-04-09 20:00:14 +02:00
@Override
2018-07-30 04:07:02 +02:00
public void didReceivedNotification(int id, int account, Object... args) {
if (id == NotificationCenter.twoStepPasswordChanged) {
if (args != null && args.length > 0 && args[0] != null) {
2015-04-09 20:00:14 +02:00
currentPasswordHash = (byte[]) args[0];
}
2021-07-15 16:24:57 +02:00
loadPasswordInfo(false, false);
2015-04-09 20:00:14 +02:00
updateRows();
}
}
2018-07-30 04:07:02 +02:00
@Override
public void onPause() {
super.onPause();
paused = true;
}
2015-04-09 20:00:14 +02:00
@Override
public void onResume() {
super.onResume();
2018-07-30 04:07:02 +02:00
paused = false;
2015-09-24 22:52:02 +02:00
AndroidUtilities.requestAdjustResize(getParentActivity(), classGuid);
2015-04-09 20:00:14 +02:00
}
2018-08-27 10:33:11 +02:00
public void setCurrentPasswordInfo(byte[] hash, TLRPC.TL_account_password password) {
2019-07-18 15:01:39 +02:00
if (hash != null) {
currentPasswordHash = hash;
}
2018-07-30 04:07:02 +02:00
currentPassword = password;
}
2019-07-18 15:01:39 +02:00
public void setDelegate(TwoStepVerificationActivityDelegate twoStepVerificationActivityDelegate) {
delegate = twoStepVerificationActivityDelegate;
}
2018-08-27 10:33:11 +02:00
public static boolean canHandleCurrentPassword(TLRPC.TL_account_password password, boolean login) {
if (login) {
if (password.current_algo instanceof TLRPC.TL_passwordKdfAlgoUnknown) {
return false;
}
} else {
if (password.new_algo instanceof TLRPC.TL_passwordKdfAlgoUnknown ||
password.current_algo instanceof TLRPC.TL_passwordKdfAlgoUnknown ||
password.new_secure_algo instanceof TLRPC.TL_securePasswordKdfAlgoUnknown) {
return false;
}
}
return true;
}
public static void initPasswordNewAlgo(TLRPC.TL_account_password password) {
if (password.new_algo instanceof TLRPC.TL_passwordKdfAlgoSHA256SHA256PBKDF2HMACSHA512iter100000SHA256ModPow) {
TLRPC.TL_passwordKdfAlgoSHA256SHA256PBKDF2HMACSHA512iter100000SHA256ModPow algo = (TLRPC.TL_passwordKdfAlgoSHA256SHA256PBKDF2HMACSHA512iter100000SHA256ModPow) password.new_algo;
byte[] salt = new byte[algo.salt1.length + 32];
Utilities.random.nextBytes(salt);
System.arraycopy(algo.salt1, 0, salt, 0, algo.salt1.length);
algo.salt1 = salt;
}
if (password.new_secure_algo instanceof TLRPC.TL_securePasswordKdfAlgoPBKDF2HMACSHA512iter100000) {
TLRPC.TL_securePasswordKdfAlgoPBKDF2HMACSHA512iter100000 algo = (TLRPC.TL_securePasswordKdfAlgoPBKDF2HMACSHA512iter100000) password.new_secure_algo;
byte[] salt = new byte[algo.salt.length + 32];
Utilities.random.nextBytes(salt);
System.arraycopy(algo.salt, 0, salt, 0, algo.salt.length);
algo.salt = salt;
}
}
2021-07-15 16:24:57 +02:00
private void loadPasswordInfo(boolean first, final boolean silent) {
2015-04-09 20:00:14 +02:00
if (!silent) {
loading = true;
2017-03-31 01:58:05 +02:00
if (listAdapter != null) {
listAdapter.notifyDataSetChanged();
}
2015-04-09 20:00:14 +02:00
}
TLRPC.TL_account_getPassword req = new TLRPC.TL_account_getPassword();
2018-08-27 10:33:11 +02:00
ConnectionsManager.getInstance(currentAccount).sendRequest(req, (response, error) -> AndroidUtilities.runOnUIThread(() -> {
if (error == null) {
loading = false;
currentPassword = (TLRPC.TL_account_password) response;
if (!canHandleCurrentPassword(currentPassword, false)) {
AlertsCreator.showUpdateAppAlert(getParentActivity(), LocaleController.getString("UpdateAppAlert", R.string.UpdateAppAlert), true);
return;
}
2021-07-15 16:24:57 +02:00
if (!silent || first) {
2018-08-27 10:33:11 +02:00
passwordEntered = currentPasswordHash != null && currentPasswordHash.length > 0 || !currentPassword.has_password;
}
initPasswordNewAlgo(currentPassword);
NotificationCenter.getInstance(currentAccount).postNotificationName(NotificationCenter.didSetOrRemoveTwoStepPassword, currentPassword);
2018-08-27 10:33:11 +02:00
}
updateRows();
}), ConnectionsManager.RequestFlagFailOnServerErrors | ConnectionsManager.RequestFlagWithoutLogin);
}
2021-07-15 16:24:57 +02:00
@Override
protected void onTransitionAnimationEnd(boolean isOpen, boolean backward) {
super.onTransitionAnimationEnd(isOpen, backward);
if (isOpen) {
if (forgotPasswordOnShow) {
onPasswordForgot();
forgotPasswordOnShow = false;
} else if (resetPasswordOnShow) {
resetPassword();
resetPasswordOnShow = false;
}
}
}
2015-04-09 20:00:14 +02:00
private void updateRows() {
2019-01-23 18:03:33 +01:00
StringBuilder lastValue = new StringBuilder();
lastValue.append(setPasswordRow);
lastValue.append(setPasswordDetailRow);
lastValue.append(changePasswordRow);
lastValue.append(turnPasswordOffRow);
lastValue.append(setRecoveryEmailRow);
lastValue.append(changeRecoveryEmailRow);
lastValue.append(passwordEnabledDetailRow);
lastValue.append(rowCount);
2015-04-09 20:00:14 +02:00
rowCount = 0;
setPasswordRow = -1;
setPasswordDetailRow = -1;
changePasswordRow = -1;
turnPasswordOffRow = -1;
setRecoveryEmailRow = -1;
changeRecoveryEmailRow = -1;
passwordEnabledDetailRow = -1;
2021-07-15 16:24:57 +02:00
if (!loading && currentPassword != null && passwordEntered) {
if (currentPassword.has_password) {
2015-04-09 20:00:14 +02:00
changePasswordRow = rowCount++;
turnPasswordOffRow = rowCount++;
if (currentPassword.has_recovery) {
changeRecoveryEmailRow = rowCount++;
} else {
setRecoveryEmailRow = rowCount++;
}
2019-01-23 18:03:33 +01:00
passwordEnabledDetailRow = rowCount++;
2018-08-27 10:33:11 +02:00
} else {
2019-01-23 18:03:33 +01:00
setPasswordRow = rowCount++;
setPasswordDetailRow = rowCount++;
2015-04-09 20:00:14 +02:00
}
}
2019-01-23 18:03:33 +01:00
StringBuilder newValue = new StringBuilder();
newValue.append(setPasswordRow);
newValue.append(setPasswordDetailRow);
newValue.append(changePasswordRow);
newValue.append(turnPasswordOffRow);
newValue.append(setRecoveryEmailRow);
newValue.append(changeRecoveryEmailRow);
newValue.append(passwordEnabledDetailRow);
newValue.append(rowCount);
if (listAdapter != null && !lastValue.toString().equals(newValue.toString())) {
2015-04-09 20:00:14 +02:00
listAdapter.notifyDataSetChanged();
2019-01-23 18:03:33 +01:00
}
if (fragmentView != null) {
if (loading || passwordEntered) {
if (listView != null) {
listView.setVisibility(View.VISIBLE);
scrollView.setVisibility(View.INVISIBLE);
listView.setEmptyView(emptyView);
2018-08-27 10:33:11 +02:00
}
if (passwordEditText != null) {
2019-01-23 18:03:33 +01:00
doneItem.setVisibility(View.GONE);
passwordEditText.setVisibility(View.INVISIBLE);
titleTextView.setVisibility(View.INVISIBLE);
bottomTextView.setVisibility(View.INVISIBLE);
bottomButton.setVisibility(View.INVISIBLE);
2021-07-15 16:24:57 +02:00
updateBottomButton();
2018-08-27 10:33:11 +02:00
}
2019-01-23 18:03:33 +01:00
fragmentView.setBackgroundColor(Theme.getColor(Theme.key_windowBackgroundGray));
fragmentView.setTag(Theme.key_windowBackgroundGray);
} else {
if (listView != null) {
listView.setEmptyView(null);
listView.setVisibility(View.INVISIBLE);
scrollView.setVisibility(View.VISIBLE);
emptyView.setVisibility(View.INVISIBLE);
2015-04-09 20:00:14 +02:00
}
2019-01-23 18:03:33 +01:00
if (passwordEditText != null) {
doneItem.setVisibility(View.VISIBLE);
passwordEditText.setVisibility(View.VISIBLE);
fragmentView.setBackgroundColor(Theme.getColor(Theme.key_windowBackgroundWhite));
fragmentView.setTag(Theme.key_windowBackgroundWhite);
titleTextView.setVisibility(View.VISIBLE);
bottomButton.setVisibility(View.VISIBLE);
2021-07-15 16:24:57 +02:00
updateBottomButton();
2019-01-23 18:03:33 +01:00
bottomTextView.setVisibility(View.INVISIBLE);
if (!TextUtils.isEmpty(currentPassword.hint)) {
passwordEditText.setHint(currentPassword.hint);
} else {
passwordEditText.setHint("");
2015-04-09 20:00:14 +02:00
}
2019-01-23 18:03:33 +01:00
AndroidUtilities.runOnUIThread(() -> {
if (!isFinishing() && !destroyed && passwordEditText != null) {
passwordEditText.requestFocus();
AndroidUtilities.showKeyboard(passwordEditText);
}
}, 200);
}
2015-04-09 20:00:14 +02:00
}
}
}
private void needShowProgress() {
2021-07-15 16:24:57 +02:00
needShowProgress(false);
}
private void needShowProgress(boolean delay) {
2015-04-09 20:00:14 +02:00
if (getParentActivity() == null || getParentActivity().isFinishing() || progressDialog != null) {
return;
}
2019-01-23 18:03:33 +01:00
progressDialog = new AlertDialog(getParentActivity(), 3);
progressDialog.setCanCacnel(false);
2021-07-15 16:24:57 +02:00
if (delay) {
progressDialog.showDelayed(300);
} else {
progressDialog.show();
}
2015-04-09 20:00:14 +02:00
}
2020-09-30 15:48:47 +02:00
public void needHideProgress() {
2015-04-09 20:00:14 +02:00
if (progressDialog == null) {
return;
}
try {
progressDialog.dismiss();
} catch (Exception e) {
2017-03-31 01:58:05 +02:00
FileLog.e(e);
2015-04-09 20:00:14 +02:00
}
progressDialog = null;
}
private void showAlertWithText(String title, String text) {
2019-12-31 14:08:08 +01:00
if (getParentActivity() == null) {
return;
}
2015-04-09 20:00:14 +02:00
AlertDialog.Builder builder = new AlertDialog.Builder(getParentActivity());
builder.setPositiveButton(LocaleController.getString("OK", R.string.OK), null);
builder.setTitle(title);
builder.setMessage(text);
2015-05-21 23:27:27 +02:00
showDialog(builder.create());
2015-04-09 20:00:14 +02:00
}
private void clearPassword() {
2018-08-27 10:33:11 +02:00
final String password = firstPassword;
2015-04-09 20:00:14 +02:00
final TLRPC.TL_account_updatePasswordSettings req = new TLRPC.TL_account_updatePasswordSettings();
2018-08-27 10:33:11 +02:00
if (currentPasswordHash == null || currentPasswordHash.length == 0) {
req.password = new TLRPC.TL_inputCheckPasswordEmpty();
}
2015-04-09 20:00:14 +02:00
req.new_settings = new TLRPC.TL_account_passwordInputSettings();
UserConfig.getInstance(currentAccount).resetSavedPassword();
currentSecret = null;
req.new_settings.flags = 3;
req.new_settings.hint = "";
req.new_settings.new_password_hash = new byte[0];
req.new_settings.new_algo = new TLRPC.TL_passwordKdfAlgoUnknown();
req.new_settings.email = "";
2015-04-09 20:00:14 +02:00
needShowProgress();
2018-08-27 10:33:11 +02:00
Utilities.globalQueue.postRunnable(() -> {
if (req.password == null) {
if (currentPassword.current_algo == null) {
TLRPC.TL_account_getPassword getPasswordReq = new TLRPC.TL_account_getPassword();
ConnectionsManager.getInstance(currentAccount).sendRequest(getPasswordReq, (response2, error2) -> AndroidUtilities.runOnUIThread(() -> {
if (error2 == null) {
currentPassword = (TLRPC.TL_account_password) response2;
initPasswordNewAlgo(currentPassword);
NotificationCenter.getInstance(currentAccount).postNotificationName(NotificationCenter.didSetOrRemoveTwoStepPassword, currentPassword);
clearPassword();
}
}), ConnectionsManager.RequestFlagWithoutLogin);
return;
}
2018-08-27 10:33:11 +02:00
req.password = getNewSrpPassword();
}
byte[] newPasswordBytes = null;
byte[] newPasswordHash = null;
2018-08-27 10:33:11 +02:00
RequestDelegate requestDelegate = (response, error) -> AndroidUtilities.runOnUIThread(() -> {
if (error != null && "SRP_ID_INVALID".equals(error.text)) {
TLRPC.TL_account_getPassword getPasswordReq = new TLRPC.TL_account_getPassword();
ConnectionsManager.getInstance(currentAccount).sendRequest(getPasswordReq, (response2, error2) -> AndroidUtilities.runOnUIThread(() -> {
if (error2 == null) {
currentPassword = (TLRPC.TL_account_password) response2;
initPasswordNewAlgo(currentPassword);
NotificationCenter.getInstance(currentAccount).postNotificationName(NotificationCenter.didSetOrRemoveTwoStepPassword, currentPassword);
clearPassword();
2018-08-27 10:33:11 +02:00
}
}), ConnectionsManager.RequestFlagWithoutLogin);
return;
}
needHideProgress();
if (error == null && response instanceof TLRPC.TL_boolTrue) {
currentPassword = null;
currentPasswordHash = new byte[0];
NotificationCenter.getInstance(currentAccount).postNotificationName(NotificationCenter.didRemoveTwoStepPassword);
NotificationCenter.getInstance(currentAccount).postNotificationName(NotificationCenter.didSetOrRemoveTwoStepPassword);
finishFragment();
2018-08-27 10:33:11 +02:00
} else if (error != null) {
if (error.text.startsWith("FLOOD_WAIT")) {
int time = Utilities.parseInt(error.text);
String timeString;
if (time < 60) {
timeString = LocaleController.formatPluralString("Seconds", time);
2018-08-27 10:33:11 +02:00
} else {
timeString = LocaleController.formatPluralString("Minutes", time / 60);
2015-04-09 20:00:14 +02:00
}
showAlertWithText(LocaleController.getString("AppName", R.string.AppName), LocaleController.formatString("FloodWaitTime", R.string.FloodWaitTime, timeString));
} else {
showAlertWithText(LocaleController.getString("AppName", R.string.AppName), error.text);
2015-04-09 20:00:14 +02:00
}
2018-08-27 10:33:11 +02:00
}
});
ConnectionsManager.getInstance(currentAccount).sendRequest(req, requestDelegate, ConnectionsManager.RequestFlagFailOnServerErrors | ConnectionsManager.RequestFlagWithoutLogin);
2018-08-27 10:33:11 +02:00
});
2015-04-09 20:00:14 +02:00
}
2020-09-30 15:48:47 +02:00
public TLRPC.TL_inputCheckPasswordSRP getNewSrpPassword() {
2018-08-27 10:33:11 +02:00
if (currentPassword.current_algo instanceof TLRPC.TL_passwordKdfAlgoSHA256SHA256PBKDF2HMACSHA512iter100000SHA256ModPow) {
TLRPC.TL_passwordKdfAlgoSHA256SHA256PBKDF2HMACSHA512iter100000SHA256ModPow algo = (TLRPC.TL_passwordKdfAlgoSHA256SHA256PBKDF2HMACSHA512iter100000SHA256ModPow) currentPassword.current_algo;
return SRPHelper.startCheck(currentPasswordHash, currentPassword.srp_id, currentPassword.srp_B, algo);
}
return null;
}
2018-07-30 04:07:02 +02:00
2018-08-27 10:33:11 +02:00
private boolean checkSecretValues(byte[] passwordBytes, TLRPC.TL_account_passwordSettings passwordSettings) {
if (passwordSettings.secure_settings != null) {
currentSecret = passwordSettings.secure_settings.secure_secret;
byte[] passwordHash;
if (passwordSettings.secure_settings.secure_algo instanceof TLRPC.TL_securePasswordKdfAlgoPBKDF2HMACSHA512iter100000) {
TLRPC.TL_securePasswordKdfAlgoPBKDF2HMACSHA512iter100000 algo = (TLRPC.TL_securePasswordKdfAlgoPBKDF2HMACSHA512iter100000) passwordSettings.secure_settings.secure_algo;
passwordHash = Utilities.computePBKDF2(passwordBytes, algo.salt);
} else if (passwordSettings.secure_settings.secure_algo instanceof TLRPC.TL_securePasswordKdfAlgoSHA512) {
TLRPC.TL_securePasswordKdfAlgoSHA512 algo = (TLRPC.TL_securePasswordKdfAlgoSHA512) passwordSettings.secure_settings.secure_algo;
passwordHash = Utilities.computeSHA512(algo.salt, passwordBytes, algo.salt);
} else {
return false;
}
currentSecretId = passwordSettings.secure_settings.secure_secret_id;
2018-07-30 04:07:02 +02:00
byte[] key = new byte[32];
System.arraycopy(passwordHash, 0, key, 0, 32);
byte[] iv = new byte[16];
System.arraycopy(passwordHash, 32, iv, 0, 16);
Utilities.aesCbcEncryptionByteArraySafe(currentSecret, key, iv, 0, currentSecret.length, 0, 0);
2018-08-27 10:33:11 +02:00
if (!PassportActivity.checkSecret(passwordSettings.secure_settings.secure_secret, passwordSettings.secure_settings.secure_secret_id)) {
TLRPC.TL_account_updatePasswordSettings req = new TLRPC.TL_account_updatePasswordSettings();
req.password = getNewSrpPassword();
req.new_settings = new TLRPC.TL_account_passwordInputSettings();
req.new_settings.new_secure_settings = new TLRPC.TL_secureSecretSettings();
req.new_settings.new_secure_settings.secure_secret = new byte[0];
req.new_settings.new_secure_settings.secure_algo = new TLRPC.TL_securePasswordKdfAlgoUnknown();
req.new_settings.new_secure_settings.secure_secret_id = 0;
req.new_settings.flags |= 4;
ConnectionsManager.getInstance(currentAccount).sendRequest(req, (response, error) -> {
});
currentSecret = null;
currentSecretId = 0;
}
2018-07-30 04:07:02 +02:00
} else {
currentSecret = null;
currentSecretId = 0;
}
2018-08-27 10:33:11 +02:00
return true;
}
2015-04-09 20:00:14 +02:00
private void processDone() {
if (!passwordEntered) {
String oldPassword = passwordEditText.getText().toString();
if (oldPassword.length() == 0) {
onFieldError(passwordEditText, false);
return;
}
final byte[] oldPasswordBytes = AndroidUtilities.getStringBytes(oldPassword);
2015-04-09 20:00:14 +02:00
needShowProgress();
Utilities.globalQueue.postRunnable(() -> {
final TLRPC.TL_account_getPasswordSettings req = new TLRPC.TL_account_getPasswordSettings();
final byte[] x_bytes;
if (currentPassword.current_algo instanceof TLRPC.TL_passwordKdfAlgoSHA256SHA256PBKDF2HMACSHA512iter100000SHA256ModPow) {
TLRPC.TL_passwordKdfAlgoSHA256SHA256PBKDF2HMACSHA512iter100000SHA256ModPow algo = (TLRPC.TL_passwordKdfAlgoSHA256SHA256PBKDF2HMACSHA512iter100000SHA256ModPow) currentPassword.current_algo;
x_bytes = SRPHelper.getX(oldPasswordBytes, algo);
} else {
x_bytes = null;
}
2018-08-27 10:33:11 +02:00
RequestDelegate requestDelegate = (response, error) -> {
if (error == null) {
Utilities.globalQueue.postRunnable(() -> {
boolean secretOk = checkSecretValues(oldPasswordBytes, (TLRPC.TL_account_passwordSettings) response);
2018-08-27 10:33:11 +02:00
AndroidUtilities.runOnUIThread(() -> {
if (delegate == null || !secretOk) {
needHideProgress();
2018-08-27 10:33:11 +02:00
}
if (secretOk) {
currentPasswordHash = x_bytes;
passwordEntered = true;
if (delegate != null) {
AndroidUtilities.hideKeyboard(passwordEditText);
delegate.didEnterPassword(getNewSrpPassword());
2015-04-09 20:00:14 +02:00
} else {
if (!TextUtils.isEmpty(currentPassword.email_unconfirmed_pattern)) {
TwoStepVerificationSetupActivity fragment = new TwoStepVerificationSetupActivity(currentAccount, TwoStepVerificationSetupActivity.TYPE_EMAIL_CONFIRM, currentPassword);
fragment.setCurrentPasswordParams(currentPasswordHash, currentSecretId, currentSecret, true);
presentFragment(fragment, true);
} else {
AndroidUtilities.hideKeyboard(passwordEditText);
TwoStepVerificationActivity fragment = new TwoStepVerificationActivity();
fragment.passwordEntered = true;
fragment.currentPasswordHash = currentPasswordHash;
fragment.currentPassword = currentPassword;
fragment.currentSecret = currentSecret;
fragment.currentSecretId = currentSecretId;
presentFragment(fragment, true);
}
2015-04-09 20:00:14 +02:00
}
2018-08-27 10:33:11 +02:00
} else {
AlertsCreator.showUpdateAppAlert(getParentActivity(), LocaleController.getString("UpdateAppAlert", R.string.UpdateAppAlert), true);
2015-04-09 20:00:14 +02:00
}
2018-08-27 10:33:11 +02:00
});
2015-04-09 20:00:14 +02:00
});
2018-08-27 10:33:11 +02:00
} else {
AndroidUtilities.runOnUIThread(() -> {
if ("SRP_ID_INVALID".equals(error.text)) {
TLRPC.TL_account_getPassword getPasswordReq = new TLRPC.TL_account_getPassword();
ConnectionsManager.getInstance(currentAccount).sendRequest(getPasswordReq, (response2, error2) -> AndroidUtilities.runOnUIThread(() -> {
if (error2 == null) {
currentPassword = (TLRPC.TL_account_password) response2;
initPasswordNewAlgo(currentPassword);
NotificationCenter.getInstance(currentAccount).postNotificationName(NotificationCenter.didSetOrRemoveTwoStepPassword, currentPassword);
processDone();
}
}), ConnectionsManager.RequestFlagWithoutLogin);
return;
}
needHideProgress();
if ("PASSWORD_HASH_INVALID".equals(error.text)) {
onFieldError(passwordEditText, true);
} else if (error.text.startsWith("FLOOD_WAIT")) {
int time = Utilities.parseInt(error.text);
String timeString;
if (time < 60) {
timeString = LocaleController.formatPluralString("Seconds", time);
} else {
timeString = LocaleController.formatPluralString("Minutes", time / 60);
}
showAlertWithText(LocaleController.getString("AppName", R.string.AppName), LocaleController.formatString("FloodWaitTime", R.string.FloodWaitTime, timeString));
2018-08-27 10:33:11 +02:00
} else {
showAlertWithText(LocaleController.getString("AppName", R.string.AppName), error.text);
2018-08-27 10:33:11 +02:00
}
});
2015-04-09 20:00:14 +02:00
}
};
2015-04-09 20:00:14 +02:00
if (currentPassword.current_algo instanceof TLRPC.TL_passwordKdfAlgoSHA256SHA256PBKDF2HMACSHA512iter100000SHA256ModPow) {
TLRPC.TL_passwordKdfAlgoSHA256SHA256PBKDF2HMACSHA512iter100000SHA256ModPow algo = (TLRPC.TL_passwordKdfAlgoSHA256SHA256PBKDF2HMACSHA512iter100000SHA256ModPow) currentPassword.current_algo;
req.password = SRPHelper.startCheck(x_bytes, currentPassword.srp_id, currentPassword.srp_B, algo);
if (req.password == null) {
TLRPC.TL_error error = new TLRPC.TL_error();
error.text = "ALGO_INVALID";
requestDelegate.run(null, error);
return;
2019-01-23 18:03:33 +01:00
}
ConnectionsManager.getInstance(currentAccount).sendRequest(req, requestDelegate, ConnectionsManager.RequestFlagFailOnServerErrors | ConnectionsManager.RequestFlagWithoutLogin);
2019-01-23 18:03:33 +01:00
} else {
TLRPC.TL_error error = new TLRPC.TL_error();
error.text = "PASSWORD_HASH_INVALID";
requestDelegate.run(null, error);
2019-01-23 18:03:33 +01:00
}
});
}
2019-01-23 18:03:33 +01:00
}
private void onFieldError(TextView field, boolean clear) {
2015-04-09 20:00:14 +02:00
if (getParentActivity() == null) {
return;
}
Vibrator v = (Vibrator) getParentActivity().getSystemService(Context.VIBRATOR_SERVICE);
if (v != null) {
v.vibrate(200);
}
if (clear) {
2019-01-23 18:03:33 +01:00
field.setText("");
2015-04-09 20:00:14 +02:00
}
2019-01-23 18:03:33 +01:00
AndroidUtilities.shakeView(field, 2, 0);
2015-04-09 20:00:14 +02:00
}
2017-03-31 01:58:05 +02:00
private class ListAdapter extends RecyclerListView.SelectionAdapter {
2015-04-09 20:00:14 +02:00
private Context mContext;
public ListAdapter(Context context) {
mContext = context;
}
@Override
2017-03-31 01:58:05 +02:00
public boolean isEnabled(RecyclerView.ViewHolder holder) {
2019-01-23 18:03:33 +01:00
int type = holder.getItemViewType();
return type == 0;
2015-04-09 20:00:14 +02:00
}
@Override
2017-03-31 01:58:05 +02:00
public int getItemCount() {
2015-04-09 20:00:14 +02:00
return loading || currentPassword == null ? 0 : rowCount;
}
@Override
2017-03-31 01:58:05 +02:00
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view;
switch (viewType) {
case 0:
view = new TextSettingsCell(mContext);
view.setBackgroundColor(Theme.getColor(Theme.key_windowBackgroundWhite));
break;
case 1:
2019-01-23 18:03:33 +01:00
default:
view = new TextInfoPrivacyCell(mContext);
2019-01-23 18:03:33 +01:00
break;
2017-03-31 01:58:05 +02:00
}
return new RecyclerListView.Holder(view);
2015-04-09 20:00:14 +02:00
}
@Override
2017-03-31 01:58:05 +02:00
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
switch (holder.getItemViewType()) {
case 0:
TextSettingsCell textCell = (TextSettingsCell) holder.itemView;
textCell.setTag(Theme.key_windowBackgroundWhiteBlackText);
textCell.setTextColor(Theme.getColor(Theme.key_windowBackgroundWhiteBlackText));
if (position == changePasswordRow) {
textCell.setText(LocaleController.getString("ChangePassword", R.string.ChangePassword), true);
} else if (position == setPasswordRow) {
textCell.setText(LocaleController.getString("SetAdditionalPassword", R.string.SetAdditionalPassword), true);
} else if (position == turnPasswordOffRow) {
textCell.setText(LocaleController.getString("TurnPasswordOff", R.string.TurnPasswordOff), true);
} else if (position == changeRecoveryEmailRow) {
textCell.setText(LocaleController.getString("ChangeRecoveryEmail", R.string.ChangeRecoveryEmail), false);
2017-03-31 01:58:05 +02:00
} else if (position == setRecoveryEmailRow) {
textCell.setText(LocaleController.getString("SetRecoveryEmail", R.string.SetRecoveryEmail), false);
}
break;
case 1:
TextInfoPrivacyCell privacyCell = (TextInfoPrivacyCell) holder.itemView;
if (position == setPasswordDetailRow) {
privacyCell.setText(LocaleController.getString("SetAdditionalPasswordInfo", R.string.SetAdditionalPasswordInfo));
privacyCell.setBackgroundDrawable(Theme.getThemedDrawable(mContext, R.drawable.greydivider_bottom, Theme.key_windowBackgroundGrayShadow));
} else if (position == passwordEnabledDetailRow) {
privacyCell.setText(LocaleController.getString("EnabledPasswordText", R.string.EnabledPasswordText));
privacyCell.setBackgroundDrawable(Theme.getThemedDrawable(mContext, R.drawable.greydivider_bottom, Theme.key_windowBackgroundGrayShadow));
}
break;
2015-04-09 20:00:14 +02:00
}
}
@Override
2017-03-31 01:58:05 +02:00
public int getItemViewType(int position) {
if (position == setPasswordDetailRow || position == passwordEnabledDetailRow) {
2015-04-09 20:00:14 +02:00
return 1;
}
return 0;
}
2017-03-31 01:58:05 +02:00
}
2015-04-09 20:00:14 +02:00
2017-03-31 01:58:05 +02:00
@Override
2020-04-24 11:21:58 +02:00
public ArrayList<ThemeDescription> getThemeDescriptions() {
ArrayList<ThemeDescription> themeDescriptions = new ArrayList<>();
2015-04-09 20:00:14 +02:00
2020-04-24 11:21:58 +02:00
themeDescriptions.add(new ThemeDescription(listView, ThemeDescription.FLAG_CELLBACKGROUNDCOLOR, new Class[]{TextSettingsCell.class, EditTextSettingsCell.class}, null, null, null, Theme.key_windowBackgroundWhite));
themeDescriptions.add(new ThemeDescription(fragmentView, ThemeDescription.FLAG_BACKGROUND | ThemeDescription.FLAG_CHECKTAG, null, null, null, null, Theme.key_windowBackgroundWhite));
themeDescriptions.add(new ThemeDescription(fragmentView, ThemeDescription.FLAG_BACKGROUND | ThemeDescription.FLAG_CHECKTAG, null, null, null, null, Theme.key_windowBackgroundGray));
2017-03-31 01:58:05 +02:00
2020-04-24 11:21:58 +02:00
themeDescriptions.add(new ThemeDescription(actionBar, ThemeDescription.FLAG_BACKGROUND, null, null, null, null, Theme.key_actionBarDefault));
themeDescriptions.add(new ThemeDescription(listView, ThemeDescription.FLAG_LISTGLOWCOLOR, null, null, null, null, Theme.key_actionBarDefault));
themeDescriptions.add(new ThemeDescription(actionBar, ThemeDescription.FLAG_AB_ITEMSCOLOR, null, null, null, null, Theme.key_actionBarDefaultIcon));
themeDescriptions.add(new ThemeDescription(actionBar, ThemeDescription.FLAG_AB_TITLECOLOR, null, null, null, null, Theme.key_actionBarDefaultTitle));
themeDescriptions.add(new ThemeDescription(actionBar, ThemeDescription.FLAG_AB_SELECTORCOLOR, null, null, null, null, Theme.key_actionBarDefaultSelector));
2017-03-31 01:58:05 +02:00
2020-04-24 11:21:58 +02:00
themeDescriptions.add(new ThemeDescription(listView, ThemeDescription.FLAG_SELECTOR, null, null, null, null, Theme.key_listSelector));
2017-03-31 01:58:05 +02:00
2020-04-24 11:21:58 +02:00
themeDescriptions.add(new ThemeDescription(listView, 0, new Class[]{View.class}, Theme.dividerPaint, null, null, Theme.key_divider));
2017-03-31 01:58:05 +02:00
2020-04-24 11:21:58 +02:00
themeDescriptions.add(new ThemeDescription(emptyView, ThemeDescription.FLAG_PROGRESSBAR, null, null, null, null, Theme.key_progressCircle));
2017-03-31 01:58:05 +02:00
2020-04-24 11:21:58 +02:00
themeDescriptions.add(new ThemeDescription(listView, ThemeDescription.FLAG_CHECKTAG, new Class[]{TextSettingsCell.class}, new String[]{"textView"}, null, null, null, Theme.key_windowBackgroundWhiteBlackText));
themeDescriptions.add(new ThemeDescription(listView, ThemeDescription.FLAG_CHECKTAG, new Class[]{TextSettingsCell.class}, new String[]{"textView"}, null, null, null, Theme.key_windowBackgroundWhiteRedText3));
2019-01-23 18:03:33 +01:00
2020-04-24 11:21:58 +02:00
themeDescriptions.add(new ThemeDescription(listView, ThemeDescription.FLAG_TEXTCOLOR, new Class[]{EditTextSettingsCell.class}, new String[]{"textView"}, null, null, null, Theme.key_windowBackgroundWhiteBlackText));
themeDescriptions.add(new ThemeDescription(listView, ThemeDescription.FLAG_HINTTEXTCOLOR, new Class[]{EditTextSettingsCell.class}, new String[]{"textView"}, null, null, null, Theme.key_windowBackgroundWhiteHintText));
2017-03-31 01:58:05 +02:00
2020-04-24 11:21:58 +02:00
themeDescriptions.add(new ThemeDescription(listView, ThemeDescription.FLAG_BACKGROUNDFILTER, new Class[]{TextInfoPrivacyCell.class}, null, null, null, Theme.key_windowBackgroundGrayShadow));
themeDescriptions.add(new ThemeDescription(listView, 0, new Class[]{TextInfoPrivacyCell.class}, new String[]{"textView"}, null, null, null, Theme.key_windowBackgroundWhiteGrayText4));
themeDescriptions.add(new ThemeDescription(titleTextView, ThemeDescription.FLAG_TEXTCOLOR, null, null, null, null, Theme.key_windowBackgroundWhiteGrayText6));
themeDescriptions.add(new ThemeDescription(bottomTextView, ThemeDescription.FLAG_TEXTCOLOR, null, null, null, null, Theme.key_windowBackgroundWhiteGrayText6));
themeDescriptions.add(new ThemeDescription(bottomButton, ThemeDescription.FLAG_TEXTCOLOR, null, null, null, null, Theme.key_windowBackgroundWhiteBlueText4));
themeDescriptions.add(new ThemeDescription(passwordEditText, ThemeDescription.FLAG_TEXTCOLOR, null, null, null, null, Theme.key_windowBackgroundWhiteBlackText));
themeDescriptions.add(new ThemeDescription(passwordEditText, ThemeDescription.FLAG_HINTTEXTCOLOR, null, null, null, null, Theme.key_windowBackgroundWhiteHintText));
themeDescriptions.add(new ThemeDescription(passwordEditText, ThemeDescription.FLAG_BACKGROUNDFILTER, null, null, null, null, Theme.key_windowBackgroundWhiteInputField));
themeDescriptions.add(new ThemeDescription(passwordEditText, ThemeDescription.FLAG_BACKGROUNDFILTER | ThemeDescription.FLAG_DRAWABLESELECTEDSTATE, null, null, null, null, Theme.key_windowBackgroundWhiteInputFieldActivated));
return themeDescriptions;
2015-04-09 20:00:14 +02:00
}
2021-12-07 14:02:02 +01:00
@Override
public boolean onBackPressed() {
if (otherwiseReloginDays >= 0) {
showSetForcePasswordAlert();
return false;
}
return super.onBackPressed();
}
private void showSetForcePasswordAlert() {
AlertDialog.Builder builder = new AlertDialog.Builder(getParentActivity());
builder.setTitle(LocaleController.getString("Warning", R.string.Warning));
builder.setMessage(LocaleController.formatPluralString("ForceSetPasswordAlertMessage", otherwiseReloginDays));
builder.setPositiveButton(LocaleController.getString("ForceSetPasswordContinue", R.string.ForceSetPasswordContinue), (a1, a2) -> {
});
builder.setNegativeButton(LocaleController.getString("ForceSetPasswordCancel", R.string.ForceSetPasswordCancel), (a1, a2) -> {
finishFragment();
});
AlertDialog alertDialog = builder.show();
((TextView)alertDialog.getButton(DialogInterface.BUTTON_NEGATIVE)).setTextColor(Theme.getColor(Theme.key_dialogTextRed2));
}
public void setBlockingAlert(int otherwiseRelogin) {
otherwiseReloginDays = otherwiseRelogin;
}
@Override
public void finishFragment() {
if (otherwiseReloginDays >= 0) {
final Bundle args = new Bundle();
args.putBoolean("afterSignup", true);
presentFragment(new DialogsActivity(args), true);
} else {
super.finishFragment();
}
}
2015-04-09 20:00:14 +02:00
}