Moving colorpicker items into its own library.

Change-Id: Ib27e42df737b0dfd3946e0550dcd28a1576b8f24
diff --git a/Android.mk b/Android.mk
index 1147f05..b9c730c 100644
--- a/Android.mk
+++ b/Android.mk
@@ -3,7 +3,9 @@
 
 # Include res dir from chips
 chips_dir := ../../../frameworks/ex/chips/res
-res_dirs := $(chips_dir) res
+color_picker_dir := ../../../frameworks/opt/colorpicker/res
+
+res_dirs := $(chips_dir) $(color_picker_dir) res
 src_dirs := src
 
 LOCAL_EMMA_COVERAGE_FILTER := +com.android.calendar.*
@@ -22,6 +24,7 @@
 LOCAL_STATIC_JAVA_LIBRARIES := \
 		android-common \
 		android-common-chips \
+		colorpicker \
 		android-support-v4 \
 		calendar-common
 
@@ -35,6 +38,7 @@
 
 LOCAL_AAPT_FLAGS := --auto-add-overlay
 LOCAL_AAPT_FLAGS += --extra-packages com.android.ex.chips
+LOCAL_AAPT_FLAGS += --extra-packages com.android.colorpicker
 
 include $(BUILD_PACKAGE)
 
diff --git a/project.properties b/project.properties
index a960502..4f053a4 100644
--- a/project.properties
+++ b/project.properties
@@ -15,3 +15,4 @@
 android.library=false
 android.library.reference.1=../../../frameworks/ex/chips
 android.library.reference.2=../../../frameworks/opt/calendar
+android.library.reference.3=../../../frameworks/opt/colorpicker
diff --git a/res/drawable-hdpi/ic_colorpicker_swatch_selected.png b/res/drawable-hdpi/ic_colorpicker_swatch_selected.png
deleted file mode 100644
index 3cbfe1a..0000000
--- a/res/drawable-hdpi/ic_colorpicker_swatch_selected.png
+++ /dev/null
Binary files differ
diff --git a/res/drawable-mdpi/ic_colorpicker_swatch_selected.png b/res/drawable-mdpi/ic_colorpicker_swatch_selected.png
deleted file mode 100644
index acbdeca..0000000
--- a/res/drawable-mdpi/ic_colorpicker_swatch_selected.png
+++ /dev/null
Binary files differ
diff --git a/res/drawable-xhdpi/ic_colorpicker_swatch_selected.png b/res/drawable-xhdpi/ic_colorpicker_swatch_selected.png
deleted file mode 100644
index 812ff2c..0000000
--- a/res/drawable-xhdpi/ic_colorpicker_swatch_selected.png
+++ /dev/null
Binary files differ
diff --git a/res/drawable/color_picker_swatch.xml b/res/drawable/color_picker_swatch.xml
deleted file mode 100644
index db71091..0000000
--- a/res/drawable/color_picker_swatch.xml
+++ /dev/null
@@ -1,16 +0,0 @@
-<?xml version="1.0" encoding="utf-8"?>
-<!-- Copyright (C) 2013 The Android Open Source Project
-
-     Licensed under the Apache License, Version 2.0 (the "License");
-     you may not use this file except in compliance with the License.
-     You may obtain a copy of the License at
-
-          http://www.apache.org/licenses/LICENSE-2.0
-
-     Unless required by applicable law or agreed to in writing, software
-     distributed under the License is distributed on an "AS IS" BASIS,
-     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-     See the License for the specific language governing permissions and
-     limitations under the License.
--->
-<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval"  />
\ No newline at end of file
diff --git a/res/layout-sw600dp/edit_event_1.xml b/res/layout-sw600dp/edit_event_1.xml
index d15188a..2a38340 100644
--- a/res/layout-sw600dp/edit_event_1.xml
+++ b/res/layout-sw600dp/edit_event_1.xml
@@ -82,8 +82,8 @@
                 android:textColor="#FFFFFFFF" />
             <ImageButton
                 android:id="@+id/change_color_existing_event"
-                style="@style/EditEventColorImageButton" 
-                android:visibility="gone" />
+                style="@style/EditEventColorImageButton"
+                android:visibility="gone"  />
         </LinearLayout>
     </TableRow>
 
diff --git a/res/layout/color_picker_dialog.xml b/res/layout/color_picker_dialog.xml
deleted file mode 100644
index 44518c0..0000000
--- a/res/layout/color_picker_dialog.xml
+++ /dev/null
@@ -1,45 +0,0 @@
-<?xml version="1.0" encoding="utf-8"?>
-<!-- Copyright (C) 2013 The Android Open Source Project
-
-     Licensed under the Apache License, Version 2.0 (the "License");
-     you may not use this file except in compliance with the License.
-     You may obtain a copy of the License at
-
-          http://www.apache.org/licenses/LICENSE-2.0
-
-     Unless required by applicable law or agreed to in writing, software
-     distributed under the License is distributed on an "AS IS" BASIS,
-     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-     See the License for the specific language governing permissions and
-     limitations under the License.
--->
-<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
-    android:layout_width="wrap_content"
-    android:layout_height="wrap_content"
-    android:layout_gravity="center"
-    android:gravity="center" >
-
-    <FrameLayout
-        android:layout_width="wrap_content"
-        android:layout_height="wrap_content"
-        android:layout_gravity="center"
-        android:gravity="center"
-        android:padding="28dp" >
-
-        <ProgressBar
-            android:id="@android:id/progress"
-            style="?android:attr/progressBarStyleLarge"
-            android:layout_width="wrap_content"
-            android:layout_height="wrap_content"
-            android:layout_gravity="center"
-            android:gravity="center" />
-
-        <com.android.calendar.color.ColorPickerPalette
-            android:id="@+id/color_picker"
-            android:layout_width="wrap_content"
-            android:layout_height="wrap_content"
-            android:layout_gravity="center"
-            android:gravity="center"
-            android:visibility="gone" />
-    </FrameLayout>
-</ScrollView>
\ No newline at end of file
diff --git a/res/layout/edit_event_1.xml b/res/layout/edit_event_1.xml
index 4d8d793..af7174f 100644
--- a/res/layout/edit_event_1.xml
+++ b/res/layout/edit_event_1.xml
@@ -40,7 +40,7 @@
             android:gravity="center_vertical" />
         <ImageButton
             android:id="@+id/change_color_new_event"
-            style="@style/EditEventColorImageButton" 
+            style="@style/EditEventColorImageButton"
             android:visibility="invisible" />
     </LinearLayout>
 
@@ -78,8 +78,8 @@
         </LinearLayout>
         <ImageButton
             android:id="@+id/change_color_existing_event"
-            style="@style/EditEventColorImageButton" 
-            android:visibility="gone" />
+            style="@style/EditEventColorImageButton"
+            android:visibility="gone"  />
     </LinearLayout>
 
     <!-- WHAT -->
diff --git a/res/values/dimens.xml b/res/values/dimens.xml
index 0f0d4e5..4965a00 100644
--- a/res/values/dimens.xml
+++ b/res/values/dimens.xml
@@ -52,11 +52,5 @@
     <dimen name="calendar_controls_height">300dip</dimen>
     <dimen name="agenda_item_right_margin">4dip</dimen>
     <dimen name="today_icon_text_size">14sp</dimen>
-
-    <dimen name="color_swatch_large">64dip</dimen>
-    <dimen name="color_swatch_small">48dip</dimen>
-    <dimen name="color_swatch_margins_large">8dip</dimen>
-    <dimen name="color_swatch_margins_small">4dip</dimen>
-
     <dimen name="color_view_touch_area_increase">16dp</dimen>
 </resources>
diff --git a/src/com/android/calendar/CalendarColorPickerDialog.java b/src/com/android/calendar/CalendarColorPickerDialog.java
index cc98da7..09183f7 100644
--- a/src/com/android/calendar/CalendarColorPickerDialog.java
+++ b/src/com/android/calendar/CalendarColorPickerDialog.java
@@ -27,15 +27,12 @@
 import android.provider.CalendarContract.Colors;
 import android.util.SparseIntArray;
 
-import com.android.calendar.CalendarController.EventType;
-import com.android.calendar.CalendarController.ViewType;
-import com.android.calendar.color.HsvColorComparator;
-import com.android.calendar.color.ColorPickerDialog;
-import com.android.calendar.color.ColorPickerSwatch.OnColorSelectedListener;
+import com.android.colorpicker.ColorPickerDialog;
+import com.android.colorpicker.ColorPickerSwatch.OnColorSelectedListener;
+import com.android.colorpicker.HsvColorComparator;
 
 import java.util.ArrayList;
 import java.util.Arrays;
-import java.util.Iterator;
 
 public class CalendarColorPickerDialog extends ColorPickerDialog {
 
diff --git a/src/com/android/calendar/EventInfoFragment.java b/src/com/android/calendar/EventInfoFragment.java
index cfa415f..7ef6424 100644
--- a/src/com/android/calendar/EventInfoFragment.java
+++ b/src/com/android/calendar/EventInfoFragment.java
@@ -97,8 +97,7 @@
 import com.android.calendar.CalendarEventModel.Attendee;
 import com.android.calendar.CalendarEventModel.ReminderEntry;
 import com.android.calendar.alerts.QuickResponseActivity;
-import com.android.calendar.color.ColorPickerSwatch.OnColorSelectedListener;
-import com.android.calendar.color.HsvColorComparator;
+
 import com.android.calendar.event.AttendeesView;
 import com.android.calendar.event.EditEventActivity;
 import com.android.calendar.event.EditEventHelper;
@@ -107,6 +106,8 @@
 import com.android.calendarcommon2.DateException;
 import com.android.calendarcommon2.Duration;
 import com.android.calendarcommon2.EventRecurrence;
+import com.android.colorpicker.ColorPickerSwatch.OnColorSelectedListener;
+import com.android.colorpicker.HsvColorComparator;
 
 import java.util.ArrayList;
 import java.util.Arrays;
diff --git a/src/com/android/calendar/color/ColorPickerPalette.java b/src/com/android/calendar/color/ColorPickerPalette.java
deleted file mode 100644
index dba1eda..0000000
--- a/src/com/android/calendar/color/ColorPickerPalette.java
+++ /dev/null
@@ -1,144 +0,0 @@
-/*
- * Copyright (C) 2013 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package com.android.calendar.color;
-
-import android.content.Context;
-import android.util.AttributeSet;
-import android.view.View;
-import android.view.ViewGroup;
-import android.widget.ImageView;
-import android.widget.TableLayout;
-import android.widget.TableRow;
-
-import com.android.calendar.R;
-import com.android.calendar.color.ColorPickerSwatch.OnColorSelectedListener;
-
-/**
- * A color picker custom view which creates an grid of color squares.  The number of squares per
- * row (and the padding between the squares) is determined by the user.
- */
-public class ColorPickerPalette extends TableLayout {
-
-    public OnColorSelectedListener mOnColorSelectedListener;
-
-    private int mSwatchLength;
-    private int mMarginSize;
-    private int mNumColumns;
-
-    public ColorPickerPalette(Context context, AttributeSet attrs) {
-        super(context, attrs);
-    }
-
-    public ColorPickerPalette(Context context) {
-        super(context);
-    }
-
-    /**
-     * Initialize the size, columns, and listener.  Size should be a pre-defined size (SIZE_LARGE
-     * or SIZE_SMALL) from ColorPickerDialogFragment.
-     */
-    public void init(int size, int columns, OnColorSelectedListener listener) {
-        mNumColumns = columns;
-        if (size == ColorPickerDialog.SIZE_LARGE) {
-            mSwatchLength = getResources().getDimensionPixelSize(R.dimen.color_swatch_large);
-            mMarginSize = getResources().getDimensionPixelSize(R.dimen.color_swatch_margins_large);
-        } else {
-            mSwatchLength = getResources().getDimensionPixelSize(R.dimen.color_swatch_small);
-            mMarginSize = getResources().getDimensionPixelSize(R.dimen.color_swatch_margins_small);
-        }
-        mOnColorSelectedListener = listener;
-    }
-
-    private TableRow createTableRow() {
-        TableRow row = new TableRow(getContext());
-        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(LayoutParams.WRAP_CONTENT,
-                LayoutParams.WRAP_CONTENT);
-        row.setLayoutParams(params);
-        return row;
-    }
-
-    /**
-     * Adds swatches to table in a serpentine format.
-     */
-    public void drawPalette(int[] colors, int selectedColor) {
-
-        if (colors == null) {
-            return;
-        }
-
-        this.removeAllViews();
-        int rowElements = 0;
-        int rowNumber = 0;
-
-        // Fills the table with swatches based on the array of colors.
-        TableRow row = createTableRow();
-        for (int color : colors)  {
-            addSwatchToRow(row, createColorSwatch(color, selectedColor), rowNumber);
-            rowElements++;
-            if (rowElements == mNumColumns) {
-                addView(row);
-                row = createTableRow();
-                rowElements = 0;
-                rowNumber++;
-            }
-        }
-
-        // Create blank views to fill the row if the last row has not been filled.
-        if (rowElements > 0) {
-            while (rowElements != mNumColumns) {
-                addSwatchToRow(row, createBlankSpace(), rowNumber);
-                rowElements++;
-            }
-            addView(row);
-        }
-    }
-
-    /**
-     * Appends a swatch to the end of the row for even-numbered rows (starting with row 0),
-     * to the beginning of a row for odd-numbered rows.
-     */
-    private void addSwatchToRow(TableRow row, View swatch, int rowNumber) {
-        if (rowNumber % 2 == 0) {
-            row.addView(swatch);
-        } else {
-            row.addView(swatch, 0);
-        }
-    }
-
-    /**
-     * Creates a blank space to fill the row.
-     */
-    private ImageView createBlankSpace() {
-        ImageView view = new ImageView(getContext());
-        TableRow.LayoutParams params = new TableRow.LayoutParams(mSwatchLength, mSwatchLength);
-        params.setMargins(mMarginSize, mMarginSize, mMarginSize, mMarginSize);
-        view.setLayoutParams(params);
-        return view;
-    }
-
-    /**
-     * Creates a color swatch.
-     */
-    private ColorPickerSwatch createColorSwatch(int color, int selectedColor) {
-        ColorPickerSwatch view = new ColorPickerSwatch(getContext(), color,
-                color == selectedColor, mOnColorSelectedListener);
-        TableRow.LayoutParams params = new TableRow.LayoutParams(mSwatchLength, mSwatchLength);
-        params.setMargins(mMarginSize, mMarginSize, mMarginSize, mMarginSize);
-        view.setLayoutParams(params);
-        return view;
-    }
-}
diff --git a/src/com/android/calendar/color/ColorPickerSwatch.java b/src/com/android/calendar/color/ColorPickerSwatch.java
deleted file mode 100644
index e8e9586..0000000
--- a/src/com/android/calendar/color/ColorPickerSwatch.java
+++ /dev/null
@@ -1,89 +0,0 @@
-/*
- * Copyright (C) 2013 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package com.android.calendar.color;
-
-import android.content.Context;
-import android.content.res.Resources;
-import android.graphics.PorterDuff.Mode;
-import android.graphics.drawable.Drawable;
-import android.view.View;
-import android.widget.ImageView;
-
-import com.android.calendar.R;
-
-/**
- * Creates a circular swatch of a specified color.  Adds a checkmark if marked as checked.
- */
-public class ColorPickerSwatch extends ImageView implements View.OnClickListener {
-
-    private int mColor;
-    private Drawable mColorDrawable;
-    private Drawable mCheckmark;
-    private OnColorSelectedListener mOnColorSelectedListener;
-
-    /**
-     * Interface for a callback when a color square is selected.
-     */
-    public interface OnColorSelectedListener {
-
-        /**
-         * Called when a specific color square has been selected.
-         */
-        public void onColorSelected(int color);
-    }
-
-    /**
-     * @param context
-     */
-    public ColorPickerSwatch(Context context) {
-        super(context);
-    }
-
-    public ColorPickerSwatch(Context context, int color, boolean checked,
-            OnColorSelectedListener listener) {
-        super(context);
-        setScaleType(ScaleType.FIT_XY);
-        Resources res = context.getResources();
-        mColorDrawable = res.getDrawable(R.drawable.color_picker_swatch);
-        mCheckmark = res.getDrawable(R.drawable.ic_colorpicker_swatch_selected);
-        mOnColorSelectedListener = listener;
-        setColor(color);
-        setChecked(checked);
-        setOnClickListener(this);
-    }
-
-    protected void setColor(int color) {
-        mColor = color;
-        mColorDrawable.setColorFilter(color, Mode.SRC_ATOP);
-        setBackgroundDrawable(mColorDrawable);
-    }
-
-    private void setChecked(boolean checked) {
-        if (checked) {
-            setImageDrawable(mCheckmark);
-        } else {
-            setImageDrawable(null);
-        }
-    }
-
-    @Override
-    public void onClick(View v) {
-        if (mOnColorSelectedListener != null) {
-            mOnColorSelectedListener.onColorSelected(mColor);
-        }
-    }
-}
diff --git a/src/com/android/calendar/color/HsvColorComparator.java b/src/com/android/calendar/color/HsvColorComparator.java
deleted file mode 100644
index eed7dfe..0000000
--- a/src/com/android/calendar/color/HsvColorComparator.java
+++ /dev/null
@@ -1,61 +0,0 @@
-/*
- * Copyright (C) 2013 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package com.android.calendar.color;
-
-import android.graphics.Color;
-
-import java.util.Comparator;
-
-/**
- * A color comparator which compares based on hue, saturation, and value.
- */
-public class HsvColorComparator implements Comparator<Integer> {
-
-    @Override
-    public int compare(Integer lhs, Integer rhs) {
-        float[] hsv = new float[3];
-        Color.colorToHSV(lhs, hsv);
-        float hue1 = hsv[0];
-        float sat1 = hsv[1];
-        float val1 = hsv[2];
-
-        float[] hsv2 = new float[3];
-        Color.colorToHSV(rhs, hsv2);
-        float hue2 = hsv2[0];
-        float sat2 = hsv2[1];
-        float val2 = hsv2[2];
-
-        if (hue1 < hue2) {
-            return 1;
-        } else if (hue1 > hue2) {
-            return -1;
-        } else {
-            if (sat1 < sat2) {
-                return 1;
-            } else if (sat1 > sat2) {
-                return -1;
-            } else {
-                if (val1 < val2) {
-                    return 1;
-                } else if (val1 > val2) {
-                    return -1;
-                }
-            }
-        }
-        return 0;
-    }
-}
diff --git a/src/com/android/calendar/event/EditEventFragment.java b/src/com/android/calendar/event/EditEventFragment.java
index 0ea2487..0960a0a 100644
--- a/src/com/android/calendar/event/EditEventFragment.java
+++ b/src/com/android/calendar/event/EditEventFragment.java
@@ -62,8 +62,8 @@
 import com.android.calendar.DeleteEventHelper;
 import com.android.calendar.R;
 import com.android.calendar.Utils;
-import com.android.calendar.color.ColorPickerSwatch.OnColorSelectedListener;
-import com.android.calendar.color.HsvColorComparator;
+import com.android.colorpicker.ColorPickerSwatch.OnColorSelectedListener;
+import com.android.colorpicker.HsvColorComparator;
 
 import java.io.Serializable;
 import java.util.ArrayList;
diff --git a/src/com/android/calendar/event/EditEventView.java b/src/com/android/calendar/event/EditEventView.java
index 3a33d85..549586e 100644
--- a/src/com/android/calendar/event/EditEventView.java
+++ b/src/com/android/calendar/event/EditEventView.java
@@ -20,7 +20,6 @@
 import android.app.AlertDialog;
 import android.app.DatePickerDialog;
 import android.app.DatePickerDialog.OnDateSetListener;
-import android.app.FragmentManager;
 import android.app.ProgressDialog;
 import android.app.Service;
 import android.app.TimePickerDialog;
@@ -62,7 +61,6 @@
 import android.widget.CheckBox;
 import android.widget.CompoundButton;
 import android.widget.DatePicker;
-import android.widget.ImageButton;
 import android.widget.LinearLayout;
 import android.widget.MultiAutoCompleteTextView;
 import android.widget.RadioButton;
@@ -85,7 +83,6 @@
 import com.android.calendar.TimezoneAdapter;
 import com.android.calendar.TimezoneAdapter.TimezoneRow;
 import com.android.calendar.Utils;
-import com.android.calendar.color.ColorPickerSwatch.OnColorSelectedListener;
 import com.android.calendar.event.EditEventHelper.EditDoneRunnable;
 import com.android.calendarcommon2.EventRecurrence;
 import com.android.common.Rfc822InputFilter;
diff --git a/src/com/android/calendar/event/EventColorPickerDialog.java b/src/com/android/calendar/event/EventColorPickerDialog.java
index 23c2d78..7eac6b0 100644
--- a/src/com/android/calendar/event/EventColorPickerDialog.java
+++ b/src/com/android/calendar/event/EventColorPickerDialog.java
@@ -21,7 +21,7 @@
 import android.os.Bundle;
 
 import com.android.calendar.R;
-import com.android.calendar.color.ColorPickerDialog;
+import com.android.colorpicker.ColorPickerDialog;
 
 /**
  * A dialog which displays event colors, with an additional button for the calendar color.