2013-10-25 15:19:00 +00:00
|
|
|
/*
|
2013-12-20 19:25:49 +00:00
|
|
|
* This is the source code of Telegram for Android v. 1.3.2.
|
2013-10-25 15:19:00 +00: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).
|
|
|
|
*
|
|
|
|
* Copyright Nikolai Kudashov, 2013.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package org.telegram.ui;
|
|
|
|
|
|
|
|
import android.app.Activity;
|
|
|
|
import android.content.SharedPreferences;
|
2014-10-21 20:35:16 +00:00
|
|
|
import android.text.InputType;
|
|
|
|
import android.util.TypedValue;
|
|
|
|
import android.view.Gravity;
|
2013-10-25 15:19:00 +00:00
|
|
|
import android.view.KeyEvent;
|
|
|
|
import android.view.LayoutInflater;
|
2014-11-17 02:44:57 +00:00
|
|
|
import android.view.MotionEvent;
|
2013-10-25 15:19:00 +00:00
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
|
|
|
import android.view.inputmethod.EditorInfo;
|
|
|
|
import android.widget.EditText;
|
2014-10-21 20:35:16 +00:00
|
|
|
import android.widget.LinearLayout;
|
2013-10-25 15:19:00 +00:00
|
|
|
import android.widget.TextView;
|
|
|
|
|
2014-07-02 22:39:05 +00:00
|
|
|
import org.telegram.android.AndroidUtilities;
|
|
|
|
import org.telegram.android.LocaleController;
|
2014-12-01 17:56:31 +00:00
|
|
|
import org.telegram.messenger.ApplicationLoader;
|
2014-02-28 22:28:25 +00:00
|
|
|
import org.telegram.messenger.TLObject;
|
|
|
|
import org.telegram.messenger.TLRPC;
|
2013-10-25 15:19:00 +00:00
|
|
|
import org.telegram.messenger.ConnectionsManager;
|
2014-07-02 22:39:05 +00:00
|
|
|
import org.telegram.android.MessagesController;
|
2014-08-22 14:24:33 +00:00
|
|
|
import org.telegram.android.NotificationCenter;
|
2013-10-25 15:19:00 +00:00
|
|
|
import org.telegram.messenger.R;
|
|
|
|
import org.telegram.messenger.RPCRequest;
|
|
|
|
import org.telegram.messenger.UserConfig;
|
2014-11-13 20:10:14 +00:00
|
|
|
import org.telegram.ui.ActionBar.ActionBar;
|
|
|
|
import org.telegram.ui.ActionBar.ActionBarMenu;
|
|
|
|
import org.telegram.ui.ActionBar.BaseFragment;
|
2013-10-25 15:19:00 +00:00
|
|
|
|
2014-11-13 20:10:14 +00:00
|
|
|
public class ChangeNameActivity extends BaseFragment {
|
|
|
|
|
2013-10-25 15:19:00 +00:00
|
|
|
private EditText firstNameField;
|
|
|
|
private EditText lastNameField;
|
|
|
|
private View headerLabelView;
|
|
|
|
private View doneButton;
|
|
|
|
|
2014-11-13 20:10:14 +00:00
|
|
|
private final static int done_button = 1;
|
|
|
|
|
2013-10-25 15:19:00 +00:00
|
|
|
@Override
|
2015-02-26 01:32:51 +00:00
|
|
|
public View createView(LayoutInflater inflater) {
|
2014-06-03 23:31:48 +00:00
|
|
|
if (fragmentView == null) {
|
2014-11-13 20:10:14 +00:00
|
|
|
actionBar.setBackButtonImage(R.drawable.ic_ab_back);
|
2014-11-18 05:01:04 +00:00
|
|
|
actionBar.setAllowOverlayTitle(true);
|
2014-11-13 20:10:14 +00:00
|
|
|
actionBar.setTitle(LocaleController.getString("EditName", R.string.EditName));
|
|
|
|
actionBar.setActionBarMenuOnItemClick(new ActionBar.ActionBarMenuOnItemClick() {
|
2014-06-03 23:31:48 +00:00
|
|
|
@Override
|
2014-11-13 20:10:14 +00:00
|
|
|
public void onItemClick(int id) {
|
|
|
|
if (id == -1) {
|
2014-06-03 23:31:48 +00:00
|
|
|
finishFragment();
|
2014-11-13 20:10:14 +00:00
|
|
|
} else if (id == done_button) {
|
|
|
|
if (firstNameField.getText().length() != 0) {
|
|
|
|
saveName();
|
|
|
|
finishFragment();
|
|
|
|
}
|
2014-06-03 23:31:48 +00:00
|
|
|
}
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2014-11-13 20:10:14 +00:00
|
|
|
ActionBarMenu menu = actionBar.createMenu();
|
2014-11-17 02:44:57 +00:00
|
|
|
doneButton = menu.addItemWithWidth(done_button, R.drawable.ic_done, AndroidUtilities.dp(56));
|
2013-10-25 15:19:00 +00:00
|
|
|
|
2014-08-22 14:24:33 +00:00
|
|
|
TLRPC.User user = MessagesController.getInstance().getUser(UserConfig.getClientUserId());
|
2013-10-25 15:19:00 +00:00
|
|
|
if (user == null) {
|
2014-06-13 10:42:21 +00:00
|
|
|
user = UserConfig.getCurrentUser();
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
|
2014-12-01 17:56:31 +00:00
|
|
|
fragmentView = new LinearLayout(getParentActivity());
|
2014-10-21 20:35:16 +00:00
|
|
|
fragmentView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
|
|
|
|
((LinearLayout) fragmentView).setOrientation(LinearLayout.VERTICAL);
|
2014-11-17 02:44:57 +00:00
|
|
|
fragmentView.setOnTouchListener(new View.OnTouchListener() {
|
|
|
|
@Override
|
|
|
|
public boolean onTouch(View v, MotionEvent event) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
});
|
2014-10-21 20:35:16 +00:00
|
|
|
|
2014-12-01 17:56:31 +00:00
|
|
|
firstNameField = new EditText(getParentActivity());
|
2014-11-17 02:44:57 +00:00
|
|
|
firstNameField.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 18);
|
|
|
|
firstNameField.setHintTextColor(0xff979797);
|
|
|
|
firstNameField.setTextColor(0xff212121);
|
2014-10-21 20:35:16 +00:00
|
|
|
firstNameField.setMaxLines(1);
|
|
|
|
firstNameField.setLines(1);
|
|
|
|
firstNameField.setSingleLine(true);
|
|
|
|
firstNameField.setGravity(LocaleController.isRTL ? Gravity.RIGHT : Gravity.LEFT);
|
2014-11-17 02:44:57 +00:00
|
|
|
firstNameField.setInputType(InputType.TYPE_TEXT_FLAG_CAP_SENTENCES | InputType.TYPE_TEXT_FLAG_AUTO_CORRECT);
|
2014-10-21 20:35:16 +00:00
|
|
|
firstNameField.setImeOptions(EditorInfo.IME_ACTION_NEXT);
|
2014-03-22 22:31:55 +00:00
|
|
|
firstNameField.setHint(LocaleController.getString("FirstName", R.string.FirstName));
|
2014-11-17 02:44:57 +00:00
|
|
|
AndroidUtilities.clearCursorDrawable(firstNameField);
|
|
|
|
((LinearLayout) fragmentView).addView(firstNameField);
|
|
|
|
LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams)firstNameField.getLayoutParams();
|
|
|
|
layoutParams.topMargin = AndroidUtilities.dp(24);
|
|
|
|
layoutParams.height = AndroidUtilities.dp(36);
|
|
|
|
layoutParams.leftMargin = AndroidUtilities.dp(24);
|
|
|
|
layoutParams.rightMargin = AndroidUtilities.dp(24);
|
|
|
|
layoutParams.width = LinearLayout.LayoutParams.MATCH_PARENT;
|
|
|
|
firstNameField.setLayoutParams(layoutParams);
|
2013-10-25 15:19:00 +00:00
|
|
|
firstNameField.setOnEditorActionListener(new TextView.OnEditorActionListener() {
|
|
|
|
@Override
|
|
|
|
public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
|
|
|
|
if (i == EditorInfo.IME_ACTION_NEXT) {
|
|
|
|
lastNameField.requestFocus();
|
|
|
|
lastNameField.setSelection(lastNameField.length());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
});
|
2014-10-21 20:35:16 +00:00
|
|
|
|
2014-12-01 17:56:31 +00:00
|
|
|
lastNameField = new EditText(getParentActivity());
|
2014-11-17 02:44:57 +00:00
|
|
|
lastNameField.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 18);
|
|
|
|
lastNameField.setHintTextColor(0xff979797);
|
|
|
|
lastNameField.setTextColor(0xff212121);
|
2014-10-21 20:35:16 +00:00
|
|
|
lastNameField.setMaxLines(1);
|
|
|
|
lastNameField.setLines(1);
|
|
|
|
lastNameField.setSingleLine(true);
|
|
|
|
lastNameField.setGravity(LocaleController.isRTL ? Gravity.RIGHT : Gravity.LEFT);
|
2014-11-17 02:44:57 +00:00
|
|
|
lastNameField.setInputType(InputType.TYPE_TEXT_FLAG_CAP_SENTENCES | InputType.TYPE_TEXT_FLAG_AUTO_CORRECT);
|
2014-10-21 20:35:16 +00:00
|
|
|
lastNameField.setImeOptions(EditorInfo.IME_ACTION_DONE);
|
2014-03-22 22:31:55 +00:00
|
|
|
lastNameField.setHint(LocaleController.getString("LastName", R.string.LastName));
|
2014-11-17 02:44:57 +00:00
|
|
|
AndroidUtilities.clearCursorDrawable(lastNameField);
|
|
|
|
((LinearLayout) fragmentView).addView(lastNameField);
|
|
|
|
layoutParams = (LinearLayout.LayoutParams)lastNameField.getLayoutParams();
|
|
|
|
layoutParams.topMargin = AndroidUtilities.dp(16);
|
|
|
|
layoutParams.height = AndroidUtilities.dp(36);
|
|
|
|
layoutParams.leftMargin = AndroidUtilities.dp(24);
|
|
|
|
layoutParams.rightMargin = AndroidUtilities.dp(24);
|
|
|
|
layoutParams.width = LinearLayout.LayoutParams.MATCH_PARENT;
|
|
|
|
lastNameField.setLayoutParams(layoutParams);
|
2013-10-25 15:19:00 +00:00
|
|
|
lastNameField.setOnEditorActionListener(new TextView.OnEditorActionListener() {
|
|
|
|
@Override
|
|
|
|
public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
|
|
|
|
if (i == EditorInfo.IME_ACTION_DONE) {
|
|
|
|
doneButton.performClick();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (user != null) {
|
|
|
|
firstNameField.setText(user.first_name);
|
|
|
|
firstNameField.setSelection(firstNameField.length());
|
|
|
|
lastNameField.setText(user.last_name);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ViewGroup parent = (ViewGroup)fragmentView.getParent();
|
|
|
|
if (parent != null) {
|
|
|
|
parent.removeView(fragmentView);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return fragmentView;
|
|
|
|
}
|
|
|
|
|
2014-06-03 23:31:48 +00:00
|
|
|
@Override
|
|
|
|
public void onResume() {
|
2014-06-04 16:00:42 +00:00
|
|
|
super.onResume();
|
2014-06-03 23:31:48 +00:00
|
|
|
SharedPreferences preferences = ApplicationLoader.applicationContext.getSharedPreferences("mainconfig", Activity.MODE_PRIVATE);
|
|
|
|
boolean animations = preferences.getBoolean("view_animations", true);
|
|
|
|
if (!animations) {
|
|
|
|
firstNameField.requestFocus();
|
2014-07-02 22:39:05 +00:00
|
|
|
AndroidUtilities.showKeyboard(firstNameField);
|
2014-06-03 23:31:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-25 15:19:00 +00:00
|
|
|
private void saveName() {
|
2014-10-17 18:29:13 +00:00
|
|
|
TLRPC.User currentUser = UserConfig.getCurrentUser();
|
|
|
|
if (currentUser == null || lastNameField.getText() == null || firstNameField.getText() == null) {
|
2014-02-04 18:36:55 +00:00
|
|
|
return;
|
|
|
|
}
|
2014-10-21 20:35:16 +00: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;
|
|
|
|
}
|
2014-10-17 18:29:13 +00:00
|
|
|
TLRPC.TL_account_updateProfile req = new TLRPC.TL_account_updateProfile();
|
2014-10-21 20:35:16 +00:00
|
|
|
currentUser.first_name = req.first_name = newFirst;
|
|
|
|
currentUser.last_name = req.last_name = newLast;
|
2014-08-22 14:24:33 +00:00
|
|
|
TLRPC.User user = MessagesController.getInstance().getUser(UserConfig.getClientUserId());
|
2013-10-25 15:19:00 +00:00
|
|
|
if (user != null) {
|
|
|
|
user.first_name = req.first_name;
|
|
|
|
user.last_name = req.last_name;
|
|
|
|
}
|
2013-11-04 12:31:01 +00:00
|
|
|
UserConfig.saveConfig(true);
|
2014-11-17 02:44:57 +00:00
|
|
|
NotificationCenter.getInstance().postNotificationName(NotificationCenter.mainUserInfoChanged);
|
2014-08-22 14:24:33 +00:00
|
|
|
NotificationCenter.getInstance().postNotificationName(NotificationCenter.updateInterfaces, MessagesController.UPDATE_MASK_NAME);
|
2014-03-22 22:31:55 +00:00
|
|
|
ConnectionsManager.getInstance().performRpc(req, new RPCRequest.RPCRequestDelegate() {
|
2013-10-25 15:19:00 +00:00
|
|
|
@Override
|
|
|
|
public void run(TLObject response, TLRPC.TL_error error) {
|
|
|
|
|
|
|
|
}
|
2014-07-02 22:39:05 +00:00
|
|
|
});
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
2014-06-03 23:31:48 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onOpenAnimationEnd() {
|
|
|
|
firstNameField.requestFocus();
|
2014-07-02 22:39:05 +00:00
|
|
|
AndroidUtilities.showKeyboard(firstNameField);
|
2014-06-03 23:31:48 +00:00
|
|
|
}
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|