Commit 5d0c1419 authored by Marcin Wiacek's avatar Marcin Wiacek Committed by Commit Bot

@IntDef cleanup in browser/java/appmenu

@IntDef/@StringDef annotation are preferred way for declaring
set of String/int values:

1. they need less space in APK than enum, see
https://developer.android.com/topic/performance/reduce-apk-size#remove-enums
2. they give more control over allowed values than "static final" values

Main goal of patch is writing "static final" values, enum
and some classes in one common @IntDef/@StringDef form:

1. with @IntDef/@StringDef first, @Retention second
   and related @interface third
2. with values inside @interface
3. with NUM_ENTRIES declaring number of entries if necessary
4. with comment about numbering from 0 without gaps when necessary
5. with @Retention(RetentionPolicy.SOURCE)
6. without "static final" in the @interface

Additionally there are done some other trivial cleanups.

Change-Id: Ie6b59c0f1a8ec6fe50d3177619f0768798f97508
Reviewed-on: https://chromium-review.googlesource.com/1128092
Commit-Queue: Marcin Wiącek <marcin@mwiacek.com>
Commit-Queue: Theresa <twellington@chromium.org>
Reviewed-by: default avatarTheresa <twellington@chromium.org>
Cr-Commit-Position: refs/heads/master@{#574382}
parent 0c4e06c2
...@@ -9,6 +9,7 @@ import android.animation.AnimatorListenerAdapter; ...@@ -9,6 +9,7 @@ import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet; import android.animation.AnimatorSet;
import android.animation.ObjectAnimator; import android.animation.ObjectAnimator;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.support.annotation.IntDef;
import android.support.v7.content.res.AppCompatResources; import android.support.v7.content.res.AppCompatResources;
import android.text.TextUtils; import android.text.TextUtils;
import android.view.LayoutInflater; import android.view.LayoutInflater;
...@@ -27,6 +28,8 @@ import org.chromium.chrome.browser.widget.ViewHighlighter; ...@@ -27,6 +28,8 @@ import org.chromium.chrome.browser.widget.ViewHighlighter;
import org.chromium.ui.base.LocalizationUtils; import org.chromium.ui.base.LocalizationUtils;
import org.chromium.ui.interpolators.BakedBezierInterpolator; import org.chromium.ui.interpolators.BakedBezierInterpolator;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.List; import java.util.List;
/** /**
...@@ -41,41 +44,41 @@ import java.util.List; ...@@ -41,41 +44,41 @@ import java.util.List;
* 3) Hope that the icon row still fits on small phones. * 3) Hope that the icon row still fits on small phones.
*/ */
class AppMenuAdapter extends BaseAdapter { class AppMenuAdapter extends BaseAdapter {
/** @IntDef({MenuItemType.STANDARD, MenuItemType.TITLE_BUTTON, MenuItemType.THREE_BUTTON,
* Regular Android menu item that contains a title and an icon if icon is specified. MenuItemType.FOUR_BUTTON, MenuItemType.FIVE_BUTTON, MenuItemType.UPDATE})
*/ @Retention(RetentionPolicy.SOURCE)
private static final int STANDARD_MENU_ITEM = 0; private @interface MenuItemType {
/**
/** * Regular Android menu item that contains a title and an icon if icon is specified.
* Menu item that has two buttons, the first one is a title and the second one is an icon. */
* It is different from the regular menu item because it contains two separate buttons. int STANDARD = 0;
*/ /**
private static final int TITLE_BUTTON_MENU_ITEM = 1; * Menu item for updating Chrome; uses a custom layout.
*/
/** int UPDATE = 1;
* Menu item that has three buttons. Every one of these buttons is displayed as an icon. /**
*/ * Menu item that has two buttons, the first one is a title and the second one is an icon.
private static final int THREE_BUTTON_MENU_ITEM = 2; * It is different from the regular menu item because it contains two separate buttons.
*/
/** int TITLE_BUTTON = 2;
* Menu item that has four buttons. Every one of these buttons is displayed as an icon. /**
*/ * Menu item that has three buttons. Every one of these buttons is displayed as an icon.
private static final int FOUR_BUTTON_MENU_ITEM = 3; */
int THREE_BUTTON = 3;
/** /**
* Menu item that has five buttons. Every one of these buttons is displayed as an icon. * Menu item that has four buttons. Every one of these buttons is displayed as an icon.
*/ */
private static final int FIVE_BUTTON_MENU_ITEM = 4; int FOUR_BUTTON = 4;
/**
/** * Menu item that has five buttons. Every one of these buttons is displayed as an icon.
* Menu item for updating Chrome; uses a custom layout. */
*/ int FIVE_BUTTON = 5;
private static final int UPDATE_MENU_ITEM = 5; /**
* The number of view types specified above. If you add a view type you MUST increment
/** * this.
* The number of view types specified above. If you add a view type you MUST increment this. */
*/ int NUM_ENTRIES = 6;
private static final int VIEW_TYPE_COUNT = 6; }
/** IDs of all of the buttons in icon_row_menu_item.xml. */ /** IDs of all of the buttons in icon_row_menu_item.xml. */
private static final int[] BUTTON_IDS = { private static final int[] BUTTON_IDS = {
...@@ -117,26 +120,26 @@ class AppMenuAdapter extends BaseAdapter { ...@@ -117,26 +120,26 @@ class AppMenuAdapter extends BaseAdapter {
@Override @Override
public int getViewTypeCount() { public int getViewTypeCount() {
return VIEW_TYPE_COUNT; return MenuItemType.NUM_ENTRIES;
} }
@Override @Override
public int getItemViewType(int position) { public @MenuItemType int getItemViewType(int position) {
MenuItem item = getItem(position); MenuItem item = getItem(position);
int viewCount = item.hasSubMenu() ? item.getSubMenu().size() : 1; int viewCount = item.hasSubMenu() ? item.getSubMenu().size() : 1;
if (item.getItemId() == R.id.update_menu_id) { if (item.getItemId() == R.id.update_menu_id) {
return UPDATE_MENU_ITEM; return MenuItemType.UPDATE;
} else if (viewCount == 5) {
return FIVE_BUTTON_MENU_ITEM;
} else if (viewCount == 4) {
return FOUR_BUTTON_MENU_ITEM;
} else if (viewCount == 3) {
return THREE_BUTTON_MENU_ITEM;
} else if (viewCount == 2) { } else if (viewCount == 2) {
return TITLE_BUTTON_MENU_ITEM; return MenuItemType.TITLE_BUTTON;
} else if (viewCount == 3) {
return MenuItemType.THREE_BUTTON;
} else if (viewCount == 4) {
return MenuItemType.FOUR_BUTTON;
} else if (viewCount == 5) {
return MenuItemType.FIVE_BUTTON;
} }
return STANDARD_MENU_ITEM; return MenuItemType.STANDARD;
} }
@Override @Override
...@@ -156,7 +159,7 @@ class AppMenuAdapter extends BaseAdapter { ...@@ -156,7 +159,7 @@ class AppMenuAdapter extends BaseAdapter {
public View getView(int position, View convertView, ViewGroup parent) { public View getView(int position, View convertView, ViewGroup parent) {
final MenuItem item = getItem(position); final MenuItem item = getItem(position);
switch (getItemViewType(position)) { switch (getItemViewType(position)) {
case STANDARD_MENU_ITEM: { case MenuItemType.STANDARD: {
StandardMenuItemViewHolder holder = null; StandardMenuItemViewHolder holder = null;
if (convertView == null if (convertView == null
|| !(convertView.getTag() instanceof StandardMenuItemViewHolder)) { || !(convertView.getTag() instanceof StandardMenuItemViewHolder)) {
...@@ -172,11 +175,10 @@ class AppMenuAdapter extends BaseAdapter { ...@@ -172,11 +175,10 @@ class AppMenuAdapter extends BaseAdapter {
} else { } else {
holder = (StandardMenuItemViewHolder) convertView.getTag(); holder = (StandardMenuItemViewHolder) convertView.getTag();
} }
setupStandardMenuItemViewHolder(holder, convertView, item); setupStandardMenuItemViewHolder(holder, convertView, item);
break; break;
} }
case UPDATE_MENU_ITEM: { case MenuItemType.UPDATE: {
CustomMenuItemViewHolder holder = null; CustomMenuItemViewHolder holder = null;
if (convertView == null if (convertView == null
|| !(convertView.getTag() instanceof CustomMenuItemViewHolder)) { || !(convertView.getTag() instanceof CustomMenuItemViewHolder)) {
...@@ -193,7 +195,6 @@ class AppMenuAdapter extends BaseAdapter { ...@@ -193,7 +195,6 @@ class AppMenuAdapter extends BaseAdapter {
} else { } else {
holder = (CustomMenuItemViewHolder) convertView.getTag(); holder = (CustomMenuItemViewHolder) convertView.getTag();
} }
setupStandardMenuItemViewHolder(holder, convertView, item); setupStandardMenuItemViewHolder(holder, convertView, item);
String summary = UpdateMenuItemHelper.getInstance().getMenuItemSummaryText( String summary = UpdateMenuItemHelper.getInstance().getMenuItemSummaryText(
mInflater.getContext()); mInflater.getContext());
...@@ -202,22 +203,18 @@ class AppMenuAdapter extends BaseAdapter { ...@@ -202,22 +203,18 @@ class AppMenuAdapter extends BaseAdapter {
} else { } else {
holder.summary.setText(summary); holder.summary.setText(summary);
} }
break; break;
} }
case THREE_BUTTON_MENU_ITEM: { case MenuItemType.THREE_BUTTON:
convertView = createMenuItemRow(convertView, parent, item, 3); convertView = createMenuItemRow(convertView, parent, item, 3);
break; break;
} case MenuItemType.FOUR_BUTTON:
case FOUR_BUTTON_MENU_ITEM: {
convertView = createMenuItemRow(convertView, parent, item, 4); convertView = createMenuItemRow(convertView, parent, item, 4);
break; break;
} case MenuItemType.FIVE_BUTTON:
case FIVE_BUTTON_MENU_ITEM: {
convertView = createMenuItemRow(convertView, parent, item, 5); convertView = createMenuItemRow(convertView, parent, item, 5);
break; break;
} case MenuItemType.TITLE_BUTTON: {
case TITLE_BUTTON_MENU_ITEM: {
assert item.hasSubMenu(); assert item.hasSubMenu();
final MenuItem titleItem = item.getSubMenu().getItem(0); final MenuItem titleItem = item.getSubMenu().getItem(0);
final MenuItem subItem = item.getSubMenu().getItem(1); final MenuItem subItem = item.getSubMenu().getItem(1);
......
...@@ -9,6 +9,7 @@ import android.annotation.SuppressLint; ...@@ -9,6 +9,7 @@ import android.annotation.SuppressLint;
import android.app.Activity; import android.app.Activity;
import android.content.res.Resources; import android.content.res.Resources;
import android.graphics.Rect; import android.graphics.Rect;
import android.support.annotation.IntDef;
import android.view.MotionEvent; import android.view.MotionEvent;
import android.view.View; import android.view.View;
import android.view.ViewConfiguration; import android.view.ViewConfiguration;
...@@ -20,6 +21,8 @@ import org.chromium.base.metrics.RecordHistogram; ...@@ -20,6 +21,8 @@ import org.chromium.base.metrics.RecordHistogram;
import org.chromium.base.metrics.RecordUserAction; import org.chromium.base.metrics.RecordUserAction;
import org.chromium.chrome.R; import org.chromium.chrome.R;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
...@@ -35,9 +38,13 @@ class AppMenuDragHelper { ...@@ -35,9 +38,13 @@ class AppMenuDragHelper {
private final AppMenu mAppMenu; private final AppMenu mAppMenu;
// Internally used action constants for dragging. // Internally used action constants for dragging.
private static final int ITEM_ACTION_HIGHLIGHT = 0; @IntDef({ItemAction.HIGHLIGHT, ItemAction.PERFORM, ItemAction.CLEAR_HIGHLIGHT_ALL})
private static final int ITEM_ACTION_PERFORM = 1; @Retention(RetentionPolicy.SOURCE)
private static final int ITEM_ACTION_CLEAR_HIGHLIGHT_ALL = 2; private @interface ItemAction {
int HIGHLIGHT = 0;
int PERFORM = 1;
int CLEAR_HIGHLIGHT_ALL = 2;
}
private static final float AUTO_SCROLL_AREA_MAX_RATIO = 0.25f; private static final float AUTO_SCROLL_AREA_MAX_RATIO = 0.25f;
...@@ -83,8 +90,8 @@ class AppMenuDragHelper { ...@@ -83,8 +90,8 @@ class AppMenuDragHelper {
// Force touch move event to highlight items correctly for the scrolled position. // Force touch move event to highlight items correctly for the scrolled position.
if (!Float.isNaN(mLastTouchX) && !Float.isNaN(mLastTouchY)) { if (!Float.isNaN(mLastTouchX) && !Float.isNaN(mLastTouchY)) {
menuItemAction(Math.round(mLastTouchX), Math.round(mLastTouchY), menuItemAction(
ITEM_ACTION_HIGHLIGHT); Math.round(mLastTouchX), Math.round(mLastTouchY), ItemAction.HIGHLIGHT);
} }
}); });
...@@ -122,7 +129,7 @@ class AppMenuDragHelper { ...@@ -122,7 +129,7 @@ class AppMenuDragHelper {
// needed to by menuItemAction. Only clear highlighting if the menu is still showing. // needed to by menuItemAction. Only clear highlighting if the menu is still showing.
// See crbug.com/589805. // See crbug.com/589805.
if (mAppMenu.getPopup().isShowing()) { if (mAppMenu.getPopup().isShowing()) {
menuItemAction(0, 0, ITEM_ACTION_CLEAR_HIGHLIGHT_ALL); menuItemAction(0, 0, ItemAction.CLEAR_HIGHLIGHT_ALL);
} }
mDragScrolling.cancel(); mDragScrolling.cancel();
} }
...@@ -175,14 +182,15 @@ class AppMenuDragHelper { ...@@ -175,14 +182,15 @@ class AppMenuDragHelper {
if (!mDragScrolling.isRunning()) return false; if (!mDragScrolling.isRunning()) return false;
boolean didPerformClick = false; boolean didPerformClick = false;
int itemAction = ITEM_ACTION_CLEAR_HIGHLIGHT_ALL; @ItemAction
int itemAction = ItemAction.CLEAR_HIGHLIGHT_ALL;
switch (eventActionMasked) { switch (eventActionMasked) {
case MotionEvent.ACTION_DOWN: case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_MOVE: case MotionEvent.ACTION_MOVE:
itemAction = ITEM_ACTION_HIGHLIGHT; itemAction = ItemAction.HIGHLIGHT;
break; break;
case MotionEvent.ACTION_UP: case MotionEvent.ACTION_UP:
itemAction = ITEM_ACTION_PERFORM; itemAction = ItemAction.PERFORM;
break; break;
default: default:
break; break;
...@@ -231,7 +239,7 @@ class AppMenuDragHelper { ...@@ -231,7 +239,7 @@ class AppMenuDragHelper {
* @param action Action type to perform, it should be one of ITEM_ACTION_* constants. * @param action Action type to perform, it should be one of ITEM_ACTION_* constants.
* @return true whether or not a menu item is performed (executed). * @return true whether or not a menu item is performed (executed).
*/ */
private boolean menuItemAction(int screenX, int screenY, int action) { private boolean menuItemAction(int screenX, int screenY, @ItemAction int action) {
ListView listView = mAppMenu.getListView(); ListView listView = mAppMenu.getListView();
// Starting M, we have a popup menu animation that slides down. If we process dragging // Starting M, we have a popup menu animation that slides down. If we process dragging
...@@ -268,17 +276,17 @@ class AppMenuDragHelper { ...@@ -268,17 +276,17 @@ class AppMenuDragHelper {
&& getScreenVisibleRect(itemView).contains(screenX, screenY); && getScreenVisibleRect(itemView).contains(screenX, screenY);
switch (action) { switch (action) {
case ITEM_ACTION_HIGHLIGHT: case ItemAction.HIGHLIGHT:
itemView.setPressed(shouldPerform); itemView.setPressed(shouldPerform);
break; break;
case ITEM_ACTION_PERFORM: case ItemAction.PERFORM:
if (shouldPerform) { if (shouldPerform) {
RecordUserAction.record("MobileUsingMenuBySwButtonDragging"); RecordUserAction.record("MobileUsingMenuBySwButtonDragging");
itemView.performClick(); itemView.performClick();
didPerformClick = true; didPerformClick = true;
} }
break; break;
case ITEM_ACTION_CLEAR_HIGHLIGHT_ALL: case ItemAction.CLEAR_HIGHLIGHT_ALL:
itemView.setPressed(false); itemView.setPressed(false);
break; break;
default: default:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment