游戏模块,整理完成
This commit is contained in:
290
app/src/main/java/com/gh/gamecenter/adapter/PluginAdapter.java
Normal file
290
app/src/main/java/com/gh/gamecenter/adapter/PluginAdapter.java
Normal file
@ -0,0 +1,290 @@
|
||||
package com.gh.gamecenter.adapter;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.support.v4.util.ArrayMap;
|
||||
import android.support.v7.widget.RecyclerView;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.LinearLayout;
|
||||
|
||||
import com.android.volley.Response;
|
||||
import com.android.volley.VolleyError;
|
||||
import com.gc.materialdesign.views.ProgressBarCircularIndeterminate;
|
||||
import com.gh.base.AppController;
|
||||
import com.gh.common.constant.Config;
|
||||
import com.gh.common.constant.Constants;
|
||||
import com.gh.common.util.DownloadItemUtils;
|
||||
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.download.DownloadEntry;
|
||||
import com.gh.gamecenter.GameDetailsActivity;
|
||||
import com.gh.gamecenter.R;
|
||||
import com.gh.gamecenter.adapter.viewholder.GameNormalViewHolder;
|
||||
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.DismissEntity;
|
||||
import com.gh.gamecenter.entity.GameEntity;
|
||||
import com.gh.gamecenter.game.GameFragment;
|
||||
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.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.LinkedBlockingQueue;
|
||||
|
||||
/**
|
||||
* Created by LGT on 2016/7/6.
|
||||
*/
|
||||
public class PluginAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
|
||||
|
||||
private Context context;
|
||||
|
||||
private RecyclerView plugin_list;
|
||||
private ProgressBarCircularIndeterminate plugin_pb_loading;
|
||||
private LinearLayout reuse_no_connection;
|
||||
|
||||
private List<GameEntity> pluginList;
|
||||
|
||||
private ArrayMap<String, Integer> locationMap;
|
||||
private ArrayMap<String, LinkedBlockingQueue<String>> platformMap;
|
||||
private ArrayMap<String, ArrayMap<String, DownloadEntry>> gameMap;
|
||||
private ArrayMap<String, String> nameMap;
|
||||
private ArrayMap<String, String> statusMap;
|
||||
private DismissEntity dismissEntity;
|
||||
|
||||
public PluginAdapter(Context context,
|
||||
RecyclerView recyclerView,
|
||||
ProgressBarCircularIndeterminate pbLoading,
|
||||
LinearLayout linearLayout) {
|
||||
|
||||
this.context = context;
|
||||
plugin_list = recyclerView;
|
||||
plugin_pb_loading = pbLoading;
|
||||
reuse_no_connection = linearLayout;
|
||||
|
||||
pluginList = new ArrayList<GameEntity>();
|
||||
|
||||
locationMap = new ArrayMap<String, Integer>();
|
||||
platformMap = new ArrayMap<String, LinkedBlockingQueue<String>>();
|
||||
gameMap = new ArrayMap<String, ArrayMap<String, DownloadEntry>>();
|
||||
nameMap = new ArrayMap<String, String>();
|
||||
statusMap = new ArrayMap<String, String>();
|
||||
dismissEntity = new DismissEntity(false);
|
||||
|
||||
// 黄壮华 添加 初始化游戏状态 修改2015/8/21
|
||||
DownloadItemUtils.initializeGameMap(context, gameMap);
|
||||
|
||||
new Thread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
init();
|
||||
}
|
||||
}).start();
|
||||
}
|
||||
|
||||
private int count;
|
||||
|
||||
//检查可以插件化的游戏
|
||||
private void init() {
|
||||
ConcernManager concernManager = new ConcernManager(context);
|
||||
List<ConcernInfo> infos = concernManager.getInstalledGame();
|
||||
final int size = infos.size();
|
||||
final List<GameEntity> list = new ArrayList<GameEntity>();
|
||||
count = 0;
|
||||
for (ConcernInfo info : infos) {
|
||||
JsonObjectExtendedRequest request = new JsonObjectExtendedRequest(
|
||||
TimestampUtils.addTimestamp(Config.HOST + "v1d45/game/" + info.getId() + "/digest", Constants.GAME_CD),
|
||||
new Response.Listener<JSONObject>() {
|
||||
@Override
|
||||
public void onResponse(JSONObject response) {
|
||||
Gson gson = new Gson();
|
||||
GameEntity gameEntity = gson.fromJson(response.toString(), GameEntity.class);
|
||||
list.add(gameEntity);
|
||||
count++;
|
||||
if (count == size) {
|
||||
processingData(list);
|
||||
}
|
||||
}
|
||||
}, new Response.ErrorListener() {
|
||||
@Override
|
||||
public void onErrorResponse(VolleyError error) {
|
||||
count++;
|
||||
if (count == size) {
|
||||
if (list.isEmpty()) {
|
||||
plugin_list.setVisibility(View.GONE);
|
||||
plugin_pb_loading.setVisibility(View.GONE);
|
||||
reuse_no_connection.setVisibility(View.VISIBLE);
|
||||
} else {
|
||||
processingData(list);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
AppController.addToRequestQueue(request, GameFragment.class);
|
||||
}
|
||||
}
|
||||
|
||||
private void processingData(List<GameEntity> list) {
|
||||
if (plugin_pb_loading != null && plugin_pb_loading.getVisibility() == View.VISIBLE) {
|
||||
plugin_pb_loading.setVisibility(View.GONE);
|
||||
}
|
||||
if (list == null || list.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
ConcernManager concernManager = new ConcernManager(context);
|
||||
PackageManager mPackageManager = new PackageManager(context);
|
||||
List<ConcernInfo> infos = concernManager.getInstalledGame();
|
||||
HashMap<String, Boolean> map;
|
||||
GameEntity gameEntity;
|
||||
for (ConcernInfo info : infos) {
|
||||
map = info.getPackageNames();
|
||||
for (int i = 0, size = list.size(); i < size; i++) {
|
||||
gameEntity = list.get(i);
|
||||
if (gameEntity.getId().equals(info.getId())
|
||||
&& gameEntity.getTag() != null && gameEntity.getTag().size() != 0
|
||||
&& gameEntity.getApk() != null) {
|
||||
for (String key : map.keySet()) {
|
||||
if (map.get(key)) {
|
||||
if (!mPackageManager.isSignature(key)) {
|
||||
for (ApkEntity apkEntity : gameEntity.getApk()) {
|
||||
if (apkEntity.getPackageName().equals(key)) {
|
||||
GameEntity entity = gameEntity.clone();
|
||||
entity.setPluginPlatform(
|
||||
PlatformUtils.getInstance(context).getPlatformName(apkEntity.getPlatform()));
|
||||
ArrayList<ApkEntity> apkList = new ArrayList<ApkEntity>();
|
||||
apkList.add(apkEntity);
|
||||
entity.setApk(apkList);
|
||||
pluginList.add(entity);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!pluginList.isEmpty()) {
|
||||
GameManager manager = new GameManager(context);
|
||||
for (GameEntity gEntity : pluginList) {
|
||||
gEntity.setEntryMap(gameMap.get(gEntity.getName()));
|
||||
for (ApkEntity aEntity : gEntity.getApk()) {
|
||||
manager.addOrUpdate(new GameInfo(aEntity.getPackageName(),
|
||||
gEntity.getId(), gEntity.getName()));
|
||||
}
|
||||
}
|
||||
notifyItemRangeInserted(0, pluginList.size());
|
||||
initLocationMap();
|
||||
}
|
||||
}
|
||||
|
||||
private void initLocationMap() {
|
||||
for (int i = 0; i < pluginList.size(); i++) {
|
||||
GameEntity gameEntity = pluginList.get(i);
|
||||
locationMap.put(gameEntity.getName() + " - " + gameEntity.getPluginPlatform(), i);
|
||||
for (ApkEntity apkEntity : gameEntity.getApk()) {
|
||||
nameMap.put(apkEntity.getPackageName(), gameEntity.getName() + " - " + gameEntity.getPluginPlatform());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
|
||||
View view = LayoutInflater.from(parent.getContext()).inflate(
|
||||
R.layout.home_game_normal_item, parent, false);
|
||||
return new GameNormalViewHolder(view);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
|
||||
if (holder instanceof GameNormalViewHolder) {
|
||||
GameNormalViewHolder viewHolder = (GameNormalViewHolder) holder;
|
||||
|
||||
final GameEntity entity = pluginList.get(position);
|
||||
|
||||
viewHolder.home1_game_order.setVisibility(View.GONE);
|
||||
|
||||
ImageUtils.getInstance(context).display(entity.getIcon(), viewHolder.gameThumb);
|
||||
if (entity.getPluginPlatform() != null) {
|
||||
viewHolder.gameNameAndSize.setText(entity.getName() + " - " + entity.getPluginPlatform());
|
||||
} else {
|
||||
viewHolder.gameNameAndSize.setText(entity.getName());
|
||||
}
|
||||
if (entity.getApk() == null || entity.getApk().isEmpty()) {
|
||||
viewHolder.gameDes.setText(entity.getBrief());
|
||||
} else {
|
||||
viewHolder.gameDes.setText(entity.getApk().get(0).getSize() + " | " + entity.getBrief());
|
||||
}
|
||||
GameViewUtils.setLabelList(context, viewHolder.labelList, entity.getTag());
|
||||
|
||||
holder.itemView.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
Intent intent = new Intent(context, GameDetailsActivity.class);
|
||||
intent.putExtra("gameId", entity.getId());
|
||||
intent.putExtra("entrance", "游戏-插件");
|
||||
context.startActivity(intent);
|
||||
}
|
||||
});
|
||||
|
||||
final int i = position;
|
||||
|
||||
DownloadItemUtils.setOnClickListener(context, viewHolder.downloadBtn,
|
||||
viewHolder.download_speed, viewHolder.download_percentage, entity, i,
|
||||
PluginAdapter.this,
|
||||
statusMap, platformMap, "游戏-插件",
|
||||
dismissEntity, "主页:" + entity.getName());
|
||||
|
||||
DownloadItemUtils.updateItem(context, viewHolder.labelList,
|
||||
viewHolder.game_progressbar, viewHolder.game_ll_info, viewHolder.download_speed,
|
||||
viewHolder.download_percentage, viewHolder.downloadBtn, entity, platformMap,
|
||||
statusMap);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getItemCount() {
|
||||
return pluginList.size();
|
||||
}
|
||||
|
||||
public List<GameEntity> getPluginList() {
|
||||
return pluginList;
|
||||
}
|
||||
|
||||
public ArrayMap<String, Integer> getLocationMap() {
|
||||
return locationMap;
|
||||
}
|
||||
|
||||
public ArrayMap<String, LinkedBlockingQueue<String>> getPlatformMap() {
|
||||
return platformMap;
|
||||
}
|
||||
|
||||
public ArrayMap<String, ArrayMap<String, DownloadEntry>> getGameMap() {
|
||||
return gameMap;
|
||||
}
|
||||
|
||||
public ArrayMap<String, String> getNameMap() {
|
||||
return nameMap;
|
||||
}
|
||||
|
||||
public ArrayMap<String, String> getStatusMap() {
|
||||
return statusMap;
|
||||
}
|
||||
|
||||
public DismissEntity getDismissEntity() {
|
||||
return dismissEntity;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user