| 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | package org.chromium.ui; |
| 6 | |
| 7 | import android.app.AlertDialog; |
| 8 | import android.app.Dialog; |
| 9 | import android.content.Context; |
| 10 | import android.content.DialogInterface; |
| 11 | import android.view.LayoutInflater; |
| 12 | import android.view.View; |
| 13 | import android.widget.Button; |
| 14 | import android.widget.TextView; |
| 15 | |
| 16 | /** |
| 17 | * UI for the color chooser that shows on the Android platform as a result of |
| 18 | * <input type=color > form element. |
| 19 | */ |
| 20 | public class ColorPickerDialog extends AlertDialog implements OnColorChangedListener { |
| 21 | private final ColorPickerAdvanced mAdvancedColorPicker; |
| 22 | |
| 23 | private final ColorPickerSimple mSimpleColorPicker; |
| 24 | |
| 25 | private final Button mMoreButton; |
| 26 | |
| 27 | // The view up in the corner that shows the user the color they've currently selected. |
| 28 | private final View mCurrentColorView; |
| 29 | |
| 30 | private final OnColorChangedListener mListener; |
| 31 | |
| 32 | private final int mInitialColor; |
| 33 | |
| 34 | private int mCurrentColor; |
| 35 | |
| 36 | /** |
| 37 | * @param context The context the dialog is to run in. |
| 38 | * @param theme The theme to display the dialog in. |
| 39 | * @param listener The object to notify when the color is set. |
| 40 | * @param color The initial color to set. |
| 41 | */ |
| 42 | public ColorPickerDialog(Context context, OnColorChangedListener listener, int color) { |
| 43 | super(context, 0); |
| 44 | |
| 45 | mListener = listener; |
| 46 | mInitialColor = color; |
| 47 | mCurrentColor = mInitialColor; |
| 48 | |
| 49 | // Initialize title |
| 50 | LayoutInflater inflater = (LayoutInflater) context |
| 51 | .getSystemService(Context.LAYOUT_INFLATER_SERVICE); |
| 52 | View title = inflater.inflate(R.layout.color_picker_dialog_title, null); |
| 53 | setCustomTitle(title); |
| 54 | |
| 55 | mCurrentColorView = title.findViewById(R.id.selected_color_view); |
| 56 | |
| 57 | TextView titleText = (TextView) title.findViewById(R.id.title); |
| 58 | titleText.setText(R.string.color_picker_dialog_title); |
| 59 | |
| 60 | // Initialize Set/Cancel buttons |
| 61 | String positiveButtonText = context.getString(R.string.color_picker_button_set); |
| 62 | setButton(BUTTON_POSITIVE, positiveButtonText, |
| 63 | new Dialog.OnClickListener() { |
| 64 | @Override |
| 65 | public void onClick(DialogInterface dialogInterface, int i) { |
| 66 | tryNotifyColorSet(mCurrentColor); |
| 67 | } |
| 68 | }); |
| 69 | |
| 70 | // Note that with the color picker there's not really any such thing as |
| 71 | // "cancelled". |
| 72 | // The color picker flow only finishes when we return a color, so we |
| 73 | // have to always |
| 74 | // return something. The concept of "cancelled" in this case just means |
| 75 | // returning |
| 76 | // the color that we were initialized with. |
| 77 | String negativeButtonText = context.getString(R.string.color_picker_button_cancel); |
| 78 | setButton(BUTTON_NEGATIVE, negativeButtonText, |
| 79 | new Dialog.OnClickListener() { |
| 80 | @Override |
| 81 | public void onClick(DialogInterface dialogInterface, int i) { |
| 82 | tryNotifyColorSet(mInitialColor); |
| 83 | } |
| 84 | }); |
| 85 | |
| 86 | setOnCancelListener(new DialogInterface.OnCancelListener() { |
| 87 | @Override |
| 88 | public void onCancel(DialogInterface arg0) { |
| 89 | tryNotifyColorSet(mInitialColor); |
| 90 | } |
| 91 | }); |
| 92 | |
| 93 | // Initialize main content view |
| 94 | View content = inflater.inflate(R.layout.color_picker_dialog_content, null); |
| 95 | setView(content); |
| 96 | |
| 97 | // Initialize More button. |
| 98 | mMoreButton = (Button) content.findViewById(R.id.more_colors_button); |
| 99 | mMoreButton.setOnClickListener(new Button.OnClickListener() { |
| 100 | @Override |
| 101 | public void onClick(View v) { |
| 102 | showAdvancedView(); |
| 103 | } |
| 104 | }); |
| 105 | |
| 106 | // Initialize advanced color view (hidden initially). |
| 107 | mAdvancedColorPicker = |
| 108 | (ColorPickerAdvanced) content.findViewById(R.id.color_picker_advanced); |
| 109 | mAdvancedColorPicker.setVisibility(View.GONE); |
| 110 | |
| 111 | // Initialize simple color view (default view). |
| 112 | mSimpleColorPicker = (ColorPickerSimple) content.findViewById(R.id.color_picker_simple); |
| 113 | mSimpleColorPicker.init(this); |
| 114 | |
| 115 | updateCurrentColor(mInitialColor); |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Listens to the ColorPicker for when the user has changed the selected color, and |
| 120 | * updates the current color (the color shown in the title) accordingly. |
| 121 | * |
| 122 | * @param color The new color chosen by the user. |
| 123 | */ |
| 124 | @Override |
| 125 | public void onColorChanged(int color) { |
| 126 | updateCurrentColor(color); |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Hides the simple view (the default) and shows the advanced one instead, hiding the |
| 131 | * "More" button at the same time. |
| 132 | */ |
| 133 | private void showAdvancedView() { |
| 134 | // Only need to hide the borders, not the Views themselves, since the Views are |
| 135 | // contained within the borders. |
| 136 | View buttonBorder = findViewById(R.id.more_colors_button_border); |
| 137 | buttonBorder.setVisibility(View.GONE); |
| 138 | |
| 139 | View simpleViewBorder = findViewById(R.id.color_picker_simple_border); |
| 140 | simpleViewBorder.setVisibility(View.GONE); |
| 141 | |
| 142 | mAdvancedColorPicker.setVisibility(View.VISIBLE); |
| 143 | mAdvancedColorPicker.setListener(this); |
| 144 | mAdvancedColorPicker.setColor(mCurrentColor); |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Tries to notify any listeners that the color has been set. |
| 149 | */ |
| 150 | private void tryNotifyColorSet(int color) { |
| 151 | if (mListener != null) { |
| 152 | mListener.onColorChanged(color); |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Updates the internal cache of the currently selected color, updating the colorful little |
| 158 | * box in the title at the same time. |
| 159 | */ |
| 160 | private void updateCurrentColor(int color) { |
| 161 | mCurrentColor = color; |
| 162 | if (mCurrentColorView != null) { |
| 163 | mCurrentColorView.setBackgroundColor(color); |
| 164 | } |
| 165 | } |
| 166 | } |