/*
   * changes the like on the passed post
   */
  private void togglePostLike() {
    if (!isAdded() || !hasPost() || !NetworkUtils.checkConnection(getActivity())) {
      return;
    }

    boolean isAskingToLike = !mPost.isLikedByCurrentUser;
    ReaderIconCountView likeCount = (ReaderIconCountView) getView().findViewById(R.id.count_likes);
    likeCount.setSelected(isAskingToLike);
    ReaderAnim.animateLikeButton(likeCount.getImageView(), isAskingToLike);

    if (!ReaderPostActions.performLikeAction(mPost, isAskingToLike)) {
      likeCount.setSelected(!isAskingToLike);
      return;
    }

    // get the post again since it has changed, then refresh to show changes
    mPost = ReaderPostTable.getPost(mBlogId, mPostId, false);
    refreshLikes();
    refreshIconCounts();

    if (isAskingToLike) {
      AnalyticsUtils.trackWithBlogDetails(AnalyticsTracker.Stat.READER_ARTICLE_LIKED, mBlogId);
    } else {
      AnalyticsUtils.trackWithBlogDetails(AnalyticsTracker.Stat.READER_ARTICLE_UNLIKED, mBlogId);
    }
  }
  public static boolean followBlogById(
      final long blogId, final boolean isAskingToFollow, final ActionListener actionListener) {
    if (blogId == 0) {
      if (actionListener != null) {
        actionListener.onActionResult(false);
      }
      return false;
    }

    ReaderBlogTable.setIsFollowedBlogId(blogId, isAskingToFollow);
    ReaderPostTable.setFollowStatusForPostsInBlog(blogId, isAskingToFollow);

    if (isAskingToFollow) {
      AnalyticsUtils.trackWithBlogDetails(AnalyticsTracker.Stat.READER_BLOG_FOLLOWED, blogId);
    } else {
      AnalyticsUtils.trackWithBlogDetails(AnalyticsTracker.Stat.READER_BLOG_UNFOLLOWED, blogId);
    }

    final String actionName = (isAskingToFollow ? "follow" : "unfollow");
    final String path =
        "sites/" + blogId + "/follows/" + (isAskingToFollow ? "new" : "mine/delete");

    com.wordpress.rest.RestRequest.Listener listener =
        new RestRequest.Listener() {
          @Override
          public void onResponse(JSONObject jsonObject) {
            boolean success = isFollowActionSuccessful(jsonObject, isAskingToFollow);
            if (success) {
              AppLog.d(T.READER, "blog " + actionName + " succeeded");
            } else {
              AppLog.w(
                  T.READER,
                  "blog " + actionName + " failed - " + jsonToString(jsonObject) + " - " + path);
              localRevertFollowBlogId(blogId, isAskingToFollow);
            }
            if (actionListener != null) {
              actionListener.onActionResult(success);
            }
          }
        };
    RestRequest.ErrorListener errorListener =
        new RestRequest.ErrorListener() {
          @Override
          public void onErrorResponse(VolleyError volleyError) {
            AppLog.w(T.READER, "blog " + actionName + " failed with error");
            AppLog.e(T.READER, volleyError);
            localRevertFollowBlogId(blogId, isAskingToFollow);
            if (actionListener != null) {
              actionListener.onActionResult(false);
            }
          }
        };
    WordPress.getRestClientUtilsV1_1().post(path, listener, errorListener);

    return true;
  }
    private void trackUploadAnalytics() {
      mPost.getStatusEnum();

      boolean isFirstTimePublishing = false;
      if (mPost.hasChangedFromDraftToPublished()
          || (mPost.isLocalDraft() && mPost.getStatusEnum() == PostStatus.PUBLISHED)) {
        isFirstTimePublishing = true;
      }

      if (isFirstTimePublishing) {
        // Calculate the words count
        Map<String, Object> properties = new HashMap<String, Object>();
        properties.put("word_count", AnalyticsUtils.getWordCount(mPost.getContent()));

        if (mHasImage) {
          properties.put("with_photos", true);
        }
        if (mHasVideo) {
          properties.put("with_videos", true);
        }
        if (mHasCategory) {
          properties.put("with_categories", true);
        }
        if (!TextUtils.isEmpty(mPost.getKeywords())) {
          properties.put("with_tags", true);
        }

        AnalyticsTracker.track(AnalyticsTracker.Stat.EDITOR_PUBLISHED_POST, properties);
      }
    }
    /**
     * This method is called when: 1. the app starts (but it's not opened by a service or a
     * broadcast receiver, i.e. an activity is resumed) 2. the app was in background and is now
     * foreground
     */
    public void onAppComesFromBackground() {
      AppLog.i(T.UTILS, "App comes from background");
      ConnectionChangeReceiver.setEnabled(WordPress.this, true);
      AnalyticsUtils.refreshMetadata();
      mApplicationOpenedDate = new Date();
      AnalyticsTracker.track(AnalyticsTracker.Stat.APPLICATION_OPENED);
      if (NetworkUtils.isNetworkAvailable(mContext)) {
        // Rate limited PN Token Update
        updatePushNotificationTokenIfNotLimited();

        // Rate limited WPCom blog list Update
        sUpdateWordPressComBlogList.runIfNotLimited();

        // Rate limited blog options Update
        sUpdateCurrentBlogOption.runIfNotLimited();
      }
      sDeleteExpiredStats.runIfNotLimited();
    }
  private void initAnalytics(final long elapsedTimeOnCreate) {
    AnalyticsTracker.registerTracker(
        new AnalyticsTrackerMixpanel(getContext(), BuildConfig.MIXPANEL_TOKEN));
    AnalyticsTracker.registerTracker(new AnalyticsTrackerNosara(getContext()));
    AnalyticsTracker.init(getContext());
    AnalyticsUtils.refreshMetadata();

    // Track app upgrade
    int versionCode = PackageUtils.getVersionCode(getContext());
    int oldVersionCode = AppPrefs.getLastAppVersionCode();
    if (oldVersionCode != 0 && oldVersionCode < versionCode) {
      Map<String, Long> properties = new HashMap<String, Long>(1);
      properties.put("elapsed_time_on_create", elapsedTimeOnCreate);
      // app upgraded
      AnalyticsTracker.track(AnalyticsTracker.Stat.APPLICATION_UPGRADED, properties);
    }
    AppPrefs.setLastAppVersionCode(versionCode);
  }