我是靠谱客的博主 自然发卡,这篇文章主要介绍android 多线程下载器 源码解析,现在分享给大家,希望可以做个参考。

               以前一直没有时间整理自己封装的类库。本篇博文跟大家介绍下之前自己写的一款android 平台的下载器。

               先介绍下下载器实现的功能,多线程下载,断点续传,多任务通知栏排队下载,wifi 拉包,联网自动恢复下载任务,非wifi 状态预定下载任务,定期清理下载缓存,多数据库存储之间的数据迁移。

              demo 的运行效果。

            

demo 代码解读

首先先分析下demo 的代码:

       

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
package com.downloader.sdk.dlplugin.demo; import android.app.Activity; import android.net.Proxy; import android.os.Bundle; import android.support.v4.view.PagerAdapter; import android.support.v4.view.ViewPager; import android.support.v4.view.ViewPager.OnPageChangeListener; import android.util.DisplayMetrics; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.view.animation.TranslateAnimation; import android.widget.Button; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.ListView; import com.downloader.sdk.dlplugin.DownLoadCallback; import com.downloader.sdk.dlplugin.DownloadManager; import com.downloader.sdk.dlplugin.DownloadStrategy; import com.downloader.sdk.dlplugin.DownloadTask; import com.downloader.sdk.dlplugin.DownloadTaskStatus; import com.downloader.sdk.dlplugin.demo.adapter.DownloadedListAdapter; import com.downloader.sdk.dlplugin.demo.adapter.DownloadingListAdapter; import com.downloader.sdk.dlplugin.demo.data.DownloadUrls; import org.json.JSONObject; import java.io.File; import java.util.ArrayList; public class DownLoadPluginActivtiy extends Activity { private Button addButton = null; private Button resetButton = null; private Button stopButton = null; private DownloadingListAdapter downloadListAdapter; private DownloadedListAdapter downloadedListAdapter; private int urlIndex = 0; //pageInditator private View v_downloading, v_downloaded;// 需要显示的View private ListView lv_downloading, lv_downloaded; private LinearLayout ll_downloading, ll_downloaded; private ViewPager viewPager; private TranslateAnimation transAnima; private ImageView indicator = null; private ArrayList<View> views; private int currentIndex = 0; int width; private DownloadManager downloadManager; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.dlface); downloadManager = DownloadManager.getInstance(this); initView(); } private void initView() { DisplayMetrics dms = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(dms); width = dms.widthPixels / 2 - 50; indicator = (ImageView) findViewById(R.id.iv_line); LayoutInflater lf = getLayoutInflater().from(this); v_downloading = lf.inflate(R.layout.downloading_tab, null); v_downloaded = lf.inflate(R.layout.downloaded_tab, null); views = new ArrayList<View>(); views.add(v_downloading); views.add(v_downloaded); ll_downloading = (LinearLayout) findViewById(R.id.ll_downloading); ll_downloaded = (LinearLayout) findViewById(R.id.ll_downloaded); ll_downloading.setOnClickListener(tabClickHandler); ll_downloaded.setOnClickListener(tabClickHandler); lv_downloading = (ListView) v_downloading.findViewById(R.id.lv_downloading); lv_downloaded = (ListView) v_downloaded.findViewById(R.id.lv_downloaded); downloadListAdapter = DownloadingListAdapter.getInstance().init(this, lv_downloading); downloadedListAdapter = DownloadedListAdapter.getInstance().init(this, lv_downloaded); lv_downloading.setAdapter(downloadListAdapter); lv_downloaded.setAdapter(downloadedListAdapter); addButton = (Button) v_downloading.findViewById(R.id.btn_add); resetButton = (Button) v_downloading.findViewById(R.id.btn_reset); addButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { /* * 入口 * 1、生成taskId * 2、插入数据库记录 * 3、添加下载任务 * */ try { JSONObject downloadSteategyJsonObj = new JSONObject(); downloadSteategyJsonObj.put(DownloadStrategy.SEGMENT_KEY, 5); downloadSteategyJsonObj.put(DownloadStrategy.VISIBILITY_KEY, 0); DownloadTask task = downloadManager.postTask(DownloadUrls.url[urlIndex], DownloadUrls.apkName[urlIndex], null, downloadSteategyJsonObj); if (task != null && task.getId() >= 0 && task.getStatus() == DownloadTaskStatus.WAITING) { downloadListAdapter.addItem(task.getId(), DownloadUrls.url[urlIndex], false); } urlIndex++; downloadManager.setDownLoadCallback(new DownLoadCallback() { @Override public void onStart(long taskId, String url) { super.onStart(taskId, url); Log.e("YCW", "onStart-taskId--->" + taskId + ";url--->" + url); } @Override public void onFailure(long taskId, String url, int erro) { super.onFailure(taskId, url, erro); Log.e("YCW", "onFailure-taskId--->" + taskId + ";url--->" + url); } @Override public void onLoading(long taskId, String url, long totalSize, long currentSize, long speed, int percent) { super.onLoading(taskId, url, totalSize, currentSize, speed, percent); Log.e("YCW", "onLoading-taskId--->" + taskId + ";url--->" + url + ";percent--->" + percent); downloadListAdapter.setDownLoaderProgress(url, totalSize, currentSize, speed); } @Override public void onFinish(long taskId, String url) { super.onFinish(taskId, url); Log.e("YCW", "onFinish-taskId--->" + taskId + ";url--->" + url); } }); } catch (Exception e) { e.printStackTrace(); } } }); resetButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { /* * 0、清空数据库 * 1、清空队列 * 2、重置url索引 * 3、清空下载文件 * */ downloadListAdapter.reInit(); downloadListAdapter = DownloadingListAdapter.getInstance().init(v.getContext(), lv_downloading); lv_downloading.setAdapter(downloadListAdapter); downloadManager.deleteAllHandler(); downloadListAdapter.reInit(); downloadedListAdapter.reset(); deleteAllFiles(new File("/sdcard/adsagedlplugin/")); urlIndex = 0; long a = 10L; int bb = new Long(a).intValue(); String proxyHost = Proxy.getHost(v.getContext()); int proxyPort = Proxy.getPort(v.getContext()); } }); viewPager = (ViewPager) findViewById(R.id.viewpager); viewPager.setAdapter(new MyPageAdapter()); viewPager.setOnPageChangeListener(new OnPageChangeListener() { @Override public void onPageSelected(int arg0) { initAnimation(arg0); } @Override public void onPageScrolled(int arg0, float arg1, int arg2) { } @Override public void onPageScrollStateChanged(int arg0) { } }); } OnClickListener tabClickHandler = new OnClickListener() { @Override public void onClick(View v) { int tempIdx = 0; if (v == ll_downloading) { tempIdx = 0; } else if (v == ll_downloaded) { tempIdx = 1; } if (currentIndex != tempIdx) { initAnimation(tempIdx); viewPager.setCurrentItem(currentIndex); } } }; private void initAnimation(int moveto) { transAnima = new TranslateAnimation(currentIndex * width, moveto * width, 0, 0); transAnima.setFillAfter(true); transAnima.setDuration(200); indicator.startAnimation(transAnima); currentIndex = moveto; } private void deleteAllFiles(File root) { File files[] = root.listFiles(); if (files != null) for (File f : files) { if (f.isDirectory()) { // 判断是否为文件夹 deleteAllFiles(f); try { f.delete(); } catch (Exception e) { } } else { if (f.exists()) { // 判断是否存在 deleteAllFiles(f); try { f.delete(); } catch (Exception e) { } } } } } private class MyPageAdapter extends PagerAdapter { @Override public Object instantiateItem(ViewGroup container, int position) { container.addView(views.get(position)); return views.get(position); } @Override public int getCount() { return views.size(); } @Override public boolean isViewFromObject(View arg0, Object arg1) { return arg0 == arg1; } @Override public void destroyItem(ViewGroup container, int position, Object object) { container.removeView(views.get(position)); } @Override public int getItemPosition(Object object) { return super.getItemPosition(object); } } }

 

该demo 主要演示了如何获取DownLoadManager 的实例,如何设置回调监听,

 

复制代码
1
downloadManager = DownloadManager.getInstance(this);

获取DownloadManager 实例。

 

复制代码
1
2
3
4
JSONObject downloadSteategyJsonObj = new JSONObject(); downloadSteategyJsonObj.put(DownloadStrategy.SEGMENT_KEY, 5); downloadSteategyJsonObj.put(DownloadStrategy.VISIBILITY_KEY, 0); DownloadTask task = downloadManager.postTask(DownloadUrls.url[urlIndex], DownloadUrls.apkName[urlIndex], null, downloadSteategyJsonObj);

通过postTask 添加下载任务

 

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
downloadManager.setDownLoadCallback(new DownLoadCallback() { @Override public void onStart(long taskId, String url) { super.onStart(taskId, url); Log.e("YCW", "onStart-taskId--->" + taskId + ";url--->" + url); } @Override public void onFailure(long taskId, String url, int erro) { super.onFailure(taskId, url, erro); Log.e("YCW", "onFailure-taskId--->" + taskId + ";url--->" + url); } @Override public void onLoading(long taskId, String url, long totalSize, long currentSize, long speed, int percent) { super.onLoading(taskId, url, totalSize, currentSize, speed, percent); Log.e("YCW", "onLoading-taskId--->" + taskId + ";url--->" + url + ";percent--->" + percent); downloadListAdapter.setDownLoaderProgress(url, totalSize, currentSize, speed); } @Override public void onFinish(long taskId, String url) { super.onFinish(taskId, url); Log.e("YCW", "onFinish-taskId--->" + taskId + ";url--->" + url); } }); } catch (Exception e) { e.printStackTrace(); }

设置回调监听。

onLoading 进度回调,onFinish 下载完成回调,onFailure 下载错误回调。

下载器的目录结构:

              

   下载器共包含32个类。

   DownLoaderManager 是下载器的对外接口类,和下载worker 工作队列,线程并发的处理类。

   DownLoadCallBack 下载器的下载监听回调

   DownLoadConfig  常量配置

   DownloadStrategy 下载策略,包括进度的部长,下载根目录,下载是否显示通知栏进度条等

   DownLoadTask   下载任务,数据库sqlite 的实体bean

 

复制代码
1
DownloadTaskStatus 下载状态

   下面看着源码讲解下这个类:

 

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
package com.downloader.sdk.dlplugin; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; 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 android.webkit.MimeTypeMap; import android.widget.Toast; import com.downloader.sdk.dlplugin.util.common.ApkUtil; import com.downloader.sdk.dlplugin.util.common.FileInfoUtils; import com.downloader.sdk.dlplugin.util.common.SdCardUtil; import com.downloader.sdk.dlplugin.util.common.StringUtils; import com.downloader.sdk.dlplugin.util.db.DBUtil; import com.downloader.sdk.dlplugin.util.http.AsyncHttpClient; import com.downloader.sdk.dlplugin.util.http.AsyncHttpResponseHandler; import com.downloader.sdk.dlplugin.util.http.FileHttpResponseHandler; import com.downloader.sdk.dlplugin.util.log.Logger; import com.downloader.sdk.dlplugin.util.netstate.NetChangeObserver; import com.downloader.sdk.dlplugin.util.netstate.NetWorkUtil; import com.downloader.sdk.dlplugin.util.netstate.NetWorkUtil.netType; import com.downloader.sdk.dlplugin.util.netstate.NetworkStateReceiver; import com.downloader.sdk.dlplugin.util.notification.NotificationHelper; import org.apache.http.Header; import org.apache.http.HttpResponse; import org.apache.http.HttpStatus; import org.apache.http.ProtocolException; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.RedirectHandler; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.params.HttpClientParams; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.params.HttpParams; import org.apache.http.protocol.HttpContext; import org.json.JSONException; import org.json.JSONObject; import java.io.File; import java.io.IOException; import java.net.MalformedURLException; import java.net.URI; import java.net.URISyntaxException; import java.util.ArrayList; import java.util.HashMap; import java.util.LinkedList; import java.util.List; import java.util.Queue; /** * @author ycw */ public class DownloadManager extends Thread { private HandlerQueue mhandlerQueue;// queue for waiting task private static List<AsyncHttpResponseHandler> mDownloadinghandlers;// store private static List<AsyncHttpResponseHandler> mPausinghandlers;// store private AsyncHttpClient mAsyncHttpClient; private Boolean isRunning = false; private DownLoadCallback mDownLoadCallback; private String mRootPath = ""; // cache all realtime task status private ArrayList<DownloadTaskStatus> mDownloadTaskStatusData; private HashMap<Long, DownloadStrategy> mDownloadStrategyHashMap; private NotificationHelper mNotificationHelper; private Context mContext; private DBUtil mDbutil; private static DownloadManager mDownloadManager; private final static String TAG = "DownloadManager"; public static final String NOTIFICATION_CLICK_ACTION = "com.downloader.notification_click_action"; public static final String NOFINISH_NOTIFICATION_CLICK_ACTION = "com.downloader.notification_nofinishclick_action"; private static BroadcastReceiver mNotifactionClickReceiver; private boolean mIsWorking = true; private static BroadcastReceiver sSdCardReceiver; private static String mDataBaseName; private static boolean mIsSaveOldData; private static int mDbHashCode; private static Handler myHandler; public static DownloadManager getInstance(Context context) { if (mDownloadManager == null) { mDownloadManager = new DownloadManager(context, DownLoadConfig.FILE_ROOT, "mftour_download", false); mDataBaseName = "mftour_download"; } return mDownloadManager; } /** * @param context * @param dataBaseName * @param downLoadPath * @param isSaveOldData * @return */ public static DownloadManager getInstance(Context context, String dataBaseName, String downLoadPath, boolean isSaveOldData) { Logger.d(TAG, "getInstance(" + "context," + dataBaseName + "," + downLoadPath + "," + isSaveOldData + ")"); if (!TextUtils.isEmpty(downLoadPath)) { DownLoadConfig.FILE_ROOT = downLoadPath; } if (mDownloadManager == null) { mDownloadManager = new DownloadManager(context, DownLoadConfig.FILE_ROOT, dataBaseName, isSaveOldData); mDataBaseName = dataBaseName; } return mDownloadManager; } private DownloadManager(Context context, String rootPath, String dataBaseName, boolean isSaveOldDataBase) { initParams(context, rootPath, dataBaseName, isSaveOldDataBase); // 获取cup颗数,动态设置并发数 setWorkerCount(); sSdCardReceiver = getSdReceiver(); // 注册sd 监听器 registerSdReceiver(sSdCardReceiver); // sd 卡监听 // register network state receiver NetworkStateReceiver.registerNetworkStateReceiver(mContext); NetworkStateReceiver.registerObserver(new NetChangeObserver() { @SuppressWarnings("static-access") @Override public void onConnect(netType type) { mIsWorking = true; if (null != NetWorkUtil.getAPNType(mContext) && NetWorkUtil.getAPNType(mContext) != type.wifi) { // 说明是移动网络,延时,改变重试次数 if (mAsyncHttpClient != null) { mAsyncHttpClient .setTimeout(DownLoadConfig.unWifiTimeOut); } } else { if (mAsyncHttpClient != null) { mAsyncHttpClient.setTimeout(DownLoadConfig.wifiTimeOut); } } showToast("网络已连接"); continueAllHandler(); } @Override public void onDisConnect() { mIsWorking = false; showToast("网络已断开"); pauseAllHandler(); } }); // register notification click action recevier if (mNotifactionClickReceiver == null) { mNotifactionClickReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { if (intent.getAction().equalsIgnoreCase( NOTIFICATION_CLICK_ACTION)) { Bundle extras = intent.getExtras(); long mTaskId = extras.getLong("taskid"); String mTaskUrl = extras.getString("taskurl"); sendOnClickMessageToCallback(mTaskId, mTaskUrl); } else if (intent.getAction().equalsIgnoreCase( NOFINISH_NOTIFICATION_CLICK_ACTION)) { Bundle extras = intent.getExtras(); long taskId = extras.getLong("taskid"); String taskUrl = extras.getString("taskurl"); pauseOrContinue(taskId, taskUrl); } } }; } registerNotificationClickReceiver(mNotifactionClickReceiver); if (!StringUtils.isEmpty(rootPath)) { File rootFile = new File(rootPath); if (!rootFile.exists()) { rootFile.mkdir(); } } cleanOutdatedDownloadedRecords(); } private BroadcastReceiver getSdReceiver() { sSdCardReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { String actionStr = intent.getAction(); Logger.d(TAG, "sdAction--->" + actionStr); // if (Intent.ACTION_MEDIA_UNMOUNTED.equals(actionStr)) { // List<DownloadTask> downloadingTaskList = mDbutil // .findDownLoadingTask(); // if (downloadingTaskList != null // && downloadingTaskList.size() > 0) { // for (DownloadTask mTask : downloadingTaskList) { // if (mDownLoadCallback != null) { // mDownLoadCallback.sendFailureMessage( // mTask.getId(), mTask.getUrl(), // Erros.NO_SD); // Logger.d(TAG, "sdAction--->" + actionStr // + ";;mTaskId-->" + mTask.getId()); // } // // } // } // // } } }; return sSdCardReceiver; } private void registerSdReceiver(BroadcastReceiver sdCardReceiver) { IntentFilter intentFilter = new IntentFilter( Intent.ACTION_MEDIA_MOUNTED); intentFilter.addAction(Intent.ACTION_MEDIA_UNMOUNTED); intentFilter.addAction(Intent.ACTION_MEDIA_EJECT); intentFilter.addDataScheme("file"); if (mContext != null) { mContext.registerReceiver(sdCardReceiver, intentFilter); } } private void unRegisterSdReceiver() { if (mContext != null) { mContext.unregisterReceiver(sSdCardReceiver); } } private void setWorkerCount() { // int processNum = 0; int processNum = Runtime.getRuntime().availableProcessors(); DownLoadConfig.MAX_DOWNLOAD_THREAD_COUNT = processNum + 1; Logger.d(TAG, "processNum-->" + processNum); } private void initParams(Context context, String rootPath, String dataBaseName, boolean isSaveOldData) { this.mDbHashCode = dataBaseName.hashCode(); this.mRootPath = rootPath; this.mIsSaveOldData = isSaveOldData; this.mContext = context.getApplicationContext(); if (!TextUtils.isEmpty(dataBaseName) && !"dlplugin_database".equals(dataBaseName)) { this.mDbutil = DBUtil.getInstance(mContext, dataBaseName, isSaveOldData); } else { this.mDbutil = DBUtil.getInstance(mContext, "dlplugin_database", isSaveOldData); } mhandlerQueue = new HandlerQueue(); mDownloadinghandlers = new ArrayList<AsyncHttpResponseHandler>(); mPausinghandlers = new ArrayList<AsyncHttpResponseHandler>(); mDownloadTaskStatusData = new ArrayList<DownloadTaskStatus>(); mDownloadStrategyHashMap = new HashMap<Long, DownloadStrategy>(); mNotificationHelper = new NotificationHelper(mContext); mNotificationHelper.cancelAllNotifactions(); mAsyncHttpClient = new AsyncHttpClient(); myHandler = new Handler() { @Override public void handleMessage(Message msg) { // TODO Auto-generated method stub super.handleMessage(msg); if (msg.what == -1) { mNotificationHelper.cancelNotification(msg.arg1 + mDbHashCode); } } }; } private void pauseOrContinue(long taskId, String url) { DownloadTask mDownloadTask = mDbutil.fetchOneDownload(taskId); if (mDownloadTask != null) { if (mDownloadTask.getStatus() == DownloadTaskStatus.DOWNLOADING && DownLoadConfig.isClickable) { pauseTask(taskId); mNotificationHelper.updateNotificationTicker(taskId + mDbHashCode, "暂停下载"); } else if (mDownloadTask.getStatus() == DownloadTaskStatus.PAUSEING && DownLoadConfig.isClickable) { String postFix = FileInfoUtils.getPostFix(url); continueHandler(url, mDownloadTask.getVisibility()); mNotificationHelper.updateNotificationTicker(taskId + mDbHashCode, "继续下载"); } } } public void sendOnClickMessageToCallback(long taskId, String url) { if (mDownLoadCallback != null) mDownLoadCallback.sendClickNotifationMesage(taskId, url); } public void registerNotificationClickReceiver(BroadcastReceiver mreceiver) { IntentFilter mFilter = new IntentFilter(); // filter.addAction(DL_ANDROID_NET_CHANGE_ACTION); mFilter.addAction(NOTIFICATION_CLICK_ACTION); mFilter.addAction(NOFINISH_NOTIFICATION_CLICK_ACTION); mContext.getApplicationContext().registerReceiver(mreceiver, mFilter); } public String getRootPath() { if (StringUtils.isEmpty(mRootPath)) { mRootPath = DownLoadConfig.FILE_ROOT; } return mRootPath; } public void setDownLoadCallback(DownLoadCallback downLoadCallback) { this.mDownLoadCallback = downLoadCallback; } public void toStartMananger() { isRunning = true; this.start(); if (mDownLoadCallback != null) { mDownLoadCallback.sendStartMangerMessage(); } // checkUncompletehandlers(); } public void close() { isRunning = false; pauseAllHandler(); if (mDownLoadCallback != null) { mDownLoadCallback.sendStopMessage(); } this.stop(); } @Override public void run() { super.run(); while (isRunning) { if (mIsWorking) { toRequestDownLoad(); } } } private void toRequestDownLoad() { FileHttpResponseHandler handler = (FileHttpResponseHandler) mhandlerQueue .poll(); if (handler.isFirstStart()) { Logger.d(TAG, "taskId--->" + handler.getTaskId() + ";" + handler.getUrl() + ";开始下载"); if (mDownLoadCallback != null) { mDownLoadCallback.sendStartDownLoading(handler.getTaskId(), handler.getUrl()); } } if (handler != null) { mDownloadinghandlers.add(handler); DownloadTaskStatus status = getTaskRealtimeStatus( handler.getTaskId(), handler.getUrl()); if (status != null) { status.setCurrentStatus(DownloadTaskStatus.DOWNLOADING); } handler.setInterrupt(false); String url = handler.getUrl(); DefaultHttpClient httpClient = new DefaultHttpClient(); String[] redirectArrays; try { redirectArrays = handleRedirect(handler, httpClient, url); String redirectUrl = redirectArrays[0]; String mineTypePostFix = MimeTypeMap.getSingleton() .getExtensionFromMimeType(redirectArrays[1]); String postFix = FileInfoUtils.getPostFix(redirectUrl); try { toDownLoad(handler, redirectUrl, mineTypePostFix, postFix); } catch (Exception e) { cancelNotifactionByFileResponseHandler(handler); if (mDownLoadCallback != null) { mDownLoadCallback.sendFailureMessage( handler.getTaskId(), url, Erros.URL_EXECEPTION); } Logger.e(TAG, e.toString()); } } catch (ClientProtocolException e) { cancelNotifactionByFileResponseHandler(handler); if (mDownLoadCallback != null) { mDownLoadCallback.sendFailureMessage(handler.getTaskId(), url, Erros.URL_EXECEPTION); } Logger.e(TAG, e.toString()); } catch (MyRedirectException e) { cancelNotifactionByFileResponseHandler(handler); if (mDownLoadCallback != null) { mDownLoadCallback.sendFailureMessage(handler.getTaskId(), url, Erros.URL_EXECEPTION); } Logger.e(TAG, e.toString()); } catch (IOException e) { // TODO Auto-generated catch block cancelNotifactionByFileResponseHandler(handler); if (mDownLoadCallback != null) { mDownLoadCallback.sendFailureMessage(handler.getTaskId(), url, Erros.URL_EXECEPTION); } Logger.e(TAG, e.toString()); } } // saveFailedTask(handler); // cancelNotifactionByFileResponseHandler(handler); // if (mDownLoadCallback != null) { // mDownLoadCallback.sendFailureMessage(handler.getTaskId(), // url, Erros.URL_EXECEPTION); // } } private void cancelNotifactionByFileResponseHandler( FileHttpResponseHandler handler) { if (handler != null) { mNotificationHelper.cancelNotification(handler.getTaskId() + mDbHashCode); } } private void saveFailedTask(FileHttpResponseHandler handler) { if (mDownloadinghandlers.contains(handler)) { mDownloadinghandlers.remove(handler); } mhandlerQueue.offer(handler); } private void toDownLoad(FileHttpResponseHandler handler, String redirectUrl, String mineTypePostFix, String postFix) throws Exception { if (TextUtils.isEmpty(mineTypePostFix) && !TextUtils.isEmpty(postFix)) { handler.setContextTypePostFix(mineTypePostFix); RenameTempFileByUrlPostFix(handler, redirectUrl); } else if (!TextUtils.isEmpty(mineTypePostFix) && TextUtils.isEmpty(postFix)) { String filePath = handler.getFile().getAbsolutePath(); File tempFile = new File(filePath + "." + mineTypePostFix + FileHttpResponseHandler.getTempSuffix()); File file = new File(filePath + "." + mineTypePostFix); handler.setTempFile(tempFile); handler.setFile(file); mDbutil.updateDownload(handler.getTaskId(), null, handler.getCnname(), null, DownloadTaskStatus.DOWNLOADING, 0, filePath, null); } else if (!TextUtils.isEmpty(mineTypePostFix) && !TextUtils.isEmpty(postFix)) { handler.setContextTypePostFix(mineTypePostFix); RenameTempFileByUrlPostFix(handler, redirectUrl); } mAsyncHttpClient.download(redirectUrl, handler); } /** * 根据url 的后缀名命名 * * @param handler * @param url */ private void RenameTempFileByUrlPostFix(FileHttpResponseHandler handler, String url) { String fileName = StringUtils.getOriginalFileNameFromUrl(url); if (TextUtils.isEmpty(fileName)) { fileName = System.currentTimeMillis() + "." + handler.getContextTypePostFix(); } String mFilePath = handler.getBaseDirFile() + File.separator + fileName; File tempFile = new File(mFilePath + FileHttpResponseHandler.getTempSuffix()); File file = new File(mFilePath); handler.setTempFile(tempFile); handler.setFile(file); mDbutil.updateDownload(handler.getTaskId(), null, handler.getCnname(), null, DownloadTaskStatus.DOWNLOADING, 0, mFilePath, null); } private static class MyRedirectException extends RuntimeException { /** * */ private static final long serialVersionUID = 1L; public MyRedirectException() { super(); } } private static class MyRedirectHandler implements RedirectHandler { private String mFinalUrl = null; private int mRedirectCount = 0; private String mContentType = null; public MyRedirectHandler(String url) { mFinalUrl = url; } @Override public boolean isRedirectRequested(HttpResponse response, HttpContext context) { int statueCode = response.getStatusLine().getStatusCode(); if (statueCode == HttpStatus.SC_MOVED_TEMPORARILY || statueCode == HttpStatus.SC_MOVED_PERMANENTLY) { if (mRedirectCount > DownLoadConfig.REDIRECT_COUNT) { throw new MyRedirectException(); } mRedirectCount++; return true; } mContentType = response.getFirstHeader("Content-Type").getValue(); Logger.d(TAG, "contentType--->" + mContentType); return false; } @Override public URI getLocationURI(HttpResponse response, HttpContext context) throws ProtocolException { URI uri = null; Header locationHeader = response.getFirstHeader("location"); if (locationHeader != null) { String url = locationHeader.getValue(); try { uri = new URI(url); mFinalUrl = uri.toString(); } catch (URISyntaxException e) { } } return uri; } public String getFinalUrl() { return mFinalUrl; } public String getContentType() { return mContentType; } } private String[] handleRedirect(FileHttpResponseHandler handler, DefaultHttpClient httpClient, String url) throws ClientProtocolException, IOException, MyRedirectException { String[] mResult = new String[2]; try { HttpGet request = new HttpGet(url); HttpParams params = httpClient.getParams(); HttpClientParams.setRedirecting(params, true); request.setParams(params); httpClient.setParams(params); MyRedirectHandler redirectHandler = new MyRedirectHandler(url); httpClient.setRedirectHandler(redirectHandler); httpClient.execute(request); if (!TextUtils.isEmpty(redirectHandler.getFinalUrl())) { mResult[0] = redirectHandler.getFinalUrl(); mResult[1] = redirectHandler.getContentType(); } } catch (Exception e) { // TODO Auto-generated catch block Logger.e(TAG, e.toString()); if (e != null) { Message message = new Message(); message.arg1 = (int) handler.getTaskId(); message.what = -1; myHandler.sendMessage(message); } } return mResult; } /* * remove outdated download records */ public void cleanOutdatedDownloadedRecords() { long latestUpdateTime = mDbutil.getLatestCleanTime(); if ((System.currentTimeMillis() - latestUpdateTime) >= DownLoadConfig.CLEAN_HISTORY_INTERVAL) { mDbutil.removeCompletedDownloadTask(); } } /** * 7天清除apk 和临时文件 */ public void cleanOutDateApkFile() { if (mDbutil != null) { ArrayList<DownloadTask> downloadTasksList = mDbutil .findAllTask(); for (DownloadTask task : downloadTasksList) { Logger.d(TAG, "savePath--->" + task.getSavepath() + ";createTime--->" + task.getCreatetime()); long createTime = task.getCreatetime(); if (System.currentTimeMillis() - createTime >= DownLoadConfig.CLEAN_APK_OUTDATE) { String path = task.getSavepath(); String tempPath = task.getSavepath() + FileHttpResponseHandler.TEMP_SUFFIX; if (!TextUtils.isEmpty(path)) { File apkFile = new File(path); if (apkFile.exists()) { apkFile.delete(); } else { File apkTempFile = new File(tempPath); apkTempFile.delete(); } } } } } } /** * @param type=0 恢复 有通知 条的任务 ,type =1 恢复 静默 任务 .type=2 恢复所有 未完成的任务 */ public void restartUnCompletedTask(int type) { List<DownloadTask> taskList = mDbutil.findUnCompletedTask(); if (type == DownloadStrategy.ALL_UNCOMPELETED) { if (taskList != null) { int listSize = taskList.size(); if (listSize > 0) { for (DownloadTask task : taskList) { if (task.getVisibility() == DownloadStrategy.VISIBILITY) { mNotificationHelper.addNotification(task.getId() + mDbHashCode, -1, StringUtils.getOriginalFileNameFromUrl(task .getUrl()), task.getCnname(), task .getUrl()); } DownloadStrategy downloadStrategy = DownloadStrategy .parseFromJsonString(task.getDownloadStrategy()); addHandler(task.getId(), task.getUrl(), downloadStrategy != null ? downloadStrategy : null, task.getCnname(), false); } } } } else if (type == DownloadStrategy.VISIBILITY) { if (taskList != null) { int listSize = taskList.size(); if (listSize > 0) { for (DownloadTask task : taskList) { if (task.getVisibility() == DownloadStrategy.VISIBILITY) { mNotificationHelper.addNotification(task.getId() + mDbHashCode, -1, StringUtils.getOriginalFileNameFromUrl(task .getUrl()), task.getCnname(), task .getUrl()); DownloadStrategy downloadStrategy = DownloadStrategy .parseFromJsonString(task.getDownloadStrategy()); addHandler(task.getId(), task.getUrl(), downloadStrategy != null ? downloadStrategy : null, task.getCnname(), false); } } } } } else if (type == DownloadStrategy.GONE) { if (taskList != null) { int listSize = taskList.size(); if (listSize > 0) { for (DownloadTask task : taskList) { if (task.getVisibility() == DownloadStrategy.GONE) { DownloadStrategy downloadStrategy = DownloadStrategy .parseFromJsonString(task.getDownloadStrategy()); addHandler(task.getId(), task.getUrl(), downloadStrategy != null ? downloadStrategy : null, task.getCnname(), false); } } } } } } public void addHandler(long taskId, String url, DownloadStrategy mDownLoadStrategy, String cnname, boolean isFirstStart) { if (getTotalhandlerCount() >= DownLoadConfig.MAX_handler_COUNT) { return; } if (TextUtils.isEmpty(url) || hasHandler(taskId, url)) { Logger.d(TAG, "任务中存在这个任务,或者任务不满足要求"); return; } try { if (isFirstStart) { addHandler( newcHttpResponseHandler(mDownLoadStrategy, taskId, url, cnname, true), mDownLoadStrategy); } else { addHandler( newcHttpResponseHandler(mDownLoadStrategy, taskId, url, cnname, false), mDownLoadStrategy); } // Logger.d(TAG, "下载完成!!"); } catch (MalformedURLException e) { Logger.e(TAG, e.toString()); } } private void addHandler(AsyncHttpResponseHandler handler, DownloadStrategy downLoadStrategy) { FileHttpResponseHandler fhandler = (FileHttpResponseHandler) handler; fhandler.setmDownLoadCallback(mDownLoadCallback); broadcastAddHandler(fhandler.getTaskId(), fhandler.getUrl()); mhandlerQueue.offer(handler); mDownloadTaskStatusData.add(new DownloadTaskStatus( fhandler.getTaskId(), fhandler.getUrl()) .setCurrentStatus(DownloadTaskStatus.WAITING)); if (downLoadStrategy != null) mDownloadStrategyHashMap .put(fhandler.getTaskId(), downLoadStrategy); if (!this.isAlive()) { this.toStartMananger(); Logger.d(TAG, "toStartMananger()"); } } private void broadcastAddHandler(long taskId, String url) { broadcastAddHandler(taskId, url, false); } private void broadcastAddHandler(long taskId, String url, boolean isInterrupt) { if (mDownLoadCallback != null) { mDownLoadCallback.sendAddMessage(taskId, url, false); } } /** * by robin public void reBroadcastAddAllhandler() { FileHttpResponseHandler * handler; for (int i = 0; i < mDownloadinghandlers.size(); i++) { handler * = (FileHttpResponseHandler) mDownloadinghandlers.get(i); * broadcastAddHandler(handler.getTaskId(),handler.getUrl(), * handler.isInterrupt()); } for (int i = 0; i < mhandlerQueue.size(); i++) * { handler = (FileHttpResponseHandler) mhandlerQueue.get(i); * broadcastAddHandler(handler.getTaskId(),handler.getUrl()); } for (int i = * 0; i < mPausinghandlers.size(); i++) { handler = * (FileHttpResponseHandler) mPausinghandlers.get(i); * broadcastAddHandler(handler.getTaskId(),handler.getUrl()); } } */ public boolean hasHandler(long taskId, String url) { boolean mResult = false; try { FileHttpResponseHandler handler; if (mDownloadinghandlers != null && mDownloadinghandlers.size() > 0) { for (int i = 0; i < mDownloadinghandlers.size(); i++) { handler = (FileHttpResponseHandler) mDownloadinghandlers .get(i); if (handler.getUrl().equals(url)) { mResult = true; } } } if (mhandlerQueue != null && mhandlerQueue.size() > 0) { for (int i = 0; i < mhandlerQueue.size(); i++) { handler = (FileHttpResponseHandler) mhandlerQueue.get(i); if (handler.getUrl().equals(url)) { mResult = true; } } } } catch (Exception e) { // TODO Auto-generated catch block Logger.e(TAG, e.toString()); } return mResult; } public AsyncHttpResponseHandler gethandler(int postion) { if (postion >= mDownloadinghandlers.size()) { return mhandlerQueue.get(postion - mDownloadinghandlers.size()); } else { return mDownloadinghandlers.get(postion); } } /** * ycw 判断文件状态 * * @param url * @return */ public DownloadTask getDownloadTask(String url) { if (mDbutil != null) { if (!TextUtils.isEmpty(url)) { DownloadTask downloadTask = mDbutil.findDownloadByUrl(url); if (downloadTask == null) { return null; } if (downloadTask.getStatus() == DownloadTaskStatus.COMPLETED) { String filePath = downloadTask.getSavepath(); File file = new File(filePath); if (TextUtils.isEmpty(filePath) || !file.exists()) { downloadTask.setStatus(DownloadTaskStatus.UNCOMPLETED); } } return downloadTask; } else { return null; } } else { return null; } } public int getTotalhandlerCount() { // by robin return getQueuehandlerCount() + getDownloadinghandlerCount() // + getPausinghandlerCount(); return mDownloadTaskStatusData.size(); } public synchronized void pauseAllHandler() { AsyncHttpResponseHandler handler; if (mhandlerQueue.size() > 0) { for (int i = 0; i < mhandlerQueue.size(); i++) { handler = mhandlerQueue.get(i); handler.setFirstStart(false); mhandlerQueue.remove(handler); mPausinghandlers.add(handler); } } if (mDownloadinghandlers.size() > 0) { for (int i = 0; i < mDownloadinghandlers.size(); i++) { handler = mDownloadinghandlers.get(i); if (handler != null) { handler.setFirstStart(false); pausehandler(handler); } } } } public synchronized void deleteHandler(long taskId) { FileHttpResponseHandler handler; for (int i = 0; i < mDownloadinghandlers.size(); i++) { handler = (FileHttpResponseHandler) mDownloadinghandlers.get(i); if (handler != null && handler.getTaskId() == taskId) { File file = handler.getFile(); if (file.exists()) file.delete(); File tempFile = handler.getTempFile(); if (tempFile.exists()) { tempFile.delete(); } handler.setInterrupt(true); completehandler(handler); return; } } for (int i = 0; i < mhandlerQueue.size(); i++) { handler = (FileHttpResponseHandler) mhandlerQueue.get(i); if (handler != null && handler.getTaskId() == taskId) { mhandlerQueue.remove(handler); } } for (int i = 0; i < mPausinghandlers.size(); i++) { handler = (FileHttpResponseHandler) mPausinghandlers.get(i); if (handler != null && handler.getTaskId() == taskId) { mPausinghandlers.remove(handler); } } } public synchronized void deleteAllHandler() { // pauseAllHandler(); FileHttpResponseHandler handler; for (int i = 0; i < mDownloadinghandlers.size(); i++) { handler = (FileHttpResponseHandler) mDownloadinghandlers.get(i); handler.setmDownLoadCallback(mDownLoadCallback); if (handler != null) { File file = handler.getFile(); if (file.exists()) file.delete(); File tempFile = handler.getTempFile(); if (tempFile.exists()) { tempFile.delete(); } handler.setInterrupt(true); completehandler(handler); return; } } for (int i = 0; i < mhandlerQueue.size(); i++) { handler = (FileHttpResponseHandler) mhandlerQueue.get(i); if (handler != null) { mhandlerQueue.remove(handler); } } for (int i = 0; i < mPausinghandlers.size(); i++) { handler = (FileHttpResponseHandler) mPausinghandlers.get(i); if (handler != null) { mPausinghandlers.remove(handler); } } } private synchronized void pausehandler(AsyncHttpResponseHandler response) { try { FileHttpResponseHandler fileHttpResponseHandler = (FileHttpResponseHandler) response; if (response != null) { // move to pausing list long taskId = fileHttpResponseHandler.getTaskId(); String url = fileHttpResponseHandler.getUrl(); fileHttpResponseHandler.setInterrupt(true); DownloadTask downloadTask = mDbutil.fetchOneDownload(taskId); if (downloadTask != null) { DownloadStrategy downloadStrategy = DownloadStrategy .parseFromJsonString(downloadTask .getDownloadStrategy()); if (mDownLoadCallback != null && !NetWorkUtil.isNetworkAvailable(mContext)) { mDownLoadCallback.sendFailureMessage(taskId, url, Erros.NO_NETWORK); } if (mDownloadinghandlers != null) { mDownloadinghandlers.remove(response); String cname = getCName(url); response = newcHttpResponseHandler(downloadStrategy, taskId, url, cname, false); mPausinghandlers.add(response); } DownloadTaskStatus downloadTaskStatus = getTaskRealtimeStatus( taskId, url); if (downloadTaskStatus != null) { downloadTaskStatus .setCurrentStatus(DownloadTaskStatus.PAUSEING); } syncDownloadStatusWithDB(taskId, DownloadTaskStatus.PAUSEING, null, null, null); } } } catch (MalformedURLException e) { // TODO Auto-generated catch block Logger.e(TAG, e.toString()); } } private String getCName(String url) { DownloadTask mDownloadTask = mDbutil.findDownloadByUrl(url); String cname = ""; if (mDownloadTask != null) { cname = mDownloadTask.getCnname(); } return cname; } /** * ycw 添加是否显示进度条参数 * * @param url * * @param visibility */ public synchronized void continueHandler(String url, int visibility) { FileHttpResponseHandler handler; Logger.e("ydp", "add:" + url); for (int i = 0; i < mDownloadinghandlers.size(); i++) { handler = (FileHttpResponseHandler) mDownloadinghandlers.get(i); Logger.e("ydp", "url:" + i + handler.getUrl()); } for (int i = 0; i < mDownloadinghandlers.size(); i++) { String cnname = getCName(url); handler = (FileHttpResponseHandler) mDownloadinghandlers.get(i); if (handler != null && handler.getUrl().equals(url)) { long taskId = handler.getTaskId(); Logger.d(TAG, "准备改变正在下载的进度条状态" + ";taskId-->" + taskId + ";url-->" + url); if (visibility == DownloadStrategy.VISIBILITY) { mNotificationHelper.addNotification(handler.getTaskId() + mDbHashCode, -1, StringUtils.getOriginalFileNameFromUrl(url), cnname, url); Logger.d(TAG, "添加正在下载的进度条状态为显示" + ";taskId-->" + taskId + ";url-->" + url); } else { mNotificationHelper.cancelNotification(taskId + mDbHashCode); Logger.d(TAG, "将正在下载的进度条状态为隐藏" + ";taskId-->" + taskId + ";url-->" + url); } // 更新数据库 DownloadTask downloadTask = mDbutil.fetchOneDownload(taskId); String downloadStrategyStr = downloadTask.getDownloadStrategy(); try { if (!TextUtils.isEmpty(downloadStrategyStr)) { JSONObject downLoadStrateObj = new JSONObject( downloadStrategyStr); downLoadStrateObj.put(DownloadStrategy.VISIBILITY_KEY, visibility); Logger.d(TAG, "dbDownloadStrategy--->" + downLoadStrateObj); mDbutil.updateDownloadStrategyById(taskId, downLoadStrateObj.toString()); Logger.d(TAG, "改变正在下载的进度条数据库状态" + ";taskId-->" + taskId + ";url-->" + url + ";visibility--->" + visibility); } } catch (JSONException e) { // TODO Auto-generated catch block Logger.e(TAG, e.toString()); } } } for (int i = 0; i < mPausinghandlers.size(); i++) { handler = (FileHttpResponseHandler) mPausinghandlers.get(i); Logger.d(TAG, "准备改变缓存队列里的进度条状态" + ";taskId-->" + handler.getTaskId() + ";url-->" + handler.getUrl()); if (handler != null && handler.getUrl().equals(url)) { String cnname = getCName(url); if (visibility == DownloadStrategy.VISIBILITY) { mNotificationHelper.addNotification(handler.getTaskId() + mDbHashCode, -1, StringUtils.getOriginalFileNameFromUrl(url), cnname, url); Logger.d(TAG, "已经将缓存队列里的进度条状态改为addNotification" + ";taskId-->" + handler.getTaskId() + ";url-->" + handler.getUrl()); } else { cancelNotifactionByFileResponseHandler(handler); Logger.d(TAG, "隐藏缓存下载的进度条状态为隐藏cancelNotification" + ";taskId-->" + handler.getTaskId() + ";url-->" + url); } long taskId = handler.getTaskId(); DownloadTask downloadTask = mDbutil.fetchOneDownload(taskId); String downloadStrategyStr = downloadTask.getDownloadStrategy(); try { if (!TextUtils.isEmpty(downloadStrategyStr)) { JSONObject downLoadStrateObj = new JSONObject( downloadStrategyStr); downLoadStrateObj.put(DownloadStrategy.VISIBILITY_KEY, visibility); Logger.d(TAG, "dbDownloadStrategy--->" + downLoadStrateObj); mDbutil.updateDownloadStrategyById(taskId, downLoadStrateObj.toString()); Logger.d(TAG, "改变缓存的进度条数据库状态" + ";taskId-->" + taskId + ";url-->" + url + ";visibility--->" + visibility); } } catch (JSONException e) { // TODO Auto-generated catch block Logger.e(TAG, e.toString()); } continuehandler(handler); break; } } } public synchronized void continuehandler( AsyncHttpResponseHandler responseHandler) { if (responseHandler != null) { if (mPausinghandlers == null || mhandlerQueue == null) { Logger.d(TAG, "mPausinghandlers == null || mhandlerQueue == null"); return; } mPausinghandlers.remove(responseHandler); mhandlerQueue.offer(responseHandler); FileHttpResponseHandler fileHttpResponseHandler = (FileHttpResponseHandler) responseHandler; fileHttpResponseHandler.setmDownLoadCallback(mDownLoadCallback); DownloadTaskStatus status = getTaskRealtimeStatus( fileHttpResponseHandler.getTaskId(), fileHttpResponseHandler.getUrl()); if (status != null) { status.setCurrentStatus(DownloadTaskStatus.DOWNLOADING); } syncDownloadStatusWithDB(fileHttpResponseHandler.getTaskId(), DownloadTaskStatus.DOWNLOADING, null, null, null); } } public synchronized void continueAllHandler() { if (mPausinghandlers == null || mPausinghandlers.size() == 0) { return; } FileHttpResponseHandler handler; int count = mPausinghandlers.size(); if (count == 0) { return; } for (int i = 0; i < count; i++) { handler = (FileHttpResponseHandler) mPausinghandlers.get(0); handler.setmDownLoadCallback(mDownLoadCallback); String cnname = getCName(handler.getUrl()); String url = handler.getUrl(); DownloadTask downloadTask = mDbutil.fetchOneDownload(handler .getTaskId()); if (downloadTask != null) { if (downloadTask.getVisibility() == DownloadStrategy.VISIBILITY) { mNotificationHelper.addNotification(handler.getTaskId() + mDbHashCode, -1, StringUtils.getOriginalFileNameFromUrl(url), cnname, handler.getUrl()); } } continuehandler(handler); } } public synchronized void completehandler( AsyncHttpResponseHandler mAsyncHttpResponseHandler) { long taskId = -1; String url = null; String apkSavePath = null; try { if (mDownloadinghandlers.contains(mAsyncHttpResponseHandler)) { // by robin // DownLoadConfigUtil.clearURL(mDownloadinghandlers.indexOf(handler)); mDownloadinghandlers.remove(mAsyncHttpResponseHandler); if (mDownLoadCallback != null) { FileHttpResponseHandler fhandler = (FileHttpResponseHandler) mAsyncHttpResponseHandler; taskId = fhandler.getTaskId(); url = fhandler.getUrl(); removeRealtimeStatus(taskId, url); // 说明下载完成 if (fhandler != null && fhandler.getDownloadSize() == fhandler .getTotalSize() && fhandler.getDownloadSize() > 0 && fhandler.getTotalSize() > 0) { apkSavePath = fhandler.getFile().getAbsolutePath(); //Object[] apkinfos = null; // syncDownloadStatusWithDB(taskId,DownloadTaskStatus.COMPLETED,apkSavePath,apkinfos!=null?(String)apkinfos[0]:null,apkinfos!=null?(String)apkinfos[2]:null); String packageName = mDbutil.fetchOneDownload(taskId) .getPackageName(); String postFix = FileInfoUtils.getPostFix(apkSavePath); if (SdCardUtil.isMounted()) { if (postFix != null && postFix.equalsIgnoreCase("apk")) { // apkinfos = ApkUtil.fetchApkFileInfo(mContext, // apkSavePath); // 若传递了包名,者不更改 if (!TextUtils.isEmpty(packageName)) { syncDownloadStatusWithDB(taskId, DownloadTaskStatus.COMPLETED, apkSavePath, packageName, null); } else { syncDownloadStatusWithDB( taskId, DownloadTaskStatus.COMPLETED, apkSavePath, null, null); } } else { syncDownloadStatusWithDB(taskId, DownloadTaskStatus.COMPLETED, apkSavePath, null, null); } if (mDownLoadCallback != null) { mDownLoadCallback .sendFinishMessage(taskId, url); mDownLoadCallback.sendLoadMessage(taskId,url, fhandler.getTotalSize(), fhandler.getTotalSize(),0,100); } mNotificationHelper.updateNotification(taskId + mDbHashCode, 100, 100); } } } } } catch (Exception exception) { Log.e("YCW", "e.toString--->" + exception.toString()); if (taskId != -1 && url != null) { if (mDownLoadCallback != null) { mNotificationHelper.updateNotification(taskId + mDbHashCode, 100, 100); mDownLoadCallback.sendFailureMessage(taskId, url, Erros.APK_UNINSTALLABLE); mNotificationHelper .updateNotificationTicker(taskId + mDbHashCode, "文件损坏"); DownloadTask mDownloadTask = mDbutil.findDownloadByUrl(url); if (mDownloadTask != null) { if (!TextUtils.isEmpty(apkSavePath)) { // mDbutil.deleteDownload(taskId); File apkFile = new File(apkSavePath); if (apkFile.exists()) { apkFile.delete(); } } } mNotificationHelper.cancelNotification(taskId + mDbHashCode); } } } } private void testSavepath(long taskId, String tag) { String savaPaths = getDownloadedTaskInfo(taskId).getSavepath(); Logger.d(TAG, tag + "():" + "savaPaths---->" + savaPaths); } private AsyncHttpResponseHandler newcHttpResponseHandler( DownloadStrategy temDownLoadStrategy, long taskId, String url, String cnname, boolean isFirstStart) throws MalformedURLException { String rootPathStrategy = null; if (temDownLoadStrategy != null) { if (!TextUtils.isEmpty(temDownLoadStrategy.rootpath)) { rootPathStrategy = temDownLoadStrategy.rootpath; Logger.d(TAG, "mRootPathStrategy---->" + rootPathStrategy); } } FileHttpResponseHandler handler = new FileHttpResponseHandler(taskId, url, rootPathStrategy == null ? mRootPath : rootPathStrategy, StringUtils.getOriginalFileNameFromUrl(url), cnname, isFirstStart, mContext, mDataBaseName, mIsSaveOldData) { private int times = 0; private int step = 1; private int stepCount = 0; private int stepMax = 100; private DownloadStrategy downloadStrategy = null; private int lastPercent = 0; @Override public void onProgress(long totalSize, long currentSize, long speed) { // super.onProgress(totalSize, currentSize, speed); int downloadPercent = Long.valueOf( currentSize * 100 / totalSize).intValue(); if ((times == 20) || (currentSize == totalSize)) { mNotificationHelper.updateNotification(this.getTaskId() + mDbHashCode, Long.valueOf(downloadPercent).intValue(), Long .valueOf(downloadPercent).intValue()); Logger.d(TAG, "notifaction--->" + this.getTaskId()); times = 0; } times++; if (mDownLoadCallback != null && downloadPercent > 0) { if (downloadStrategy == null) { downloadStrategy = mDownloadStrategyHashMap.get(this .getTaskId()); } if (downloadStrategy != null && downloadStrategy.step >= 1 && downloadStrategy.step < 100 && 100 % downloadStrategy.step == 0) { step = downloadStrategy.step; Logger.d(TAG, "step---->" + step); stepMax = 100 / step; if (stepCount < stepMax && downloadPercent >= step * stepCount && downloadPercent % step == 0 && lastPercent != downloadPercent) { lastPercent = downloadPercent; mDownLoadCallback.sendLoadMessage(this.getTaskId(), this.getUrl(), totalSize, currentSize, speed, downloadPercent); Logger.d(TAG, "stepCount:" + stepCount + ";taskId--->" + this.getTaskId() + ";precent--->" + downloadPercent); stepCount++; } } } } @Override public void onSuccess(String content) { if (mDownLoadCallback != null) { mDownLoadCallback.sendSuccessMessage(this.getTaskId(), this.getUrl()); } } @Override public void onFinish() { completehandler(this); } @Override public void onStart() { // 注意这暂停重新开始也走这里 // 从缓存查找,如果缓存 if (mDownLoadCallback != null) { mDownLoadCallback.sendStartMessage(this.getTaskId(), this.getUrl()); } DownloadTask downloadTask = getDownloadedTaskInfo(this .getTaskId()); if (downloadTask != null) { DownloadStrategy downloadStrategy = DownloadStrategy .parseFromJsonString(downloadTask .getDownloadStrategy()); String postFix = FileInfoUtils.getPostFix(this.getUrl()); if (downloadTask != null) { if (downloadTask.getStatus() != DownloadTaskStatus.PAUSEING) { if (downloadStrategy != null) { if (downloadStrategy.getVisibility() == DownloadStrategy.VISIBILITY) { mNotificationHelper.addNotification(this .getTaskId() + mDbHashCode, -1, StringUtils .getOriginalFileNameFromUrl(this .getUrl()), downloadTask .getCnname(), this.getUrl()); } } else { mNotificationHelper.addNotification(this .getTaskId() + mDbHashCode, -1, StringUtils .getOriginalFileNameFromUrl(this .getUrl()), downloadTask .getCnname(), this.getUrl()); } syncDownloadStatusWithDB(this.getTaskId(), DownloadTaskStatus.DOWNLOADING, null, null, null); testSavepath(downloadTask.getId(), "onStart"); } else { continueHandler(this.getUrl(), downloadTask.getVisibility()); } } } // by robin // DownLoadConfigUtil.storeURL(mDownloadinghandlers.indexOf(this),getTaskId(),getUrl()); } @Override public void onFailure(Throwable error) { if (error != null) { Message message = new Message(); message.arg1 = (int) this.getTaskId(); message.what = -1; myHandler.sendMessage(message); } if (error != null) { Logger.e(TAG, "Throwable:" + error.getMessage()); Logger.d(TAG, "[hot track]===onFailed==point 9=="); error.printStackTrace(); String erroMessage = error.getMessage(); if (TextUtils.isEmpty(erroMessage)) { if (mDownLoadCallback != null) { mDownLoadCallback.sendFailureMessage( this.getTaskId(), this.getUrl(), Erros.TIMEOUT); } return; } if (erroMessage.contains("ETIMEDOUT")) { if (mDownLoadCallback != null) { if (NetWorkUtil.isNetworkAvailable(mContext)) { mDownLoadCallback.sendFailureMessage( this.getTaskId(), this.getUrl(), Erros.TIMEOUT); } } } else if (erroMessage.contains("host")) { if (mDownLoadCallback != null) { mDownLoadCallback.sendFailureMessage( this.getTaskId(), this.getUrl(), Erros.URL_EXECEPTION); } } else if (erroMessage.contains("I/O")) { if (!Environment.MEDIA_MOUNTED.equals(Environment .getExternalStorageState())) { // if (mDownLoadCallback != null) { // mDownLoadCallback.onFailure(this.getTaskId(), // this.getUrl(), Erros.NO_SD); // } } } } mNotificationHelper.updateNotificationTicker(this.getTaskId() + mDbHashCode, "下载失败"); mNotificationHelper.cancelNotification(this.getTaskId() + mDbHashCode); pauseTask(this.getTaskId()); } }; return handler; } @SuppressWarnings("unused") private boolean isWaitingTask(String url) { for (int i = 0; i < mhandlerQueue.size(); i++) { if (((FileHttpResponseHandler) mhandlerQueue.get(i)).getUrl() .equals(url)) { return true; } } return false; } @SuppressWarnings("unused") private boolean isPausingTask(String url) { for (int i = 0; i < mPausinghandlers.size(); i++) { if (((FileHttpResponseHandler) mPausinghandlers.get(i)).getUrl() .equals(url)) return true; } return false; } @SuppressWarnings("unused") private boolean isDownloadingTask(String url) { for (int i = 0; i < mDownloadinghandlers.size(); i++) { if (((FileHttpResponseHandler) mDownloadinghandlers.get(i)) .getUrl().equals(url)) return true; } return false; } private class HandlerQueue { private Queue<AsyncHttpResponseHandler> handlerQueue; public HandlerQueue() { handlerQueue = new LinkedList<AsyncHttpResponseHandler>(); } public void offer(AsyncHttpResponseHandler handler) { handlerQueue.offer(handler); } public AsyncHttpResponseHandler poll() { AsyncHttpResponseHandler handler = null; while (mDownloadinghandlers.size() >= DownLoadConfig.MAX_DOWNLOAD_THREAD_COUNT || (handler = handlerQueue.poll()) == null) { try { Thread.sleep(1000); // sleep } catch (InterruptedException e) { Logger.e(TAG, e.toString()); } } return handler; } public AsyncHttpResponseHandler get(int position) { if (position >= size()) { return null; } return ((LinkedList<AsyncHttpResponseHandler>) handlerQueue) .get(position); } public int size() { return handlerQueue.size(); } @SuppressWarnings("unused") public boolean remove(int position) { return handlerQueue.remove(get(position)); } public boolean remove(AsyncHttpResponseHandler handler) { return handlerQueue.remove(handler); } } private void removeRealtimeStatus(long taskId, String url) { for (int i = 0; i < mDownloadTaskStatusData.size(); i++) { DownloadTaskStatus s = mDownloadTaskStatusData.get(i); if (s.getTaskId() == taskId || s.getUrl().equals(url)) { mDownloadTaskStatusData.remove(i); break; } } } private DownloadTaskStatus getTaskRealtimeStatus(long taskId, String url) { if (mDownloadTaskStatusData != null) { for (int i = 0; i < mDownloadTaskStatusData.size(); i++) { DownloadTaskStatus s = mDownloadTaskStatusData.get(i); if (s != null) { if (s.getTaskId() == taskId || s.getUrl().equals(url)) return s; } } } return null; } private void syncDownloadStatusWithDB(long taskId, int status, String savePath, String packageName, String appName) { boolean savedbResult = mDbutil.updateDownload(taskId, null, appName, null, status, -1, savePath, packageName); } /***** * blow method for client to invoke * * @throws Exception *****/ /* * post one download task with url and chinese name * @param url * @param cnname app name, mostly in chinese * @param packageName * @downloadStrategy download Strategy * @return DownloadTask */ public synchronized DownloadTask postTask(String url, String cnname, String packageName, JSONObject downloadStrategyJsonObj) throws Exception { Logger.d(TAG, "postTask::" + url + "|" + cnname + "|" + packageName + "|" + downloadStrategyJsonObj); if (!url.startsWith("http") && !url.startsWith("https")) { throw new Exception(); } url = url.trim(); url = encodeUrl(url); Logger.d(TAG, "encodeurl--->" + url); int visibility = 0; String postFix = FileInfoUtils.getPostFix(url); DownloadTask downloadTask = new DownloadTask(url, cnname); DownloadTaskStatus istatus = getTaskRealtimeStatus(0, url); DownloadStrategy downloadStrategy = null; if (downloadStrategyJsonObj != null) { downloadStrategy = DownloadStrategy .parseFromJsonObject(downloadStrategyJsonObj); } if (downloadStrategy != null) { visibility = downloadStrategy.getVisibility(); downloadTask.setVisibility(visibility); } else { downloadTask.setVisibility(visibility); } if (istatus != null) { // 内存中有这个状态 Logger.d(TAG, "内存中有----》taskId:" + downloadTask.getId() + ";url---->" + url + ";+statues" + istatus.getCurrentStatus()); downloadTask.setStatus(istatus.getCurrentStatus()); // 继续任务并且修改内存和文件状态为Dowloading if (NetWorkUtil.isNetworkConnected(mContext)) { Logger.d(TAG, "有网络连接"); if (downloadTask.getStatus() == DownloadTaskStatus.WAITING) { DownloadTask downloadTasktemp = mDbutil .findDownloadByUrl(url); Logger.d(TAG, "正在添加进度条---》url" + url + ";taskId" + downloadTasktemp.getId() + "visibility-->" + downloadTasktemp.getDownloadStrategy()); downloadTasktemp.setVisibility(visibility); if (visibility == DownloadStrategy.VISIBILITY) { addNotifactions(url, downloadTasktemp); // 修改数据库 mDbutil.updateDownloadStrategyById( downloadTasktemp.getId(), downloadStrategyJsonObj != null ? downloadStrategyJsonObj .toString() : null); } else { mNotificationHelper.cancelNotification(downloadTasktemp .getId() + mDbHashCode); mDbutil.updateDownloadStrategyById( downloadTasktemp.getId(), downloadStrategyJsonObj != null ? downloadStrategyJsonObj .toString() : null); } if (downloadTasktemp != null) { Logger.d(TAG, "数据库里面存在---》url" + url + ";taskId" + downloadTasktemp.getId()); mDownloadManager .addHandler( downloadTasktemp.getId(), url, downloadStrategy != null ? DownloadStrategy .parseFromJsonObject(downloadStrategyJsonObj) : null, downloadTask .getCnname(), false); Logger.d(TAG, "addHandler()---》url" + url + ";taskId" + downloadTasktemp.getId()); } else { Logger.d(TAG, "数据库里不存在---》url" + url + ";taskId" + downloadTasktemp.getId()); } } else { continueHandler(url, visibility); } } } else { Logger.d(TAG, "内存无----》taskId:" + downloadTask.getId() + ";url---->" + url); // 从数据库里面查看文件状态,数据库有记录 DownloadTask downloadTasktemp = mDbutil.findDownloadByUrl(url); if (downloadTasktemp != null) { downloadTask = downloadTasktemp; downloadTask.setPostFix(postFix); downloadTask.setVisibility(visibility); if (downloadTask != null) { if (downloadTask.getStatus() == DownloadTaskStatus.COMPLETED) { if ("apk".equalsIgnoreCase(postFix)) { boolean mApkIsAvaliable = checkApk(downloadTask); String savePath = downloadTask.getSavepath(); if (!TextUtils.isEmpty(savePath) && new File(savePath).exists() && mApkIsAvaliable) { downloadTask.setSavepath(downloadTask .getSavepath()); } else {// 数据库里有完成记录,但是临时文件不存在(或则已经损坏),重新下载 deleteTempFile(downloadTask); // 删除数据库的记录 mDbutil.deleteDownload(downloadTask.getId()); mNotificationHelper .cancelNotification(downloadTask .getId() + mDbHashCode); postTaskFirst(url, cnname, packageName, downloadStrategyJsonObj, downloadTask); } } else { if (!new File(DownLoadConfig.FILE_ROOT + downloadTask.getName()).exists()) { deleteTempFile(downloadTask); if (downloadTask.getVisibility() == DownloadStrategy.VISIBILITY) { addNotifactions(url, downloadTask); } putnewTask(url, downloadStrategyJsonObj, downloadTask); } } } else {// 数据库里面有记录,文件状态没有完成,临时文件存在,断点续传 downloadTask.setStatus(DownloadTaskStatus.UNCOMPLETED) .setSavepath( DownLoadConfig.FILE_ROOT + downloadTask.getName()); if (downloadTask.getVisibility() == DownloadStrategy.VISIBILITY) { addNotifactions(url, downloadTask); } putnewTask(url, downloadStrategyJsonObj, downloadTask); } } } else { postTaskFirst(url, cnname, packageName, downloadStrategyJsonObj, downloadTask); } } return downloadTask; } public synchronized boolean saveTempTask(String url, String cnname, String packageName, JSONObject downloadStrategyJsonObj) { boolean result = true; Logger.d(TAG, "saveTempTask::" + url + "|" + cnname + "|" + packageName + "|" + downloadStrategyJsonObj); try { url = url.trim(); url = encodeUrl(url); Logger.d(TAG, "encodeurl--->" + url); if (mDbutil != null) { DownloadTask downloadTask = mDbutil.findDownloadByUrl(url); if (downloadTask == null) { mDbutil.insertDownload( StringUtils.getOriginalFileNameFromUrl(url), cnname, url, packageName, downloadStrategyJsonObj != null ? downloadStrategyJsonObj .toString() : null, DownloadTaskStatus.TEMP_NODOWNLOAD); } } else { result = false; } } catch (Exception e) { result = true; Logger.e(TAG, e.toString()); } return result; } public void startTempTask() { if (mDbutil != null) { List<DownloadTask> downloadTaskList = mDbutil .findAllTempTaskByStatus(); if (downloadTaskList != null) { int listSize = downloadTaskList.size(); if (listSize > 0) { for (DownloadTask task : downloadTaskList) { Logger.d( TAG, "startTempTask().getVisibility-->" + task.getVisibility()); if (task.getVisibility() == DownloadStrategy.VISIBILITY) { mNotificationHelper.addNotification(task.getId() + mDbHashCode, -1, StringUtils .getOriginalFileNameFromUrl(task .getUrl()), task .getCnname(), task.getUrl()); } DownloadStrategy downloadStrategy = DownloadStrategy .parseFromJsonString(task.getDownloadStrategy()); addHandler(task.getId(), task.getUrl(), downloadStrategy != null ? downloadStrategy : null, task.getCnname(), false); } } } } } private void addNotifactions(String url, DownloadTask downloadTask) { try { mNotificationHelper.addNotification(downloadTask.getId() + mDbHashCode, -1, StringUtils.getOriginalFileNameFromUrl(url), downloadTask.getCnname(), url); } catch (Exception e) { e.printStackTrace(); } } private String encodeUrl(String url) { url = Uri.decode(url); String encodeUrl = Uri.encode(url, "UTF-8").replace("%3A", ":") .replace("%2F", "/"); return encodeUrl; } /** * ycw 删除临时文件 * * @param downloadTask */ private void deleteTempFile(DownloadTask downloadTask) { File mTempFile = new File(DownLoadConfig.FILE_ROOT + downloadTask.getName()); if (mTempFile.exists()) { mTempFile.delete(); } } /** * ycw * * @param downloadTask * @return */ private boolean checkApk(DownloadTask downloadTask) { boolean isAvaliable = true; String savePath = downloadTask.getSavepath(); if (downloadTask != null) { try { if (new File(savePath).exists()) { ApkUtil.fetchApkFileInfo(mContext, downloadTask.getSavepath()); } else { isAvaliable = false; } } catch (Exception e) { isAvaliable = false; Logger.e(TAG, e.toString()); } } return isAvaliable; } /** * ycw * * @param url * @param cnname * @param packageName * @param downloadStrategy * @param downloadTask */ private void postTaskFirst(String url, String cnname, String packageName, JSONObject downloadStrategy, DownloadTask downloadTask) { long taskId = mDbutil.insertDownload( StringUtils.getOriginalFileNameFromUrl(url), cnname, url, packageName, downloadStrategy != null ? downloadStrategy.toString() : null); Logger.d(TAG, "new taskid=" + taskId); if (downloadStrategy != null) { DownloadStrategy downloadStrategy2 = DownloadStrategy .parseFromJsonObject(downloadStrategy); if (downloadStrategy2 != null) { if (downloadStrategy2.getVisibility() == DownloadStrategy.VISIBILITY) { mNotificationHelper.addNotification(taskId + mDbHashCode, -1, StringUtils.getOriginalFileNameFromUrl(url), downloadTask.getCnname(), url); } } } else { mNotificationHelper.addNotification(taskId + mDbHashCode, -1, StringUtils.getOriginalFileNameFromUrl(url), downloadTask.getCnname(), url); } mDownloadManager.addHandler( taskId, url, downloadStrategy != null ? DownloadStrategy .parseFromJsonObject(downloadStrategy) : null, downloadTask.getCnname(), true); downloadTask.setId(taskId).setStatus(DownloadTaskStatus.WAITING); } private void putnewTask(String url, JSONObject downloadStrategy, DownloadTask downloadTask) { if (downloadTask != null) { long taskId = downloadTask.getId(); syncDownloadStatusWithDB(taskId, DownloadTaskStatus.WAITING, null, null, null); mDownloadManager.addHandler( taskId, url, downloadStrategy != null ? DownloadStrategy .parseFromJsonObject(downloadStrategy) : null, downloadTask.getCnname(), false); downloadTask.setId(taskId).setStatus(DownloadTaskStatus.WAITING); } } public int getDownloadTaskStatus(long taskId) { DownloadTaskStatus istatus = getTaskRealtimeStatus(taskId, null); if (istatus != null) return istatus.getCurrentStatus(); // if can't find in cache DownloadTask entity = mDbutil.fetchOneDownload(taskId); return entity != null ? entity.getStatus() : DownloadTaskStatus.UNKNOW; } /** * get one downloaded task info including base-infomation save path etc. * * @param taskId * @return DownloadTask */ public DownloadTask getDownloadedTaskInfo(long taskId) { DownloadTask downloadTask = mDbutil.fetchOneDownload(taskId); // return (entity!=null && // entity.getStatus()==DownloadTaskStatus.COMPLETED)?entity:null; if (downloadTask == null) { return null; } if (downloadTask.getStatus() == DownloadTaskStatus.COMPLETED) { String filePath = downloadTask.getSavepath(); File file = new File(filePath); if (TextUtils.isEmpty(filePath) || !file.exists()) { downloadTask.setStatus(DownloadTaskStatus.UNCOMPLETED); } } return (downloadTask != null) ? downloadTask : null; } public DownloadTask getDownloadedTaskInfo(String url) { DownloadTask entity = mDbutil.findDownloadByUrl(url); if (entity != null) { for (int i = 0; i < mDownloadinghandlers.size(); i++) { FileHttpResponseHandler fh = (FileHttpResponseHandler) mDownloadinghandlers .get(i); if (fh.getUrl().equals(url)) { long totalSize = fh.getTotalSize(); if (totalSize != 0) { int downloadPercent = Long.valueOf( fh.getDownloadSize() * 100 / totalSize) .intValue(); entity.setProgress(downloadPercent); } else { entity.setProgress(100); } break; } } } return entity; } public synchronized void pauseTask(long taskId) { FileHttpResponseHandler handler; for (int i = 0; i < mDownloadinghandlers.size(); i++) { handler = (FileHttpResponseHandler) mDownloadinghandlers.get(i); if (handler != null && handler.getTaskId() == taskId) { pausehandler(handler); } } } // remove notification specified by packageName public void removeNotification(String packageName) { ArrayList<DownloadTask> mDownloadtaskList = mDbutil .findDownloadByPackageName(packageName); if (mDownloadtaskList.size() > 0) { for (DownloadTask mTask : mDownloadtaskList) { mNotificationHelper.cancelNotification(mTask.getId() + mDbHashCode); } } } public void removeNotification(long taskId) { mNotificationHelper.cancelNotification(taskId + mDbHashCode); } public Context getContext() { return mContext; } public void setContext(Context context) { this.mContext = context; } public void setClickAble(boolean clickAble) { if (clickAble) { DownLoadConfig.isClickable = true; } } private void showToast(String text) { if (DownLoadConfig.isShow) { Toast.makeText(mContext, text, Toast.LENGTH_LONG).show(); } } public void setShowToast(boolean isShow) { if (isShow) { DownLoadConfig.isShow = true; } } public void setNotifacationCancleAble(boolean isCancelAble) { if (isCancelAble) { DownLoadConfig.notifactionCancleAble = true; } } public void unRegisterAllBroadCastReceiver() { try { NetworkStateReceiver.unRegisterNetworkStateReceiver(mContext); if (mNotifactionClickReceiver != null && mContext != null) { mContext.unregisterReceiver(mNotifactionClickReceiver); } unRegisterSdReceiver(); } catch (Exception e) { // TODO Auto-generated catch block Logger.e(TAG, e.toString()); } } /** * 要在getInstance 之后调用 * * @param mContext type=0 恢复 有通知 条的任务 ,type =1 恢复 静默 任务 .type=2 恢复所有 未完成的任务 */ public void restartUnCompleteTask(Context mContext, int type) { if (mDownloadManager != null) { restartUnCompletedTask(type); } } public static String getmDataBaseName() { return mDataBaseName; } public static void setmDataBaseName(String mDataBaseName) { DownloadManager.mDataBaseName = mDataBaseName; } /** * 根据包名查找完成记录 * * @param packageName * @return */ public List<DownloadTask> getCompletedTaskByPackage(String packageName) { ArrayList<DownloadTask> tempDownloadtaskList = null; ArrayList<DownloadTask> downloadcompletetaskList = new ArrayList<DownloadTask>(); if (mDbutil != null) { tempDownloadtaskList = mDbutil .findDownloadByPackageName(packageName); } for (int i = 0; i < tempDownloadtaskList.size(); i++) { DownloadTask downloadTask = tempDownloadtaskList.get(i); String filePath = downloadTask.getSavepath(); File file = new File(filePath); if (!TextUtils.isEmpty(filePath) && file.exists()) { downloadcompletetaskList.add(downloadTask); } } return downloadcompletetaskList; } }

    这里需要说明的是 获取的单例 的方法有多个重载,如没有特殊业务需求直接调用getInstance(Context context) 即可

    postTask 方法返回的是DownloadTask 对象,可在调用处判断下载状态,如果是DownloadTaskStatus.COMPLETED 并且

   下载文件存在,可以直接走安装逻辑。

   FileType 类

    

复制代码
1
2
3
4
5
6
package com.downloader.sdk.dlplugin.util.common; public enum FileType { PNG, OGG, GIF, JPG, JS, MP3, WAV, APK, HTML, PHP, JAR, SO, EXE, AVI, MP4, GP3, JSP, TEXT, JSON, DOC, EXL, PPT, RAR, ZIP, UNKOWN }

   本下载器支持的文件类型

  Errors 类:

   (错误类型,在DownloadCallBack 的onFailure()回调参数中会使用,用于下载失败原因统计。

 

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package com.downloader.sdk.dlplugin; public class Erros { /** * 资源不可用,有可能是url 不被识别 */ public static final int URL_EXECEPTION = 1; /** * apk损坏 */ public static final int APK_UNINSTALLABLE = 2; /** * 网络连接超时 */ public static final int TIMEOUT = 3; /** * 无网络连接 */ public static final int NO_NETWORK = 4; /** * sd卡容量不足 */ public static final int SD_NOSPACE = 5; /** * 没有sd卡,或者sd未挂载 */ public static final int NO_SD = 6; }

DlNotifaction 类

Notifaction 的封装类,下载器通知的相关方法封装。

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
package com.downloader.sdk.dlplugin.util.notification; import android.app.Notification; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ProgressBar; import android.widget.RemoteViews; import com.downloader.sdk.dlplugin.DownLoadConfig; import com.downloader.sdk.dlplugin.DownloadManager; import com.downloader.sdk.dlplugin.util.common.StringUtils; import java.lang.reflect.Field; import java.util.UUID; public class DLNotification { private Notification notification; private Context mContext; private static final int MAX_NAME_LENGTH = 30; private int[] remoteViewInnerids; private boolean existProgressBar; private long taskId; private String url; public DLNotification(Context context, long taskId, int icon, String name, String cnname, String url) { this.mContext = context.getApplicationContext(); this.notification = new Notification(); this.taskId = taskId; this.url = url; Intent intent = new Intent(mContext, this.getClass()); Bundle bundle = new Bundle(); intent.putExtra("ExtraData", bundle); PendingIntent pendingIntent = PendingIntent.getService(mContext, UUID .randomUUID().hashCode(), intent, PendingIntent.FLAG_UPDATE_CURRENT); this.notification.icon = android.R.drawable.stat_sys_download; this.notification.defaults=Notification.DEFAULT_SOUND; String displayName = StringUtils.isEmpty(cnname) ? StringUtils .toLength(name, MAX_NAME_LENGTH) : cnname; this.notification.when = System.currentTimeMillis(); // this.notification.setLatestEventInfo(mContext, displayName, null, // pendingIntent); Notification.Builder builder = new Notification.Builder(context);//新建Notification.Builder对象 builder.setContentTitle(displayName);//设置标题 builder.setContentText("正在下载");//设置内容 builder.setSmallIcon(notification.icon);//设置图片 builder.setContentIntent(pendingIntent);//执行intent builder.build(); notification = builder.getNotification();//将builder对象转换为普通的notification if (this.notification.tickerText == null) { this.notification.tickerText = "【" + displayName + "】开始下载"; } RemoteViews remoteViews = this.notification.contentView; remoteViewInnerids = fetchNecessaryIds(); if (remoteViewInnerids[0] > 0) { remoteViews.setViewVisibility(remoteViewInnerids[0], View.VISIBLE); remoteViews.setImageViewResource(remoteViewInnerids[0], android.R.drawable.stat_sys_download_done); } if (remoteViewInnerids[1] > 0) remoteViews.setTextViewText(remoteViewInnerids[1], displayName); if (remoteViewInnerids[2] > 0) remoteViews.setTextViewText(remoteViewInnerids[2], "0%"); View v = null; try { v = LayoutInflater.from(mContext).inflate( remoteViews.getLayoutId(), null); } catch (Exception e) { } if (v != null) findProgressBarInRemoteViewLayout(v); if (remoteViewInnerids[7] > 0 && existProgressBar) { remoteViews.setProgressBar(remoteViewInnerids[7], 100, 0, false); remoteViews.setViewVisibility(remoteViewInnerids[7], View.VISIBLE); } } public void setProcess(int downloadedPercent, int progress) { if (remoteViewInnerids[2] > 0) this.notification.contentView.setTextViewText( remoteViewInnerids[2], progress + "%"); if (existProgressBar) this.notification.contentView.setProgressBar(remoteViewInnerids[7], 100, downloadedPercent, false); if (progress == 100) { this.notification.icon = android.R.drawable.stat_sys_download_done; Bundle bundle = new Bundle(); bundle.putLong("taskid", this.taskId); bundle.putString("taskurl", this.url); Intent mIntent = new Intent( DownloadManager.NOTIFICATION_CLICK_ACTION); mIntent.putExtras(bundle); PendingIntent pintent = PendingIntent.getBroadcast(mContext, Long .valueOf(this.taskId).intValue(), mIntent, PendingIntent.FLAG_UPDATE_CURRENT); this.notification.contentView.setOnClickPendingIntent( this.notification.contentView.getLayoutId(), pintent); this.notification.contentIntent = pintent; if (DownLoadConfig.notifactionCancleAble) { this.notification.flags = Notification.FLAG_AUTO_CANCEL; } } else { this.notification.icon = android.R.drawable.stat_sys_download_done; Bundle bundle = new Bundle(); bundle.putLong("taskid", this.taskId); bundle.putString("taskurl", this.url); Intent mIntent = new Intent( DownloadManager.NOFINISH_NOTIFICATION_CLICK_ACTION); mIntent.putExtras(bundle); PendingIntent pintent = PendingIntent.getBroadcast(mContext, Long .valueOf(this.taskId).intValue(), mIntent, PendingIntent.FLAG_UPDATE_CURRENT); this.notification.contentView.setOnClickPendingIntent( this.notification.contentView.getLayoutId(), pintent); this.notification.contentIntent = pintent; } } public void setIcon(Drawable icon) { if (remoteViewInnerids[0] > 0) { BitmapDrawable bd = (BitmapDrawable) icon; this.notification.contentView.setImageViewBitmap( remoteViewInnerids[0], bd.getBitmap()); } } public static int getNotificationIdByTaskId(long taskId) { return Long.valueOf(taskId).intValue(); } public Notification getNotification() { return notification; } public void setNotification(Notification notification) { this.notification = notification; } private int[] fetchNecessaryIds() { int[] rtn = { 0, 0, 0, 0, 0, 0, 0, 0 };// order:icon,title,text,text1,text2,info,time,progress Field[] fields = null; try { Class cls = Class.forName("com.android.internal.R$id"); fields = cls.getFields(); } catch (ClassNotFoundException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } for (int i = 0; i < fields.length; i++) { Field fd = fields[i]; String fname = fd.getName(); int fvalue = 0; try { fvalue = fd.getInt(fname); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } if (fname.equals("icon")) { rtn[0] = fvalue; } if (fname.equals("title")) { rtn[1] = fvalue; } if (fname.equals("text")) { rtn[2] = fvalue; } if (fname.equals("text1")) { rtn[3] = fvalue; } if (fname.equals("text2")) { rtn[4] = fvalue; } if (fname.equals("info")) { rtn[5] = fvalue; } if (fname.equals("time")) { rtn[6] = fvalue; } if (fname.equals("progress")) { rtn[7] = fvalue; } } return rtn; } private void findProgressBarInRemoteViewLayout(View paramView) { if (paramView == null) return; out: if (paramView instanceof ViewGroup) { for (int i = 0; i < ((ViewGroup) paramView).getChildCount(); ++i) { View localView = ((ViewGroup) paramView).getChildAt(i); if (localView instanceof ProgressBar) { existProgressBar = true; break out; } if (localView instanceof ViewGroup) findProgressBarInRemoteViewLayout(localView); } } } }

6.0 手机上正常使用,7.0 小米上remoteView 为null ,导致功能有点问题,随后修复。

DBUtil 类

对下载数据进行入库,查询,删除 操作

 

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
package com.downloader.sdk.dlplugin.util.db; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.text.TextUtils; import android.util.Log; import com.downloader.sdk.dlplugin.DownloadTask; import com.downloader.sdk.dlplugin.DownloadTaskStatus; import com.downloader.sdk.dlplugin.util.log.Logger; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; import java.util.List; /** * * */ /** * @author ycw */ public class DBUtil extends SQLiteOpenHelper { private static DBUtil mInstance = null; private SQLiteDatabase mDb; private Context mContext; private boolean mIsSaveOldData; private static String dataBaseName = "dlplugin_database"; private static final String TAG = "DBUtil"; private static final int DATABASE_VERSION = 59; private static final String DATABASE_TABLE_DOWNLOAD = "tb_download"; private static final String DATABASE_TABLE_CONFIG = "tb_config"; /** * Database creation sql statement */ private static final String CREATE_DOWNLOAD_TABLE = "create table " + DATABASE_TABLE_DOWNLOAD + " (" + DownloadTask.KEY_ROWID + " integer primary key autoincrement, " + DownloadTask.KEY_NAME + " text not null, " + DownloadTask.KEY_CNNAME + " text ," + DownloadTask.KEY_FILESIZE + " INTEGER NOT NULL DEFAULT 0," + DownloadTask.KEY_CREATETIME + " INTEGER NOT NULL DEFAULT 0," + DownloadTask.KEY_URL + " text not null, " + DownloadTask.KEY_PACKAGE_NAME + " text, " + DownloadTask.KEY_DOWNLOAD_STRATEGY + " text, " + DownloadTask.KEY_STATUS + " INTEGER NOT NULL DEFAULT 0," + DownloadTask.KEY_INSTALLED + " INTEGER NOT NULL DEFAULT 0," + DownloadTask.KEY_SAVEPATH + " text );"; private static final String CREATE_CONFIG_TABLE = "create table " + DATABASE_TABLE_CONFIG + " (_id integer primary key autoincrement, latestCleanTime INTEGER NOT NULL DEFAULT 0);"; private static final String DROP_DOWNLOAD_TABLE = "DROP TABLE IF EXISTS " + DATABASE_TABLE_DOWNLOAD; private static final String DROP_CONFIG_TABLE = "DROP TABLE IF EXISTS " + DATABASE_TABLE_CONFIG; private DBUtil(Context context, String dataBaseName, boolean isSaveOldDataBase) { super(context, dataBaseName, null, DATABASE_VERSION); handleDb(context, dataBaseName, isSaveOldDataBase); this.dataBaseName = dataBaseName; this.mIsSaveOldData = isSaveOldDataBase; this.mContext = context; try { mDb = getWritableDatabase(); } catch (Exception e) { mDb = getReadableDatabase(); } } public static void handleDb(Context context, String dataBaseName, boolean isSaveOldData) { if (isSaveOldData) { // 老数据库存在,数据库存在 ,重命名老数据库,删除 新数据库 if (DBUtil.dataBaseExits(context, "dlplugin_database") && DBUtil.dataBaseExits(context, dataBaseName)) { DBUtil.delDataBase(context, dataBaseName); DBUtil.reNameDataBase(context, "dlplugin_database", dataBaseName); Logger.d(TAG, "老数据库存在,新数据库存在 ,删除新数据库,重命名老数据库"); // 老数据库不存在,新数据库存在 ,打开新数据库 对象 } } else { if (DBUtil.dataBaseExits(context, "dlplugin_database") && DBUtil.dataBaseExits(context, dataBaseName)) { DBUtil.delDataBase(context, dataBaseName); DBUtil.delDataBase(context, "dlplugin_database"); } } } /** * 单例 */ public static synchronized DBUtil getInstance(Context context, String dataBaseName, boolean isSaveOldData) { if (mInstance == null) { mInstance = new DBUtil(context, dataBaseName, isSaveOldData); } return mInstance; } @Override public void onCreate(SQLiteDatabase db) { Log.i(TAG, "Creating DataBase: " + CREATE_DOWNLOAD_TABLE); if (mIsSaveOldData) { if (oldDataBaseExits(mContext) && !"dlplugin_database".equals(dataBaseName)) { Logger.e(TAG, "old db extist!!"); File oldDataBaseFile = mContext.getApplicationContext().getDatabasePath( "dlplugin_database"); File newDataBaseFile = new File(oldDataBaseFile.getParentFile().getAbsolutePath() + File.separator + dataBaseName); if (newDataBaseFile.exists()) { newDataBaseFile.delete(); Logger.e(TAG, "delete init null db "); } copyDataBase(mContext); // 重新指定 数据库连接 // 得到数据库对象 Logger.e(TAG, "open not null db "); if (mDb != null) { mDb.close(); } try { mDb = getWritableDatabase(); Logger.e(TAG, "get getWritableDatabase"); } catch (Exception e) { mDb = getReadableDatabase(); Logger.e(TAG, "get getReadableDatabase"); } } else { createTable(db); } } else { try { if (oldDataBaseExits(mContext)) { File oldDataBaseFile = mContext.getApplicationContext().getDatabasePath( "dlplugin_database"); if (oldDataBaseFile.exists()) { oldDataBaseFile.delete(); } } } catch (Exception e) { // TODO Auto-generated catch block Logger.e(TAG, e.toString()); } createTable(db); } } public void createTable(SQLiteDatabase db) { db.execSQL(CREATE_DOWNLOAD_TABLE); db.execSQL(CREATE_CONFIG_TABLE); db.execSQL("insert into " + DATABASE_TABLE_CONFIG + "(latestCleanTime) values(" + System.currentTimeMillis() + ")"); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.execSQL(DROP_DOWNLOAD_TABLE); db.execSQL(CREATE_DOWNLOAD_TABLE); db.execSQL(DROP_CONFIG_TABLE); db.execSQL(CREATE_CONFIG_TABLE); db.execSQL("insert into " + DATABASE_TABLE_CONFIG + "(latestCleanTime) values(" + System.currentTimeMillis() + ")"); Log.w(TAG, "Upgrading database from version " + oldVersion + " to " + newVersion); } public void close() { this.close(); } public void truncateDownloadTable() { mDb.execSQL("delete from " + DATABASE_TABLE_DOWNLOAD, new Object[] {}); mDb.execSQL("update sqlite_sequence set seq=0 where name= '" + DATABASE_TABLE_DOWNLOAD + "'", new Object[] {}); } public long insertDownload(String name, String cnname, String url, String packageName, String downloadStrategy) { ContentValues initialValues = new ContentValues(); initialValues.put(DownloadTask.KEY_NAME, name); initialValues.put(DownloadTask.KEY_CNNAME, cnname); // initialValues.put(DownloadEntity.KEY_FILESIZE, name); initialValues.put(DownloadTask.KEY_CREATETIME, System.currentTimeMillis()); initialValues.put(DownloadTask.KEY_URL, url); if (packageName != null) initialValues.put(DownloadTask.KEY_PACKAGE_NAME, packageName); if (downloadStrategy != null) initialValues.put(DownloadTask.KEY_DOWNLOAD_STRATEGY, downloadStrategy); initialValues.put(DownloadTask.KEY_STATUS, 0); initialValues.put(DownloadTask.KEY_INSTALLED, 0); return mDb.insert(DATABASE_TABLE_DOWNLOAD, null, initialValues); } public long insertDownload(String name, String cnname, String url, String packageName, String downloadStrategy, int stauts) { ContentValues initialValues = new ContentValues(); initialValues.put(DownloadTask.KEY_NAME, name); initialValues.put(DownloadTask.KEY_CNNAME, cnname); // initialValues.put(DownloadEntity.KEY_FILESIZE, name); initialValues.put(DownloadTask.KEY_CREATETIME, System.currentTimeMillis()); initialValues.put(DownloadTask.KEY_URL, url); if (packageName != null) initialValues.put(DownloadTask.KEY_PACKAGE_NAME, packageName); if (downloadStrategy != null) initialValues.put(DownloadTask.KEY_DOWNLOAD_STRATEGY, downloadStrategy); initialValues.put(DownloadTask.KEY_STATUS, stauts); initialValues.put(DownloadTask.KEY_INSTALLED, 0); return mDb.insert(DATABASE_TABLE_DOWNLOAD, null, initialValues); } public boolean deleteDownload(long rowId) { return mDb.delete(DATABASE_TABLE_DOWNLOAD, DownloadTask.KEY_ROWID + "=" + rowId, null) > 0; } public Cursor fetchAllDownload() { return mDb.query(DATABASE_TABLE_DOWNLOAD, new String[] { DownloadTask.KEY_ROWID, DownloadTask.KEY_NAME, DownloadTask.KEY_CNNAME, DownloadTask.KEY_URL, DownloadTask.KEY_STATUS, DownloadTask.KEY_INSTALLED }, null, null, null, null, null); } public List<DownloadTask> findUnCompletedTask() { List<DownloadTask> result = new ArrayList<DownloadTask>(); Cursor mCursor = null; try { mCursor = mDb .query(true, DATABASE_TABLE_DOWNLOAD, new String[] { DownloadTask.KEY_ROWID, DownloadTask.KEY_NAME, DownloadTask.KEY_CNNAME, DownloadTask.KEY_FILESIZE, DownloadTask.KEY_CREATETIME, DownloadTask.KEY_URL, DownloadTask.KEY_PACKAGE_NAME, DownloadTask.KEY_DOWNLOAD_STRATEGY, DownloadTask.KEY_STATUS, DownloadTask.KEY_INSTALLED, DownloadTask.KEY_SAVEPATH }, DownloadTask.KEY_STATUS + "<" + DownloadTaskStatus.COMPLETED + " and " + DownloadTask.KEY_STATUS + ">=" + DownloadTaskStatus.WAITING, null, null, null, "_id", null); if (mCursor.getCount() > 0) { while (mCursor.moveToNext()) { DownloadTask entity = new DownloadTask() .setId(mCursor.getLong(0)) .setName(mCursor.getString(1)) .setCnname(mCursor.getString(2)) .setFilesize(mCursor.getLong(3)) .setCreatetime(mCursor.getLong(4)) .setUrl(mCursor.getString(5)) .setPackageName(mCursor.getString(6)) .setDownloadStrategy(mCursor.getString(7)) .setStatus(mCursor.getInt(8)) .setInstalled(mCursor.getInt(9)) .setSavepath(mCursor.getString(10)) .setFileTypeByUrl( mCursor.getString(10) == null ? mCursor .getString(5) : mCursor .getString(10)); result.add(entity); } } } catch (Exception e) { return result; } finally { if (mCursor != null) { mCursor.close(); } } return result; } public List<DownloadTask> findDownLoadingTask() { List<DownloadTask> result = new ArrayList<DownloadTask>(); Cursor mCursor = null; try { mCursor = mDb.query(true, DATABASE_TABLE_DOWNLOAD, new String[] { DownloadTask.KEY_ROWID, DownloadTask.KEY_NAME, DownloadTask.KEY_CNNAME, DownloadTask.KEY_FILESIZE, DownloadTask.KEY_CREATETIME, DownloadTask.KEY_URL, DownloadTask.KEY_PACKAGE_NAME, DownloadTask.KEY_DOWNLOAD_STRATEGY, DownloadTask.KEY_STATUS, DownloadTask.KEY_INSTALLED, DownloadTask.KEY_SAVEPATH }, DownloadTask.KEY_STATUS + "==" + DownloadTaskStatus.DOWNLOADING, null, null, null, "_id", null); if (mCursor.getCount() > 0) { while (mCursor.moveToNext()) { DownloadTask entity = new DownloadTask() .setId(mCursor.getLong(0)) .setName(mCursor.getString(1)) .setCnname(mCursor.getString(2)) .setFilesize(mCursor.getLong(3)) .setCreatetime(mCursor.getLong(4)) .setUrl(mCursor.getString(5)) .setPackageName(mCursor.getString(6)) .setDownloadStrategy(mCursor.getString(7)) .setStatus(mCursor.getInt(8)) .setInstalled(mCursor.getInt(9)) .setSavepath(mCursor.getString(10)) .setFileTypeByUrl( mCursor.getString(10) == null ? mCursor .getString(5) : mCursor .getString(10)); result.add(entity); } } } catch (Exception e) { return result; } finally { if (mCursor != null) { mCursor.close(); } } return result; } public DownloadTask fetchOneDownload(long id) { DownloadTask entity = null; Cursor mCursor = null; try { mCursor = mDb.query(true, DATABASE_TABLE_DOWNLOAD, new String[] { DownloadTask.KEY_ROWID, DownloadTask.KEY_NAME, DownloadTask.KEY_CNNAME, DownloadTask.KEY_FILESIZE, DownloadTask.KEY_CREATETIME, DownloadTask.KEY_URL, DownloadTask.KEY_PACKAGE_NAME, DownloadTask.KEY_DOWNLOAD_STRATEGY, DownloadTask.KEY_STATUS, DownloadTask.KEY_INSTALLED, DownloadTask.KEY_SAVEPATH }, DownloadTask.KEY_ROWID + "=" + id, null, null, null, null, null); if (mCursor.getCount() > 0) { mCursor.moveToFirst(); entity = new DownloadTask() .setId(mCursor.getLong(0)) .setName(mCursor.getString(1)) .setCnname(mCursor.getString(2)) .setFilesize(mCursor.getLong(3)) .setCreatetime(mCursor.getLong(4)) .setUrl(mCursor.getString(5)) .setPackageName(mCursor.getString(6)) .setDownloadStrategy(mCursor.getString(7)) .setStatus(mCursor.getInt(8)) .setInstalled(mCursor.getInt(9)) .setSavepath(mCursor.getString(10)) .setFileTypeByUrl( mCursor.getString(10) == null ? mCursor .getString(5) : mCursor.getString(10)); } } catch (Exception e) { return null; } finally { if (mCursor != null) { mCursor.close(); } } return entity; } public DownloadTask findDownloadByUrl(String url) { DownloadTask entity = null; Cursor mCursor = null; try { mCursor = mDb.query(true, DATABASE_TABLE_DOWNLOAD, new String[] { DownloadTask.KEY_ROWID, DownloadTask.KEY_NAME, DownloadTask.KEY_CNNAME, DownloadTask.KEY_FILESIZE, DownloadTask.KEY_CREATETIME, DownloadTask.KEY_URL, DownloadTask.KEY_PACKAGE_NAME, DownloadTask.KEY_DOWNLOAD_STRATEGY, DownloadTask.KEY_STATUS, DownloadTask.KEY_INSTALLED, DownloadTask.KEY_SAVEPATH }, DownloadTask.KEY_URL + "='" + url + "'", null, null, null, null, null); if (mCursor.getCount() > 0) { mCursor.moveToFirst(); entity = new DownloadTask(); entity = entity .setId(mCursor.getLong(0)) .setName(mCursor.getString(1)) .setCnname(mCursor.getString(2)) .setFilesize(mCursor.getLong(3)) .setCreatetime(mCursor.getLong(4)) .setUrl(mCursor.getString(5)) .setPackageName(mCursor.getString(6)) .setDownloadStrategy(mCursor.getString(7)) .setStatus(mCursor.getInt(8)) .setInstalled(mCursor.getInt(9)) .setSavepath(mCursor.getString(10)) .setFileTypeByUrl( mCursor.getString(10) == null ? mCursor .getString(5) : mCursor.getString(10)); } } catch (Exception e) { return null; } finally { if (mCursor != null) { mCursor.close(); } } return entity; } public ArrayList<DownloadTask> findDownloadByPackageName(String packageName) { ArrayList<DownloadTask> mDownloadTasksList = new ArrayList<DownloadTask>(); Cursor mCursor = null; try { mCursor = mDb.query(true, DATABASE_TABLE_DOWNLOAD, new String[] { DownloadTask.KEY_ROWID, DownloadTask.KEY_NAME, DownloadTask.KEY_CNNAME, DownloadTask.KEY_FILESIZE, DownloadTask.KEY_CREATETIME, DownloadTask.KEY_URL, DownloadTask.KEY_PACKAGE_NAME, DownloadTask.KEY_DOWNLOAD_STRATEGY, DownloadTask.KEY_STATUS, DownloadTask.KEY_INSTALLED, DownloadTask.KEY_SAVEPATH }, DownloadTask.KEY_PACKAGE_NAME + "='" + packageName + "' and " + DownloadTask.KEY_STATUS + "=" + DownloadTaskStatus.COMPLETED, null, null, null, null, null); if (mCursor.getCount() > 0) { while (mCursor.moveToNext()) { DownloadTask entity = new DownloadTask(); entity = entity .setId(mCursor.getLong(0)) .setName(mCursor.getString(1)) .setCnname(mCursor.getString(2)) .setFilesize(mCursor.getLong(3)) .setCreatetime(mCursor.getLong(4)) .setUrl(mCursor.getString(5)) .setPackageName(mCursor.getString(6)) .setDownloadStrategy(mCursor.getString(7)) .setStatus(mCursor.getInt(8)) .setInstalled(mCursor.getInt(9)) .setSavepath(mCursor.getString(10)) .setFileTypeByUrl( mCursor.getString(10) == null ? mCursor .getString(5) : mCursor .getString(10)); mDownloadTasksList.add(entity); } } } catch (Exception e) { return null; } finally { if (mCursor != null) { mCursor.close(); } } return mDownloadTasksList; } public boolean updateDownload(long id, String name, String cnname, String url, int status, int installed, String savepath, String packageName) { ContentValues args = new ContentValues(); if (name != null && !name.equals("")) args.put(DownloadTask.KEY_NAME, name); if (cnname != null && !cnname.equals("")) args.put(DownloadTask.KEY_CNNAME, cnname); if (url != null && !url.equals("")) args.put(DownloadTask.KEY_URL, url); if (status >= 0) args.put(DownloadTask.KEY_STATUS, status); if (installed == 0 || installed == 1) args.put(DownloadTask.KEY_INSTALLED, installed); if (savepath != null && !savepath.equals("")) args.put(DownloadTask.KEY_SAVEPATH, savepath); if (packageName != null && !packageName.equals("")) args.put(DownloadTask.KEY_PACKAGE_NAME, packageName); return mDb.update(DATABASE_TABLE_DOWNLOAD, args, DownloadTask.KEY_ROWID + "=" + id, null) > 0; } public boolean updateDownloadStrategyById(long id, String downloadStrategyStr) { ContentValues args = new ContentValues(); if (!TextUtils.isEmpty(downloadStrategyStr)) { args.put(DownloadTask.KEY_DOWNLOAD_STRATEGY, downloadStrategyStr); } return mDb.update(DATABASE_TABLE_DOWNLOAD, args, DownloadTask.KEY_ROWID + "=" + id, null) > 0; } // 清除过期的下载记录 public void removeCompletedDownloadTask() { if (mDb.delete(DATABASE_TABLE_DOWNLOAD, DownloadTask.KEY_STATUS + "=" + DownloadTaskStatus.COMPLETED, null) > 0) { udpateLatestCleanTime(System.currentTimeMillis()); } } public long getLatestCleanTime() { long returnVal = -1; Cursor mCursor = null; try { mCursor = mDb.query(true, DATABASE_TABLE_CONFIG, new String[] { "_id", "latestCleanTime" }, "_id=1", null, null, null, null, null); if (mCursor.getCount() > 0) { mCursor.moveToFirst(); returnVal = mCursor.getLong(1); if (returnVal == 0) returnVal = -1; } } catch (Exception e) { Log.i(TAG, "getLatestCleanTime: error!"); } finally { if (mCursor != null) { mCursor.close(); } } return returnVal; } public boolean udpateLatestCleanTime(long updateTime) { ContentValues args = new ContentValues(); if (updateTime > 0) args.put("latestCleanTime", updateTime); return mDb.update(DATABASE_TABLE_CONFIG, args, "_id=1", null) > 0; } public ArrayList<DownloadTask> findAllTask() { ArrayList<DownloadTask> result = new ArrayList<DownloadTask>(); Cursor mCursor = null; try { mCursor = mDb.query(true, DATABASE_TABLE_DOWNLOAD, new String[] { DownloadTask.KEY_ROWID, DownloadTask.KEY_NAME, DownloadTask.KEY_CNNAME, DownloadTask.KEY_FILESIZE, DownloadTask.KEY_CREATETIME, DownloadTask.KEY_URL, DownloadTask.KEY_PACKAGE_NAME, DownloadTask.KEY_DOWNLOAD_STRATEGY, DownloadTask.KEY_STATUS, DownloadTask.KEY_INSTALLED, DownloadTask.KEY_SAVEPATH }, null, null, null, null, "_id", null); if (mCursor.getCount() > 0) { while (mCursor.moveToNext()) { DownloadTask entity = new DownloadTask() .setId(mCursor.getLong(0)) .setName(mCursor.getString(1)) .setCnname(mCursor.getString(2)) .setFilesize(mCursor.getLong(3)) .setCreatetime(mCursor.getLong(4)) .setUrl(mCursor.getString(5)) .setPackageName(mCursor.getString(6)) .setDownloadStrategy(mCursor.getString(7)) .setStatus(mCursor.getInt(8)) .setInstalled(mCursor.getInt(9)) .setSavepath(mCursor.getString(10)) .setFileTypeByUrl( mCursor.getString(10) == null ? mCursor .getString(5) : mCursor .getString(10)); result.add(entity); } } } catch (Exception e) { return result; } finally { if (mCursor != null) { mCursor.close(); } } return result; } public ArrayList<DownloadTask> findAllTempTaskByStatus() { ArrayList<DownloadTask> downLoadTaskList = new ArrayList<DownloadTask>(); Cursor mCursor = null; try { mCursor = mDb.query(true, DATABASE_TABLE_DOWNLOAD, new String[] { DownloadTask.KEY_ROWID, DownloadTask.KEY_NAME, DownloadTask.KEY_CNNAME, DownloadTask.KEY_FILESIZE, DownloadTask.KEY_CREATETIME, DownloadTask.KEY_URL, DownloadTask.KEY_PACKAGE_NAME, DownloadTask.KEY_DOWNLOAD_STRATEGY, DownloadTask.KEY_STATUS, DownloadTask.KEY_INSTALLED, DownloadTask.KEY_SAVEPATH }, DownloadTask.KEY_STATUS + "=" + DownloadTaskStatus.TEMP_NODOWNLOAD, null, null, null, "_id", null); if (mCursor.getCount() > 0) { while (mCursor.moveToNext()) { DownloadTask entity = new DownloadTask() .setId(mCursor.getLong(0)) .setName(mCursor.getString(1)) .setCnname(mCursor.getString(2)) .setFilesize(mCursor.getLong(3)) .setCreatetime(mCursor.getLong(4)) .setUrl(mCursor.getString(5)) .setPackageName(mCursor.getString(6)) .setDownloadStrategy(mCursor.getString(7)) .setStatus(mCursor.getInt(8)) .setInstalled(mCursor.getInt(9)) .setSavepath(mCursor.getString(10)) .setFileTypeByUrl( mCursor.getString(10) == null ? mCursor .getString(5) : mCursor .getString(10)); downLoadTaskList.add(entity); } } } catch (Exception e) { } finally { if (mCursor != null) { mCursor.close(); } } return downLoadTaskList; } public static String getDATABASE_NAME() { return dataBaseName; } public void setDATABASE_NAME(String dATABASE_NAME) { dataBaseName = dATABASE_NAME; } /** * 重命名数据库达到 复制老数据库 的效果 ` * * @param context */ public void copyDataBase(Context context) { Logger.e(TAG, "prepare to copy olddb!"); if (context != null) { File oldDataBaseFile = context.getApplicationContext().getDatabasePath( "dlplugin_database"); if (oldDataBaseFile.exists() && !"dlplugin_database".equals(dataBaseName)) { File newDataBaseFile = new File(oldDataBaseFile.getParentFile().getAbsolutePath() + File.separator + dataBaseName); if (newDataBaseFile.exists()) { Logger.e(TAG, "newDataBaseFile ---exists!"); } FileInputStream inputStream = null; FileOutputStream outputStream = null; try { inputStream = new FileInputStream(oldDataBaseFile); outputStream = new FileOutputStream(newDataBaseFile); byte[] buffer = new byte[1024]; int len = 0; try { while ((len = inputStream.read(buffer)) != -1) { outputStream.write(buffer, 0, len); } outputStream.flush(); oldDataBaseFile.delete(); } catch (IOException e) { // TODO Auto-generated catch block Logger.e(TAG, e.toString()); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block Logger.e(TAG, e.toString()); } finally { if (outputStream != null) { try { outputStream.close(); } catch (IOException e) { // TODO Auto-generated catch block Logger.e(TAG, e.toString()); } } if (inputStream != null) { try { inputStream.close(); } catch (IOException e) { // TODO Auto-generated catch block Logger.e(TAG, e.toString()); } } } } } Logger.e(TAG, " db copy already!"); } /** * 判断老数据库是否 存在 * * @param context * @return */ public boolean oldDataBaseExits(Context context) { boolean result = false; if (context != null) { File oldDataBaseFile = context.getApplicationContext().getDatabasePath( "dlplugin_database"); if (oldDataBaseFile.exists()) { result = true; } } return result; } /** * 判断老数据库是否 存在 * * @param context * @return */ public static boolean dataBaseExits(Context context, String dbName) { boolean result = false; if (context != null) { File oldDataBaseFile = context.getApplicationContext().getDatabasePath( dbName); if (oldDataBaseFile.exists()) { result = true; } } return result; } /** * 判断老数据库是否 存在 * * @param context * @return */ public static boolean delDataBase(Context context, String dbName) { boolean result = false; if (context != null) { File dataBaseFile = context.getApplicationContext().getDatabasePath( dbName); if (dataBaseFile.exists()) { result = dataBaseFile.delete(); } } return result; } /** * 重命名数据库达到 复制老数据库 的效果 ` * * @param context */ public static void reNameDataBase(Context context, String oldDataBaseName, String newDataBaseName) { Logger.e(TAG, "prepare to copy olddb!"); if (context != null) { File oldDataBaseFile = context.getApplicationContext().getDatabasePath( oldDataBaseName); if (oldDataBaseFile.exists() && !"dlplugin_database".equals(dataBaseName)) { File newDataBaseFile = new File(oldDataBaseFile.getParentFile().getAbsolutePath() + File.separator + dataBaseName); if (!newDataBaseFile.exists()) { Logger.e(TAG, "newDataBaseFile ---exists!"); oldDataBaseFile.renameTo(newDataBaseFile); } } } Logger.e(TAG, " db copy already!"); } }

此外附上github 项目地址:https://github.com/15811472463/downloader.git

   

 

 

 

 

 

         

最后

以上就是自然发卡最近收集整理的关于android 多线程下载器 源码解析的全部内容,更多相关android内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(89)

评论列表共有 0 条评论

立即
投稿
返回
顶部