Commit a752f752e6e837579a5b51788cde827fe7f455fd

Authored by 李攀
1 parent 457e8f8b

scan and close led when play

1 1 package com.gimi.common.cinema.utils;
2 2
3   -import android.annotation.SuppressLint;
4 3 import android.content.Context;
  4 +import android.media.MediaPlayer;
5 5 import android.text.TextUtils;
6 6 import android.util.Log;
7 7 import com.gimi.common.cinema.model.AsyncCallback;
... ... @@ -15,8 +15,18 @@ import com.xgimi.gimicinema.R;
15 15 import com.xgimi.gimicinema.activity.CinemaConfig;
16 16
17 17 import java.io.File;
  18 +import java.io.IOException;
18 19 import java.util.ArrayList;
  20 +import java.util.Arrays;
19 21 import java.util.HashMap;
  22 +import java.util.List;
  23 +import java.util.concurrent.Callable;
  24 +import java.util.concurrent.Future;
  25 +import java.util.concurrent.LinkedBlockingDeque;
  26 +import java.util.concurrent.ThreadFactory;
  27 +import java.util.concurrent.ThreadPoolExecutor;
  28 +import java.util.concurrent.TimeUnit;
  29 +import java.util.concurrent.atomic.AtomicInteger;
20 30
21 31 /**
22 32 * movie scan
... ... @@ -25,7 +35,6 @@ import java.util.HashMap;
25 35 public class LocalMovieScanUtils {
26 36 private String[] picExtensions;
27 37 private String[] mediaExtensions;
28   -// private ArrayList<SaveMessage> result = new ArrayList<>();
29 38
30 39 public LocalMovieScanUtils(Context context) {
31 40 picExtensions = context.getResources().getStringArray(R.array.photo_filter);
... ... @@ -45,7 +54,7 @@ public class LocalMovieScanUtils {
45 54 FolderItem folderItem = new FolderItem();
46 55 folderItem.setFolderName(fileDir);
47 56 folderItem.setFolderPath(rootPath + fileDir + "/");
48   -// if (fileDir.contains("3D")) {
  57 +// if (fileDir.contains("AQ")) {
49 58 types.add(folderItem);
50 59 // }
51 60 }
... ... @@ -181,6 +190,7 @@ public class LocalMovieScanUtils {
181 190 * @return ArrayList<LocalMovieMessage>
182 191 */
183 192 ArrayList<LocalMovieMessage> getAllLocalMovie(String rootPath, String folder, AsyncCallback<Integer> callback) {
  193 + Log.d("scan-time", "start:" + System.currentTimeMillis());
184 194 ArrayList<String> all = new ArrayList<>();
185 195 File file = new File(rootPath);
186 196 if (file.listFiles() != null) {
... ... @@ -226,7 +236,7 @@ public class LocalMovieScanUtils {
226 236 moviesItems.addAll(getAllType(type.getFolderPath(), callback));
227 237 }
228 238 }
229   -// FileReadUtils.writeDate(rootPath + "/length.txt", new Gson().toJson(result));
  239 + Log.d("scan-time", " end:" + System.currentTimeMillis());
230 240 return moviesItems;
231 241 }
232 242
... ... @@ -249,69 +259,193 @@ public class LocalMovieScanUtils {
249 259 * @param callback callback
250 260 * @return ArrayList<LocalMovieMessage>
251 261 */
252   - @SuppressLint("SdCardPath")
253 262 private ArrayList<LocalMovieMessage> getAllType(String rootPath, AsyncCallback<Integer> callback) {
254 263 ArrayList<LocalMovieMessage> movies = new ArrayList<>();
255 264 if (rootPath.contains("lost+found")) {
256 265 return movies;
257 266 }
  267 + if (callback != null) {
  268 + String[] split = rootPath.split("/");
  269 + callback.onMessage(split[split.length - 1]);
  270 + }
258 271 File file = new File(rootPath);
259 272 String[] fileDirs = file.list();
260 273 //遍历
261 274 if (fileDirs == null || fileDirs.length == 0) {
262 275 return movies;
263 276 }
264   - for (String fileDir : fileDirs) {
265   - File curFiles = new File(rootPath + fileDir);
266   - if (curFiles.isDirectory()) {
267   - if (callback != null) {
268   - callback.onMessage(fileDir);
269   - }
270   - String curPath = curFiles.getAbsoluteFile().toString();
271   - LocalMovieMessage moviesItem = new LocalMovieMessage();
272   - String name = NameFilterUtils.getName(fileDir).trim();
273   - moviesItem.setMovieName(name);
274   - String allFirstSpell = PinyinUtil.getAllFirstSpell(name);
275   - String newStr = allFirstSpell.replaceAll("[^\\w,]", "");
276   - moviesItem.setNamePinyin(newStr);
277   - String media = getMedia(curPath + "/", mediaExtensions);
278   - String poster;
279   - poster = MovieMessageUtils.getLocalMoviePoster(curPath);
280   - if (TextUtils.isEmpty(poster)) {
281   - poster = getMediaPicture(curPath + "/", picExtensions);
282   - }
283   - if (!TextUtils.isEmpty(poster)) {
284   - moviesItem.setPosterPath(poster);
285   - }
286   - if (!TextUtils.isEmpty(media)) {
287   - File mFile = new File(media);
288   - long length = mFile.length();
289   - String md5 = MD5Utils.stringMD5(FileHashUtils.getFileHash(media));
290   - moviesItem.setMd5(md5);
291   - //read douban id and douban msg,至于name id 信息另外做,没有必要每次更新时都去添加
292   - try {
293   - setDoubanMsg(curPath, moviesItem);
294   - } catch (Exception e) {
295   - Log.e("otherError", media);
296   - e.printStackTrace();
  277 + int length = fileDirs.length;
  278 + int coreCount = 8;
  279 + int poolCount = 9;
  280 + ThreadPoolExecutor myExecutor = new ThreadPoolExecutor(coreCount, poolCount,
  281 + 200, TimeUnit.SECONDS, new LinkedBlockingDeque<Runnable>());
  282 + List<Future<ArrayList<LocalMovieMessage>>> results = new ArrayList<>();
  283 + for (int i = 0; i < coreCount; i++) {
  284 + ScanTask task = new ScanTask(rootPath, Arrays.copyOfRange(
  285 + fileDirs, length * i / coreCount, (length * (i + 1)) / coreCount));
  286 + Future<ArrayList<LocalMovieMessage>> result = myExecutor.submit(task);
  287 + results.add(result);
  288 + }
  289 + for (Future<ArrayList<LocalMovieMessage>> f : results) {
  290 + try {
  291 + movies.addAll(f.get());
  292 + } catch (Exception ex) {
  293 + ex.printStackTrace();
  294 + f.cancel(true);
  295 + }
  296 + }
  297 + myExecutor.shutdown();
  298 +// for (String fileDir : fileDirs) {
  299 +// File curFiles = new File(rootPath + fileDir);
  300 +// if (curFiles.isDirectory()) {
  301 +// if (callback != null) {
  302 +// callback.onMessage(fileDir);
  303 +// }
  304 +// String curPath = curFiles.getAbsoluteFile().toString();
  305 +// LocalMovieMessage moviesItem = new LocalMovieMessage();
  306 +// String name = NameFilterUtils.getName(fileDir).trim();
  307 +// moviesItem.setMovieName(name);
  308 +// String allFirstSpell = PinyinUtil.getAllFirstSpell(name);
  309 +// String newStr = allFirstSpell.replaceAll("[^\\w,]", "");
  310 +//// Log.d("lovely_pinyin", allFirstSpell + ":" + newStr);
  311 +// moviesItem.setNamePinyin(newStr);
  312 +// String media = getMedia(curPath + "/", mediaExtensions);
  313 +// String poster;
  314 +// poster = MovieMessageUtils.getLocalMoviePoster(curPath);
  315 +// if (TextUtils.isEmpty(poster)) {
  316 +// poster = getMediaPicture(curPath + "/", picExtensions);
  317 +// }
  318 +// if (!TextUtils.isEmpty(poster)) {
  319 +// moviesItem.setPosterPath(poster);
  320 +// }
  321 +// if (!TextUtils.isEmpty(media)) {
  322 +// File mFile = new File(media);
  323 +//// long length = mFile.length();
  324 +// moviesItem.setMd5(MD5Utils.stringMD5(FileHashUtils.getFileHash(media)));
  325 +// //read douban id and douban msg,至于name id 信息另外做,没有必要每次更新时都去添加
  326 +// try {
  327 +// setDoubanMsg(curPath, moviesItem);
  328 +// } catch (Exception e) {
  329 +// Log.e("otherError", media);
  330 +// e.printStackTrace();
  331 +// }
  332 +// moviesItem.setType(fileDir);
  333 +// moviesItem.setPlayPath(media);
  334 +// String movieDlTime = String.valueOf(mFile.lastModified());
  335 +// moviesItem.setMovieLength(movieDlTime);
  336 +// moviesItem.setDlTime(movieDlTime);
  337 +// moviesItem.setCount(MovieMessageUtils.getPlayCount(curPath));
  338 +// movies.add(moviesItem);
  339 +// }
  340 +// }
  341 +// }
  342 + return movies;
  343 + }
  344 +
  345 + private class ScanTask implements Callable<ArrayList<LocalMovieMessage>> {
  346 + private String rootPath;
  347 + private String[] dirs;
  348 +
  349 + private ScanTask(String rootPath, String[] dirs) {
  350 + this.rootPath = rootPath;
  351 + this.dirs = dirs;
  352 + }
  353 +
  354 + @Override
  355 + public ArrayList<LocalMovieMessage> call() throws Exception {
  356 + ArrayList<LocalMovieMessage> movies = new ArrayList<>();
  357 + for (String fileDir : dirs) {
  358 + File curFiles = new File(rootPath + fileDir);
  359 + if (curFiles.isDirectory()) {
  360 + String curPath = curFiles.getAbsoluteFile().toString();
  361 + LocalMovieMessage moviesItem = new LocalMovieMessage();
  362 + String name = NameFilterUtils.getName(fileDir).trim();
  363 + moviesItem.setMovieName(name);
  364 + String allFirstSpell = PinyinUtil.getAllFirstSpell(name);
  365 + String newStr = allFirstSpell.replaceAll("[^\\w,]", "");
  366 + moviesItem.setNamePinyin(newStr);
  367 + String media = getMedia(curPath + "/", mediaExtensions);
  368 + String poster;
  369 + poster = MovieMessageUtils.getLocalMoviePoster(curPath);
  370 + if (TextUtils.isEmpty(poster)) {
  371 + poster = getMediaPicture(curPath + "/", picExtensions);
  372 + }
  373 + if (!TextUtils.isEmpty(poster)) {
  374 + moviesItem.setPosterPath(poster);
  375 + }
  376 + if (!TextUtils.isEmpty(media)) {
  377 + File mFile = new File(media);
  378 +// long length = mFile.length();
  379 + moviesItem.setMd5(MD5Utils.stringMD5(FileHashUtils.getFileHash(media)));
  380 + //read douban id and douban msg,至于name id 信息另外做,没有必要每次更新时都去添加
  381 + try {
  382 + setDoubanMsg(curPath, moviesItem);
  383 + } catch (Exception e) {
  384 + Log.e("otherError", media);
  385 + e.printStackTrace();
  386 + }
  387 + moviesItem.setType(fileDir);
  388 + moviesItem.setPlayPath(media);
  389 + String movieDlTime = String.valueOf(mFile.lastModified());
  390 + moviesItem.setMovieLength(movieDlTime);
  391 + moviesItem.setDlTime(movieDlTime);
  392 + moviesItem.setCount(MovieMessageUtils.getPlayCount(curPath));
  393 + movies.add(moviesItem);
  394 + curPath = null;
  395 + name = null;
  396 + allFirstSpell = null;
  397 + newStr = null;
  398 + media = null;
  399 + poster = null;
  400 + movieDlTime = null;
297 401 }
298   -// SaveMessage saveMessage = new SaveMessage();
299   -// saveMessage.setFile_hash(md5);
300   -// long movieLength = getMovieLength(media);
301   -// int len = (int) (movieLength / 60);
302   -// saveMessage.setLength(len+"");
303   -// result.add(saveMessage);
304   - moviesItem.setType(fileDir);
305   - moviesItem.setPlayPath(media);
306   - String movieDlTime = String.valueOf(mFile.lastModified());
307   - moviesItem.setMovieLength(movieDlTime);
308   - moviesItem.setDlTime(movieDlTime);
309   - moviesItem.setCount(MovieMessageUtils.getPlayCount(curPath));
310   - movies.add(moviesItem);
311 402 }
312 403 }
  404 + return movies;
313 405 }
314   - return movies;
  406 + }
  407 +
  408 + /**
  409 + * The default thread factory.
  410 + */
  411 + private static class MyThreadFactory implements ThreadFactory {
  412 + private static final AtomicInteger poolNumber = new AtomicInteger(1);
  413 + private final ThreadGroup group;
  414 + private final AtomicInteger threadNumber = new AtomicInteger(1);
  415 + private final String namePrefix;
  416 +
  417 + MyThreadFactory() {
  418 + SecurityManager s = System.getSecurityManager();
  419 + group = (s != null) ? s.getThreadGroup() :
  420 + Thread.currentThread().getThreadGroup();
  421 + namePrefix = "pool-" +
  422 + poolNumber.getAndIncrement() +
  423 + "-thread-";
  424 + }
  425 +
  426 + public Thread newThread(Runnable r) {
  427 + Thread t = new Thread(group, r,
  428 + namePrefix + threadNumber.getAndIncrement(),
  429 + 0);
  430 +
  431 + t.setDaemon(true);
  432 + if (t.getPriority() != Thread.NORM_PRIORITY)
  433 + t.setPriority(Thread.NORM_PRIORITY);
  434 + return t;
  435 + }
  436 + }
  437 +
  438 + private synchronized long getDuration(String path) {
  439 + long in = System.currentTimeMillis();
  440 + MediaPlayer mMediaPlayer = new MediaPlayer();
  441 + try {
  442 + mMediaPlayer.setDataSource(path);
  443 + } catch (IOException e) {
  444 + e.printStackTrace();
  445 + }
  446 + int duration = mMediaPlayer.getDuration();
  447 + Log.d("duration", (System.currentTimeMillis() - in) + ":" + duration);
  448 + return duration;
315 449 }
316 450
317 451 private long getMovieLength(String mUri) {
... ... @@ -337,6 +471,6 @@ public class LocalMovieScanUtils {
337 471 mmr.release();
338 472 }
339 473 Log.d("getAllType", "" + (System.currentTimeMillis() - l));
340   - return duration / 1000;
  474 + return duration;
341 475 }
342 476 }
... ...
... ... @@ -29,21 +29,16 @@ public class QrCodeShowActivity extends Activity implements IUpdateQrCodeView {
29 29 public static final int KILL_SELF = 0x9983;
30 30 private Handler handler = new Handler();
31 31 private QrCodeShowPresent present;
32   - Runnable r = new Runnable() {
33   - @Override
34   - public void run() {
35   - closeSystem();
36   - }
37   - };
  32 +// Runnable r = new Runnable() {
  33 +// @Override
  34 +// public void run() {
  35 +// closeSystem();
  36 +// }
  37 +// };
38 38
39   - private void closeSystem() {
40   - LogUtils.i("closeSystem");
41   - LightOperationUtils.close();
42   - LightOperationUtils.setLightValue(5);
43   - new SystemUtils().closeFtLed(QrCodeShowActivity.this.getApplicationContext());
44   - present.reportSleepStatus(orderSn, roomSn);
45   - QrCodeShowActivity.this.finish();
46   - }
  39 +// private void closeSystem() {
  40 +//
  41 +// }
47 42
48 43 private String orderSn;
49 44 private String roomSn;
... ... @@ -54,7 +49,12 @@ public class QrCodeShowActivity extends Activity implements IUpdateQrCodeView {
54 49 public void run() {
55 50 //日志显示,没有结束,以此确保20min后关闭相关
56 51 if (updateCount++ == 2) {
57   - closeSystem();
  52 + LogUtils.i("closeSystem");
  53 + LightOperationUtils.close();
  54 + LightOperationUtils.setLightValue(5);
  55 + new SystemUtils().closeFtLed(QrCodeShowActivity.this.getApplicationContext());
  56 + present.reportSleepStatus(orderSn, roomSn);
  57 + QrCodeShowActivity.this.finish();
58 58 return;
59 59 }
60 60 LogUtils.i("room-info", "QrCodeShowActivity update qr code runnable run");
... ... @@ -72,7 +72,6 @@ public class QrCodeShowActivity extends Activity implements IUpdateQrCodeView {
72 72 orderSn = getIntent().getStringExtra("order_sn");
73 73 roomSn = getIntent().getStringExtra("room_sn");
74 74 iv = (ImageView) findViewById(qrCodeIv);
75   - handler.postDelayed(r, 20 * 60 * 1000);
76 75
77 76 if (!TextUtils.isEmpty(roomSn) && !TextUtils.isEmpty(orderSn)) {
78 77 handler.post(updateQrCodeRunnable);
... ... @@ -89,7 +88,7 @@ public class QrCodeShowActivity extends Activity implements IUpdateQrCodeView {
89 88 } catch (Exception e) {
90 89 e.printStackTrace();
91 90 }
92   - handler.removeCallbacks(r);
  91 + handler.removeCallbacks(updateQrCodeRunnable);
93 92 handler.removeCallbacks(null);
94 93 EventBus.getDefault().unregister(this);
95 94 }
... ... @@ -97,7 +96,7 @@ public class QrCodeShowActivity extends Activity implements IUpdateQrCodeView {
97 96 @Override
98 97 protected void onPause() {
99 98 super.onPause();
100   - handler.removeCallbacks(r);
  99 + handler.removeCallbacks(updateQrCodeRunnable);
101 100 handler.removeCallbacks(null);
102 101 }
103 102
... ...
Please register or login to post a comment