330 lines
12 KiB
Java
330 lines
12 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.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.entity.SubjectEntity;
|
|
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.List;
|
|
import java.util.Map;
|
|
import java.util.concurrent.LinkedBlockingQueue;
|
|
|
|
import de.greenrobot.event.EventBus;
|
|
|
|
/**
|
|
* Created by khy on 2016/6/28.
|
|
*/
|
|
public class Game2Fragment extends Fragment implements SwipeRefreshLayout.OnRefreshListener {
|
|
|
|
private View view;
|
|
private RecyclerView recyclerview;
|
|
private SwipeRefreshLayout game_swipe_refresh;
|
|
private Game2FragmentAdapter adapter;
|
|
private LinearLayout reuse_no_connection;
|
|
private ProgressBarCircularIndeterminate game_pb_loading;
|
|
|
|
//下载用到的map
|
|
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 DismissEntity dismissEntity;
|
|
|
|
private boolean isEverpause;
|
|
private boolean isDestroy;
|
|
|
|
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);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
};
|
|
|
|
DataWatcher dataWatcher = new DataWatcher() {
|
|
@Override
|
|
public void onDataChanged(HashMap<String, DownloadEntry> downloadingEntries) {
|
|
if (!game_swipe_refresh.isRefreshing()) {
|
|
for (Map.Entry<String, DownloadEntry> stringDownloadEntryEntry : downloadingEntries.entrySet()) {
|
|
DownloadEntry downloadEntry = stringDownloadEntryEntry.getValue();
|
|
|
|
Integer location = locationMap.get(downloadEntry.getName());
|
|
if (location != null) {
|
|
int index = location;
|
|
GameEntity detailedEntity = getEntityByLocation(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;
|
|
|
|
//初始化gamemap
|
|
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);
|
|
recyclerview.setLayoutManager(new LinearLayoutManager(getActivity()));
|
|
adapter = new Game2FragmentAdapter(Game2Fragment.this, locationMap, gameMap,
|
|
platformMap, nameMap, recyclerview, game_swipe_refresh, reuse_no_connection,
|
|
statusMap, dismissEntity, game_pb_loading, false);
|
|
recyclerview.setAdapter(adapter);
|
|
recyclerview.addItemDecoration(new VerticalItemDecoration(getActivity(), 1));
|
|
|
|
EventBus.getDefault().register(this);
|
|
}
|
|
|
|
@Nullable
|
|
@Override
|
|
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
|
|
@Nullable 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 = getEntityByLocation(index);
|
|
if (detailedEntity != null) {
|
|
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) {
|
|
Integer location = locationMap.get(name);
|
|
if (location != null) {
|
|
if ("安装".equals(busFour.getType())) {
|
|
GameEntity detailedEntity = getEntityByLocation(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.VISIBLE);
|
|
reuse_no_connection.setVisibility(View.GONE);
|
|
handler.postDelayed(runnable, 1000);
|
|
}
|
|
}
|
|
}
|
|
|
|
//Fragment界面切换事件
|
|
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() == 1) {
|
|
if (game_pb_loading.getVisibility() == View.VISIBLE) {
|
|
adapter.load();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private GameEntity getEntityByLocation(int position) {
|
|
List<SubjectEntity> list = adapter.getSubectList();
|
|
int offset = 0;
|
|
for (int i = 0; i < list.size(); i++) {
|
|
if (position > offset && position <= list.get(i).getData().size() + offset) {
|
|
return list.get(i).getData().get(position - offset - 1);
|
|
}
|
|
offset = list.get(i).getData().size() + offset + 1;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
@Override
|
|
public void onResume() {
|
|
super.onResume();
|
|
if (isEverpause) {
|
|
//修改游戏下载状态
|
|
DownloadItemUtils.initializeGameMap(getActivity(), gameMap);
|
|
for (SubjectEntity subjectEntity : adapter.getSubectList()) {
|
|
for (GameEntity entity :subjectEntity.getData()) {
|
|
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);
|
|
}
|
|
|
|
public boolean isEverpause() {
|
|
return isEverpause;
|
|
}
|
|
|
|
Runnable runnable = new Runnable() {
|
|
@Override
|
|
public void run() {
|
|
adapter = new Game2FragmentAdapter(Game2Fragment.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(Game2Fragment.class);
|
|
EventBus.getDefault().unregister(this);
|
|
view = null;
|
|
recyclerview = null;
|
|
adapter = null;
|
|
reuse_no_connection = null;
|
|
game_swipe_refresh = null;
|
|
game_pb_loading = null;
|
|
locationMap = null;
|
|
gameMap = null;
|
|
platformMap = null;
|
|
nameMap = null;
|
|
statusMap = null;
|
|
dismissEntity = null;
|
|
handler = null;
|
|
dataWatcher = null;
|
|
}
|
|
}
|