@Override
  public List<FileItem> getTDDownloadList(int complete) {
    List<FileItem> fileItems = new ArrayList<FileItem>();

    int pageIndex = 0;
    int pageCapacity = 100;

    List<FileItem> list = getTasklist(complete, pageIndex, pageCapacity);
    if (list != null && list.size() > 0) {
      fileItems.addAll(list);
    }
    while (list != null && list.size() >= 100) {
      pageIndex++;
      list = getTasklist(complete, pageIndex, pageCapacity);
      if (list != null && list.size() > 0) {
        fileItems.addAll(list);
      }
    }

    if (complete == GetTaskListAPI.TYPE_TASK_COMPLETED) {
      // 取路由器文件cid
      for (FileItem item : fileItems) {
        if (!TextUtils.isEmpty(item.filePath)) {
          String smbPath = SmbUtil.getSmbPathFromPlayUrl(item.filePath);
          try {
            SmbFile smbFile = new SmbFile(smbPath);
            BufferedInputStream bis = new BufferedInputStream(new SmbFileInputStream(smbFile));
            String cid = CidUtil.queryCid(bis, smbFile.length());
            if (!TextUtils.isEmpty(cid)) {
              item.cid = cid;
            } else {
              item.cid = "";
            }
          } catch (SmbException e) {
          } catch (MalformedURLException e) {
          } catch (UnknownHostException e) {
          }
        }
      }

    } else {
      for (int i = fileItems.size() - 1; i >= 0; i--) {
        FileItem item = fileItems.get(i);
        if (item.fileStatus != Constants.TDTASK_STATUS_DOWNLOADING) {
          fileItems.remove(item);
        }
      }
    }
    return fileItems;
  }
  private List<FileItem> getTasklist(int complete, int pageindex, int pageCapacity) {
    List<FileItem> fileItems = new ArrayList<FileItem>();

    GetTaskListAPI taskListAPI =
        new GetTaskListAPI(mRouterIp, complete, pageindex, pageCapacity, mVersionCode, false);
    AppConfig.logRemote("router tddownload get tasklist");
    TaskList taskList = null;
    try {
      taskList = taskListAPI.request();
      if (taskList != null && taskList.tasks != null) {
        for (TaskList.Task task : taskList.tasks) {
          String path = task.filePath;
          String smbPath = getRouterSmbPath(path);
          AppConfig.logRemote("router tddownload file path is: " + smbPath);

          if (complete == GetTaskListAPI.TYPE_TASK_COMPLETED) {
            FileItem item = createFileItem(smbPath);
            if (item != null) {
              AppConfig.logRemote("router item: " + item.filePath + "  category:" + item.category);
              item.fileStatus = task.stat;
              if (item.category == FileCategory.DIR || item.category == FileCategory.VIDEO) {
                fileItems.add(item);
              }
            }
          } else {
            FileItem item = new FileItem();
            item.fileStatus = task.stat;
            item.filePath = task.filePath;
            fileItems.add(item);
          }
        }

        AppConfig.logRemote("router gettasklist size is: " + fileItems.size());

        return fileItems;
      }
    } catch (IOException e) {
      AppConfig.logRemote("[[XLRouterDownloadMgr]] getTaskList IOException:" + e.getMessage());
    }

    return null;
  }
  public static List<FileItem> filterItems(List<FileItem> list, Context context) {

    List<FileItem> result = new ArrayList<FileItem>();

    FileExploreHistoryManager fileExploreHistoryManager = new FileExploreHistoryManager(context);

    DeviceItem deviceItem = new DeviceItem();
    deviceItem.setType(DeviceItem.DeviceType.XL_ROUTER_TDDOWNLOAD);

    if (list != null) {

      for (FileItem fileItem : list) {

        fileItem.isNew = fileExploreHistoryManager.isFileNew(fileItem, deviceItem);

        if (fileItem.category == FileCategory.VIDEO) {
          result.add(fileItem);
        }

        if (fileItem.category == FileCategory.DIR) {
          AppConfig.logRemote("router file.getpaht is: " + fileItem.filePath);
          List<FileItem> childrens = getChildFiles(fileItem.filePath + File.separator, context);
          if (childrens != null && childrens.size() > 0) {

            for (int i = 0; i < childrens.size(); i++) {
              FileItem childItem = childrens.get(i);
              if (childItem.category == FileCategory.VIDEO
                  || childItem.category == FileCategory.DIR) {
                result.add(fileItem);
                break;
              }
            }
          }
        }
      }
    }

    AppConfig.logRemote("router tddownload after filter : " + result.size());

    return result;
  }
  public static List<FileItem> getChildFiles(String smbPath, Context context) {
    try {
      List<FileItem> list = new ArrayList<FileItem>();

      SmbFile smbRootFile = new SmbFile(smbPath);

      if (!smbRootFile.exists()) {
        return null;
      }

      SmbFile[] fileList = smbRootFile.listFiles();
      if (fileList == null || fileList.length == 0) {
        return null;
      }

      FileExploreHistoryManager fileExploreHistoryManager = new FileExploreHistoryManager(context);

      DeviceItem deviceItem = new DeviceItem();
      deviceItem.setType(DeviceItem.DeviceType.XL_ROUTER_TDDOWNLOAD);

      for (SmbFile smbFile : fileList) {
        FileItem item = new FileItem();
        String fileName = smbFile.getName();
        String filePath = smbFile.getCanonicalPath();
        item.filePath = filePath;
        item.lastModifyTime = smbFile.getLastModified();
        item.canRead = smbFile.canRead();
        item.canWrite = smbFile.canWrite();
        try {
          BufferedInputStream bis = new BufferedInputStream(new SmbFileInputStream(smbFile));
          String cid = CidUtil.queryCid(bis, smbFile.length());
          if (!TextUtils.isEmpty(cid)) {
            item.cid = cid;
          } else {
            item.cid = "";
          }
        } catch (SmbException e) {
        } catch (MalformedURLException e) {
        } catch (UnknownHostException e) {
        }

        if (smbFile.isDirectory()) {
          fileName = fileName.substring(0, fileName.length() - 1);
          item.fileName = fileName;

          item.category = FileCategory.DIR;
          item.fileSize = 0;

          item.isNew = fileExploreHistoryManager.isFileNew(item, deviceItem);

          list.add(item);
        } else if (smbFile.isFile()) {
          item.category = FileCategoryHelper.getFileCategoryByName(fileName);
          item.fileSize = smbFile.getContentLength();
          item.fileName = fileName;

          if (item.category == FileCategory.VIDEO) {
            item.isNew = fileExploreHistoryManager.isFileNew(item, deviceItem);
            list.add(item);
          }
        }
      }

      return list;

    } catch (MalformedURLException e) {
      AppConfig.logRemote(
          "[[XlrounterDownloadMgr]] createFileItem MalformedURLException:" + e.getMessage());
    } catch (SmbException e) {
      AppConfig.logRemote("[[XlrounterDownloadMgr]] createFileItem SmbException:" + e.getMessage());
    }

    return null;
  }
  public static FileItem createFileItem(String smbPath) {
    try {
      SmbFile smbFile = new SmbFile(smbPath);
      if (!smbFile.exists()) {
        return null;
      }

      FileItem item = new FileItem();
      String fileName = smbFile.getName();
      String filePath = smbFile.getCanonicalPath();

      if (smbFile.isDirectory()) {
        fileName = fileName.substring(0, fileName.length() - 1);
        item.category = FileCategory.DIR;
        item.fileSize = 0;
      } else if (smbFile.isFile()) {
        item.category = FileCategoryHelper.getFileCategoryByName(fileName);
        item.fileSize = smbFile.getContentLength();
      }

      item.fileName = fileName;
      item.filePath = filePath;
      item.lastModifyTime = smbFile.getLastModified();
      item.canRead = smbFile.canRead();
      item.canWrite = smbFile.canWrite();
      item.cid = "";

      return item;

    } catch (MalformedURLException e) {
      AppConfig.logRemote(
          "[[XlrounterDownloadMgr]] createFileItem MalformedURLException:" + e.getMessage());
    } catch (SmbException e) {
      AppConfig.logRemote("[[XlrounterDownloadMgr]] createFileItem SmbException:" + e.getMessage());
    }

    return null;
  }