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.ContentView;
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 FeedbackReportingView extends SwipableOverlayView {
27 // XML layout for the BannerView.
28 private static final int VIEW_LAYOUT = R.layout.feedback_reporting_view;
30 // Class to alert about FeedbackReportingView 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(FeedbackReportingView view);
49 void onNoPressed(FeedbackReportingView view);
53 * Creates a FeedbackReportingView and adds it to the given ContentView.
55 * @param contentView ContentView to display the FeedbackReportingView for.
56 * @param feedbackObserver Class that is alerted for FeedbackReportingView events.
57 * @return The created banner.
59 public static FeedbackReportingView create(ContentView contentView,
60 FeedbackObserver feedbackObserver) {
61 Context context = contentView.getContext().getApplicationContext();
62 FeedbackReportingView banner =
63 (FeedbackReportingView) LayoutInflater.from(context).inflate(VIEW_LAYOUT, null);
64 banner.initialize(feedbackObserver);
65 banner.addToView(contentView);
70 * Creates a FeedbackReportingView.
72 * @param context Context for acquiring resources.
73 * @param attrs Attributes from the XML layout inflation.
75 public FeedbackReportingView(Context context, AttributeSet attrs) {
76 super(context, attrs);
79 private void initialize(FeedbackObserver feedbackObserver) {
80 mFeedbackObserver = feedbackObserver;
81 mNoButton = (ImageButton) findViewById(R.id.distillation_quality_answer_no);
82 mYesButton = (ImageButton) findViewById(R.id.distillation_quality_answer_yes);
83 mNoButton.setClickable(true);
84 mYesButton.setClickable(true);
85 mNoButton.setOnClickListener(new OnClickListener() {
87 public void onClick(View v) {
88 if (mSelectionMade) return;
89 mSelectionMade = true;
90 mNoButton.setImageResource(R.drawable.distillation_quality_answer_no_pressed);
92 if (mFeedbackObserver != null) {
93 mFeedbackObserver.onNoPressed(FeedbackReportingView.this);
97 mYesButton.setOnClickListener(new OnClickListener() {
99 public void onClick(View v) {
100 if (mSelectionMade) return;
101 mSelectionMade = true;
102 mYesButton.setImageResource(R.drawable.distillation_quality_answer_yes_pressed);
104 if (mFeedbackObserver != null) {
105 mFeedbackObserver.onYesPressed(FeedbackReportingView.this);
111 private void disableUI() {
112 // Clear OnClickListener to assure no more calls and that everything is cleaned up.
113 mNoButton.setOnClickListener(null);
114 mYesButton.setOnClickListener(null);
116 // Disable the buttons, so the images for highlighted/non-highlighted will not change if the
117 // user continues to tap the buttons while it is dismissing.
118 mNoButton.setEnabled(false);
119 mYesButton.setEnabled(false);
123 * This is overridden since the method visibility is protected in the parent
124 * {@link SwipableOverlayView}. The
125 * {@link org.chromium.chrome.browser.dom_distiller.FeedbackReporter} needs to be able to
126 * dismiss this {@link org.chromium.chrome.browser.dom_distiller.FeedbackReportingView}, so by
127 * overriding this method in this class, it is callable from
128 * {@link org.chromium.chrome.browser.dom_distiller.FeedbackReporter}.
131 protected boolean dismiss(boolean horizontally) {
132 return super.dismiss(horizontally);
136 protected void onViewClicked() {
140 protected void onViewPressed(MotionEvent event) {
144 protected void onViewSwipedAway() {