193 lines
6.0 KiB
Java
193 lines
6.0 KiB
Java
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.SubjectAdapter;
|
|
import com.gh.gamecenter.entity.ApkEntity;
|
|
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;
|
|
|
|
public class SubjectActivity extends BaseActivity {
|
|
|
|
public static final String TAG = SubjectActivity.class.getSimpleName();
|
|
|
|
private RecyclerView subject_list;
|
|
private SubjectAdapter adapter;
|
|
private ProgressBarCircularIndeterminate subject_pb_loading;
|
|
private LinearLayout reuse_no_connection;
|
|
private LinearLayoutManager layoutManager;
|
|
|
|
private int page = 1;
|
|
|
|
private boolean isEverpause = false;
|
|
|
|
// 黄壮华 添加观察者 修改2015/8/15
|
|
private DataWatcher dataWatcher = new DataWatcher() {
|
|
@Override
|
|
public void onDataChanged(DownloadEntity downloadEntity) {
|
|
ArrayList<Integer> locationList = adapter.getLocationMap().get(downloadEntity.getPackageName());
|
|
if (locationList != null) {
|
|
GameEntity gameEntity;
|
|
for (int location : locationList) {
|
|
gameEntity = adapter.getSubjectList().get(location);
|
|
if (gameEntity != null) {
|
|
DownloadItemUtils.processDate(SubjectActivity.this, gameEntity, downloadEntity,
|
|
adapter, location);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
};
|
|
|
|
@Override
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
super.onCreate(savedInstanceState);
|
|
|
|
View contentView = View.inflate(this, R.layout.activity_subject, null);
|
|
init(contentView, getIntent().getStringExtra("name"));
|
|
|
|
reuse_no_connection.setOnClickListener(new View.OnClickListener() {
|
|
@Override
|
|
public void onClick(View v) {
|
|
subject_pb_loading.setVisibility(View.VISIBLE);
|
|
subject_list.setVisibility(View.VISIBLE);
|
|
reuse_no_connection.setVisibility(View.GONE);
|
|
adapter = new SubjectAdapter(SubjectActivity.this);
|
|
subject_list.setAdapter(adapter);
|
|
}
|
|
});
|
|
|
|
layoutManager = new LinearLayoutManager(this);
|
|
|
|
subject_list.setHasFixedSize(true);
|
|
subject_list.setLayoutManager(layoutManager);
|
|
adapter = new SubjectAdapter(this);
|
|
subject_list.setAdapter(adapter);
|
|
|
|
subject_list.setOnScrollListener(new RecyclerView.OnScrollListener() {
|
|
@Override
|
|
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
|
|
super.onScrollStateChanged(recyclerView, newState);
|
|
if (adapter.isLoaded() && newState == RecyclerView.SCROLL_STATE_IDLE
|
|
&& adapter.getItemCount() == layoutManager.findLastVisibleItemPosition() + 1){
|
|
page++;
|
|
adapter.initList(page);
|
|
}
|
|
}
|
|
});
|
|
|
|
}
|
|
|
|
@Override
|
|
public void loadError() {
|
|
if (subject_pb_loading != null && subject_pb_loading.getVisibility() == View.VISIBLE) {
|
|
subject_pb_loading.setVisibility(View.GONE);
|
|
}
|
|
toast("加载失败,请检查网络状态");
|
|
subject_list.setVisibility(View.GONE);
|
|
reuse_no_connection.setVisibility(View.VISIBLE);
|
|
}
|
|
|
|
@Override
|
|
public void loadDone() {
|
|
if (subject_pb_loading != null && subject_pb_loading.getVisibility() == View.VISIBLE) {
|
|
subject_pb_loading.setVisibility(View.GONE);
|
|
}
|
|
}
|
|
|
|
// 下载被删除事件
|
|
public void onEventMainThread(EBDownloadStatus status) {
|
|
if ("delete".equals(status.getStatus())) {
|
|
DownloadManager.getInstance(this).removePlatform(status.getName(), status.getPlatform());
|
|
|
|
ArrayList<Integer> locationList = adapter.getLocationMap().get(status.getPackageName());
|
|
if (locationList != null) {
|
|
GameEntity gameEntity;
|
|
for (int location : locationList) {
|
|
gameEntity = adapter.getSubjectList().get(location);
|
|
if (gameEntity != null && gameEntity.getEntryMap() != null) {
|
|
gameEntity.getEntryMap().remove(status.getPlatform());
|
|
}
|
|
adapter.notifyItemChanged(location);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public void onEventMainThread(EBPackage busFour) {
|
|
ArrayList<Integer> locationList = adapter.getLocationMap().get(busFour.getPackageName());
|
|
if (locationList != null) {
|
|
GameEntity gameEntity;
|
|
for (int location : locationList) {
|
|
if ("安装".equals(busFour.getType())) {
|
|
gameEntity = adapter.getSubjectList().get(location);
|
|
for (ApkEntity apkEntity : gameEntity.getApk()) {
|
|
if (apkEntity.getPackageName().equals(busFour.getPackageName())) {
|
|
if (gameEntity.getEntryMap() != null) {
|
|
gameEntity.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) {
|
|
subject_list.setVisibility(View.VISIBLE);
|
|
subject_pb_loading.setVisibility(View.VISIBLE);
|
|
reuse_no_connection.setVisibility(View.GONE);
|
|
adapter = new SubjectAdapter(SubjectActivity.this);
|
|
subject_list.setAdapter(adapter);
|
|
}
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void onResume() {
|
|
super.onResume();
|
|
if (isEverpause) {
|
|
for (GameEntity entity : adapter.getSubjectList()) {
|
|
entity.setEntryMap(DownloadManager.getInstance(this).getEntryMap(entity.getName()));
|
|
}
|
|
}
|
|
isEverpause = false;
|
|
DownloadManager.getInstance(getApplicationContext()).addObserver(dataWatcher);
|
|
}
|
|
|
|
@Override
|
|
public void onPause() {
|
|
super.onPause();
|
|
isEverpause = true;
|
|
DownloadManager.getInstance(getApplicationContext()).removeObserver(dataWatcher);
|
|
}
|
|
|
|
@Override
|
|
public void onDestroy() {
|
|
super.onDestroy();
|
|
AppController.canclePendingRequests(TAG);
|
|
}
|
|
}
|