 Chromium Code Reviews
 Chromium Code Reviews Issue 1050163004:
  [Contextual Search] Implements Opt-out promo.  (Closed) 
  Base URL: https://chromium.googlesource.com/chromium/src.git@master
    
  
    Issue 1050163004:
  [Contextual Search] Implements Opt-out promo.  (Closed) 
  Base URL: https://chromium.googlesource.com/chromium/src.git@master| Index: chrome/android/java/src/org/chromium/chrome/browser/widget/ContextualSearchControl.java | 
| diff --git a/chrome/android/java/src/org/chromium/chrome/browser/widget/ContextualSearchControl.java b/chrome/android/java/src/org/chromium/chrome/browser/widget/ContextualSearchControl.java | 
| new file mode 100644 | 
| index 0000000000000000000000000000000000000000..a65f7d9a518af65f4d9ca249c62a72b8c9f0e2d8 | 
| --- /dev/null | 
| +++ b/chrome/android/java/src/org/chromium/chrome/browser/widget/ContextualSearchControl.java | 
| @@ -0,0 +1,126 @@ | 
| +// Copyright 2014 The Chromium Authors. All rights reserved. | 
| 
David Trainor- moved to gerrit
2015/04/02 20:38:24
2015, here and everywhere else
 
pedro (no code reviews)
2015/04/03 21:01:02
Done.
 | 
| +// Use of this source code is governed by a BSD-style license that can be | 
| +// found in the LICENSE file. | 
| + | 
| +package org.chromium.chrome.browser.widget; | 
| 
David Trainor- moved to gerrit
2015/04/02 20:38:24
Can you put this in either browser.compositor.bott
 
pedro (no code reviews)
2015/04/03 21:01:02
Done.
 | 
| + | 
| +import android.content.Context; | 
| +import android.graphics.Rect; | 
| +import android.util.AttributeSet; | 
| +import android.view.View; | 
| +import android.view.ViewParent; | 
| +import android.widget.LinearLayout; | 
| +import android.widget.TextView; | 
| + | 
| +import org.chromium.chrome.R; | 
| +import org.chromium.ui.resources.dynamics.ViewResourceAdapter; | 
| + | 
| +/** | 
| + * Root ControlContainer for the Contextual Search panel. | 
| + * Handles user interaction with the Contextual Search control. | 
| + * Based on ToolbarControlContainer. | 
| + */ | 
| +public class ContextualSearchControl extends LinearLayout { | 
| 
Donn Denman
2015/04/02 19:45:00
I didn't really review this, since it's just being
 
pedro (no code reviews)
2015/04/03 21:01:02
Yes, this is just being moved.
 | 
| + private static final float RESOLVED_SEARCH_TERM_SIDE_PADDING_DP = 40.f; | 
| + private final int mSidePaddingPx; | 
| + | 
| + private ViewResourceAdapter mResourceAdapter; | 
| + | 
| + private TextView mSelectionText; | 
| + private TextView mStartText; | 
| + private TextView mEndText; | 
| + | 
| + private boolean mIsDirty = false; | 
| + | 
| + /** | 
| + * Constructs a new control container. | 
| + * <p> | 
| + * This constructor is used when inflating from XML. | 
| + * | 
| + * @param context The context used to build this view. | 
| + * @param attrs The attributes used to determine how to construct this view. | 
| + */ | 
| + public ContextualSearchControl(Context context, AttributeSet attrs) { | 
| + super(context, attrs); | 
| + | 
| + final float pxToDp = 1.0f / context.getResources().getDisplayMetrics().density; | 
| + mSidePaddingPx = Math.round(RESOLVED_SEARCH_TERM_SIDE_PADDING_DP / pxToDp); | 
| + } | 
| + | 
| + /** | 
| + * @return The {@link ViewResourceAdapter} that exposes this {@link View} as a CC resource. | 
| + */ | 
| + public ViewResourceAdapter getResourceAdapter() { | 
| + return mResourceAdapter; | 
| + } | 
| + | 
| + @Override | 
| + public void onFinishInflate() { | 
| + super.onFinishInflate(); | 
| + | 
| + mSelectionText = (TextView) findViewById(R.id.main_text); | 
| + mStartText = (TextView) findViewById(R.id.surrounding_text_start); | 
| + mEndText = (TextView) findViewById(R.id.surrounding_text_end); | 
| + | 
| + mResourceAdapter = new ViewResourceAdapter(findViewById(R.id.contextual_search_view)); | 
| + } | 
| + | 
| + @Override | 
| + public ViewParent invalidateChildInParent(int[] location, Rect dirty) { | 
| + ViewParent parent = super.invalidateChildInParent(location, dirty); | 
| + // TODO(pedrosimonetti): ViewGroup#invalidateChildInParent() is being called multiple | 
| + // times with different rectangles (for each of the individual repaints it seems). This | 
| + // means in order to invalidate it only once we need to keep track of the dirty state, | 
| + // and call ViewResourceAdapter#invalidate() only once per change of state, passing | 
| + // "null" to indicate that the whole area should be invalidated. This can be deleted | 
| + // if we stop relying on an Android View to render our Search Bar Text. | 
| + if (mIsDirty && mResourceAdapter != null) { | 
| + mIsDirty = false; | 
| + mResourceAdapter.invalidate(null); | 
| + } | 
| + return parent; | 
| + } | 
| + | 
| + /** | 
| + * Sets the text to display on top of the first-run promo. | 
| + * @param selection The portion of the text that represents the user's selection. | 
| + */ | 
| + public void setFirstRunText(String selection) { | 
| + // TODO(pedrosimonetti): confirm that is okay to remove the experimental text | 
| +// String firstRunText = ContextualSearchFieldTrial.getEnglishExperimentFirstRunText( | 
| 
Donn Denman
2015/04/02 19:45:00
You can remove this, it's not being used.
You sho
 
Donn Denman
2015/04/02 20:02:28
Actually, I'll remove this definition in my CL.
 
pedro (no code reviews)
2015/04/03 21:01:02
Acknowledged.
 
pedro (no code reviews)
2015/04/03 21:01:02
Acknowledged.
 | 
| +// selection); | 
| +// if (firstRunText == null) { | 
| +// firstRunText = | 
| +// getContext().getString(R.string.contextual_search_action_bar, selection); | 
| +// } | 
| + String firstRunText = | 
| + getContext().getString(R.string.contextual_search_action_bar, selection); | 
| + setCentralText(firstRunText); | 
| + } | 
| + | 
| + /** | 
| + * Sets the search context to display in the control. | 
| + * @param selection The portion of the context that represents the user's selection. | 
| + * @param start The portion of the context from its start to the selection. | 
| + * @param end The portion of the context the selection to its end. | 
| + */ | 
| + public void setSearchContext(String selection, String start, String end) { | 
| + mSelectionText.setPadding(0, 0, 0, 0); | 
| + mSelectionText.setText(selection); | 
| + mStartText.setText(start); | 
| + mEndText.setText(end); | 
| + mIsDirty = true; | 
| + } | 
| + | 
| + /** | 
| + * Sets the resolved search search to display in the control. | 
| + * @param searchTerm The string that represents the resolved search term. | 
| + */ | 
| + public void setCentralText(String searchTerm) { | 
| + mSelectionText.setPadding(mSidePaddingPx, 0, mSidePaddingPx, 0); | 
| + mSelectionText.setText(searchTerm); | 
| + mStartText.setText(""); | 
| + mEndText.setText(""); | 
| + mIsDirty = true; | 
| + } | 
| +} |