Repository navigation
Expand file tree
/
Copy pathRepositoryManager.java
More file actions
430 lines (386 loc) · 10.9 KB
/
Copy pathRepositoryManager.java
File metadata and controls
430 lines (386 loc) · 10.9 KB
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
package com.dianping.loader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Build;
import android.os.SystemClock;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.util.Log;
import com.dianping.loader.model.FileSpec;
public class RepositoryManager {
/**
* 已经下载并校验完毕,可以直接加载
*/
static final String STATUS_DONE = "DONE";
/**
* 未开始下载
*/
static final String STATUS_IDLE = "IDLE";
/**
* 当前下载
*/
static final String STATUS_RUNNING = "RUNNING";
/**
* call in the background thread
*/
static interface StatusChangeListener {
void onStatusChanged(FileSpec file, String newStatus);
}
private final Context context;
private final ConnectivityManager connManager;
// ./repo/<id>/<md5 or 1>.apk
// ./repo/<id>/dexout
private final File repoDir;
// ./repo/tmp/<id>.<random.4>
private final File tmpDir;
private final LinkedList<FileSpec> order = new LinkedList<FileSpec>();
private final HashMap<String, FileSpec> map = new HashMap<String, FileSpec>();
private final HashMap<String, String> status = new HashMap<String, String>();
private final HashMap<String, Integer> require = new HashMap<String, Integer>();
private final ArrayList<StatusChangeListener> listeners = new ArrayList<StatusChangeListener>();
private Worker running;
public RepositoryManager(Context context) {
this.context = context;
ConnectivityManager cm = null;
try {
cm = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
} catch (Exception e) {
Log.w("loader",
"repository manager start without connectivity manager", e);
}
connManager = cm;
repoDir = new File(context.getFilesDir(), "repo");
repoDir.mkdir();
tmpDir = new File(repoDir, "tmp");
tmpDir.mkdir();
File[] tmps = tmpDir.listFiles();
if (tmps != null) {
for (File tmpf : tmps) {
tmpf.delete();
}
}
disableConnectionReuseIfNecessary();
}
//
// internal
//
void addListener(StatusChangeListener listener) {
listeners.add(listener);
}
void removeListener(StatusChangeListener listener) {
listeners.remove(listener);
}
synchronized int runningCount() {
int count = 0;
for (String str : status.values()) {
if (STATUS_RUNNING == str)
count++;
}
return count;
}
synchronized int totalCount() {
return map.size();
}
synchronized String getStatus(String fileId) {
if (status.get(fileId) == null) {
FileSpec file = map.get(fileId);
if (file != null) {
File path = getPath(file);
status.put(fileId, path.isFile() ? STATUS_DONE : STATUS_IDLE);
} else {
return null;
}
}
return status.get(fileId);
}
File getDir() {
return repoDir;
}
public File getPath(FileSpec file) {
File dir = new File(repoDir, file.id());
File path = new File(dir, TextUtils.isEmpty(file.md5()) ? "1.apk"
: file.md5() + ".apk");
return path;
}
public synchronized void addFiles(FileSpec[] files) {
for (FileSpec f : files) {
map.put(f.id(), f);
}
for (FileSpec f : files) {
appendDepsList(map, order, f.id());
}
}
// return false if missing file or loop deps
public boolean appendDepsList(List<FileSpec> list, String fileId) {
return appendDepsList(map, list, fileId);
}
// return false if missing file or loop deps
public static boolean appendDepsList(HashMap<String, FileSpec> map,
List<FileSpec> list, String fileId) {
FileSpec f = map.get(fileId);
if (f == null)
return false;
if (list.contains(f))
return true;
if (f.deps() != null) {
for (String dep : f.deps()) {
if (!appendDepsList(map, list, dep))
return false;
}
}
if (list.contains(f))
return false;
list.add(f);
return true;
}
public synchronized void notifyConnectivityChanged() {
if (running != null)
return;
if (pickFromQueue() == null)
return;
start();
}
synchronized void start() {
if (running == null) {
if (status.size() == map.size()) {
int idleCount = 0;
for (String str : status.values()) {
if (STATUS_IDLE == str)
idleCount++;
}
if (idleCount == 0)
return;
}
running = new Worker();
running.start();
}
}
public synchronized void require(FileSpec... files) {
order.removeAll(Arrays.asList(files));
for (int i = files.length - 1; i >= 0; i--) {
FileSpec file = files[i];
order.addFirst(file);
Integer rc = require.get(file.id());
if (rc == null) {
require.put(file.id(), 1);
} else {
require.put(file.id(), rc + 1);
}
}
running = new Worker();
running.start();
}
public synchronized void dismiss(FileSpec... files) {
for (FileSpec file : files) {
Integer rc = require.get(file.id());
if (rc != null && rc > 0) {
require.put(file.id(), rc - 1);
}
}
}
//
// worker
//
private synchronized FileSpec pickFromQueue() {
int networkType = -1;
for (FileSpec file : order) {
if (getStatus(file.id()) == STATUS_IDLE) {
Integer rc = require.get(file.id());
if (rc != null && rc > 0) {
return file;
}
if (file.down() >= FileSpec.DOWN_ALWAYS)
return file;
if (file.down() <= FileSpec.DOWN_NONE)
continue;
if (networkType < 0) {
networkType = getNetworkType();
}
switch (file.down()) {
case FileSpec.DOWN_WIFI:
if (networkType > 3)
return file;
break;
case FileSpec.DOWN_3G:
if (networkType > 2)
return file;
}
}
}
return null;
}
private class Worker extends Thread {
private int failCounter = 0;
@Override
public void run() {
FileSpec current;
while (running == this && (current = pickFromQueue()) != null) {
final FileSpec fCurrent = current;
Log.i("loader", "start download " + current.id() + " from "
+ current.url());
long startMs = SystemClock.elapsedRealtime();
status.put(current.id(), STATUS_RUNNING);
for (StatusChangeListener l : listeners) {
l.onStatusChanged(fCurrent, STATUS_RUNNING);
}
String rnd = Integer.toHexString(new Random(System
.currentTimeMillis()).nextInt(0xf000) + 0x1000);
File f = new File(tmpDir, current.id() + "." + rnd);
boolean succeed = false;
try {
URL url = new URL(current.url());
HttpURLConnection conn = (HttpURLConnection) url
.openConnection();
conn.setConnectTimeout(15000);
// conn.setRequestProperty("User-Agent",
// Environment.mapiUserAgent());
InputStream ins = conn.getInputStream();
FileOutputStream fos = new FileOutputStream(f);
byte[] buf = new byte[1024 * 4]; // 4k buffer
int l;
while ((l = ins.read(buf, 0, buf.length)) != -1) {
fos.write(buf, 0, l);
}
fos.close();
ins.close();
conn.disconnect();
succeed = true;
} catch (Exception e) {
Log.w("loader", "fail to download " + current.id()
+ " from " + current.url(), e);
}
if (f.length() > 0 && !TextUtils.isEmpty(current.md5())) {
succeed = false;
try {
MessageDigest m = MessageDigest.getInstance("MD5");
m.reset();
FileInputStream fis = new FileInputStream(f);
byte[] buf = new byte[1024 * 4]; // 4k buffer
int l;
while ((l = fis.read(buf, 0, buf.length)) != -1) {
m.update(buf, 0, l);
}
fis.close();
String md5 = byteArrayToHexString(m.digest());
succeed = current.md5().equals(md5);
if (!succeed) {
Log.e("loader", "fail to match " + current.id()
+ " md5, " + md5 + " / " + current.md5());
}
} catch (Exception e) {
Log.e("loader",
"fail to verify file " + f.getAbsolutePath(), e);
}
}
File path = getPath(current);
if (succeed) {
path.getParentFile().mkdir();
succeed = f.renameTo(path);
if (!succeed) {
Log.e("loader", "fail to move " + current.id()
+ " from " + f.getAbsolutePath() + " to "
+ path.getAbsolutePath());
}
}
if (!succeed) {
// delete tmp file if not succeed
f.delete();
}
succeed = path.isFile();
final String newStatus = succeed ? STATUS_DONE : STATUS_IDLE;
status.put(current.id(), newStatus);
if (succeed) {
long elapse = SystemClock.elapsedRealtime() - startMs;
Log.i("loader", current.id() + " (" + path.length()
+ " bytes) finished in " + elapse + "ms");
if (failCounter > 0)
failCounter--;
} else {
order.remove(current);
order.addLast(current);
failCounter++;
}
for (StatusChangeListener l : listeners) {
l.onStatusChanged(fCurrent, newStatus);
}
if (failCounter >= 3) {
Log.w("loader", "download fail 3 times, abort");
break;
}
}
synchronized (RepositoryManager.this) {
if (running == this) {
running = null;
}
}
}
}
//
// utils
//
/**
* 0: Unknown<br>
* 1: 2G<br>
* 2: 3G or faster<br>
* 3: Wifi<br>
*/
int getNetworkType() {
try {
NetworkInfo info = connManager.getActiveNetworkInfo();
if (info.getType() == ConnectivityManager.TYPE_MOBILE) {
switch (info.getSubtype()) {
case TelephonyManager.NETWORK_TYPE_1xRTT:// ~ 50-100 kbps
case TelephonyManager.NETWORK_TYPE_CDMA:// ~ 14-64 kbps
case TelephonyManager.NETWORK_TYPE_EDGE:// ~ 50-100 kbps
case TelephonyManager.NETWORK_TYPE_GPRS:// ~ 100 kbps
case TelephonyManager.NETWORK_TYPE_IDEN:// ~25 kbps
return 1;
default:
return 2;
}
} else if (info.getType() == ConnectivityManager.TYPE_WIFI) {
return 3;
}
} catch (Exception e) {
}
return 0;
}
private void disableConnectionReuseIfNecessary() {
// HTTP connection reuse which was buggy pre-froyo
if (Integer.parseInt(Build.VERSION.SDK) < 8) {
System.setProperty("http.keepAlive", "false");
}
}
private final static String[] hexDigits = { "0", "1", "2", "3", "4", "5",
"6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };
public static String byteArrayToHexString(byte[] b) {
StringBuilder resultSb = new StringBuilder();
for (int i = 0; i < b.length; i++) {
resultSb.append(byteToHexString(b[i]));
}
return resultSb.toString();
}
private static String byteToHexString(byte b) {
int n = b;
if (n < 0)
n = 0x100 + n;
int d1 = n >> 4;
int d2 = n & 0xF;
return hexDigits[d1] + hexDigits[d2];
}
}