package com.gh.gamecenter.plugin; 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.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.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-8 * @update 2015-8-11 * @des 主页插件fragment */ public class Plugin2Fragment extends Fragment implements OnRefreshListener { private View view; private RecyclerView recyclerview; private SwipeRefreshLayout game_swipe_refresh; private Plugin2FragmentAdapter adapter; private LinearLayoutManager layoutManager; private LinearLayout reuse_no_connection; // 黄壮华 添加 记录信息的map 修改2015/8/15 private ArrayMap locationMap; private ArrayMap> gameMap; private ArrayMap> platformMap; private ArrayMap nameMap; private boolean isEverpause = false; private boolean isDestroy = false; private ArrayMap 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 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 downloadingEntries) { if (!game_swipe_refresh.isRefreshing()) { for (java.util.Map.Entry entry : downloadingEntries .entrySet()) { DownloadEntry downloadEntry = entry.getValue(); Integer location = locationMap.get(downloadEntry.getName()); 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); statusMap = new ArrayMap(); locationMap = new ArrayMap(); gameMap = new ArrayMap>(); platformMap = new ArrayMap>(); nameMap = new ArrayMap(); dismissEntity = new DismissEntity(false); 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 Plugin2FragmentAdapter(Plugin2Fragment.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 (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 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.intValue(); 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); 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() != 1) { statusMap.clear(); } } } @Override public void onResume() { super.onResume(); if (isEverpause) { // 黄壮华 添加 初始化游戏状态 修改2015/8/21 DownloadItemUtils.initializeGameMap(getActivity(), gameMap); List entities = new ArrayList(); for (GameEntity entity : adapter.getList()) { entity.setEntryMap(gameMap.get(entity.getName())); entities.add(entity); } adapter.setList(entities); } isEverpause = false; DownloadManager.getInstance(getActivity()).addObserver(dataWatcher); } public boolean isEverpause() { return isEverpause; } @Override public void onPause() { super.onPause(); isEverpause = true; statusMap.clear(); DownloadManager.getInstance(getActivity()).removeObserver(dataWatcher); } Runnable runnable = new Runnable() { @Override public void run() { adapter = new Plugin2FragmentAdapter(Plugin2Fragment.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; AppController.canclePendingRequests(Plugin2Fragment.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; } }