Esempio n. 1
0
  /**
   * Get a usable cache directory (external if available, internal otherwise). . Check if media is
   * mounted or storage is built-in, if so, try and use external cache folder otherwise use internal
   * cache folder . If both of them can not meet the requirement, use the bigger one.
   *
   * @param context The context to use
   * @param uniqueName A unique folder name to append to the cache folder
   * @return The cache folder
   */
  public static CacheDirInfo getDiskCacheDir(
      Context context, String uniqueName, long requireSpace) {
    File sdPath = null;
    File internalPath = null;
    Long sdCardFree = 0L;

    boolean usingInternal = false;

    if (hasSDCardMounted()) {
      sdPath = getExternalCacheDir(context);
      if (!sdPath.exists()) {
        sdPath.mkdirs();
      }
      sdCardFree = getUsableSpace(sdPath);
    }

    CacheDirInfo cacheDirInfo = new CacheDirInfo();
    cacheDirInfo.requireSize = requireSpace;

    // sd card can not meet the requirement
    // try to use the build-in storage
    if (sdPath == null || sdCardFree < requireSpace) {
      internalPath = context.getCacheDir();
      long internalFree = getUsableSpace(internalPath);

      // both lower then requirement, choose the bigger one
      if (internalFree < requireSpace) {
        if (internalFree > sdCardFree) {
          usingInternal = true;
          cacheDirInfo.realSize = internalFree;
        } else {
          usingInternal = false;
          cacheDirInfo.realSize = sdCardFree;
        }
        cacheDirInfo.isNotEnough = true;
      } else {
        usingInternal = true;
        cacheDirInfo.realSize = requireSpace;
      }
    } else {
      usingInternal = false;
      cacheDirInfo.realSize = requireSpace;
    }

    cacheDirInfo.isInternal = usingInternal;
    if (usingInternal) {
      cacheDirInfo.path = new File(internalPath.getPath() + File.separator + uniqueName);
    } else {
      cacheDirInfo.path = new File(sdPath.getPath() + File.separator + uniqueName);
    }
    if (!cacheDirInfo.path.exists() && !cacheDirInfo.path.mkdirs()) {
      CLog.e("cube-cache", "can not create directory for: %s", cacheDirInfo.path);
    }
    return cacheDirInfo;
  }
Esempio n. 2
0
 public void setHandler(IScrollHeaderFrameHandler handler) {
   mIScrollHeaderFrameHandler = handler;
   CLog.d(LOG_TAG, "setHandler: %s", this);
 }