Files
assistant-android/app/src/main/java/com/gh/gamecenter/adapter/ImagePagerAdapter.java
2016-11-14 19:01:27 +08:00

184 lines
6.5 KiB
Java

/*
* Copyright 2014 trinea.cn All right reserved. This software is the confidential and proprietary information of
* trinea.cn ("Confidential Information"). You shall not disclose such Confidential Information and shall use it only in
* accordance with the terms of the license agreement you entered into with trinea.cn.
*/
package com.gh.gamecenter.adapter;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import com.android.volley.Request;
import com.facebook.drawee.view.SimpleDraweeView;
import com.gh.base.AppController;
import com.gh.common.constant.Config;
import com.gh.common.util.DataUtils;
import com.gh.common.util.DeviceUtils;
import com.gh.common.util.GameUtils;
import com.gh.common.util.ImageUtils;
import com.gh.common.util.NewsUtils;
import com.gh.common.util.PackageUtils;
import com.gh.common.util.TokenUtils;
import com.gh.common.util.Utils;
import com.gh.gamecenter.GameDetailActivity;
import com.gh.gamecenter.NewsDetailActivity;
import com.gh.gamecenter.R;
import com.gh.gamecenter.SubjectActivity;
import com.gh.gamecenter.entity.SlideEntity;
import com.gh.gamecenter.volley.extended.StringExtendedRequest;
import org.json.JSONObject;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* ImagePagerAdapter
*
* @author <a href="http://www.trinea.cn" target="_blank">Trinea</a> 2014-2-23
*/
public class ImagePagerAdapter extends RecyclingPagerAdapter {
private Context context;
// private List<Integer> imageIdList;
private List<SlideEntity> slideList = null;
private int size;
private boolean isInfiniteLoop;
// private ImageIndicator indicator;
public ImagePagerAdapter(Context context, List<SlideEntity> slideList,
boolean isInfiniteLoop) {
this.context = context;
this.slideList = slideList;
this.size = getSize(slideList);
this.isInfiniteLoop = isInfiniteLoop;
}
@Override
public int getCount() {
// Infinite loop
return isInfiniteLoop ? Integer.MAX_VALUE : getSize(slideList);
}
public int getSize(List<SlideEntity> sourceList) {
return sourceList == null ? 0 : sourceList.size();
}
/**
* get really position
*
* @param position
* @return
*/
private int getPosition(int position) {
return isInfiniteLoop ? position % size : position;
}
@Override
public View getView(final int position, View view, ViewGroup container) {
if (view == null) {
view = new SimpleDraweeView(context);
}
final SlideEntity slideEntity = slideList.get(getPosition(position));
ImageUtils.getInstance().display(context.getResources(), (SimpleDraweeView) view,
slideEntity.getImage(), R.drawable.preload);
// indicator.setPosition(slideList.size(), getPosition(position));
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Map<String, Object> kv = new HashMap<>();
kv.put("名字", slideEntity.getName());
kv.put("位置", getPosition(position) + 1);
DataUtils.onEvent(context, "点击", "游戏-插件-滚动图", kv);
// 首页轮播图数据统计
statLunbotuData(getPosition(position));
if ("game".equals(slideEntity.getType())) {
String entrance = "(游戏-插件:滚动图["
+ slideEntity.getName()
+ "=" + slideEntity.getType()
+ "=" + (getPosition(position) + 1)
+ "])";
GameUtils.startGameDetailActivity(context, slideEntity.getLink(), entrance);
} else if ("news".equals(slideEntity.getType())) {
// 统计阅读量
NewsUtils.statNewsViews(slideEntity.getLink());
Intent intent = new Intent(context, NewsDetailActivity.class);
intent.putExtra("newsId", slideEntity.getLink());
intent.putExtra("entrance", "(游戏-插件:滚动图["
+ slideEntity.getName()
+ "=" + slideEntity.getType()
+ "=" + (getPosition(position) + 1)
+ "])");
context.startActivity(intent);
} else if ("column".equals(slideEntity.getType())) {
Intent intent = new Intent(context, SubjectActivity.class);
intent.putExtra("id", slideEntity.getLink());
intent.putExtra("name", slideEntity.getName());
context.startActivity(intent);
}
}
});
return view;
}
// 首页轮播图数据统计
public void statLunbotuData(int position) {
SlideEntity slideEntity = slideList.get(position);
Map<String, String> map = new HashMap<>();
map.put("location", String.valueOf(position + 1));
map.put("type", slideEntity.getType());
map.put("title", slideEntity.getName());
map.put("form", "click");
String version = PackageUtils.getVersion(context);
String user = DeviceUtils.getDeviceID(context);
String channel = (String) PackageUtils.getMetaData(context, context.getPackageName(), "TD_CHANNEL_ID");
map.put("version", version);
map.put("user", user);
map.put("device_id", TokenUtils.getDeviceId(context));
map.put("channel", channel);
String url = Config.DATA_HOST + "api/v1d0/log";
Map<String, String> params = new HashMap<>();
params.put("topic", "lunbotu");
params.put("source", "GH-ASSIST-Client");
params.put("time", String.valueOf(Utils.getTime(context)));
params.put("content", new JSONObject(map).toString());
StringExtendedRequest request = new StringExtendedRequest(Request.Method.POST, url, null, null);
request.setParams(params);
request.setShouldCache(false);
AppController.addToRequestQueue(request);
}
/**
* @return the isInfiniteLoop
*/
public boolean isInfiniteLoop() {
return isInfiniteLoop;
}
/**
* @param isInfiniteLoop
* the isInfiniteLoop to set
*/
public ImagePagerAdapter setInfiniteLoop(boolean isInfiniteLoop) {
this.isInfiniteLoop = isInfiniteLoop;
return this;
}
}