438 lines
20 KiB
Java
438 lines
20 KiB
Java
package com.gh.gamecenter.download;
|
|
|
|
import android.app.NotificationManager;
|
|
import android.content.Context;
|
|
import android.content.Intent;
|
|
import android.os.Message;
|
|
import android.support.v4.content.ContextCompat;
|
|
import android.support.v7.widget.DefaultItemAnimator;
|
|
import android.support.v7.widget.LinearLayoutManager;
|
|
import android.support.v7.widget.RecyclerView;
|
|
import android.view.MotionEvent;
|
|
import android.view.View;
|
|
import android.widget.LinearLayout;
|
|
import android.widget.RelativeLayout;
|
|
import android.widget.TextView;
|
|
|
|
import com.gh.base.fragment.BaseFragment;
|
|
import com.gh.common.util.DialogUtils;
|
|
import com.gh.common.util.EntranceUtils;
|
|
import com.gh.common.util.NetworkUtils;
|
|
import com.gh.common.util.PackageUtils;
|
|
import com.gh.common.view.RecyclerViewExtended;
|
|
import com.gh.download.DownloadManager;
|
|
import com.gh.gamecenter.DownloadManagerActivity;
|
|
import com.gh.gamecenter.MainActivity;
|
|
import com.gh.gamecenter.R;
|
|
import com.gh.gamecenter.eventbus.EBDownloadChanged;
|
|
import com.gh.gamecenter.eventbus.EBMiPush;
|
|
import com.gh.gamecenter.eventbus.EBPackage;
|
|
import com.gh.gamecenter.eventbus.EBSkip;
|
|
import com.gh.gamecenter.eventbus.EBUISwitch;
|
|
import com.lightgame.download.DataWatcher;
|
|
import com.lightgame.download.DownloadConfig;
|
|
import com.lightgame.download.DownloadEntity;
|
|
import com.lightgame.download.DownloadStatus;
|
|
|
|
import org.greenrobot.eventbus.EventBus;
|
|
import org.greenrobot.eventbus.Subscribe;
|
|
import org.greenrobot.eventbus.ThreadMode;
|
|
|
|
import butterknife.BindView;
|
|
import butterknife.OnClick;
|
|
|
|
import static com.gh.gamecenter.kuaichuan.KuaichuanNotification.NOTIFY_ID;
|
|
|
|
|
|
/**
|
|
* 下载管理 fragment
|
|
*
|
|
* @author 黄壮华
|
|
*/
|
|
public class GameDownloadFragment extends BaseFragment implements View.OnClickListener {
|
|
|
|
@BindView(R.id.downloadmanager_rv_show)
|
|
RecyclerViewExtended mDownloadmanagerRv;
|
|
@BindView(R.id.reuse_nodata_skip)
|
|
LinearLayout mNoDataSkip;
|
|
@BindView(R.id.downloadmanager_rl_head)
|
|
RelativeLayout mDownloadmanagerHeadRl;
|
|
@BindView(R.id.downloadmanager_tv_task)
|
|
TextView mDownloadmanagerTaskTv;
|
|
@BindView(R.id.reuse_nodata_skip_tv_hint)
|
|
TextView mNoDataSkipHintTv;
|
|
@BindView(R.id.reuse_nodata_skip_tv_btn)
|
|
TextView mNoDataSkipBtn;
|
|
@BindView(R.id.downloadmanager_tv_allstart)
|
|
TextView mDownloadmanagerAllstartTv;
|
|
|
|
GameDownloadFragmentAdapter adapter;
|
|
RelativeLayout.LayoutParams rparams;
|
|
|
|
private String url;
|
|
|
|
private boolean isScroll;
|
|
|
|
private DataWatcher dataWatcher = new DataWatcher() {
|
|
@Override
|
|
public void onDataChanged(DownloadEntity downloadEntity) {
|
|
if (downloadEntity.getStatus().equals(DownloadStatus.downloading)
|
|
|| downloadEntity.getStatus().equals(DownloadStatus.pause)
|
|
|| downloadEntity.getStatus().equals(DownloadStatus.waiting)) {
|
|
adapter.notifyItemChanged(adapter.getBase());
|
|
}
|
|
Integer location = adapter.getLocation(downloadEntity.getUrl());
|
|
if (location != null) {
|
|
if (!"pause".equals(adapter.getStatusMap().get(downloadEntity.getUrl()))) {
|
|
if (downloadEntity.getStatus().equals(DownloadStatus.done)) {
|
|
// 检查对应下载列表中是否存在该数据
|
|
boolean isContains = false;
|
|
for (DownloadEntity entity : adapter.getDownloadingList()) {
|
|
if (entity.getUrl().equals(downloadEntity.getUrl())) {
|
|
isContains = true;
|
|
break;
|
|
}
|
|
}
|
|
if (isContains) {
|
|
// 删除下载中列表对应数据
|
|
if (adapter.getDownloadingList().size() == 1) {
|
|
location = adapter.getLocation(downloadEntity.getUrl());
|
|
if (location != null) {
|
|
adapter.getDownloadingList().remove(location.intValue());
|
|
adapter.initLocationMap();
|
|
adapter.notifyItemRangeRemoved(adapter.getBase(), 2);
|
|
}
|
|
} else {
|
|
location = adapter.getLocation(downloadEntity.getUrl());
|
|
if (location != null) {
|
|
adapter.getDownloadingList().remove(location.intValue());
|
|
adapter.initLocationMap();
|
|
adapter.notifyItemRemoved(adapter.getBase() + location + 1);
|
|
adapter.notifyItemChanged(adapter.getBase() + 1);
|
|
}
|
|
}
|
|
|
|
// 添加进已完成列表
|
|
if (adapter.getDoneList().isEmpty()) {
|
|
adapter.getDoneList().add(0, downloadEntity);
|
|
adapter.initLocationMap();
|
|
adapter.notifyItemRangeInserted(0, 2);
|
|
} else {
|
|
adapter.getDoneList().add(0, downloadEntity);
|
|
adapter.initLocationMap();
|
|
adapter.notifyItemInserted(1);
|
|
}
|
|
|
|
} else {
|
|
location = adapter.getLocation(downloadEntity.getUrl());
|
|
if (location != null) {
|
|
adapter.notifyItemChanged(location + 1);
|
|
}
|
|
}
|
|
|
|
adapter.getUrlMap().put(PackageUtils.getPackageNameByPath(getActivity(),
|
|
downloadEntity.getPath()), downloadEntity.getUrl());
|
|
} else if (DownloadStatus.cancel.equals(downloadEntity.getStatus())) { // 有可能由于网络劫持造成的
|
|
adapter.initMap();
|
|
adapter.notifyDataSetChanged();
|
|
int listSize = adapter.getDownloadingList().size() + adapter.getDoneList().size();
|
|
if (listSize == 0) {
|
|
EventBus.getDefault().post(new EBDownloadChanged("download", View.GONE, listSize));
|
|
if (mNoDataSkip.getVisibility() == View.GONE) {
|
|
mNoDataSkip.setVisibility(View.VISIBLE);
|
|
}
|
|
} else {
|
|
EventBus.getDefault().post(new EBDownloadChanged("download", View.VISIBLE, listSize));
|
|
}
|
|
} else {
|
|
location = adapter.getLocation(downloadEntity.getUrl());
|
|
if (location != null && adapter.getDownloadingList().size() > location) {
|
|
adapter.getDownloadingList().set(location, downloadEntity);
|
|
adapter.notifyItemChanged(adapter.getBase() + location + 1);
|
|
}
|
|
}
|
|
if (downloadEntity.getStatus() == DownloadStatus.neterror) {
|
|
adapter.notifyItemChanged(adapter.getBase());
|
|
}
|
|
}
|
|
} else {
|
|
if (!adapter.getDeleteList().contains(downloadEntity.getUrl())) {
|
|
DownloadStatus status = downloadEntity.getStatus();
|
|
if (status.equals(DownloadStatus.downloading)
|
|
|| status.equals(DownloadStatus.waiting)) {
|
|
if (adapter.getDownloadingList().isEmpty()) {
|
|
adapter.getDownloadingList().add(0, downloadEntity);
|
|
adapter.initLocationMap();
|
|
adapter.notifyItemRangeInserted(adapter.getBase(), 2);
|
|
if (mNoDataSkip.getVisibility() == View.VISIBLE) {
|
|
mNoDataSkip.setVisibility(View.GONE);
|
|
}
|
|
EventBus.getDefault().post(new EBDownloadChanged("download",
|
|
View.VISIBLE, 1));
|
|
} else {
|
|
adapter.getDownloadingList().add(0, downloadEntity);
|
|
adapter.initLocationMap();
|
|
adapter.notifyItemInserted(adapter.getBase() + 1);
|
|
adapter.notifyItemChanged(adapter.getBase());
|
|
EventBus.getDefault().post(new EBDownloadChanged("download",
|
|
View.VISIBLE, adapter.getDoneList().size() + adapter.getDownloadingList().size()));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
};
|
|
|
|
@Override
|
|
protected int getLayoutId() {
|
|
return R.layout.downloadmanager;
|
|
}
|
|
|
|
@Override
|
|
protected void initView(View view) {
|
|
super.initView(view);
|
|
String path = getActivity().getIntent().getStringExtra(EntranceUtils.KEY_PATH);
|
|
url = getActivity().getIntent().getStringExtra(EntranceUtils.KEY_URL);
|
|
|
|
isScroll = false;
|
|
|
|
rparams = (RelativeLayout.LayoutParams) mDownloadmanagerHeadRl.getLayoutParams();
|
|
|
|
mNoDataSkip.setVisibility(View.GONE);
|
|
mNoDataSkipHintTv.setText("暂无下载");
|
|
mNoDataSkipBtn.setText("去首页看看");
|
|
mNoDataSkipBtn.setOnClickListener(new View.OnClickListener() {
|
|
@Override
|
|
public void onClick(View v) {
|
|
Intent intent = new Intent(getActivity(), MainActivity.class);
|
|
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
|
|
getActivity().startActivity(intent);
|
|
|
|
mNoDataSkipBtn.postDelayed(new Runnable() {
|
|
@Override
|
|
public void run() {
|
|
EventBus.getDefault().post(new EBSkip(MainActivity.EB_SKIP_GAMEFRAGMENT, 0));
|
|
}
|
|
}, 300);
|
|
}
|
|
});
|
|
|
|
mDownloadmanagerRv.setHasFixedSize(true);
|
|
adapter = new GameDownloadFragmentAdapter(getActivity(), mNoDataSkip, url);
|
|
mDownloadmanagerRv.setAdapter(adapter);
|
|
final LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
|
|
mDownloadmanagerRv.setLayoutManager(layoutManager);
|
|
((DefaultItemAnimator) mDownloadmanagerRv.getItemAnimator()).setSupportsChangeAnimations(false);
|
|
mDownloadmanagerRv.addOnScrollListener(new RecyclerView.OnScrollListener() {
|
|
@Override
|
|
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
|
|
int position = layoutManager.findFirstVisibleItemPosition();
|
|
if (adapter.getDoneList().isEmpty()) {
|
|
if (position >= 0) {
|
|
mDownloadmanagerHeadRl.setVisibility(View.VISIBLE);
|
|
mDownloadmanagerTaskTv.setText(R.string.downloading);
|
|
mDownloadmanagerAllstartTv.setVisibility(View.VISIBLE);
|
|
} else {
|
|
mDownloadmanagerHeadRl.setVisibility(View.GONE);
|
|
}
|
|
} else {
|
|
if (position >= 0 && position <= adapter.getDoneList().size()) {
|
|
mDownloadmanagerHeadRl.setVisibility(View.VISIBLE);
|
|
mDownloadmanagerTaskTv.setText("已完成");
|
|
mDownloadmanagerAllstartTv.setVisibility(View.GONE);
|
|
} else if (position >= adapter.getDoneList().size() + 1) {
|
|
mDownloadmanagerHeadRl.setVisibility(View.VISIBLE);
|
|
mDownloadmanagerTaskTv.setText(R.string.downloading);
|
|
mDownloadmanagerAllstartTv.setVisibility(View.VISIBLE);
|
|
} else {
|
|
mDownloadmanagerHeadRl.setVisibility(View.GONE);
|
|
}
|
|
}
|
|
|
|
if (adapter.getDoneList().size() != 0
|
|
&& position == adapter.getDoneList().size()) {
|
|
int buttom = layoutManager.findViewByPosition(position).getBottom();
|
|
if (buttom <= mDownloadmanagerHeadRl.getHeight()) {
|
|
rparams.topMargin = buttom - mDownloadmanagerHeadRl.getHeight();
|
|
mDownloadmanagerHeadRl.setLayoutParams(rparams);
|
|
} else {
|
|
rparams.topMargin = 0;
|
|
mDownloadmanagerHeadRl.setLayoutParams(rparams);
|
|
}
|
|
} else {
|
|
rparams.topMargin = 0;
|
|
mDownloadmanagerHeadRl.setLayoutParams(rparams);
|
|
}
|
|
}
|
|
});
|
|
mDownloadmanagerRv.setOnDispatchTouchListener(new RecyclerViewExtended.OnDispatchTouchListener() {
|
|
@Override
|
|
public void onDispatch(View v, MotionEvent event) {
|
|
if (url != null && event.getAction() == MotionEvent.ACTION_DOWN) {
|
|
url = null;
|
|
adapter.setUrl(null);
|
|
}
|
|
}
|
|
});
|
|
|
|
if (path != null) {
|
|
adapter.showPluginDialog(path);
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void onResume() {
|
|
super.onResume();
|
|
adapter.initMap();
|
|
DownloadManager.getInstance(getActivity()).addObserver(dataWatcher);
|
|
adapter.notifyDataSetChanged();
|
|
|
|
if (adapter.getDownloadingList().isEmpty() && adapter.getDoneList().isEmpty()) {
|
|
mNoDataSkip.setVisibility(View.VISIBLE);
|
|
} else {
|
|
mNoDataSkip.setVisibility(View.GONE);
|
|
}
|
|
if (url != null && !isScroll) {
|
|
for (int i = 0, size = adapter.getDoneList().size(); i < size; i++) {
|
|
if (adapter.getDoneList().get(i).getUrl().equals(url)) {
|
|
mDownloadmanagerRv.scrollToPosition(i + 1);
|
|
isScroll = true;
|
|
return;
|
|
}
|
|
}
|
|
for (int i = 0, size = adapter.getDownloadingList().size(); i < size; i++) {
|
|
if (adapter.getDownloadingList().get(i).getUrl().equals(url)) {
|
|
mDownloadmanagerRv.scrollToPosition(adapter.getBase() + i + 1);
|
|
isScroll = true;
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void onPause() {
|
|
super.onPause();
|
|
DownloadManager.getInstance(getActivity()).removeObserver(dataWatcher);
|
|
}
|
|
|
|
@Subscribe(threadMode = ThreadMode.MAIN)
|
|
public void onEventMainThread(EBMiPush mipush) {
|
|
if ("plugin_install".equals(mipush.getFrom())) {
|
|
String path = (String) mipush.getObj();
|
|
adapter.showPluginDialog(path);
|
|
}
|
|
}
|
|
|
|
@Subscribe(threadMode = ThreadMode.MAIN)
|
|
public void onEventMainThread(EBPackage busFour) {
|
|
String packageName = busFour.getPackageName();
|
|
String url = adapter.getUrl(packageName);
|
|
if (url != null) {
|
|
Integer location = adapter.getLocation(url);
|
|
if (location != null) {
|
|
if ("安装".equals(busFour.getType())) {
|
|
// DownloadEntity downloadEntity = DownloadManager.getInstance(getActivity()).get(url);
|
|
// if (downloadEntity == null
|
|
// || !downloadEntity.isPlugin() // 不是插件游戏,自己删除数据库数据和安装包
|
|
// || PackageUtils.isSignature(getActivity(), packageName)) {// 是插件游戏,判断签名是否相同,是才删除数据库数据和安装包
|
|
if (adapter.getDownloadingList().isEmpty() && adapter.getDoneList().size() == 1) {
|
|
adapter.getDoneList().remove(location.intValue());
|
|
adapter.getLocationMap().clear();
|
|
adapter.notifyDataSetChanged();
|
|
EventBus.getDefault().post(new EBDownloadChanged("download", View.GONE, 0));
|
|
if (mNoDataSkip.getVisibility() == View.GONE) {
|
|
mNoDataSkip.setVisibility(View.VISIBLE);
|
|
}
|
|
} else if (adapter.getDoneList().size() == 1) {
|
|
adapter.getDoneList().remove(location.intValue());
|
|
adapter.initLocationMap();
|
|
adapter.notifyItemRangeRemoved(0, 2);
|
|
EventBus.getDefault().post(new EBDownloadChanged("download", View.VISIBLE,
|
|
adapter.getDoneList().size() + adapter.getDownloadingList().size()));
|
|
} else {
|
|
adapter.getDoneList().remove(location.intValue());
|
|
adapter.initLocationMap();
|
|
adapter.notifyItemRemoved(location + 1);
|
|
EventBus.getDefault().post(new EBDownloadChanged("download", View.VISIBLE,
|
|
adapter.getDoneList().size() + adapter.getDownloadingList().size()));
|
|
}
|
|
// }
|
|
}
|
|
}
|
|
}
|
|
|
|
if ("安装".equals(busFour.getType())) {
|
|
// 取消快传完成通知栏
|
|
NotificationManager nManager = (NotificationManager) getContext().getSystemService(Context.NOTIFICATION_SERVICE);
|
|
nManager.cancel(packageName, NOTIFY_ID);
|
|
}
|
|
}
|
|
|
|
@Subscribe(threadMode = ThreadMode.MAIN)
|
|
public void onEventMainThread(EBUISwitch busNine) {
|
|
if (DownloadManagerActivity.TAG.equals(busNine.getFrom())) {
|
|
if (busNine.getPosition() != 0) {
|
|
adapter.getDeleteList().clear();
|
|
}
|
|
}
|
|
}
|
|
|
|
@OnClick(R.id.downloadmanager_tv_allstart)
|
|
@Override
|
|
public void onClick(View v) {
|
|
if (v.getId() == R.id.downloadmanager_tv_allstart) {
|
|
String str = ((TextView) v).getText().toString();
|
|
if ("全部开始".equals(str)) {
|
|
if (NetworkUtils.isWifiConnected(getActivity())) {
|
|
startAll();
|
|
} else {
|
|
DialogUtils.showDownloadDialog(getActivity(), new DialogUtils.ConfirmListener() {
|
|
@Override
|
|
public void onConfirm() {
|
|
startAll();
|
|
}
|
|
});
|
|
}
|
|
} else {
|
|
pauseAll();
|
|
}
|
|
adapter.notifyItemChanged(adapter.getBase());
|
|
}
|
|
}
|
|
|
|
public void pauseAll() {
|
|
for (DownloadEntity downloadEntity : adapter.getDownloadingList()) {
|
|
DownloadManager.getInstance(getActivity()).put(downloadEntity.getUrl(),
|
|
System.currentTimeMillis());
|
|
Message msg = Message.obtain();
|
|
msg.what = DownloadConfig.PAUSE_DOWNLOAD_TASK;
|
|
msg.obj = downloadEntity.getUrl();
|
|
DownloadManager.getInstance(getActivity()).sendMessageDelayed(msg, 1000);
|
|
// DownloadManager.getInstance(getContext()).pause(downloadEntity);
|
|
}
|
|
DownloadManager.getInstance(getContext()).pauseAll();
|
|
mDownloadmanagerAllstartTv.setText("全部开始");
|
|
mDownloadmanagerAllstartTv.setTextColor(ContextCompat.getColor(getContext(), R.color.theme));
|
|
}
|
|
|
|
private void startAll() {
|
|
for (DownloadEntity downloadEntity : adapter.getDownloadingList()) {
|
|
// DownloadManager.getInstance(getContext()).add(downloadEntity);
|
|
// adapter.getStatusMap().put(downloadEntity.getUrl(), "downloading");
|
|
|
|
DownloadManager.getInstance(getActivity()).put(downloadEntity.getUrl(),
|
|
System.currentTimeMillis());
|
|
Message msg = Message.obtain();
|
|
msg.what = DownloadConfig.CONTINUE_DOWNLOAD_TASK;
|
|
msg.obj = downloadEntity.getUrl();
|
|
DownloadManager.getInstance(getActivity()).sendMessageDelayed(msg, 1000);
|
|
}
|
|
// DownloadManager.getInstance(getContext()).startAll();
|
|
mDownloadmanagerAllstartTv.setText("全部暂停");
|
|
mDownloadmanagerAllstartTv.setTextColor(ContextCompat.getColor(getContext(), R.color.btn_gray));
|
|
|
|
}
|
|
|
|
}
|