1 // Copyright 2014 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.
5 package org.chromium.chrome.browser.dom_distiller;
7 import android.content.Context;
8 import android.util.AttributeSet;
9 import android.view.LayoutInflater;
10 import android.view.MotionEvent;
11 import android.view.View;
12 import android.widget.ImageButton;
14 import org.chromium.chrome.R;
15 import org.chromium.chrome.browser.banners.SwipableOverlayView;
16 import org.chromium.content.browser.ContentViewCore;
19 * A view which displays a question to the user about the quality of distillation, where the user
20 * is given the option to respond.
22 * <p>The observer is called when the user makes a choice. After this point, it is not possible to
23 * interact with the view, and it is ready for dismissal. The selected option stays visibly
26 public class DomDistillerFeedbackReportingView extends SwipableOverlayView {
27 // XML layout for the BannerView.
28 private static final int VIEW_LAYOUT = R.layout.dom_distiller_feedback_reporting_view;
30 // Class to alert about DomDistillerFeedbackReportingView events.
31 private FeedbackObserver mFeedbackObserver;
33 // The button to click for selecting 'No'.
34 private ImageButton mNoButton;
36 // The button to click for selecting 'Yes'.
37 private ImageButton mYesButton;
39 // Whether a selection has already been made, which means new events should be ignored.
40 private boolean mSelectionMade;
43 * Called when the user makes a choice. After the call, it is not possible to interact further
46 interface FeedbackObserver {
47 void onYesPressed(DomDistillerFeedbackReportingView view);
49 void onNoPressed(DomDistillerFeedbackReportingView view);
53 * Creates a DomDistillerFeedbackReportingView and adds it to the given ContentViewCore.
55 * @param contentView ContentViewCore to display the DomDistillerFeedbackReportingView for.
56 * @param feedbackObserver Class that is alerted for DomDistillerFeedbackReportingView events.
57 * @return The created banner.
59 public static DomDistillerFeedbackReportingView create(ContentViewCore contentViewCore,
60 FeedbackObserver feedbackObserver) {
61 Context context = contentViewCore.getContext().getApplicationContext();
62 DomDistillerFeedbackReportingView view =
63 (DomDistillerFeedbackReportingView) LayoutInflater.from(context)
64 .inflate(VIEW_LAYOUT, null);
65 view.initialize(feedbackObserver);
66 view.addToView(contentViewCore);
71 * Creates a DomDistillerFeedbackReportingView.
73 * @param context Context for acquiring resources.
74 * @param attrs Attributes from the XML layout inflation.
76 public DomDistillerFeedbackReportingView(Context context, AttributeSet attrs) {
77 super(context, attrs);
80 private void initialize(FeedbackObserver feedbackObserver) {
81 mFeedbackObserver = feedbackObserver;
82 mNoButton = (ImageButton) findViewById(R.id.distillation_quality_answer_no);
83 mYesButton = (ImageButton) findViewById(R.id.distillation_quality_answer_yes);
84 mNoButton.setClickable(true);
85 mYesButton.setClickable(true);
86 mNoButton.setOnClickListener(new OnClickListener() {
88 public void onClick(View v) {
89 if (mSelectionMade) return;
90 mSelectionMade = true;
91 mNoButton.setImageResource(R.drawable.distillation_quality_answer_no_pressed);
93 if (mFeedbackObserver != null) {
94 mFeedbackObserver.onNoPressed(DomDistillerFeedbackReportingView.this);
98 mYesButton.setOnClickListener(new OnClickListener() {
100 public void onClick(View v) {
101 if (mSelectionMade) return;
102 mSelectionMade = true;
103 mYesButton.setImageResource(R.drawable.distillation_quality_answer_yes_pressed);
105 if (mFeedbackObserver != null) {
106 mFeedbackObserver.onYesPressed(DomDistillerFeedbackReportingView.this);
112 private void disableUI() {
113 // Clear OnClickListener to assure no more calls and that everything is cleaned up.
114 mNoButton.setOnClickListener(null);
115 mYesButton.setOnClickListener(null);
117 // Disable the buttons, so the images for highlighted/non-highlighted will not change if the
118 // user continues to tap the buttons while it is dismissing.
119 mNoButton.setEnabled(false);
120 mYesButton.setEnabled(false);
124 * This is overridden since the method visibility is protected in the parent
125 * {@link SwipableOverlayView}. The {@link DomDistillerFeedbackReporter} needs to be able to
126 * dismiss this {@link DomDistillerFeedbackReportingView}, so by overriding this method in this
127 * class, it is callable from {@link DomDistillerFeedbackReporter}.
130 protected boolean dismiss(boolean horizontally) {
131 return super.dismiss(horizontally);
135 protected void onViewClicked() {
139 protected void onViewPressed(MotionEvent event) {
143 protected void onViewSwipedAway() {