311 lines
9.1 KiB
Java
311 lines
9.1 KiB
Java
package com.gh.gamecenter.game;
|
|
|
|
import android.os.Bundle;
|
|
import android.os.Handler;
|
|
import android.os.Message;
|
|
import android.support.annotation.Nullable;
|
|
import android.support.v4.app.Fragment;
|
|
import android.support.v4.util.ArrayMap;
|
|
import android.support.v4.widget.SwipeRefreshLayout;
|
|
import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener;
|
|
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.LinearLayout;
|
|
|
|
import com.gh.base.AppController;
|
|
import com.gh.common.constant.Constants;
|
|
import com.gh.common.util.DownloadItemUtils;
|
|
import com.gh.common.util.Utils;
|
|
import com.gh.common.view.VerticalItemDecoration;
|
|
import com.gh.download.DataWatcher;
|
|
import com.gh.download.DownloadEntry;
|
|
import com.gh.download.DownloadManager;
|
|
import com.gh.gamecenter.R;
|
|
import com.gh.gamecenter.entity.ApkEntity;
|
|
import com.gh.gamecenter.entity.DismissEntity;
|
|
import com.gh.gamecenter.entity.GameEntity;
|
|
import com.gh.gamecenter.eventbus.EBNetworkState;
|
|
import com.gh.gamecenter.eventbus.EBPWDismiss;
|
|
import com.gh.gamecenter.eventbus.EBPackage;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.HashMap;
|
|
import java.util.List;
|
|
import java.util.concurrent.LinkedBlockingQueue;
|
|
|
|
import de.greenrobot.event.EventBus;
|
|
|
|
/**
|
|
*
|
|
* @author 温冠超
|
|
* @email 294299195@qq.com
|
|
* @date 2015-8-10 modified 2015-8-12 新游界面
|
|
*/
|
|
public class Game1Fragment extends Fragment implements OnRefreshListener {
|
|
|
|
private View view;
|
|
private RecyclerView recyclerview;
|
|
private SwipeRefreshLayout game_swipe_refresh;
|
|
private Game1FragmentAdapter adapter;
|
|
private LinearLayoutManager layoutManager;
|
|
private LinearLayout reuse_no_connection;
|
|
|
|
// 黄壮华 添加 记录信息的map 修改2015/8/15
|
|
private ArrayMap<String, Integer> locationMap;
|
|
private ArrayMap<String, ArrayMap<String, DownloadEntry>> gameMap;
|
|
private ArrayMap<String, LinkedBlockingQueue<String>> platformMap;
|
|
private ArrayMap<String, String> nameMap;
|
|
|
|
private boolean isEverpause = false;
|
|
private boolean isDestroy = false;
|
|
|
|
private ArrayMap<String, String> statusMap;
|
|
|
|
private DismissEntity dismissEntity;
|
|
|
|
private Handler handler = new Handler() {
|
|
@Override
|
|
public void handleMessage(Message msg) {
|
|
if (!isDestroy) {
|
|
if (msg.what == Constants.DOWNLOAD_ROLL) {
|
|
String name = (String) msg.obj;
|
|
if (platformMap != null) {
|
|
LinkedBlockingQueue<String> queue = platformMap
|
|
.get(name);
|
|
if (queue.size() > 1) {
|
|
queue.offer(queue.poll());
|
|
Message msg2 = Message.obtain();
|
|
msg2.obj = name;
|
|
msg2.what = Constants.DOWNLOAD_ROLL;
|
|
sendMessageDelayed(msg2, 3000);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
};
|
|
|
|
// 黄壮华 添加观察者 修改2015/8/15
|
|
private DataWatcher dataWatcher = new DataWatcher() {
|
|
|
|
@Override
|
|
public void onDataChanged(
|
|
HashMap<String, DownloadEntry> downloadingEntries) {
|
|
if (!game_swipe_refresh.isRefreshing()) {
|
|
for (java.util.Map.Entry<String, DownloadEntry> entry : downloadingEntries
|
|
.entrySet()) {
|
|
DownloadEntry downloadEntry = entry.getValue();
|
|
String name = downloadEntry.getName();
|
|
Integer location = locationMap.get(name);
|
|
if (location != null) {
|
|
int index = location.intValue();
|
|
|
|
GameEntity detailedEntity = adapter.getList()
|
|
.get(index);
|
|
|
|
if (detailedEntity != null) {
|
|
DownloadItemUtils.processDate(detailedEntity,
|
|
downloadEntry, platformMap, handler,
|
|
adapter, index, statusMap);
|
|
}
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
};
|
|
|
|
@Override
|
|
public void onCreate(@Nullable Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
view = View.inflate(getActivity(), R.layout.plugin1_fragment, null);
|
|
|
|
dismissEntity = new DismissEntity(false);
|
|
|
|
statusMap = new ArrayMap<String, String>();
|
|
locationMap = new ArrayMap<String, Integer>();
|
|
gameMap = new ArrayMap<String, ArrayMap<String, DownloadEntry>>();
|
|
platformMap = new ArrayMap<String, LinkedBlockingQueue<String>>();
|
|
nameMap = new ArrayMap<String, String>();
|
|
|
|
game_swipe_refresh = (SwipeRefreshLayout) view
|
|
.findViewById(R.id.game_swipe_refresh);
|
|
game_swipe_refresh.setColorSchemeResources(R.color.theme_colors);
|
|
game_swipe_refresh.setOnRefreshListener(this);
|
|
|
|
recyclerview = (RecyclerView) view.findViewById(R.id.game_list);
|
|
recyclerview.setHasFixedSize(true);
|
|
layoutManager = new LinearLayoutManager(getActivity());
|
|
recyclerview.setLayoutManager(layoutManager);
|
|
|
|
// 黄壮华 添加 初始化游戏状态 修改2015/8/21
|
|
DownloadItemUtils.initializeGameMap(getActivity(), gameMap);
|
|
|
|
reuse_no_connection = (LinearLayout) view
|
|
.findViewById(R.id.reuse_no_connection);
|
|
reuse_no_connection.setOnClickListener(new View.OnClickListener() {
|
|
@Override
|
|
public void onClick(View v) {
|
|
game_swipe_refresh.setRefreshing(true);
|
|
recyclerview.setVisibility(View.VISIBLE);
|
|
reuse_no_connection.setVisibility(View.GONE);
|
|
handler.postDelayed(runnable, 1000);
|
|
}
|
|
});
|
|
|
|
// 黄壮华 传递引用 修改2015/8/15
|
|
adapter = new Game1FragmentAdapter(Game1Fragment.this, locationMap,
|
|
gameMap, platformMap, nameMap, recyclerview,
|
|
game_swipe_refresh, reuse_no_connection,
|
|
statusMap, dismissEntity);
|
|
recyclerview.setAdapter(adapter);
|
|
recyclerview.setOnScrollListener(new RecyclerView.OnScrollListener() {
|
|
|
|
@Override
|
|
public void onScrollStateChanged(RecyclerView recyclerView,
|
|
int newState) {
|
|
super.onScrollStateChanged(recyclerView, newState);
|
|
if (!isDestroy
|
|
&& newState == RecyclerView.SCROLL_STATE_IDLE
|
|
&& layoutManager.findLastVisibleItemPosition() == adapter
|
|
.getList().size()) {
|
|
if (!adapter.isRemove() && !adapter.isLoading()) {
|
|
adapter.addList(adapter.getList().size());
|
|
}
|
|
}
|
|
}
|
|
});
|
|
recyclerview.addItemDecoration(new VerticalItemDecoration(
|
|
getActivity(), 1));
|
|
|
|
EventBus.getDefault().register(this);
|
|
|
|
}
|
|
|
|
@Override
|
|
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
|
Bundle savedInstanceState) {
|
|
if (container != null) {
|
|
container.removeView(view);
|
|
}
|
|
return view;
|
|
}
|
|
|
|
public void onEventMainThread(EBPWDismiss dismiss) {
|
|
if (dismissEntity != null) {
|
|
dismissEntity.setShow(false);
|
|
}
|
|
}
|
|
|
|
public void onEventMainThread(EBPackage busFour) {
|
|
String name = nameMap.get(busFour.getPackageName());
|
|
if (name != null) {
|
|
int location = locationMap.get(name);
|
|
if ("安装".equals(busFour.getType())) {
|
|
GameEntity detailedEntity = adapter.getList().get(location);
|
|
for (ApkEntity apkEntity : detailedEntity.getApk()) {
|
|
if (apkEntity.getPackageName().equals(
|
|
busFour.getPackageName())) {
|
|
detailedEntity.getEntryMap().remove(
|
|
apkEntity.getPlatform());
|
|
adapter.notifyItemChanged(location);
|
|
break;
|
|
}
|
|
}
|
|
} else if ("卸载".equals(busFour.getType())) {
|
|
adapter.notifyItemChanged(location);
|
|
}
|
|
}
|
|
}
|
|
|
|
public void onEventMainThread(EBNetworkState busNetworkState) {
|
|
if (busNetworkState.isNetworkConnected()) {
|
|
if (reuse_no_connection.getVisibility() == View.VISIBLE) {
|
|
game_swipe_refresh.setRefreshing(true);
|
|
recyclerview.setVisibility(View.VISIBLE);
|
|
reuse_no_connection.setVisibility(View.GONE);
|
|
handler.postDelayed(runnable, 1000);
|
|
} else if (adapter.isNetworkError()) {
|
|
adapter.setNetworkError(false);
|
|
adapter.notifyItemChanged(adapter.getItemCount() - 1);
|
|
adapter.addList(adapter.getList().size());
|
|
}
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void onResume() {
|
|
super.onResume();
|
|
if (isEverpause) {
|
|
// 黄壮华 添加 初始化游戏状态 修改2015/8/20
|
|
DownloadItemUtils.initializeGameMap(getActivity(), gameMap);
|
|
List<GameEntity> entities = new ArrayList<GameEntity>();
|
|
for (GameEntity entity : adapter.getList()) {
|
|
entity.setEntryMap(gameMap.get(entity.getName()));
|
|
entities.add(entity);
|
|
}
|
|
adapter.setList(entities);
|
|
}
|
|
isEverpause = false;
|
|
DownloadManager.getInstance(getActivity()).addObserver(dataWatcher);
|
|
}
|
|
|
|
@Override
|
|
public void onPause() {
|
|
super.onPause();
|
|
isEverpause = true;
|
|
statusMap.clear();
|
|
DownloadManager.getInstance(getActivity()).removeObserver(dataWatcher);
|
|
}
|
|
|
|
public boolean isEverpause() {
|
|
return isEverpause;
|
|
}
|
|
|
|
Runnable runnable = new Runnable() {
|
|
@Override
|
|
public void run() {
|
|
if (!isDestroy) {
|
|
adapter = new Game1FragmentAdapter(Game1Fragment.this,
|
|
locationMap, gameMap, platformMap, nameMap,
|
|
recyclerview, game_swipe_refresh, reuse_no_connection,
|
|
statusMap, dismissEntity);
|
|
recyclerview.setAdapter(adapter);
|
|
}
|
|
}
|
|
};
|
|
|
|
@Override
|
|
public void onRefresh() {
|
|
handler.postDelayed(runnable, 1000);
|
|
}
|
|
|
|
@Override
|
|
public void onDestroy() {
|
|
super.onDestroy();
|
|
isDestroy = true;
|
|
adapter.setDestroy(true);
|
|
AppController.canclePendingRequests(Game1Fragment.class);
|
|
EventBus.getDefault().unregister(this);
|
|
view = null;
|
|
recyclerview = null;
|
|
game_swipe_refresh = null;
|
|
adapter = null;
|
|
layoutManager = null;
|
|
reuse_no_connection = null;
|
|
locationMap = null;
|
|
gameMap = null;
|
|
platformMap = null;
|
|
nameMap = null;
|
|
statusMap = null;
|
|
dismissEntity = null;
|
|
handler = null;
|
|
dataWatcher = null;
|
|
}
|
|
}
|