gt:         if (itemId == R.id.menu_save_post) {
context: package org.wordpress.android.ui.posts;
  
  import android.app.Activity;
  import android.app.Fragment;
  import android.app.FragmentManager;
  import android.app.NotificationManager;
  import android.content.BroadcastReceiver;
  import android.content.Context;
  import android.content.Intent;
  import android.content.IntentFilter;
  import android.database.Cursor;
  import android.graphics.BitmapFactory;
  import android.net.Uri;
  import android.os.AsyncTask;
  import android.os.Bundle;
  import android.os.Environment;
  import android.support.v13.app.FragmentPagerAdapter;
  import android.support.v4.app.NotificationCompat;
  import android.support.v4.view.ViewPager;
  import android.support.v7.app.ActionBar;
  import android.support.v7.app.ActionBarActivity;
  import android.text.Html;
  import android.text.Spannable;
  import android.text.SpannableStringBuilder;
  import android.text.Spanned;
  import android.text.TextUtils;
  import android.text.style.CharacterStyle;
  import android.text.style.SuggestionSpan;
  import android.view.ContextMenu;
  import android.view.Menu;
  import android.view.MenuInflater;
  import android.view.MenuItem;
  import android.view.View;
  import android.view.ViewGroup;
  import android.webkit.URLUtil;
  import android.widget.Toast;
  
  import org.wordpress.android.Constants;
  import org.wordpress.android.R;
  import org.wordpress.android.WordPress;
  import org.wordpress.android.analytics.AnalyticsTracker;
  import org.wordpress.android.analytics.AnalyticsTracker.Stat;
  import org.wordpress.android.editor.EditorFragmentAbstract;
  import org.wordpress.android.editor.EditorFragmentAbstract.EditorFragmentListener;
  import org.wordpress.android.editor.LegacyEditorFragment;
  import org.wordpress.android.models.Blog;
  import org.wordpress.android.models.Post;
  import org.wordpress.android.models.PostStatus;
  import org.wordpress.android.ui.ActivityId;
  import org.wordpress.android.ui.RequestCodes;
  import org.wordpress.android.ui.media.MediaGalleryActivity;
  import org.wordpress.android.ui.media.MediaGalleryPickerActivity;
  import org.wordpress.android.ui.media.MediaGridFragment;
  import org.wordpress.android.ui.media.MediaPickerActivity;
  import org.wordpress.android.ui.media.MediaSourceWPImages;
  import org.wordpress.android.ui.media.MediaSourceWPVideos;
  import org.wordpress.android.ui.media.WordPressMediaUtils;
  import org.wordpress.android.ui.media.services.MediaUploadEvents;
  import org.wordpress.android.ui.media.services.MediaUploadService;
  import org.wordpress.android.util.AppLog;
  import org.wordpress.android.util.AppLog.T;
  import org.wordpress.android.util.AutolinkUtils;
  import org.wordpress.android.util.DeviceUtils;
  import org.wordpress.android.util.ImageUtils;
  import org.wordpress.android.util.MediaUtils;
  import org.wordpress.android.util.NetworkUtils;
  import org.wordpress.android.util.StringUtils;
  import org.wordpress.android.util.ToastUtils;
  import org.wordpress.android.util.ToastUtils.Duration;
  import org.wordpress.android.ui.suggestion.adapters.TagSuggestionAdapter;
  import org.wordpress.android.ui.suggestion.util.SuggestionServiceConnectionManager;
  import org.wordpress.android.ui.suggestion.util.SuggestionUtils;
  import org.wordpress.android.widgets.SuggestionAutoCompleteText;
  import org.wordpress.android.util.WPHtml;
  import org.wordpress.android.util.helpers.MediaFile;
  import org.wordpress.android.util.helpers.MediaGallery;
  import org.wordpress.android.util.helpers.MediaGalleryImageSpan;
  import org.wordpress.android.util.helpers.WPImageSpan;
  import org.wordpress.android.widgets.WPViewPager;
  import org.wordpress.mediapicker.MediaItem;
  import org.wordpress.mediapicker.source.MediaSource;
  import org.wordpress.mediapicker.source.MediaSourceDeviceImages;
  import org.wordpress.mediapicker.source.MediaSourceDeviceVideos;
  import org.wordpress.passcodelock.AppLockManager;
  import org.xmlrpc.android.ApiHelper;
  
  import java.io.File;
  import java.util.ArrayList;
  import java.util.HashMap;
  import java.util.List;
  import java.util.Map;
  import java.util.Timer;
  import java.util.TimerTask;
  
  import de.greenrobot.event.EventBus;
  
  public class EditPostActivity extends ActionBarActivity implements EditorFragmentListener {
      public static final String EXTRA_POSTID = "postId";
      public static final String EXTRA_IS_PAGE = "isPage";
      public static final String EXTRA_IS_NEW_POST = "isNewPost";
      public static final String EXTRA_IS_QUICKPRESS = "isQuickPress";
      public static final String EXTRA_QUICKPRESS_BLOG_ID = "quickPressBlogId";
      public static final String STATE_KEY_CURRENT_POST = "stateKeyCurrentPost";
      public static final String STATE_KEY_ORIGINAL_POST = "stateKeyOriginalPost";
      public static final String STATE_KEY_EDITOR_FRAGMENT = "editorFragment";
  
      // Context menu positioning
      private static final int SELECT_PHOTO_MENU_POSITION = 0;
      private static final int CAPTURE_PHOTO_MENU_POSITION = 1;
      private static final int SELECT_VIDEO_MENU_POSITION = 2;
      private static final int CAPTURE_VIDEO_MENU_POSITION = 3;
      private static final int ADD_GALLERY_MENU_POSITION = 4;
      private static final int SELECT_LIBRARY_MENU_POSITION = 5;
      private static final int NEW_PICKER_MENU_POSITION = 6;
  
      private static final String ANALYTIC_PROP_NUM_LOCAL_PHOTOS_ADDED = "number_of_local_photos_added";
      private static final String ANALYTIC_PROP_NUM_WP_PHOTOS_ADDED = "number_of_wp_library_photos_added";
  
      private static int PAGE_CONTENT = 0;
      private static int PAGE_SETTINGS = 1;
      private static int PAGE_PREVIEW = 2;
  
      private static final int AUTOSAVE_INTERVAL_MILLIS = 10000;
      private Timer mAutoSaveTimer;
  
      // Each element is a list of media IDs being uploaded to a gallery, keyed by gallery ID
      private Map<Long, List<String>> mPendingGalleryUploads = new HashMap<>();
  
      // -1=no response yet, 0=unavailable, 1=available
      private int mBlogMediaStatus = -1;
      private boolean mMediaUploadServiceStarted;
  
      /**
       * The {@link android.support.v4.view.PagerAdapter} that will provide
       * fragments for each of the sections. We use a
       * {@link FragmentPagerAdapter} derivative, which will keep every
       * loaded fragment in memory. If this becomes too memory intensive, it
       * may be best to switch to a
       * {@link android.support.v13.app.FragmentStatePagerAdapter}.
       */
      SectionsPagerAdapter mSectionsPagerAdapter;
  
      /**
       * The {@link ViewPager} that will host the section contents.
       */
      WPViewPager mViewPager;
  
      private Post mPost;
      private Post mOriginalPost;
      private TagSuggestionAdapter mTagSuggestionAdapter;
      private SuggestionServiceConnectionManager mSuggestionServiceConnectionManager;
      private int remoteBlogId;
  
      private EditorFragmentAbstract mEditorFragment;
      private EditPostSettingsFragment mEditPostSettingsFragment;
      private EditPostPreviewFragment mEditPostPreviewFragment;
      private SuggestionAutoCompleteText mTags;
  
      private boolean mIsNewPost;
      private boolean mHasSetPostContent;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_new_edit_post);
  
          // Set up the action bar.
          final ActionBar actionBar = getSupportActionBar();
          if (actionBar != null) {
              actionBar.setElevation(0.0f);
              actionBar.setDisplayHomeAsUpEnabled(true);
          }
  
          FragmentManager fragmentManager = getFragmentManager();
          Bundle extras = getIntent().getExtras();
          String action = getIntent().getAction();
          if (savedInstanceState == null) {
              if (Intent.ACTION_SEND.equals(action) || Intent.ACTION_SEND_MULTIPLE.equals(action)
                      || NEW_MEDIA_GALLERY.equals(action)
                      || NEW_MEDIA_POST.equals(action)
                      || getIntent().hasExtra(EXTRA_IS_QUICKPRESS)
                      || (extras != null && extras.getInt("quick-media", -1) > -1)) {
                  if (getIntent().hasExtra(EXTRA_QUICKPRESS_BLOG_ID)) {
                      // QuickPress might want to use a different blog than the current blog
                      int blogId = getIntent().getIntExtra(EXTRA_QUICKPRESS_BLOG_ID, -1);
                      Blog quickPressBlog = WordPress.wpDB.instantiateBlogByLocalId(blogId);
                      if (quickPressBlog == null) {
                          showErrorAndFinish(R.string.blog_not_found);
                          return;
                      }
                      if (quickPressBlog.isHidden()) {
                          showErrorAndFinish(R.string.error_blog_hidden);
                          return;
                      }
                      WordPress.currentBlog = quickPressBlog;
                  }
  
                  // Create a new post for share intents and QuickPress
                  mPost = new Post(WordPress.getCurrentLocalTableBlogId(), false);
                  WordPress.wpDB.savePost(mPost);
                  mIsNewPost = true;
              } else if (extras != null) {
                  // Load post from the postId passed in extras
                  long localTablePostId = extras.getLong(EXTRA_POSTID, -1);
                  boolean isPage = extras.getBoolean(EXTRA_IS_PAGE);
                  mIsNewPost = extras.getBoolean(EXTRA_IS_NEW_POST);
                  mPost = WordPress.wpDB.getPostForLocalTablePostId(localTablePostId);
                  mOriginalPost = WordPress.wpDB.getPostForLocalTablePostId(localTablePostId);
              } else {
                  // A postId extra must be passed to this activity
                  showErrorAndFinish(R.string.post_not_found);
                  return;
              }
          } else {
              if (savedInstanceState.containsKey(STATE_KEY_ORIGINAL_POST)) {
                  try {
                      mPost = (Post) savedInstanceState.getSerializable(STATE_KEY_CURRENT_POST);
                      mOriginalPost = (Post) savedInstanceState.getSerializable(STATE_KEY_ORIGINAL_POST);
                  } catch (ClassCastException e) {
                      mPost = null;
                  }
              }
              mEditorFragment = (EditorFragmentAbstract) fragmentManager.getFragment(savedInstanceState, STATE_KEY_EDITOR_FRAGMENT);
          }
  
          if (mHasSetPostContent = mEditorFragment != null) {
              mEditorFragment.setImageLoader(WordPress.imageLoader);
          }
  
          // Ensure we have a valid blog
          if (WordPress.getCurrentBlog() == null) {
              showErrorAndFinish(R.string.blog_not_found);
              return;
          }
  
          // Ensure we have a valid post
          if (mPost == null) {
              showErrorAndFinish(R.string.post_not_found);
              return;
          }
  
          if (mIsNewPost) {
              trackEditorCreatedPost(action, getIntent());
          }
  
          setTitle(StringUtils.unescapeHTML(WordPress.getCurrentBlog().getBlogName()));
  
          mSectionsPagerAdapter = new SectionsPagerAdapter(fragmentManager);
  
          // Set up the ViewPager with the sections adapter.
          mViewPager = (WPViewPager) findViewById(R.id.pager);
          mViewPager.setAdapter(mSectionsPagerAdapter);
          mViewPager.setOffscreenPageLimit(2);
          mViewPager.setPagingEnabled(false);
  
          // When swiping between different sections, select the corresponding
          // tab. We can also use ActionBar.Tab#select() to do this if we have
          // a reference to the Tab.
          mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
              @Override
              public void onPageSelected(int position) {
                  invalidateOptionsMenu();
                  if (position == PAGE_CONTENT) {
                      setTitle(StringUtils.unescapeHTML(WordPress.getCurrentBlog().getBlogName()));
                  } else if (position == PAGE_SETTINGS) {
                      setTitle(mPost.isPage() ? R.string.page_settings : R.string.post_settings);
                  } else if (position == PAGE_PREVIEW) {
                      setTitle(mPost.isPage() ? R.string.preview_page : R.string.preview_post);
                      savePost(true);
                      if (mEditPostPreviewFragment != null) {
                          mEditPostPreviewFragment.loadPost();
                      }
                  }
              }
          });
          ActivityId.trackLastActivity(ActivityId.POST_EDITOR);
      }
  
      class AutoSaveTask extends TimerTask {
          public void run() {
              savePost(true);
          }
      }
  
      @Override
      protected void onResume() {
          super.onResume();
  
          registerReceiver(mGalleryReceiver,
                  new IntentFilter(LegacyEditorFragment.ACTION_MEDIA_GALLERY_TOUCHED));
  
          refreshBlogMedia();
          mAutoSaveTimer = new Timer();
          mAutoSaveTimer.scheduleAtFixedRate(new AutoSaveTask(), AUTOSAVE_INTERVAL_MILLIS, AUTOSAVE_INTERVAL_MILLIS);
      }
  
      @Override
      public void onStart() {
          super.onStart();
          EventBus.getDefault().register(this);
      }
  
      @Override
      public void onStop() {
          EventBus.getDefault().unregister(this);
          super.onStop();
      }
  
      @Override
      protected void onPause() {
          super.onPause();
  
          try {
              unregisterReceiver(mGalleryReceiver);
          } catch (IllegalArgumentException e) {
              AppLog.d(T.EDITOR, "Illegal state! Can't unregister receiver that was no registered");
          }
  
          stopMediaUploadService();
          mAutoSaveTimer.cancel();
      }
  
      @Override
      protected void onDestroy() {
          super.onDestroy();
  
          AnalyticsTracker.track(AnalyticsTracker.Stat.EDITOR_CLOSED_POST);
  
          if (mSuggestionServiceConnectionManager != null) {
              mSuggestionServiceConnectionManager.unbindFromService();
          }
      }
  
      @Override
      protected void onSaveInstanceState(Bundle outState) {
          super.onSaveInstanceState(outState);
          // Saves both post objects so we can restore them in onCreate()
          savePost(true);
          outState.putSerializable(STATE_KEY_CURRENT_POST, mPost);
          outState.putSerializable(STATE_KEY_ORIGINAL_POST, mOriginalPost);
  
          if (mEditorFragment != null) {
              getFragmentManager().putFragment(outState, STATE_KEY_EDITOR_FRAGMENT, mEditorFragment);
          }
      }
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          super.onCreateOptionsMenu(menu);
          MenuInflater inflater = getMenuInflater();
          inflater.inflate(R.menu.edit_post, menu);
  
          mTags = (SuggestionAutoCompleteText) findViewById(R.id.tags);
          mTags.setTokenizer(new SuggestionAutoCompleteText.CommaTokenizer());
  
          remoteBlogId = WordPress.getCurrentRemoteBlogId();
          mSuggestionServiceConnectionManager = new SuggestionServiceConnectionManager(this, remoteBlogId);
          mTagSuggestionAdapter = SuggestionUtils.setupTagSuggestions(remoteBlogId, this, mSuggestionServiceConnectionManager);
          if (mTagSuggestionAdapter != null) {
              mTags.setAdapter(mTagSuggestionAdapter);
          }
  
          return true;
      }
  
      @Override
      public boolean onPrepareOptionsMenu(Menu menu) {
          MenuItem previewMenuItem = menu.findItem(R.id.menu_preview_post);
          if (mViewPager != null && mViewPager.getCurrentItem() > PAGE_CONTENT) {
              previewMenuItem.setVisible(false);
          } else {
              previewMenuItem.setVisible(true);
          }
  
          // Set text of the save button in the ActionBar
          if (mPost != null) {
              MenuItem saveMenuItem = menu.findItem(R.id.menu_save_post);
              switch (mPost.getStatusEnum()) {
                  case SCHEDULED:
                      saveMenuItem.setTitle(getString(R.string.schedule_verb));
                      break;
                  case PUBLISHED:
                  case UNKNOWN:
                      if (mPost.isLocalDraft()) {
                          saveMenuItem.setTitle(R.string.publish_post);
                      } else {
                          saveMenuItem.setTitle(R.string.update_verb);
                      }
                      break;
                  default:
                      if (mPost.isLocalDraft()) {
                          saveMenuItem.setTitle(R.string.save);
                      } else {
                          saveMenuItem.setTitle(R.string.update_verb);
                      }
              }
          }
  
          return super.onPrepareOptionsMenu(menu);
      }
  
      private Map<String, Object> getWordCountTrackingProperties() {
          Map<String, Object> properties = new HashMap<String, Object>();
          String text = Html.fromHtml(mPost.getContent().replaceAll("<img[^>]*>", "")).toString();
          properties.put("word_count", text.split("\\s+").length);
          return properties;
      }
  
      private void trackSavePostAnalytics() {
          PostStatus status = mPost.getStatusEnum();
          switch (status) {
              case PUBLISHED:
                  if (mPost.isUploaded()) {
                      AnalyticsTracker.track(AnalyticsTracker.Stat.EDITOR_UPDATED_POST);
                  } else {
                      AnalyticsTracker.track(AnalyticsTracker.Stat.EDITOR_PUBLISHED_POST,
                              getWordCountTrackingProperties());
                  }
                  break;
              case SCHEDULED:
                  if (mPost.isUploaded()) {
                      AnalyticsTracker.track(AnalyticsTracker.Stat.EDITOR_UPDATED_POST);
                  } else {
                      AnalyticsTracker.track(AnalyticsTracker.Stat.EDITOR_SCHEDULED_POST,
                              getWordCountTrackingProperties());
                  }
                  break;
              case DRAFT:
                  AnalyticsTracker.track(AnalyticsTracker.Stat.EDITOR_SAVED_DRAFT);
                  break;
              default:
                  // No-op
          }
      }
  
      // Menu actions
      @Override
      public boolean onOptionsItemSelected(MenuItem item) {
          int itemId = item.getItemId();
  