@SuppressLint("InlinedApi")
  private void applyRules(View view, BoxElement elm) {
    int alignmentInBox = elm.getAlignmentInBox();
    int elmW = elm.getWidthCell();
    boolean bOverlapTB = false;
    boolean bOverlapLR = false;
    FrameLayout.LayoutParams lp = (LayoutParams) view.getLayoutParams();

    switch (alignmentInBox) {
      case DEFINE_BOX_ELEMENT_RULES_RIGHT_TOP:
        if (UIUtils.hasICS()) {
          lp.gravity = Gravity.END;
        } else {
          lp.gravity = Gravity.RIGHT | Gravity.TOP;
        }
        break;
      case DEFINE_BOX_ELEMENT_RULES_LEFT_BOTTOM:
        if (UIUtils.hasICS()) {
          lp.gravity = Gravity.START;
        } else {
          lp.gravity = Gravity.LEFT | Gravity.BOTTOM;
        }
        break;
      case DEFINE_BOX_ELEMENT_RULES_RIGHT_BOTTOM:
        if (UIUtils.hasICS()) {
          lp.gravity = Gravity.END;
        } else {
          lp.gravity = Gravity.RIGHT | Gravity.BOTTOM;
        }
        break;
      case DEFINE_BOX_ELEMENT_RULES_OVERLAP_TOP:
        bOverlapTB = true;
        lp.gravity = Gravity.TOP;
        break;
      case DEFINE_BOX_ELEMENT_RULES_OVERLAP_BOTTOM:
        bOverlapTB = true;
        lp.gravity = Gravity.BOTTOM;
        if (view instanceof TextView) {
          ((TextView) view).setGravity(Gravity.BOTTOM);
        }
        break;
      case DEFINE_BOX_ELEMENT_RULES_OVERLAP_LEFT:
        bOverlapLR = true;
        if (UIUtils.hasICS()) {
          lp.gravity = Gravity.START;
        }
        break;
      case DEFINE_BOX_ELEMENT_RULES_OVERLAP_RIGHT:
        bOverlapLR = true;
        if (UIUtils.hasICS()) {
          lp.gravity = Gravity.END;
        } else {
          lp.gravity = Gravity.RIGHT | Gravity.TOP;
        }
        break;
      case DEFINE_BOX_ELEMENT_RULES_STRETCH:
        lp.width = getBoxSupposeWidth();
        lp.height = getBoxSupposeHeight();
        break;
      case DEFINE_BOX_ELEMENT_RULES_LEFT_TOP:
      default:
        lp.gravity = Gravity.LEFT | Gravity.TOP;
        break;
    }

    if (bOverlapTB) {
      lp.width =
          mBoxCellSize * mNumberOfColumn + mMultiPaddingNum * (mNumberOfColumn - 1) * mGapWidth;
      if (mBoxLayoutIndex == 0) {
        lp.height =
            (mBoxCellSize * mNumberOfRow + mMultiPaddingNum * (mNumberOfRow - 1) * getPaddingTop())
                / 4;
      } else {
        lp.height =
            (int)
                ((2
                        * (mBoxCellSize * mNumberOfRow
                            + mMultiPaddingNum * (mNumberOfRow - 1) * getPaddingTop()))
                    / 5.5f);
      }
      // lp.height = (mBoxCellSize * mNumberOfRow + mMultiPaddingNum
      // * (mNumberOfRow - 1) * getPaddingTop()) / 3;
    } else if (bOverlapLR) {
      lp.width = mBoxCellSize * elmW + mMultiPaddingNum * (elmW - 1) * mGapWidth;
      lp.height =
          (mBoxCellSize * mNumberOfRow + mMultiPaddingNum * (mNumberOfRow - 1) * getPaddingTop());
    } else if (alignmentInBox != DEFINE_BOX_ELEMENT_RULES_STRETCH) {
      lp.width = mBoxCellSize * elmW + mMultiPaddingNum * (elmW - 1) * mGapWidth;
      lp.height = mBoxCellSize;
    }

    if ((view instanceof ImageView) && alignmentInBox != DEFINE_BOX_ELEMENT_RULES_STRETCH) {
      ((ImageView) view).setScaleType(ScaleType.FIT_XY);
      if (mNumberOfColumn > 1) {
        mNeedBackground = true;
      }
      // NOTE: image could be able to "eat" the padding in box.
      lp.width = mBoxCellSize * elmW + mMultiPaddingNum * (elmW) * mGapWidth;
    } else if (view instanceof TextView) {
      TextView tv = (TextView) view;
      if (TNPreferenceManager.isIgnoreTextGravity()) {
        tv.setGravity(Gravity.LEFT | Gravity.TOP);
      } else {
        tv.setGravity(lp.gravity);
      }
      // mWidthText = lp.width;
      // mHeightText = lp.height;
      tv.setPadding(mGapWidth, mGapWidth / 2, mGapWidth, mGapWidth);
    }

    view.setLayoutParams(lp);
    view.setVisibility(VISIBLE);
  }
  private void setBoxStory(BoxStory boxStory) {
    GKIMLog.lf(this.getContext(), 0, TAG + "=>setBoxStory.");
    if (boxStory != null) {
      int storyLayout = boxStory.getLayout();
      mNumberOfColumn =
          (mBoxLayoutIndex > 1)
              ? 1
              : getResources().getInteger(R.integer.section_video_page_max_cols);
      mNumberOfRow = (mBoxLayoutIndex > 0) ? 1 : 2;
      // mStoryBoxWidth = boxStory.getBoxStoryWidth();
      // mStoryBoxHeight = boxStory.getBoxStoryHeight();
      GKIMLog.l(1, TAG + " setBoxStory :" + boxStory.getStoryId());
      boolean hasImage = false;
      boolean hasTitle = false;
      boolean hasShortCont = false;
      mFavorited = boxStory.isFavourited();
      mVideoIcon = boxStory.getVideo() > 0 ? true : false;

      if (boxStory.hasElementType(BoxElement.BOXELEMENT_TYPE_IMAGE)) {
        mImage = (ImageView) this.findViewById(R.id.boxview_image);
        if (mImage != null) {
          BoxElement[] imageBoxes = boxStory.getBoxElementbyType(BoxElement.BOXELEMENT_TYPE_IMAGE);
          if (imageBoxes != null) {
            BoxElement elm = imageBoxes[0];
            // NOTE: start loading from ImageFetcher
            // use only 1 time when setBoxStory.
            ImageLoader.getInstance().displayImage(elm.getContent(), mImage);
            if (mBoxLayoutIndex == 0) {
              elm.setAlignmentInBox(7);
              elm.setWidthCell(2);
            } else if (mBoxLayoutIndex == 1) {
              elm.setAlignmentInBox(-1);
              elm.setWidthCell(1);
            } else {
              elm.setAlignmentInBox(7);
              elm.setWidthCell(1);
            }
            applyRules(mImage, elm);
            mImage.setScaleType(ImageView.ScaleType.CENTER_CROP);
          }
          hasImage = true;
        } else {
          GKIMLog.lf(this.getContext(), 4, TAG + "=>setBoxStory failed on find ImageView.");
        }
      } else {
        mNeedBackground = true;
        if (mBoxLayoutIndex == 1) {
          mNeedBackground = false;
          mImage = (ImageView) this.findViewById(R.id.boxview_image);
          mImage.setImageResource(R.drawable.ic_launcher);
          if (mImage != null) {
            BoxElement elm = new BoxElement();
            elm.setAlignmentInBox(-1);
            elm.setWidthCell(1);
            applyRules(mImage, elm);
            hasImage = true;
            mImage.setVisibility(INVISIBLE);
          }
        }
      }
      if (boxStory.hasElementType(BoxElement.BOXELEMENT_TYPE_SHORTCONTENT)) {
        mShortContent = (TextView) this.findViewById(R.id.boxview_shortcontent);
        if (mShortContent != null) {
          BoxElement[] shortTextElms =
              boxStory.getBoxElementbyType(BoxElement.BOXELEMENT_TYPE_SHORTCONTENT);
          if (shortTextElms != null) {
            BoxElement elm = shortTextElms[0];
            mShortContent.setTextColor(elm.getTextColor());
            // Note: tweak text size on xmls
            // mShortContent.setTextSize(elm.getTextSize());
            mShortContent.setTextSize(
                getResources().getDimensionPixelSize(R.dimen.section_box_title_textsize));
            mShortContent.setTypeface(
                UIUtils.getDefaultTypeFace(this.getContext().getApplicationContext(), 0),
                elm.getTextType());
            mShortContent.setText(elm.getContent());

            applyRules(mShortContent, elm);
            hasShortCont = true;
          }
        } else {
          GKIMLog.lf(this.getContext(), 4, TAG + "=>setBoxStory failed on find short TextView.");
        }
      }
      if (boxStory.hasElementType(BoxElement.BOXELEMENT_TYPE_TITLE)) {
        if (mTitle != null) {
          BoxElement[] titleElms = boxStory.getBoxElementbyType(BoxElement.BOXELEMENT_TYPE_TITLE);
          if (titleElms != null) {
            BoxElement elm = titleElms[0];
            mTitle.setTextSize(
                getResources().getDimensionPixelSize(R.dimen.section_boxvideo_title_textsize));
            mTitle.setTypeface(
                UIUtils.getDefaultTypeFace(this.getContext().getApplicationContext(), 0),
                elm.getTextType());
            // Fix: don't change color text when user read
            if (mReadBox) {
              mTitle.setTextColor(getResources().getColor(R.color.box_hasread_textcolor));
            } else {
              mTitle.setTextColor(elm.getTextColor());
            }
            if (mBoxLayoutIndex == 1) {
              elm.setWidthCell(1);
              elm.setAlignmentInBox(8);
            } else {
              elm.setAlignmentInBox(6);
            }
            applyRules(mLLTitle, elm);
            String title = elm.getContent();
            mTitle.setText(title);
            hasTitle = true;

            float textSize = TNPreferenceManager.getTextSizeBoxView();
            if (mBoxLayoutIndex == 0) {
              if (textSize > 0) {
                mTitle.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize * 1.25f);
              }
            } else if (mBoxLayoutIndex == 1) {
              if (textSize > 0) {
                mTitle.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize * 1.125f);
              }
            } else {
              if (textSize > 0) {
                mTitle.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
              }
            }

            // - getResources()
            // .getInteger(
            // R.integer.extract_txt_size_height

            adjustTextLength(mTitle);
            updatelayoutIconVideo();
          }
        } else {
          GKIMLog.lf(this.getContext(), 4, TAG + "=>setBoxStory failed on find title TextView.");
        }
      }

      mLLTitle.setBackgroundColor(boxStory.getBackground1Color());
      this.setBackgroundColor(boxStory.getBackground1Color());
      // finally update the boxView's size
      GKIMLog.lf(
          this.getContext(),
          5,
          TAG
              + "=>setBoxStory, type: "
              + storyLayout
              + ", row: "
              + mNumberOfRow
              + ", col: "
              + mNumberOfColumn
              + ", with "
              + (hasImage ? "imgage, " : "")
              + (hasTitle ? "title, " : "")
              + (hasShortCont ? "short content." : ""));
      setId(generateBoxIdFromSectionId(boxStory));
      this.setTag(boxStory);
    }
  }