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