Demo entry 6042082

容器类

   

Submitted by anonymous on Sep 21, 2016 at 10:56
Language: Java. Code size: 6.6 kB.

**
 * hybrid 容器类
 */
public class H5CacheManager {
  private static H5CacheManager instance;

  private Context context;

  public static H5CacheManager getInstance() {
    if (instance == null) {
      synchronized (H5CacheManager.class) {
        if (instance == null) {
          instance = new H5CacheManager();
        }
      }
    }
    return instance;
  }

  private H5CacheManager() {
    context = WeiboApplication.instance.getApplicationContext();
  }

  /**
   * 加载本地资源
   *
   * @param url url
   * @return 本地资源
   */
  public LocalWebResource loadWebResource(String url) {
    LocalWebResource localWebResource = null;

    CacheType cacheType = getCacheType(url);
    if (H5Utils.isUpdatePackage()) {
      //如何使安装包正则更新 则不加载本地资源
      localWebResource = null;
    } else {
      if (cacheType == CacheType.nocache) {
        localWebResource = null;
      } else if (cacheType == CacheType.h5app) {
        String localAppName = isLocalH5App(url);

        if (!TextUtils.isEmpty(localAppName)) {
          String localH5App = H5App.getInstance().getLocalH5App();
          if (!(localAppName.equals(localH5App))) {
            H5App.getInstance().setLocalH5App(localAppName);
            H5App.getInstance().loadH5AppCacheInfo(context, localAppName);
          }
        }
        localWebResource = loadResource(url);
      }
    }

    return localWebResource;
  }

  /**
   * 加载静态资源
   *
   * @param url 线上路径
   * @return 本地资源
   */
  private LocalWebResource loadResource(String url) {
    LocalWebResource localWebResource = null;

    String finalPath;
    String processUrl;
    if (TextUtils.isEmpty(H5App.getInstance().getLocalH5App())) {
      finalPath = null;
    } else {
      int questionMarkLocation = url.indexOf("?");
      if (questionMarkLocation > 0) {
        processUrl = url.substring(0, questionMarkLocation);
      } else {
        processUrl = url;
      }

      int poundKeyLocation = processUrl.indexOf("#");
      if (poundKeyLocation > 0) {
        processUrl = processUrl.substring(0, poundKeyLocation);
      }

      //processUrl = H5Utils.removeUrlHost(processUrl);
      if (TextUtils.isEmpty(processUrl)) {
        return null;
      } else if (H5App.getInstance().getVirtualDomain() == null || !H5App.getInstance()
          .getVirtualDomain()
          .containsKey(processUrl)) {
        return null;
      } else {
        processUrl = H5App.getInstance().getVirtualDomain().get(processUrl);
        processUrl = String.format("%s/%s/%s", H5App.getInstance().getH5AppDir(context),
            H5App.getInstance().getLocalH5App(), processUrl);
        finalPath = processUrl;
      }
    }
    if (TextUtils.isEmpty(finalPath)) {
      localWebResource = null;
    } else if (H5Utils.isUpdatePackage()) {
      localWebResource = null;
    } else {
      File file = new File(finalPath);
      if (file.exists()) {
        try {
          localWebResource = new LocalWebResource(H5Utils.getMimeType(finalPath), "UTF-8",
              new FileInputStream(file));
        } catch (FileNotFoundException e) {
          e.printStackTrace();
        }
      }
    }
    return localWebResource;
  }

  ///**
  // * 获取文件类型
  // * @param str 处理过的url
  // * @return 文件类型
  // */
  //private static String getFileType(String str) {
  //  String toLowerCas[e;
  //  int lastIndexOf = str.lastIndexOf("?");
  //  if (lastIndexOf > 0 && str.charAt(lastIndexOf - 1) != '?') {
  //    str = str.substring(0, lastIndexOf);
  //  }
  //  lastIndexOf = str.lastIndexOf(".");
  //  if (lastIndexOf > 0) {
  //    toLowerCase = str.substring(lastIndexOf + 1, str.length()).toLowerCase();
  //  } else {
  //    toLowerCase = "";
  //  }
  //  return toLowerCase;
  //}

  /**
   * 获取cache类型
   * @param url
   * @return
   */
  private CacheType getCacheType(String url) {
    CacheType cacheType = CacheType.nocache;
    if (url.startsWith("http")) {
      if (!TextUtils.isEmpty(isLocalH5App(url))) {
        return CacheType.h5app;
      }
    }
    return cacheType;
  }

  /**
   * 根据url返回业务名
   *
   * @param url 指定url
   * @return h5appName
   */
  private String isLocalH5App(String url) {
    String appName = checkH5CacheValid(url);
    if (appName != null) {
      return appName;
    }
    return "";
  }

  private String checkH5CacheValid(String url) {
    String listJson = H5App.getInstance().getH5CacheListJson(context);
    if (!TextUtils.isEmpty(listJson)) {
      try {
        HybridCacheList hybridCacheList = new HybridCacheList(listJson);
        H5PackageMap h5PackageMap = generateH5PackageMap(hybridCacheList);
        Map<String, String> h5PackageList = h5PackageMap.getH5CacheList();
        return getH5CacheValue(url, h5PackageList);
      } catch (WeiboParseException e) {
        e.printStackTrace();
        return null;
      }
    }
    return null;
  }

  /**
   * 从url与业务对应集合中取出相应的业务
   *
   * @param url 访问url
   * @param h5PackageList url 业务 键值对
   * @return 业务名
   */
  private static String getH5CacheValue(String url, Map<String, String> h5PackageList) {
    try {
      if (h5PackageList != null && h5PackageList.size() > 0) {
        for (String urlKey : h5PackageList.keySet()) {
          String appName = h5PackageList.get(urlKey);
          if (WeiboBrowserUtils.patternCheck(url, H5Utils.convertLuaRegex2Java(urlKey))) {
            return appName;
          }
        }
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
    return null;
  }

  /**
   * 通过解析服务器返回json,并生成用于url与业务对应的map
   */
  private H5PackageMap generateH5PackageMap(HybridCacheList cacheList) {
    ArrayList<HybridCache> hybridCaches = cacheList.getHybridCaches();
    HashMap<String, String> h5CacheMap = new HashMap<>();
    if (hybridCaches != null && hybridCaches.size() > 0) {
      for (HybridCache cache : hybridCaches) {
        if (cache != null) {

          ArrayList<String> urlPrefixList = cache.getUrlPrefixList();
          String cacheName = cache.getCacheName();
          if (urlPrefixList != null && urlPrefixList.size() > 0) {
            for (String urlPrefix : urlPrefixList) {
              h5CacheMap.put(urlPrefix, cacheName);
            }
          }
        }
      }
    }

    H5PackageMap h5PackageMap = new H5PackageMap();
    h5PackageMap.setH5CacheList(h5CacheMap);

    return h5PackageMap;
  }

  private enum CacheType {
    nocache,
    h5app,
  }
}

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).