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

295 lines
11 KiB
Java
Raw Normal View History

2013-10-25 17:19:00 +02:00
/*
* This is the source code of Telegram for Android v. 1.2.3.
* 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.Context;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.Display;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Surface;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
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 com.actionbarsherlock.app.ActionBar;
import org.telegram.PhoneFormat.PhoneFormat;
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.Utilities;
import org.telegram.ui.Views.BackupImageView;
import org.telegram.ui.Views.BaseFragment;
public class ContactAddActivity extends BaseFragment implements NotificationCenter.NotificationCenterDelegate {
private int user_id;
private View doneButton;
private EditText firstNameField;
private EditText lastNameField;
private BackupImageView avatarImage;
private TextView onlineText;
private TextView phoneText;
@Override
public boolean onFragmentCreate() {
super.onFragmentCreate();
NotificationCenter.Instance.addObserver(this, MessagesController.updateInterfaces);
user_id = getArguments().getInt("user_id", 0);
return true;
}
@Override
public void onFragmentDestroy() {
super.onFragmentDestroy();
NotificationCenter.Instance.removeObserver(this, MessagesController.updateInterfaces);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (fragmentView == null) {
fragmentView = inflater.inflate(R.layout.contact_add_layout, container, false);
TLRPC.User user = MessagesController.Instance.users.get(user_id);
Typeface typeface = Utilities.getTypeface("fonts/rlight.ttf");
onlineText = (TextView)fragmentView.findViewById(R.id.settings_online);
onlineText.setTypeface(typeface);
avatarImage = (BackupImageView)fragmentView.findViewById(R.id.settings_avatar_image);
phoneText = (TextView)fragmentView.findViewById(R.id.settings_name);
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);
}
updateAvatarLayout();
} else {
ViewGroup parent = (ViewGroup)fragmentView.getParent();
if (parent != null) {
parent.removeView(fragmentView);
}
}
return fragmentView;
}
private void updateAvatarLayout() {
if (phoneText == null) {
return;
}
TLRPC.User user = MessagesController.Instance.users.get(user_id);
if (user == null) {
return;
}
phoneText.setText(PhoneFormat.Instance.format("+" + user.phone));
if (user.status == null) {
onlineText.setText(getStringEntry(R.string.Offline));
} else {
int currentTime = ConnectionsManager.Instance.getCurrentTime();
if (user.status.expires > currentTime || user.status.was_online > currentTime) {
onlineText.setText(getStringEntry(R.string.Online));
} else {
if (user.status.was_online <= 10000 && user.status.expires <= 10000) {
onlineText.setText(getStringEntry(R.string.Invisible));
} else {
int value = user.status.was_online;
if (value == 0) {
value = user.status.expires;
}
onlineText.setText(String.format("%s %s", getStringEntry(R.string.LastSeen), Utilities.formatDateOnline(value)));
}
}
}
TLRPC.FileLocation photo = null;
if (user.photo != null) {
photo = user.photo.photo_small;
}
avatarImage.setImage(photo, "50_50", Utilities.getUserAvatarForId(user.id));
}
public void didReceivedNotification(int id, Object... args) {
if (id == MessagesController.updateInterfaces) {
updateAvatarLayout();
}
}
@Override
public boolean canApplyUpdateStatus() {
return false;
}
@Override
public void applySelfActionBar() {
if (parentActivity == null) {
return;
}
ActionBar actionBar = parentActivity.getSupportActionBar();
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowTitleEnabled(false);
TextView title = (TextView)parentActivity.findViewById(R.id.abs__action_bar_title);
if (title == null) {
final int subtitleId = parentActivity.getResources().getIdentifier("action_bar_title", "id", "android");
title = (TextView)parentActivity.findViewById(subtitleId);
}
if (title != null) {
title.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
title.setCompoundDrawablePadding(0);
}
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) {
if (firstNameField.getText().length() != 0) {
TLRPC.User user = MessagesController.Instance.users.get(user_id);
user.first_name = firstNameField.getText().toString();
user.last_name = lastNameField.getText().toString();
MessagesController.Instance.addContact(user);
finishFragment();
}
}
});
}
@Override
public void onResume() {
super.onResume();
if (getSherlockActivity() == null) {
return;
}
((ApplicationActivity)parentActivity).updateActionBar();
fixLayout();
2013-11-04 13:31:01 +01:00
SharedPreferences preferences = ApplicationLoader.applicationContext.getSharedPreferences("Notifications", Activity.MODE_PRIVATE);
2013-10-25 17:19:00 +02:00
boolean animations = preferences.getBoolean("view_animations", true);
if (!animations) {
firstNameField.requestFocus();
Utilities.showKeyboard(firstNameField);
}
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
fixLayout();
}
private void fixLayout() {
final View view = getView();
if (view != null) {
ViewTreeObserver obs = view.getViewTreeObserver();
obs.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
@Override
public boolean onPreDraw() {
WindowManager manager = (WindowManager)parentActivity.getSystemService(Context.WINDOW_SERVICE);
Display display = manager.getDefaultDisplay();
int rotation = display.getRotation();
int height;
int currentActionBarHeight = parentActivity.getSupportActionBar().getHeight();
2013-11-04 13:31:01 +01:00
float density = ApplicationLoader.applicationContext.getResources().getDisplayMetrics().density;
2013-10-25 17:19:00 +02:00
if (currentActionBarHeight != 48 * density && currentActionBarHeight != 40 * density) {
height = currentActionBarHeight;
} else {
height = (int)(48.0f * density);
if (rotation == Surface.ROTATION_270 || rotation == Surface.ROTATION_90) {
height = (int)(40.0f * density);
}
}
view.setPadding(view.getPaddingLeft(), height, view.getPaddingRight(), view.getPaddingBottom());
view.getViewTreeObserver().removeOnPreDrawListener(this);
return false;
}
});
}
}
@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) {
ContactAddActivity.this.onAnimationStart();
}
public void onAnimationRepeat(Animation animation) {
}
public void onAnimationEnd(Animation animation) {
ContactAddActivity.this.onAnimationEnd();
firstNameField.requestFocus();
Utilities.showKeyboard(firstNameField);
}
});
return anim;
} else {
return super.onCreateAnimation(transit, enter, nextAnim);
}
}
}