Commit fffe11b3 authored by Grigory Fedorov's avatar Grigory Fedorov

All vcard operations moved to ContactVcardViewerFragment.

parent fc2c2dc4
...@@ -12,9 +12,16 @@ import android.widget.ImageView; ...@@ -12,9 +12,16 @@ import android.widget.ImageView;
import android.widget.LinearLayout; import android.widget.LinearLayout;
import android.widget.TextView; import android.widget.TextView;
import com.xabber.android.data.Application;
import com.xabber.android.data.LogManager;
import com.xabber.android.data.VcardMaps; import com.xabber.android.data.VcardMaps;
import com.xabber.android.data.account.OnAccountChangedListener;
import com.xabber.android.data.entity.BaseEntity;
import com.xabber.android.data.extension.capability.CapabilitiesManager; import com.xabber.android.data.extension.capability.CapabilitiesManager;
import com.xabber.android.data.extension.capability.ClientInfo; import com.xabber.android.data.extension.capability.ClientInfo;
import com.xabber.android.data.extension.vcard.OnVCardListener;
import com.xabber.android.data.extension.vcard.VCardManager;
import com.xabber.android.data.roster.OnContactChangedListener;
import com.xabber.android.data.roster.PresenceManager; import com.xabber.android.data.roster.PresenceManager;
import com.xabber.android.data.roster.ResourceItem; import com.xabber.android.data.roster.ResourceItem;
import com.xabber.androiddev.R; import com.xabber.androiddev.R;
...@@ -29,20 +36,76 @@ import com.xabber.xmpp.vcard.Telephone; ...@@ -29,20 +36,76 @@ import com.xabber.xmpp.vcard.Telephone;
import com.xabber.xmpp.vcard.TelephoneType; import com.xabber.xmpp.vcard.TelephoneType;
import com.xabber.xmpp.vcard.VCard; import com.xabber.xmpp.vcard.VCard;
import com.xabber.xmpp.vcard.VCardProperty; import com.xabber.xmpp.vcard.VCardProperty;
import com.xabber.xmpp.vcard.VCardProvider;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;
import java.io.StringReader;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection;
import java.util.List; import java.util.List;
public class ContactVcardViewerFragment extends Fragment { public class ContactVcardViewerFragment extends Fragment implements OnVCardListener, OnContactChangedListener, OnAccountChangedListener {
private static final String SAVED_VCARD = "com.xabber.android.ui.ContactVcardViewerFragment.SAVED_VCARD";
private static final String SAVED_VCARD_ERROR = "com.xabber.android.ui.ContactVcardViewerFragment.SAVED_VCARD_ERROR";
public static final String ARGUMENT_ACCOUNT = "com.xabber.android.ui.ContactVcardViewerFragment.ARGUMENT_ACCOUNT";
public static final String ARGUMENT_USER = "com.xabber.android.ui.ContactVcardViewerFragment.ARGUMENT_USER";
private LinearLayout xmppItems; private LinearLayout xmppItems;
private LinearLayout contactInfoItems; private LinearLayout contactInfoItems;
String account; String account;
String bareAddress; String user;
private VCard vCard;
private boolean vCardError;
public static ContactVcardViewerFragment newInstance(String account, String user) {
ContactVcardViewerFragment fragment = new ContactVcardViewerFragment();
Bundle arguments = new Bundle();
arguments.putString(ARGUMENT_ACCOUNT, account);
arguments.putString(ARGUMENT_USER, user);
fragment.setArguments(arguments);
return fragment;
}
@Override @Override
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
Bundle args = getArguments();
account = args.getString(ARGUMENT_ACCOUNT, null);
user = args.getString(ARGUMENT_USER, null);
vCard = null;
vCardError = false;
if (savedInstanceState != null) {
vCardError = savedInstanceState.getBoolean(SAVED_VCARD_ERROR, false);
String xml = savedInstanceState.getString(SAVED_VCARD);
if (xml != null)
try {
XmlPullParser parser = XmlPullParserFactory.newInstance().newPullParser();
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
parser.setInput(new StringReader(xml));
int eventType = parser.next();
if (eventType != XmlPullParser.START_TAG) {
throw new IllegalStateException(String.valueOf(eventType));
}
if (!VCard.ELEMENT_NAME.equals(parser.getName())) {
throw new IllegalStateException(parser.getName());
}
if (!VCard.NAMESPACE.equals(parser.getNamespace())) {
throw new IllegalStateException(parser.getNamespace());
}
vCard = (VCard) (new VCardProvider()).parseIQ(parser);
} catch (Exception e) {
LogManager.exception(this, e);
}
}
} }
@Nullable @Nullable
...@@ -58,6 +121,74 @@ public class ContactVcardViewerFragment extends Fragment { ...@@ -58,6 +121,74 @@ public class ContactVcardViewerFragment extends Fragment {
return view; return view;
} }
@Override
public void onResume() {
super.onResume();
Application.getInstance().addUIListener(OnVCardListener.class, this);
Application.getInstance().addUIListener(OnContactChangedListener.class, this);
Application.getInstance().addUIListener(OnAccountChangedListener.class, this);
if (vCard == null && !vCardError) {
VCardManager.getInstance().request(account, user, null);
}
updateContact(account, user);
updateVCard(vCard);
}
@Override
public void onPause() {
super.onPause();
Application.getInstance().removeUIListener(OnVCardListener.class, this);
Application.getInstance().removeUIListener(OnContactChangedListener.class, this);
Application.getInstance().removeUIListener(OnAccountChangedListener.class, this);
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putBoolean(SAVED_VCARD_ERROR, vCardError);
if (vCard != null) {
outState.putString(SAVED_VCARD, vCard.getChildElementXML());
}
}
@Override
public void onVCardReceived(String account, String bareAddress, VCard vCard) {
if (!this.account.equals(account) || !this.user.equals(bareAddress)) {
return;
}
this.vCard = vCard;
this.vCardError = false;
updateVCard(vCard);
}
@Override
public void onVCardFailed(String account, String bareAddress) {
if (!this.account.equals(account) || !this.user.equals(bareAddress)) {
return;
}
this.vCard = null;
this.vCardError = true;
Application.getInstance().onError(R.string.XMPP_EXCEPTION);
}
@Override
public void onContactsChanged(Collection<BaseEntity> entities) {
for (BaseEntity entity : entities) {
if (entity.equals(account, user)) {
updateContact(account, user);
break;
}
}
}
@Override
public void onAccountsChanged(Collection<String> accounts) {
if (accounts.contains(account)) {
updateContact(account, user);
}
}
/** /**
* @param source * @param source
* @param value * @param value
...@@ -76,7 +207,7 @@ public class ContactVcardViewerFragment extends Fragment { ...@@ -76,7 +207,7 @@ public class ContactVcardViewerFragment extends Fragment {
public void updateContact(String account, String bareAddress) { public void updateContact(String account, String bareAddress) {
this.account = account; this.account = account;
this.bareAddress = bareAddress; this.user = bareAddress;
xmppItems.removeAllViews(); xmppItems.removeAllViews();
......
...@@ -22,12 +22,9 @@ import android.view.View; ...@@ -22,12 +22,9 @@ import android.view.View;
import android.widget.TextView; import android.widget.TextView;
import com.xabber.android.data.Application; import com.xabber.android.data.Application;
import com.xabber.android.data.LogManager;
import com.xabber.android.data.account.AccountManager; import com.xabber.android.data.account.AccountManager;
import com.xabber.android.data.account.OnAccountChangedListener; import com.xabber.android.data.account.OnAccountChangedListener;
import com.xabber.android.data.entity.BaseEntity; import com.xabber.android.data.entity.BaseEntity;
import com.xabber.android.data.extension.vcard.OnVCardListener;
import com.xabber.android.data.extension.vcard.VCardManager;
import com.xabber.android.data.intent.AccountIntentBuilder; import com.xabber.android.data.intent.AccountIntentBuilder;
import com.xabber.android.data.intent.EntityIntentBuilder; import com.xabber.android.data.intent.EntityIntentBuilder;
import com.xabber.android.data.roster.AbstractContact; import com.xabber.android.data.roster.AbstractContact;
...@@ -39,26 +36,15 @@ import com.xabber.android.ui.helper.ContactTitleExpandableToolbarInflater; ...@@ -39,26 +36,15 @@ import com.xabber.android.ui.helper.ContactTitleExpandableToolbarInflater;
import com.xabber.android.ui.helper.ManagedActivity; import com.xabber.android.ui.helper.ManagedActivity;
import com.xabber.androiddev.R; import com.xabber.androiddev.R;
import com.xabber.xmpp.address.Jid; import com.xabber.xmpp.address.Jid;
import com.xabber.xmpp.vcard.VCard;
import com.xabber.xmpp.vcard.VCardProvider;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;
import java.io.StringReader;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
public class ContactViewer extends ManagedActivity implements public class ContactViewer extends ManagedActivity implements
OnVCardListener, OnContactChangedListener, OnAccountChangedListener { OnContactChangedListener, OnAccountChangedListener {
private static final String SAVED_VCARD = "com.xabber.android.ui.ContactViewer.SAVED_VCARD";
private static final String SAVED_VCARD_ERROR = "com.xabber.android.ui.ContactViewer.SAVED_VCARD_ERROR";
private String account; private String account;
private String bareAddress; private String bareAddress;
private VCard vCard;
private boolean vCardError;
private ContactTitleExpandableToolbarInflater contactTitleExpandableToolbarInflater; private ContactTitleExpandableToolbarInflater contactTitleExpandableToolbarInflater;
private TextView contactNameView; private TextView contactNameView;
...@@ -104,35 +90,10 @@ public class ContactViewer extends ManagedActivity implements ...@@ -104,35 +90,10 @@ public class ContactViewer extends ManagedActivity implements
finish(); finish();
return; return;
} }
vCard = null;
vCardError = false;
if (savedInstanceState != null) {
vCardError = savedInstanceState.getBoolean(SAVED_VCARD_ERROR, false);
String xml = savedInstanceState.getString(SAVED_VCARD);
if (xml != null)
try {
XmlPullParser parser = XmlPullParserFactory.newInstance().newPullParser();
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
parser.setInput(new StringReader(xml));
int eventType = parser.next();
if (eventType != XmlPullParser.START_TAG) {
throw new IllegalStateException(String.valueOf(eventType));
}
if (!VCard.ELEMENT_NAME.equals(parser.getName())) {
throw new IllegalStateException(parser.getName());
}
if (!VCard.NAMESPACE.equals(parser.getNamespace())) {
throw new IllegalStateException(parser.getNamespace());
}
vCard = (VCard) (new VCardProvider()).parseIQ(parser);
} catch (Exception e) {
LogManager.exception(this, e);
}
}
if (savedInstanceState == null) { if (savedInstanceState == null) {
getFragmentManager().beginTransaction() getFragmentManager().beginTransaction()
.add(R.id.scrollable_container, new ContactVcardViewerFragment()).commit(); .add(R.id.scrollable_container, ContactVcardViewerFragment.newInstance(account, bareAddress)).commit();
} }
...@@ -149,68 +110,24 @@ public class ContactViewer extends ManagedActivity implements ...@@ -149,68 +110,24 @@ public class ContactViewer extends ManagedActivity implements
@Override @Override
protected void onResume() { protected void onResume() {
super.onResume(); super.onResume();
Application.getInstance().addUIListener(OnVCardListener.class, this);
Application.getInstance().addUIListener(OnContactChangedListener.class, this); Application.getInstance().addUIListener(OnContactChangedListener.class, this);
Application.getInstance().addUIListener(OnAccountChangedListener.class, this); Application.getInstance().addUIListener(OnAccountChangedListener.class, this);
if (vCard == null && !vCardError) {
VCardManager.getInstance().request(account, bareAddress, null);
}
contactTitleExpandableToolbarInflater.onResume(); contactTitleExpandableToolbarInflater.onResume();
ContactVcardViewerFragment contactViewerFragment = getFragment();
contactViewerFragment.updateContact(account, bareAddress);
contactViewerFragment.updateVCard(vCard);
}
private ContactVcardViewerFragment getFragment() {
return (ContactVcardViewerFragment) getFragmentManager().findFragmentById(R.id.scrollable_container);
} }
@Override @Override
protected void onPause() { public void onPause() {
super.onPause(); super.onPause();
Application.getInstance().removeUIListener(OnVCardListener.class, this);
Application.getInstance().removeUIListener(OnContactChangedListener.class, this); Application.getInstance().removeUIListener(OnContactChangedListener.class, this);
Application.getInstance().removeUIListener(OnAccountChangedListener.class, this); Application.getInstance().removeUIListener(OnAccountChangedListener.class, this);
} }
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putBoolean(SAVED_VCARD_ERROR, vCardError);
if (vCard != null) {
outState.putString(SAVED_VCARD, vCard.getChildElementXML());
}
}
@Override
public void onVCardReceived(String account, String bareAddress, VCard vCard) {
if (!this.account.equals(account) || !this.bareAddress.equals(bareAddress)) {
return;
}
this.vCard = vCard;
this.vCardError = false;
getFragment().updateVCard(vCard);
}
@Override
public void onVCardFailed(String account, String bareAddress) {
if (!this.account.equals(account) || !this.bareAddress.equals(bareAddress)) {
return;
}
this.vCard = null;
this.vCardError = true;
Application.getInstance().onError(R.string.XMPP_EXCEPTION);
}
@Override @Override
public void onContactsChanged(Collection<BaseEntity> entities) { public void onContactsChanged(Collection<BaseEntity> entities) {
for (BaseEntity entity : entities) { for (BaseEntity entity : entities) {
if (entity.equals(account, bareAddress)) { if (entity.equals(account, bareAddress)) {
contactNameView.setText(RosterManager.getInstance().getBestContact(account, bareAddress).getName()); contactNameView.setText(RosterManager.getInstance().getBestContact(account, bareAddress).getName());
getFragment().updateContact(account, bareAddress);
break; break;
} }
} }
...@@ -220,7 +137,6 @@ public class ContactViewer extends ManagedActivity implements ...@@ -220,7 +137,6 @@ public class ContactViewer extends ManagedActivity implements
public void onAccountsChanged(Collection<String> accounts) { public void onAccountsChanged(Collection<String> accounts) {
if (accounts.contains(account)) { if (accounts.contains(account)) {
contactNameView.setText(RosterManager.getInstance().getBestContact(account, bareAddress).getName()); contactNameView.setText(RosterManager.getInstance().getBestContact(account, bareAddress).getName());
getFragment().updateContact(account, bareAddress);
} }
} }
......
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<LinearLayout
<com.github.ksoichiro.android.observablescrollview.ObservableScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scroll"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="wrap_content"
android:fillViewport="true" android:orientation="vertical"
android:scrollbars="vertical"
>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/xmpp_items"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:orientation="vertical" android:orientation="vertical"
android:paddingTop="@dimen/toolbar_height" />
android:paddingBottom="8dp"
>
<LinearLayout <LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/xmpp_items" android:id="@+id/contact_info_items"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:orientation="vertical" android:orientation="vertical"
/> />
<LinearLayout </LinearLayout>
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/contact_info_items"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
/>
</LinearLayout>
</com.github.ksoichiro.android.observablescrollview.ObservableScrollView>
...@@ -6,10 +6,26 @@ ...@@ -6,10 +6,26 @@
android:descendantFocusability="beforeDescendants" android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true" > android:focusableInTouchMode="true" >
<FrameLayout
android:id="@+id/scrollable_container" <com.github.ksoichiro.android.observablescrollview.ObservableScrollView
android:layout_width="match_parent" xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"/> android:id="@+id/scroll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:scrollbars="vertical"
>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="@dimen/toolbar_height"
android:paddingBottom="8dp"
android:id="@+id/scrollable_container"
/>
</com.github.ksoichiro.android.observablescrollview.ObservableScrollView>
<include layout="@layout/expandable_fake_toolbar" /> <include layout="@layout/expandable_fake_toolbar" />
......
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