Commit b8489de56c0af252e6f76c547171b146e37712f4

Authored by wugian
1 parent ca06f447

error-prone,code style

@@ -3,6 +3,9 @@ buildscript { @@ -3,6 +3,9 @@ buildscript {
3 repositories { 3 repositories {
4 mavenCentral() 4 mavenCentral()
5 jcenter() 5 jcenter()
  6 + maven {
  7 + url "https://plugins.gradle.org/m2/"
  8 + }
6 } 9 }
7 10
8 dependencies { 11 dependencies {
@@ -11,6 +14,7 @@ buildscript { @@ -11,6 +14,7 @@ buildscript {
11 // classpath 'me.tatarka.retrolambda.projectlombok:lombok.ast:0.2.3.a2' 14 // classpath 'me.tatarka.retrolambda.projectlombok:lombok.ast:0.2.3.a2'
12 classpath 'com.antfortune.freeline:gradle:0.8.3' 15 classpath 'com.antfortune.freeline:gradle:0.8.3'
13 classpath "io.realm:realm-gradle-plugin:2.0.2" 16 classpath "io.realm:realm-gradle-plugin:2.0.2"
  17 + classpath "net.ltgt.gradle:gradle-errorprone-plugin:0.0.9"
14 } 18 }
15 // configurations.classpath.exclude group: 'com.android.tools.external.lombok' 19 // configurations.classpath.exclude group: 'com.android.tools.external.lombok'
16 } 20 }
@@ -35,6 +39,9 @@ allprojects { @@ -35,6 +39,9 @@ allprojects {
35 jcenter() 39 jcenter()
36 mavenCentral() 40 mavenCentral()
37 } 41 }
  42 + if (project.hasProperty("enableErrorProne")) {
  43 + apply plugin: "net.ltgt.errorprone"
  44 + }
38 } 45 }
39 46
40 // Define versions in a single place 47 // Define versions in a single place
@@ -382,6 +382,9 @@ public class NewDBManager { @@ -382,6 +382,9 @@ public class NewDBManager {
382 case QUERY_TYPE_DEMAND: 382 case QUERY_TYPE_DEMAND:
383 order = " order by cast(movie_count as int) desc"; 383 order = " order by cast(movie_count as int) desc";
384 break; 384 break;
  385 + default:
  386 + order = " order by screen_time desc";
  387 + break;
385 } 388 }
386 // String order = " order by " + orderKey + (isUp ? " desc" : " asc"); 389 // String order = " order by " + orderKey + (isUp ? " desc" : " asc");
387 String sqlSub = " and path not like '%/LXJ/%'"; 390 String sqlSub = " and path not like '%/LXJ/%'";
@@ -510,6 +513,9 @@ public class NewDBManager { @@ -510,6 +513,9 @@ public class NewDBManager {
510 case QUERY_TYPE_SCREEN_TIME: 513 case QUERY_TYPE_SCREEN_TIME:
511 orderBy = "screen_time desc"; 514 orderBy = "screen_time desc";
512 break; 515 break;
  516 + default:
  517 + orderBy = NewDBHelper.DOUBAN_RATING + " desc";
  518 + break;
513 } 519 }
514 Cursor c = db.query(NewDBHelper.TABLE_NAME, null, null, null, null, null, orderBy, String.valueOf(top)); 520 Cursor c = db.query(NewDBHelper.TABLE_NAME, null, null, null, null, null, orderBy, String.valueOf(top));
515 readCursor2List(persons, c); 521 readCursor2List(persons, c);
@@ -36,7 +36,7 @@ public class GimiSysData { @@ -36,7 +36,7 @@ public class GimiSysData {
36 '}'; 36 '}';
37 } 37 }
38 38
39 - public class Message { 39 + public static class Message {
40 @Expose 40 @Expose
41 private String msg_content; 41 private String msg_content;
42 @Expose 42 @Expose
@@ -54,7 +54,7 @@ public class GimiSysMsg { @@ -54,7 +54,7 @@ public class GimiSysMsg {
54 return "GimiSysMsg{" + 54 return "GimiSysMsg{" +
55 "message='" + message + '\'' + 55 "message='" + message + '\'' +
56 ", code=" + code + 56 ", code=" + code +
57 - ", data=" + (data != null ? data.toString() : "data is null") + 57 + ", data=" + (data != null ? data : "data is null") +
58 '}'; 58 '}';
59 } 59 }
60 } 60 }
@@ -52,7 +52,7 @@ public class MessageData { @@ -52,7 +52,7 @@ public class MessageData {
52 '}'; 52 '}';
53 } 53 }
54 54
55 - public class Message { 55 + public static class Message {
56 @Expose 56 @Expose
57 private String msg_content; 57 private String msg_content;
58 @Expose 58 @Expose
@@ -44,7 +44,7 @@ public class PosterMessage { @@ -44,7 +44,7 @@ public class PosterMessage {
44 this.data = data; 44 this.data = data;
45 } 45 }
46 46
47 - public class DataEntity { 47 + public static class DataEntity {
48 private String versionId; 48 private String versionId;
49 private String posterUrl; 49 private String posterUrl;
50 50
@@ -50,821 +50,793 @@ import java.util.concurrent.atomic.AtomicLong; @@ -50,821 +50,793 @@ import java.util.concurrent.atomic.AtomicLong;
50 * @author Michael Yang(www.yangfuhai.com) update at 2013.08.07 50 * @author Michael Yang(www.yangfuhai.com) update at 2013.08.07
51 */ 51 */
52 public class ACache { 52 public class ACache {
53 - public static final int TIME_HOUR = 60 * 60;  
54 - public static final int TIME_DAY = TIME_HOUR * 24;  
55 - private static final int MAX_SIZE = 1000 * 1000 * 50; // 50 mb  
56 - private static final int MAX_COUNT = Integer.MAX_VALUE; // 不限制存放数据的数量  
57 - private static Map<String, ACache> mInstanceMap = new HashMap<String, ACache>();  
58 - private ACacheManager mCache;  
59 -  
60 - public static ACache get(Context ctx) {  
61 - return get(ctx, "ACache");  
62 - }  
63 -  
64 - public static ACache get(Context ctx, String cacheName) {  
65 - File f = new File(ctx.getCacheDir(), cacheName);  
66 - return get(f, MAX_SIZE, MAX_COUNT);  
67 - }  
68 -  
69 - public static ACache get(File cacheDir) {  
70 - return get(cacheDir, MAX_SIZE, MAX_COUNT);  
71 - }  
72 -  
73 - public static ACache get(Context ctx, long max_zise, int max_count) {  
74 - File f = new File(ctx.getCacheDir(), "ACache");  
75 - return get(f, max_zise, max_count);  
76 - }  
77 -  
78 - public static ACache get(File cacheDir, long max_zise, int max_count) {  
79 - ACache manager = mInstanceMap.get(cacheDir.getAbsoluteFile() + myPid());  
80 - if (manager == null) {  
81 - manager = new ACache(cacheDir, max_zise, max_count);  
82 - mInstanceMap.put(cacheDir.getAbsolutePath() + myPid(), manager);  
83 - }  
84 - return manager;  
85 - }  
86 -  
87 - private static String myPid() {  
88 - return "_" + android.os.Process.myPid();  
89 - }  
90 -  
91 - private ACache(File cacheDir, long max_size, int max_count) {  
92 - if (!cacheDir.exists() && !cacheDir.mkdirs()) {  
93 - throw new RuntimeException("can't make dirs in "  
94 - + cacheDir.getAbsolutePath());  
95 - }  
96 - mCache = new ACacheManager(cacheDir, max_size, max_count);  
97 - }  
98 -  
99 - // =======================================  
100 - // ============ String数据 读写 ==============  
101 - // =======================================  
102 - /**  
103 - * 保存 String数据 到 缓存中  
104 - *  
105 - * @param key  
106 - * 保存的key  
107 - * @param value  
108 - * 保存的String数据  
109 - */  
110 - public void put(String key, String value) {  
111 - File file = mCache.newFile(key);  
112 - BufferedWriter out = null;  
113 - try {  
114 - out = new BufferedWriter(new FileWriter(file), 1024);  
115 - out.write(value);  
116 - } catch (IOException e) {  
117 - e.printStackTrace();  
118 - } finally {  
119 - if (out != null) {  
120 - try {  
121 - out.flush();  
122 - out.close();  
123 - } catch (IOException e) {  
124 - e.printStackTrace();  
125 - }  
126 - }  
127 - mCache.put(file);  
128 - }  
129 - }  
130 -  
131 - /**  
132 - * 保存 String数据 到 缓存中  
133 - *  
134 - * @param key  
135 - * 保存的key  
136 - * @param value  
137 - * 保存的String数据  
138 - * @param saveTime  
139 - * 保存的时间,单位:秒  
140 - */  
141 - public void put(String key, String value, int saveTime) {  
142 - put(key, Utils.newStringWithDateInfo(saveTime, value));  
143 - }  
144 -  
145 - /**  
146 - * 读取 String数据  
147 - *  
148 - * @param key  
149 - * @return String 数据  
150 - */  
151 - public String getAsString(String key) {  
152 - File file = mCache.get(key);  
153 - if (!file.exists())  
154 - return null;  
155 - boolean removeFile = false;  
156 - BufferedReader in = null;  
157 - try {  
158 - in = new BufferedReader(new FileReader(file));  
159 - String readString = "";  
160 - String currentLine;  
161 - while ((currentLine = in.readLine()) != null) {  
162 - readString += currentLine;  
163 - }  
164 - if (!Utils.isDue(readString)) {  
165 - return Utils.clearDateInfo(readString);  
166 - } else {  
167 - removeFile = true;  
168 - return null;  
169 - }  
170 - } catch (IOException e) {  
171 - e.printStackTrace();  
172 - return null;  
173 - } finally {  
174 - if (in != null) {  
175 - try {  
176 - in.close();  
177 - } catch (IOException e) {  
178 - e.printStackTrace();  
179 - }  
180 - }  
181 - if (removeFile)  
182 - remove(key);  
183 - }  
184 - }  
185 -  
186 - // =======================================  
187 - // ============= JSONObject 数据 读写 ==============  
188 - // =======================================  
189 - /**  
190 - * 保存 JSONObject数据 到 缓存中  
191 - *  
192 - * @param key  
193 - * 保存的key  
194 - * @param value  
195 - * 保存的JSON数据  
196 - */  
197 - public void put(String key, JSONObject value) {  
198 - put(key, value.toString());  
199 - }  
200 -  
201 - /**  
202 - * 保存 JSONObject数据 到 缓存中  
203 - *  
204 - * @param key  
205 - * 保存的key  
206 - * @param value  
207 - * 保存的JSONObject数据  
208 - * @param saveTime  
209 - * 保存的时间,单位:秒  
210 - */  
211 - public void put(String key, JSONObject value, int saveTime) {  
212 - put(key, value.toString(), saveTime);  
213 - }  
214 -  
215 - /**  
216 - * 读取JSONObject数据  
217 - *  
218 - * @param key  
219 - * @return JSONObject数据  
220 - */  
221 - public JSONObject getAsJSONObject(String key) {  
222 - String JSONString = getAsString(key);  
223 - try {  
224 - JSONObject obj = new JSONObject(JSONString);  
225 - return obj;  
226 - } catch (Exception e) {  
227 - e.printStackTrace();  
228 - return null;  
229 - }  
230 - }  
231 -  
232 - // =======================================  
233 - // ============ JSONArray 数据 读写 =============  
234 - // =======================================  
235 - /**  
236 - * 保存 JSONArray数据 到 缓存中  
237 - *  
238 - * @param key  
239 - * 保存的key  
240 - * @param value  
241 - * 保存的JSONArray数据  
242 - */  
243 - public void put(String key, JSONArray value) {  
244 - put(key, value.toString());  
245 - }  
246 -  
247 - /**  
248 - * 保存 JSONArray数据 到 缓存中  
249 - *  
250 - * @param key  
251 - * 保存的key  
252 - * @param value  
253 - * 保存的JSONArray数据  
254 - * @param saveTime  
255 - * 保存的时间,单位:秒  
256 - */  
257 - public void put(String key, JSONArray value, int saveTime) {  
258 - put(key, value.toString(), saveTime);  
259 - }  
260 -  
261 - /**  
262 - * 读取JSONArray数据  
263 - *  
264 - * @param key  
265 - * @return JSONArray数据  
266 - */  
267 - public JSONArray getAsJSONArray(String key) {  
268 - String JSONString = getAsString(key);  
269 - try {  
270 - JSONArray obj = new JSONArray(JSONString);  
271 - return obj;  
272 - } catch (Exception e) {  
273 - e.printStackTrace();  
274 - return null;  
275 - }  
276 - }  
277 -  
278 - // =======================================  
279 - // ============== byte 数据 读写 =============  
280 - // =======================================  
281 - /**  
282 - * 保存 byte数据 到 缓存中  
283 - *  
284 - * @param key  
285 - * 保存的key  
286 - * @param value  
287 - * 保存的数据  
288 - */  
289 - public void put(String key, byte[] value) {  
290 - File file = mCache.newFile(key);  
291 - FileOutputStream out = null;  
292 - try {  
293 - out = new FileOutputStream(file);  
294 - out.write(value);  
295 - } catch (Exception e) {  
296 - e.printStackTrace();  
297 - } finally {  
298 - if (out != null) {  
299 - try {  
300 - out.flush();  
301 - out.close();  
302 - } catch (IOException e) {  
303 - e.printStackTrace();  
304 - }  
305 - }  
306 - mCache.put(file);  
307 - }  
308 - }  
309 -  
310 - /**  
311 - * 保存 byte数据 到 缓存中  
312 - *  
313 - * @param key  
314 - * 保存的key  
315 - * @param value  
316 - * 保存的数据  
317 - * @param saveTime  
318 - * 保存的时间,单位:秒  
319 - */  
320 - public void put(String key, byte[] value, int saveTime) {  
321 - put(key, Utils.newByteArrayWithDateInfo(saveTime, value));  
322 - }  
323 -  
324 - /**  
325 - * 获取 byte 数据  
326 - *  
327 - * @param key  
328 - * @return byte 数据  
329 - */  
330 - public byte[] getAsBinary(String key) {  
331 - RandomAccessFile RAFile = null;  
332 - boolean removeFile = false;  
333 - try {  
334 - File file = mCache.get(key);  
335 - if (!file.exists())  
336 - return null;  
337 - RAFile = new RandomAccessFile(file, "r");  
338 - byte[] byteArray = new byte[(int) RAFile.length()];  
339 - RAFile.read(byteArray);  
340 - if (!Utils.isDue(byteArray)) {  
341 - return Utils.clearDateInfo(byteArray);  
342 - } else {  
343 - removeFile = true;  
344 - return null;  
345 - }  
346 - } catch (Exception e) {  
347 - e.printStackTrace();  
348 - return null;  
349 - } finally {  
350 - if (RAFile != null) {  
351 - try {  
352 - RAFile.close();  
353 - } catch (IOException e) {  
354 - e.printStackTrace();  
355 - }  
356 - }  
357 - if (removeFile)  
358 - remove(key);  
359 - }  
360 - }  
361 -  
362 - // =======================================  
363 - // ============= 序列化 数据 读写 ===============  
364 - // =======================================  
365 - /**  
366 - * 保存 Serializable数据 到 缓存中  
367 - *  
368 - * @param key  
369 - * 保存的key  
370 - * @param value  
371 - * 保存的value  
372 - */  
373 - public void put(String key, Serializable value) {  
374 - put(key, value, -1);  
375 - }  
376 -  
377 - /**  
378 - * 保存 Serializable数据到 缓存中  
379 - *  
380 - * @param key  
381 - * 保存的key  
382 - * @param value  
383 - * 保存的value  
384 - * @param saveTime  
385 - * 保存的时间,单位:秒  
386 - */  
387 - public void put(String key, Serializable value, int saveTime) {  
388 - ByteArrayOutputStream baos = null;  
389 - ObjectOutputStream oos = null;  
390 - try {  
391 - baos = new ByteArrayOutputStream();  
392 - oos = new ObjectOutputStream(baos);  
393 - oos.writeObject(value);  
394 - byte[] data = baos.toByteArray();  
395 - if (saveTime != -1) {  
396 - put(key, data, saveTime);  
397 - } else {  
398 - put(key, data);  
399 - }  
400 - } catch (Exception e) {  
401 - e.printStackTrace();  
402 - } finally {  
403 - try {  
404 - oos.close();  
405 - } catch (IOException e) {  
406 - }  
407 - }  
408 - }  
409 -  
410 - /**  
411 - * 读取 Serializable数据  
412 - *  
413 - * @param key  
414 - * @return Serializable 数据  
415 - */  
416 - public Object getAsObject(String key) {  
417 - byte[] data = getAsBinary(key);  
418 - if (data != null) {  
419 - ByteArrayInputStream bais = null;  
420 - ObjectInputStream ois = null;  
421 - try {  
422 - bais = new ByteArrayInputStream(data);  
423 - ois = new ObjectInputStream(bais);  
424 - Object reObject = ois.readObject();  
425 - return reObject;  
426 - } catch (Exception e) {  
427 - e.printStackTrace();  
428 - return null;  
429 - } finally {  
430 - try {  
431 - if (bais != null)  
432 - bais.close();  
433 - } catch (IOException e) {  
434 - e.printStackTrace();  
435 - }  
436 - try {  
437 - if (ois != null)  
438 - ois.close();  
439 - } catch (IOException e) {  
440 - e.printStackTrace();  
441 - }  
442 - }  
443 - }  
444 - return null;  
445 -  
446 - }  
447 -  
448 - // =======================================  
449 - // ============== bitmap 数据 读写 =============  
450 - // =======================================  
451 - /**  
452 - * 保存 bitmap 到 缓存中  
453 - *  
454 - * @param key  
455 - * 保存的key  
456 - * @param value  
457 - * 保存的bitmap数据  
458 - */  
459 - public void put(String key, Bitmap value) {  
460 - put(key, Utils.Bitmap2Bytes(value));  
461 - }  
462 -  
463 - /**  
464 - * 保存 bitmap 到 缓存中  
465 - *  
466 - * @param key  
467 - * 保存的key  
468 - * @param value  
469 - * 保存的 bitmap 数据  
470 - * @param saveTime  
471 - * 保存的时间,单位:秒  
472 - */  
473 - public void put(String key, Bitmap value, int saveTime) {  
474 - put(key, Utils.Bitmap2Bytes(value), saveTime);  
475 - }  
476 -  
477 - /**  
478 - * 读取 bitmap 数据  
479 - *  
480 - * @param key  
481 - * @return bitmap 数据  
482 - */  
483 - public Bitmap getAsBitmap(String key) {  
484 - if (getAsBinary(key) == null) {  
485 - return null;  
486 - }  
487 - return Utils.Bytes2Bimap(getAsBinary(key));  
488 - }  
489 -  
490 - // =======================================  
491 - // ============= drawable 数据 读写 =============  
492 - // =======================================  
493 - /**  
494 - * 保存 drawable 到 缓存中  
495 - *  
496 - * @param key  
497 - * 保存的key  
498 - * @param value  
499 - * 保存的drawable数据  
500 - */  
501 - public void put(String key, Drawable value) {  
502 - put(key, Utils.drawable2Bitmap(value));  
503 - }  
504 -  
505 - /**  
506 - * 保存 drawable 到 缓存中  
507 - *  
508 - * @param key  
509 - * 保存的key  
510 - * @param value  
511 - * 保存的 drawable 数据  
512 - * @param saveTime  
513 - * 保存的时间,单位:秒  
514 - */  
515 - public void put(String key, Drawable value, int saveTime) {  
516 - put(key, Utils.drawable2Bitmap(value), saveTime);  
517 - }  
518 -  
519 - /**  
520 - * 读取 Drawable 数据  
521 - *  
522 - * @param key  
523 - * @return Drawable 数据  
524 - */  
525 - public Drawable getAsDrawable(String key) {  
526 - if (getAsBinary(key) == null) {  
527 - return null;  
528 - }  
529 - return Utils.bitmap2Drawable(Utils.Bytes2Bimap(getAsBinary(key)));  
530 - }  
531 -  
532 - /**  
533 - * 获取缓存文件  
534 - *  
535 - * @param key  
536 - * @return value 缓存的文件  
537 - */  
538 - public File file(String key) {  
539 - File f = mCache.newFile(key);  
540 - if (f.exists())  
541 - return f;  
542 - return null;  
543 - }  
544 -  
545 - /**  
546 - * 移除某个key  
547 - *  
548 - * @param key  
549 - * @return 是否移除成功  
550 - */  
551 - public boolean remove(String key) {  
552 - return mCache.remove(key);  
553 - }  
554 -  
555 - /**  
556 - * 清除所有数据  
557 - */  
558 - public void clear() {  
559 - mCache.clear();  
560 - }  
561 -  
562 - /**  
563 - * @title 缓存管理器  
564 - * @author 杨福海(michael) www.yangfuhai.com  
565 - * @version 1.0  
566 - */  
567 - public class ACacheManager {  
568 - private final AtomicLong cacheSize;  
569 - private final AtomicInteger cacheCount;  
570 - private final long sizeLimit;  
571 - private final int countLimit;  
572 - private final Map<File, Long> lastUsageDates = Collections  
573 - .synchronizedMap(new HashMap<File, Long>());  
574 - protected File cacheDir;  
575 -  
576 - private ACacheManager(File cacheDir, long sizeLimit, int countLimit) {  
577 - this.cacheDir = cacheDir;  
578 - this.sizeLimit = sizeLimit;  
579 - this.countLimit = countLimit;  
580 - cacheSize = new AtomicLong();  
581 - cacheCount = new AtomicInteger();  
582 - calculateCacheSizeAndCacheCount();  
583 - }  
584 -  
585 - /**  
586 - * 计算 cacheSize和cacheCount  
587 - */  
588 - private void calculateCacheSizeAndCacheCount() {  
589 - new Thread(new Runnable() {  
590 - @Override  
591 - public void run() {  
592 - int size = 0;  
593 - int count = 0;  
594 - File[] cachedFiles = cacheDir.listFiles();  
595 - if (cachedFiles != null) {  
596 - for (File cachedFile : cachedFiles) {  
597 - size += calculateSize(cachedFile);  
598 - count += 1;  
599 - lastUsageDates.put(cachedFile,  
600 - cachedFile.lastModified());  
601 - }  
602 - cacheSize.set(size);  
603 - cacheCount.set(count);  
604 - }  
605 - }  
606 - }).start();  
607 - }  
608 -  
609 - private void put(File file) {  
610 - int curCacheCount = cacheCount.get();  
611 - while (curCacheCount + 1 > countLimit) {  
612 - long freedSize = removeNext();  
613 - cacheSize.addAndGet(-freedSize);  
614 -  
615 - curCacheCount = cacheCount.addAndGet(-1);  
616 - }  
617 - cacheCount.addAndGet(1);  
618 -  
619 - long valueSize = calculateSize(file);  
620 - long curCacheSize = cacheSize.get();  
621 - while (curCacheSize + valueSize > sizeLimit) {  
622 - long freedSize = removeNext();  
623 - curCacheSize = cacheSize.addAndGet(-freedSize);  
624 - }  
625 - cacheSize.addAndGet(valueSize);  
626 -  
627 - Long currentTime = System.currentTimeMillis();  
628 - file.setLastModified(currentTime);  
629 - lastUsageDates.put(file, currentTime);  
630 - }  
631 -  
632 - private File get(String key) {  
633 - File file = newFile(key);  
634 - Long currentTime = System.currentTimeMillis();  
635 - file.setLastModified(currentTime);  
636 - lastUsageDates.put(file, currentTime);  
637 -  
638 - return file;  
639 - }  
640 -  
641 - private File newFile(String key) {  
642 - return new File(cacheDir, key.hashCode() + "");  
643 - }  
644 -  
645 - private boolean remove(String key) {  
646 - File image = get(key);  
647 - return image.delete();  
648 - }  
649 -  
650 - private void clear() {  
651 - lastUsageDates.clear();  
652 - cacheSize.set(0);  
653 - File[] files = cacheDir.listFiles();  
654 - if (files != null) {  
655 - for (File f : files) {  
656 - f.delete();  
657 - }  
658 - }  
659 - }  
660 -  
661 - /**  
662 - * 移除旧的文件  
663 - *  
664 - * @return  
665 - */  
666 - private long removeNext() {  
667 - if (lastUsageDates.isEmpty()) {  
668 - return 0;  
669 - }  
670 -  
671 - Long oldestUsage = null;  
672 - File mostLongUsedFile = null;  
673 - Set<Entry<File, Long>> entries = lastUsageDates.entrySet();  
674 - synchronized (lastUsageDates) {  
675 - for (Entry<File, Long> entry : entries) {  
676 - if (mostLongUsedFile == null) {  
677 - mostLongUsedFile = entry.getKey();  
678 - oldestUsage = entry.getValue();  
679 - } else {  
680 - Long lastValueUsage = entry.getValue();  
681 - if (lastValueUsage < oldestUsage) {  
682 - oldestUsage = lastValueUsage;  
683 - mostLongUsedFile = entry.getKey();  
684 - }  
685 - }  
686 - }  
687 - }  
688 -  
689 - long fileSize = calculateSize(mostLongUsedFile);  
690 - if (mostLongUsedFile.delete()) {  
691 - lastUsageDates.remove(mostLongUsedFile);  
692 - }  
693 - return fileSize;  
694 - }  
695 -  
696 - private long calculateSize(File file) {  
697 - return file.length();  
698 - }  
699 - }  
700 -  
701 - /**  
702 - * @title 时间计算工具类  
703 - * @author 杨福海(michael) www.yangfuhai.com  
704 - * @version 1.0  
705 - */  
706 - private static class Utils {  
707 -  
708 - /**  
709 - * 判断缓存的String数据是否到期  
710 - *  
711 - * @param str  
712 - * @return true:到期了 false:还没有到期  
713 - */  
714 - private static boolean isDue(String str) {  
715 - return isDue(str.getBytes());  
716 - }  
717 -  
718 - /**  
719 - * 判断缓存的byte数据是否到期  
720 - *  
721 - * @param data  
722 - * @return true:到期了 false:还没有到期  
723 - */  
724 - private static boolean isDue(byte[] data) {  
725 - String[] strs = getDateInfoFromDate(data);  
726 - if (strs != null && strs.length == 2) {  
727 - String saveTimeStr = strs[0];  
728 - while (saveTimeStr.startsWith("0")) {  
729 - saveTimeStr = saveTimeStr  
730 - .substring(1, saveTimeStr.length());  
731 - }  
732 - long saveTime = Long.valueOf(saveTimeStr);  
733 - long deleteAfter = Long.valueOf(strs[1]);  
734 - if (System.currentTimeMillis() > saveTime + deleteAfter * 1000) {  
735 - return true;  
736 - }  
737 - }  
738 - return false;  
739 - }  
740 -  
741 - private static String newStringWithDateInfo(int second, String strInfo) {  
742 - return createDateInfo(second) + strInfo;  
743 - }  
744 -  
745 - private static byte[] newByteArrayWithDateInfo(int second, byte[] data2) {  
746 - byte[] data1 = createDateInfo(second).getBytes();  
747 - byte[] retdata = new byte[data1.length + data2.length];  
748 - System.arraycopy(data1, 0, retdata, 0, data1.length);  
749 - System.arraycopy(data2, 0, retdata, data1.length, data2.length);  
750 - return retdata;  
751 - }  
752 -  
753 - private static String clearDateInfo(String strInfo) {  
754 - if (strInfo != null && hasDateInfo(strInfo.getBytes())) {  
755 - strInfo = strInfo.substring(strInfo.indexOf(mSeparator) + 1,  
756 - strInfo.length());  
757 - }  
758 - return strInfo;  
759 - }  
760 -  
761 - private static byte[] clearDateInfo(byte[] data) {  
762 - if (hasDateInfo(data)) {  
763 - return copyOfRange(data, indexOf(data, mSeparator) + 1,  
764 - data.length);  
765 - }  
766 - return data;  
767 - }  
768 -  
769 - private static boolean hasDateInfo(byte[] data) {  
770 - return data != null && data.length > 15 && data[13] == '-'  
771 - && indexOf(data, mSeparator) > 14;  
772 - }  
773 -  
774 - private static String[] getDateInfoFromDate(byte[] data) {  
775 - if (hasDateInfo(data)) {  
776 - String saveDate = new String(copyOfRange(data, 0, 13));  
777 - String deleteAfter = new String(copyOfRange(data, 14,  
778 - indexOf(data, mSeparator)));  
779 - return new String[] { saveDate, deleteAfter };  
780 - }  
781 - return null;  
782 - }  
783 -  
784 - private static int indexOf(byte[] data, char c) {  
785 - for (int i = 0; i < data.length; i++) {  
786 - if (data[i] == c) {  
787 - return i;  
788 - }  
789 - }  
790 - return -1;  
791 - }  
792 -  
793 - private static byte[] copyOfRange(byte[] original, int from, int to) {  
794 - int newLength = to - from;  
795 - if (newLength < 0)  
796 - throw new IllegalArgumentException(from + " > " + to);  
797 - byte[] copy = new byte[newLength];  
798 - System.arraycopy(original, from, copy, 0,  
799 - Math.min(original.length - from, newLength));  
800 - return copy;  
801 - }  
802 -  
803 - private static final char mSeparator = ' ';  
804 -  
805 - private static String createDateInfo(int second) {  
806 - String currentTime = System.currentTimeMillis() + "";  
807 - while (currentTime.length() < 13) {  
808 - currentTime = "0" + currentTime;  
809 - }  
810 - return currentTime + "-" + second + mSeparator;  
811 - }  
812 -  
813 - /*  
814 - * Bitmap → byte[]  
815 - */  
816 - private static byte[] Bitmap2Bytes(Bitmap bm) {  
817 - if (bm == null) {  
818 - return null;  
819 - }  
820 - ByteArrayOutputStream baos = new ByteArrayOutputStream();  
821 - bm.compress(Bitmap.CompressFormat.PNG, 100, baos);  
822 - return baos.toByteArray();  
823 - }  
824 -  
825 - /*  
826 - * byte[] → Bitmap  
827 - */  
828 - private static Bitmap Bytes2Bimap(byte[] b) {  
829 - if (b.length == 0) {  
830 - return null;  
831 - }  
832 - return BitmapFactory.decodeByteArray(b, 0, b.length);  
833 - }  
834 -  
835 - /*  
836 - * Drawable → Bitmap  
837 - */  
838 - private static Bitmap drawable2Bitmap(Drawable drawable) {  
839 - if (drawable == null) {  
840 - return null;  
841 - }  
842 - // 取 drawable 的长宽  
843 - int w = drawable.getIntrinsicWidth();  
844 - int h = drawable.getIntrinsicHeight();  
845 - // 取 drawable 的颜色格式  
846 - Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888  
847 - : Bitmap.Config.RGB_565;  
848 - // 建立对应 bitmap  
849 - Bitmap bitmap = Bitmap.createBitmap(w, h, config);  
850 - // 建立对应 bitmap 的画布  
851 - Canvas canvas = new Canvas(bitmap);  
852 - drawable.setBounds(0, 0, w, h);  
853 - // 把 drawable 内容画到画布中  
854 - drawable.draw(canvas);  
855 - return bitmap;  
856 - }  
857 -  
858 - /*  
859 - * Bitmap → Drawable  
860 - */  
861 - @SuppressWarnings("deprecation")  
862 - private static Drawable bitmap2Drawable(Bitmap bm) {  
863 - if (bm == null) {  
864 - return null;  
865 - }  
866 - return new BitmapDrawable(bm);  
867 - }  
868 - } 53 + public static final int TIME_HOUR = 60 * 60;
  54 + public static final int TIME_DAY = TIME_HOUR * 24;
  55 + private static final int MAX_SIZE = 1000 * 1000 * 50; // 50 mb
  56 + private static final int MAX_COUNT = Integer.MAX_VALUE; // 不限制存放数据的数量
  57 + private static Map<String, ACache> mInstanceMap = new HashMap<String, ACache>();
  58 + private ACacheManager mCache;
  59 +
  60 + public static ACache get(Context ctx) {
  61 + return get(ctx, "ACache");
  62 + }
  63 +
  64 + public static ACache get(Context ctx, String cacheName) {
  65 + File f = new File(ctx.getCacheDir(), cacheName);
  66 + return get(f, MAX_SIZE, MAX_COUNT);
  67 + }
  68 +
  69 + public static ACache get(File cacheDir) {
  70 + return get(cacheDir, MAX_SIZE, MAX_COUNT);
  71 + }
  72 +
  73 + public static ACache get(Context ctx, long max_zise, int max_count) {
  74 + File f = new File(ctx.getCacheDir(), "ACache");
  75 + return get(f, max_zise, max_count);
  76 + }
  77 +
  78 + public static ACache get(File cacheDir, long max_zise, int max_count) {
  79 + ACache manager = mInstanceMap.get(cacheDir.getAbsoluteFile() + myPid());
  80 + if (manager == null) {
  81 + manager = new ACache(cacheDir, max_zise, max_count);
  82 + mInstanceMap.put(cacheDir.getAbsolutePath() + myPid(), manager);
  83 + }
  84 + return manager;
  85 + }
  86 +
  87 + private static String myPid() {
  88 + return "_" + android.os.Process.myPid();
  89 + }
  90 +
  91 + private ACache(File cacheDir, long max_size, int max_count) {
  92 + if (!cacheDir.exists() && !cacheDir.mkdirs()) {
  93 + throw new RuntimeException("can't make dirs in "
  94 + + cacheDir.getAbsolutePath());
  95 + }
  96 + mCache = new ACacheManager(cacheDir, max_size, max_count);
  97 + }
  98 +
  99 + // =======================================
  100 + // ============ String数据 读写 ==============
  101 + // =======================================
  102 +
  103 + /**
  104 + * 保存 String数据 到 缓存中
  105 + *
  106 + * @param key 保存的key
  107 + * @param value 保存的String数据
  108 + */
  109 + public void put(String key, String value) {
  110 + File file = mCache.newFile(key);
  111 + BufferedWriter out = null;
  112 + try {
  113 + out = new BufferedWriter(new FileWriter(file), 1024);
  114 + out.write(value);
  115 + } catch (IOException e) {
  116 + e.printStackTrace();
  117 + } finally {
  118 + if (out != null) {
  119 + try {
  120 + out.flush();
  121 + out.close();
  122 + } catch (IOException e) {
  123 + e.printStackTrace();
  124 + }
  125 + }
  126 + mCache.put(file);
  127 + }
  128 + }
  129 +
  130 + /**
  131 + * 保存 String数据 到 缓存中
  132 + *
  133 + * @param key 保存的key
  134 + * @param value 保存的String数据
  135 + * @param saveTime 保存的时间,单位:秒
  136 + */
  137 + public void put(String key, String value, int saveTime) {
  138 + put(key, Utils.newStringWithDateInfo(saveTime, value));
  139 + }
  140 +
  141 + /**
  142 + * 读取 String数据
  143 + *
  144 + * @param key
  145 + * @return String 数据
  146 + */
  147 + public String getAsString(String key) {
  148 + File file = mCache.get(key);
  149 + if (!file.exists())
  150 + return null;
  151 + boolean removeFile = false;
  152 + BufferedReader in = null;
  153 + try {
  154 + in = new BufferedReader(new FileReader(file));
  155 + String readString = "";
  156 + String currentLine;
  157 + while ((currentLine = in.readLine()) != null) {
  158 + readString += currentLine;
  159 + }
  160 + if (!Utils.isDue(readString)) {
  161 + return Utils.clearDateInfo(readString);
  162 + } else {
  163 + removeFile = true;
  164 + return null;
  165 + }
  166 + } catch (IOException e) {
  167 + e.printStackTrace();
  168 + return null;
  169 + } finally {
  170 + if (in != null) {
  171 + try {
  172 + in.close();
  173 + } catch (IOException e) {
  174 + e.printStackTrace();
  175 + }
  176 + }
  177 + if (removeFile)
  178 + remove(key);
  179 + }
  180 + }
  181 +
  182 + // =======================================
  183 + // ============= JSONObject 数据 读写 ==============
  184 + // =======================================
  185 +
  186 + /**
  187 + * 保存 JSONObject数据 到 缓存中
  188 + *
  189 + * @param key 保存的key
  190 + * @param value 保存的JSON数据
  191 + */
  192 + public void put(String key, JSONObject value) {
  193 + put(key, value.toString());
  194 + }
  195 +
  196 + /**
  197 + * 保存 JSONObject数据 到 缓存中
  198 + *
  199 + * @param key 保存的key
  200 + * @param value 保存的JSONObject数据
  201 + * @param saveTime 保存的时间,单位:秒
  202 + */
  203 + public void put(String key, JSONObject value, int saveTime) {
  204 + put(key, value.toString(), saveTime);
  205 + }
  206 +
  207 + /**
  208 + * 读取JSONObject数据
  209 + *
  210 + * @param key
  211 + * @return JSONObject数据
  212 + */
  213 + public JSONObject getAsJSONObject(String key) {
  214 + String JSONString = getAsString(key);
  215 + try {
  216 + JSONObject obj = new JSONObject(JSONString);
  217 + return obj;
  218 + } catch (Exception e) {
  219 + e.printStackTrace();
  220 + return null;
  221 + }
  222 + }
  223 +
  224 + // =======================================
  225 + // ============ JSONArray 数据 读写 =============
  226 + // =======================================
  227 +
  228 + /**
  229 + * 保存 JSONArray数据 到 缓存中
  230 + *
  231 + * @param key 保存的key
  232 + * @param value 保存的JSONArray数据
  233 + */
  234 + public void put(String key, JSONArray value) {
  235 + put(key, value.toString());
  236 + }
  237 +
  238 + /**
  239 + * 保存 JSONArray数据 到 缓存中
  240 + *
  241 + * @param key 保存的key
  242 + * @param value 保存的JSONArray数据
  243 + * @param saveTime 保存的时间,单位:秒
  244 + */
  245 + public void put(String key, JSONArray value, int saveTime) {
  246 + put(key, value.toString(), saveTime);
  247 + }
  248 +
  249 + /**
  250 + * 读取JSONArray数据
  251 + *
  252 + * @param key
  253 + * @return JSONArray数据
  254 + */
  255 + public JSONArray getAsJSONArray(String key) {
  256 + String JSONString = getAsString(key);
  257 + try {
  258 + JSONArray obj = new JSONArray(JSONString);
  259 + return obj;
  260 + } catch (Exception e) {
  261 + e.printStackTrace();
  262 + return null;
  263 + }
  264 + }
  265 +
  266 + // =======================================
  267 + // ============== byte 数据 读写 =============
  268 + // =======================================
  269 +
  270 + /**
  271 + * 保存 byte数据 到 缓存中
  272 + *
  273 + * @param key 保存的key
  274 + * @param value 保存的数据
  275 + */
  276 + public void put(String key, byte[] value) {
  277 + File file = mCache.newFile(key);
  278 + FileOutputStream out = null;
  279 + try {
  280 + out = new FileOutputStream(file);
  281 + out.write(value);
  282 + } catch (Exception e) {
  283 + e.printStackTrace();
  284 + } finally {
  285 + if (out != null) {
  286 + try {
  287 + out.flush();
  288 + out.close();
  289 + } catch (IOException e) {
  290 + e.printStackTrace();
  291 + }
  292 + }
  293 + mCache.put(file);
  294 + }
  295 + }
  296 +
  297 + /**
  298 + * 保存 byte数据 到 缓存中
  299 + *
  300 + * @param key 保存的key
  301 + * @param value 保存的数据
  302 + * @param saveTime 保存的时间,单位:秒
  303 + */
  304 + public void put(String key, byte[] value, int saveTime) {
  305 + put(key, Utils.newByteArrayWithDateInfo(saveTime, value));
  306 + }
  307 +
  308 + /**
  309 + * 获取 byte 数据
  310 + *
  311 + * @param key
  312 + * @return byte 数据
  313 + */
  314 + public byte[] getAsBinary(String key) {
  315 + RandomAccessFile RAFile = null;
  316 + boolean removeFile = false;
  317 + try {
  318 + File file = mCache.get(key);
  319 + if (!file.exists())
  320 + return null;
  321 + RAFile = new RandomAccessFile(file, "r");
  322 + byte[] byteArray = new byte[(int) RAFile.length()];
  323 + RAFile.read(byteArray);
  324 + if (!Utils.isDue(byteArray)) {
  325 + return Utils.clearDateInfo(byteArray);
  326 + } else {
  327 + removeFile = true;
  328 + return null;
  329 + }
  330 + } catch (Exception e) {
  331 + e.printStackTrace();
  332 + return null;
  333 + } finally {
  334 + if (RAFile != null) {
  335 + try {
  336 + RAFile.close();
  337 + } catch (IOException e) {
  338 + e.printStackTrace();
  339 + }
  340 + }
  341 + if (removeFile)
  342 + remove(key);
  343 + }
  344 + }
  345 +
  346 + // =======================================
  347 + // ============= 序列化 数据 读写 ===============
  348 + // =======================================
  349 +
  350 + /**
  351 + * 保存 Serializable数据 到 缓存中
  352 + *
  353 + * @param key 保存的key
  354 + * @param value 保存的value
  355 + */
  356 + public void put(String key, Serializable value) {
  357 + put(key, value, -1);
  358 + }
  359 +
  360 + /**
  361 + * 保存 Serializable数据到 缓存中
  362 + *
  363 + * @param key 保存的key
  364 + * @param value 保存的value
  365 + * @param saveTime 保存的时间,单位:秒
  366 + */
  367 + public void put(String key, Serializable value, int saveTime) {
  368 + ByteArrayOutputStream baos = null;
  369 + ObjectOutputStream oos = null;
  370 + try {
  371 + baos = new ByteArrayOutputStream();
  372 + oos = new ObjectOutputStream(baos);
  373 + oos.writeObject(value);
  374 + byte[] data = baos.toByteArray();
  375 + if (saveTime != -1) {
  376 + put(key, data, saveTime);
  377 + } else {
  378 + put(key, data);
  379 + }
  380 + } catch (Exception e) {
  381 + e.printStackTrace();
  382 + } finally {
  383 + try {
  384 + oos.close();
  385 + } catch (IOException e) {
  386 + }
  387 + }
  388 + }
  389 +
  390 + /**
  391 + * 读取 Serializable数据
  392 + *
  393 + * @param key
  394 + * @return Serializable 数据
  395 + */
  396 + public Object getAsObject(String key) {
  397 + byte[] data = getAsBinary(key);
  398 + if (data != null) {
  399 + ByteArrayInputStream bais = null;
  400 + ObjectInputStream ois = null;
  401 + try {
  402 + bais = new ByteArrayInputStream(data);
  403 + ois = new ObjectInputStream(bais);
  404 + Object reObject = ois.readObject();
  405 + return reObject;
  406 + } catch (Exception e) {
  407 + e.printStackTrace();
  408 + return null;
  409 + } finally {
  410 + try {
  411 + if (bais != null)
  412 + bais.close();
  413 + } catch (IOException e) {
  414 + e.printStackTrace();
  415 + }
  416 + try {
  417 + if (ois != null)
  418 + ois.close();
  419 + } catch (IOException e) {
  420 + e.printStackTrace();
  421 + }
  422 + }
  423 + }
  424 + return null;
  425 +
  426 + }
  427 +
  428 + // =======================================
  429 + // ============== bitmap 数据 读写 =============
  430 + // =======================================
  431 +
  432 + /**
  433 + * 保存 bitmap 到 缓存中
  434 + *
  435 + * @param key 保存的key
  436 + * @param value 保存的bitmap数据
  437 + */
  438 + public void put(String key, Bitmap value) {
  439 + put(key, Utils.Bitmap2Bytes(value));
  440 + }
  441 +
  442 + /**
  443 + * 保存 bitmap 到 缓存中
  444 + *
  445 + * @param key 保存的key
  446 + * @param value 保存的 bitmap 数据
  447 + * @param saveTime 保存的时间,单位:秒
  448 + */
  449 + public void put(String key, Bitmap value, int saveTime) {
  450 + put(key, Utils.Bitmap2Bytes(value), saveTime);
  451 + }
  452 +
  453 + /**
  454 + * 读取 bitmap 数据
  455 + *
  456 + * @param key
  457 + * @return bitmap 数据
  458 + */
  459 + public Bitmap getAsBitmap(String key) {
  460 + if (getAsBinary(key) == null) {
  461 + return null;
  462 + }
  463 + return Utils.Bytes2Bimap(getAsBinary(key));
  464 + }
  465 +
  466 + // =======================================
  467 + // ============= drawable 数据 读写 =============
  468 + // =======================================
  469 +
  470 + /**
  471 + * 保存 drawable 到 缓存中
  472 + *
  473 + * @param key 保存的key
  474 + * @param value 保存的drawable数据
  475 + */
  476 + public void put(String key, Drawable value) {
  477 + put(key, Utils.drawable2Bitmap(value));
  478 + }
  479 +
  480 + /**
  481 + * 保存 drawable 到 缓存中
  482 + *
  483 + * @param key 保存的key
  484 + * @param value 保存的 drawable 数据
  485 + * @param saveTime 保存的时间,单位:秒
  486 + */
  487 + public void put(String key, Drawable value, int saveTime) {
  488 + put(key, Utils.drawable2Bitmap(value), saveTime);
  489 + }
  490 +
  491 + /**
  492 + * 读取 Drawable 数据
  493 + *
  494 + * @param key
  495 + * @return Drawable 数据
  496 + */
  497 + public Drawable getAsDrawable(String key) {
  498 + if (getAsBinary(key) == null) {
  499 + return null;
  500 + }
  501 + return Utils.bitmap2Drawable(Utils.Bytes2Bimap(getAsBinary(key)));
  502 + }
  503 +
  504 + /**
  505 + * 获取缓存文件
  506 + *
  507 + * @param key
  508 + * @return value 缓存的文件
  509 + */
  510 + public File file(String key) {
  511 + File f = mCache.newFile(key);
  512 + if (f.exists())
  513 + return f;
  514 + return null;
  515 + }
  516 +
  517 + /**
  518 + * 移除某个key
  519 + *
  520 + * @param key
  521 + * @return 是否移除成功
  522 + */
  523 + public boolean remove(String key) {
  524 + return mCache.remove(key);
  525 + }
  526 +
  527 + /**
  528 + * 清除所有数据
  529 + */
  530 + public void clear() {
  531 + mCache.clear();
  532 + }
  533 +
  534 + /**
  535 + * @author 杨福海(michael) www.yangfuhai.com
  536 + * @version 1.0
  537 + * @title 缓存管理器
  538 + */
  539 + public class ACacheManager {
  540 + private final AtomicLong cacheSize;
  541 + private final AtomicInteger cacheCount;
  542 + private final long sizeLimit;
  543 + private final int countLimit;
  544 + private final Map<File, Long> lastUsageDates = Collections
  545 + .synchronizedMap(new HashMap<File, Long>());
  546 + protected File cacheDir;
  547 +
  548 + private ACacheManager(File cacheDir, long sizeLimit, int countLimit) {
  549 + this.cacheDir = cacheDir;
  550 + this.sizeLimit = sizeLimit;
  551 + this.countLimit = countLimit;
  552 + cacheSize = new AtomicLong();
  553 + cacheCount = new AtomicInteger();
  554 + calculateCacheSizeAndCacheCount();
  555 + }
  556 +
  557 + /**
  558 + * 计算 cacheSize和cacheCount
  559 + */
  560 + private void calculateCacheSizeAndCacheCount() {
  561 + new Thread(new Runnable() {
  562 + @Override
  563 + public void run() {
  564 + int size = 0;
  565 + int count = 0;
  566 + File[] cachedFiles = cacheDir.listFiles();
  567 + if (cachedFiles != null) {
  568 + for (File cachedFile : cachedFiles) {
  569 + size += calculateSize(cachedFile);
  570 + count += 1;
  571 + lastUsageDates.put(cachedFile,
  572 + cachedFile.lastModified());
  573 + }
  574 + cacheSize.set(size);
  575 + cacheCount.set(count);
  576 + }
  577 + }
  578 + }).start();
  579 + }
  580 +
  581 + private void put(File file) {
  582 + int curCacheCount = cacheCount.get();
  583 + while (curCacheCount + 1 > countLimit) {
  584 + long freedSize = removeNext();
  585 + cacheSize.addAndGet(-freedSize);
  586 +
  587 + curCacheCount = cacheCount.addAndGet(-1);
  588 + }
  589 + cacheCount.addAndGet(1);
  590 +
  591 + long valueSize = calculateSize(file);
  592 + long curCacheSize = cacheSize.get();
  593 + while (curCacheSize + valueSize > sizeLimit) {
  594 + long freedSize = removeNext();
  595 + curCacheSize = cacheSize.addAndGet(-freedSize);
  596 + }
  597 + cacheSize.addAndGet(valueSize);
  598 +
  599 + Long currentTime = System.currentTimeMillis();
  600 + file.setLastModified(currentTime);
  601 + lastUsageDates.put(file, currentTime);
  602 + }
  603 +
  604 + private File get(String key) {
  605 + File file = newFile(key);
  606 + Long currentTime = System.currentTimeMillis();
  607 + file.setLastModified(currentTime);
  608 + lastUsageDates.put(file, currentTime);
  609 +
  610 + return file;
  611 + }
  612 +
  613 + private File newFile(String key) {
  614 + return new File(cacheDir, key.hashCode() + "");
  615 + }
  616 +
  617 + private boolean remove(String key) {
  618 + File image = get(key);
  619 + return image.delete();
  620 + }
  621 +
  622 + private void clear() {
  623 + lastUsageDates.clear();
  624 + cacheSize.set(0);
  625 + File[] files = cacheDir.listFiles();
  626 + if (files != null) {
  627 + for (File f : files) {
  628 + f.delete();
  629 + }
  630 + }
  631 + }
  632 +
  633 + /**
  634 + * 移除旧的文件
  635 + *
  636 + * @return
  637 + */
  638 + private long removeNext() {
  639 + if (lastUsageDates.isEmpty()) {
  640 + return 0;
  641 + }
  642 +
  643 + Long oldestUsage = null;
  644 + File mostLongUsedFile = null;
  645 + Set<Entry<File, Long>> entries = lastUsageDates.entrySet();
  646 + synchronized (lastUsageDates) {
  647 + for (Entry<File, Long> entry : entries) {
  648 + if (mostLongUsedFile == null) {
  649 + mostLongUsedFile = entry.getKey();
  650 + oldestUsage = entry.getValue();
  651 + } else {
  652 + Long lastValueUsage = entry.getValue();
  653 + if (lastValueUsage < oldestUsage) {
  654 + oldestUsage = lastValueUsage;
  655 + mostLongUsedFile = entry.getKey();
  656 + }
  657 + }
  658 + }
  659 + }
  660 +
  661 + long fileSize = calculateSize(mostLongUsedFile);
  662 + if (mostLongUsedFile.delete()) {
  663 + lastUsageDates.remove(mostLongUsedFile);
  664 + }
  665 + return fileSize;
  666 + }
  667 +
  668 + private long calculateSize(File file) {
  669 + return file.length();
  670 + }
  671 + }
  672 +
  673 + /**
  674 + * @author 杨福海(michael) www.yangfuhai.com
  675 + * @version 1.0
  676 + * @title 时间计算工具类
  677 + */
  678 + private static class Utils {
  679 +
  680 + /**
  681 + * 判断缓存的String数据是否到期
  682 + *
  683 + * @param str
  684 + * @return true:到期了 false:还没有到期
  685 + */
  686 + private static boolean isDue(String str) {
  687 + return isDue(str.getBytes());
  688 + }
  689 +
  690 + /**
  691 + * 判断缓存的byte数据是否到期
  692 + *
  693 + * @param data
  694 + * @return true:到期了 false:还没有到期
  695 + */
  696 + private static boolean isDue(byte[] data) {
  697 + String[] strs = getDateInfoFromDate(data);
  698 + if (strs != null && strs.length == 2) {
  699 + String saveTimeStr = strs[0];
  700 + while (saveTimeStr.startsWith("0")) {
  701 + saveTimeStr = saveTimeStr
  702 + .substring(1, saveTimeStr.length());
  703 + }
  704 + long saveTime = Long.valueOf(saveTimeStr);
  705 + long deleteAfter = Long.valueOf(strs[1]);
  706 + if (System.currentTimeMillis() > saveTime + deleteAfter * 1000) {
  707 + return true;
  708 + }
  709 + }
  710 + return false;
  711 + }
  712 +
  713 + private static String newStringWithDateInfo(int second, String strInfo) {
  714 + return createDateInfo(second) + strInfo;
  715 + }
  716 +
  717 + private static byte[] newByteArrayWithDateInfo(int second, byte[] data2) {
  718 + byte[] data1 = createDateInfo(second).getBytes();
  719 + byte[] retdata = new byte[data1.length + data2.length];
  720 + System.arraycopy(data1, 0, retdata, 0, data1.length);
  721 + System.arraycopy(data2, 0, retdata, data1.length, data2.length);
  722 + return retdata;
  723 + }
  724 +
  725 + private static String clearDateInfo(String strInfo) {
  726 + if (strInfo != null && hasDateInfo(strInfo.getBytes())) {
  727 + strInfo = strInfo.substring(strInfo.indexOf(mSeparator) + 1,
  728 + strInfo.length());
  729 + }
  730 + return strInfo;
  731 + }
  732 +
  733 + private static byte[] clearDateInfo(byte[] data) {
  734 + if (hasDateInfo(data)) {
  735 + return copyOfRange(data, indexOf(data, mSeparator) + 1,
  736 + data.length);
  737 + }
  738 + return data;
  739 + }
  740 +
  741 + private static boolean hasDateInfo(byte[] data) {
  742 + return data != null && data.length > 15 && data[13] == '-'
  743 + && indexOf(data, mSeparator) > 14;
  744 + }
  745 +
  746 + private static String[] getDateInfoFromDate(byte[] data) {
  747 + if (hasDateInfo(data)) {
  748 + String saveDate = new String(copyOfRange(data, 0, 13));
  749 + String deleteAfter = new String(copyOfRange(data, 14,
  750 + indexOf(data, mSeparator)));
  751 + return new String[]{saveDate, deleteAfter};
  752 + }
  753 + return null;
  754 + }
  755 +
  756 + private static int indexOf(byte[] data, char c) {
  757 + for (int i = 0; i < data.length; i++) {
  758 + if (data[i] == c) {
  759 + return i;
  760 + }
  761 + }
  762 + return -1;
  763 + }
  764 +
  765 + private static byte[] copyOfRange(byte[] original, int from, int to) {
  766 + int newLength = to - from;
  767 + if (newLength < 0)
  768 + throw new IllegalArgumentException(from + " > " + to);
  769 + byte[] copy = new byte[newLength];
  770 + System.arraycopy(original, from, copy, 0,
  771 + Math.min(original.length - from, newLength));
  772 + return copy;
  773 + }
  774 +
  775 + private static final char mSeparator = ' ';
  776 +
  777 + private static String createDateInfo(int second) {
  778 + String currentTime = System.currentTimeMillis() + "";
  779 + while (currentTime.length() < 13) {
  780 + currentTime = "0" + currentTime;
  781 + }
  782 + return currentTime + "-" + second + mSeparator;
  783 + }
  784 +
  785 + /*
  786 + * Bitmap → byte[]
  787 + */
  788 + private static byte[] Bitmap2Bytes(Bitmap bm) {
  789 + if (bm == null) {
  790 + return null;
  791 + }
  792 + ByteArrayOutputStream baos = new ByteArrayOutputStream();
  793 + bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
  794 + return baos.toByteArray();
  795 + }
  796 +
  797 + /*
  798 + * byte[] → Bitmap
  799 + */
  800 + private static Bitmap Bytes2Bimap(byte[] b) {
  801 + if (b.length == 0) {
  802 + return null;
  803 + }
  804 + return BitmapFactory.decodeByteArray(b, 0, b.length);
  805 + }
  806 +
  807 + /*
  808 + * Drawable → Bitmap
  809 + */
  810 + private static Bitmap drawable2Bitmap(Drawable drawable) {
  811 + if (drawable == null) {
  812 + return null;
  813 + }
  814 + // 取 drawable 的长宽
  815 + int w = drawable.getIntrinsicWidth();
  816 + int h = drawable.getIntrinsicHeight();
  817 + // 取 drawable 的颜色格式
  818 + Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
  819 + : Bitmap.Config.RGB_565;
  820 + // 建立对应 bitmap
  821 + Bitmap bitmap = Bitmap.createBitmap(w, h, config);
  822 + // 建立对应 bitmap 的画布
  823 + Canvas canvas = new Canvas(bitmap);
  824 + drawable.setBounds(0, 0, w, h);
  825 + // 把 drawable 内容画到画布中
  826 + drawable.draw(canvas);
  827 + return bitmap;
  828 + }
  829 +
  830 + /*
  831 + * Bitmap → Drawable
  832 + */
  833 + @SuppressWarnings("deprecation")
  834 + private static Drawable bitmap2Drawable(Bitmap bm) {
  835 + if (bm == null) {
  836 + return null;
  837 + }
  838 + return new BitmapDrawable(bm);
  839 + }
  840 + }
869 841
870 } 842 }
@@ -517,7 +517,7 @@ public class MovieDetailMsgActivity extends BaseActivity @@ -517,7 +517,7 @@ public class MovieDetailMsgActivity extends BaseActivity
517 517
518 } 518 }
519 519
520 - public class MovieItemDecoration extends RecyclerView.ItemDecoration { 520 + public static class MovieItemDecoration extends RecyclerView.ItemDecoration {
521 @Override 521 @Override
522 public void getItemOffsets(Rect outRect, View view, RecyclerView parent, 522 public void getItemOffsets(Rect outRect, View view, RecyclerView parent,
523 RecyclerView.State state) { 523 RecyclerView.State state) {
@@ -528,7 +528,7 @@ public class MovieDetailMsgActivity extends BaseActivity @@ -528,7 +528,7 @@ public class MovieDetailMsgActivity extends BaseActivity
528 } 528 }
529 } 529 }
530 530
531 - private class SerAdapter extends BaseAdapter { 531 + private static class SerAdapter extends BaseAdapter {
532 Context context; 532 Context context;
533 ArrayList<String> items; 533 ArrayList<String> items;
534 534
@@ -79,10 +79,11 @@ public class MovieListModelImpl implements IMovieListModel { @@ -79,10 +79,11 @@ public class MovieListModelImpl implements IMovieListModel {
79 list = dbManager.queryType(keyword, queryType, 79 list = dbManager.queryType(keyword, queryType,
80 SourceType.values()[index]); 80 SourceType.values()[index]);
81 } 81 }
82 - if (list != null && list.size() >= 0) { 82 + if (list != null && !list.isEmpty()) {
83 MovieListModelImpl.this.handleSuccess(listener, list); 83 MovieListModelImpl.this.handleSuccess(listener, list);
84 } else { 84 } else {
85 - MovieListModelImpl.this.handleFailure(listener); 85 + MovieListModelImpl.this.handleSuccess(listener, list);
  86 +// MovieListModelImpl.this.handleFailure(listener);
86 } 87 }
87 } 88 }
88 }); 89 });
@@ -247,7 +247,7 @@ public class AskService extends Service implements IAskView { @@ -247,7 +247,7 @@ public class AskService extends Service implements IAskView {
247 247
248 @Override 248 @Override
249 public IBinder onBind(Intent intent) { 249 public IBinder onBind(Intent intent) {
250 - return null; 250 + throw new NullPointerException();
251 } 251 }
252 252
253 @Override 253 @Override
@@ -34,7 +34,7 @@ public class CountService extends Service { @@ -34,7 +34,7 @@ public class CountService extends Service {
34 34
35 @Override 35 @Override
36 public IBinder onBind(Intent intent) { 36 public IBinder onBind(Intent intent) {
37 - throw null; 37 + throw new NullPointerException();
38 } 38 }
39 39
40 40
@@ -68,7 +68,7 @@ public class MovieMessageService extends Service { @@ -68,7 +68,7 @@ public class MovieMessageService extends Service {
68 68
69 @Override 69 @Override
70 public IBinder onBind(Intent intent) { 70 public IBinder onBind(Intent intent) {
71 - return null; 71 + throw new NullPointerException();
72 } 72 }
73 73
74 @Override 74 @Override
@@ -70,7 +70,7 @@ public class SearchMovieMessageService extends Service { @@ -70,7 +70,7 @@ public class SearchMovieMessageService extends Service {
70 70
71 @Override 71 @Override
72 public IBinder onBind(Intent intent) { 72 public IBinder onBind(Intent intent) {
73 - return null; 73 + throw new NullPointerException();
74 } 74 }
75 75
76 @Override 76 @Override
@@ -97,8 +97,8 @@ public class RatingBarView extends LinearLayout { @@ -97,8 +97,8 @@ public class RatingBarView extends LinearLayout {
97 this.starEmptyDrawable = starEmptyDrawable; 97 this.starEmptyDrawable = starEmptyDrawable;
98 } 98 }
99 99
100 - public void setStarCount(int startCount) {  
101 - this.starCount = starCount; 100 + public void setStarCount(int cc) {
  101 + this.starCount = cc;
102 } 102 }
103 103
104 public void setStarImageSize(float starImageSize) { 104 public void setStarImageSize(float starImageSize) {
Please register or login to post a comment