Android Open Source - appboy-android-sdk Feed Categories Fragment

From Project

Back to project page appboy-android-sdk.


The source code is released under:

Copyright (c) 2014 Appboy, Inc. All rights reserved. * Use of source code or binaries contained within Appboy's Android SDK is permitted only to enable use of the Appboy platform by customers of Appb...

If you think the Android project appboy-android-sdk listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.appboy.sample;
//from   w ww . j ava2  s. co m
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;

import com.appboy.enums.CardCategory;

import java.util.Arrays;
import java.util.EnumSet;
import java.util.List;
import java.util.Locale;

public class FeedCategoriesFragment extends DialogFragment {
  public static final String CATEGORIES_STRING= "categories";

  /* The activity that creates an instance of this dialog fragment must
     * implement this interface in order to receive event callbacks.
     * Each method passes the DialogFragment in case the host needs to query it. */
  public interface NoticeDialogListener {
    public void onDialogPositiveClick(FeedCategoriesFragment dialog);

  public FeedCategoriesFragment() {}

  public EnumSet<CardCategory> selectedCategories;

  // Use this instance of the interface to deliver action events
  NoticeDialogListener mListener;
  boolean[] mCategoryIsChecked;

  static final String[] CATEGORIES = {"all", CardCategory.ADVERTISING.toString(), CardCategory.ANNOUNCEMENTS.toString(), CardCategory.NEWS.toString(), CardCategory.SOCIAL.toString()};

  static FeedCategoriesFragment newInstance(EnumSet<CardCategory> categories) {
    FeedCategoriesFragment categoriesFragment = new FeedCategoriesFragment();

    Bundle args = new Bundle();
    args.putSerializable(CATEGORIES_STRING, categories);

    return categoriesFragment;

  // Override the Fragment.onAttach() method to instantiate the NoticeDialogListener
  public void onAttach(Activity activity) {
    // Verify that the host activity implements the callback interface
    try {
      // Instantiate the NoticeDialogListener so we can send events to the host
      mListener = (NoticeDialogListener) activity;
    } catch (ClassCastException e) {
      // The activity doesn't implement the interface, throw exception
      throw new ClassCastException(activity.toString()
          + " must implement NoticeDialogListener");

  public Dialog onCreateDialog(Bundle savedInstanceState) {
    selectedCategories = (EnumSet<CardCategory>)getArguments().getSerializable(CATEGORIES_STRING);
    mCategoryIsChecked = getBooleansFromEnumSet(selectedCategories);

    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

    // Set the dialog title
    builder.setTitle("Choose News Feed's Categories")

        // Specify the list array, the items to be selected by default (the EnumSet from DroidBoyActivity),
        // and the listener through which to receive callbacks when items are selected
        .setMultiChoiceItems(CATEGORIES, mCategoryIsChecked,
            new DialogInterface.OnMultiChoiceClickListener() {
              public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                ListView lv = ((AlertDialog)getDialog()).getListView();
                if (which == 0) {
                  // The "All" option is clicked, we should update all other options to be checked/unchecked.
                  for (int i=0; i<Arrays.asList(CATEGORIES).size(); i++) {
                    lv.setItemChecked(i, isChecked);
                    mCategoryIsChecked[i] = isChecked;
                } else if (which < Arrays.asList(CATEGORIES).size()) {
                  mCategoryIsChecked[which] = isChecked;
                  if (!isChecked) {
                    // When there is an option is unchecked, we should also unchecked the "All" option
                    lv.setItemChecked(0, false);
                    mCategoryIsChecked[0] = false;
        // Set the action buttons
        .setPositiveButton("OK", new DialogInterface.OnClickListener() {
          public void onClick(DialogInterface dialog, int id) {
            selectedCategories = getEnumSetFromBooleans(mCategoryIsChecked);
        .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
          public void onClick(DialogInterface dialog, int id) {
    return builder.create();

  private boolean[] getBooleansFromEnumSet(EnumSet<CardCategory> categories) {
    boolean[] array = new boolean[CATEGORIES.length];
    if (categories.equals(CardCategory.ALL_CATEGORIES)) {
      Arrays.fill(array, true);
      return array;
    } else {
      Arrays.fill(array, false);
      List<String> categoriesStringList = Arrays.asList(CATEGORIES);
      for (int i = 0; i < categoriesStringList.size(); i ++) {
        String category = categoriesStringList.get(i);
        categoriesStringList.set(i, category.toUpperCase(Locale.US));
      for (CardCategory category: categories) {
        int i = categoriesStringList.indexOf(category.toString());
        if (i >= 0) {
          array[i] = true;
        } else {
          Log.i("[FeedCategoriesFragment]: getBooleansFromEnumSet", "Cannot find Category for" +
              category.toString() + "in the categories list.");
      return array;

  private EnumSet<CardCategory> getEnumSetFromBooleans(boolean[] isChecked) {
    EnumSet<CardCategory> set = EnumSet.noneOf(CardCategory.class);
    if (isChecked[0]) {
      set = CardCategory.ALL_CATEGORIES;
    } else {
      for (int i = 1; i < Arrays.asList(CATEGORIES).size(); i ++) {
        if(isChecked[i]) {
      if (set.isEmpty()) {
    return set;

Java Source Code List