Home | History | Annotate | Download | only in dom_distiller
      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.
      4 
      5 package org.chromium.chrome.browser.dom_distiller;
      6 
      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;
     13 
     14 import org.chromium.chrome.R;
     15 import org.chromium.chrome.browser.banners.SwipableOverlayView;
     16 import org.chromium.content.browser.ContentViewCore;
     17 
     18 /**
     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.
     21  *
     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
     24  * selected.
     25  */
     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;
     29 
     30     // Class to alert about DomDistillerFeedbackReportingView events.
     31     private FeedbackObserver mFeedbackObserver;
     32 
     33     // The button to click for selecting 'No'.
     34     private ImageButton mNoButton;
     35 
     36     // The button to click for selecting 'Yes'.
     37     private ImageButton mYesButton;
     38 
     39     // Whether a selection has already been made, which means new events should be ignored.
     40     private boolean mSelectionMade;
     41 
     42     /**
     43      * Called when the user makes a choice. After the call, it is not possible to interact further
     44      * with the view.
     45      */
     46     interface FeedbackObserver {
     47         void onYesPressed(DomDistillerFeedbackReportingView view);
     48 
     49         void onNoPressed(DomDistillerFeedbackReportingView view);
     50     }
     51 
     52     /**
     53      * Creates a DomDistillerFeedbackReportingView and adds it to the given ContentViewCore.
     54      *
     55      * @param contentView      ContentViewCore to display the DomDistillerFeedbackReportingView for.
     56      * @param feedbackObserver Class that is alerted for DomDistillerFeedbackReportingView events.
     57      * @return The created banner.
     58      */
     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);
     67         return view;
     68     }
     69 
     70     /**
     71      * Creates a DomDistillerFeedbackReportingView.
     72      *
     73      * @param context Context for acquiring resources.
     74      * @param attrs   Attributes from the XML layout inflation.
     75      */
     76     public DomDistillerFeedbackReportingView(Context context, AttributeSet attrs) {
     77         super(context, attrs);
     78     }
     79 
     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() {
     87             @Override
     88             public void onClick(View v) {
     89                 if (mSelectionMade) return;
     90                 mSelectionMade = true;
     91                 mNoButton.setImageResource(R.drawable.distillation_quality_answer_no_pressed);
     92                 disableUI();
     93                 if (mFeedbackObserver != null) {
     94                     mFeedbackObserver.onNoPressed(DomDistillerFeedbackReportingView.this);
     95                 }
     96             }
     97         });
     98         mYesButton.setOnClickListener(new OnClickListener() {
     99             @Override
    100             public void onClick(View v) {
    101                 if (mSelectionMade) return;
    102                 mSelectionMade = true;
    103                 mYesButton.setImageResource(R.drawable.distillation_quality_answer_yes_pressed);
    104                 disableUI();
    105                 if (mFeedbackObserver != null) {
    106                     mFeedbackObserver.onYesPressed(DomDistillerFeedbackReportingView.this);
    107                 }
    108             }
    109         });
    110     }
    111 
    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);
    116 
    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);
    121     }
    122 
    123     /**
    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}.
    128      */
    129     @Override
    130     protected boolean dismiss(boolean horizontally) {
    131         return super.dismiss(horizontally);
    132     }
    133 
    134     @Override
    135     protected void onViewClicked() {
    136     }
    137 
    138     @Override
    139     protected void onViewPressed(MotionEvent event) {
    140     }
    141 
    142     @Override
    143     protected void onViewSwipedAway() {
    144     }
    145 }
    146