|
|
@@ -12,17 +12,28 @@ import android.support.v4.content.FileProvider;
|
|
|
import android.system.Os;
|
|
|
import android.text.TextUtils;
|
|
|
import android.view.View;
|
|
|
+import android.widget.ImageView;
|
|
|
import android.widget.ProgressBar;
|
|
|
import android.widget.TextView;
|
|
|
|
|
|
+import com.bumptech.glide.Glide;
|
|
|
import com.jiuyan.sheep_children.R;
|
|
|
+import com.jiuyan.sheep_children.SheepApp;
|
|
|
import com.jiuyan.sheep_children.base.BaseActivity;
|
|
|
import com.jiuyan.sheep_children.net.NetManager;
|
|
|
+import com.jiuyan.sheep_children.page.entry.TaskPublish;
|
|
|
+import com.jiuyan.sheep_children.utils.FileUtil;
|
|
|
+import com.liulishuo.filedownloader.BaseDownloadTask;
|
|
|
+import com.liulishuo.filedownloader.FileDownloadListener;
|
|
|
+import com.liulishuo.filedownloader.FileDownloader;
|
|
|
+import com.liulishuo.filedownloader.connection.FileDownloadConnection;
|
|
|
+import com.liulishuo.filedownloader.services.DownloadMgrInitialParams;
|
|
|
|
|
|
import java.io.File;
|
|
|
import java.io.FileOutputStream;
|
|
|
import java.io.IOException;
|
|
|
import java.io.OutputStream;
|
|
|
+import java.io.Serializable;
|
|
|
import java.nio.file.Path;
|
|
|
import java.util.Date;
|
|
|
|
|
|
@@ -32,38 +43,30 @@ import okhttp3.OkHttpClient;
|
|
|
import okhttp3.Request;
|
|
|
import okhttp3.Response;
|
|
|
|
|
|
-import static com.jiuyan.sheep_children.page.DownloadActivity.DownloadState.DONE;
|
|
|
-import static com.jiuyan.sheep_children.page.DownloadActivity.DownloadState.DOWNLOADING;
|
|
|
-import static com.jiuyan.sheep_children.page.DownloadActivity.DownloadState.PAUSE;
|
|
|
-import static com.jiuyan.sheep_children.page.DownloadActivity.DownloadState.PENDING;
|
|
|
-
|
|
|
|
|
|
/**
|
|
|
* 下载界面
|
|
|
* Created by kemllor on 2017/12/14.
|
|
|
*/
|
|
|
|
|
|
-public class DownloadActivity extends BaseActivity implements View.OnClickListener {
|
|
|
-
|
|
|
-
|
|
|
- private static final String DOWNLOAD_URL = "https://qiniu.kuaifazs.com/fszhskp_12493_haowan_2.1.2.apk";
|
|
|
-
|
|
|
- private OkHttpClient mClint;
|
|
|
+public class DownloadActivity extends BaseActivity {
|
|
|
|
|
|
|
|
|
- private int mTotal;
|
|
|
+ private String DOWNLOAD_URL = "https://qiniu.kuaifazs.com/fszhskp_12493_haowan_2.1.2.apk";
|
|
|
|
|
|
- private static final int DELTA = 4 * 1024 * 1024;
|
|
|
|
|
|
- private OutputStream mDstOutputStream;
|
|
|
-
|
|
|
- private int mCurrentDownload;
|
|
|
-
|
|
|
- private TextView mTvDownload;
|
|
|
+ // private TextView mTvDownload;
|
|
|
private ProgressBar mPbDownload;
|
|
|
+ private ImageView mImgIcon;
|
|
|
+ private TextView mTvDesc;
|
|
|
+ private TextView mTvTitle;
|
|
|
+ private View mBack;
|
|
|
+ private TextView mTvPercent;
|
|
|
+
|
|
|
|
|
|
- private DownloadState mState = PENDING;
|
|
|
private String mPath;
|
|
|
+ private String mApkPath;
|
|
|
+ private BaseDownloadTask mDownloadTask;
|
|
|
|
|
|
@Override
|
|
|
protected void onCreate(@Nullable Bundle savedInstanceState) {
|
|
|
@@ -77,157 +80,104 @@ public class DownloadActivity extends BaseActivity implements View.OnClickListen
|
|
|
|
|
|
@Override
|
|
|
public void initListener() {
|
|
|
- mTvDownload.setOnClickListener(this);
|
|
|
+ mBack.setOnClickListener(new View.OnClickListener() {
|
|
|
+ @Override
|
|
|
+ public void onClick(View v) {
|
|
|
+ finish();
|
|
|
+ }
|
|
|
+ });
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public void initData() {
|
|
|
-
|
|
|
+ mTvTitle.setText("任务下载");
|
|
|
+ Intent intent = getIntent();
|
|
|
+ TaskPublish task = (TaskPublish) intent.getSerializableExtra("data");
|
|
|
+ mPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/"
|
|
|
+ + SheepApp.mContext.getPackageName();
|
|
|
+ mApkPath = new File(mPath, SystemClock.currentThreadTimeMillis() + "_sheepTask.apk").getAbsolutePath();
|
|
|
+ if (task != null) {
|
|
|
+ DOWNLOAD_URL = task.getTask().getDownLoadUrl();
|
|
|
+ mTvDesc.setText(task.getTask().getTitle());
|
|
|
+ Glide.with(this)
|
|
|
+ .load(task.getTask().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() {
|
|
|
- initClient();
|
|
|
findViews();
|
|
|
}
|
|
|
|
|
|
- private void initClient() {
|
|
|
- if (mClint == null) {
|
|
|
- mClint = NetManager.getInstancce(getApplicationContext());
|
|
|
- }
|
|
|
- }
|
|
|
|
|
|
private void findViews() {
|
|
|
- mTvDownload = (TextView) findViewById(R.id.tv_download);
|
|
|
+ mTvPercent = (TextView) findViewById(R.id.tv_percent);
|
|
|
mPbDownload = (ProgressBar) findViewById(R.id.pb_download);
|
|
|
+ mImgIcon = (ImageView) findViewById(R.id.img_icon);
|
|
|
+ mTvDesc = (TextView) findViewById(R.id.tv_desc);
|
|
|
+ mTvTitle = (TextView) findViewById(R.id.tv_title);
|
|
|
+ mBack = findViewById(R.id.img_back);
|
|
|
}
|
|
|
|
|
|
- @Override
|
|
|
- public void onClick(View v) {
|
|
|
- if (R.id.tv_download == v.getId()) {
|
|
|
- switch (mState) {
|
|
|
- case PENDING:
|
|
|
- startDownloadSelf();
|
|
|
- mState = DOWNLOADING;
|
|
|
- break;
|
|
|
- case DOWNLOADING:
|
|
|
- mState = PAUSE;
|
|
|
- break;
|
|
|
- case PAUSE:
|
|
|
- mState = DOWNLOADING;
|
|
|
- downloadRange(mCurrentDownload);
|
|
|
- break;
|
|
|
- case DONE:
|
|
|
- if (!TextUtils.isEmpty(mPath)) {
|
|
|
- installApk(mPath);
|
|
|
- }
|
|
|
- break;
|
|
|
- }
|
|
|
- refreshButton();
|
|
|
- }
|
|
|
- }
|
|
|
|
|
|
- public enum DownloadState {
|
|
|
- PENDING,
|
|
|
- DOWNLOADING,
|
|
|
- PAUSE,
|
|
|
- DONE
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- private void refreshButton() {
|
|
|
- switch (mState) {
|
|
|
- case DOWNLOADING:
|
|
|
- mTvDownload.setText(R.string.pause);
|
|
|
- break;
|
|
|
- case PENDING:
|
|
|
- mTvDownload.setText(R.string.start_download);
|
|
|
- break;
|
|
|
- case PAUSE:
|
|
|
- mTvDownload.setText(R.string.resume);
|
|
|
- break;
|
|
|
- case DONE:
|
|
|
- mTvDownload.setText(R.string.done);
|
|
|
- break;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- //开始下载
|
|
|
- private void startDownloadSelf() {
|
|
|
- getFileLength(DOWNLOAD_URL, new Callback() {
|
|
|
- @Override
|
|
|
- public void onFailure(Call call, IOException e) {
|
|
|
+ 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
|
|
|
- public void onResponse(Call call, Response response) throws IOException {
|
|
|
- if (response.isSuccessful()) {
|
|
|
- //获取下载文件长度
|
|
|
- mTotal = Integer.valueOf(response.header("Content-Length"));
|
|
|
- mPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
|
|
|
- .getAbsolutePath() + "/" + SystemClock.currentThreadTimeMillis() + "sheepTask.apk";
|
|
|
- mDstOutputStream = new FileOutputStream(mPath);
|
|
|
- downloadRange(0);
|
|
|
- }
|
|
|
- }
|
|
|
- });
|
|
|
- }
|
|
|
+ @Override
|
|
|
+ protected void blockComplete(BaseDownloadTask task) {
|
|
|
|
|
|
- //获取文件大小
|
|
|
- private void getFileLength(String url, Callback callback) {
|
|
|
- Request request = new Request.Builder()
|
|
|
- .url(url)
|
|
|
- .method("HEAD", null).build();
|
|
|
+ }
|
|
|
|
|
|
- mClint.newCall(request).enqueue(callback);
|
|
|
- }
|
|
|
+ @Override
|
|
|
+ protected void completed(BaseDownloadTask task) {
|
|
|
+ installApk(mApkPath);
|
|
|
+ mTvPercent.setText("100%");
|
|
|
+ }
|
|
|
|
|
|
- private void downloadRange(int start) {
|
|
|
- Request request = new Request.Builder()
|
|
|
- .url(DOWNLOAD_URL)
|
|
|
- .addHeader("range", "bytes=" + start + "-" + Math.min(start + DELTA, mTotal)).build();
|
|
|
+ @Override
|
|
|
+ protected void paused(BaseDownloadTask task, int soFarBytes, int totalBytes) {
|
|
|
|
|
|
- mClint.newCall(request).enqueue(new Callback() {
|
|
|
- @Override
|
|
|
- public void onFailure(Call call, IOException e) {
|
|
|
+ }
|
|
|
|
|
|
- }
|
|
|
+ @Override
|
|
|
+ protected void error(BaseDownloadTask task, Throwable e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
|
|
|
- @Override
|
|
|
- public void onResponse(Call call, Response response) throws IOException {
|
|
|
- if (response.isSuccessful() && mState == DOWNLOADING) {
|
|
|
- final byte[] bytes = response.body().bytes();
|
|
|
- mDstOutputStream.write(bytes);
|
|
|
- mCurrentDownload += bytes.length;
|
|
|
- mPbDownload.post(new Runnable() {
|
|
|
@Override
|
|
|
- public void run() {
|
|
|
- int progress = (int) (mCurrentDownload * 1.0 / mTotal * 100);
|
|
|
- mPbDownload.setProgress(progress);
|
|
|
-
|
|
|
- if (100 == progress) {
|
|
|
- mState = DONE;
|
|
|
- refreshButton();
|
|
|
- if (!TextUtils.isEmpty(mPath)) {
|
|
|
- installApk(mPath);
|
|
|
- }
|
|
|
- }
|
|
|
+ protected void warn(BaseDownloadTask task) {
|
|
|
}
|
|
|
});
|
|
|
|
|
|
- if (mCurrentDownload >= mTotal) {
|
|
|
- mDstOutputStream.flush();
|
|
|
- mDstOutputStream.close();
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- downloadRange(mCurrentDownload);
|
|
|
- }
|
|
|
- }
|
|
|
- });
|
|
|
+ mDownloadTask.start();
|
|
|
}
|
|
|
|
|
|
+
|
|
|
public void installApk(String path) {
|
|
|
File file = new File(path);
|
|
|
Intent intent = new Intent(Intent.ACTION_VIEW);
|