ColorPickerDialog.java revision dddee2781386a459c0cb8579ef66866e1792e06c
149457ac092071a8f964f7f69156093657ccdc3d0Mathias Agopian/*
249457ac092071a8f964f7f69156093657ccdc3d0Mathias Agopian * Copyright (C) 2013 The Android Open Source Project
349457ac092071a8f964f7f69156093657ccdc3d0Mathias Agopian *
449457ac092071a8f964f7f69156093657ccdc3d0Mathias Agopian * Licensed under the Apache License, Version 2.0 (the "License");
549457ac092071a8f964f7f69156093657ccdc3d0Mathias Agopian * you may not use this file except in compliance with the License.
649457ac092071a8f964f7f69156093657ccdc3d0Mathias Agopian * You may obtain a copy of the License at
749457ac092071a8f964f7f69156093657ccdc3d0Mathias Agopian *
849457ac092071a8f964f7f69156093657ccdc3d0Mathias Agopian *      http://www.apache.org/licenses/LICENSE-2.0
949457ac092071a8f964f7f69156093657ccdc3d0Mathias Agopian *
1049457ac092071a8f964f7f69156093657ccdc3d0Mathias Agopian * Unless required by applicable law or agreed to in writing, software
1149457ac092071a8f964f7f69156093657ccdc3d0Mathias Agopian * distributed under the License is distributed on an "AS IS" BASIS,
1249457ac092071a8f964f7f69156093657ccdc3d0Mathias Agopian * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1349457ac092071a8f964f7f69156093657ccdc3d0Mathias Agopian * See the License for the specific language governing permissions and
1449457ac092071a8f964f7f69156093657ccdc3d0Mathias Agopian * limitations under the License.
1549457ac092071a8f964f7f69156093657ccdc3d0Mathias Agopian */
1649457ac092071a8f964f7f69156093657ccdc3d0Mathias Agopian
1749457ac092071a8f964f7f69156093657ccdc3d0Mathias Agopianpackage com.android.colorpicker;
18a8c386f1c36e916c1df18d41a22104d655a89817Mathias Agopian
1949457ac092071a8f964f7f69156093657ccdc3d0Mathias Agopianimport android.app.Activity;
2049457ac092071a8f964f7f69156093657ccdc3d0Mathias Agopianimport android.app.AlertDialog;
2149457ac092071a8f964f7f69156093657ccdc3d0Mathias Agopianimport android.app.Dialog;
2249457ac092071a8f964f7f69156093657ccdc3d0Mathias Agopianimport android.app.DialogFragment;
2349457ac092071a8f964f7f69156093657ccdc3d0Mathias Agopianimport android.os.Bundle;
2449457ac092071a8f964f7f69156093657ccdc3d0Mathias Agopianimport android.view.LayoutInflater;
2549457ac092071a8f964f7f69156093657ccdc3d0Mathias Agopianimport android.view.View;
2649457ac092071a8f964f7f69156093657ccdc3d0Mathias Agopianimport android.widget.ProgressBar;
2749457ac092071a8f964f7f69156093657ccdc3d0Mathias Agopian
2849457ac092071a8f964f7f69156093657ccdc3d0Mathias Agopianimport com.android.colorpicker.ColorPickerSwatch.OnColorSelectedListener;
2949457ac092071a8f964f7f69156093657ccdc3d0Mathias Agopian
3049457ac092071a8f964f7f69156093657ccdc3d0Mathias Agopian/**
31a8c386f1c36e916c1df18d41a22104d655a89817Mathias Agopian * A dialog which takes in as input an array of colors and creates a palette allowing the user to
3249457ac092071a8f964f7f69156093657ccdc3d0Mathias Agopian * select a specific color swatch, which invokes a listener.
3349457ac092071a8f964f7f69156093657ccdc3d0Mathias Agopian */
3449457ac092071a8f964f7f69156093657ccdc3d0Mathias Agopianpublic class ColorPickerDialog extends DialogFragment implements OnColorSelectedListener {
3549457ac092071a8f964f7f69156093657ccdc3d0Mathias Agopian
3649457ac092071a8f964f7f69156093657ccdc3d0Mathias Agopian    public static final int SIZE_LARGE = 1;
3749457ac092071a8f964f7f69156093657ccdc3d0Mathias Agopian    public static final int SIZE_SMALL = 2;
3849457ac092071a8f964f7f69156093657ccdc3d0Mathias Agopian
3949457ac092071a8f964f7f69156093657ccdc3d0Mathias Agopian    protected AlertDialog mAlertDialog;
4049457ac092071a8f964f7f69156093657ccdc3d0Mathias Agopian
4149457ac092071a8f964f7f69156093657ccdc3d0Mathias Agopian    protected static final String KEY_TITLE_ID = "title_id";
4249457ac092071a8f964f7f69156093657ccdc3d0Mathias Agopian    protected static final String KEY_COLORS = "colors";
4349457ac092071a8f964f7f69156093657ccdc3d0Mathias Agopian    protected static final String KEY_SELECTED_COLOR = "selected_color";
4449457ac092071a8f964f7f69156093657ccdc3d0Mathias Agopian    protected static final String KEY_COLUMNS = "columns";
4549457ac092071a8f964f7f69156093657ccdc3d0Mathias Agopian    protected static final String KEY_SIZE = "size";
4649457ac092071a8f964f7f69156093657ccdc3d0Mathias Agopian
4749457ac092071a8f964f7f69156093657ccdc3d0Mathias Agopian    protected int mTitleResId = R.string.color_picker_default_title;
4849457ac092071a8f964f7f69156093657ccdc3d0Mathias Agopian    protected int[] mColors = null;
49a8c386f1c36e916c1df18d41a22104d655a89817Mathias Agopian    protected int mSelectedColor;
5049457ac092071a8f964f7f69156093657ccdc3d0Mathias Agopian    protected int mColumns;
5149457ac092071a8f964f7f69156093657ccdc3d0Mathias Agopian    protected int mSize;
5249457ac092071a8f964f7f69156093657ccdc3d0Mathias Agopian
5349457ac092071a8f964f7f69156093657ccdc3d0Mathias Agopian    private ColorPickerPalette mPalette;
5449457ac092071a8f964f7f69156093657ccdc3d0Mathias Agopian    private ProgressBar mProgress;
5549457ac092071a8f964f7f69156093657ccdc3d0Mathias Agopian
5649457ac092071a8f964f7f69156093657ccdc3d0Mathias Agopian    protected OnColorSelectedListener mListener;
57
58    public ColorPickerDialog() {
59        // Empty constructor required for dialog fragments.
60    }
61
62    public static ColorPickerDialog newInstance(int titleResId, int[] colors, int selectedColor,
63            int columns, int size) {
64        ColorPickerDialog ret = new ColorPickerDialog();
65        ret.initialize(titleResId, colors, selectedColor, columns, size);
66        return ret;
67    }
68
69    public void initialize(int titleResId, int[] colors, int selectedColor, int columns, int size) {
70        setArguments(titleResId, columns, size);
71        setColors(colors, selectedColor);
72    }
73
74    public void setArguments(int titleResId, int columns, int size) {
75        Bundle bundle = new Bundle();
76        bundle.putInt(KEY_TITLE_ID, titleResId);
77        bundle.putInt(KEY_COLUMNS, columns);
78        bundle.putInt(KEY_SIZE, size);
79        setArguments(bundle);
80    }
81
82    public void setOnColorSelectedListener(OnColorSelectedListener listener) {
83        mListener = listener;
84    }
85
86    @Override
87    public void onCreate(Bundle savedInstanceState) {
88        super.onCreate(savedInstanceState);
89
90        if (getArguments() != null) {
91            mTitleResId = getArguments().getInt(KEY_TITLE_ID);
92            mColumns = getArguments().getInt(KEY_COLUMNS);
93            mSize = getArguments().getInt(KEY_SIZE);
94        }
95
96        if (savedInstanceState != null) {
97            mColors = savedInstanceState.getIntArray(KEY_COLORS);
98            mSelectedColor = (Integer) savedInstanceState.getSerializable(KEY_SELECTED_COLOR);
99        }
100    }
101
102    @Override
103    public Dialog onCreateDialog(Bundle savedInstanceState) {
104        final Activity activity = getActivity();
105
106        View view = LayoutInflater.from(getActivity()).inflate(R.layout.color_picker_dialog, null);
107        mProgress = (ProgressBar) view.findViewById(android.R.id.progress);
108        mPalette = (ColorPickerPalette) view.findViewById(R.id.color_picker);
109        mPalette.init(mSize, mColumns, this);
110
111        if (mColors != null) {
112            showPaletteView();
113        }
114
115        mAlertDialog = new AlertDialog.Builder(activity)
116            .setTitle(mTitleResId)
117            .setView(view)
118            .create();
119
120        return mAlertDialog;
121    }
122
123    @Override
124    public void onColorSelected(int color) {
125        if (mListener != null) {
126            mListener.onColorSelected(color);
127        }
128
129        if (getTargetFragment() instanceof OnColorSelectedListener) {
130            final OnColorSelectedListener listener =
131                    (OnColorSelectedListener) getTargetFragment();
132            listener.onColorSelected(color);
133        }
134
135        if (color != mSelectedColor) {
136            mSelectedColor = color;
137            // Redraw palette to show checkmark on newly selected color before dismissing.
138            mPalette.drawPalette(mColors, mSelectedColor);
139        }
140
141        dismiss();
142    }
143
144    public void showPaletteView() {
145        if (mProgress != null && mPalette != null) {
146            mProgress.setVisibility(View.GONE);
147            refreshPalette();
148            mPalette.setVisibility(View.VISIBLE);
149        }
150    }
151
152    public void showProgressBarView() {
153        if (mProgress != null && mPalette != null) {
154            mProgress.setVisibility(View.VISIBLE);
155            mPalette.setVisibility(View.GONE);
156        }
157    }
158
159    public void setColors(int[] colors, int selectedColor) {
160        if (mColors != colors || mSelectedColor != selectedColor) {
161            mColors = colors;
162            mSelectedColor = selectedColor;
163            refreshPalette();
164        }
165    }
166
167    public void setColors(int[] colors) {
168        if (mColors != colors) {
169            mColors = colors;
170            refreshPalette();
171        }
172    }
173
174    public void setSelectedColor(int color) {
175        if (mSelectedColor != color) {
176            mSelectedColor = color;
177            refreshPalette();
178        }
179    }
180
181    private void refreshPalette() {
182        if (mPalette != null && mColors != null) {
183            mPalette.drawPalette(mColors, mSelectedColor);
184        }
185    }
186
187    public int[] getColors() {
188        return mColors;
189    }
190
191    public int getSelectedColor() {
192        return mSelectedColor;
193    }
194
195    @Override
196    public void onSaveInstanceState(Bundle outState) {
197        super.onSaveInstanceState(outState);
198        outState.putIntArray(KEY_COLORS, mColors);
199        outState.putSerializable(KEY_SELECTED_COLOR, mSelectedColor);
200    }
201}
202