| 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.components.web_contents_delegate_android; |
| 6 | |
| 7 | import android.content.Context; |
| 8 | |
| 9 | import org.chromium.base.CalledByNative; |
| 10 | import org.chromium.base.JNINamespace; |
| 11 | import org.chromium.content.browser.ContentViewCore; |
| 12 | import org.chromium.ui.ColorPickerDialog; |
| 13 | import org.chromium.ui.OnColorChangedListener; |
| 14 | |
| 15 | /** |
| 16 | * ColorChooserAndroid communicates with the java ColorPickerDialog and the |
| 17 | * native color_chooser_android.cc |
| 18 | */ |
| 19 | @JNINamespace("web_contents_delegate_android") |
| 20 | public class ColorChooserAndroid { |
| 21 | private final ColorPickerDialog mDialog; |
| 22 | private final int mNativeColorChooserAndroid; |
| 23 | |
| 24 | private ColorChooserAndroid(int nativeColorChooserAndroid, |
| 25 | Context context, int initialColor) { |
| 26 | OnColorChangedListener listener = new OnColorChangedListener() { |
| 27 | @Override |
| 28 | public void onColorChanged(int color) { |
| 29 | mDialog.dismiss(); |
| 30 | nativeOnColorChosen(mNativeColorChooserAndroid, color); |
| 31 | } |
| 32 | }; |
| 33 | |
| 34 | mNativeColorChooserAndroid = nativeColorChooserAndroid; |
| 35 | mDialog = new ColorPickerDialog(context, listener, initialColor); |
| 36 | } |
| 37 | |
| 38 | private void openColorChooser() { |
| 39 | mDialog.show(); |
| 40 | } |
| 41 | |
| 42 | @CalledByNative |
| 43 | public void closeColorChooser() { |
| 44 | mDialog.dismiss(); |
| 45 | } |
| 46 | |
| 47 | @CalledByNative |
| 48 | public static ColorChooserAndroid createColorChooserAndroid( |
| 49 | int nativeColorChooserAndroid, |
| 50 | ContentViewCore contentViewCore, |
| 51 | int initialColor) { |
| 52 | ColorChooserAndroid chooser = new ColorChooserAndroid(nativeColorChooserAndroid, |
| 53 | contentViewCore.getContext(), initialColor); |
| 54 | chooser.openColorChooser(); |
| 55 | return chooser; |
| 56 | } |
| 57 | |
| 58 | // Implemented in color_chooser_android.cc |
| 59 | private native void nativeOnColorChosen(int nativeColorChooserAndroid, int color); |
| 60 | } |