123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294 |
- package com.bgy.autosale.payutil;
- import android.app.DownloadManager;
- import android.content.Context;
- import android.content.Intent;
- import android.database.Cursor;
- import android.net.Uri;
- import android.os.Bundle;
- import android.os.Environment;
- import android.os.Handler;
- import android.os.Message;
- import android.text.TextUtils;
- import android.util.Log;
- import com.hboxs.base_library.base.BaseApplication;
- import com.hboxs.base_library.util.FileUtil;
- import com.hboxs.base_library.util.LogUtil;
- import com.hboxs.base_library.util.SharedPreferencesUtils;
- import com.orhanobut.hawk.Hawk;
- import java.io.File;
- import java.util.HashMap;
- import java.util.Map;
- public class DownPresenter {
- static final String TAG = "DownPresenter";
- Context context = BaseApplication.getContext();
- private DownloadManager mDownloadManager;
- private boolean isRuning = true;
- private ProgressUpdate progressUpdate;
- private DownPresenter() {
- String text = "";
- Object systemService = context.getSystemService(Context.DOWNLOAD_SERVICE);
- mDownloadManager = (DownloadManager) systemService;
- }
- private static DownPresenter mDownPresenter;
- public static DownPresenter getInstance() {
- if (mDownPresenter == null) {
- synchronized (DownPresenter.class) {
- mDownPresenter = new DownPresenter();
- }
- }
- return mDownPresenter;
- }
- public void onStartCommand(Intent intent, int flags, int startId) {
- if (intent != null) {
- Bundle downloadBundle = intent.getBundleExtra("download");
- if (downloadBundle != null) {
- String downloadUrl = downloadBundle.getString("downloadUrl");
- String title = downloadBundle.getString("title");
- String downloadtype = downloadBundle.getString("downloadtype");
- String name = downloadBundle.getString("download_name");
- int screenType = downloadBundle.getInt("scrrentype");
- if (downloadtype.equals("apk")) {
- //下载apk
- String downloadApkUrl = downloadBundle.getString("downloadApkUrl");
- if (!TextUtils.isEmpty(downloadApkUrl)) {
- long downloadId = downloadApk1(downloadApkUrl, title, downloadtype);
- mTaskId = downloadId;
- //保存当前taskid
- isRuning = true;
- startQuery();
- SharedPreferencesUtils.setParam("taskid", downloadId);
- String filename = (String) SharedPreferencesUtils.getParam("apkname", "");
- //保存当前的下载文件名字以及对应的id
- Map<Long, String> map = new HashMap<>();
- map.put(downloadId, filename);
- Hawk.put("download", map);
- }
- } else {
- if (!TextUtils.isEmpty(downloadUrl)) {
- //开始下载
- long downloadId = downloadAd(downloadUrl, title, downloadtype, screenType);
- //保存当前taskid
- SharedPreferencesUtils.setParam("taskid", downloadId);
- String filename = (String) SharedPreferencesUtils.getParam("filename", "");
- //保存当前的下载文件名字以及对应的id
- Map<Long, String> map = new HashMap<>();
- map.put(downloadId, filename);
- Hawk.put("download", map);
- }
- }
- }
- }
- }
- /**
- * 下载广告数据
- *
- * @param url
- * @param title
- * @return 下载taskid
- */
- private long downloadAd(String url, String title, String downloadtype, int screenType) {
- LogUtil.d(TAG, "downloadAd: url=" + url);
- Uri downloadUri = Uri.parse(url);
- LogUtil.d(TAG, "downloadAd: getPath=" + downloadUri.getPath());
- DownloadManager.Request request = new DownloadManager.Request(downloadUri);
- String apkName = title + "." + downloadtype;
- DownloadManager mDownloadManager = null;
- if (screenType == 0) {
- Hawk.put("screen_type", 0);
- LogUtil.d(TAG, "downloadAd: 下载:" + apkName);
- File externalFilesDir = new File("/ad");
- if (externalFilesDir == null || !externalFilesDir.exists()) {
- externalFilesDir.mkdir();
- }
- File file = new File(Environment.getExternalStorageDirectory().getPath() + "/ad/" + apkName);
- if (file != null && file.exists()) {
- file.delete();
- }
- request.setDestinationInExternalPublicDir("/ad", apkName);
- //保存当前名字
- SharedPreferencesUtils.setParam("filename", apkName);
- // request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
- request.setVisibleInDownloadsUi(true);
- request.setTitle(title);
- mDownloadManager = (DownloadManager) context.getSystemService(context.DOWNLOAD_SERVICE);
- } else {
- Hawk.put("screen_type", 1);
- File externalFilesDir = new File("/adB");
- if (externalFilesDir == null || !externalFilesDir.exists()) {
- externalFilesDir.mkdir();
- }
- // File externalFilesDir = context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
- // if (externalFilesDir == null || !externalFilesDir.exists()) {
- // externalFilesDir.mkdir();
- // }
- // File file = context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS + "/" + apkName);
- File file = new File(Environment.getExternalStorageDirectory().getPath() + "/adB/" + apkName);
- if (file != null && file.exists()) {
- file.delete();
- }
- LogUtil.d(TAG, "downloadAd: 下载:" + apkName);
- // request.setDestinationInExternalFilesDir(context, Environment.DIRECTORY_DOWNLOADS,
- // apkName);
- request.setDestinationInExternalPublicDir("/adB", apkName);
- //保存当前名字
- SharedPreferencesUtils.setParam("filename", apkName);
- // request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
- request.setVisibleInDownloadsUi(true);
- request.setTitle(title);
- mDownloadManager = (DownloadManager) context.getSystemService(context.DOWNLOAD_SERVICE);
- }
- return mDownloadManager.enqueue(request);
- }
- public void remove() {
- mDownloadManager.remove(mTaskId);
- }
- /**
- * 下载apk
- *
- * @param url
- * @param title
- * @return 下载taskid
- */
- private long downloadApk1(String url, String title, String downloadtype) {
- Uri downloadUri = Uri.parse(url);
- DownloadManager.Request request = new DownloadManager.Request(downloadUri);
- String apkName = "ice" + "." + "apk";
- File externalFilesDir = context.getExternalFilesDir("apk");
- File file1 = new File(Environment.getExternalStorageDirectory().getPath() + "/apk/");
- if (file1.exists()) {
- FileUtil.deleteFile(file1);
- }
- if (externalFilesDir == null || !externalFilesDir.exists()) {
- externalFilesDir.mkdir();
- }
- File file = context.getExternalFilesDir("apk" + "/" + "ice.apk");
- Log.d(TAG, "downloadApk1: 下载:" + file.getAbsolutePath());
- if (file != null && file.exists()) {
- file.delete();
- }
- request.setDestinationInExternalPublicDir("/apk", apkName);
- //保存当前名字
- SharedPreferencesUtils.setParam("apkname", apkName);
- request.setVisibleInDownloadsUi(true);
- request.setTitle(title);
- DownloadManager mDownloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
- Log.d(TAG, "downloadApk1: 下载apk");
- long enqueue = mDownloadManager.enqueue(request);
- startQuery();
- return enqueue;
- }
- private long mTaskId;
- //apk 下载进度监听
- private final QueryRunnable mQueryProgressRunnable = new QueryRunnable();
- private final Handler mHandler = new Handler() {
- @Override
- public void handleMessage(Message msg) {
- super.handleMessage(msg);
- if (msg.what == 1001) {
- Log.d(TAG, "handleMessage msg.arg1: " + msg.arg1);
- Log.d(TAG, "handleMessage msg.arg2: " + msg.arg2);
- if (progressUpdate != null) {
- Log.d(TAG, "handleMessage: ");
- progressUpdate.onProgressUpdate(msg.arg1, msg.arg2);
- }
- }
- }
- };
- //查询下载进度
- private class QueryRunnable implements Runnable {
- @Override
- public void run() {
- queryState();
- Log.d(TAG, "QueryRunnablerun: ");
- if (isRuning) {
- mHandler.postDelayed(mQueryProgressRunnable, 100);
- }
- }
- }
- //更新下载进度
- private void startQuery() {
- if (mTaskId != 0) {
- mHandler.post(mQueryProgressRunnable);
- Log.d(TAG, "startQuery: ");
- }
- }
- //查询下载进度
- private void queryState() {
- // 通过ID向下载管理查询下载情况,返回一个cursor
- Cursor c = mDownloadManager.query(new DownloadManager.Query().setFilterById(mTaskId));
- if (c != null && c.moveToFirst()) {
- int columnIndex = c.getColumnIndex(DownloadManager.COLUMN_STATUS);
- int status = c.getInt(columnIndex);
- switch (status) {
- case DownloadManager.STATUS_RUNNING:
- Log.d(TAG, "queryState: 下载中");
- break;
- case DownloadManager.STATUS_SUCCESSFUL:
- Log.d(TAG, "queryState: 下载成功");
- stopQuery();
- break;
- //下载失败
- case DownloadManager.STATUS_FAILED:
- Log.d(TAG, "queryState: 下载失败");
- break;
- }
- }
- if (c == null) {
- } else { // 以下是从游标中进行信息提取
- if (!c.moveToFirst()) {
- if (!c.isClosed()) {
- c.close();
- }
- return;
- }
- int mDownload_so_far = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR));
- int mDownload_all = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES));
- Message msg = Message.obtain();
- if (mDownload_all > 0) {
- msg.what = 1001;
- msg.arg1 = mDownload_so_far;
- msg.arg2 = mDownload_all;
- mHandler.sendMessage(msg);
- }
- if (!c.isClosed()) {
- c.close();
- }
- }
- }
- //停止查询下载进度
- private void stopQuery() {
- isRuning = false;
- mHandler.removeCallbacks(mQueryProgressRunnable);
- }
- public void setProgressUpdate(ProgressUpdate update) {
- this.progressUpdate = update;
- }
- //进度更新
- public interface ProgressUpdate {
- void onProgressUpdate(double mDownload_so_far, double mDownload_all);
- }
- }
|