LimitActivity.java 6.04 KB
package com.cnlive.downloadfile;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.SimpleItemAnimator;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.cnlive.downloadfile.adapter.LimitDownloadAdapter;
import com.cnlive.downloadfile.download.limit.DownLoadService;
import com.cnlive.downloadfile.download.limit.DownloadInfo;
import com.cnlive.downloadfile.download.limit.DownloadLimitManager;
import com.cnlive.downloadfile.download.limit.DownloadStatus;
import com.cnlive.downloadfile.download.util.NetWorkUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import static com.cnlive.downloadfile.download.util.CheckNetworkStatusChangeListener.Status.TYPE_MOBILE;
import static com.cnlive.downloadfile.download.util.CheckNetworkStatusChangeListener.Status.TYPE_UN_NETWORK;
import static com.cnlive.downloadfile.download.util.CheckNetworkStatusChangeListener.Status.TYPE_WIFI;


public class LimitActivity extends AppCompatActivity implements View.OnClickListener {

    private RecyclerView recycler_view;
    private LimitDownloadAdapter mAdapter;
    private List<DownloadInfo> mData = new ArrayList<>();
    private Button allStartPause, allClear;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recycler);
        EventBus.getDefault().register(this);
        recycler_view = findViewById(R.id.recycler_view);

        allStartPause = findViewById(R.id.start_pause);
        allClear = findViewById(R.id.clear);

        allStartPause.setOnClickListener(this);
        allClear.setOnClickListener(this);
        if (DownloadLimitManager.getInstance().getCurremtDownload() == null) {
            allStartPause.setText("全部开始");
        } else {
            allStartPause.setText("全部暂停");
        }
        mData = MyApplication.getInstances().getDaoSession().loadAll(DownloadInfo.class);
        mAdapter = new LimitDownloadAdapter(this, mData);
        recycler_view.setLayoutManager(new LinearLayoutManager(this));
        recycler_view.setAdapter(mAdapter);
        // 取消item刷新的动画
        ((SimpleItemAnimator) recycler_view.getItemAnimator()).setSupportsChangeAnimations(false);

        mAdapter.setDelectClick(new LimitDownloadAdapter.DelectClick() {
            @Override
            public void onDelectClickListener(DownloadInfo downloadInfo, int position) {
                //删除该下载
                Intent intent = new Intent(LimitActivity.this, DownLoadService.class);
                intent.setAction(DownLoadService.ACTION_DELECT);
                intent.putExtra("fileUrl", downloadInfo);
                startService(intent);

                mData.remove(position);
                mAdapter.notifyItemRemoved(position);
                mAdapter.notifyDataSetChanged();
            }
        });
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void update(DownloadInfo info) {
        if (DownloadStatus.DOWNLOAD.equals(info.getDownloadStatus())) {
            allStartPause.setText("全部暂停");
            mAdapter.updateProgress(info);
        } else if (DownloadStatus.DOWNLOAD_OVER.equals(info.getDownloadStatus())) {
            for (int i = 0; i < mData.size(); i++) {
                if (mData.get(i).getUrl().equals(info.getUrl())) {
                    mData.set(i, info);
                    MyApplication.getInstances().getDaoSession().delete(info);
                    mData.remove(i);
                    mAdapter.notifyItemRemoved(i);
                    mAdapter.notifyDataSetChanged();
                    break;
                }
            }
            if (DownloadLimitManager.getInstance().getCurremtDownload() == null) {
                allStartPause.setText("全部开始");
            }
        } else if (DownloadStatus.DOWNLOAD_PAUSE.equals(info.getDownloadStatus())) {
            mAdapter.updateProgress(info);
            boolean isAllPause = true;
            for(int i = 0;i < mData.size();i++){
                if(!mData.get(i).getDownloadStatus().equals(DownloadStatus.DOWNLOAD_PAUSE)){
                    isAllPause = false;
                    break;
                }
            }
            if(isAllPause){
                allStartPause.setText("全部开始");
            }
        } else if (DownloadStatus.DOWNLOAD_WAIT.equals(info.getDownloadStatus())) {
            mAdapter.updateProgress(info);
        } else if (DownloadStatus.DOWNLOAD_ERROR.equals(info.getDownloadStatus())) {
            Toast.makeText(this, "下载出错", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.start_pause:
                allPause();
                break;
            case R.id.clear:
                Intent intent = new Intent(LimitActivity.this, DownLoadService.class);
                intent.setAction(DownLoadService.ACION_CLEAR);
                startService(intent);
                mData.clear();
                mAdapter.notifyDataSetChanged();
                break;
        }
    }

    private void allPause(){
        if (allStartPause.getText().toString().equals("全部开始")) {
            Intent intent = new Intent(LimitActivity.this, DownLoadService.class);
            intent.setAction(DownLoadService.ACTION_ALLSTART);
            startService(intent);
        } else {
            Intent intent = new Intent(LimitActivity.this, DownLoadService.class);
            intent.setAction(DownLoadService.ACTION_ALLPAUSE);
            startService(intent);
        }
    }
}