|
|
@@ -1,153 +0,0 @@
|
|
|
-package com.sheep.jiuyan.samllsheep.page;
|
|
|
-
|
|
|
-import android.content.Intent;
|
|
|
-import android.os.Bundle;
|
|
|
-import android.os.Environment;
|
|
|
-import android.os.SystemClock;
|
|
|
-import android.support.annotation.Nullable;
|
|
|
-import android.widget.ImageView;
|
|
|
-import android.widget.ListView;
|
|
|
-import android.widget.ProgressBar;
|
|
|
-import android.widget.TextView;
|
|
|
-
|
|
|
-import com.bumptech.glide.Glide;
|
|
|
-import com.liulishuo.filedownloader.BaseDownloadTask;
|
|
|
-import com.liulishuo.filedownloader.FileDownloadListener;
|
|
|
-import com.liulishuo.filedownloader.FileDownloader;
|
|
|
-import com.sheep.jiuyan.samllsheep.R;
|
|
|
-import com.sheep.jiuyan.samllsheep.SheepApp;
|
|
|
-import com.sheep.jiuyan.samllsheep.base.BaseActivity;
|
|
|
-import com.sheep.jiuyan.samllsheep.page.entry.Task;
|
|
|
-import com.sheep.jiuyan.samllsheep.utils.FileUtil;
|
|
|
-import com.sheep.jiuyan.samllsheep.utils.PackageUtil;
|
|
|
-import com.sheep.jiuyan.samllsheep.utils.TitleBarUtils;
|
|
|
-
|
|
|
-import java.io.File;
|
|
|
-
|
|
|
-
|
|
|
-/**
|
|
|
- * 下载界面
|
|
|
- * Created by kemllor on 2017/12/14.
|
|
|
- */
|
|
|
-
|
|
|
-public class DownloadActivity extends BaseActivity {
|
|
|
-
|
|
|
- private String DOWNLOAD_URL = "";
|
|
|
- // private TextView mTvDownload;
|
|
|
- private ProgressBar mPbDownload;
|
|
|
- private ImageView mImgIcon;
|
|
|
- private TextView mTvTime;
|
|
|
- private TextView mTvPercent;
|
|
|
-
|
|
|
-
|
|
|
- private String mPath;
|
|
|
- private String mApkPath;
|
|
|
- private BaseDownloadTask mDownloadTask;
|
|
|
- private ListView lv;
|
|
|
-
|
|
|
-
|
|
|
- @Override
|
|
|
- protected void onCreate(@Nullable Bundle savedInstanceState) {
|
|
|
- super.onCreate(savedInstanceState);
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- protected int getLayoutId() {
|
|
|
- return R.layout.download_activity;
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public void initListener() {
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public void initData() {
|
|
|
- Intent intent = getIntent();
|
|
|
- Task task = (Task) intent.getSerializableExtra("task");
|
|
|
- mPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/"
|
|
|
- + SheepApp.mContext.getPackageName();
|
|
|
- mApkPath = new File(mPath, SystemClock.currentThreadTimeMillis() + "_sheepTask.apk").getAbsolutePath();
|
|
|
- if (task != null) {
|
|
|
- DOWNLOAD_URL = task.getDownLoadUrl();
|
|
|
- mTvTime.setText(task.getTitle());
|
|
|
- Glide.with(this)
|
|
|
- .load(task.getIcon())
|
|
|
- .placeholder(R.drawable.icon)
|
|
|
- .dontAnimate()
|
|
|
- .into(mImgIcon);
|
|
|
- File file = new File(mPath);
|
|
|
- if (file.exists()) {
|
|
|
- FileUtil.deleteFile(file);
|
|
|
- }
|
|
|
- file.mkdirs();
|
|
|
- }
|
|
|
- startDownload();
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public void initView() {
|
|
|
- findViews();
|
|
|
- TitleBarUtils.getInstance()
|
|
|
- .setTitle(this,"任务下载")
|
|
|
- .setTitleFinish(this);
|
|
|
- lv=(ListView)findViewById(R.id.lv);
|
|
|
-
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- private void findViews() {
|
|
|
- mTvPercent = (TextView) findViewById(R.id.tv_percent);
|
|
|
- mPbDownload = (ProgressBar) findViewById(R.id.pb_download);
|
|
|
- mImgIcon = (ImageView) findViewById(R.id.img_icon);
|
|
|
- mTvTime = (TextView) findViewById(R.id.tv_time);
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- private void startDownload() {
|
|
|
- if (mDownloadTask == null)
|
|
|
- mDownloadTask = FileDownloader.getImpl()
|
|
|
- .create(DOWNLOAD_URL)
|
|
|
- .setPath(mApkPath, false)
|
|
|
- .setCallbackProgressTimes(300)
|
|
|
- .setMinIntervalUpdateSpeed(400)
|
|
|
- .setListener(new FileDownloadListener() {
|
|
|
- @Override
|
|
|
- protected void pending(BaseDownloadTask task, int soFarBytes, int totalBytes) {
|
|
|
- }
|
|
|
- @Override
|
|
|
- protected void progress(BaseDownloadTask task, int soFarBytes, int totalBytes) {
|
|
|
- int percent = (int) ((double) soFarBytes / (double) totalBytes * 100);
|
|
|
- mPbDownload.setProgress(percent);
|
|
|
- mTvPercent.setText(percent + "%");
|
|
|
- }
|
|
|
- @Override
|
|
|
- protected void blockComplete(BaseDownloadTask task) {
|
|
|
- }
|
|
|
- @Override
|
|
|
- protected void completed(BaseDownloadTask task) {
|
|
|
- PackageUtil.installApk(getApplicationContext(),mApkPath);
|
|
|
- mTvPercent.setText("100%");
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- protected void paused(BaseDownloadTask task, int soFarBytes, int totalBytes) {
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- protected void error(BaseDownloadTask task, Throwable e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- protected void warn(BaseDownloadTask task) {
|
|
|
-
|
|
|
-
|
|
|
- }
|
|
|
- });
|
|
|
-
|
|
|
- mDownloadTask.start();
|
|
|
- }
|
|
|
-
|
|
|
-}
|