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






From Project

Back to project page appboy-android-sdk.

License

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.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
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);
    categoriesFragment.setArguments(args);

    return categoriesFragment;
  }

  // Override the Fragment.onAttach() method to instantiate the NoticeDialogListener
  @Override
  public void onAttach(Activity activity) {
    super.onAttach(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");
    }
  }

  @Override
  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() {
              @Override
              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() {
          @Override
          public void onClick(DialogInterface dialog, int id) {
            selectedCategories = getEnumSetFromBooleans(mCategoryIsChecked);
            mListener.onDialogPositiveClick(FeedCategoriesFragment.this);
          }
        })
        .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
          @Override
          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]) {
          set.add(CardCategory.get(CATEGORIES[i]));
        }
      }
      if (set.isEmpty()) {
        set.add(CardCategory.NO_CATEGORY);
      }
    }
    return set;
  }
}




Java Source Code List

com.android.vending.billing.utils.Base64DecoderException.java
com.android.vending.billing.utils.Base64.java
com.android.vending.billing.utils.IabException.java
com.android.vending.billing.utils.IabHelper.java
com.android.vending.billing.utils.IabResult.java
com.android.vending.billing.utils.Inventory.java
com.android.vending.billing.utils.Purchase.java
com.android.vending.billing.utils.Security.java
com.android.vending.billing.utils.SkuDetails.java
com.appboy.AppboyAdmReceiver.java
com.appboy.AppboyGcmReceiver.java
com.appboy.AppboyNotificationUtils.java
com.appboy.helloworld.HelloAppboyActivity.java
com.appboy.sample.AppboyBroadcastReceiver.java
com.appboy.sample.AppboyFragmentActivity.java
com.appboy.sample.CustomAppboyNavigator.java
com.appboy.sample.CustomSlideupManagerListener.java
com.appboy.sample.CustomSlideupViewFactory.java
com.appboy.sample.DecisionFragment.java
com.appboy.sample.DroidBoyActivity.java
com.appboy.sample.DroidGirlActivity.java
com.appboy.sample.DroidboyApplication.java
com.appboy.sample.FeedCategoriesFragment.java
com.appboy.sample.FeedFragmentActivity.java
com.appboy.sample.FeedbackFragmentActivity.java
com.appboy.sample.PreferencesActivity.java
com.appboy.sample.SlideupTesterActivity.java
com.appboy.sample.Test.java
com.appboy.sample.UserProfileDialog.java
com.appboy.sample.util.SharedPrefsUtil.java
com.appboy.ui.AppboyFeedFragment.java
com.appboy.ui.AppboyFeedbackFragment.java
com.appboy.ui.AppboyNavigator.java
com.appboy.ui.AppboyWebViewActivity.java
com.appboy.ui.actions.ActionFactory.java
com.appboy.ui.actions.ActivityAction.java
com.appboy.ui.actions.GooglePlayAppDetailsAction.java
com.appboy.ui.actions.IAction.java
com.appboy.ui.actions.ViewAction.java
com.appboy.ui.actions.WebAction.java
com.appboy.ui.activities.AppboyBaseActivity.java
com.appboy.ui.activities.AppboyBaseFragmentActivity.java
com.appboy.ui.activities.AppboyFeedActivity.java
com.appboy.ui.adapters.AppboyListAdapter.java
com.appboy.ui.configuration.XmlUIConfigurationProvider.java
com.appboy.ui.slideups.AppboySlideupManager.java
com.appboy.ui.slideups.ISlideupManagerListener.java
com.appboy.ui.slideups.ISlideupViewFactory.java
com.appboy.ui.slideups.ISlideupViewLifecycleListener.java
com.appboy.ui.slideups.SlideupCloser.java
com.appboy.ui.slideups.SlideupOperation.java
com.appboy.ui.slideups.SlideupViewWrapper.java
com.appboy.ui.slideups.SwipeDismissTouchListener.java
com.appboy.ui.slideups.TouchAwareSwipeDismissTouchListener.java
com.appboy.ui.support.DrawingUtils.java
com.appboy.ui.support.StringUtils.java
com.appboy.ui.support.UriUtils.java
com.appboy.ui.support.ViewUtils.java
com.appboy.ui.widget.BannerImageCardView.java
com.appboy.ui.widget.BaseCardView.java
com.appboy.ui.widget.CaptionedImageCardView.java
com.appboy.ui.widget.CrossPromotionSmallCardView.java
com.appboy.ui.widget.DefaultCardView.java
com.appboy.ui.widget.ShortNewsCardView.java
com.appboy.ui.widget.StarRatingView.java
com.appboy.ui.widget.TextAnnouncementCardView.java