Commit 6eb0a8ef authored by Grigory Fedorov's avatar Grigory Fedorov

VCard editing improved. More fields. Save button. New OnVCardSaveListener interface.

parent e68e0543
......@@ -54,7 +54,7 @@ public class ConnectionManager implements OnInitializedListener, OnCloseListener
/**
* Timeout for receiving reply from server.
*/
public final static int PACKET_REPLY_TIMEOUT = 60000;
public final static int PACKET_REPLY_TIMEOUT = 30000;
public final static int PING_INTERVAL_SECONDS = 30;
......
package com.xabber.android.data.extension.vcard;
import com.xabber.android.data.BaseUIListener;
public interface OnVCardSaveListener extends BaseUIListener {
void onVCardSaveSuccess(String account);
void onVCardSaveFailed(String account);
}
......@@ -24,6 +24,7 @@ import com.xabber.android.data.account.AccountItem;
import com.xabber.android.data.account.AccountManager;
import com.xabber.android.data.account.OnAccountRemovedListener;
import com.xabber.android.data.connection.ConnectionItem;
import com.xabber.android.data.connection.ConnectionManager;
import com.xabber.android.data.connection.OnPacketListener;
import com.xabber.android.data.extension.avatar.AvatarManager;
import com.xabber.android.data.roster.OnRosterChangedListener;
......@@ -220,6 +221,18 @@ public class VCardManager implements OnLoadListener, OnPacketListener,
}
}
private void onVCardSaveSuccess(String account) {
for (OnVCardSaveListener listener : Application.getInstance().getUIListeners(OnVCardSaveListener.class)) {
listener.onVCardSaveSuccess(account);
}
}
private void onVCardSaveFailed(String account) {
for (OnVCardSaveListener listener : Application.getInstance().getUIListeners(OnVCardSaveListener.class)) {
listener.onVCardSaveFailed(account);
}
}
@Override
public void onPacket(ConnectionItem connection, final String bareAddress, Stanza packet) {
if (!(connection instanceof AccountItem)) {
......@@ -261,9 +274,9 @@ public class VCardManager implements OnLoadListener, OnPacketListener,
@Override
public void run() {
if (finalVCard == null) {
onVCardFailed(account, user);
onVCardFailed(account, Jid.getBareAddress(user));
} else {
onVCardReceived(account, user, finalVCard);
onVCardReceived(account, Jid.getBareAddress(user), finalVCard);
}
}
});
......@@ -272,18 +285,39 @@ public class VCardManager implements OnLoadListener, OnPacketListener,
thread.start();
}
public static void saveVCard(final String account, final VCard vCard) {
public void saveVCard(final String account, final VCard vCard) {
final XMPPConnection xmppConnection = AccountManager.getInstance().getAccount(account).getConnectionThread().getXMPPConnection();
final org.jivesoftware.smackx.vcardtemp.VCardManager vCardManager = org.jivesoftware.smackx.vcardtemp.VCardManager.getInstanceFor(xmppConnection);
final Thread thread = new Thread("Save vCard for account " + account) {
@Override
public void run() {
boolean isSuccess = true;
xmppConnection.setPacketReplyTimeout(120000);
try {
vCardManager.saveVCard(vCard);
} catch (SmackException.NoResponseException | XMPPException.XMPPErrorException | SmackException.NotConnectedException e) {
LogManager.w(this, "Error saving vCard: " + e.getMessage());
isSuccess = false;
}
xmppConnection.setPacketReplyTimeout(ConnectionManager.PACKET_REPLY_TIMEOUT);
final boolean finalIsSuccess = isSuccess;
Application.getInstance().runOnUiThread(new Runnable() {
@Override
public void run() {
if (finalIsSuccess) {
onVCardSaveSuccess(account);
} else {
onVCardSaveFailed(account);
}
}
});
}
};
thread.start();
......
......@@ -5,7 +5,10 @@ import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import com.xabber.android.R;
import com.xabber.android.data.Application;
......@@ -20,9 +23,11 @@ import com.xabber.xmpp.address.Jid;
import java.util.Collection;
public class AccountInfoEditor extends ManagedActivity implements OnAccountChangedListener {
public class AccountInfoEditor extends ManagedActivity implements OnAccountChangedListener, Toolbar.OnMenuItemClickListener {
public static final String ARG_VCARD = "com.xabber.android.ui.AccountInfoEditor.ARG_VCARD";
public static final int SAVE_MENU = R.menu.save;
ContactTitleActionBarInflater contactTitleActionBarInflater;
private String account;
......@@ -55,7 +60,7 @@ public class AccountInfoEditor extends ManagedActivity implements OnAccountChang
finish();
}
toolbar.setNavigationIcon(R.drawable.ic_clear_white_24dp);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
......@@ -63,11 +68,16 @@ public class AccountInfoEditor extends ManagedActivity implements OnAccountChang
}
});
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.fragment_container, AccountInfoEditorFragment.newInstance(account, vCard)).commit();
}
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
toolbar.inflateMenu(SAVE_MENU);
toolbar.setOnMenuItemClickListener(this);
}
@Override
......@@ -95,4 +105,28 @@ public class AccountInfoEditor extends ManagedActivity implements OnAccountChang
update();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(SAVE_MENU, menu);
return true;
}
@Override
public boolean onMenuItemClick(MenuItem item) {
return onOptionsItemSelected(item);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_save:
((AccountInfoEditorFragment) getFragmentManager().findFragmentById(R.id.fragment_container)).saveVCard();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
......@@ -11,11 +11,16 @@ import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
import com.xabber.android.R;
import com.xabber.android.data.Application;
import com.xabber.android.data.LogManager;
import com.xabber.android.data.extension.avatar.AvatarManager;
import com.xabber.android.data.extension.vcard.OnVCardSaveListener;
import com.xabber.android.data.extension.vcard.VCardManager;
import com.xabber.xmpp.vcard.TelephoneType;
import com.xabber.xmpp.vcard.VCardProperty;
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smackx.vcardtemp.packet.VCard;
......@@ -25,7 +30,7 @@ import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
public class AccountInfoEditorFragment extends Fragment {
public class AccountInfoEditorFragment extends Fragment implements OnVCardSaveListener {
public static final String ARGUMENT_ACCOUNT = "com.xabber.android.ui.AccountInfoEditorFragment.ARGUMENT_ACCOUNT";
public static final String ARGUMENT_VCARD = "com.xabber.android.ui.AccountInfoEditorFragment.ARGUMENT_USER";
......@@ -35,9 +40,23 @@ public class AccountInfoEditorFragment extends Fragment {
private EditText middleName;
private EditText familyName;
private EditText suffixName;
private EditText nickName;
private String account;
private ImageView avatar;
private Uri imageUri;
private EditText organization;
private EditText organizationUnit;
private EditText birthDate;
private EditText title;
private EditText role;
private EditText url;
private EditText description;
private EditText emailHome;
private EditText emailWork;
private EditText phoneHome;
private EditText phoneWork;
private EditText formattedName;
private View progressBar;
public static AccountInfoEditorFragment newInstance(String account, String vCard) {
AccountInfoEditorFragment fragment = new AccountInfoEditorFragment();
......@@ -73,12 +92,15 @@ public class AccountInfoEditorFragment extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.account_info_editor_fragment, container, false);
progressBar = view.findViewById(R.id.vcard_save_progress_bar);
prefixName = (EditText) view.findViewById(R.id.vcard_prefix_name);
formattedName = (EditText) view.findViewById(R.id.vcard_formatted_name);
givenName = (EditText) view.findViewById(R.id.vcard_given_name);
middleName = (EditText) view.findViewById(R.id.vcard_middle_name);
familyName = (EditText) view.findViewById(R.id.vcard_family_name);
suffixName = (EditText) view.findViewById(R.id.vcard_suffix_name);
nickName = (EditText) view.findViewById(R.id.vcard_nickname);
avatar = (ImageView) view.findViewById(R.id.vcard_avatar);
view.findViewById(R.id.vcard_change_avatar).setOnClickListener(new View.OnClickListener() {
......@@ -89,16 +111,80 @@ public class AccountInfoEditorFragment extends Fragment {
}
);
birthDate = (EditText) view.findViewById(R.id.vcard_birth_date);
title = (EditText) view.findViewById(R.id.vcard_title);
role = (EditText) view.findViewById(R.id.vcard_role);
organization = (EditText) view.findViewById(R.id.vcard_organization_name);
organizationUnit = (EditText) view.findViewById(R.id.vcard_organization_unit);
url = (EditText) view.findViewById(R.id.vcard_url);
description = (EditText) view.findViewById(R.id.vcard_decsription);
phoneHome = (EditText) view.findViewById(R.id.vcard_phone_home);
phoneWork = (EditText) view.findViewById(R.id.vcard_phone_work);
emailHome = (EditText) view.findViewById(R.id.vcard_email_home);
emailWork = (EditText) view.findViewById(R.id.vcard_email_work);
setFieldsFromVCard();
return view;
}
@Override
public void onResume() {
super.onResume();
Application.getInstance().addUIListener(OnVCardSaveListener.class, this);
}
@Override
public void onPause() {
super.onPause();
Application.getInstance().removeUIListener(OnVCardSaveListener.class, this);
}
private void setFieldsFromVCard() {
formattedName.setText(vCard.getField(VCardProperty.FN.name()));
prefixName.setText(vCard.getPrefix());
givenName.setText(vCard.getFirstName());
middleName.setText(vCard.getMiddleName());
familyName.setText(vCard.getLastName());
suffixName.setText(vCard.getSuffix());
nickName.setText(vCard.getNickName());
avatar.setImageDrawable(AvatarManager.getInstance().getAccountAvatar(account));
return view;
birthDate.setText(vCard.getField(VCardProperty.BDAY.name()));
title.setText(vCard.getField(VCardProperty.TITLE.name()));
role.setText(vCard.getField(VCardProperty.ROLE.name()));
organization.setText(vCard.getOrganization());
organizationUnit.setText(vCard.getOrganizationUnit());
url.setText(vCard.getField(VCardProperty.URL.name()));
description.setText(vCard.getField(VCardProperty.DESC.name()));
for (TelephoneType telephoneType : TelephoneType.values() ) {
String phone = vCard.getPhoneHome(telephoneType.name());
if (phone != null && !phone.isEmpty()) {
phoneHome.setText(phone);
}
}
for (TelephoneType telephoneType : TelephoneType.values() ) {
String phone = vCard.getPhoneWork(telephoneType.name());
if (phone != null && !phone.isEmpty()) {
phoneWork.setText(phone);
}
}
emailHome.setText(vCard.getEmailHome());
emailWork.setText(vCard.getEmailWork());
}
private void changeAvatar() {
......@@ -129,15 +215,33 @@ public class AccountInfoEditorFragment extends Fragment {
return byteBuffer.toByteArray();
}
String getValueFromEditText(EditText editText) {
String trimText = editText.getText().toString().trim();
if (trimText.isEmpty()) {
return null;
}
return trimText;
}
@Override
public void onStop() {
super.onStop();
}
private void updateVCardFromFields() {
vCard.setPrefix(getValueFromEditText(prefixName));
vCard.setFirstName(getValueFromEditText(givenName));
vCard.setMiddleName(getValueFromEditText(middleName));
vCard.setLastName(getValueFromEditText(familyName));
vCard.setSuffix(getValueFromEditText(suffixName));
vCard.setNickName(getValueFromEditText(nickName));
vCard.setPrefix(prefixName.getText().toString());
vCard.setFirstName(givenName.getText().toString());
vCard.setMiddleName(middleName.getText().toString());
vCard.setLastName(familyName.getText().toString());
vCard.setSuffix(suffixName.getText().toString());
String formattedNameText = getValueFromEditText(formattedName);
if (formattedNameText != null) {
vCard.setField(VCardProperty.FN.name(), formattedNameText);
}
if (imageUri != null) {
try {
......@@ -149,6 +253,41 @@ public class AccountInfoEditorFragment extends Fragment {
}
VCardManager.saveVCard(account, vCard);
vCard.setField(VCardProperty.BDAY.name(), getValueFromEditText(birthDate));
vCard.setField(VCardProperty.TITLE.name(), getValueFromEditText(title));
vCard.setField(VCardProperty.ROLE.name(), getValueFromEditText(role));
vCard.setOrganization(getValueFromEditText(organization));
vCard.setOrganizationUnit(getValueFromEditText(organizationUnit));
vCard.setField(VCardProperty.URL.name(), getValueFromEditText(url));
vCard.setField(VCardProperty.DESC.name(), getValueFromEditText(description));
vCard.setPhoneHome(TelephoneType.VOICE.name(), getValueFromEditText(phoneHome));
vCard.setPhoneWork(TelephoneType.VOICE.name(), getValueFromEditText(phoneWork));
vCard.setEmailHome(getValueFromEditText(emailHome));
vCard.setEmailWork(getValueFromEditText(emailWork));
}
public void saveVCard() {
updateVCardFromFields();
VCardManager.getInstance().saveVCard(account, vCard);
progressBar.setVisibility(View.VISIBLE);
Toast.makeText(getActivity(), getString(R.string.account_user_info_save_started), Toast.LENGTH_LONG).show();
}
@Override
public void onVCardSaveSuccess(String account) {
progressBar.setVisibility(View.GONE);
Toast.makeText(getActivity(), getString(R.string.account_user_info_save_success), Toast.LENGTH_LONG).show();
}
@Override
public void onVCardSaveFailed(String account) {
progressBar.setVisibility(View.GONE);
Toast.makeText(getActivity(), getString(R.string.account_user_info_save_fail), Toast.LENGTH_LONG).show();
}
}
......@@ -12,6 +12,7 @@
<string name="vcard_birth_date">Birthday</string>
<string name="vcard_url">Web site</string>
<string name="vcard_organization">Organization</string>
<string name="vcard_organization_unit">Organization unit</string>
<string name="vcard_title">Job title, function</string>
<string name="vcard_role">Role</string>
<string name="vcard_other">Other information</string>
......@@ -49,4 +50,7 @@
<string name="edit_contact_groups">Edit groups</string>
<string name="remove_contact">Remove contact</string>
<string name="edit_account_user_info">Edit user info</string>
<string name="account_user_info_save_success">Account user info saved successfully</string>
<string name="account_user_info_save_fail">Could not save account user info</string>
<string name="account_user_info_save_started">Saving account user info...</string>
</resources>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment