Created
July 22, 2015 00:11
-
-
Save jonalmeida/37a1c238ba28ac6bd08d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# HG changeset patch | |
# User Jonathan Almeida [:jonalmeida] <[email protected]> | |
# Date 1437523313 25200 | |
# Tue Jul 21 17:01:53 2015 -0700 | |
# Node ID 8f1665e896e598b9f529c6b53943eb01ba0b534b | |
# Parent a6b93cceaf4e15a45d37cd5e75660cdc7d671b10 | |
Bug 1184211 - Replace TwoWayView in SearchBar with RecyclerView. r?sebastian | |
diff --git a/mobile/android/base/home/BrowserSearch.java b/mobile/android/base/home/BrowserSearch.java | |
--- a/mobile/android/base/home/BrowserSearch.java | |
+++ b/mobile/android/base/home/BrowserSearch.java | |
@@ -46,17 +46,16 @@ import android.text.TextUtils; | |
import android.util.AttributeSet; | |
import android.util.Log; | |
import android.view.LayoutInflater; | |
import android.view.MotionEvent; | |
import android.view.View; | |
import android.view.View.OnClickListener; | |
import android.view.ViewGroup; | |
import android.view.ViewStub; | |
-import android.view.WindowManager; | |
import android.view.WindowManager.LayoutParams; | |
import android.view.animation.AccelerateInterpolator; | |
import android.view.animation.Animation; | |
import android.view.animation.TranslateAnimation; | |
import android.widget.AdapterView; | |
import android.widget.LinearLayout; | |
import android.widget.ListView; | |
import android.widget.TextView; | |
diff --git a/mobile/android/base/home/SearchEngineBar.java b/mobile/android/base/home/SearchEngineBar.java | |
--- a/mobile/android/base/home/SearchEngineBar.java | |
+++ b/mobile/android/base/home/SearchEngineBar.java | |
@@ -1,208 +1,129 @@ | |
/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*- | |
/* 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/. */ | |
- package org.mozilla.gecko.home; | |
+package org.mozilla.gecko.home; | |
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.support.v7.widget.LinearLayoutManager; | |
+import android.support.v7.widget.RecyclerView; | |
import android.util.AttributeSet; | |
import android.util.DisplayMetrics; | |
+import android.util.Log; | |
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; | |
-import org.mozilla.gecko.widget.TwoWayView; | |
-import java.util.ArrayList; | |
import java.util.List; | |
-public class SearchEngineBar extends TwoWayView | |
- implements AdapterView.OnItemClickListener { | |
- private static final String LOGTAG = "Gecko" + SearchEngineBar.class.getSimpleName(); | |
+public class SearchEngineBar extends RecyclerView | |
+ implements RecyclerViewItemClickListener.OnClickListener { | |
+ private static final String LOGTAG = 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 { | |
- public void onSearchBarClickListener(SearchEngine searchEngine); | |
+ void onSearchBarClickListener(SearchEngine searchEngine); | |
} | |
- private final SearchEngineAdapter adapter; | |
- private final Paint dividerPaint; | |
- private final float minIconContainerWidth; | |
- private final float dividerHeight; | |
- private final int labelContainerWidth; | |
+ private final SearchEngineAdapter mAdapter; | |
+ private final LinearLayoutManager mLayoutManager; | |
+ private final Paint mDividerPaint; | |
+ private final float mMinIconContainerWidth; | |
+ private final float mDividerHeight; | |
+ private final int mLabelContainerWidth; | |
- private int iconContainerWidth; | |
- private OnSearchBarClickListener onSearchBarClickListener; | |
+ private int mIconContainerWidth; | |
+ private OnSearchBarClickListener mOnSearchBarClickListener; | |
public SearchEngineBar(final Context context, final AttributeSet attrs) { | |
super(context, attrs); | |
- dividerPaint = new Paint(); | |
- dividerPaint.setColor(getResources().getColor(R.color.divider_light)); | |
- dividerPaint.setStyle(Paint.Style.FILL_AND_STROKE); | |
+ mDividerPaint = new Paint(); | |
+ mDividerPaint.setColor(getResources().getColor(R.color.divider_light)); | |
+ mDividerPaint.setStyle(Paint.Style.FILL_AND_STROKE); | |
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); | |
+ mMinIconContainerWidth = TypedValue.applyDimension( | |
+ TypedValue.COMPLEX_UNIT_DIP, ICON_CONTAINER_MIN_WIDTH_DP, displayMetrics); | |
+ mDividerHeight = TypedValue.applyDimension( | |
+ TypedValue.COMPLEX_UNIT_DIP, DIVIDER_HEIGHT_DP, displayMetrics); | |
+ mLabelContainerWidth = (int) TypedValue.applyDimension( | |
+ TypedValue.COMPLEX_UNIT_DIP, LABEL_CONTAINER_WIDTH_DP, displayMetrics); | |
- iconContainerWidth = (int) minIconContainerWidth; | |
+ mIconContainerWidth = Math.round(mMinIconContainerWidth); | |
- adapter = new SearchEngineAdapter(); | |
- setAdapter(adapter); | |
- setOnItemClickListener(this); | |
+ mAdapter = new SearchEngineAdapter(context); | |
+ mAdapter.setIconContainerWidth(mIconContainerWidth); | |
+ mLayoutManager = new LinearLayoutManager(context); | |
+ mLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL); | |
+ | |
+ setAdapter(mAdapter); | |
+ setLayoutManager(mLayoutManager); | |
+ addOnItemTouchListener(new RecyclerViewItemClickListener(context, this, this)); | |
} | |
- @Override | |
- public void onItemClick(final AdapterView<?> parent, final View view, final int position, | |
- final long id) { | |
- if (onSearchBarClickListener == null) { | |
- throw new IllegalStateException( | |
- OnSearchBarClickListener.class.getSimpleName() + " is not initialized"); | |
- } | |
- | |
- if (position == 0) { | |
- // Ignore click on label | |
- return; | |
- } | |
- | |
- final SearchEngine searchEngine = adapter.getItem(position); | |
- onSearchBarClickListener.onSearchBarClickListener(searchEngine); | |
+ public void setSearchEngines(List<SearchEngine> searchEngines) { | |
+ mAdapter.setSearchEngines(searchEngines); | |
} | |
- protected void setOnSearchBarClickListener(final OnSearchBarClickListener listener) { | |
- onSearchBarClickListener = listener; | |
- } | |
- | |
- protected void setSearchEngines(final List<SearchEngine> searchEngines) { | |
- adapter.setSearchEngines(searchEngines); | |
+ public void setOnSearchBarClickListener(OnSearchBarClickListener listener) { | |
+ mOnSearchBarClickListener = listener; | |
} | |
@Override | |
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { | |
super.onMeasure(widthMeasureSpec, heightMeasureSpec); | |
- final int searchEngineCount = adapter.getCount() - 1; | |
+ final int searchEngineCount = mAdapter.getItemCount(); | |
if (searchEngineCount > 0) { | |
- final float availableWidthPerContainer = (getMeasuredWidth() - labelContainerWidth) / searchEngineCount; | |
+ final float availableWidthPerContainer = (getMeasuredWidth() - mLabelContainerWidth) / searchEngineCount; | |
final int desiredIconContainerSize = (int) Math.max( | |
availableWidthPerContainer, | |
- minIconContainerWidth | |
+ mMinIconContainerWidth | |
); | |
- if (desiredIconContainerSize != iconContainerWidth) { | |
- iconContainerWidth = desiredIconContainerSize; | |
- adapter.notifyDataSetChanged(); | |
+ if (desiredIconContainerSize != mIconContainerWidth) { | |
+ mIconContainerWidth = desiredIconContainerSize; | |
+ mAdapter.setIconContainerWidth(mIconContainerWidth); | |
+ mAdapter.notifyDataSetChanged(); | |
} | |
} | |
} | |
@Override | |
- protected void onDraw(Canvas canvas) { | |
+ public void onDraw(Canvas canvas) { | |
super.onDraw(canvas); | |
- canvas.drawRect(0, 0, getWidth(), dividerHeight, dividerPaint); | |
+ canvas.drawRect(0, 0, getWidth(), mDividerHeight, mDividerPaint); | |
} | |
- 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) { | |
- this.searchEngines = searchEngines; | |
- notifyDataSetChanged(); | |
+ @Override | |
+ public void onClick(View view, int position) { | |
+ if (mOnSearchBarClickListener == null) { | |
+ throw new IllegalStateException( | |
+ OnSearchBarClickListener.class.getSimpleName() + " is not initializer." | |
+ ); | |
} | |
- @Override | |
- public int getCount() { | |
- // Adding offset for label at position 0 (Bug 1172071) | |
- return searchEngines.size() + 1; | |
+ if (position == 0) { | |
+ return; | |
} | |
- @Override | |
- public SearchEngine getItem(final int position) { | |
- // Returning null for the label at position 0 (Bug 1172071) | |
- return position == 0 ? null : searchEngines.get(position - 1); | |
- } | |
+ final SearchEngine searchEngine = mAdapter.getItem(position + 1); | |
+ mOnSearchBarClickListener.onSearchBarClickListener(searchEngine); | |
+ } | |
- @Override | |
- public long getItemId(final int position) { | |
- 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) { | |
- if (position == 0) { | |
- return getLabelView(convertView, parent); | |
- } else { | |
- 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); | |
- } | |
- | |
- final Drawable icon = DrawableCompat.wrap( | |
- ContextCompat.getDrawable(parent.getContext(), R.drawable.search_icon_active).mutate()); | |
- 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 = getItem(position); | |
- faviconView.setImageBitmap(searchEngine.getIcon()); | |
- | |
- final String desc = getResources().getString(R.string.search_bar_item_desc, searchEngine.getEngineIdentifier()); | |
- view.setContentDescription(desc); | |
- | |
- return view; | |
- } | |
+ @Override | |
+ public void onLongClick(View view, int position) { | |
+ // do nothing | |
} | |
} | |
diff --git a/mobile/android/base/moz.build b/mobile/android/base/moz.build | |
--- a/mobile/android/base/moz.build | |
+++ b/mobile/android/base/moz.build | |
@@ -344,16 +344,17 @@ gbjar.sources += [ | |
'home/RecyclerViewItemClickListener.java', | |
'home/RemoteTabsBaseFragment.java', | |
'home/RemoteTabsExpandableListFragment.java', | |
'home/RemoteTabsExpandableListState.java', | |
'home/RemoteTabsPanel.java', | |
'home/RemoteTabsSplitPlaneFragment.java', | |
'home/RemoteTabsStaticFragment.java', | |
'home/SearchEngine.java', | |
+ 'home/SearchEngineAdapter.java', | |
'home/SearchEngineBar.java', | |
'home/SearchEngineRow.java', | |
'home/SearchLoader.java', | |
'home/SimpleCursorLoader.java', | |
'home/SpacingDecoration.java', | |
'home/TabMenuStrip.java', | |
'home/TabMenuStripLayout.java', | |
'home/TopSitesGridItemView.java', | |
diff --git a/mobile/android/base/resources/layout/search_engine_bar_item.xml b/mobile/android/base/resources/layout/search_engine_bar_item.xml | |
--- a/mobile/android/base/resources/layout/search_engine_bar_item.xml | |
+++ b/mobile/android/base/resources/layout/search_engine_bar_item.xml | |
@@ -12,16 +12,17 @@ | |
The actual width of the FrameLayout is calculated at runtime by the | |
SearchEngineBar class to spread the icons across the device's width. --> | |
<FrameLayout | |
xmlns:android="http://schemas.android.com/apk/res/android" | |
android:id="@+id/search_engine_icon_container" | |
android:layout_width="72dp" | |
android:layout_height="match_parent" | |
+ android:clickable="true" | |
android:background="@color/pressed_about_page_header_grey"> | |
<!-- Width & height are set to make the Favicons as sharp as possible | |
based on asset size. --> | |
<ImageView | |
android:id="@+id/search_engine_icon" | |
android:layout_width="24dp" | |
android:layout_height="24dp" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment