1// Copyright 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
5package org.chromium.components.web_contents_delegate_android;
6
7import android.content.Context;
8
9import org.chromium.base.CalledByNative;
10import org.chromium.base.JNINamespace;
11import org.chromium.content.browser.ContentViewCore;
12import org.chromium.ui.ColorPickerDialog;
13import org.chromium.ui.ColorSuggestion;
14import org.chromium.ui.OnColorChangedListener;
15
16/**
17 * ColorChooserAndroid communicates with the java ColorPickerDialog and the
18 * native color_chooser_android.cc
19 */
20@JNINamespace("web_contents_delegate_android")
21public class ColorChooserAndroid {
22    private final ColorPickerDialog mDialog;
23    private final long mNativeColorChooserAndroid;
24
25    private ColorChooserAndroid(long nativeColorChooserAndroid,
26            Context context, int initialColor, ColorSuggestion[] suggestions) {
27        OnColorChangedListener listener = new OnColorChangedListener() {
28          @Override
29          public void onColorChanged(int color) {
30              mDialog.dismiss();
31              nativeOnColorChosen(mNativeColorChooserAndroid, color);
32          }
33        };
34
35        mNativeColorChooserAndroid = nativeColorChooserAndroid;
36        mDialog = new ColorPickerDialog(context, listener, initialColor, suggestions);
37    }
38
39    private void openColorChooser() {
40        mDialog.show();
41    }
42
43    @CalledByNative
44    public void closeColorChooser() {
45        mDialog.dismiss();
46    }
47
48    @CalledByNative
49    public static ColorChooserAndroid createColorChooserAndroid(
50            long nativeColorChooserAndroid,
51            ContentViewCore contentViewCore,
52            int initialColor,
53            ColorSuggestion[] suggestions) {
54        ColorChooserAndroid chooser = new ColorChooserAndroid(nativeColorChooserAndroid,
55            contentViewCore.getContext(), initialColor, suggestions);
56        chooser.openColorChooser();
57        return chooser;
58    }
59
60    @CalledByNative
61    private static ColorSuggestion[] createColorSuggestionArray(int size) {
62        return new ColorSuggestion[size];
63    }
64
65    /**
66     * @param array ColorSuggestion array that should get a new suggestion added.
67     * @param index Index in the array where to place a new suggestion.
68     * @param color Color of the suggestion.
69     * @param label Label of the suggestion.
70     */
71    @CalledByNative
72    private static void addToColorSuggestionArray(ColorSuggestion[] array, int index,
73            int color, String label) {
74        array[index] = new ColorSuggestion(color, label);
75    }
76
77    // Implemented in color_chooser_android.cc
78    private native void nativeOnColorChosen(long nativeColorChooserAndroid, int color);
79}
80