/**
   * This method will be called when a list of users is posted to the EventBus.
   *
   * @param event The bus event containing a list of user objects.
   */
  public void onEvent(BusEventGroupMembers 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<User> users = event.getUsers();

    // Store users in database an add them as group members to the group.
    UserDatabaseManager userDBM = new UserDatabaseManager(this);
    GroupDatabaseManager groupDBM = new GroupDatabaseManager(this);
    for (User u : users) {
      userDBM.storeUser(u);
      // Update users to make name changes visible.
      userDBM.updateUser(u);
      if (u.getActive() != null && u.getActive()) {
        groupDBM.addUserToGroup(pushMessage.getId1(), u.getId());
      } else {
        groupDBM.removeUserFromGroup(pushMessage.getId1(), u.getId());
      }
    }
    NotificationSettings notificationSettings =
        settingsDBM.getGroupNotificationSettings(pushMessage.getId1());
    if (notificationSettings == NotificationSettings.GENERAL) {
      notificationSettings = settingsDBM.getSettings().getNotificationSettings();
    }
    if (notificationSettings.equals(NotificationSettings.ALL)) {
      sendGroupMemberNotification(pushMessage.getId1());
    }
    groupDBM.setGroupNewEvents(pushMessage.getId1(), true);
  }
 /**
  * This method will be called when a list of moderators is posted to the EventBus.
  *
  * @param event The bus event containing a list of moderator objects.
  */
 public void onEvent(BusEventModerators 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());
   ModeratorController.storeModerators(this, event.getModerators(), pushMessage.getId1());
 }
 /**
  * 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());
       }
     }
   }
 }
 /**
  * This method will be called when a conversation is posted to the EventBus.
  *
  * @param conversation The bus event containing a conversation object.
  */
 public void onEvent(Conversation conversation) {
   // Unregister this instance. For new push messages the new instance will be registered in
   // onCreate().
   EventBus.getDefault().unregister(this);
   Log.d(TAG, "EventBus:" + conversation.toString());
   GroupDatabaseManager groupDBM = new GroupDatabaseManager(getApplicationContext());
   groupDBM.updateConversation(conversation);
   groupDBM.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());
     }
   }
 }
  /**
   * Called when message is received.
   *
   * @param from SenderID of the sender.
   * @param data Data bundle containing message data as key/value pairs. For Set of keys use
   *     data.keySet().
   */
  @Override
  public void onMessageReceived(String from, Bundle data) {
    String message = data.getString("message");
    Gson gson = new Gson();

    pushMessage = gson.fromJson(message, PushMessage.class);
    Log.d(TAG, pushMessage.toString());

    // Process the push message.
    if (pushMessage != null) {
      handlePushMessage();
    }
  }
 /**
  * This method will be called when conversation messages are posted to the EventBus.
  *
  * @param event The bus event containing conversation message objects.
  */
 public void onEvent(BusEventConversationMessages 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<ConversationMessage> conversationMessages = event.getConversationMessages();
   // Store new conversation messages in database.
   GroupDatabaseManager groupDBM = new GroupDatabaseManager(getApplicationContext());
   for (ConversationMessage cm : conversationMessages) {
     groupDBM.storeConversationMessage(cm);
   }
   if (!conversationMessages.isEmpty()) {
     groupDBM.setGroupNewEvents(pushMessage.getId1(), true);
     NotificationSettings notificationSettings =
         settingsDBM.getGroupNotificationSettings(pushMessage.getId1());
     if (notificationSettings == NotificationSettings.GENERAL) {
       notificationSettings = settingsDBM.getSettings().getNotificationSettings();
     }
     if (!notificationSettings.equals(NotificationSettings.NONE)) {
       sendNewConversationMessageNotification(pushMessage.getId1(), pushMessage.getId2());
     }
   }
 }
  /**
   * 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);
  }
 /** Load data described by push message. */
 private void handlePushMessage() {
   int messageNumber;
   NotificationSettings notificationSettings;
   switch (pushMessage.getPushType()) {
     case ANNOUNCEMENT_NEW:
       // Load new announcements from server.
       messageNumber =
           new ChannelDatabaseManager(getApplicationContext())
               .getMaxMessageNumberAnnouncement(pushMessage.getId1());
       ChannelAPI.getInstance(getApplicationContext())
           .getAnnouncements(pushMessage.getId1(), messageNumber);
       break;
     case CHANNEL_DELETED:
       // Mark local channel as deleted.
       int channelId = pushMessage.getId1();
       ChannelController.deleteChannel(getApplicationContext(), channelId);
       // Notify user as if this push message had the priority HIGH.
       notificationSettings = settingsDBM.getChannelNotificationSettings(channelId);
       if (notificationSettings == NotificationSettings.GENERAL) {
         notificationSettings = settingsDBM.getSettings().getNotificationSettings();
       }
       if (!notificationSettings.equals(NotificationSettings.NONE)) {
         sendChannelDeletedNotification(channelId);
       }
       break;
     case CHANNEL_CHANGED:
       // Load updated channel data from server.
       ChannelAPI.getInstance(getApplicationContext()).getChannel(pushMessage.getId1());
       break;
     case MODERATOR_ADDED:
     case MODERATOR_CHANGED:
     case MODERATOR_REMOVED:
       // Refresh responsible moderator data.
       ChannelAPI.getInstance(this).getResponsibleModerators(pushMessage.getId1());
       break;
     case CONVERSATION_CHANGED:
     case CONVERSATION_CLOSED:
       GroupAPI.getInstance(getApplicationContext())
           .getConversation(pushMessage.getId1(), pushMessage.getId2());
       break;
     case CONVERSATION_CHANGED_ALL:
     case CONVERSATION_NEW:
       GroupAPI.getInstance(getApplicationContext()).getConversations(pushMessage.getId1());
       break;
     case CONVERSATION_MESSAGE_NEW:
       // Get conversation message data. Request new messages only.
       messageNumber =
           new GroupDatabaseManager(getApplicationContext())
               .getMaxMessageNumberConversationMessage(pushMessage.getId2());
       GroupAPI.getInstance(this)
           .getConversationMessages(pushMessage.getId1(), pushMessage.getId2(), messageNumber);
       break;
     case CONVERSATION_DELETED:
       notificationSettings = settingsDBM.getGroupNotificationSettings(pushMessage.getId1());
       if (notificationSettings == NotificationSettings.GENERAL) {
         notificationSettings = settingsDBM.getSettings().getNotificationSettings();
       }
       if (!notificationSettings.equals(NotificationSettings.NONE)) {
         sendConversationNotification(pushMessage.getId1());
       }
       new GroupDatabaseManager(getApplicationContext()).deleteConversation(pushMessage.getId2());
       new GroupDatabaseManager(getApplicationContext())
           .setGroupNewEvents(pushMessage.getId1(), true);
       break;
     case PARTICIPANT_NEW:
     case PARTICIPANT_CHANGED:
     case PARTICIPANT_LEFT:
     case PARTICIPANT_REMOVED:
       GroupAPI.getInstance(this).getGroupMembers(pushMessage.getId1());
       break;
     case BALLOT_NEW:
     case BALLOT_CHANGED:
     case BALLOT_CHANGED_ALL:
       GroupAPI.getInstance(this).getBallots(pushMessage.getId1());
       break;
     case BALLOT_OPTION_DELETED:
     case BALLOT_OPTION_ALL:
       GroupAPI.getInstance(this).getOptions(pushMessage.getId1(), pushMessage.getId2(), true);
       break;
     case BALLOT_OPTION_VOTE:
     case BALLOT_OPTION_VOTE_ALL:
       // Do nothing. Load new votes when user enters ballot screen.
       break;
     case GROUP_CHANGED:
       GroupAPI.getInstance(this).getGroup(pushMessage.getId1());
       break;
   }
 }