Bug 1172071 - Add icon label to search engine bar. r=mcomella.

This commit is contained in:
Sebastian Kaspari 2015-06-17 15:55:31 +02:00
parent 577a41dd06
commit 59912ca623
3 changed files with 87 additions and 11 deletions

View File

@ -7,15 +7,21 @@
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.support.v4.graphics.drawable.DrawableCompat;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.ImageView;
import org.mozilla.gecko.R;
@ -29,6 +35,7 @@ public class SearchEngineBar extends TwoWayView
private static final String LOGTAG = "Gecko" + SearchEngineBar.class.getSimpleName();
private static final float ICON_CONTAINER_MIN_WIDTH_DP = 72;
private static final float LABEL_CONTAINER_WIDTH_DP = 48;
private static final float DIVIDER_HEIGHT_DP = 1;
public interface OnSearchBarClickListener {
@ -39,6 +46,7 @@ public class SearchEngineBar extends TwoWayView
private final Paint dividerPaint;
private final float minIconContainerWidth;
private final float dividerHeight;
private final int labelContainerWidth;
private int iconContainerWidth;
private OnSearchBarClickListener onSearchBarClickListener;
@ -53,6 +61,7 @@ public class SearchEngineBar extends TwoWayView
final DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
minIconContainerWidth = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, ICON_CONTAINER_MIN_WIDTH_DP, displayMetrics);
dividerHeight = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, DIVIDER_HEIGHT_DP, displayMetrics);
labelContainerWidth = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, LABEL_CONTAINER_WIDTH_DP, displayMetrics);
iconContainerWidth = (int) minIconContainerWidth;
@ -69,6 +78,11 @@ public class SearchEngineBar extends TwoWayView
OnSearchBarClickListener.class.getSimpleName() + " is not initialized");
}
if (position == 0) {
// Ignore click on label
return;
}
final SearchEngine searchEngine = adapter.getItem(position);
onSearchBarClickListener.onSearchBarClickListener(searchEngine);
}
@ -85,10 +99,10 @@ public class SearchEngineBar extends TwoWayView
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
final int searchEngineCount = getCount();
final int searchEngineCount = adapter.getCount() - 1;
if (searchEngineCount > 0) {
final float availableWidthPerContainer = getMeasuredWidth() / searchEngineCount;
final float availableWidthPerContainer = (getMeasuredWidth() - labelContainerWidth) / searchEngineCount;
final int desiredIconContainerSize = (int) Math.max(
availableWidthPerContainer,
@ -110,6 +124,9 @@ public class SearchEngineBar extends TwoWayView
}
public class SearchEngineAdapter extends BaseAdapter {
private static final int VIEW_TYPE_SEARCH_ENGINE = 0;
private static final int VIEW_TYPE_LABEL = 1;
List<SearchEngine> searchEngines = new ArrayList<>();
public void setSearchEngines(final List<SearchEngine> searchEngines) {
@ -119,12 +136,14 @@ public class SearchEngineBar extends TwoWayView
@Override
public int getCount() {
return searchEngines.size();
// Adding offset for label at position 0 (Bug 1172071)
return searchEngines.size() + 1;
}
@Override
public SearchEngine getItem(final int position) {
return searchEngines.get(position);
// Returning null for the label at position 0 (Bug 1172071)
return position == 0 ? null : searchEngines.get(position - 1);
}
@Override
@ -132,19 +151,51 @@ public class SearchEngineBar extends TwoWayView
return position;
}
@Override
public int getItemViewType(int position) {
return position == 0 ? VIEW_TYPE_LABEL : VIEW_TYPE_SEARCH_ENGINE;
}
@Override
public int getViewTypeCount() {
return 2;
}
@Override
public View getView(final int position, final View convertView, final ViewGroup parent) {
final View view;
if (convertView == null) {
view = LayoutInflater.from(getContext()).inflate(R.layout.search_engine_bar_item, parent, false);
if (position == 0) {
return getLabelView(convertView, parent);
} else {
view = convertView;
return getSearchEngineView(position, convertView, parent);
}
}
private View getLabelView(View view, final ViewGroup parent) {
if (view == null) {
view = LayoutInflater.from(getContext()).inflate(R.layout.search_engine_bar_label, parent, false);
}
view.setLayoutParams(new LayoutParams(iconContainerWidth, ViewGroup.LayoutParams.MATCH_PARENT));
Drawable icon = DrawableCompat.wrap(ContextCompat.getDrawable(parent.getContext(), R.drawable.suggestion_item_search));
DrawableCompat.setTint(icon, getResources().getColor(R.color.disabled_grey));
final ImageView iconView = (ImageView) view.findViewById(R.id.search_engine_label);
iconView.setImageDrawable(icon);
iconView.setScaleType(ImageView.ScaleType.FIT_XY);
return view;
}
private View getSearchEngineView(final int position, View view, final ViewGroup parent) {
if (view == null) {
view = LayoutInflater.from(getContext()).inflate(R.layout.search_engine_bar_item, parent, false);
}
LayoutParams params = (LayoutParams) view.getLayoutParams();
params.width = iconContainerWidth;
view.setLayoutParams(params);
final ImageView faviconView = (ImageView) view.findViewById(R.id.search_engine_icon);
final SearchEngine searchEngine = searchEngines.get(position);
final SearchEngine searchEngine = getItem(position);
faviconView.setImageBitmap(searchEngine.getIcon());
final String desc = getResources().getString(R.string.search_bar_item_desc, searchEngine.getEngineIdentifier());

View File

@ -0,0 +1,21 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- This Source Code Form is subject to the terms of the Mozilla Public
- License, v. 2.0. If a copy of the MPL was not distributed with this
- file, You can obtain one at http://mozilla.org/MPL/2.0/. -->
<!-- TwoWayView doesn't let us set the margin around items (except as
gecko:itemMargin, but that doesn't increase the hit area) so we
have to surround the main View by a ViewGroup to create a pressable margin. -->
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="48dp"
android:layout_height="match_parent">
<ImageView
android:id="@+id/search_engine_label"
android:layout_width="16dp"
android:layout_height="16dp"
android:layout_gravity="center"
android:scaleType="fitCenter"/>
</FrameLayout>

View File

@ -156,7 +156,11 @@ public class testAddSearchEngine extends AboutHomeTest {
return false;
}
return (searchResultList.getAdapter().getCount() + searchEngineBar.getAdapter().getCount() == expectedCount);
final int actualCount = searchResultList.getAdapter().getCount()
+ searchEngineBar.getAdapter().getCount()
- 1; // Subtract one for the search engine bar label (Bug 1172071)
return (actualCount == expectedCount);
}
}, MAX_WAIT_TEST_MS);