private void addOrRemoveSaveBoxStory(View fromView, StoryDetail storyDetail) {
    GKIMLog.lf(this, 0, TAG + "=>addOrRemoveSaveBoxStory");
    if (!mStoryId.equals(String.valueOf(storyDetail.getStoryid()))) {
      GKIMLog.lf(
          this,
          0,
          TAG
              + "=> not match current story, won't save. ("
              + mStoryId
              + ", "
              + storyDetail.getStoryid()
              + ").");
      return;
    }
    String currentStoryId = mStoryId;
    if (TNPreferenceManager.hasSavedStory(currentStoryId)) {
      // if (!TNPreferenceManager.checkLoggedIn()) {
      // UIUtils.showToast(this,
      // getResources().getString(R.string.request_for_login));
      // } else {
      TNPreferenceManager.deleteStory(currentStoryId);
      fromView.setSelected(false);
      GKIMLog.lf(this, 0, TAG + "=>removed story: " + currentStoryId);
      TNPreferenceManager.updateSaved(currentStoryId, 0);

      UIUtils.showToast(this, getResources().getString(R.string.storydetail_story_has_removed));

      // }
    } else {
      // if (!TNPreferenceManager.checkLoggedIn()) {
      // UIUtils.showToast(this,
      // getResources().getString(R.string.request_for_login));
      // } else {
      TNPreferenceManager.saveStory(storyDetail);
      fromView.setSelected(true);
      GKIMLog.lf(this, 0, TAG + "=>added story: " + currentStoryId);
      TNPreferenceManager.updateSaved(currentStoryId, 1);
      UIUtils.showToast(this, getResources().getString(R.string.storydetail_story_has_saved));
      // }
    }
  }
 @TargetApi(16)
 @SuppressWarnings("deprecation")
 public void setBoxBackground(Drawable background) {
   if (background == null) {
     background = TNPreferenceManager.getBackgroundDrawable1(getSectionRefId());
   }
   if (UIUtils.hasJellyBean()) {
     setBackground(background);
   } else {
     setBackgroundDrawable(background);
   }
 }
 @SuppressLint("InlinedApi")
 private void initBoxView() {
   mTabletVersion = getResources().getBoolean(R.bool.istablet);
   mGapWidth = TNPreferenceManager.getGapWidth();
   if (mGapWidth < 0) {
     mGapWidth = getResources().getInteger(R.integer.middlepadding);
   }
   mBoxCellSize = TNPreferenceManager.getCellWidthVideoHome();
   if (mBoxCellSize < 0) {
     mBoxCellSize = this.getContext().getResources().getInteger(R.integer.boxcellsize);
   }
   // Retrieve the drawable resource assigned to the
   // android.R.attr.selectableItemBackground
   // theme attribute from the current theme.
   if (UIUtils.hasHoneycomb()) {
     TypedArray a =
         getContext().obtainStyledAttributes(new int[] {android.R.attr.selectableItemBackground});
     mForegroundDrawable = a.getDrawable(0);
     a.recycle();
   } else {
     mForegroundDrawable = new ColorDrawable(Color.CYAN);
   }
   inflateBoxView();
 }
  protected void showTextModeDialog() {
    if (mTextModeDialog == null) {
      mTextModeDialog = new GUIStoryTextModeDialog(this);
      mTextModeDialog.setOnShareClickListener(mOnClickListener);
    }
    mTextModeDialog.setOnDismissListener(
        new OnDismissListener() {

          @Override
          public void onDismiss(DialogInterface dialog) {
            // TODO Auto-generated method stub
            FragmentManager fm = getSupportFragmentManager();
            VideoStoryDetailFragment frag =
                (VideoStoryDetailFragment)
                    fm.findFragmentByTag(getFragmentTag(mPager.getCurrentItem()));
            if (frag != null) {
              frag.reloadContentWebView();
            }
          }
        });
    mTextModeDialog.setToggleChecked(TNPreferenceManager.isNightMode());
    mTextModeDialog.show();
  }
  @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);
    }
  }
 @Override
 public boolean handleMessage(Message msg) {
   switch (msg.what) {
     case TNPreferenceManager.HANDLER_MSG_HAS_LOGGIN_CHANGED:
       GKIMLog.lf(null, 0, TAG + "=>login has changed.");
       // XXX: not correct on the UID from logout to login state.
       // boolean bLoggedIn = TNPreferenceManager.checkLoggedIn();
       if (mTabletVersion) {
         FragmentManager fm = getSupportFragmentManager();
         VideoStoryDetailFragment frag =
             (VideoStoryDetailFragment)
                 fm.findFragmentByTag(getFragmentTag(mPager.getCurrentItem()));
         // if (bLoggedIn) {
         frag.setCheckSave(TNPreferenceManager.hasSavedStory(mStoryId));
         // } else {
         // frag.setCheckSave(false);
         // }
       } else {
         // if (bLoggedIn) {
         GKIMLog.l(1, TAG + " handleMessage call setSavedStory");
         setSavedStory(TNPreferenceManager.hasSavedStory(mStoryId));
         // } else {
         // setSavedStory(false);
         // }
       }
       hideGUIListMenu();
       return true;
     case TNPreferenceManager.HANDLER_MSG_HAS_STORYDETAIL_LOAD_COMPLETED:
       // Story activity will receive this event when a
       // StoryDetail is completely loaded in fragment.
       StoryDetail sd = (StoryDetail) msg.obj;
       if (sd == null || TextUtils.isEmpty(sd.getSectionid())) {
         return false;
       }
       String sectionid = sd.getSectionid();
       String storyid = String.valueOf(sd.getStoryid());
       try {
         if (mStoryId == null || storyid.equals(mStoryId)) {
           GKIMLog.lf(
               VideoStoryDetailFragmentActivity.this,
               0,
               TAG
                   + "=>myOnPageSelectedLogic, update section code from current story: "
                   + storyid
                   + ", section: "
                   + sectionid);
           TNPreferenceManager.updateCurrentStandingSectionId(sectionid);
         }
       } catch (Exception e) {
         GKIMLog.lf(
             VideoStoryDetailFragmentActivity.this,
             0,
             TAG
                 + "=> failed to get current section code from story: "
                 + sectionid
                 + ", "
                 + e.getMessage());
       }
       break;
     default:
       break;
   }
   return false;
 }
 protected void changeTextMode(boolean bChecked) {
   TNPreferenceManager.setTextMode(bChecked);
   updateFragmentTextmode(bChecked);
 }
  private void myOnPageSelectedLogic(int position) {
    // if(TNPreferenceManager.isConnectionAvailable()) {
    mStoryId = mPagerAdapter.getPageStoryId(position);
    GKIMLog.lf(null, 1, TAG + "=>onPageSelected: " + position + " story: " + mStoryId);
    TNPreferenceManager.putReadStory(mStoryId);

    String storyName = "";
    try {
      FragmentManager fm = getSupportFragmentManager();
      VideoStoryDetailFragment frag =
          (VideoStoryDetailFragment) fm.findFragmentByTag(getFragmentTag(mPager.getCurrentItem()));
      if (frag != null) {
        StoryDetail sd = frag.getStoryDetail();
        String sectionid = "";
        if (sd != null) {
          sectionid = sd.getSectionid();
          storyName = sd.getStorytitle();
        }
        GKIMLog.lf(this, 0, TAG + "=>myOnPageSelectedLogic, saving section code: " + sectionid);
        TNPreferenceManager.updateCurrentStandingSectionId(sectionid);
      }
    } catch (Exception e) {
      GKIMLog.lf(
          this,
          0,
          TAG
              + "=> failed to get current section code from story: "
              + mStoryId
              + ", "
              + e.getMessage());
    }
    HashMap<String, String> map = new HashMap<String, String>();
    map.put(TNPreferenceManager.EVENT_KEY_STORY_NAME, storyName);
    map.put(TNPreferenceManager.EVENT_KEY_STORY_ID, mStoryId);
    Tracking.sendEvent(TNPreferenceManager.EVENT_STORY_VIEW, map);

    updateFragmentTextmode(TNPreferenceManager.isNightMode());
    updateFragmentTextZoommode();
    if (!mTabletVersion) {
      int count = 0;
      if (mHashStoryCommentCount != null
          && mHashStoryCommentCount.containsKey(mStoryId)
          && mGuiFooter != null) {
        count = mHashStoryCommentCount.get(mStoryId);
      }
      mGuiFooter.setCommentCountView(count);
      count = 0;
      if (mHashStoryFBLikeCount != null && mHashStoryFBLikeCount.containsKey(mStoryId)) {
        count = mHashStoryFBLikeCount.get(mStoryId);
      }
      mGuiFooter.setFbLikeCountView(count);
    }
    // boolean bLoggedIn = TNPreferenceManager.checkLoggedIn();
    // if (bLoggedIn) {
    GKIMLog.l(1, TAG + " myOnPageSelectedLogic call setSavedStory");
    setSavedStory(TNPreferenceManager.hasSavedStory(mStoryId));
    // } else {
    // setSavedStory(false);
    // }

    if (mGuiFooter != null && !mTabletVersion) {
      int mFbLikeCount =
          getSharedPreferences(mStoryId + "_", Context.MODE_PRIVATE).getInt(mStoryId + "_", 0);
      boolean is = getSharedPreferences(mStoryId, Context.MODE_PRIVATE).getBoolean(mStoryId, false);
      TextView tvlikefb = (TextView) mGuiFooter.findViewById(R.id.tv_storydetail_fblike_count);
      tvlikefb.setText(mFbLikeCount + "");
      if (is) {
        tvlikefb.setBackgroundResource(R.drawable.ic_storyfooter_fblike_over);
      } else {
        tvlikefb.setBackgroundResource(R.drawable.ic_storyfooter_fblike);
      }
    }
  }
  @Override
  protected void onCreate(Bundle arg0) {
    GKIMLog.lf(this, 1, TAG + "=>onCreate.");
    super.onCreate(arg0);
    uiHelper = new UiLifecycleHelper(this, callback);
    uiHelper.onCreate(arg0);
    if (TNPreferenceManager.getApplicationContext() == null) {
      TNPreferenceManager.setContext(this);
    }
    mTabletVersion = (getResources().getBoolean(R.bool.istablet));
    if (getResources().getBoolean(R.bool.portrait_only)) {
      GKIMLog.lf(this, 0, TAG + "=>Not support for rotation");
      setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    }

    Intent intent = this.getIntent();
    if (intent != null) {
      // should pass to VideoStoryDetailFragment.
      String action = intent.getAction();
      if (ACTION_STORY_VIEW.equalsIgnoreCase(action)) {
        // start story from PNS
        Bundle extra = intent.getExtras();
        if (extra != null && extra.containsKey(DATA_KEY_STORY_JSON)) {
          String strjson = extra.getString(DATA_KEY_STORY_JSON);
          try {
            JSONObject jo = new JSONObject(strjson);
            if (jo.has(DATA_KEY_STORY_ID)) {
              mStoryId = jo.getString(DATA_KEY_STORY_ID);
              // mStoryChecked = false;
              mOpenedFromSectionId = TNPreferenceManager.EXTRAVALUE_SECTION_HOME;
            }
          } catch (Exception e) {
            GKIMLog.lf(
                null,
                4,
                TAG + "=>Exception from parsing json string: " + strjson + ": " + e.getMessage());
          }
        }
      } else if (intent.hasExtra(TNPreferenceManager.EXTRAKEY_IS_STORY)
          && intent.hasExtra(TNPreferenceManager.EXTRAKEY_STORYID)) {
        mStoryId = intent.getStringExtra(TNPreferenceManager.EXTRAKEY_STORYID);
        // mStoryChecked = intent.getBooleanExtra(
        // TNPreferenceManager.EXTRAKEY_IS_STORY_CHECKED, false);
        mOpenedFromSectionId =
            intent.getStringExtra(TNPreferenceManager.EXTRAKEY_OPEN_STORY_FROM_SECTION);
        mHasFromPNS =
            intent.getBooleanExtra(TNPreferenceManager.EXTRAKEY_OPEN_STORY_FROM_PNS, false);
        String storyIds = intent.getStringExtra(TNPreferenceManager.EXTRAKEY_STORYIDS_FROM_SECTION);
        if (storyIds != null && storyIds.length() > 0) {
          mListStoryIds = storyIds.split(",");
          // mNumberOfStories = mListStoryIds.length;
        }
        mStoryType = intent.getStringExtra(TNPreferenceManager.EXTRAKEY_STORY_FROM_VIDEO_SECTION);
      }
    }

    initLayout();

    GKIMLog.lf(this, 0, TAG + "=>onCreate: " + mStoryId);
    mHashStoryCommentCount = new Hashtable<String, Integer>();
    mHashStoryFBLikeCount = new Hashtable<String, Integer>();
    if (DEBUG) {
      UIUtils.showToast(this, "starting Story: " + mStoryId);
    }
    if (mStoryId == null || mStoryId.length() <= 0) {
      this.finish();
    }
    if (mListStoryIds == null || mListStoryIds.length <= 0) {
      mListStoryIds = new String[] {mStoryId};
    }
  }