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

302 lines
16 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;
2017-03-31 01:58:05 +02:00
import android.graphics.drawable.Drawable;
2013-10-25 17:19:00 +02:00
import android.os.Bundle;
2014-11-17 03:44:57 +01:00
import android.text.InputType;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.Gravity;
2013-10-25 17:19:00 +02:00
import android.view.View;
import android.view.inputmethod.EditorInfo;
2014-11-17 03:44:57 +01:00
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.ScrollView;
2013-10-25 17:19:00 +02:00
import android.widget.TextView;
2015-09-24 22:52:02 +02:00
import org.telegram.messenger.AndroidUtilities;
2013-10-25 17:19:00 +02:00
import org.telegram.PhoneFormat.PhoneFormat;
2015-09-24 22:52:02 +02:00
import org.telegram.messenger.ContactsController;
import org.telegram.messenger.LocaleController;
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;
2014-11-13 21:10:14 +01:00
import org.telegram.ui.ActionBar.ActionBar;
import org.telegram.ui.ActionBar.ActionBarMenu;
2017-03-31 01:58:05 +02:00
import org.telegram.ui.ActionBar.Theme;
import org.telegram.ui.ActionBar.ThemeDescription;
import org.telegram.ui.Components.AvatarDrawable;
import org.telegram.ui.Components.BackupImageView;
2014-11-13 21:10:14 +01:00
import org.telegram.ui.ActionBar.BaseFragment;
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
public class ContactAddActivity extends BaseFragment implements NotificationCenter.NotificationCenterDelegate {
2014-11-13 21:10:14 +01:00
2013-10-25 17:19:00 +02:00
private View doneButton;
2017-12-08 18:35:59 +01:00
private EditTextBoldCursor firstNameField;
private EditTextBoldCursor lastNameField;
2013-10-25 17:19:00 +02:00
private BackupImageView avatarImage;
2014-11-17 03:44:57 +01:00
private TextView nameTextView;
private TextView onlineTextView;
2017-03-31 01:58:05 +02:00
private AvatarDrawable avatarDrawable;
2013-10-25 17:19:00 +02:00
2014-11-13 21:10:14 +01:00
private int user_id;
private boolean addContact;
private String phone = null;
private final static int done_button = 1;
public ContactAddActivity(Bundle args) {
super(args);
}
2013-10-25 17:19:00 +02:00
@Override
public boolean onFragmentCreate() {
2018-07-30 04:07:02 +02:00
NotificationCenter.getInstance(currentAccount).addObserver(this, NotificationCenter.updateInterfaces);
2013-10-25 17:19:00 +02:00
user_id = getArguments().getInt("user_id", 0);
2013-12-20 20:25:49 +01:00
phone = getArguments().getString("phone");
2014-11-13 21:10:14 +01:00
addContact = getArguments().getBoolean("addContact", false);
2018-07-30 04:07:02 +02:00
TLRPC.User user = MessagesController.getInstance(currentAccount).getUser(user_id);
return user != null && super.onFragmentCreate();
2013-10-25 17:19:00 +02:00
}
@Override
public void onFragmentDestroy() {
super.onFragmentDestroy();
2018-07-30 04:07:02 +02:00
NotificationCenter.getInstance(currentAccount).removeObserver(this, NotificationCenter.updateInterfaces);
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);
if (addContact) {
actionBar.setTitle(LocaleController.getString("AddContactTitle", R.string.AddContactTitle));
} else {
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) {
2018-07-30 04:07:02 +02:00
TLRPC.User user = MessagesController.getInstance(currentAccount).getUser(user_id);
2015-04-09 20:00:14 +02:00
user.first_name = firstNameField.getText().toString();
user.last_name = lastNameField.getText().toString();
2018-07-30 04:07:02 +02:00
ContactsController.getInstance(currentAccount).addContact(user);
finishFragment();
2018-07-30 04:07:02 +02:00
SharedPreferences preferences = MessagesController.getNotificationsSettings(currentAccount);
2016-04-22 15:49:00 +02:00
preferences.edit().putInt("spam3_" + user_id, 1).commit();
2018-07-30 04:07:02 +02:00
NotificationCenter.getInstance(currentAccount).postNotificationName(NotificationCenter.updateInterfaces, MessagesController.UPDATE_MASK_NAME);
2019-01-23 18:03:33 +01:00
NotificationCenter.getInstance(currentAccount).postNotificationName(NotificationCenter.peerSettingsDidLoad, (long) user_id);
}
}
2015-04-09 20:00:14 +02:00
}
});
2015-04-09 20:00:14 +02:00
ActionBarMenu menu = actionBar.createMenu();
doneButton = menu.addItemWithWidth(done_button, R.drawable.ic_done, AndroidUtilities.dp(56));
2014-11-17 03:44:57 +01:00
2015-04-09 20:00:14 +02:00
fragmentView = new ScrollView(context);
2013-10-25 17:19:00 +02:00
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) fragmentView).addView(linearLayout, LayoutHelper.createScroll(LayoutHelper.MATCH_PARENT, LayoutHelper.WRAP_CONTENT, Gravity.TOP | Gravity.LEFT));
2019-01-23 18:03:33 +01:00
linearLayout.setOnTouchListener((v, event) -> true);
2014-11-17 03:44:57 +01:00
2015-04-09 20:00:14 +02:00
FrameLayout frameLayout = new FrameLayout(context);
2017-03-31 01:58:05 +02:00
linearLayout.addView(frameLayout, LayoutHelper.createLinear(LayoutHelper.MATCH_PARENT, LayoutHelper.WRAP_CONTENT, 24, 24, 24, 0));
2014-11-17 03:44:57 +01:00
2015-04-09 20:00:14 +02:00
avatarImage = new BackupImageView(context);
avatarImage.setRoundRadius(AndroidUtilities.dp(30));
2017-03-31 01:58:05 +02:00
frameLayout.addView(avatarImage, LayoutHelper.createFrame(60, 60, (LocaleController.isRTL ? Gravity.RIGHT : Gravity.LEFT) | Gravity.TOP));
2013-10-25 17:19:00 +02:00
2015-04-09 20:00:14 +02:00
nameTextView = new TextView(context);
2017-03-31 01:58:05 +02:00
nameTextView.setTextColor(Theme.getColor(Theme.key_windowBackgroundWhiteBlackText));
2015-04-09 20:00:14 +02:00
nameTextView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 20);
nameTextView.setLines(1);
nameTextView.setMaxLines(1);
nameTextView.setSingleLine(true);
nameTextView.setEllipsize(TextUtils.TruncateAt.END);
nameTextView.setGravity((LocaleController.isRTL ? Gravity.RIGHT : Gravity.LEFT));
nameTextView.setTypeface(AndroidUtilities.getTypeface("fonts/rmedium.ttf"));
2017-03-31 01:58:05 +02:00
frameLayout.addView(nameTextView, LayoutHelper.createFrame(LayoutHelper.WRAP_CONTENT, LayoutHelper.WRAP_CONTENT, (LocaleController.isRTL ? Gravity.RIGHT : Gravity.LEFT) | Gravity.TOP, LocaleController.isRTL ? 0 : 80, 3, LocaleController.isRTL ? 80 : 0, 0));
2014-11-17 03:44:57 +01:00
2015-04-09 20:00:14 +02:00
onlineTextView = new TextView(context);
2017-03-31 01:58:05 +02:00
onlineTextView.setTextColor(Theme.getColor(Theme.key_windowBackgroundWhiteGrayText3));
2015-04-09 20:00:14 +02:00
onlineTextView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 14);
onlineTextView.setLines(1);
onlineTextView.setMaxLines(1);
onlineTextView.setSingleLine(true);
onlineTextView.setEllipsize(TextUtils.TruncateAt.END);
onlineTextView.setGravity((LocaleController.isRTL ? Gravity.RIGHT : Gravity.LEFT));
2017-03-31 01:58:05 +02:00
frameLayout.addView(onlineTextView, LayoutHelper.createFrame(LayoutHelper.WRAP_CONTENT, LayoutHelper.WRAP_CONTENT, (LocaleController.isRTL ? Gravity.RIGHT : Gravity.LEFT) | Gravity.TOP, LocaleController.isRTL ? 0 : 80, 32, LocaleController.isRTL ? 80 : 0, 0));
2014-11-17 03:44:57 +01: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);
2017-03-31 01:58:05 +02:00
linearLayout.addView(firstNameField, LayoutHelper.createLinear(LayoutHelper.MATCH_PARENT, 36, 24, 24, 24, 0));
2019-01-23 18:03:33 +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-01-23 18:03:33 +01:00
return false;
2015-04-09 20:00:14 +02:00
});
2013-10-25 17:19:00 +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);
2017-03-31 01:58:05 +02:00
linearLayout.addView(lastNameField, LayoutHelper.createLinear(LayoutHelper.MATCH_PARENT, 36, 24, 16, 24, 0));
2019-01-23 18:03:33 +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-01-23 18:03:33 +01:00
return false;
2015-04-09 20:00:14 +02:00
});
2013-10-25 17:19:00 +02:00
2018-07-30 04:07:02 +02:00
TLRPC.User user = MessagesController.getInstance(currentAccount).getUser(user_id);
2015-04-09 20:00:14 +02:00
if (user != null) {
if (user.phone == null) {
if (phone != null) {
user.phone = PhoneFormat.stripExceptNumbers(phone);
}
2013-10-25 17:19:00 +02:00
}
2015-04-09 20:00:14 +02:00
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;
}
private void updateAvatarLayout() {
2014-11-17 03:44:57 +01:00
if (nameTextView == null) {
2013-10-25 17:19:00 +02:00
return;
}
2018-07-30 04:07:02 +02:00
TLRPC.User user = MessagesController.getInstance(currentAccount).getUser(user_id);
2013-10-25 17:19:00 +02:00
if (user == null) {
return;
}
2014-11-17 03:44:57 +01:00
nameTextView.setText(PhoneFormat.getInstance().format("+" + user.phone));
2018-07-30 04:07:02 +02:00
onlineTextView.setText(LocaleController.formatUserStatus(currentAccount, user));
2013-10-25 17:19:00 +02:00
TLRPC.FileLocation photo = null;
if (user.photo != null) {
photo = user.photo.photo_small;
}
2019-01-23 18:03:33 +01:00
avatarImage.setImage(photo, "50_50", avatarDrawable = new AvatarDrawable(user), user);
2013-10-25 17:19:00 +02:00
}
2018-07-30 04:07:02 +02:00
public void didReceivedNotification(int id, int account, Object... args) {
if (id == NotificationCenter.updateInterfaces) {
2014-11-17 03:44:57 +01:00
int mask = (Integer) args[0];
if ((mask & MessagesController.UPDATE_MASK_AVATAR) != 0 || (mask & MessagesController.UPDATE_MASK_STATUS) != 0) {
updateAvatarLayout();
}
2013-10-25 17:19:00 +02:00
}
}
@Override
public void onResume() {
super.onResume();
2015-04-09 20:00:14 +02:00
updateAvatarLayout();
2018-07-30 04:07:02 +02:00
SharedPreferences preferences = MessagesController.getGlobalMainSettings();
2013-10-25 17:19:00 +02:00
boolean animations = preferences.getBoolean("view_animations", true);
if (!animations) {
firstNameField.requestFocus();
AndroidUtilities.showKeyboard(firstNameField);
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) {
firstNameField.requestFocus();
AndroidUtilities.showKeyboard(firstNameField);
}
2013-10-25 17:19:00 +02:00
}
2017-03-31 01:58:05 +02:00
@Override
public ThemeDescription[] getThemeDescriptions() {
2019-01-23 18:03:33 +01:00
ThemeDescription.ThemeDescriptionDelegate cellDelegate = () -> {
if (avatarImage != null) {
TLRPC.User user = MessagesController.getInstance(currentAccount).getUser(user_id);
if (user == null) {
return;
2017-03-31 01:58:05 +02:00
}
2019-01-23 18:03:33 +01:00
avatarDrawable.setInfo(user);
avatarImage.invalidate();
2017-03-31 01:58:05 +02:00
}
};
return new ThemeDescription[]{
new ThemeDescription(fragmentView, ThemeDescription.FLAG_BACKGROUND, null, null, null, null, Theme.key_windowBackgroundWhite),
new ThemeDescription(actionBar, ThemeDescription.FLAG_BACKGROUND, null, null, null, null, Theme.key_actionBarDefault),
new ThemeDescription(actionBar, ThemeDescription.FLAG_AB_ITEMSCOLOR, null, null, null, null, Theme.key_actionBarDefaultIcon),
new ThemeDescription(actionBar, ThemeDescription.FLAG_AB_TITLECOLOR, null, null, null, null, Theme.key_actionBarDefaultTitle),
new ThemeDescription(actionBar, ThemeDescription.FLAG_AB_SELECTORCOLOR, null, null, null, null, Theme.key_actionBarDefaultSelector),
new ThemeDescription(nameTextView, ThemeDescription.FLAG_TEXTCOLOR, null, null, null, null, Theme.key_windowBackgroundWhiteBlackText),
new ThemeDescription(onlineTextView, ThemeDescription.FLAG_TEXTCOLOR, null, null, null, null, Theme.key_windowBackgroundWhiteGrayText3),
new ThemeDescription(firstNameField, ThemeDescription.FLAG_TEXTCOLOR, null, null, null, null, Theme.key_windowBackgroundWhiteBlackText),
new ThemeDescription(firstNameField, ThemeDescription.FLAG_HINTTEXTCOLOR, null, null, null, null, Theme.key_windowBackgroundWhiteHintText),
new ThemeDescription(firstNameField, ThemeDescription.FLAG_BACKGROUNDFILTER, null, null, null, null, Theme.key_windowBackgroundWhiteInputField),
new ThemeDescription(firstNameField, ThemeDescription.FLAG_BACKGROUNDFILTER | ThemeDescription.FLAG_DRAWABLESELECTEDSTATE, null, null, null, null, Theme.key_windowBackgroundWhiteInputFieldActivated),
new ThemeDescription(lastNameField, ThemeDescription.FLAG_TEXTCOLOR, null, null, null, null, Theme.key_windowBackgroundWhiteBlackText),
new ThemeDescription(lastNameField, ThemeDescription.FLAG_HINTTEXTCOLOR, null, null, null, null, Theme.key_windowBackgroundWhiteHintText),
new ThemeDescription(lastNameField, ThemeDescription.FLAG_BACKGROUNDFILTER, null, null, null, null, Theme.key_windowBackgroundWhiteInputField),
new ThemeDescription(lastNameField, ThemeDescription.FLAG_BACKGROUNDFILTER | ThemeDescription.FLAG_DRAWABLESELECTEDSTATE, null, null, null, null, Theme.key_windowBackgroundWhiteInputFieldActivated),
2019-01-23 18:03:33 +01:00
new ThemeDescription(null, 0, null, null, new Drawable[]{Theme.avatar_broadcastDrawable, Theme.avatar_savedDrawable}, cellDelegate, Theme.key_avatar_text),
2018-07-30 04:07:02 +02:00
new ThemeDescription(null, 0, null, null, null, cellDelegate, Theme.key_avatar_backgroundRed),
new ThemeDescription(null, 0, null, null, null, cellDelegate, Theme.key_avatar_backgroundOrange),
new ThemeDescription(null, 0, null, null, null, cellDelegate, Theme.key_avatar_backgroundViolet),
new ThemeDescription(null, 0, null, null, null, cellDelegate, Theme.key_avatar_backgroundGreen),
new ThemeDescription(null, 0, null, null, null, cellDelegate, Theme.key_avatar_backgroundCyan),
new ThemeDescription(null, 0, null, null, null, cellDelegate, Theme.key_avatar_backgroundBlue),
new ThemeDescription(null, 0, null, null, null, cellDelegate, Theme.key_avatar_backgroundPink),
2017-03-31 01:58:05 +02:00
};
}
2013-10-25 17:19:00 +02:00
}