ColorPickerDialog.java revision 2cfe25aefb32ed215f1c661d9670baf276fb7776
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    private static final String KEY_COLORS = "colors";
42    private static final String KEY_CURRENT_COLOR = "current_color";
43    private static final String KEY_COLUMNS = "columns";
44    private static final String KEY_SIZE = "size";
45
46    protected String mTitle;
47    protected int mTitleResId;
48    protected int[] mColors;
49    protected int mSelectedColor;
50    protected int mColumns;
51    protected int mSize;
52    private ColorPickerPalette mPalette;
53    private ProgressBar mProgress;
54
55    protected OnColorSelectedListener mListener;
56
57    public ColorPickerDialog() {
58        // Empty constructor required for dialog fragments.
59    }
60
61    public ColorPickerDialog(int titleResId, int[] colors, int selectedColor,
62            int columns, int size) {
63        mTitleResId = titleResId;
64        mColors = colors;
65        mSelectedColor = selectedColor;
66        mColumns = columns;
67        mSize = size;
68    }
69
70    public void setOnColorSelectedListener(OnColorSelectedListener listener) {
71        mListener = listener;
72    }
73
74    @Override
75    public void onCreate(Bundle savedInstanceState) {
76        super.onCreate(savedInstanceState);
77        setRetainInstance(true);
78    }
79
80    @Override
81    public Dialog onCreateDialog(Bundle savedInstanceState) {
82        final Activity activity = getActivity();
83
84        if (savedInstanceState != null) {
85            mColors = (int[]) savedInstanceState.getSerializable(KEY_COLORS);
86            mSelectedColor = savedInstanceState.getInt(KEY_CURRENT_COLOR);
87            mColumns = savedInstanceState.getInt(KEY_COLUMNS);
88            mSize = savedInstanceState.getInt(KEY_SIZE);
89        }
90
91        View view = LayoutInflater.from(getActivity()).inflate(R.layout.color_picker_dialog, null);
92        mProgress = (ProgressBar) view.findViewById(android.R.id.progress);
93        mPalette = (ColorPickerPalette) view.findViewById(R.id.color_picker);
94        mPalette.init(mSize, mColumns, this);
95
96        if (mColors != null) {
97            showPalette();
98        }
99
100        if (mTitle == null) {
101            mTitle = activity.getString(mTitleResId);
102        }
103
104        mAlertDialog = new AlertDialog.Builder(activity)
105            .setTitle(mTitle)
106            .setView(view)
107            .create();
108
109        return mAlertDialog;
110    }
111
112    @Override
113    public void onColorSelected(int color) {
114        if (mListener != null) {
115            mListener.onColorSelected(color);
116        }
117
118        if (getTargetFragment() instanceof OnColorSelectedListener) {
119            final OnColorSelectedListener listener =
120                    (OnColorSelectedListener) getTargetFragment();
121            listener.onColorSelected(color);
122        }
123
124        mSelectedColor = color;
125        dismiss();
126    }
127
128    public void showPalette() {
129        mProgress.setVisibility(View.GONE);
130        mPalette.setVisibility(View.VISIBLE);
131        mPalette.drawPalette(mColors, mSelectedColor);
132    }
133
134    public void showProgress() {
135        mProgress.setVisibility(View.VISIBLE);
136        mPalette.setVisibility(View.GONE);
137    }
138
139    public void setColors(int[] colors) {
140        if (colors == null) {
141            return;
142        }
143        mColors = colors;
144        showPalette();
145    }
146
147    public void setColors(int[] colors, int selectedColor) {
148        mSelectedColor = selectedColor;
149        setColors(colors);
150    }
151
152    public void setSelectedColor(int color) {
153        if (mSelectedColor != color) {
154            setColors(mColors, color);
155        }
156    }
157
158    public int[] getColors() {
159        return mColors;
160    }
161
162    public int getSelectedColor() {
163        return mSelectedColor;
164    }
165
166    @Override
167    public void onSaveInstanceState(Bundle outState) {
168        super.onSaveInstanceState(outState);
169        outState.putSerializable(KEY_COLORS, mColors);
170        outState.putInt(KEY_CURRENT_COLOR, mSelectedColor);
171        outState.putInt(KEY_COLUMNS, mColumns);
172        outState.putInt(KEY_SIZE, mSize);
173    }
174
175    @Override
176    public void onDestroyView() {
177        if (getDialog() != null && getRetainInstance()) {
178            getDialog().setDismissMessage(null);
179        }
180        super.onDestroyView();
181    }
182}
183