/**
  * This method will be called when conversations are posted to the EventBus.
  *
  * @param event The bus event containing conversation objects.
  */
 public void onEvent(BusEventConversations event) {
   // Unregister this instance. For new push messages the new instance will be registered in
   // onCreate().
   EventBus.getDefault().unregister(this);
   Log.d(TAG, event.toString());
   boolean newConversationsStored =
       GroupController.storeConversations(
           getApplicationContext(), event.getConversations(), pushMessage.getId1());
   if (newConversationsStored
       || (event.getConversations() != null
           && !event.getConversations().isEmpty()
           && pushMessage.getPushType().equals(PushType.CONVERSATION_CHANGED_ALL))) {
     new GroupDatabaseManager(getApplicationContext())
         .setGroupNewEvents(pushMessage.getId1(), true);
     NotificationSettings notificationSettings =
         settingsDBM.getGroupNotificationSettings(pushMessage.getId1());
     if (notificationSettings == NotificationSettings.GENERAL) {
       notificationSettings = settingsDBM.getSettings().getNotificationSettings();
     }
     if (notificationSettings.equals(NotificationSettings.ALL)) {
       sendConversationNotification(pushMessage.getId1());
     } else if (notificationSettings.equals(NotificationSettings.PRIORITY)) {
       if (pushMessage.getPushType().equals(PushType.CONVERSATION_NEW)) {
         sendConversationNotification(pushMessage.getId1());
       }
     }
   }
 }
  /**
   * Create and show a notification containing the conversations name where the new conversation
   * message was posted.
   */
  private void sendNewConversationMessageNotification(int groupId, int conversationId) {
    Intent intent;
    boolean loggedIn = Util.getInstance(this).getLoggedInModerator() != null;
    if (loggedIn) {
      // User is logged in as local moderator.
      intent = new Intent(this, ModeratorMainActivity.class);
    } else {
      // User isn't logged in.
      intent = new Intent(this, GroupActivity.class);
    }
    intent.putExtra("groupId", groupId);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

    // Create a PendingIntent containing the entire back stack.
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
    if (loggedIn) {
      stackBuilder.addParentStack(ModeratorMainActivity.class);
    } else {
      stackBuilder.addParentStack(GroupActivity.class);
    }
    stackBuilder.addNextIntent(intent);
    PendingIntent pendingIntent =
        stackBuilder.getPendingIntent(groupId, PendingIntent.FLAG_UPDATE_CURRENT);

    // Set group icon as large icon.
    Conversation conversation =
        new GroupDatabaseManager(getApplicationContext()).getConversation(conversationId);
    Bitmap bitmap =
        BitmapFactory.decodeResource(
            getResources(),
            GroupController.getConversationIcon(conversation, getApplicationContext()));
    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder =
        new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_stat_notification_icon_1)
            .setColor(ContextCompat.getColor(this, R.color.uni_main_primary))
            .setLargeIcon(bitmap)
            .setContentTitle(
                String.format(
                    getString(R.string.push_message_conversation), conversation.getTitle()))
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);

    if (conversation.getNumberOfUnreadConversationMessages() > 1) {
      notificationBuilder.setContentText(
          getString(R.string.push_message_new_conversation_messages));
      notificationBuilder.setNumber(conversation.getNumberOfUnreadConversationMessages());
    } else {
      notificationBuilder.setContentText(getString(R.string.push_message_new_conversation_message));
    }

    NotificationManager notificationManager =
        (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(5, notificationBuilder.build());
  }
  /**
   * This method will be called when a list of options is posted to the EventBus.
   *
   * @param event The bus event containing a list of ballot options objects.
   */
  public void onEventMainThread(BusEventOptions event) {
    // Unregister this instance. For new push messages the new instance will be registered in
    // onCreate().
    EventBus.getDefault().unregister(this);
    Log.d(TAG, event.toString());
    List<Option> options = event.getOptions();

    boolean newOptions =
        GroupController.storeOptions(getApplicationContext(), options, pushMessage.getId2());
    boolean newVotes = GroupController.storeVoters(getApplicationContext(), options);

    if (newVotes || newOptions) {
      NotificationSettings notificationSettings =
          settingsDBM.getGroupNotificationSettings(pushMessage.getId1());
      if (notificationSettings == NotificationSettings.GENERAL) {
        notificationSettings = settingsDBM.getSettings().getNotificationSettings();
      }
      if (notificationSettings.equals(NotificationSettings.ALL)) {
        sendOptionNotification(pushMessage.getId1(), pushMessage.getId2());
      }
      new GroupDatabaseManager(getApplicationContext())
          .setGroupNewEvents(pushMessage.getId1(), true);
    }
  }
  /**
   * This method will be called when a list of ballots is posted to the EventBus.
   *
   * @param event The bus event containing a list of ballot objects.
   */
  public void onEventMainThread(BusEventBallots event) {
    // Unregister this instance. For new push messages the new instance will be registered in
    // onCreate().
    EventBus.getDefault().unregister(this);
    Log.d(TAG, event.toString());
    List<Ballot> ballots = event.getBallots();
    GroupController.storeBallots(getApplicationContext(), ballots, pushMessage.getId1());

    NotificationSettings notificationSettings =
        settingsDBM.getGroupNotificationSettings(pushMessage.getId1());
    if (notificationSettings == NotificationSettings.GENERAL) {
      notificationSettings = settingsDBM.getSettings().getNotificationSettings();
    }
    if (notificationSettings.equals(NotificationSettings.ALL)) {
      sendBallotNotification(pushMessage.getId1());
    } else if (notificationSettings.equals(NotificationSettings.PRIORITY)) {
      if (pushMessage.getPushType().equals(PushType.BALLOT_NEW)) {
        sendBallotNotification(pushMessage.getId1());
      }
    }
    new GroupDatabaseManager(getApplicationContext()).setGroupNewEvents(pushMessage.getId1(), true);
  }