DownPresenter.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. package com.bgy.autosale.payutil;
  2. import android.app.DownloadManager;
  3. import android.content.Context;
  4. import android.content.Intent;
  5. import android.database.Cursor;
  6. import android.net.Uri;
  7. import android.os.Bundle;
  8. import android.os.Environment;
  9. import android.os.Handler;
  10. import android.os.Message;
  11. import android.text.TextUtils;
  12. import android.util.Log;
  13. import com.hboxs.base_library.base.BaseApplication;
  14. import com.hboxs.base_library.util.FileUtil;
  15. import com.hboxs.base_library.util.LogUtil;
  16. import com.hboxs.base_library.util.SharedPreferencesUtils;
  17. import com.orhanobut.hawk.Hawk;
  18. import java.io.File;
  19. import java.util.HashMap;
  20. import java.util.Map;
  21. public class DownPresenter {
  22. static final String TAG = "DownPresenter";
  23. Context context = BaseApplication.getContext();
  24. private DownloadManager mDownloadManager;
  25. private boolean isRuning = true;
  26. private ProgressUpdate progressUpdate;
  27. private DownPresenter() {
  28. String text = "";
  29. Object systemService = context.getSystemService(Context.DOWNLOAD_SERVICE);
  30. mDownloadManager = (DownloadManager) systemService;
  31. }
  32. private static DownPresenter mDownPresenter;
  33. public static DownPresenter getInstance() {
  34. if (mDownPresenter == null) {
  35. synchronized (DownPresenter.class) {
  36. mDownPresenter = new DownPresenter();
  37. }
  38. }
  39. return mDownPresenter;
  40. }
  41. public void onStartCommand(Intent intent, int flags, int startId) {
  42. if (intent != null) {
  43. Bundle downloadBundle = intent.getBundleExtra("download");
  44. if (downloadBundle != null) {
  45. String downloadUrl = downloadBundle.getString("downloadUrl");
  46. String title = downloadBundle.getString("title");
  47. String downloadtype = downloadBundle.getString("downloadtype");
  48. String name = downloadBundle.getString("download_name");
  49. int screenType = downloadBundle.getInt("scrrentype");
  50. if (downloadtype.equals("apk")) {
  51. //下载apk
  52. String downloadApkUrl = downloadBundle.getString("downloadApkUrl");
  53. if (!TextUtils.isEmpty(downloadApkUrl)) {
  54. long downloadId = downloadApk1(downloadApkUrl, title, downloadtype);
  55. mTaskId = downloadId;
  56. //保存当前taskid
  57. isRuning = true;
  58. startQuery();
  59. SharedPreferencesUtils.setParam("taskid", downloadId);
  60. String filename = (String) SharedPreferencesUtils.getParam("apkname", "");
  61. //保存当前的下载文件名字以及对应的id
  62. Map<Long, String> map = new HashMap<>();
  63. map.put(downloadId, filename);
  64. Hawk.put("download", map);
  65. }
  66. } else {
  67. if (!TextUtils.isEmpty(downloadUrl)) {
  68. //开始下载
  69. long downloadId = downloadAd(downloadUrl, title, downloadtype, screenType);
  70. //保存当前taskid
  71. SharedPreferencesUtils.setParam("taskid", downloadId);
  72. String filename = (String) SharedPreferencesUtils.getParam("filename", "");
  73. //保存当前的下载文件名字以及对应的id
  74. Map<Long, String> map = new HashMap<>();
  75. map.put(downloadId, filename);
  76. Hawk.put("download", map);
  77. }
  78. }
  79. }
  80. }
  81. }
  82. /**
  83. * 下载广告数据
  84. *
  85. * @param url
  86. * @param title
  87. * @return 下载taskid
  88. */
  89. private long downloadAd(String url, String title, String downloadtype, int screenType) {
  90. LogUtil.d(TAG, "downloadAd: url=" + url);
  91. Uri downloadUri = Uri.parse(url);
  92. LogUtil.d(TAG, "downloadAd: getPath=" + downloadUri.getPath());
  93. DownloadManager.Request request = new DownloadManager.Request(downloadUri);
  94. String apkName = title + "." + downloadtype;
  95. DownloadManager mDownloadManager = null;
  96. if (screenType == 0) {
  97. Hawk.put("screen_type", 0);
  98. LogUtil.d(TAG, "downloadAd: 下载:" + apkName);
  99. File externalFilesDir = new File("/ad");
  100. if (externalFilesDir == null || !externalFilesDir.exists()) {
  101. externalFilesDir.mkdir();
  102. }
  103. File file = new File(Environment.getExternalStorageDirectory().getPath() + "/ad/" + apkName);
  104. if (file != null && file.exists()) {
  105. file.delete();
  106. }
  107. request.setDestinationInExternalPublicDir("/ad", apkName);
  108. //保存当前名字
  109. SharedPreferencesUtils.setParam("filename", apkName);
  110. // request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
  111. request.setVisibleInDownloadsUi(true);
  112. request.setTitle(title);
  113. mDownloadManager = (DownloadManager) context.getSystemService(context.DOWNLOAD_SERVICE);
  114. } else {
  115. Hawk.put("screen_type", 1);
  116. File externalFilesDir = new File("/adB");
  117. if (externalFilesDir == null || !externalFilesDir.exists()) {
  118. externalFilesDir.mkdir();
  119. }
  120. // File externalFilesDir = context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
  121. // if (externalFilesDir == null || !externalFilesDir.exists()) {
  122. // externalFilesDir.mkdir();
  123. // }
  124. // File file = context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS + "/" + apkName);
  125. File file = new File(Environment.getExternalStorageDirectory().getPath() + "/adB/" + apkName);
  126. if (file != null && file.exists()) {
  127. file.delete();
  128. }
  129. LogUtil.d(TAG, "downloadAd: 下载:" + apkName);
  130. // request.setDestinationInExternalFilesDir(context, Environment.DIRECTORY_DOWNLOADS,
  131. // apkName);
  132. request.setDestinationInExternalPublicDir("/adB", apkName);
  133. //保存当前名字
  134. SharedPreferencesUtils.setParam("filename", apkName);
  135. // request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
  136. request.setVisibleInDownloadsUi(true);
  137. request.setTitle(title);
  138. mDownloadManager = (DownloadManager) context.getSystemService(context.DOWNLOAD_SERVICE);
  139. }
  140. return mDownloadManager.enqueue(request);
  141. }
  142. public void remove() {
  143. mDownloadManager.remove(mTaskId);
  144. }
  145. /**
  146. * 下载apk
  147. *
  148. * @param url
  149. * @param title
  150. * @return 下载taskid
  151. */
  152. private long downloadApk1(String url, String title, String downloadtype) {
  153. Uri downloadUri = Uri.parse(url);
  154. DownloadManager.Request request = new DownloadManager.Request(downloadUri);
  155. String apkName = "ice" + "." + "apk";
  156. File externalFilesDir = context.getExternalFilesDir("apk");
  157. File file1 = new File(Environment.getExternalStorageDirectory().getPath() + "/apk/");
  158. if (file1.exists()) {
  159. FileUtil.deleteFile(file1);
  160. }
  161. if (externalFilesDir == null || !externalFilesDir.exists()) {
  162. externalFilesDir.mkdir();
  163. }
  164. File file = context.getExternalFilesDir("apk" + "/" + "ice.apk");
  165. Log.d(TAG, "downloadApk1: 下载:" + file.getAbsolutePath());
  166. if (file != null && file.exists()) {
  167. file.delete();
  168. }
  169. request.setDestinationInExternalPublicDir("/apk", apkName);
  170. //保存当前名字
  171. SharedPreferencesUtils.setParam("apkname", apkName);
  172. request.setVisibleInDownloadsUi(true);
  173. request.setTitle(title);
  174. DownloadManager mDownloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
  175. Log.d(TAG, "downloadApk1: 下载apk");
  176. long enqueue = mDownloadManager.enqueue(request);
  177. startQuery();
  178. return enqueue;
  179. }
  180. private long mTaskId;
  181. //apk 下载进度监听
  182. private final QueryRunnable mQueryProgressRunnable = new QueryRunnable();
  183. private final Handler mHandler = new Handler() {
  184. @Override
  185. public void handleMessage(Message msg) {
  186. super.handleMessage(msg);
  187. if (msg.what == 1001) {
  188. Log.d(TAG, "handleMessage msg.arg1: " + msg.arg1);
  189. Log.d(TAG, "handleMessage msg.arg2: " + msg.arg2);
  190. if (progressUpdate != null) {
  191. Log.d(TAG, "handleMessage: ");
  192. progressUpdate.onProgressUpdate(msg.arg1, msg.arg2);
  193. }
  194. }
  195. }
  196. };
  197. //查询下载进度
  198. private class QueryRunnable implements Runnable {
  199. @Override
  200. public void run() {
  201. queryState();
  202. Log.d(TAG, "QueryRunnablerun: ");
  203. if (isRuning) {
  204. mHandler.postDelayed(mQueryProgressRunnable, 100);
  205. }
  206. }
  207. }
  208. //更新下载进度
  209. private void startQuery() {
  210. if (mTaskId != 0) {
  211. mHandler.post(mQueryProgressRunnable);
  212. Log.d(TAG, "startQuery: ");
  213. }
  214. }
  215. //查询下载进度
  216. private void queryState() {
  217. // 通过ID向下载管理查询下载情况,返回一个cursor
  218. Cursor c = mDownloadManager.query(new DownloadManager.Query().setFilterById(mTaskId));
  219. if (c != null && c.moveToFirst()) {
  220. int columnIndex = c.getColumnIndex(DownloadManager.COLUMN_STATUS);
  221. int status = c.getInt(columnIndex);
  222. switch (status) {
  223. case DownloadManager.STATUS_RUNNING:
  224. Log.d(TAG, "queryState: 下载中");
  225. break;
  226. case DownloadManager.STATUS_SUCCESSFUL:
  227. Log.d(TAG, "queryState: 下载成功");
  228. stopQuery();
  229. break;
  230. //下载失败
  231. case DownloadManager.STATUS_FAILED:
  232. Log.d(TAG, "queryState: 下载失败");
  233. break;
  234. }
  235. }
  236. if (c == null) {
  237. } else { // 以下是从游标中进行信息提取
  238. if (!c.moveToFirst()) {
  239. if (!c.isClosed()) {
  240. c.close();
  241. }
  242. return;
  243. }
  244. int mDownload_so_far = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR));
  245. int mDownload_all = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES));
  246. Message msg = Message.obtain();
  247. if (mDownload_all > 0) {
  248. msg.what = 1001;
  249. msg.arg1 = mDownload_so_far;
  250. msg.arg2 = mDownload_all;
  251. mHandler.sendMessage(msg);
  252. }
  253. if (!c.isClosed()) {
  254. c.close();
  255. }
  256. }
  257. }
  258. //停止查询下载进度
  259. private void stopQuery() {
  260. isRuning = false;
  261. mHandler.removeCallbacks(mQueryProgressRunnable);
  262. }
  263. public void setProgressUpdate(ProgressUpdate update) {
  264. this.progressUpdate = update;
  265. }
  266. //进度更新
  267. public interface ProgressUpdate {
  268. void onProgressUpdate(double mDownload_so_far, double mDownload_all);
  269. }
  270. }