Bug 1027137 - PageActionLayout cleanup. r=wesj

This commit is contained in:
Brian Nicholson 2014-07-15 11:19:50 -07:00
parent 2c06430ae6
commit ae49ab84f8

View File

@ -15,6 +15,7 @@ import org.mozilla.gecko.util.NativeEventListener;
import org.mozilla.gecko.util.NativeJSObject; import org.mozilla.gecko.util.NativeJSObject;
import org.mozilla.gecko.util.ThreadUtils; import org.mozilla.gecko.util.ThreadUtils;
import org.mozilla.gecko.widget.GeckoPopupMenu; import org.mozilla.gecko.widget.GeckoPopupMenu;
import android.content.Context; import android.content.Context;
import android.content.res.Resources; import android.content.res.Resources;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
@ -27,6 +28,8 @@ import android.widget.ImageButton;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.LinearLayout; import android.widget.LinearLayout;
import java.util.Iterator;
import java.util.List;
import java.util.UUID; import java.util.UUID;
import java.util.ArrayList; import java.util.ArrayList;
@ -36,10 +39,11 @@ public class PageActionLayout extends LinearLayout implements NativeEventListene
private static final String MENU_BUTTON_KEY = "MENU_BUTTON_KEY"; private static final String MENU_BUTTON_KEY = "MENU_BUTTON_KEY";
private static final int DEFAULT_PAGE_ACTIONS_SHOWN = 2; private static final int DEFAULT_PAGE_ACTIONS_SHOWN = 2;
private ArrayList<PageAction> mPageActionList; private final Context mContext;
private final LinearLayout mLayout;
private final List<PageAction> mPageActionList;
private GeckoPopupMenu mPageActionsMenu; private GeckoPopupMenu mPageActionsMenu;
private Context mContext;
private LinearLayout mLayout;
// By default it's two, can be changed by calling setNumberShown(int) // By default it's two, can be changed by calling setNumberShown(int)
private int mMaxVisiblePageActions; private int mMaxVisiblePageActions;
@ -63,8 +67,8 @@ public class PageActionLayout extends LinearLayout implements NativeEventListene
mMaxVisiblePageActions = count; mMaxVisiblePageActions = count;
for(int index = 0; index < count; index++) { for (int index = 0; index < count; index++) {
if ((this.getChildCount() - 1) < index) { if ((getChildCount() - 1) < index) {
mLayout.addView(createImageButton()); mLayout.addView(createImageButton());
} }
} }
@ -117,14 +121,15 @@ public class PageActionLayout extends LinearLayout implements NativeEventListene
} }
} }
private void addPageAction(final String id, final String title, final String imageData, final OnPageActionClickListeners mOnPageActionClickListeners, boolean mImportant) { private void addPageAction(final String id, final String title, final String imageData,
final OnPageActionClickListeners onPageActionClickListeners, boolean important) {
ThreadUtils.assertOnUiThread(); ThreadUtils.assertOnUiThread();
final PageAction pageAction = new PageAction(id, title, null, mOnPageActionClickListeners, mImportant); final PageAction pageAction = new PageAction(id, title, null, onPageActionClickListeners, important);
int insertAt = mPageActionList.size(); int insertAt = mPageActionList.size();
while(insertAt > 0 && mPageActionList.get(insertAt-1).isImportant()) { while (insertAt > 0 && mPageActionList.get(insertAt - 1).isImportant()) {
insertAt--; insertAt--;
} }
mPageActionList.add(insertAt, pageAction); mPageActionList.add(insertAt, pageAction);
@ -142,9 +147,11 @@ public class PageActionLayout extends LinearLayout implements NativeEventListene
private void removePageAction(String id) { private void removePageAction(String id) {
ThreadUtils.assertOnUiThread(); ThreadUtils.assertOnUiThread();
for(int i = 0; i < mPageActionList.size(); i++) { final Iterator<PageAction> iter = mPageActionList.iterator();
if (mPageActionList.get(i).getID().equals(id)) { while (iter.hasNext()) {
mPageActionList.remove(i); final PageAction pageAction = iter.next();
if (pageAction.getID().equals(id)) {
iter.remove();
refreshPageActionIcons(); refreshPageActionIcons();
return; return;
} }
@ -154,8 +161,9 @@ public class PageActionLayout extends LinearLayout implements NativeEventListene
private ImageButton createImageButton() { private ImageButton createImageButton() {
ThreadUtils.assertOnUiThread(); ThreadUtils.assertOnUiThread();
final int width = mContext.getResources().getDimensionPixelSize(R.dimen.page_action_button_width);
ImageButton imageButton = new ImageButton(mContext, null, R.style.UrlBar_ImageButton_Icon); ImageButton imageButton = new ImageButton(mContext, null, R.style.UrlBar_ImageButton_Icon);
imageButton.setLayoutParams(new LayoutParams(mContext.getResources().getDimensionPixelSize(R.dimen.page_action_button_width), LayoutParams.MATCH_PARENT)); imageButton.setLayoutParams(new LayoutParams(width, LayoutParams.MATCH_PARENT));
imageButton.setScaleType(ImageView.ScaleType.CENTER_INSIDE); imageButton.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
imageButton.setOnClickListener(this); imageButton.setOnClickListener(this);
imageButton.setOnLongClickListener(this); imageButton.setOnLongClickListener(this);
@ -186,6 +194,8 @@ public class PageActionLayout extends LinearLayout implements NativeEventListene
} }
private void setActionForView(final ImageButton view, final PageAction pageAction) { private void setActionForView(final ImageButton view, final PageAction pageAction) {
ThreadUtils.assertOnUiThread();
if (pageAction == null) { if (pageAction == null) {
view.setTag(null); view.setTag(null);
view.setImageDrawable(null); view.setImageDrawable(null);
@ -204,12 +214,13 @@ public class PageActionLayout extends LinearLayout implements NativeEventListene
ThreadUtils.assertOnUiThread(); ThreadUtils.assertOnUiThread();
final Resources resources = mContext.getResources(); final Resources resources = mContext.getResources();
for(int index = 0; index < this.getChildCount(); index++) { for (int i = 0; i < this.getChildCount(); i++) {
final ImageButton v = (ImageButton)this.getChildAt(index); final ImageButton v = (ImageButton) this.getChildAt(i);
final PageAction pageAction = getPageActionForViewAt(index); final PageAction pageAction = getPageActionForViewAt(i);
// If there are more pageactions then buttons, set the menu icon. Otherwise set the page action's icon if there is a page action. // If there are more page actions than buttons, set the menu icon.
if (index == (this.getChildCount() - 1) && mPageActionList.size() > mMaxVisiblePageActions) { // Otherwise, set the page action's icon if there is a page action.
if ((i == this.getChildCount() - 1) && (mPageActionList.size() > mMaxVisiblePageActions)) {
v.setTag(MENU_BUTTON_KEY); v.setTag(MENU_BUTTON_KEY);
v.setImageDrawable(resources.getDrawable(R.drawable.icon_pageaction)); v.setImageDrawable(resources.getDrawable(R.drawable.icon_pageaction));
v.setVisibility((pageAction != null) ? View.VISIBLE : View.GONE); v.setVisibility((pageAction != null) ? View.VISIBLE : View.GONE);
@ -233,12 +244,12 @@ public class PageActionLayout extends LinearLayout implements NativeEventListene
* and hence we maintain the insertion order of the child Views which is essentially the reverse of their index * and hence we maintain the insertion order of the child Views which is essentially the reverse of their index
*/ */
int buttonIndex = (this.getChildCount() - 1) - index; final int buttonIndex = (this.getChildCount() - 1) - index;
int totalVisibleButtons = ((mPageActionList.size() < this.getChildCount()) ? mPageActionList.size() : this.getChildCount());
if (mPageActionList.size() > buttonIndex) { if (mPageActionList.size() > buttonIndex) {
// Return the pageactions starting from the end of the list for the number of visible pageactions. // Return the pageactions starting from the end of the list for the number of visible pageactions.
return mPageActionList.get((mPageActionList.size() - totalVisibleButtons) + buttonIndex); final int buttonCount = Math.min(mPageActionList.size(), getChildCount());
return mPageActionList.get((mPageActionList.size() - buttonCount) + buttonIndex);
} }
return null; return null;
} }
@ -246,8 +257,7 @@ public class PageActionLayout extends LinearLayout implements NativeEventListene
private PageAction getPageActionWithId(String id) { private PageAction getPageActionWithId(String id) {
ThreadUtils.assertOnUiThread(); ThreadUtils.assertOnUiThread();
for(int i = 0; i < mPageActionList.size(); i++) { for (PageAction pageAction : mPageActionList) {
PageAction pageAction = mPageActionList.get(i);
if (pageAction.getID().equals(id)) { if (pageAction.getID().equals(id)) {
return pageAction; return pageAction;
} }
@ -255,17 +265,17 @@ public class PageActionLayout extends LinearLayout implements NativeEventListene
return null; return null;
} }
private void showMenu(View mPageActionButton, int toShow) { private void showMenu(View pageActionButton, int toShow) {
ThreadUtils.assertOnUiThread(); ThreadUtils.assertOnUiThread();
if (mPageActionsMenu == null) { if (mPageActionsMenu == null) {
mPageActionsMenu = new GeckoPopupMenu(mPageActionButton.getContext(), mPageActionButton); mPageActionsMenu = new GeckoPopupMenu(pageActionButton.getContext(), pageActionButton);
mPageActionsMenu.inflate(0); mPageActionsMenu.inflate(0);
mPageActionsMenu.setOnMenuItemClickListener(new GeckoPopupMenu.OnMenuItemClickListener() { mPageActionsMenu.setOnMenuItemClickListener(new GeckoPopupMenu.OnMenuItemClickListener() {
@Override @Override
public boolean onMenuItemClick(MenuItem item) { public boolean onMenuItemClick(MenuItem item) {
int id = item.getItemId(); int id = item.getItemId();
for(int i = 0; i < mPageActionList.size(); i++) { for (int i = 0; i < mPageActionList.size(); i++) {
PageAction pageAction = mPageActionList.get(i); PageAction pageAction = mPageActionList.get(i);
if (pageAction.key() == id) { if (pageAction.key() == id) {
pageAction.onClick(); pageAction.onClick();
@ -279,12 +289,10 @@ public class PageActionLayout extends LinearLayout implements NativeEventListene
Menu menu = mPageActionsMenu.getMenu(); Menu menu = mPageActionsMenu.getMenu();
menu.clear(); menu.clear();
for(int i = 0; i < mPageActionList.size(); i++) { for (int i = 0; i < mPageActionList.size() && i < toShow; i++) {
if (i < toShow) { PageAction pageAction = mPageActionList.get(i);
PageAction pageAction = mPageActionList.get(i); MenuItem item = menu.add(Menu.NONE, pageAction.key(), Menu.NONE, pageAction.getTitle());
MenuItem item = menu.add(Menu.NONE, pageAction.key(), Menu.NONE, pageAction.getTitle()); item.setIcon(pageAction.getDrawable());
item.setIcon(pageAction.getDrawable());
}
} }
mPageActionsMenu.show(); mPageActionsMenu.show();
} }