1/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.colorpicker;
18
19import android.app.Activity;
20import android.app.AlertDialog;
21import android.app.Dialog;
22import android.app.DialogFragment;
23import android.os.Bundle;
24import android.view.LayoutInflater;
25import android.view.View;
26import android.widget.ProgressBar;
27
28import com.android.colorpicker.ColorPickerSwatch.OnColorSelectedListener;
29
30/**
31 * A dialog which takes in as input an array of colors and creates a palette allowing the user to
32 * select a specific color swatch, which invokes a listener.
33 */
34public class ColorPickerDialog extends DialogFragment implements OnColorSelectedListener {
35
36    public static final int SIZE_LARGE = 1;
37    public static final int SIZE_SMALL = 2;
38
39    protected AlertDialog mAlertDialog;
40
41    protected static final String KEY_TITLE_ID = "title_id";
42    protected static final String KEY_COLORS = "colors";
43    protected static final String KEY_COLOR_CONTENT_DESCRIPTIONS = "color_content_descriptions";
44    protected static final String KEY_SELECTED_COLOR = "selected_color";
45    protected static final String KEY_COLUMNS = "columns";
46    protected static final String KEY_SIZE = "size";
47
48    protected int mTitleResId = R.string.color_picker_default_title;
49    protected int[] mColors = null;
50    protected String[] mColorContentDescriptions = null;
51    protected int mSelectedColor;
52    protected int mColumns;
53    protected int mSize;
54
55    private ColorPickerPalette mPalette;
56    private ProgressBar mProgress;
57
58    protected OnColorSelectedListener mListener;
59
60    public ColorPickerDialog() {
61        // Empty constructor required for dialog fragments.
62    }
63
64    public static ColorPickerDialog newInstance(int titleResId, int[] colors, int selectedColor,
65            int columns, int size) {
66        ColorPickerDialog ret = new ColorPickerDialog();
67        ret.initialize(titleResId, colors, selectedColor, columns, size);
68        return ret;
69    }
70
71    public void initialize(int titleResId, int[] colors, int selectedColor, int columns, int size) {
72        setArguments(titleResId, columns, size);
73        setColors(colors, selectedColor);
74    }
75
76    public void setArguments(int titleResId, int columns, int size) {
77        Bundle bundle = new Bundle();
78        bundle.putInt(KEY_TITLE_ID, titleResId);
79        bundle.putInt(KEY_COLUMNS, columns);
80        bundle.putInt(KEY_SIZE, size);
81        setArguments(bundle);
82    }
83
84    public void setOnColorSelectedListener(OnColorSelectedListener listener) {
85        mListener = listener;
86    }
87
88    @Override
89    public void onCreate(Bundle savedInstanceState) {
90        super.onCreate(savedInstanceState);
91
92        if (getArguments() != null) {
93            mTitleResId = getArguments().getInt(KEY_TITLE_ID);
94            mColumns = getArguments().getInt(KEY_COLUMNS);
95            mSize = getArguments().getInt(KEY_SIZE);
96        }
97
98        if (savedInstanceState != null) {
99            mColors = savedInstanceState.getIntArray(KEY_COLORS);
100            mSelectedColor = (Integer) savedInstanceState.getSerializable(KEY_SELECTED_COLOR);
101            mColorContentDescriptions = savedInstanceState.getStringArray(
102                    KEY_COLOR_CONTENT_DESCRIPTIONS);
103        }
104    }
105
106    @Override
107    public Dialog onCreateDialog(Bundle savedInstanceState) {
108        final Activity activity = getActivity();
109
110        View view = LayoutInflater.from(getActivity()).inflate(R.layout.color_picker_dialog, null);
111        mProgress = (ProgressBar) view.findViewById(android.R.id.progress);
112        mPalette = (ColorPickerPalette) view.findViewById(R.id.color_picker);
113        mPalette.init(mSize, mColumns, this);
114
115        if (mColors != null) {
116            showPaletteView();
117        }
118
119        mAlertDialog = new AlertDialog.Builder(activity)
120            .setTitle(mTitleResId)
121            .setView(view)
122            .create();
123
124        return mAlertDialog;
125    }
126
127    @Override
128    public void onColorSelected(int color) {
129        if (mListener != null) {
130            mListener.onColorSelected(color);
131        }
132
133        if (getTargetFragment() instanceof OnColorSelectedListener) {
134            final OnColorSelectedListener listener =
135                    (OnColorSelectedListener) getTargetFragment();
136            listener.onColorSelected(color);
137        }
138
139        if (color != mSelectedColor) {
140            mSelectedColor = color;
141            // Redraw palette to show checkmark on newly selected color before dismissing.
142            mPalette.drawPalette(mColors, mSelectedColor);
143        }
144
145        dismiss();
146    }
147
148    public void showPaletteView() {
149        if (mProgress != null && mPalette != null) {
150            mProgress.setVisibility(View.GONE);
151            refreshPalette();
152            mPalette.setVisibility(View.VISIBLE);
153        }
154    }
155
156    public void showProgressBarView() {
157        if (mProgress != null && mPalette != null) {
158            mProgress.setVisibility(View.VISIBLE);
159            mPalette.setVisibility(View.GONE);
160        }
161    }
162
163    public void setColors(int[] colors, int selectedColor) {
164        if (mColors != colors || mSelectedColor != selectedColor) {
165            mColors = colors;
166            mSelectedColor = selectedColor;
167            refreshPalette();
168        }
169    }
170
171    public void setColors(int[] colors) {
172        if (mColors != colors) {
173            mColors = colors;
174            refreshPalette();
175        }
176    }
177
178    public void setSelectedColor(int color) {
179        if (mSelectedColor != color) {
180            mSelectedColor = color;
181            refreshPalette();
182        }
183    }
184
185    public void setColorContentDescriptions(String[] colorContentDescriptions) {
186        if (mColorContentDescriptions != colorContentDescriptions) {
187            mColorContentDescriptions = colorContentDescriptions;
188            refreshPalette();
189        }
190    }
191
192    private void refreshPalette() {
193        if (mPalette != null && mColors != null) {
194            mPalette.drawPalette(mColors, mSelectedColor, mColorContentDescriptions);
195        }
196    }
197
198    public int[] getColors() {
199        return mColors;
200    }
201
202    public int getSelectedColor() {
203        return mSelectedColor;
204    }
205
206    @Override
207    public void onSaveInstanceState(Bundle outState) {
208        super.onSaveInstanceState(outState);
209        outState.putIntArray(KEY_COLORS, mColors);
210        outState.putSerializable(KEY_SELECTED_COLOR, mSelectedColor);
211        outState.putStringArray(KEY_COLOR_CONTENT_DESCRIPTIONS, mColorContentDescriptions);
212    }
213}
214