@Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.conversations);

    gcm = Gcm.getInstance(getApplicationContext());
    billing = Billing.getInstance(getApplicationContext());
    database = Database.getInstance(getApplicationContext());
    preferences = Preferences.getInstance(getApplicationContext());

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    ViewCompat.setElevation(toolbar, getResources().getDimension(R.dimen.toolbar_elevation));
    setSupportActionBar(toolbar);

    LinearLayoutManager layoutManager = new LinearLayoutManager(this);
    layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
    adapter = new ConversationsRecyclerViewAdapter(this, layoutManager);
    recyclerView = (RecyclerView) findViewById(R.id.list);
    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(layoutManager);
    recyclerView.setAdapter(adapter);

    actionMode = null;
    actionModeEnabled = false;

    SwipeRefreshLayout swipeRefreshLayout =
        (SwipeRefreshLayout) findViewById(R.id.swipe_refresh_layout);
    swipeRefreshLayout.setOnRefreshListener(
        new SwipeRefreshLayout.OnRefreshListener() {
          @Override
          public void onRefresh() {
            preFullUpdate();
          }
        });
    swipeRefreshLayout.setColorSchemeResources(R.color.accent);

    ImageButton button = (ImageButton) findViewById(R.id.new_button);
    ViewCompat.setElevation(button, getResources().getDimension(R.dimen.fab_elevation));
    button.setOnClickListener(
        new View.OnClickListener() {
          @Override
          public void onClick(View v) {
            if (!preferences.getEmail().equals("")
                && !preferences.getPassword().equals("")
                && !preferences.getDid().equals("")) {
              Intent intent = new Intent(conversationsActivity, NewConversationActivity.class);
              startActivity(intent);
            }
          }
        });

    SynchronizationIntervalReceiver.setupSynchronizationInterval(getApplicationContext());
  }
  /**
   * Called when an action mode item is clicked.
   *
   * @param mode The action mode containing the item that is clicked.
   * @param item The item that is clicked.
   * @return Returns true if the method handles the item clicked.
   */
  @Override
  public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
    switch (item.getItemId()) {
      case R.id.mark_read_unread_button:
        for (int i = 0; i < adapter.getItemCount(); i++) {
          if (adapter.isItemChecked(i)) {
            Message[] smses = adapter.getItem(i).getMessages();
            for (Message message : smses) {
              message.setUnread(
                  item.getTitle()
                      .equals(getResources().getString(R.string.conversations_action_mark_unread)));
              database.insertMessage(message);
            }
          }
        }
        adapter.refresh();
        mode.finish();
        return true;
      case R.id.delete_button:
        List<Long> databaseIds = new ArrayList<>();
        for (int i = 0; i < adapter.getItemCount(); i++) {
          if (adapter.isItemChecked(i)) {
            for (Message message : adapter.getItem(i).getMessages()) {
              if (message.getDatabaseId() != null) {
                databaseIds.add(message.getDatabaseId());
              }
            }
          }
        }

        Long[] databaseIdsArray = new Long[databaseIds.size()];
        databaseIds.toArray(databaseIdsArray);
        deleteMessages(databaseIdsArray);
        mode.finish();
        return true;
      default:
        return false;
    }
  }
 /**
  * Initiates a full update of the message database. This update follows all synchronization rules
  * set in the application's settings.
  */
 public void preFullUpdate() {
   adapter.refresh();
   gcm.registerForGcm(conversationsActivity, false, false);
   database.synchronize(false, true, conversationsActivity);
 }