package com.gh.gamecenter; import android.os.Bundle; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.view.View; import android.widget.LinearLayout; import com.gc.materialdesign.views.ProgressBarCircularIndeterminate; import com.gh.base.AppController; import com.gh.base.BaseActivity; import com.gh.common.util.DownloadItemUtils; import com.gh.download.DataWatcher; import com.gh.download.DownloadEntity; import com.gh.download.DownloadManager; import com.gh.gamecenter.adapter.PluginAdapter; import com.gh.gamecenter.entity.GameEntity; import com.gh.gamecenter.eventbus.EBDownloadStatus; import com.gh.gamecenter.eventbus.EBNetworkState; import com.gh.gamecenter.eventbus.EBPackage; import java.util.ArrayList; import java.util.List; /** * Created by LGT on 2016/7/6. */ public class PluginActivity extends BaseActivity { public static final String TAG = PluginActivity.class.getSimpleName(); private RecyclerView plugin_list; private PluginAdapter adapter; private ProgressBarCircularIndeterminate plugin_pb_loading; private LinearLayout reuse_no_connection; private String entrance; private boolean isEverpause = false; private DataWatcher dataWatcher = new DataWatcher() { @Override public void onDataChanged(DownloadEntity downloadEntity) { //特殊 插件化update if (downloadEntity.isPluggable()) { ArrayList locationList = adapter.getLocationMap().get(downloadEntity.getPackageName()); if (locationList != null) { GameEntity gameEntity; for (int location : locationList) { gameEntity = adapter.getPluginList().get(location); if (gameEntity != null) { DownloadItemUtils.processDate(PluginActivity.this, gameEntity, downloadEntity, adapter, location); } } } } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); entrance = getIntent().getStringExtra("entrance"); View contentView = View.inflate(this, R.layout.activity_plugin, null); init(contentView, "可以插件化的游戏"); reuse_no_connection.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { plugin_pb_loading.setVisibility(View.VISIBLE); plugin_list.setVisibility(View.VISIBLE); reuse_no_connection.setVisibility(View.GONE); adapter = new PluginAdapter(PluginActivity.this, entrance); plugin_list.setAdapter(adapter); } }); plugin_list.setHasFixedSize(true); plugin_list.setLayoutManager(new LinearLayoutManager(this)); adapter = new PluginAdapter(this, entrance); plugin_list.setAdapter(adapter); } @Override public void loadDone() { if (plugin_pb_loading != null && plugin_pb_loading.getVisibility() == View.VISIBLE) { plugin_pb_loading.setVisibility(View.GONE); } } // 下载被删除事件 public void onEventMainThread(EBDownloadStatus status) { if ("delete".equals(status.getStatus())) { DownloadManager.getInstance(this).removePlatform(status.getName(), status.getPlatform()); ArrayList locationList = adapter.getLocationMap().get(status.getPackageName()); if (locationList != null) { GameEntity gameEntity; for (int location : locationList) { gameEntity = adapter.getPluginList().get(location); if (gameEntity != null && gameEntity.getEntryMap() != null) { gameEntity.getEntryMap().remove(status.getPlatform()); } adapter.notifyItemChanged(location); } } } } //连接上网络事件 public void onEventMainThread(EBNetworkState busNetworkState) { if (busNetworkState.isNetworkConnected()) { if (reuse_no_connection.getVisibility() == View.VISIBLE) { plugin_list.setVisibility(View.VISIBLE); plugin_pb_loading.setVisibility(View.VISIBLE); reuse_no_connection.setVisibility(View.GONE); adapter = new PluginAdapter(this, entrance); plugin_list.setAdapter(adapter); } } } //安装、卸载事件 public void onEventMainThread(EBPackage busFour) { ArrayList locationList = adapter.getLocationMap().get(busFour.getPackageName()); if (locationList != null) { for (int location : locationList) { if ("安装".equals(busFour.getType()) || "卸载".equals(busFour.getType())) { List list = adapter.getPluginList(); for (int i = 0, size = list.size(); i < size; i++) { if (list.get(i).getApk().get(0).getPackageName().equals(busFour.getPackageName())) { list.remove(i); adapter.notifyItemRemoved(location); break; } } } } adapter.initLocationMap(); } } @Override protected void onPause() { super.onPause(); isEverpause = true; DownloadManager.getInstance(this).removeObserver(dataWatcher); } @Override protected void onResume() { super.onResume(); if (isEverpause) { for (GameEntity entity : adapter.getPluginList()) { entity.setEntryMap(DownloadManager.getInstance(this).getEntryMap(entity.getName())); } adapter.notifyDataSetChanged(); } isEverpause = false; DownloadManager.getInstance(this).addObserver(dataWatcher); } @Override protected void onDestroy() { super.onDestroy(); AppController.canclePendingRequests(TAG); } }