content: package com.moonpi.swiftnotes.ColorPicker;
  
  import android.app.Activity;
  import android.app.AlertDialog;
  import android.app.Dialog;
  import android.app.DialogFragment;
  import android.app.FragmentManager;
  import android.content.DialogInterface;
  import android.os.Bundle;
  import android.support.annotation.NonNull;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.widget.ProgressBar;
  
  import com.moonpi.swiftnotes.R;
  
  import static com.moonpi.swiftnotes.ColorPicker.ColorPickerSwatch.*;
  
  
  /**
   * A dialog which takes in as input an array of colors and creates a palette allowing the user to
   * select a specific color swatch, which invokes a listener
   */
  public class ColorPickerDialog extends DialogFragment implements OnColorSelectedListener {
  
      public static final int SIZE_LARGE = 1;
      public static final int SIZE_SMALL = 2;
  
      protected AlertDialog mAlertDialog;
  
      protected static final String KEY_TITLE_ID = "title_id";
      protected static final String KEY_COLORS = "colors";
      protected static final String KEY_COLOR_CONTENT_DESCRIPTIONS = "color_content_descriptions";
      protected static final String KEY_SELECTED_COLOR = "selected_color";
      protected static final String KEY_COLUMNS = "columns";
      protected static final String KEY_SIZE = "size";
  
      protected int mTitleResId = R.string.action_note_colour;
      protected int[] mColors = null;
      protected String[] mColorContentDescriptions = null;
      protected int mSelectedColor;
      protected int mColumns;
      protected int mSize;
  
      protected boolean isShowing = false;
  
      private ColorPickerPalette mPalette;
      private ProgressBar mProgress;
  
      protected OnColorSelectedListener mListener;
  
      public ColorPickerDialog() {
          // Empty constructor required for dialog fragments
      }
  
      public static ColorPickerDialog newInstance(int titleResId, int[] colors, int selectedColor,
              int columns, int size) {
  
          ColorPickerDialog ret = new ColorPickerDialog();
          ret.initialize(titleResId, colors, selectedColor, columns, size);
          return ret;
      }
  
      public void initialize(int titleResId, int[] colors, int selectedColor, int columns, int size) {
          setArguments(titleResId, columns, size);
          setColors(colors, selectedColor);
      }
  
      public void setArguments(int titleResId, int columns, int size) {
          Bundle bundle = new Bundle();
          bundle.putInt(KEY_TITLE_ID, titleResId);
          bundle.putInt(KEY_COLUMNS, columns);
          bundle.putInt(KEY_SIZE, size);
          setArguments(bundle);
      }
  
  
      @Override
      public void onSaveInstanceState(@NonNull Bundle outState) {
          super.onSaveInstanceState(outState);
          outState.putIntArray(KEY_COLORS, mColors);
          outState.putSerializable(KEY_SELECTED_COLOR, mSelectedColor);
          outState.putStringArray(KEY_COLOR_CONTENT_DESCRIPTIONS, mColorContentDescriptions);
      }
  
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
  
          if (getArguments() != null) {
              mTitleResId = getArguments().getInt(KEY_TITLE_ID);
              mColumns = getArguments().getInt(KEY_COLUMNS);
              mSize = getArguments().getInt(KEY_SIZE);
          }
  
          if (savedInstanceState != null) {
              mColors = savedInstanceState.getIntArray(KEY_COLORS);
              mSelectedColor = (Integer) savedInstanceState.getSerializable(KEY_SELECTED_COLOR);
              mColorContentDescriptions = savedInstanceState.getStringArray(
                      KEY_COLOR_CONTENT_DESCRIPTIONS);
          }
      }
  
  
      @Override
      public Dialog onCreateDialog(Bundle savedInstanceState) {
          final Activity activity = getActivity();
  
          View view = LayoutInflater.from(getActivity()).inflate(R.layout.color_picker_dialog, null);
          mProgress = (ProgressBar) view.findViewById(android.R.id.progress);
          mPalette = (ColorPickerPalette) view.findViewById(R.id.color_picker);
          mPalette.init(mSize, mColumns, this);
  
          if (mColors != null)
              showPaletteView();
  
          mAlertDialog = new AlertDialog.Builder(activity)
              .setTitle(mTitleResId)
              .setView(view)
              .create();
  
          return mAlertDialog;
      }
  
  
      public void setOnColorSelectedListener(OnColorSelectedListener listener) {
          mListener = listener;
      }
  
      @Override
      public void onColorSelected(int color) {
          if (mListener != null)
              mListener.onColorSelected(color);
  
          if (getTargetFragment() instanceof OnColorSelectedListener) {
              final OnColorSelectedListener listener =
                      (OnColorSelectedListener) getTargetFragment();
              listener.onColorSelected(color);
          }
  
          if (color != mSelectedColor) {
              mSelectedColor = color;
              // Redraw palette to show checkmark on newly selected color before dismissing
              mPalette.drawPalette(mColors, mSelectedColor);
          }
  
          dismiss();
      }
  
      public void showPaletteView() {
          if (mProgress != null && mPalette != null) {
              mProgress.setVisibility(View.GONE);
              refreshPalette();
              mPalette.setVisibility(View.VISIBLE);
          }
      }
  
      public void showProgressBarView() {
          if (mProgress != null && mPalette != null) {
              mProgress.setVisibility(View.VISIBLE);
              mPalette.setVisibility(View.GONE);
          }
      }
  
      public void setColors(int[] colors, int selectedColor) {
          if (mColors != colors || mSelectedColor != selectedColor) {
              mColors = colors;
              mSelectedColor = selectedColor;
              refreshPalette();
          }
      }
  
      public void setColors(int[] colors) {
          if (mColors != colors) {
              mColors = colors;
              refreshPalette();
          }
      }
  
      public void setSelectedColor(int color) {
          if (mSelectedColor != color) {
              mSelectedColor = color;
              refreshPalette();
          }
      }
  
      public void setColorContentDescriptions(String[] colorContentDescriptions) {
          if (mColorContentDescriptions != colorContentDescriptions) {
              mColorContentDescriptions = colorContentDescriptions;
              refreshPalette();
          }
      }
  
      private void refreshPalette() {
          if (mPalette != null && mColors != null) {
              mPalette.drawPalette(mColors, mSelectedColor, mColorContentDescriptions);
          }
      }
  
      public int[] getColors() {
          return mColors;
      }
  
      public int getSelectedColor() {
          return mSelectedColor;
      }
  
  
      @Override
      public void show(@NonNull FragmentManager manager, String tag) {
          if (isShowing)
              return;
  
          super.show(manager, tag);
          isShowing = true;
      }
  
      @Override
      public void onDismiss(DialogInterface dialog) {
          isShowing = false;
          super.onDismiss(dialog);
      }
  
  
      public boolean isDialogShowing() {
          return isShowing;
      }
  }
  