/** 得到影片历史列表 */
  public synchronized List<DownLoadInfo> getSmovieHistroy() {

    List<DownLoadInfo> resultsInfos = new ArrayList<DownLoadInfo>();
    String sql =
        "select  *  from "
            + PipiDBHelp.HISTROY_TABLENAME
            + " order by "
            + TableName.Movie_ID
            + " desc";
    try {
      database = pipiDBHelp.getWritableDatabase();
      cursor = database.rawQuery(sql, null);
      if (cursor != null && cursor.getCount() != 0) {
        while (cursor.moveToNext()) {
          DownLoadInfo downInfo = new DownLoadInfo();
          downInfo.setDownID(cursor.getString(cursor.getColumnIndex("sMovieID")));
          downInfo.setDownName(cursor.getString(cursor.getColumnIndex("sMovieName")));
          downInfo.setDownImg(cursor.getString(cursor.getColumnIndex("sMovieImgUrl")));
          downInfo.setDownUrl(cursor.getString(cursor.getColumnIndex("sMovieUrl")));
          downInfo.setDownTotalSize(cursor.getInt(cursor.getColumnIndex("sMovieSize")));
          downInfo.setDownProgress(cursor.getInt(cursor.getColumnIndex("sMoviePlayProgress")));
          downInfo.setDownState(cursor.getInt(cursor.getColumnIndex("sMovieLoadState")));
          downInfo.setDownTag(cursor.getString(cursor.getColumnIndex(TableName.MoviePlaySourKey)));
          downInfo.setDownPath(cursor.getString(cursor.getColumnIndex("sMovieLocalUrl")));
          downInfo.setDownPosition(
              cursor.getInt(cursor.getColumnIndex(TableName.MoviePlayPosition)));
          resultsInfos.add(downInfo);
        }
      }

    } catch (Exception e) {
      // TODO: handle exception
    } finally {
      closeCursor();
    }
    return resultsInfos;
  }
  public synchronized List<DownTask> getActiveDownLoad() {
    List<DownTask> mDownLoadTaskList = new ArrayList<DownTask>();
    SQLiteDatabase database = null;
    Cursor cursor = null;
    String sql = "select  *  from " + PipiDBHelp.STORE_TABLENAME;

    try {
      database = pipiDBHelp.getReadableDatabase();
      cursor = database.rawQuery(sql, null);
      while (cursor.moveToNext()) {

        DownLoadInfo downInfo = new DownLoadInfo();
        downInfo.setDownID(cursor.getString(cursor.getColumnIndex("sMovieID")));
        downInfo.setDownName(cursor.getString(cursor.getColumnIndex("sMovieName")));
        downInfo.setDownImg(cursor.getString(cursor.getColumnIndex("sMovieImgUrl")));
        downInfo.setDownUrl(cursor.getString(cursor.getColumnIndex("sMovieUrl")));
        downInfo.setDownTotalSize(cursor.getLong(cursor.getColumnIndex("sMovieSize")));
        downInfo.setDownProgress(cursor.getLong(cursor.getColumnIndex("sMoviePlayProgress")));
        downInfo.setDownState(cursor.getInt(cursor.getColumnIndex("sMovieLoadState")));
        downInfo.setDownPosition(cursor.getInt(cursor.getColumnIndex(TableName.MoviePlayPosition)));
        downInfo.setDownTag(cursor.getString(cursor.getColumnIndex(TableName.MoviePlaySourKey)));
        String path = cursor.getString(cursor.getColumnIndex("sMovieLocalUrl"));
        downInfo.setDownPath(path);
        JSONArray array;
        try {
          array = new JSONArray(cursor.getString(cursor.getColumnIndex("sMoviePlayList")));
          ArrayList<String> list = new ArrayList<String>();
          for (int i = 0; i < array.length(); i++) list.add(array.getString(i));

          downInfo.setPlayList(list);
          // 下载片段索引
          if (!TextUtils.isEmpty(path)) {
            try {
              File file = new File(path);
              if (file != null && file.isDirectory()) {
                downInfo.setDownIndex(
                    file.listFiles().length - 1 > 0 ? file.listFiles().length - 1 : 0);
              }
            } catch (Exception e) {
              // TODO: handle exception
            }
          }
        } catch (JSONException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        }

        DownTask task = new DownTask(downInfo);
        if (downInfo.getDownState() == DownTask.TASK_FileMerge) {
          // task.finish();//上次未合并完的再次进行合并
        } else if (downInfo.getDownState() != DownTask.TASK_FINISHED) // 已经下载完的
        downInfo.setDownState(DownTask.TASK_PAUSE_DOWNLOAD); // 未下载完的标示为暂停,让用户主动去下�?
        mDownLoadTaskList.add(task);
      }
    } finally {
      if (cursor != null) {
        cursor.close();
      }
      database.close();
    }
    return mDownLoadTaskList;
  }
  /** 得到影片下载列表 */
  public synchronized List<DownLoadInfo> getSmovieStores() {

    List<DownLoadInfo> resultsInfos = new ArrayList<DownLoadInfo>();
    String sql =
        "select  *  from "
            + PipiDBHelp.STORE_TABLENAME
            + " order by "
            + TableName.Movie_ID
            + " desc ";
    try {
      database = pipiDBHelp.getWritableDatabase();
      cursor = database.rawQuery(sql, null);
      if (cursor != null && cursor.getCount() != 0) {
        while (cursor.moveToNext()) {
          DownLoadInfo downInfo = new DownLoadInfo();
          downInfo.setDownID(cursor.getString(cursor.getColumnIndex("sMovieID")));
          downInfo.setDownName(cursor.getString(cursor.getColumnIndex("sMovieName")));
          downInfo.setDownImg(cursor.getString(cursor.getColumnIndex("sMovieImgUrl")));
          downInfo.setDownUrl(cursor.getString(cursor.getColumnIndex("sMovieUrl")));
          downInfo.setDownTotalSize(cursor.getLong(cursor.getColumnIndex("sMovieSize")));
          downInfo.setDownProgress(cursor.getLong(cursor.getColumnIndex("sMoviePlayProgress")));
          downInfo.setDownState(cursor.getInt(cursor.getColumnIndex("sMovieLoadState")));
          downInfo.setDownPosition(
              cursor.getInt(cursor.getColumnIndex(TableName.MoviePlayPosition)));
          downInfo.setDownTag(cursor.getString(cursor.getColumnIndex(TableName.MoviePlaySourKey)));
          String path = cursor.getString(cursor.getColumnIndex("sMovieLocalUrl"));
          downInfo.setDownPath(path);
          JSONArray array;
          try {
            array = new JSONArray(cursor.getString(cursor.getColumnIndex("sMoviePlayList")));
            ArrayList<String> list = new ArrayList<String>();
            for (int i = 0; i < array.length(); i++) list.add(array.getString(i));

            downInfo.setPlayList(list);
          } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
          }
          // 下载片段索引
          if (!TextUtils.isEmpty(path)) {
            try {
              File file = new File(path);
              if (file != null && file.isDirectory()) {
                downInfo.setDownIndex(
                    file.listFiles().length - 1 > 0 ? file.listFiles().length - 1 : 0);
              }
            } catch (Exception e) {
              // TODO: handle exception
            }
          }

          resultsInfos.add(downInfo);
        }
      }
    } catch (Exception e) {
      // TODO: handle exception
    } finally {
      closeCursor();
    }
    return resultsInfos;
  }
 // 文件合并
 @SuppressWarnings("resource")
 public synchronized boolean GetFileMerge(final DownLoadInfo downInfo) {
   String parent = HtmlUtils.getFileDir(downInfo);
   final List<String> list = new ArrayList<String>();
   File file = new File(parent);
   if (!file.exists()) return true;
   // 检索要合并的文件名,不合格的剔除
   File[] childFiles =
       file.listFiles(
           new FileFilter() {
             public boolean accept(File pathname) {
               return pathname.getName().startsWith(downInfo.getDownName())
                   && !pathname.getName().endsWith(".ts");
             }
           });
   if (childFiles != null) {
     // 文件片段多于一个才需要合并
     String filePath = null;
     for (File file1 : childFiles) {
       filePath = file1.getAbsolutePath();
       list.add(filePath);
     }
     if (list.size() == 1) {
       DBHelperDao.getDBHelperDaoInstace().updataMovieStoreLocal(downInfo.getDownUrl(), filePath);
     } else {
       DBHelperDao.getDBHelperDaoInstace()
           .updataMovieStoreState(downInfo.getDownUrl(), DownTask.TASK_FileMerge);
       downInfo.setDownState(DownTask.TASK_FileMerge);
       String mp4filepath = null;
       if (filePath.endsWith(".flv")) {
         mp4filepath =
             parent
                 + "/"
                 + downInfo.getDownName()
                 + String.format("%03d", downInfo.getDownPosition())
                 + ".flv";
       } else {
         mp4filepath =
             parent
                 + "/"
                 + downInfo.getDownName()
                 + String.format("%03d", downInfo.getDownPosition())
                 + ".mp4";
       }
       final String[] filearray = list.toArray(new String[list.size()]);
       Arrays.sort(filearray); // 排序
       try {
         mlibvlc.nativeFileMerge(mp4filepath, filearray, false);
         // GetFileMergeStart(list, mp4filepath);
         // 合并完文件在更新数据库信息
         FileInputStream stream = new FileInputStream(mp4filepath);
         if (stream != null) {
           long size = stream.available();
           downInfo.setDownTotalSize(size);
           downInfo.setDownLocal(downInfo.getDownUrl());
           DBHelperDao.getDBHelperDaoInstace().updataMovieStoreSize(downInfo.getDownUrl(), size);
           DBHelperDao.getDBHelperDaoInstace()
               .updataMovieStoreLocal(downInfo.getDownUrl(), mp4filepath);
           downInfo.setDownState(DownTask.TASK_FINISHED);
           DBHelperDao.getDBHelperDaoInstace()
               .updataMovieStoreState(downInfo.getDownUrl(), DownTask.TASK_FINISHED);
           // 合并完成 删除源文件
           new Thread(
                   new Runnable() {
                     @Override
                     public void run() {
                       // TODO Auto-generated method stub
                       for (String fileName : list) {
                         FileUtils.deleteFile(fileName);
                       }
                     }
                   })
               .start();
         }
       } catch (Exception e) {
         // TODO: handle exception
         e.printStackTrace();
         return false;
       }
     }
   }
   return true;
 }