/
Cocos2dxDownloader.java
325 lines (279 loc) · 11.1 KB
/
Cocos2dxDownloader.java
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
package org.cocos2dx.lib;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.BinaryHttpResponseHandler;
import com.loopj.android.http.FileAsyncHttpResponseHandler;
import com.loopj.android.http.RequestHandle;
import cz.msebera.android.httpclient.Header;
import cz.msebera.android.httpclient.message.BasicHeader;
import java.io.File;
import java.util.*;
class DataTaskHandler extends BinaryHttpResponseHandler {
int _id;
private Cocos2dxDownloader _downloader;
private long _lastBytesWritten;
void LogD(String msg) {
android.util.Log.d("Cocos2dxDownloader", msg);
}
public DataTaskHandler(Cocos2dxDownloader downloader, int id) {
super(new String[]{".*"});
_downloader = downloader;
_id = id;
_lastBytesWritten = 0;
}
@Override
public void onProgress(long bytesWritten, long totalSize) {
//LogD("onProgress(bytesWritten:" + bytesWritten + " totalSize:" + totalSize);
long dlBytes = bytesWritten - _lastBytesWritten;
long dlNow = bytesWritten;
long dlTotal = totalSize;
_downloader.onProgress(_id, dlBytes, dlNow, dlTotal);
_lastBytesWritten = bytesWritten;
}
@Override
public void onStart() {
_downloader.onStart(_id);
}
@Override
public void onFailure(int i, Header[] headers, byte[] errorResponse, Throwable throwable) {
LogD("onFailure(i:" + i + " headers:" + headers + " throwable:" + throwable);
String errStr = "";
if (null != throwable) {
errStr = throwable.toString();
}
_downloader.onFinish(_id, i, errStr, null);
}
@Override
public void onSuccess(int i, Header[] headers, byte[] binaryData) {
LogD("onSuccess(i:" + i + " headers:" + headers);
_downloader.onFinish(_id, 0, null, binaryData);
}
}
class FileTaskHandler extends FileAsyncHttpResponseHandler {
int _id;
File _finalFile;
private long _initFileLen;
private long _lastBytesWritten;
private Cocos2dxDownloader _downloader;
void LogD(String msg) {
android.util.Log.d("Cocos2dxDownloader", msg);
}
public FileTaskHandler(Cocos2dxDownloader downloader, int id, File temp, File finalFile) {
super(temp, true);
_finalFile = finalFile;
_downloader = downloader;
_id = id;
_initFileLen = getTargetFile().length();
_lastBytesWritten = 0;
}
@Override
public void onProgress(long bytesWritten, long totalSize) {
//LogD("onProgress(bytesWritten:" + bytesWritten + " totalSize:" + totalSize);
long dlBytes = bytesWritten - _lastBytesWritten;
long dlNow = bytesWritten + _initFileLen;
long dlTotal = totalSize + _initFileLen;
_downloader.onProgress(_id, dlBytes, dlNow, dlTotal);
_lastBytesWritten = bytesWritten;
}
@Override
public void onStart() {
_downloader.onStart(_id);
}
@Override
public void onFinish() {
// onFinish called after onSuccess/onFailure
_downloader.runNextTaskIfExists();
}
@Override
public void onFailure(int i, Header[] headers, Throwable throwable, File file) {
LogD("onFailure(i:" + i + " headers:" + headers + " throwable:" + throwable + " file:" + file);
String errStr = "";
if (null != throwable) {
errStr = throwable.toString();
}
_downloader.onFinish(_id, i, errStr, null);
}
@Override
public void onSuccess(int i, Header[] headers, File file) {
LogD("onSuccess(i:" + i + " headers:" + headers + " file:" + file);
String errStr = null;
do {
// rename temp file to final file
// if final file exist, remove it
if (_finalFile.exists()) {
if (_finalFile.isDirectory()) {
errStr = "Dest file is directory:" + _finalFile.getAbsolutePath();
break;
}
if (false == _finalFile.delete()) {
errStr = "Can't remove old file:" + _finalFile.getAbsolutePath();
break;
}
}
File tempFile = getTargetFile();
tempFile.renameTo(_finalFile);
} while (false);
_downloader.onFinish(_id, 0, errStr, null);
}
}
class DownloadTask {
DownloadTask() {
handle = null;
handler = null;
resetStatus();
}
void resetStatus() {
bytesReceived = 0;
totalBytesReceived = 0;
totalBytesExpected = 0;
data = null;
}
RequestHandle handle;
AsyncHttpResponseHandler handler;
// progress
long bytesReceived;
long totalBytesReceived;
long totalBytesExpected;
byte[] data;
}
public class Cocos2dxDownloader {
private int _id;
private AsyncHttpClient _httpClient = new AsyncHttpClient();
private String _tempFileNameSufix;
private int _countOfMaxProcessingTasks;
private HashMap _taskMap = new HashMap();
private Queue<Runnable> _taskQueue = new LinkedList<Runnable>();
private int _runningTaskCount = 0;
void onProgress(final int id, final long downloadBytes, final long downloadNow, final long downloadTotal) {
DownloadTask task = (DownloadTask)_taskMap.get(id);
if (null != task) {
task.bytesReceived = downloadBytes;
task.totalBytesReceived = downloadNow;
task.totalBytesExpected = downloadTotal;
}
Cocos2dxHelper.runOnGLThread(new Runnable() {
@Override
public void run() {
nativeOnProgress(_id, id, downloadBytes, downloadNow, downloadTotal);
}
});
}
public void onStart(int id) {
DownloadTask task = (DownloadTask)_taskMap.get(id);
if (null != task) {
task.resetStatus();
}
}
public void onFinish(final int id, final int errCode, final String errStr, final byte[] data) {
DownloadTask task = (DownloadTask)_taskMap.get(id);
if (null == task) return;
_taskMap.remove(id);
Cocos2dxHelper.runOnGLThread(new Runnable() {
@Override
public void run() {
nativeOnFinish(_id, id, errCode, errStr, data);
}
});
}
public static Cocos2dxDownloader createDownloader(int id, int timeoutInSeconds, String tempFileNameSufix, int countOfMaxProcessingTasks) {
Cocos2dxDownloader downloader = new Cocos2dxDownloader();
downloader._id = id;
downloader._httpClient.setEnableRedirects(true);
if (timeoutInSeconds > 0) {
downloader._httpClient.setTimeout(timeoutInSeconds * 1000);
}
// downloader._httpClient.setMaxRetriesAndTimeout(3, timeoutInSeconds * 1000);
downloader._httpClient.allowRetryExceptionClass(javax.net.ssl.SSLException.class);
downloader._tempFileNameSufix = tempFileNameSufix;
downloader._countOfMaxProcessingTasks = countOfMaxProcessingTasks;
return downloader;
}
public static void createTask(final Cocos2dxDownloader downloader, int id_, String url_, String path_) {
final int id = id_;
final String url = url_;
final String path = path_;
Runnable taskRunnable = new Runnable() {
@Override
public void run() {
DownloadTask task = new DownloadTask();
if (0 == path.length()) {
// data task
task.handler = new DataTaskHandler(downloader, id);
task.handle = downloader._httpClient.get(Cocos2dxHelper.getActivity(), url, task.handler);
}
do {
if (0 == path.length()) break;
// file task
File tempFile = new File(path + downloader._tempFileNameSufix);
if (tempFile.isDirectory()) break;
File parent = tempFile.getParentFile();
if (!parent.isDirectory() && !parent.mkdirs()) break;
File finalFile = new File(path);
if (tempFile.isDirectory()) break;
task.handler = new FileTaskHandler(downloader, id, tempFile, finalFile);
Header[] headers = null;
long fileLen = tempFile.length();
if (fileLen > 0) {
// continue download
List<Header> list = new ArrayList<Header>();
list.add(new BasicHeader("Range", "bytes=" + fileLen + "-"));
headers = list.toArray(new Header[list.size()]);
}
task.handle = downloader._httpClient.get(Cocos2dxHelper.getActivity(), url, headers, null, task.handler);
//task.handle = downloader._httpClient.get(url, task.handler);
} while (false);
if (null == task.handle) {
final String errStr = "Can't create DownloadTask for " + url;
Cocos2dxHelper.runOnGLThread(new Runnable() {
@Override
public void run() {
downloader.nativeOnFinish(downloader._id, id, 0, errStr, null);
}
});
} else {
downloader._taskMap.put(id, task);
}
}
};
downloader.enqueueTask(taskRunnable);
}
public static void cancelAllRequests(final Cocos2dxDownloader downloader) {
Cocos2dxHelper.getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
//downloader._httpClient.cancelAllRequests(true);
Iterator iter = downloader._taskMap.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry entry = (Map.Entry) iter.next();
//Object key = entry.getKey();
DownloadTask task = (DownloadTask) entry.getValue();
if (null != task.handle) {
task.handle.cancel(true);
}
}
}
});
}
public void enqueueTask(Runnable taskRunnable) {
synchronized (_taskQueue) {
if (_runningTaskCount < _countOfMaxProcessingTasks) {
Cocos2dxHelper.getActivity().runOnUiThread(taskRunnable);
_runningTaskCount++;
} else {
_taskQueue.add(taskRunnable);
}
}
}
public void runNextTaskIfExists() {
synchronized (_taskQueue) {
Runnable taskRunnable = Cocos2dxDownloader.this._taskQueue.poll();
if (taskRunnable != null) {
Cocos2dxHelper.getActivity().runOnUiThread(taskRunnable);
} else {
_runningTaskCount--;
}
}
}
native void nativeOnProgress(int id, int taskId, long dl, long dlnow, long dltotal);
native void nativeOnFinish(int id, int taskId, int errCode, String errStr, final byte[] data);
}