@Override
    protected void onPostExecute(Boolean postUploadedSuccessfully) {
      if (postUploadedSuccessfully) {
        WordPress.wpDB.deleteMediaFilesForPost(mPost);
        mPostUploadNotifier.cancelNotification();
        mPostUploadNotifier.updateNotificationSuccess(mPost, mLatestIcon);
      } else {
        mPostUploadNotifier.updateNotificationError(
            mErrorMessage, mIsMediaError, mPost.isPage(), mErrorUnavailableVideoPress);
      }

      postUploaded();
      EventBus.getDefault()
          .post(new PostUploadEnded(postUploadedSuccessfully, mPost.getLocalTableBlogId()));
    }
 @Override
 protected void onCancelled(Boolean aBoolean) {
   super.onCancelled(aBoolean);
   // mPostUploadNotifier and mPost can be null if onCancelled is called before doInBackground
   if (mPostUploadNotifier != null && mPost != null) {
     mPostUploadNotifier.updateNotificationError(
         mErrorMessage, mIsMediaError, mPost.isPage(), mErrorUnavailableVideoPress);
   }
 }
    /** Finds media in post content, uploads them, and returns the HTML to insert in the post */
    private String processPostMedia(String postContent) {
      String imageTagsPattern = "<img[^>]+android-uri\\s*=\\s*['\"]([^'\"]+)['\"][^>]*>";
      Pattern pattern = Pattern.compile(imageTagsPattern);
      Matcher matcher = pattern.matcher(postContent);

      int totalMediaItems = 0;
      List<String> imageTags = new ArrayList<String>();
      while (matcher.find()) {
        imageTags.add(matcher.group());
        totalMediaItems++;
      }

      mPostUploadNotifier.setTotalMediaItems(totalMediaItems);

      int mediaItemCount = 0;
      for (String tag : imageTags) {
        Pattern p = Pattern.compile("android-uri=\"([^\"]+)\"");
        Matcher m = p.matcher(tag);
        if (m.find()) {
          String imageUri = m.group(1);
          if (!imageUri.equals("")) {
            MediaFile mediaFile = WordPress.wpDB.getMediaFile(imageUri, mPost);
            if (mediaFile != null) {
              // Get image thumbnail for notification icon
              Bitmap imageIcon =
                  ImageUtils.getWPImageSpanThumbnailFromFilePath(
                      mContext, imageUri, DisplayUtils.dpToPx(mContext, 128));

              // Crop the thumbnail to be squared in the center
              if (imageIcon != null) {
                int squaredSize = DisplayUtils.dpToPx(mContext, 64);
                imageIcon = ThumbnailUtils.extractThumbnail(imageIcon, squaredSize, squaredSize);
                mLatestIcon = imageIcon;
              }

              mediaItemCount++;
              mPostUploadNotifier.setCurrentMediaItem(mediaItemCount);
              mPostUploadNotifier.updateNotificationIcon(imageIcon);

              String mediaUploadOutput;
              if (mediaFile.isVideo()) {
                mHasVideo = true;
                mediaUploadOutput = uploadVideo(mediaFile);
              } else {
                mHasImage = true;
                mediaUploadOutput = uploadImage(mediaFile);
              }

              if (mediaUploadOutput != null) {
                postContent = postContent.replace(tag, mediaUploadOutput);
              } else {
                postContent = postContent.replace(tag, "");
                mIsMediaError = true;
              }
            }
          }
        }
      }

      return postContent;
    }
    @Override
    protected Boolean doInBackground(Post... posts) {
      mErrorUnavailableVideoPress = false;
      mPost = posts[0];

      mPostUploadNotifier = new PostUploadNotifier(mPost);
      String postTitle =
          TextUtils.isEmpty(mPost.getTitle()) ? getString(R.string.untitled) : mPost.getTitle();
      String uploadingPostTitle = String.format(getString(R.string.posting_post), postTitle);
      String uploadingPostMessage =
          String.format(
              getString(R.string.sending_content),
              mPost.isPage()
                  ? getString(R.string.page).toLowerCase()
                  : getString(R.string.post).toLowerCase());
      mPostUploadNotifier.updateNotificationMessage(uploadingPostTitle, uploadingPostMessage);

      mBlog = WordPress.wpDB.instantiateBlogByLocalId(mPost.getLocalTableBlogId());
      if (mBlog == null) {
        mErrorMessage = mContext.getString(R.string.blog_not_found);
        return false;
      }

      // Create the XML-RPC client
      mClient =
          XMLRPCFactory.instantiate(mBlog.getUri(), mBlog.getHttpuser(), mBlog.getHttppassword());

      if (TextUtils.isEmpty(mPost.getPostStatus())) {
        mPost.setPostStatus(PostStatus.toString(PostStatus.PUBLISHED));
      }

      String descriptionContent = processPostMedia(mPost.getDescription());

      String moreContent = "";
      if (!TextUtils.isEmpty(mPost.getMoreText())) {
        moreContent = processPostMedia(mPost.getMoreText());
      }

      mPostUploadNotifier.updateNotificationMessage(uploadingPostTitle, uploadingPostMessage);

      // If media file upload failed, let's stop here and prompt the user
      if (mIsMediaError) {
        return false;
      }

      JSONArray categoriesJsonArray = mPost.getJSONCategories();
      String[] postCategories = null;
      if (categoriesJsonArray != null) {
        if (categoriesJsonArray.length() > 0) {
          mHasCategory = true;
        }

        postCategories = new String[categoriesJsonArray.length()];
        for (int i = 0; i < categoriesJsonArray.length(); i++) {
          try {
            postCategories[i] = TextUtils.htmlEncode(categoriesJsonArray.getString(i));
          } catch (JSONException e) {
            AppLog.e(T.POSTS, e);
          }
        }
      }

      Map<String, Object> contentStruct = new HashMap<String, Object>();

      if (!mPost.isPage() && mPost.isLocalDraft()) {
        // add the tagline
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mContext);

        if (prefs.getBoolean(getString(R.string.pref_key_post_sig_enabled), false)) {
          String tagline = prefs.getString(getString(R.string.pref_key_post_sig), "");
          if (!TextUtils.isEmpty(tagline)) {
            String tag = "\n\n<span class=\"post_sig\">" + tagline + "</span>\n\n";
            if (TextUtils.isEmpty(moreContent)) descriptionContent += tag;
            else moreContent += tag;
          }
        }
      }

      // Post format
      if (!mPost.isPage()) {
        if (!TextUtils.isEmpty(mPost.getPostFormat())) {
          contentStruct.put("wp_post_format", mPost.getPostFormat());
        }
      }

      contentStruct.put("post_type", (mPost.isPage()) ? "page" : "post");
      contentStruct.put("title", mPost.getTitle());
      long pubDate = mPost.getDate_created_gmt();
      if (pubDate != 0) {
        Date date_created_gmt = new Date(pubDate);
        contentStruct.put("date_created_gmt", date_created_gmt);
        Date dateCreated = new Date(pubDate + (date_created_gmt.getTimezoneOffset() * 60000));
        contentStruct.put("dateCreated", dateCreated);
      }

      if (!TextUtils.isEmpty(moreContent)) {
        descriptionContent = descriptionContent.trim() + "<!--more-->" + moreContent;
        mPost.setMoreText("");
      }

      // get rid of the p and br tags that the editor adds.
      if (mPost.isLocalDraft()) {
        descriptionContent =
            descriptionContent.replace("<p>", "").replace("</p>", "\n").replace("<br>", "");
      }

      // gets rid of the weird character android inserts after images
      descriptionContent = descriptionContent.replaceAll("\uFFFC", "");

      contentStruct.put("description", descriptionContent);
      if (!mPost.isPage()) {
        contentStruct.put("mt_keywords", mPost.getKeywords());

        if (postCategories != null && postCategories.length > 0) {
          contentStruct.put("categories", postCategories);
        }
      }

      contentStruct.put("mt_excerpt", mPost.getPostExcerpt());
      contentStruct.put((mPost.isPage()) ? "page_status" : "post_status", mPost.getPostStatus());

      // Geolocation
      if (mPost.supportsLocation()) {
        JSONObject remoteGeoLatitude = mPost.getCustomField("geo_latitude");
        JSONObject remoteGeoLongitude = mPost.getCustomField("geo_longitude");
        JSONObject remoteGeoPublic = mPost.getCustomField("geo_public");

        Map<Object, Object> hLatitude = new HashMap<Object, Object>();
        Map<Object, Object> hLongitude = new HashMap<Object, Object>();
        Map<Object, Object> hPublic = new HashMap<Object, Object>();

        try {
          if (remoteGeoLatitude != null) {
            hLatitude.put("id", remoteGeoLatitude.getInt("id"));
          }

          if (remoteGeoLongitude != null) {
            hLongitude.put("id", remoteGeoLongitude.getInt("id"));
          }

          if (remoteGeoPublic != null) {
            hPublic.put("id", remoteGeoPublic.getInt("id"));
          }

          if (mPost.hasLocation()) {
            PostLocation location = mPost.getLocation();
            if (!hLatitude.containsKey("id")) {
              hLatitude.put("key", "geo_latitude");
            }

            if (!hLongitude.containsKey("id")) {
              hLongitude.put("key", "geo_longitude");
            }

            if (!hPublic.containsKey("id")) {
              hPublic.put("key", "geo_public");
            }

            hLatitude.put("value", location.getLatitude());
            hLongitude.put("value", location.getLongitude());
            hPublic.put("value", 1);
          }
        } catch (JSONException e) {
          AppLog.e(T.EDITOR, e);
        }

        if (!hLatitude.isEmpty() && !hLongitude.isEmpty() && !hPublic.isEmpty()) {
          Object[] geo = {hLatitude, hLongitude, hPublic};
          contentStruct.put("custom_fields", geo);
        }
      }

      // featured image
      if (featuredImageID != -1) {
        contentStruct.put("wp_post_thumbnail", featuredImageID);
      }

      if (!TextUtils.isEmpty(mPost.getQuickPostType())) {
        mClient.addQuickPostHeader(mPost.getQuickPostType());
      }

      contentStruct.put("wp_password", mPost.getPassword());

      Object[] params;
      if (mPost.isLocalDraft())
        params =
            new Object[] {
              mBlog.getRemoteBlogId(),
              mBlog.getUsername(),
              mBlog.getPassword(),
              contentStruct,
              false
            };
      else
        params =
            new Object[] {
              mPost.getRemotePostId(),
              mBlog.getUsername(),
              mBlog.getPassword(),
              contentStruct,
              false
            };

      try {
        EventBus.getDefault().post(new PostUploadStarted(mPost.getLocalTableBlogId()));

        if (mPost.isLocalDraft()) {
          Object object = mClient.call("metaWeblog.newPost", params);
          if (object instanceof String) {
            mPost.setRemotePostId((String) object);
          }
        } else {
          mClient.call("metaWeblog.editPost", params);
        }

        // Track any Analytics before modifying the post
        trackUploadAnalytics();

        mPost.setLocalDraft(false);
        mPost.setLocalChange(false);
        WordPress.wpDB.updatePost(mPost);

        // request the new/updated post from the server to ensure local copy matches server
        ApiHelper.updateSinglePost(
            mBlog.getLocalTableBlogId(), mPost.getRemotePostId(), mPost.isPage());

        return true;
      } catch (final XMLRPCException e) {
        setUploadPostErrorMessage(e);
      } catch (IOException e) {
        setUploadPostErrorMessage(e);
      } catch (XmlPullParserException e) {
        setUploadPostErrorMessage(e);
      }

      return false;
    }