private void handleCommonRegistration(
      MasterSecret masterSecret, TextSecureAccountManager accountManager, String number)
      throws IOException {
    setState(new RegistrationState(RegistrationState.STATE_GENERATING_KEYS, number));
    Recipient self =
        RecipientFactory.getRecipientsFromString(this, number, false).getPrimaryRecipient();
    IdentityKeyPair identityKey = IdentityKeyUtil.getIdentityKeyPair(this, masterSecret);
    List<PreKeyRecord> records = PreKeyUtil.generatePreKeys(this, masterSecret);
    PreKeyRecord lastResort = PreKeyUtil.generateLastResortKey(this, masterSecret);
    SignedPreKeyRecord signedPreKey =
        PreKeyUtil.generateSignedPreKey(this, masterSecret, identityKey);
    accountManager.setPreKeys(identityKey.getPublicKey(), lastResort, signedPreKey, records);

    setState(new RegistrationState(RegistrationState.STATE_GCM_REGISTERING, number));

    String gcmRegistrationId = "";
    accountManager.setGcmId(Optional.of(gcmRegistrationId));

    TextSecurePreferences.setGcmRegistrationId(this, gcmRegistrationId);
    TextSecurePreferences.setWebsocketRegistered(this, true);

    DatabaseFactory.getIdentityDatabase(this)
        .saveIdentity(masterSecret, self.getRecipientId(), identityKey.getPublicKey());
    DirectoryHelper.refreshDirectory(this, accountManager, number);

    DirectoryRefreshListener.schedule(this);
  }
  @Override
  public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
    if (isEncryptedConversation && isSingleConversation()) {
      boolean isPushDestination = DirectoryHelper.isPushDestination(this, getRecipients());
      Recipient primaryRecipient =
          getRecipients() == null ? null : getRecipients().getPrimaryRecipient();
      boolean hasSession = Session.hasSession(this, masterSecret, primaryRecipient);

      getMenuInflater().inflate(R.menu.conversation_button_context, menu);

      if (attachmentManager.isAttachmentPresent()) {
        menu.removeItem(R.id.menu_context_send_encrypted_sms);
        menu.removeItem(R.id.menu_context_send_unencrypted_sms);
      } else {
        menu.removeItem(R.id.menu_context_send_encrypted_mms);
        menu.removeItem(R.id.menu_context_send_unencrypted_mms);
      }

      if (!isPushDestination) {
        menu.removeItem(R.id.menu_context_send_push);
      }

      if (!hasSession) {
        menu.removeItem(R.id.menu_context_send_encrypted_mms);
        menu.removeItem(R.id.menu_context_send_encrypted_sms);
      }
    }
  }
 private void handleAddAttachment() {
   if (this.isMmsEnabled || DirectoryHelper.isPushDestination(this, getRecipients())) {
     AlertDialog.Builder builder =
         new AlertDialog.Builder(new ContextThemeWrapper(this, R.style.TextSecure_Light_Dialog));
     builder.setIcon(R.drawable.ic_dialog_attach);
     builder.setTitle(R.string.ConversationActivity_add_attachment);
     builder.setAdapter(attachmentAdapter, new AttachmentTypeListener());
     builder.show();
   } else {
     handleManualMmsRequired();
   }
 }
  private void initializeSecurity() {
    TypedArray drawables = obtainStyledAttributes(SEND_ATTRIBUTES);
    Recipient primaryRecipient =
        getRecipients() == null ? null : getRecipients().getPrimaryRecipient();
    boolean isPushDestination = DirectoryHelper.isPushDestination(this, getRecipients());
    boolean isSecureDestination =
        isSingleConversation() && Session.hasSession(this, masterSecret, primaryRecipient);

    if (isPushDestination || isSecureDestination) {
      this.isEncryptedConversation = true;
      this.characterCalculator = new EncryptedCharacterCalculator();
    } else {
      this.isEncryptedConversation = false;
      this.characterCalculator = new CharacterCalculator();
    }

    if (isPushDestination) {
      sendButton.setImageDrawable(drawables.getDrawable(0));
      setComposeTextHint(getString(R.string.conversation_activity__type_message_push));
    } else if (isSecureDestination) {
      sendButton.setImageDrawable(drawables.getDrawable(1));
      setComposeTextHint(
          attachmentManager.isAttachmentPresent()
              ? getString(R.string.conversation_activity__type_message_mms_secure)
              : getString(R.string.conversation_activity__type_message_sms_secure));
    } else {
      sendButton.setImageDrawable(drawables.getDrawable(2));
      setComposeTextHint(
          (attachmentManager.isAttachmentPresent() || !recipients.isSingleRecipient())
              ? getString(R.string.conversation_activity__type_message_mms_insecure)
              : getString(R.string.conversation_activity__type_message_sms_insecure));
    }

    drawables.recycle();

    calculateCharactersRemaining();
  }