package com.weikou.beibeivideo.util.downutil;
|
|
import android.content.Context;
|
import android.content.Intent;
|
import android.net.Uri;
|
import android.os.AsyncTask;
|
import android.os.Build;
|
import android.util.Log;
|
|
import com.weikou.beibeivideo.BuildConfig;
|
import com.weikou.beibeivideo.entity.AdType;
|
import com.weikou.beibeivideo.service.DownLoadFileService;
|
|
import org.greenrobot.eventbus.EventBus;
|
|
import java.io.File;
|
|
import androidx.core.content.FileProvider;
|
|
/**
|
* @author weikou2015 下载工具类
|
*/
|
public class DownLoadApks extends AsyncTask<String, Integer, String> implements
|
DownLoadFile.FileProgressListener {
|
private static final String TAG = "DownLoadApks";
|
// TextView tv;
|
Context context;
|
IProgress progress;
|
String apkType;
|
|
public DownLoadApks(Context context, IProgress progress, String apkType) {
|
// this.tv = tv;
|
this.context = context;
|
this.progress = progress;
|
this.apkType = apkType;
|
}
|
|
int sopro = 0;
|
|
@Override
|
protected void onProgressUpdate(Integer... values) {
|
super.onProgressUpdate(values);
|
Log.i(TAG, "下载进度:" + values[0]);
|
if (progress != null) {
|
if (isSo) {
|
AdType adType = new AdType();
|
adType.setProgress(values[0]);
|
EventBus.getDefault().post(adType);
|
}
|
progress.getProgress(values[0]);
|
}
|
// if (values[0] == 100) {
|
// tv.setText("完成");
|
// } else {
|
// tv.setText("下载" + values[0] + "%");
|
// }
|
|
}
|
|
private boolean isSo = false;
|
|
@Override
|
protected String doInBackground(String... params) {
|
String url = params[0];
|
// 下载
|
DownLoadFile dl = new DownLoadFile();
|
String[] urls = url.split("/");
|
String name = "";
|
if (urls.length > 0)
|
if (urls[urls.length - 1].contains(".so")) {
|
name = FileUtils.getAppRootPath(context) + File.separator
|
+ urls[urls.length - 1];
|
} else {
|
name = FileUtils.getRootPath(context) + File.separator
|
+ urls[urls.length - 1];
|
}
|
else
|
name = FileUtils.getRootPath(context) + File.separator
|
+ System.currentTimeMillis() + ".apk";
|
System.out.println("APK名称:" + name);
|
Log.i(TAG, "APK名称:" + urls[urls.length - 1]);
|
if (name.contains(".so"))
|
isSo = true;
|
try {
|
File f = dl.downLoadFile(this, name, url, context);
|
return f.getPath();
|
} catch (Exception e) {
|
DownLoadFileService.j = -1;
|
}
|
return "";
|
}
|
|
// 完成网络请求
|
@Override
|
protected void onPostExecute(String result) {
|
super.onPostExecute(result);
|
if ((!ApkUtil.checkAPP(context, apkType)) && (!isSo)) {
|
Intent intent = new Intent(Intent.ACTION_VIEW);
|
File file = new File(result);
|
|
//判断是否是AndroidN以及更高的版本
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
|
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
|
Uri contentUri = FileProvider.getUriForFile(context.getApplicationContext(), BuildConfig.APPLICATION_ID + ".fileprovider", file);
|
intent.setDataAndType(contentUri, "application/vnd.android.package-archive");
|
} else {
|
Uri uri = Uri.fromFile(file);
|
intent.setDataAndType(uri, "application/vnd.android.package-archive");
|
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
}
|
context.startActivity(intent);
|
}
|
}
|
|
@Override
|
public void update(int progress) {
|
publishProgress(progress);
|
}
|
|
public interface IProgress {
|
public void getProgress(int p);
|
|
}
|
}
|