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_SELECTED_COLOR = "selected_color";
44    protected static final String KEY_COLUMNS = "columns";
45    protected static final String KEY_SIZE = "size";
46
47    protected int mTitleResId = R.string.color_picker_default_title;
48    protected int[] mColors = null;
49    protected int mSelectedColor;
50    protected int mColumns;
51    protected int mSize;
52
53    private ColorPickerPalette mPalette;
54    private ProgressBar mProgress;
55
56    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