/**
   * 在当前容器中,添加一页
   *
   * @param layer
   * @param isGone
   */
  private void addView(VDVideoViewLayer layer, boolean isGone) {
    RelativeLayout.LayoutParams params =
        new RelativeLayout.LayoutParams(
            android.view.ViewGroup.LayoutParams.MATCH_PARENT,
            android.view.ViewGroup.LayoutParams.MATCH_PARENT);
    if (isGone) {
      layer.setVisibility(View.GONE);
    } else {
      layer.setVisibility(View.VISIBLE);
    }

    if (layer != null) {
      addView(layer, params);
    }
  }
  /**
   * 从layerAttrs属性中加载相应的layer,支持两种格式: 1、直接使用@layer方式组装数组 2、使用@array方式做二维数组方式
   *
   * @param context
   * @param attrs
   */
  private void readLayerAttrs(int resID) {
    // 将resourceID指向的数组加载到内存中,一般为:
    // 复杂模式:
    // <array name="sv_videoview_layers">
    // <item>@array/sv_videoview_layer_adlayer</item>
    // <item>@array/sv_videoview_layer_controllayer</item>
    // </array>
    // 或者,简单模式:
    // <array name="sina_video_videoview_layers">
    // <item>@layout/ad_layer</item>
    // <item>@layout/control_layer</item>
    // </array>
    // 格式
    TypedArray layerTypedArr = getResources().obtainTypedArray(resID);
    LayoutInflater inflater =
        (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    // obtainStyledAttributes而来的数组使用getIndexCount方法取得数量
    // obtainTypedArray得到的数组使用length来得到数量
    int complexNoHorizonLayerNum = 0;
    for (int j = 0; j < layerTypedArr.length(); j++) {
      int resID2 = -1;
      resID2 = layerTypedArr.getResourceId(j, -1);

      if (resID2 == -1) {
        if (layerTypedArr != null) layerTypedArr.recycle();
        throw new IllegalArgumentException("resID2=-1");
      }

      String type = getResources().getResourceTypeName(resID2);
      if (type.equals("layout")) {
        // 简单模式
        VDVideoViewLayer layer = (VDVideoViewLayer) inflater.inflate(resID2, null);
        // 添加进入当前容器
        addView(layer, false);
        // 保存引用
        VDVideoViewLayerContext item = new VDVideoViewLayerContext();
        // 默认就是竖屏的
        layer.mIsVertical = true;
        item.addSimpleItem(layer);
        mVDVideoViewLayerData.addLayerContext(item);

      } else if (type.equals("array")) {
        // 复杂模式
        TypedArray orientationLayerArr = getResources().obtainTypedArray(resID2);
        if (orientationLayerArr.length() != 2) {
          if (layerTypedArr != null) layerTypedArr.recycle();
          if (orientationLayerArr != null) {
            orientationLayerArr.recycle();
          }
          throw new IllegalArgumentException("使用布局数组的情况下,一个数组只能容纳两个layer");
        }

        VDVideoViewLayerContext item = new VDVideoViewLayerContext();
        for (int k = 0; k < orientationLayerArr.length(); k++) {
          int resID3 = orientationLayerArr.getResourceId(k, -1);
          // 复杂模式下,无论何种情况,都要求有两个默认的layer在其中
          VDVideoViewLayer layer = new VDVideoViewLayer(mContext);
          if (resID3 == -1) {
            if (k == 1) {
              // 只允许只横不竖,那么只有竖屏,也就是k=0的时候,可以为-1,其他情况判错
              if (layerTypedArr != null) layerTypedArr.recycle();
              if (orientationLayerArr != null) {
                orientationLayerArr.recycle();
              }
              throw new IllegalArgumentException("resID3=-1");
            }
            complexNoHorizonLayerNum++;
          } else {
            layer = (VDVideoViewLayer) inflater.inflate(resID3, this, false);
          }
          boolean isGone = getIsFullMode(k);
          // 添加进入当前容器
          layer.mIsVertical = !isGone;
          addView(layer, isGone);
          // 保存引用
          item.addComplexItem(layer);
          // 判断是否是广告层
          if (layer instanceof VDVideoADLayer) {
            item.mIsInsertADLayer = true;
          }
        }
        mVDVideoViewLayerData.addLayerContext(item);
        orientationLayerArr.recycle();
      } else {
        throw new IllegalArgumentException("加入的类型错误");
      }
    }
    layerTypedArr.recycle();

    int simpleLayerNum = 0;
    int complexLayerNum = 0;

    for (VDVideoViewLayerContext value : mVDVideoViewLayerData.getLayerList()) {
      if (value.mComplexLayer.size() != 0) {
        complexLayerNum++;
      }
      if (value.mSimpleLayer != null) {
        simpleLayerNum++;
      }
    }
    if (simpleLayerNum == 0 && complexLayerNum == 0) {
      // 空的,直接返回错误
      throw new IllegalArgumentException("layout为空");
    } else if (simpleLayerNum != 0 && complexLayerNum != 0) {
      throw new IllegalArgumentException("简单模式、复杂模式只能二选一");
    } else if (simpleLayerNum != 0) {
      mVDVideoViewLayerData.setLayerType(VDVideoViewLayerContextData.LAYER_SIMPLE);
    } else if (complexLayerNum != 0) {
      if (complexNoHorizonLayerNum == complexLayerNum) {
        mVDVideoViewLayerData.setLayerType(VDVideoViewLayerContextData.LAYER_COMPLEX_NOVERTICAL);
      } else {
        mVDVideoViewLayerData.setLayerType(VDVideoViewLayerContextData.LAYER_COMPLEX_ALL);
      }
    }
    VDVideoViewController controller = VDVideoViewController.getInstance(mContext);
    if (controller != null) controller.setLayerContextData(mVDVideoViewLayerData);
  }