@Override
 public void handleMessage(Message aMsg) {
   Log.i(TAG, "Received Message:" + aMsg.what + " " + aMsg.arg1 + " " + aMsg.arg2);
   switch (aMsg.arg1) {
     case AwfulSyncService.Status.OKAY:
       loadingSucceeded();
       if (mDialog != null) {
         mDialog.dismiss();
         mDialog = null;
       }
       if (aMsg.what == AwfulSyncService.MSG_FETCH_POST_REPLY) {
         refreshLoader();
       }
       if (aMsg.what == AwfulSyncService.MSG_SEND_POST) {
         sendSuccessful = true;
         if (getActivity() != null) {
           Toast.makeText(
                   getActivity(),
                   getActivity().getString(R.string.post_sent),
                   Toast.LENGTH_LONG)
               .show();
           leave();
         }
       }
       break;
     case AwfulSyncService.Status.WORKING:
       loadingStarted();
       break;
     case AwfulSyncService.Status.ERROR:
       loadingFailed();
       if (mDialog != null) {
         mDialog.dismiss();
         mDialog = null;
       }
       if (aMsg.what == AwfulSyncService.MSG_SEND_POST) {
         saveReply();
         if (getActivity() != null) {
           Toast.makeText(
                   getActivity(), "Post Failed to Send! Message Saved...", Toast.LENGTH_LONG)
               .show();
         }
       }
       if (aMsg.what == AwfulSyncService.MSG_FETCH_POST_REPLY && getActivity() != null) {
         Toast.makeText(getActivity(), "Reply Load Failed!", Toast.LENGTH_LONG).show();
       }
       break;
     default:
       super.handleMessage(aMsg);
   }
 }
 private void autosave() {
   if (!sendSuccessful && mMessage != null) {
     if (mMessage.length() < 1
         || mMessage.getText().toString().replaceAll("\\s", "").length() < 1
         || this.sendSuccessful) {
       Log.i(TAG, "Message unchanged, discarding.");
       deleteReply(); // if the reply is unchanged, throw it out.
       mMessage.setText("");
     } else {
       Log.i(TAG, "Message Unsent, saving.");
       saveReply();
     }
   }
 }
 @Override
 public void onStop() {
   super.onStop();
   if (!sendSuccessful) {
     if (mMessage
         .getText()
         .toString()
         .replaceAll("\\s", "")
         .equalsIgnoreCase(originalReplyData.replaceAll("\\s", ""))) {
       Log.i(TAG, "Message unchanged, discarding.");
       deleteReply(); // if the reply is unchanged, throw it out.
     } else {
       Log.i(TAG, "Message Unsent, saving.");
       saveReply();
     }
   }
   cleanupTasks();
 }
  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    if (DEBUG) Log.e(TAG, "onOptionsItemSelected");
    switch (item.getItemId()) {
      case R.id.bbcode_bold:
        insertBBCode(BBCODE.BOLD);
        break;
      case R.id.bbcode_italics:
        insertBBCode(BBCODE.ITALICS);
        break;
      case R.id.bbcode_underline:
        insertBBCode(BBCODE.UNDERLINE);
        break;
      case R.id.bbcode_strikeout:
        insertBBCode(BBCODE.STRIKEOUT);
        break;
      case R.id.bbcode_url:
        insertBBCode(BBCODE.URL);
        break;
      case R.id.bbcode_video:
        insertBBCode(BBCODE.VIDEO);
        break;
      case R.id.bbcode_image:
        insertBBCode(BBCODE.IMAGE);
        break;
      case R.id.bbcode_thumbnail:
        insertBBCode(BBCODE.THUMBNAIL);
        break;
      case R.id.bbcode_quote:
        insertBBCode(BBCODE.QUOTE);
        break;
      case R.id.bbcode_spoiler:
        insertBBCode(BBCODE.SPOILER);
        break;
      case R.id.bbcode_code:
        insertBBCode(BBCODE.CODE);
        break;
      case R.id.submit_button:
        postReply();
        break;
      case R.id.discard:
        deleteReply();
        getActivity().setResult(RESULT_CANCELLED);
        leave();
        break;
      case R.id.save_draft:
        saveReply();
        getActivity().setResult(RESULT_CANCELLED);
        leave();
        break;
      case R.id.emotes:
        selectionStart = mMessage.getSelectionStart();
        new EmoteFragment(this).show(getFragmentManager(), "emotes");
        break;
      case R.id.add_attachment:
        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        intent.setType("image/*");
        startActivityForResult(Intent.createChooser(intent, "Select Picture"), ADD_ATTACHMENT);

        break;
      case R.id.remove_attachment:
        this.mFileAttachment = null;
        Toast removeToast =
            Toast.makeText(
                getAwfulActivity(),
                getAwfulActivity().getResources().getText(R.string.file_removed),
                Toast.LENGTH_SHORT);
        removeToast.show();
        invalidateOptionsMenu();
        break;
      case R.id.signature:
        item.setChecked(!item.isChecked());
        postSignature = item.isChecked();
        break;
      case R.id.disableEmots:
        item.setChecked(!item.isChecked());
        disableEmots = item.isChecked();
        break;
      default:
        return super.onOptionsItemSelected(item);
    }

    return true;
  }