344 lines
10 KiB
Java
344 lines
10 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.text.TextUtils;
|
|
import android.view.LayoutInflater;
|
|
import android.view.View;
|
|
import android.view.ViewGroup;
|
|
import android.widget.LinearLayout;
|
|
|
|
import com.gc.materialdesign.views.ProgressBarCircularIndeterminate;
|
|
import com.gh.base.AppController;
|
|
import com.gh.common.constant.Constants;
|
|
import com.gh.common.util.DownloadItemUtils;
|
|
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.EBDownloadDelete;
|
|
import com.gh.gamecenter.eventbus.EBNetworkState;
|
|
import com.gh.gamecenter.eventbus.EBPWDismiss;
|
|
import com.gh.gamecenter.eventbus.EBPackage;
|
|
import com.gh.gamecenter.eventbus.EBUISwitch;
|
|
|
|
import java.util.HashMap;
|
|
import java.util.concurrent.LinkedBlockingQueue;
|
|
|
|
import de.greenrobot.event.EventBus;
|
|
|
|
/**
|
|
*
|
|
* @author 温冠超
|
|
* @email 294299195@qq.com
|
|
* @date 2015-8-8
|
|
* @update 2015-8-11
|
|
* @des 主页插件fragment
|
|
*/
|
|
public class Game3Fragment extends Fragment implements OnRefreshListener {
|
|
|
|
private View view;
|
|
private RecyclerView recyclerview;
|
|
private SwipeRefreshLayout game_swipe_refresh;
|
|
private Game3FragmentAdapter adapter;
|
|
private LinearLayoutManager layoutManager;
|
|
private LinearLayout reuse_no_connection;
|
|
private ProgressBarCircularIndeterminate game_pb_loading;
|
|
|
|
// 黄壮华 添加 记录信息的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 ArrayMap<String, String> statusMap;
|
|
|
|
private boolean isEverpause;
|
|
private boolean isDestroy;
|
|
|
|
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();
|
|
|
|
Integer location = locationMap.get(downloadEntry.getName());
|
|
if (location != null) {
|
|
int index = location;
|
|
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);
|
|
|
|
locationMap = new ArrayMap<String, Integer>();
|
|
gameMap = new ArrayMap<String, ArrayMap<String, DownloadEntry>>();
|
|
platformMap = new ArrayMap<String, LinkedBlockingQueue<String>>();
|
|
nameMap = new ArrayMap<String, String>();
|
|
statusMap = new ArrayMap<String, String>();
|
|
|
|
dismissEntity = new DismissEntity(false);
|
|
|
|
isEverpause = false;
|
|
isDestroy = false;
|
|
|
|
// 黄壮华 添加 初始化游戏状态 修改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);
|
|
game_pb_loading.setVisibility(View.VISIBLE);
|
|
reuse_no_connection.setVisibility(View.GONE);
|
|
handler.postDelayed(runnable, 1000);
|
|
}
|
|
});
|
|
|
|
game_swipe_refresh = (SwipeRefreshLayout) view.findViewById(R.id.game_swipe_refresh);
|
|
game_swipe_refresh.setColorSchemeResources(R.color.theme_colors);
|
|
game_swipe_refresh.setOnRefreshListener(this);
|
|
|
|
game_pb_loading = (ProgressBarCircularIndeterminate) view.findViewById(R.id.game_pb_loading);
|
|
|
|
recyclerview = (RecyclerView) view.findViewById(R.id.game_list);
|
|
recyclerview.setHasFixedSize(true);
|
|
layoutManager = new LinearLayoutManager(getActivity());
|
|
recyclerview.setLayoutManager(layoutManager);
|
|
// 黄壮华 传递引用 修改2015/8/15
|
|
adapter = new Game3FragmentAdapter(Game3Fragment.this, locationMap,
|
|
gameMap, platformMap, nameMap, recyclerview,
|
|
game_swipe_refresh, reuse_no_connection, statusMap,
|
|
dismissEntity,game_pb_loading, false);
|
|
recyclerview.setAdapter(adapter);
|
|
recyclerview.setOnScrollListener(new RecyclerView.OnScrollListener() {
|
|
|
|
@Override
|
|
public void onScrollStateChanged(RecyclerView recyclerView,
|
|
int newState) {
|
|
super.onScrollStateChanged(recyclerView, newState);
|
|
if (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(EBDownloadDelete busThree) {
|
|
LinkedBlockingQueue<String> queue = platformMap.get(busThree.getName());
|
|
if (queue != null) {
|
|
queue.remove(busThree.getPlatform());
|
|
platformMap.put(busThree.getName(), queue);
|
|
}
|
|
Integer location = locationMap.get(busThree.getName());
|
|
if (location != null) {
|
|
int index = location;
|
|
|
|
GameEntity detailedEntity = adapter.getList().get(index);
|
|
if (TextUtils.isEmpty(busThree.getPlatform())) {
|
|
detailedEntity.getEntryMap().remove("官方版");
|
|
} else {
|
|
detailedEntity.getEntryMap().remove(busThree.getPlatform());
|
|
}
|
|
adapter.notifyItemChanged(index);
|
|
}
|
|
}
|
|
|
|
//版本选择框消失事件
|
|
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);
|
|
game_pb_loading.setVisibility(View.GONE);
|
|
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());
|
|
}
|
|
}
|
|
}
|
|
|
|
public void onEventMainThread(EBUISwitch busNine) {
|
|
if ("MainActivity".equals(busNine.getFrom())) {
|
|
if (busNine.getPosition() != 0) {
|
|
statusMap.clear();
|
|
}
|
|
} else if ("GameFragment".equals(busNine.getFrom())) {
|
|
if (busNine.getPosition() == 2) {
|
|
if (game_pb_loading.getVisibility() == View.VISIBLE) {
|
|
adapter.load();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public boolean isEverpause() {
|
|
return isEverpause;
|
|
}
|
|
|
|
@Override
|
|
public void onResume() {
|
|
super.onResume();
|
|
if (isEverpause) {
|
|
// 黄壮华 添加 初始化游戏状态 修改2015/8/20
|
|
DownloadItemUtils.initializeGameMap(getActivity(), gameMap);
|
|
for (GameEntity entity : adapter.getList()) {
|
|
entity.setEntryMap(gameMap.get(entity.getName()));
|
|
}
|
|
adapter.notifyDataSetChanged();
|
|
}
|
|
isEverpause = false;
|
|
DownloadManager.getInstance(getActivity()).addObserver(dataWatcher);
|
|
}
|
|
|
|
@Override
|
|
public void onPause() {
|
|
super.onPause();
|
|
isEverpause = true;
|
|
statusMap.clear();
|
|
DownloadManager.getInstance(getActivity()).removeObserver(dataWatcher);
|
|
}
|
|
|
|
Runnable runnable = new Runnable() {
|
|
@Override
|
|
public void run() {
|
|
if (!isDestroy) {
|
|
adapter = new Game3FragmentAdapter(Game3Fragment.this,
|
|
locationMap, gameMap, platformMap, nameMap, recyclerview,
|
|
game_swipe_refresh, reuse_no_connection, statusMap, dismissEntity,
|
|
game_pb_loading, true);
|
|
recyclerview.setAdapter(adapter);
|
|
}
|
|
}
|
|
};
|
|
|
|
@Override
|
|
public void onRefresh() {
|
|
handler.postDelayed(runnable, 1000);
|
|
}
|
|
|
|
@Override
|
|
public void onDestroy() {
|
|
super.onDestroy();
|
|
isDestroy = true;
|
|
AppController.canclePendingRequests(Game3Fragment.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;
|
|
game_pb_loading = null;
|
|
}
|
|
|
|
}
|