409 lines
16 KiB
Java
409 lines
16 KiB
Java
package com.gh.gamecenter.personal;
|
|
|
|
import android.content.Context;
|
|
import android.content.Intent;
|
|
import android.support.v4.util.ArrayMap;
|
|
import android.support.v7.widget.LinearLayoutManager;
|
|
import android.support.v7.widget.RecyclerView;
|
|
import android.view.LayoutInflater;
|
|
import android.view.View;
|
|
import android.view.ViewGroup;
|
|
import android.widget.RelativeLayout;
|
|
|
|
import com.android.volley.NoConnectionError;
|
|
import com.android.volley.Response;
|
|
import com.android.volley.TimeoutError;
|
|
import com.android.volley.VolleyError;
|
|
import com.gh.base.AppController;
|
|
import com.gh.common.constant.Config;
|
|
import com.gh.common.constant.Constants;
|
|
import com.gh.common.util.DataUtils;
|
|
import com.gh.common.util.DisplayUtils;
|
|
import com.gh.common.util.GameViewUtils;
|
|
import com.gh.common.util.ImageUtils;
|
|
import com.gh.common.util.PlatformUtils;
|
|
import com.gh.common.util.TimestampUtils;
|
|
import com.gh.common.util.TrafficUtils;
|
|
import com.gh.common.util.Utils;
|
|
import com.gh.common.view.CardLinearLayout;
|
|
import com.gh.gamecenter.GameDetailsActivity;
|
|
import com.gh.gamecenter.R;
|
|
import com.gh.gamecenter.adapter.viewholder.FooterViewHolder;
|
|
import com.gh.gamecenter.adapter.viewholder.GameNormalViewHolder;
|
|
import com.gh.gamecenter.adapter.viewholder.ReuseViewHolder;
|
|
import com.gh.gamecenter.db.info.ConcernInfo;
|
|
import com.gh.gamecenter.db.info.GameInfo;
|
|
import com.gh.gamecenter.entity.ApkEntity;
|
|
import com.gh.gamecenter.entity.GameEntity;
|
|
import com.gh.gamecenter.entity.MyGameInfo;
|
|
import com.gh.gamecenter.manager.ConcernManager;
|
|
import com.gh.gamecenter.manager.GameManager;
|
|
import com.gh.gamecenter.manager.PackageManager;
|
|
import com.gh.gamecenter.volley.extended.JsonObjectExtendedRequest;
|
|
import com.google.gson.Gson;
|
|
|
|
import org.json.JSONObject;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.Collections;
|
|
import java.util.Comparator;
|
|
import java.util.HashMap;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
|
|
/**
|
|
* Created by LGT on 2016/8/12.
|
|
*/
|
|
public class InstallFragmentAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
|
|
|
|
private Context context;
|
|
|
|
private RecyclerView fm_install_rv_show;
|
|
|
|
private ArrayList<GameEntity> gameList;
|
|
private ArrayList<MyGameInfo> sortedList;
|
|
|
|
private boolean isRemove;
|
|
|
|
public InstallFragmentAdapter(Context context, RecyclerView recyclerView) {
|
|
this.context = context;
|
|
|
|
fm_install_rv_show = recyclerView;
|
|
|
|
gameList = new ArrayList<>();
|
|
sortedList = new ArrayList<>();
|
|
|
|
isRemove = false;
|
|
|
|
new Thread(new Runnable() {
|
|
@Override
|
|
public void run() {
|
|
init();
|
|
if (sortedList.isEmpty()) {
|
|
|
|
} else {
|
|
List<String> ids = new ArrayList<>();
|
|
for (MyGameInfo info : sortedList) {
|
|
if (!ids.contains(info.getGame_id())) {
|
|
ids.add(info.getGame_id());
|
|
}
|
|
}
|
|
initGameList(ids);
|
|
}
|
|
}
|
|
}).start();
|
|
}
|
|
|
|
//初始化
|
|
private void init() {
|
|
|
|
ArrayList<MyGameInfo> signatureList = new ArrayList<>();
|
|
ArrayList<MyGameInfo> unsignatureList = new ArrayList<>();
|
|
ArrayList<MyGameInfo> noopenList = new ArrayList<>();
|
|
ArrayList<MyGameInfo> oftenuseList = new ArrayList<>();
|
|
|
|
PackageManager pManager = new PackageManager(context);
|
|
TrafficUtils tUtils = TrafficUtils.getInstance(context);
|
|
|
|
ConcernManager cManager = new ConcernManager(context);
|
|
List<ConcernInfo> runnableGame = cManager.getInstalledGame();
|
|
for (ConcernInfo concernEntity : runnableGame) {
|
|
for (Map.Entry<String, Boolean> entry : concernEntity.getPackageNames().entrySet()) {
|
|
if (entry.getValue()) {
|
|
MyGameInfo info = new MyGameInfo();
|
|
info.setGame_id(concernEntity.getId());
|
|
info.setPackage_name(entry.getKey());
|
|
info.setTraffic(tUtils.getTraffice(entry.getKey()));
|
|
info.setSignature(pManager.isSignature(entry.getKey()));
|
|
info.setInstalledTime(pManager.getInstalledTime(entry.getKey()));
|
|
if (info.isSignature()) {
|
|
signatureList.add(info);
|
|
} else {
|
|
unsignatureList.add(info);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
for (MyGameInfo info : signatureList) {
|
|
if (info.getTraffic() == 0) {
|
|
noopenList.add(info);
|
|
} else {
|
|
oftenuseList.add(info);
|
|
}
|
|
}
|
|
|
|
Comparator<MyGameInfo> comparator = new Comparator<MyGameInfo>() {
|
|
@Override
|
|
public int compare(MyGameInfo lhs, MyGameInfo rhs) {
|
|
if (rhs.getInstalledTime() > lhs.getInstalledTime()) {
|
|
return 1;
|
|
} else if (rhs.getInstalledTime() < lhs.getInstalledTime()) {
|
|
return -1;
|
|
} else {
|
|
return 0;
|
|
}
|
|
}
|
|
};
|
|
|
|
Collections.sort(noopenList, comparator);
|
|
|
|
comparator = new Comparator<MyGameInfo>() {
|
|
@Override
|
|
public int compare(MyGameInfo lhs, MyGameInfo rhs) {
|
|
if (rhs.getTraffic() > lhs.getTraffic()) {
|
|
return 1;
|
|
} else if (rhs.getTraffic() < lhs.getTraffic()) {
|
|
return -1;
|
|
} else {
|
|
return 0;
|
|
}
|
|
}
|
|
};
|
|
|
|
Collections.sort(oftenuseList, comparator);
|
|
|
|
Collections.sort(unsignatureList, comparator);
|
|
|
|
sortedList.addAll(noopenList);
|
|
sortedList.addAll(oftenuseList);
|
|
sortedList.addAll(unsignatureList);
|
|
|
|
Utils.log("sortedList size = " + sortedList.size());
|
|
}
|
|
|
|
private int count;
|
|
|
|
//获取游戏简介
|
|
private void initGameList(List<String> ids) {
|
|
final List<JSONObject> result = new ArrayList<>();
|
|
final int size = ids.size();
|
|
count = 0;
|
|
for (int i = 0; i < ids.size(); i++) {
|
|
JsonObjectExtendedRequest objectRequest = new JsonObjectExtendedRequest(
|
|
TimestampUtils.addTimestamp(Config.HOST + "v1d45/game/"
|
|
+ ids.get(i) + "/digest", Constants.GAME_CD),
|
|
new Response.Listener<JSONObject>() {
|
|
@Override
|
|
public void onResponse(JSONObject response) {
|
|
result.add(response);
|
|
count++;
|
|
if (count == size) {
|
|
processingData(result);
|
|
}
|
|
}
|
|
},
|
|
new Response.ErrorListener() {
|
|
@Override
|
|
public void onErrorResponse(VolleyError error) {
|
|
count++;
|
|
if (count == size) {
|
|
processingData(result);
|
|
}
|
|
// 无网络连接和访问超时
|
|
if (error.getClass().equals(NoConnectionError.class)
|
|
|| error.getClass().equals(TimeoutError.class)) {
|
|
|
|
}
|
|
}
|
|
});
|
|
AppController.addToRequestQueue(objectRequest,
|
|
PersonalFragmentAdapter.class);
|
|
}
|
|
}
|
|
|
|
private void processingData(List<JSONObject> data) {
|
|
Gson gson = new Gson();
|
|
List<GameEntity> gameList = new ArrayList<>();
|
|
for (int i = 0, size = data.size(); i < size; i++) {
|
|
gameList.add(gson.fromJson(data.get(i).toString(), GameEntity.class));
|
|
}
|
|
if (gameList.size() != 0) {
|
|
for (int i = 0, size = sortedList.size(); i < size; i++) {
|
|
String id = sortedList.get(i).getGame_id();
|
|
for (GameEntity entity : gameList) {
|
|
if (entity.getId().equals(id)) {
|
|
GameEntity newEntity = entity.clone();
|
|
if (newEntity != null && newEntity.getApk().size() > 1) {
|
|
for (ApkEntity apkEntity : newEntity.getApk()) {
|
|
if (sortedList.get(i).getPackage_name().equals(apkEntity.getPackageName())) {
|
|
ArrayList<ApkEntity> list = new ArrayList<>();
|
|
list.add(apkEntity);
|
|
newEntity.setApk(list);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
this.gameList.add(newEntity);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
GameManager manager = new GameManager(context);
|
|
for (GameEntity entity : this.gameList) {
|
|
for (ApkEntity apkEntity : entity.getApk()) {
|
|
manager.addOrUpdate(new GameInfo(
|
|
apkEntity.getPackageName(), entity.getId(), entity.getName()));
|
|
}
|
|
}
|
|
}
|
|
|
|
if (this.gameList.size() != 0) {
|
|
ArrayMap<String, ArrayList<GameEntity>> map = new ArrayMap<>();
|
|
ArrayList<String> keys = new ArrayList<>();
|
|
ArrayList<GameEntity> list;
|
|
for (GameEntity gameEntity : this.gameList) {
|
|
list = map.get(gameEntity.getId());
|
|
if (list == null) {
|
|
list = new ArrayList<>();
|
|
map.put(gameEntity.getId(), list);
|
|
keys.add(gameEntity.getId());
|
|
}
|
|
list.add(gameEntity);
|
|
}
|
|
list = new ArrayList<>();
|
|
for (String key : keys) {
|
|
list.addAll(map.get(key));
|
|
}
|
|
this.gameList = list;
|
|
notifyItemRangeInserted(2, this.gameList.size());
|
|
isRemove = true;
|
|
notifyItemRemoved(getItemCount());
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
|
|
if (viewType == 0) {
|
|
RelativeLayout relativeLayout = new RelativeLayout(context);
|
|
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(
|
|
ViewGroup.LayoutParams.MATCH_PARENT, DisplayUtils.dip2px(context, 229)
|
|
- DisplayUtils.getInternalDimensionSize(context.getResources(), "status_bar_height"));
|
|
relativeLayout.setLayoutParams(params);
|
|
return new ReuseViewHolder(relativeLayout);
|
|
} else if (viewType == 1) {
|
|
RelativeLayout relativeLayout = new RelativeLayout(context);
|
|
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(
|
|
ViewGroup.LayoutParams.MATCH_PARENT, DisplayUtils.dip2px(context, 30)
|
|
+ DisplayUtils.getInternalDimensionSize(context.getResources(), "status_bar_height"));
|
|
relativeLayout.setLayoutParams(params);
|
|
return new ReuseViewHolder(relativeLayout);
|
|
} else if (gameList.size() > 0 && viewType > 1 && viewType <= 1 + gameList.size()) {
|
|
View view = LayoutInflater.from(parent.getContext()).inflate(
|
|
R.layout.game_normal_item, parent, false);
|
|
return new GameNormalViewHolder(view);
|
|
} else if (isRemove) {
|
|
RelativeLayout relativeLayout = new RelativeLayout(context);
|
|
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(
|
|
ViewGroup.LayoutParams.MATCH_PARENT, 0);
|
|
relativeLayout.setLayoutParams(params);
|
|
return new ReuseViewHolder(relativeLayout);
|
|
} else {
|
|
View view = LayoutInflater.from(parent.getContext()).inflate(
|
|
R.layout.refresh_footerview, parent, false);
|
|
return new FooterViewHolder(view);
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
|
|
if (holder instanceof GameNormalViewHolder) {
|
|
initGameNormal((GameNormalViewHolder) holder, gameList.get(position - 2), position - 2);
|
|
} else if (holder instanceof ReuseViewHolder && position == getItemCount() - 1) {
|
|
ReuseViewHolder viewHolder = (ReuseViewHolder) holder;
|
|
|
|
int height = 0;
|
|
LinearLayoutManager linearLayoutManager = (LinearLayoutManager) fm_install_rv_show.getLayoutManager();
|
|
if (linearLayoutManager.findFirstVisibleItemPosition() < 1) {
|
|
for (int i = 0, size = fm_install_rv_show.getChildCount(); i < size; i++) {
|
|
height += fm_install_rv_show.getChildAt(i).getMeasuredHeight();
|
|
}
|
|
height = fm_install_rv_show.getHeight() + DisplayUtils.dip2px(context, 229)
|
|
- DisplayUtils.getInternalDimensionSize(context.getResources(), "status_bar_height") - height;
|
|
if (height < 0) {
|
|
height = 0;
|
|
}
|
|
Utils.log("height = " + height);
|
|
}
|
|
|
|
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(
|
|
ViewGroup.LayoutParams.MATCH_PARENT, height);
|
|
viewHolder.itemView.setLayoutParams(params);
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public int getItemCount() {
|
|
return 2 + gameList.size() + 1;
|
|
}
|
|
|
|
@Override
|
|
public int getItemViewType(int position) {
|
|
return position;
|
|
}
|
|
|
|
private void initGameNormal(GameNormalViewHolder holder, final GameEntity entity, final int i) {
|
|
|
|
// 第一个
|
|
if (i == 0) {
|
|
((CardLinearLayout) holder.itemView).setmTop(DisplayUtils.dip2px(context, 8));
|
|
} else {
|
|
((CardLinearLayout) holder.itemView).setmTop(0);
|
|
}
|
|
|
|
// 最后一个
|
|
if (i == gameList.size() - 1) {
|
|
((CardLinearLayout) holder.itemView).setBottom(true);
|
|
} else {
|
|
((CardLinearLayout) holder.itemView).setBottom(false);
|
|
}
|
|
|
|
ImageUtils.getInstance(context).display(entity.getIcon(), holder.gameThumb);
|
|
|
|
if (entity.getApk() == null || entity.getApk().isEmpty()) {
|
|
holder.gameDes.setText(entity.getBrief());
|
|
holder.gameNameAndSize.setText(entity.getName());
|
|
} else {
|
|
holder.gameDes.setText(String.format("%s %s", entity.getApk().get(0).getSize(), entity.getBrief()));
|
|
holder.gameNameAndSize.setText(String.format("%s - %s", entity.getName(),
|
|
PlatformUtils.getInstance(context).getPlatformName(entity.getApk().get(0).getPlatform())));
|
|
}
|
|
GameViewUtils.setLabelList(context, holder.labelList, entity.getTag());
|
|
|
|
holder.itemView.setOnClickListener(new View.OnClickListener() {
|
|
@Override
|
|
public void onClick(View v) {
|
|
|
|
Map<String, Object> kv = new HashMap<>();
|
|
kv.put("名字", entity.getName());
|
|
kv.put("位置", i + 1);
|
|
DataUtils.onEvent(context, "点击", "我的光环-已安装", kv);
|
|
|
|
// if (entity.getPluginPlatform() != null) {
|
|
Intent intent = new Intent(context, GameDetailsActivity.class);
|
|
intent.putExtra("gameId", entity.getId());
|
|
intent.putExtra("entrance", "我的光环-已安装");
|
|
context.startActivity(intent);
|
|
// } else {
|
|
// AppController.put("GameEntity", entity);
|
|
// Intent intent = new Intent(context, GameDetailsActivity.class);
|
|
// intent.putExtra("entrance", "我的光环-已安装");
|
|
// intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
|
// context.startActivity(intent);
|
|
// }
|
|
}
|
|
});
|
|
|
|
// DownloadItemUtils.setOnClickListener(context, holder.downloadBtn,
|
|
// holder.download_speed, holder.download_percentage, entity, i,
|
|
// this,
|
|
// statusMap, platformMap, "我的光环-已安装",
|
|
// dismissEntity, "我的光环-已安装" + ":" + entity.getName());
|
|
|
|
// DownloadItemUtils.updateItem(context, holder.labelList,
|
|
// holder.game_progressbar, holder.game_ll_info, holder.download_speed,
|
|
// holder.download_percentage, holder.downloadBtn, entity, platformMap,
|
|
// statusMap, entity.getPluginPlatform() != null);
|
|
}
|
|
}
|