Nagram/TMessagesProj/src/main/java/org/telegram/ui/SettingsChangeNameActivity.java

194 lines
7.3 KiB
Java
Raw Normal View History

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;
import android.os.Bundle;
2013-12-20 19:25:49 +00:00
import android.support.v7.app.ActionBar;
2013-10-25 15:19:00 +00:00
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;
import org.telegram.TL.TLObject;
import org.telegram.TL.TLRPC;
import org.telegram.messenger.ConnectionsManager;
import org.telegram.messenger.MessagesController;
import org.telegram.messenger.NotificationCenter;
import org.telegram.messenger.R;
import org.telegram.messenger.RPCRequest;
import org.telegram.messenger.UserConfig;
import org.telegram.messenger.Utilities;
import org.telegram.ui.Views.BaseFragment;
public class SettingsChangeNameActivity extends BaseFragment {
private EditText firstNameField;
private EditText lastNameField;
private View headerLabelView;
private View doneButton;
public SettingsChangeNameActivity() {
animationType = 1;
}
@Override
public boolean canApplyUpdateStatus() {
return false;
}
@Override
public void onResume() {
super.onResume();
if (isFinish) {
return;
}
ActionBar actionBar = parentActivity.getSupportActionBar();
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowTitleEnabled(false);
2013-12-20 19:25:49 +00:00
actionBar.setDisplayHomeAsUpEnabled(false);
2013-10-25 15:19:00 +00:00
actionBar.setSubtitle(null);
2013-12-20 19:25:49 +00:00
//parentActivity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
2013-10-25 15:19:00 +00:00
actionBar.setCustomView(R.layout.settings_do_action_layout);
View cancelButton = actionBar.getCustomView().findViewById(R.id.cancel_button);
cancelButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
finishFragment();
}
});
doneButton = actionBar.getCustomView().findViewById(R.id.done_button);
doneButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
2013-12-26 16:46:13 +00:00
if (firstNameField.getText().length() != 0) {
2013-10-25 15:19:00 +00:00
saveName();
finishFragment();
}
}
});
SharedPreferences preferences = ApplicationLoader.applicationContext.getSharedPreferences("mainconfig", Activity.MODE_PRIVATE);
2013-10-25 15:19:00 +00:00
boolean animations = preferences.getBoolean("view_animations", true);
if (!animations) {
firstNameField.requestFocus();
Utilities.showKeyboard(firstNameField);
}
}
@Override
public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {
if (nextAnim != 0) {
Animation anim = AnimationUtils.loadAnimation(getActivity(), nextAnim);
anim.setAnimationListener(new Animation.AnimationListener() {
public void onAnimationStart(Animation animation) {
SettingsChangeNameActivity.this.onAnimationStart();
}
public void onAnimationRepeat(Animation animation) {
}
public void onAnimationEnd(Animation animation) {
SettingsChangeNameActivity.this.onAnimationEnd();
firstNameField.requestFocus();
Utilities.showKeyboard(firstNameField);
}
});
return anim;
} else {
return super.onCreateAnimation(transit, enter, nextAnim);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (fragmentView == null) {
fragmentView = inflater.inflate(R.layout.settings_change_name_layout, container, false);
TLRPC.User user = MessagesController.Instance.users.get(UserConfig.clientUserId);
if (user == null) {
user = UserConfig.currentUser;
}
firstNameField = (EditText)fragmentView.findViewById(R.id.first_name_field);
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;
}
});
lastNameField = (EditText)fragmentView.findViewById(R.id.last_name_field);
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);
}
TextView headerLabel = (TextView)fragmentView.findViewById(R.id.settings_section_text);
headerLabel.setText(getStringEntry(R.string.YourFirstNameAndLastName));
} else {
ViewGroup parent = (ViewGroup)fragmentView.getParent();
if (parent != null) {
parent.removeView(fragmentView);
}
}
return fragmentView;
}
private void saveName() {
TLRPC.TL_account_updateProfile req = new TLRPC.TL_account_updateProfile();
if (UserConfig.currentUser == null || lastNameField.getText() == null || firstNameField.getText() == null) {
return;
}
2013-10-25 15:19:00 +00:00
UserConfig.currentUser.first_name = req.first_name = firstNameField.getText().toString();
UserConfig.currentUser.last_name = req.last_name = lastNameField.getText().toString();
TLRPC.User user = MessagesController.Instance.users.get(UserConfig.clientUserId);
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);
NotificationCenter.Instance.postNotificationName(MessagesController.updateInterfaces, MessagesController.UPDATE_MASK_NAME);
2013-10-25 15:19:00 +00:00
ConnectionsManager.Instance.performRpc(req, new RPCRequest.RPCRequestDelegate() {
@Override
public void run(TLObject response, TLRPC.TL_error error) {
}
}, null, true, RPCRequest.RPCRequestClassGeneric);
}
}