Exemplo n.º 1
0
  /**
   * get resource value by string-format via resourceId.
   *
   * @param resourceId
   * @param resourceTable
   * @param locale
   * @return
   */
  public static String getResourceByid(
      long resourceId, boolean isStyle, ResourceTable resourceTable, Locale locale) {
    //        An Android Resource id is a 32-bit integer. It comprises
    //        an 8-bit Package id [bits 24-31]
    //        an 8-bit Type id [bits 16-23]
    //        a 16-bit Entry index [bits 0-15]

    // android system styles.
    if (isStyle
        && (resourceId & AndroidConstants.STYLE_ID_START) == AndroidConstants.STYLE_ID_START) {
      return "@android:style/" + ResourceTable.styleMap.get((int) resourceId);
    }

    String str = "resourceId:0x" + Long.toHexString(resourceId);
    if (resourceTable == null) {
      return str;
    }

    short packageId = (short) (resourceId >> 24 & 0xff);
    short typeId = (short) ((resourceId >> 16) & 0xff);
    int entryIndex = (int) (resourceId & 0xffff);
    ResourcePackage resourcePackage = resourceTable.getPackage(packageId);
    if (resourcePackage == null) {
      return str;
    }
    TypeSpec typeSpec = resourcePackage.getTypeSpec(typeId);
    List<Type> types = resourcePackage.getTypes(typeId);
    if (typeSpec == null || types == null) {
      return str;
    }
    if (!typeSpec.exists(entryIndex)) {
      return str;
    }

    // read from type resource
    String result = null;
    String ref = null;
    int currentLevel = -1;
    for (Type type : types) {
      ResourceEntry resource = type.getResourceEntry(entryIndex);
      if (resource == null) {
        continue;
      }
      int level = locale.match(type.locale);
      if (level == 2) {
        ref = resource.key;
        result = resource.toString();
        break;
      } else if (level > currentLevel) {
        ref = resource.key;
        result = resource.toString();
      }
    }
    if (locale.equals(Locale.none) || result == null) {
      result = "@" + typeSpec.name + "/" + ref;
    }
    return result;
  }