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

212 lines
11 KiB
Java
Raw Normal View History

2013-10-25 17:19:00 +02:00
/*
2019-01-23 18:03:33 +01:00
* This is the source code of Telegram for Android v. 5.x.x.
2013-10-25 17:19:00 +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).
*
2019-01-23 18:03:33 +01:00
* Copyright Nikolai Kudashov, 2013-2018.
2013-10-25 17:19:00 +02:00
*/
package org.telegram.ui;
2015-04-09 20:00:14 +02:00
import android.content.Context;
2013-10-25 17:19:00 +02:00
import android.content.SharedPreferences;
2014-10-21 22:35:16 +02:00
import android.text.InputType;
import android.util.TypedValue;
import android.view.Gravity;
2013-10-25 17:19:00 +02:00
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
2014-10-21 22:35:16 +02:00
import android.widget.LinearLayout;
2013-10-25 17:19:00 +02:00
2015-09-24 22:52:02 +02:00
import org.telegram.messenger.AndroidUtilities;
import org.telegram.messenger.LocaleController;
import org.telegram.tgnet.ConnectionsManager;
import org.telegram.tgnet.TLRPC;
import org.telegram.messenger.MessagesController;
import org.telegram.messenger.NotificationCenter;
2013-10-25 17:19:00 +02:00
import org.telegram.messenger.R;
import org.telegram.messenger.UserConfig;
2014-11-13 21:10:14 +01:00
import org.telegram.ui.ActionBar.ActionBar;
import org.telegram.ui.ActionBar.ActionBarMenu;
import org.telegram.ui.ActionBar.BaseFragment;
2017-03-31 01:58:05 +02:00
import org.telegram.ui.ActionBar.Theme;
import org.telegram.ui.ActionBar.ThemeDescription;
2017-12-08 18:35:59 +01:00
import org.telegram.ui.Components.EditTextBoldCursor;
import org.telegram.ui.Components.LayoutHelper;
2013-10-25 17:19:00 +02:00
2020-04-24 11:21:58 +02:00
import java.util.ArrayList;
2014-11-13 21:10:14 +01:00
public class ChangeNameActivity extends BaseFragment {
2017-12-08 18:35:59 +01:00
private EditTextBoldCursor firstNameField;
private EditTextBoldCursor lastNameField;
2013-10-25 17:19:00 +02:00
private View headerLabelView;
private View doneButton;
2014-11-13 21:10:14 +01:00
private final static int done_button = 1;
2013-10-25 17:19:00 +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(true);
actionBar.setTitle(LocaleController.getString("EditName", R.string.EditName));
actionBar.setActionBarMenuOnItemClick(new ActionBar.ActionBarMenuOnItemClick() {
@Override
public void onItemClick(int id) {
if (id == -1) {
finishFragment();
} else if (id == done_button) {
if (firstNameField.getText().length() != 0) {
saveName();
finishFragment();
}
2013-10-25 17:19:00 +02:00
}
2015-04-09 20:00:14 +02:00
}
});
ActionBarMenu menu = actionBar.createMenu();
2020-12-24 06:36:01 +01:00
doneButton = menu.addItemWithWidth(done_button, R.drawable.ic_done, AndroidUtilities.dp(56), LocaleController.getString("Done", R.string.Done));
2013-10-25 17:19:00 +02:00
2018-07-30 04:07:02 +02:00
TLRPC.User user = MessagesController.getInstance(currentAccount).getUser(UserConfig.getInstance(currentAccount).getClientUserId());
2015-04-09 20:00:14 +02:00
if (user == null) {
2018-07-30 04:07:02 +02:00
user = UserConfig.getInstance(currentAccount).getCurrentUser();
2015-04-09 20:00:14 +02:00
}
2013-10-25 17:19:00 +02:00
2015-05-21 23:27:27 +02:00
LinearLayout linearLayout = new LinearLayout(context);
fragmentView = linearLayout;
2015-04-09 20:00:14 +02:00
fragmentView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
((LinearLayout) fragmentView).setOrientation(LinearLayout.VERTICAL);
2019-12-31 14:08:08 +01:00
fragmentView.setOnTouchListener((v, event) -> true);
2013-10-25 17:19:00 +02:00
2017-12-08 18:35:59 +01:00
firstNameField = new EditTextBoldCursor(context);
2015-04-09 20:00:14 +02:00
firstNameField.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 18);
2017-03-31 01:58:05 +02:00
firstNameField.setHintTextColor(Theme.getColor(Theme.key_windowBackgroundWhiteHintText));
firstNameField.setTextColor(Theme.getColor(Theme.key_windowBackgroundWhiteBlackText));
firstNameField.setBackgroundDrawable(Theme.createEditTextDrawable(context, false));
2015-04-09 20:00:14 +02:00
firstNameField.setMaxLines(1);
firstNameField.setLines(1);
firstNameField.setSingleLine(true);
firstNameField.setGravity(LocaleController.isRTL ? Gravity.RIGHT : Gravity.LEFT);
firstNameField.setInputType(InputType.TYPE_TEXT_FLAG_CAP_SENTENCES | InputType.TYPE_TEXT_FLAG_AUTO_CORRECT);
firstNameField.setImeOptions(EditorInfo.IME_ACTION_NEXT);
firstNameField.setHint(LocaleController.getString("FirstName", R.string.FirstName));
2017-12-08 18:35:59 +01:00
firstNameField.setCursorColor(Theme.getColor(Theme.key_windowBackgroundWhiteBlackText));
firstNameField.setCursorSize(AndroidUtilities.dp(20));
firstNameField.setCursorWidth(1.5f);
2015-05-21 23:27:27 +02:00
linearLayout.addView(firstNameField, LayoutHelper.createLinear(LayoutHelper.MATCH_PARENT, 36, 24, 24, 24, 0));
2019-12-31 14:08:08 +01:00
firstNameField.setOnEditorActionListener((textView, i, keyEvent) -> {
if (i == EditorInfo.IME_ACTION_NEXT) {
lastNameField.requestFocus();
lastNameField.setSelection(lastNameField.length());
return true;
2015-04-09 20:00:14 +02:00
}
2019-12-31 14:08:08 +01:00
return false;
2015-04-09 20:00:14 +02:00
});
2014-10-21 22:35:16 +02:00
2017-12-08 18:35:59 +01:00
lastNameField = new EditTextBoldCursor(context);
2015-04-09 20:00:14 +02:00
lastNameField.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 18);
2017-03-31 01:58:05 +02:00
lastNameField.setHintTextColor(Theme.getColor(Theme.key_windowBackgroundWhiteHintText));
lastNameField.setTextColor(Theme.getColor(Theme.key_windowBackgroundWhiteBlackText));
lastNameField.setBackgroundDrawable(Theme.createEditTextDrawable(context, false));
2015-04-09 20:00:14 +02:00
lastNameField.setMaxLines(1);
lastNameField.setLines(1);
lastNameField.setSingleLine(true);
lastNameField.setGravity(LocaleController.isRTL ? Gravity.RIGHT : Gravity.LEFT);
lastNameField.setInputType(InputType.TYPE_TEXT_FLAG_CAP_SENTENCES | InputType.TYPE_TEXT_FLAG_AUTO_CORRECT);
lastNameField.setImeOptions(EditorInfo.IME_ACTION_DONE);
lastNameField.setHint(LocaleController.getString("LastName", R.string.LastName));
2017-12-08 18:35:59 +01:00
lastNameField.setCursorColor(Theme.getColor(Theme.key_windowBackgroundWhiteBlackText));
lastNameField.setCursorSize(AndroidUtilities.dp(20));
lastNameField.setCursorWidth(1.5f);
2015-05-21 23:27:27 +02:00
linearLayout.addView(lastNameField, LayoutHelper.createLinear(LayoutHelper.MATCH_PARENT, 36, 24, 16, 24, 0));
2019-12-31 14:08:08 +01:00
lastNameField.setOnEditorActionListener((textView, i, keyEvent) -> {
if (i == EditorInfo.IME_ACTION_DONE) {
doneButton.performClick();
return true;
2013-10-25 17:19:00 +02:00
}
2019-12-31 14:08:08 +01:00
return false;
2015-04-09 20:00:14 +02:00
});
if (user != null) {
firstNameField.setText(user.first_name);
firstNameField.setSelection(firstNameField.length());
lastNameField.setText(user.last_name);
2013-10-25 17:19:00 +02:00
}
2015-04-09 20:00:14 +02:00
2013-10-25 17:19:00 +02:00
return fragmentView;
}
@Override
public void onResume() {
super.onResume();
2018-07-30 04:07:02 +02:00
SharedPreferences preferences = MessagesController.getGlobalMainSettings();
boolean animations = preferences.getBoolean("view_animations", true);
if (!animations) {
firstNameField.requestFocus();
AndroidUtilities.showKeyboard(firstNameField);
}
}
2013-10-25 17:19:00 +02:00
private void saveName() {
2018-07-30 04:07:02 +02:00
TLRPC.User currentUser = UserConfig.getInstance(currentAccount).getCurrentUser();
if (currentUser == null || lastNameField.getText() == null || firstNameField.getText() == null) {
return;
}
2014-10-21 22:35:16 +02:00
String newFirst = firstNameField.getText().toString();
String newLast = lastNameField.getText().toString();
if (currentUser.first_name != null && currentUser.first_name.equals(newFirst) && currentUser.last_name != null && currentUser.last_name.equals(newLast)) {
return;
}
TLRPC.TL_account_updateProfile req = new TLRPC.TL_account_updateProfile();
2016-03-16 13:26:32 +01:00
req.flags = 3;
2014-10-21 22:35:16 +02:00
currentUser.first_name = req.first_name = newFirst;
currentUser.last_name = req.last_name = newLast;
2018-07-30 04:07:02 +02:00
TLRPC.User user = MessagesController.getInstance(currentAccount).getUser(UserConfig.getInstance(currentAccount).getClientUserId());
2013-10-25 17:19:00 +02:00
if (user != null) {
user.first_name = req.first_name;
user.last_name = req.last_name;
}
2018-07-30 04:07:02 +02:00
UserConfig.getInstance(currentAccount).saveConfig(true);
NotificationCenter.getInstance(currentAccount).postNotificationName(NotificationCenter.mainUserInfoChanged);
NotificationCenter.getInstance(currentAccount).postNotificationName(NotificationCenter.updateInterfaces, MessagesController.UPDATE_MASK_NAME);
2019-12-31 14:08:08 +01:00
ConnectionsManager.getInstance(currentAccount).sendRequest(req, (response, error) -> {
2013-10-25 17:19:00 +02:00
});
2013-10-25 17:19:00 +02:00
}
@Override
2015-10-29 18:10:07 +01:00
public void onTransitionAnimationEnd(boolean isOpen, boolean backward) {
if (isOpen) {
2019-07-18 15:01:39 +02:00
AndroidUtilities.runOnUIThread(() -> {
if (firstNameField != null) {
firstNameField.requestFocus();
AndroidUtilities.showKeyboard(firstNameField);
2015-11-26 22:04:02 +01:00
}
}, 100);
2015-10-29 18:10:07 +01: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<>();
themeDescriptions.add(new ThemeDescription(fragmentView, ThemeDescription.FLAG_BACKGROUND, null, null, null, null, Theme.key_windowBackgroundWhite));
themeDescriptions.add(new ThemeDescription(actionBar, ThemeDescription.FLAG_BACKGROUND, 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));
themeDescriptions.add(new ThemeDescription(firstNameField, ThemeDescription.FLAG_TEXTCOLOR, null, null, null, null, Theme.key_windowBackgroundWhiteBlackText));
themeDescriptions.add(new ThemeDescription(firstNameField, ThemeDescription.FLAG_HINTTEXTCOLOR, null, null, null, null, Theme.key_windowBackgroundWhiteHintText));
themeDescriptions.add(new ThemeDescription(firstNameField, ThemeDescription.FLAG_BACKGROUNDFILTER, null, null, null, null, Theme.key_windowBackgroundWhiteInputField));
themeDescriptions.add(new ThemeDescription(firstNameField, ThemeDescription.FLAG_BACKGROUNDFILTER | ThemeDescription.FLAG_DRAWABLESELECTEDSTATE, null, null, null, null, Theme.key_windowBackgroundWhiteInputFieldActivated));
themeDescriptions.add(new ThemeDescription(lastNameField, ThemeDescription.FLAG_TEXTCOLOR, null, null, null, null, Theme.key_windowBackgroundWhiteBlackText));
themeDescriptions.add(new ThemeDescription(lastNameField, ThemeDescription.FLAG_HINTTEXTCOLOR, null, null, null, null, Theme.key_windowBackgroundWhiteHintText));
themeDescriptions.add(new ThemeDescription(lastNameField, ThemeDescription.FLAG_BACKGROUNDFILTER, null, null, null, null, Theme.key_windowBackgroundWhiteInputField));
themeDescriptions.add(new ThemeDescription(lastNameField, ThemeDescription.FLAG_BACKGROUNDFILTER | ThemeDescription.FLAG_DRAWABLESELECTEDSTATE, null, null, null, null, Theme.key_windowBackgroundWhiteInputFieldActivated));
return themeDescriptions;
2017-03-31 01:58:05 +02:00
}
2013-10-25 17:19:00 +02:00
}