package com.partynetwork.iparty.discover;
import java.util.ArrayList;
import java.util.List;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;
import com.baidu.mobstat.StatService;
import com.partynetwork.dataprovider.DataProvider.IJsonResultListener;
import com.partynetwork.dataprovider.json.NetworkEntity;
import com.partynetwork.dataprovider.json.struct.Contacts_getContactsArrayRequest;
import com.partynetwork.dataprovider.json.struct.Contacts_getContactsArrayResponse;
import com.partynetwork.dataprovider.json.struct.Contacts_getGroupArrayRequest;
import com.partynetwork.dataprovider.json.struct.Contacts_getGroupArrayResponse;
import com.partynetwork.dataprovider.json.struct.Contacts_getRecommendArrayRequest;
import com.partynetwork.dataprovider.json.struct.Contacts_getRecommendArrayResponse;
import com.partynetwork.dataprovider.util.ActionUtil;
import com.partynetwork.dataprovider.util.DisplayUtil;
import com.partynetwork.dataprovider.util.Util;
import com.partynetwork.iparty.R;
import com.partynetwork.iparty.app.AppContext;
import com.partynetwork.iparty.app.common.BitmapManager;
import com.partynetwork.iparty.app.entities.CommonUser;
import com.partynetwork.iparty.contacts.ContactsListActivity;
import com.partynetwork.iparty.contacts.ContactsRecommendActivity;
import com.partynetwork.iparty.contacts.ContactsRecommendView;
import com.partynetwork.iparty.helper.IntentHelper;
import com.partynetwork.iparty.info.ContactsArrayResponseInfo;
import com.partynetwork.iparty.info.ContactsGroupArrayResponseInfo;
import com.partynetwork.iparty.info.ContactsInfo;
import com.partynetwork.myview.myimageview.CircularImage;
public class DiscoverContactsActivity extends Activity implements
IJsonResultListener, OnClickListener {
private Activity mActivity; // 上下文
private LinearLayout contactsContent; // 人脉分类集合
private LinearLayout recommendContent; // 推荐的人脉集合
private TextView recommendAll; // 推荐的查看全部
private LinearLayout groupContent; // 分组布局
private BitmapManager bitmapManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.contacts);
mActivity=this;
initView();
init();
}
/**
* 初始化方法
*/
private void init() {
bitmapManager = new BitmapManager();
refresh();
}
/**
* 实例化对象
*/
private void initView() {
contactsContent = (LinearLayout) mActivity
.findViewById(R.id.contacts_content_ll);
recommendContent = (LinearLayout) mActivity
.findViewById(R.id.recommend_content_ll);
recommendAll = (TextView) mActivity.findViewById(R.id.recommend_all_tv);
recommendAll.setOnClickListener(this);
groupContent = (LinearLayout) mActivity
.findViewById(R.id.group_content_ll);
mActivity.findViewById(R.id.menu_head_left).setOnClickListener(this);
mActivity.findViewById(R.id.menu_head_right).setOnClickListener(this);
}
/**
* 刷新
*/
public void refresh() {
AppContext context = AppContext.getInstance();
// 推荐的人
Contacts_getRecommendArrayRequest request = new Contacts_getRecommendArrayRequest();
request.setUserId(context.getLoginUid());
context.getmDataProvider().getJsonFromNetwork(request, this);
// 人脉
Contacts_getContactsArrayRequest contactsArrayRequest = new Contacts_getContactsArrayRequest();
contactsArrayRequest.setUserId(context.getLoginUid());
context.getmDataProvider().getJsonFromNetwork(contactsArrayRequest,
this);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.menu_head_left:
finish();
break;
case R.id.recommend_all_tv:
// 推荐人脉的查看更多
Intent intent = new Intent(mActivity,
ContactsRecommendActivity.class);
intent.putExtra(ContactsRecommendActivity.PAGE_TYPE,
ContactsRecommendActivity.RECOMMEND);
mActivity.startActivity(intent);
break;
case R.id.menu_head_right:
// 查找更多
Intent intent2 = new Intent(mActivity,
ContactsRecommendActivity.class);
intent2.putExtra(ContactsRecommendActivity.PAGE_TYPE,
ContactsRecommendActivity.SEARCH);
mActivity.startActivity(intent2);
break;
default:
break;
}
}
@Override
public void onNetworkRequest() {
}
@Override
public void onResultSuccess(NetworkEntity entity) {
// 推荐的人
if (entity.getRequest().getAction()
.equals(new Contacts_getRecommendArrayRequest().getAction())) {
Contacts_getRecommendArrayResponse recommendArrayResponse = (Contacts_getRecommendArrayResponse) entity
.getResponse();
ContactsInfo[] infoArray = recommendArrayResponse.getDetails();
addRecommendBodyList(infoArray);
}
// 人脉
if (entity.getRequest().getAction()
.equals(new Contacts_getContactsArrayRequest().getAction())) {
Contacts_getContactsArrayResponse contactsArrayResponse = (Contacts_getContactsArrayResponse) entity
.getResponse();
ContactsArrayResponseInfo[] responseInfoArray = contactsArrayResponse
.getDetails();
addContactsBodyList(responseInfoArray);
}
// 分组
if (entity.getRequest().getAction()
.equals(new Contacts_getGroupArrayRequest().getAction())) {
Contacts_getGroupArrayResponse groupArrayResponse = (Contacts_getGroupArrayResponse) entity
.getResponse();
ContactsGroupArrayResponseInfo[] groupArray = groupArrayResponse
.getDetails();
addGroupBodyList(groupArray);
}
}
@Override
public void onResultFail(String result) {
}
/**
* 添加推荐的人脉
*
* @param infoArray
*/
private void addRecommendBodyList(ContactsInfo[] infoArray) {
if (infoArray == null || infoArray.length == 0) {
return;
}
recommendContent.removeAllViews();
for (int i = 0; i < infoArray.length; i++) {
if (i > 2) {
return;
}
ContactsRecommendView view = new ContactsRecommendView(mActivity,
bitmapManager);
view.setInfo(infoArray[i]);
recommendContent.addView(view);
LinearLayout line = new LinearLayout(mActivity);
LayoutParams params = new LayoutParams(LayoutParams.FILL_PARENT, 1);
line.setLayoutParams(params);
line.setBackgroundColor(mActivity.getResources().getColor(
R.color.lightgray));
recommendContent.addView(line);
}
}
/** 人脉分组 */
private String[] contactsTitleArray = new String[] { "i好友名单", "你关注的人",
"粉丝", "去你妹名单" };
/**
* 添加人脉分类列表
*
* @param info
*/
private void addContactsBodyList(ContactsArrayResponseInfo[] infoArray) {
if (infoArray == null || infoArray.length == 0) {
return;
}
contactsContent.removeAllViews();
for (int i = 0; i < infoArray.length; i++) {
ContactsArrayResponseInfo responseInfo = null;
for (ContactsArrayResponseInfo info : infoArray) {
if (info.getUserType() == i) {
responseInfo = info;
}
}
if (responseInfo == null) {
return;
}
View view = LayoutInflater.from(mActivity).inflate(
R.layout.contacts_count_item, null);
TextView title = (TextView) view.findViewById(R.id.title);
if (contactsTitleArray[i] != null) {
title.setText(contactsTitleArray[i]);
}
TextView count = (TextView) view.findViewById(R.id.count);
count.setText("所有(" + responseInfo.getUserNumber() + ")");
count.setTag(i);
LinearLayout content = (LinearLayout) view.findViewById(R.id.body);
for (CommonUser cInfo : responseInfo.getUserArray()) {
CircularImage headImage = new CircularImage(mActivity);
LayoutParams params = new LayoutParams(DisplayUtil.dip2px(
mActivity, 50), DisplayUtil.dip2px(mActivity, 50));
params.rightMargin = 5;
headImage.setLayoutParams(params);
headImage.setTag(cInfo.getUserId());
bitmapManager.loadBitmap(cInfo.getUserHeadUrl(), headImage);
headImage.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
IntentHelper.goPersonalCenterActivity(mActivity,
Integer.parseInt(v.getTag().toString()));
}
});
content.addView(headImage);
}
/** 设置监听 */
count.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(mActivity,
ContactsListActivity.class);
intent.putExtra(ContactsListActivity.USER_TYPE,
Integer.parseInt(v.getTag().toString()));
mActivity.startActivity(intent);
}
});
contactsContent.addView(view);
}
}
/**
* 添加i好友分组列表
*
* @param groupArray
*/
private void addGroupBodyList(ContactsGroupArrayResponseInfo[] groupArray) {
if (groupArray == null || groupArray.length == 0) {
return;
}
groupContent.removeAllViews();
for (ContactsGroupArrayResponseInfo info : groupArray) {
PageTask task = new PageTask();
task.execute(info);
}
}
class PageTask extends
AsyncTask<ContactsGroupArrayResponseInfo, Void, View> {
int width = (int) (0.1 * Util.getScreenWidth(mActivity));
// 空隙
int margin = 2;
@Override
protected View doInBackground(ContactsGroupArrayResponseInfo... arg0) {
// 最外围的LinearLayout 纵向
LinearLayout parentLayout = new LinearLayout(mActivity);
parentLayout.setOrientation(LinearLayout.VERTICAL);
// 里面第二层的LinearLayout 横向
LinearLayout middleLayout1 = new LinearLayout(mActivity);
middleLayout1.setOrientation(LinearLayout.HORIZONTAL);
// 第一个大头像
ImageView head1 = new ImageView(mActivity);
LayoutParams smallHeadParams = new LayoutParams(2 * width + margin,
2 * width + margin);
head1.setLayoutParams(smallHeadParams);
head1.setScaleType(ScaleType.CENTER_CROP);
// 添加到第二层LinearLayout
middleLayout1.addView(head1);
// 第三层LinearLayout 纵向
LinearLayout middleLayout2 = new LinearLayout(mActivity);
middleLayout2.setOrientation(LinearLayout.VERTICAL);
LayoutParams params1 = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
params1.setMargins(margin, 0, 0, 0);
middleLayout2.setLayoutParams(params1);
// 第四层LinearLayout 横向
LinearLayout childLayout1 = new LinearLayout(mActivity);
childLayout1.setOrientation(LinearLayout.HORIZONTAL);
// 第二个头像
ImageView head2 = new ImageView(mActivity);
LayoutParams smallHeadParams1 = new LayoutParams(width, width);
head2.setLayoutParams(smallHeadParams1);
head2.setScaleType(ScaleType.CENTER_CROP);
// 第三个头像
ImageView head3 = new ImageView(mActivity);
LayoutParams smallHeadParams2 = new LayoutParams(width, width);
smallHeadParams2.leftMargin = margin;
head3.setLayoutParams(smallHeadParams2);
head3.setScaleType(ScaleType.CENTER_CROP);
// 添加到第四次LinearLayout
childLayout1.addView(head2);
childLayout1.addView(head3);
// 将弟四层LinearLayout添加到第三层LinearLayout
middleLayout2.addView(childLayout1);
// 下方的第四层LinearLayout
LinearLayout childLayout2 = new LinearLayout(mActivity);
childLayout2.setOrientation(LinearLayout.HORIZONTAL);
LayoutParams params2 = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
params2.setMargins(0, margin, 0, 0);
childLayout2.setLayoutParams(params2);
// 第四个头像
ImageView head4 = new ImageView(mActivity);
head4.setLayoutParams(smallHeadParams1);
head4.setScaleType(ScaleType.CENTER_CROP);
// 第五个头像
ImageView head5 = new ImageView(mActivity);
head5.setLayoutParams(smallHeadParams2);
head5.setScaleType(ScaleType.CENTER_CROP);
// 添加到下方第四层LinearLayout
childLayout2.addView(head4);
childLayout2.addView(head5);
// 将下方第四次LinearLayout添加到第三层中
middleLayout2.addView(childLayout2);
// 将第三层LinearLayout添加到第二层中
middleLayout1.addView(middleLayout2);
// 将第二层LinearLayout添加到最外层
parentLayout.addView(middleLayout1);
List<ImageView> headList = new ArrayList<ImageView>();
headList.add(head1);
headList.add(head2);
headList.add(head3);
headList.add(head4);
headList.add(head5);
if (arg0[0].getUserArray() != null) {
for (int i = 0; i < arg0[0].getUserArray().length; i++) {
bitmapManager.loadBitmap(
arg0[0].getUserArray()[i].getUserHeadUrl(),
headList.get(i));
}
}
// 组名称
TextView tv = new TextView(mActivity);
String name = arg0[0].getGroupName();
if (name == null || name.equals("")) {
name = "未命名分组";
}
tv.setText(name + "(" + arg0[0].getUserNumber() + ")");
// 最外层添加组名称
parentLayout.addView(tv);
Bundle bundle = new Bundle();
bundle.putInt("id", arg0[0].getGroupId());
bundle.putString("name", arg0[0].getGroupName());
parentLayout.setTag(bundle);
parentLayout.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Bundle bundle = (Bundle) v.getTag();
Intent intent = new Intent(mActivity,
ContactsListActivity.class);
intent.putExtra(ContactsListActivity.USER_TYPE,
ActionUtil.Contacts.GROUP);
intent.putExtra(ContactsListActivity.GROUP_ID,
bundle.getInt("id"));
intent.putExtra(ContactsListActivity.GROUP_NAME,
bundle.getString("name"));
mActivity.startActivity(intent);
}
});
return parentLayout;
}
@Override
protected void onPostExecute(View result) {
// 添加到分组布局中
groupContent.addView(result);
super.onPostExecute(result);
}
}
/**
* 刷新界面
*/
@Override
public void onResume() {
super.onResume();
refresh();
StatService.onPageStart(mActivity, "ContactsPage");
}
@Override
public void onPause() {
super.onPause();
StatService.onPageEnd(mActivity, "ContactsPage");
}
}