yy1717
2021-02-07 cea2a94fc97e79897cdfd217be8250c075974a1a
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
package com.anyun.exam.lib;
 
import android.app.DownloadManager;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.Cursor;
import android.net.Uri;
import android.os.Environment;
import android.util.Log;
 
import java.io.File;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
 
public class DownloadManagerUtil {
    public static final String TAG = DownloadManagerUtil.class.getCanonicalName();
    private Context context;
    private DownloadManager downloadManager = null;
    private static DownloadManagerUtil instance = null;
    private DownloadManagerCallback callback = null;
 
    class DownloadItem {
        long id;
        String fileName;
        String url;
 
        public DownloadItem (long id, String fileName, String url) {
            this.id = id;
            this.fileName = fileName;
            this.url = url;
        }
    }
 
    public static DownloadManagerUtil getInstance(Context context) {
        if (instance == null) {
            synchronized (DownloadManagerUtil.class) {
                if (instance == null) {
                    instance = new DownloadManagerUtil(context);
                }
            }
        }
        return instance;
    }
 
    private DownloadManagerUtil(Context context) {
        this.context = context;
        if (downloadManager == null) {
            downloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
        }
 
        RegisterDownload();
    }
 
    public void RegisterDownload() {
        IntentFilter filter = new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE);
        context.registerReceiver(mReceiver, filter);
    }
 
    public void SetDMCallback(final DownloadManagerCallback cb) {
        callback = cb;
    }
 
    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (action.equals(DownloadManager.ACTION_DOWNLOAD_COMPLETE)) {
                long id = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);
                DownloadManager.Query query = new DownloadManager.Query();
                Cursor cursor = downloadManager.query(query);
 
                while (cursor != null && cursor.moveToFirst()) {
                    int cid = cursor.getInt( cursor.getColumnIndex(DownloadManager.COLUMN_ID) );
                    int status = cursor.getInt( cursor.getColumnIndex(DownloadManager.COLUMN_STATUS) );
 
                    if (id == cid && status == DownloadManager.STATUS_SUCCESSFUL) {
                        String title = cursor.getString( cursor.getColumnIndex(DownloadManager.COLUMN_TITLE) );
                        String fileUri = cursor.getString(cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_LOCAL_URI));
                        Log.d(TAG, "下载完毕 " + fileUri);
 
                        if (callback != null) {
                            callback.DownloadComplete(title, Uri.parse(fileUri).getPath());
                        }
                        break;
                    }
                }
            }
        }
    };
 
    public void DownloadFile(String url, String saveFileName, String title, String desc) {
        Log.d(TAG, "下载文件 " + url);
        // 跳过已经在下载的
        if (CheckAndClear(title) == 1) {
            Log.d(TAG, "跳过已经在下载的 " + title);
            return;
        }
 
        DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
 
        File file = new File(context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), saveFileName);
        request.setDestinationUri(Uri.fromFile(file));
 
        request.setTitle(title);
        request.setDescription(desc);
 
        if (downloadManager != null) {
            long idx = downloadManager.enqueue(request);
            Log.d(TAG, "新建下载任务 id = " + idx);
        }
    }
 
    public int CheckAndClear(String title) {
        DownloadManager.Query query = new DownloadManager.Query();
        Cursor cursor = downloadManager.query(query);
        while (cursor != null && cursor.moveToNext()) {
            String theTitle = cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_TITLE));
            if (theTitle.equals(title)) {
                int status = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS));
                long id = cursor.getLong(cursor.getColumnIndex(DownloadManager.COLUMN_ID));
                Log.d(TAG, "当前该文件下载状态 " + status);
 
                if (status == DownloadManager.STATUS_FAILED || status == DownloadManager.STATUS_SUCCESSFUL) {
                    downloadManager.remove(id);
                    return 0;
                } else if (status != DownloadManager.STATUS_RUNNING) {
                    // 如果是多次处于暂停\挂起状态,还是删除
                    downloadManager.remove(id);
                    return 0;
                }
                return 1;
            }
        }
        return 0;
    }
 
    public int CheckDownloadStatus(int id) {
        DownloadManager.Query query = new DownloadManager.Query();
        Cursor cursor = downloadManager.query(query);
 
        while (cursor != null && cursor.moveToNext()) {
            int status = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS));
 
            if (id == cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_ID))) {
                return status;
            }
        }
        return -1;
    }
}