Пример #1
0
 @Override
 public void onProcessWork(Runnable r) {
   if (r != null) {
     CustomThreadPool.getInstance()
         .excuteWithSpecialThread(FileDownloader.class.getSimpleName(), new TaskWrapper(r));
   }
 }
Пример #2
0
  private void setImageUrlLocal(final String localUrl, final boolean forceOriginLoad) {
    if (!TextUtils.isEmpty(localUrl)) {
      mUrl = localUrl;
      Bitmap bt = mImageCache.getResourceFromMem(mCategory, localUrl);
      if (bt == null) {
        CustomThreadPool.asyncWork(
            new Runnable() {
              @Override
              public void run() {
                File file = new File(localUrl);
                if (file.exists()) {
                  Bitmap bt = null;
                  if (!forceOriginLoad) {
                    bt =
                        mImageCache.getResource(
                            CustomImageCategory.THUMBNAIL_CATEGORY.toString(), localUrl);
                  }
                  if (bt == null) {
                    bt = mImageCache.getResource(mCategory, localUrl);
                  }

                  if (bt == null) {
                    mImageCache.putResource(mCategory, localUrl, localUrl);
                    bt = mImageCache.getResource(mCategory, localUrl);
                  }

                  Message msg = Message.obtain();
                  msg.what = LOCAL_FILE_IMAGE_LOAD;
                  msg.obj = bt;
                  mHandler.sendMessageDelayed(msg, 30);
                }

                Message msg = Message.obtain();
                msg.what = LOCAL_FILE_IMAGE_LOAD;
                msg.obj = null;
                mHandler.sendMessageDelayed(msg, 30);
              }
            });
      } else {
        mCurrentBtLoadSuccess = true;
        setImageBitmap(bt, false);
      }
    }
  }
  @Override
  public void getSublistFileItems(final FileItem file, final Context context) {

    CustomThreadPool.getInstance()
        .excute(
            new CustomThreadPool.TaskWrapper(
                new Runnable() {
                  @Override
                  public void run() {
                    if (file != null) {

                      List<FileItem> children =
                          getChildFiles(file.filePath + File.separator, context);

                      SublistEvent event = new SublistEvent();
                      event.list = children;
                      EventBus.getDefault().post(event);
                    }
                  }
                }));
  }
Пример #4
0
  /**
   * @param url
   * @param forceOriginLoad 标示支持同步加载,如果不支持同步加载的,就是fastload模式
   */
  private void setImageUrl(final String url, final boolean forceOriginLoad) {
    if (!TextUtils.isEmpty(url)) {
      mUrl = url;
      Bitmap bt = mImageCache.getResourceFromMem(mCategory, url);
      if (bt == null) {
        CustomThreadPool.asyncWork(
            new Runnable() {
              @Override
              public void run() {
                Bitmap bt = null;
                if (!forceOriginLoad) {
                  bt =
                      mImageCache.getResource(
                          CustomImageCategory.THUMBNAIL_CATEGORY.toString(), url);
                }
                if (bt == null) {
                  bt = mImageCache.getResource(mCategory, url);
                }

                // the f**k logic for category != UtilsConfig.IMAGE_CACHE_CATEGORY_RAW
                if (bt == null
                    && !TextUtils.isEmpty(mCategory)
                    && !mCategory.equals(UtilsConfig.IMAGE_CACHE_CATEGORY_RAW)) {
                  Bitmap rawBt = mImageCache.getResource(UtilsConfig.IMAGE_CACHE_CATEGORY_RAW, url);
                  if (rawBt != null && mBitmapOperationListener != null) {
                    bt = mBitmapOperationListener.onAfterBitmapDownload(rawBt);
                    if (bt != null) {
                      mImageCache.putResource(mCategory, url, bt);
                    }
                  }
                  mImageCache.releaseResource(UtilsConfig.IMAGE_CACHE_CATEGORY_RAW, url);
                }

                if (bt == null) {
                  registeHandler();
                  if (mCurrentDownloadRequest != null
                      && !url.equals(mCurrentDownloadRequest.getmDownloadUrl())) {
                    mCurrentDownloadRequest.cancelDownload();
                  }

                  if (mWebImageViewStatusListener != null) {
                    mHandler.post(
                        new Runnable() {
                          @Override
                          public void run() {
                            mWebImageViewStatusListener.onPreLoadImage(WebImageView.this, mUrl);
                          }
                        });
                  }

                  mCurrentDownloadRequest =
                      new ImageFetchRequest(
                          DownloadRequest.DOWNLOAD_TYPE.IMAGE,
                          url,
                          mCategory,
                          mBitmapOperationListener);
                  mImageDownloaer.postRequest(mCurrentDownloadRequest);
                } else {
                  Message msg = Message.obtain();
                  msg.what = LOCAL_LOAD_IMAGE_SUCCESS;
                  msg.obj = bt;
                  Bundle data = new Bundle();
                  data.putString(DATA_KEY_URL, url);
                  msg.setData(data);
                  mHandler.sendMessageDelayed(msg, 30);
                }
              }
            });

        if (mHasAnimation) {
          this.clearAnimation();
        }
        this.setImageDrawable(mDefaultSrc);
      } else {
        mCurrentBtLoadSuccess = true;
        setImageBitmap(bt, false);
      }
    }
  }
Пример #5
0
  /**
   * 新提交的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;
  }