@Override
  public void onDestroy() {
    mSuccessHandler.removeAllObserver();
    mFailedHandler.removeAllObserver();
    synchronized (mRequestList) {
      mRequestList.clear();
    }

    synchronized (mListenerList) {
      mListenerList.clear();
    }
  }
  public boolean postRequest(DownloadRequest request, DownloadListener l) {
    if (mRequestList == null
        || request == null
        || TextUtils.isEmpty(request.mDownloadUrl)
        || l == null) {
      return false;
    }

    DownloadListenerObj downloadObj = new DownloadListenerObj(request.mDownloadUrl, l);
    boolean contain = false;
    synchronized (mListenerList) {
      for (DownloadListenerObj obj : mListenerList) {
        if (downloadObj.equals(obj)) {
          contain = true;
        }
      }
      if (!contain) {
        mListenerList.add(downloadObj);
      }
    }

    // 检查是否已经下载过此request对应的文件
    String cachedFile = checkFromCache(request);
    if (!TextUtils.isEmpty(cachedFile)) {
      File file = new File(cachedFile);
      if (file.exists()) {
        DownloadResponse response = tryToHandleDownloadFile(cachedFile, request);
        if (response != null) {
          mSuccessHandler.notifyAll(-1, -1, response);
          handleProcess(request.mDownloadUrl, (int) file.length(), (int) file.length());
          if (l != null) {
            handleResponseByListener(DOWNLOAD_SUCCESS, request.mDownloadUrl, response, false);
          }
        }
        return true;
      }
    }

    return postRequest(request);
  }
  @Override
  public void run() {
    while (!bIsStop) {
      waitforUrl();

      if (DEBUG) {
        CommonUtilsConfig.LOGD_WITH_TIME("<<<<< [[run]] >>>>>");
      }
      synchronized (mRequestList) {
        if (mRequestList.size() == 0) {
          // bIsRunning = false;
          bIsStop = true;
          break;
        }
      }

      if (DEBUG) {
        CommonUtilsConfig.LOGD_WITH_TIME("<<<<< [[run]]  end synchronized (mRequestList) >>>>>");
      }

      DownloadRequest request = null;
      try {
        request = findRequestCanOperate(mRequestList);
        if (request == null) {
          bIsStop = true;
        }
        if (request != null && request.mStatus != DownloadRequest.STATUS_CANCEL) {
          if (DEBUG) {
            CommonUtilsConfig.LOGD(
                "================ <<" + Thread.currentThread().getName() + ">> working on : ");
            CommonUtilsConfig.LOGD("begin operate one request : " + request.toString());
            CommonUtilsConfig.LOGD("============================================");
          }

          String cacheFile =
              InternetUtils.requestBigResourceWithCache(
                  mContext, request.mDownloadUrl, request.getHeaders());

          if (DEBUG) {
            CommonUtilsConfig.LOGD("----- after get the cache file : " + cacheFile + " =======");
          }
          if (!TextUtils.isEmpty(cacheFile)) {
            // 将文件移动到下载完成的页面
            String filePath = mvFileToDownloadedDir(cacheFile, request.mFileExtension);
            if (!TextUtils.isEmpty(filePath)) {
              // notify success
              // 将文件移动到下载完成的页面
              DownloadResponse response = tryToHandleDownloadFile(filePath, request);
              if (response != null) {
                mSuccessHandler.notifyAll(-1, -1, response);
                handleResponseByListener(DOWNLOAD_SUCCESS, request.mDownloadUrl, response, false);
                removeRequest(request);
                continue;
              } else {
                handleResponseByListener(DOWNLOAD_FAILED, request.mDownloadUrl, request, false);
                mFailedHandler.notifyAll(-1, -1, request);
                continue;
              }
            } else {
              handleResponseByListener(DOWNLOAD_FAILED, request.mDownloadUrl, request, false);
              mFailedHandler.notifyAll(-1, -1, request);
              continue;
            }
          }

          if (request.getmStatus() != DownloadRequest.STATUS_CANCEL) {
            handleResponseByListener(DOWNLOAD_FAILED, request.mDownloadUrl, request, false);
            mFailedHandler.notifyAll(-1, -1, request);
          } else {
            handleResponseByListener(DOWNLOAD_FAILED, request.mDownloadUrl, request, true);
          }

          if (DEBUG) {
            CommonUtilsConfig.LOGD("success end operate one request : " + request);
          }
        }
      } catch (Exception e) {
        e.printStackTrace();
        if (DEBUG) {
          CommonUtilsConfig.LOGD("Exception : ", e);
          CommonUtilsConfig.LOGD("exception end operate one request : " + request);
          CommonUtilsConfig.LOGD(e.getStackTrace().toString());
        }

        if (request.getmStatus() != DownloadRequest.STATUS_CANCEL) {
          handleResponseByListener(DOWNLOAD_FAILED, request.mDownloadUrl, request, false);
          mFailedHandler.notifyAll(-1, -1, request);
        } else {
          handleResponseByListener(DOWNLOAD_FAILED, request.mDownloadUrl, request, true);
        }
      }

      removeRequest(request);
    }

    System.gc();
  }
  /**
   * 新提交的request会默认
   *
   * @param request
   * @return
   */
  public boolean postRequest(DownloadRequest request) {
    if (mRequestList == null || request == null || TextUtils.isEmpty(request.mDownloadUrl)) {
      return false;
    }

    if (DEBUG) {
      CommonUtilsConfig.LOGD_WITH_TIME(
          "<<<<< [[postRequest]] >>>>> ::::::::: " + request.toString());
    }

    // 检查是否已经下载过此request对应的文件
    String cachedFile = checkFromCache(request);
    if (!TextUtils.isEmpty(cachedFile)) {
      File file = new File(cachedFile);
      if (file.exists()) {
        DownloadResponse response = tryToHandleDownloadFile(cachedFile, request);
        if (response != null) {
          mSuccessHandler.notifyAll(-1, -1, response);
          handleProcess(request.mDownloadUrl, (int) file.length(), (int) file.length());
        }
        return true;
      }
    }

    synchronized (mRequestList) {
      boolean contain = false;
      for (DownloadRequest r : mRequestList) {
        if (r.mUrlHashCode == request.mUrlHashCode) {
          contain = true;
          break;
        }
      }
      if (!contain) {
        // mRequestList.add(request);
        // 将最新添加的任务放在下载队列的最前面
        if (mLastInFirstDownload) {
          mRequestList.add(0, request);
        } else {
          mRequestList.add(request);
        }

        if (DEBUG) {
          CommonUtilsConfig.LOGD(
              "postRequest, add request : " + request.toString() + " into download list");
        }
      }
      bIsStop = false;

      ThreadPoolSnapShot tss =
          CustomThreadPool.getInstance()
              .getSpecialThreadSnapShot(FileDownloader.class.getSimpleName());
      if (tss == null) {
        return false;
      } else {
        if (tss.taskCount < tss.ALLOWED_MAX_TAKS) {
          if (DEBUG) {
            CommonUtilsConfig.LOGD("entry into [[postRequest]] to start process ");
          }
          processWorks();
        }
      }
    }
    if (DEBUG) {
      CommonUtilsConfig.LOGD_WITH_TIME(
          "<<<<< [[postRequest]]  end synchronized (mRequestList) >>>>>");
    }

    synchronized (objLock) {
      if (bIsWaiting) {
        bIsWaiting = false;

        if (DEBUG) {
          CommonUtilsConfig.LOGD("try to notify download process begin");
        }
        objLock.notify();
      }
    }

    if (DEBUG) {
      CommonUtilsConfig.LOGD_WITH_TIME("<<<<< [[postRequest]]  end synchronized (objLock) >>>>>");
    }

    return true;
  }
 public void unRegisteFailedHandler(Handler handler) {
   mFailedHandler.unRegisteObserver(handler);
 }
 public void unRegisteSuccessHandler(Handler handler) {
   mSuccessHandler.unRegisteObserver(handler);
 }