Bug 958889 - Part 2: Miscellaneous code clean up. r=bnicholson
authorMichael Comella <michael.l.comella@gmail.com>
Tue, 29 Apr 2014 11:21:38 -0700
changeset 181248 c29c34aea5b089b43a79faac68ce7388bcbc12ef
parent 181247 e68c18a91f3d95e3696e827a45090c8ab5f71e69
child 181249 01acb9c035091d8ce9262df36c3c84e0af3736d4
push id272
push userpvanderbeken@mozilla.com
push dateMon, 05 May 2014 16:31:18 +0000
reviewersbnicholson
bugs958889
milestone32.0a1
Bug 958889 - Part 2: Miscellaneous code clean up. r=bnicholson
mobile/android/base/tabspanel/TabsPanel.java
mobile/android/base/tabspanel/TabsTray.java
mobile/android/base/widget/TwoWayView.java
--- a/mobile/android/base/tabspanel/TabsPanel.java
+++ b/mobile/android/base/tabspanel/TabsPanel.java
@@ -28,17 +28,17 @@ import android.widget.Button;
 import android.widget.FrameLayout;
 import android.widget.ImageButton;
 import android.widget.LinearLayout;
 import android.widget.RelativeLayout;
 
 public class TabsPanel extends LinearLayout
                        implements LightweightTheme.OnChangeListener,
                                   IconTabWidget.OnTabChangedListener {
-    private static final String LOGTAG = "GeckoTabsPanel";
+    private static final String LOGTAG = "Gecko" + TabsPanel.class.getSimpleName();
 
     public static enum Panel {
         NORMAL_TABS,
         PRIVATE_TABS,
         REMOTE_TABS
     }
 
     public static interface PanelView {
@@ -73,18 +73,18 @@ public class TabsPanel extends LinearLay
     private boolean mVisible;
 
     public TabsPanel(Context context, AttributeSet attrs) {
         super(context, attrs);
         mContext = context;
         mActivity = (GeckoApp) context;
         mTheme = ((GeckoApplication) context.getApplicationContext()).getLightweightTheme();
 
-        setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,
-                                                      LinearLayout.LayoutParams.FILL_PARENT));
+        setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
+                                                      LinearLayout.LayoutParams.MATCH_PARENT));
         setOrientation(LinearLayout.VERTICAL);
 
         mCurrentPanel = Panel.NORMAL_TABS;
         mVisible = false;
 
         mIsSideBar = false;
 
         LayoutInflater.from(context).inflate(R.layout.tabs_panel, this);
@@ -122,20 +122,21 @@ public class TabsPanel extends LinearLay
         if (!GeckoProfile.get(mContext).inGuestMode()) {
             mTabWidget.addTab(R.drawable.tabs_synced, R.string.tabs_synced);
         }
 
         mTabWidget.setTabSelectionListener(this);
     }
 
     public void addTab() {
-        if (mCurrentPanel == Panel.NORMAL_TABS)
+        if (mCurrentPanel == Panel.NORMAL_TABS) {
            mActivity.addTab();
-        else
+        } else {
            mActivity.addPrivateTab();
+        }
 
         mActivity.autoHideTabs();
     }
 
     @Override
     public void onTabChanged(int index) {
         if (index == 0)
             show(Panel.NORMAL_TABS, false);
@@ -175,16 +176,17 @@ public class TabsPanel extends LinearLay
 
     @Override
     public void onDetachedFromWindow() {
         super.onDetachedFromWindow();
         mTheme.removeListener(this);
     }
 
     @Override
+    @SuppressWarnings("deprecation") // setBackgroundDrawable deprecated by API level 16
     public void onLightweightThemeChanged() {
         final int background = getResources().getColor(R.color.background_tabs);
         final LightweightThemeDrawable drawable = mTheme.getColorDrawable(this, background, true);
         if (drawable == null)
             return;
 
         drawable.setAlpha(34, 0);
         setBackgroundDrawable(drawable);
@@ -236,17 +238,17 @@ public class TabsPanel extends LinearLay
     static class TabsPanelToolbar extends LinearLayout
                                   implements LightweightTheme.OnChangeListener {
         private final LightweightTheme mTheme;
 
         public TabsPanelToolbar(Context context, AttributeSet attrs) {
             super(context, attrs);
             mTheme = ((GeckoApplication) context.getApplicationContext()).getLightweightTheme();
 
-            setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,
+            setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                                                           (int) context.getResources().getDimension(R.dimen.browser_toolbar_height)));
 
             setOrientation(LinearLayout.HORIZONTAL);
         }
 
         @Override
         public void onAttachedToWindow() {
             super.onAttachedToWindow();
@@ -255,16 +257,17 @@ public class TabsPanel extends LinearLay
 
         @Override
         public void onDetachedFromWindow() {
             super.onDetachedFromWindow();
             mTheme.removeListener(this);
         }
 
         @Override
+        @SuppressWarnings("deprecation") // setBackgroundDrawable deprecated by API level 16
         public void onLightweightThemeChanged() {
             final int background = getResources().getColor(R.color.background_tabs);
             final LightweightThemeDrawable drawable = mTheme.getColorDrawable(this, background);
             if (drawable == null)
                 return;
 
             drawable.setAlpha(34, 34);
             setBackgroundDrawable(drawable);
@@ -281,40 +284,46 @@ public class TabsPanel extends LinearLay
             onLightweightThemeChanged();
         }
     }
 
     public void show(Panel panel) {
         show(panel, true);
     }
 
-    public void show(Panel panel, boolean shouldResize) {
+    public void show(Panel panelToShow, boolean shouldResize) {
         if (!isShown())
             setVisibility(View.VISIBLE);
 
         if (mPanel != null) {
             // Hide the old panel.
             mPanel.hide();
         }
 
         final boolean showAnimation = !mVisible;
         mVisible = true;
-        mCurrentPanel = panel;
+        mCurrentPanel = panelToShow;
 
-        int index = panel.ordinal();
+        int index = panelToShow.ordinal();
         mTabWidget.setCurrentTab(index);
 
-        if (index == 0) {
-            mPanel = mPanelNormal;
-        } else if (index == 1) {
-            mPanel = mPanelPrivate;
-        } else {
-            mPanel = mPanelRemote;
+        switch (panelToShow) {
+            case NORMAL_TABS:
+                mPanel = mPanelNormal;
+                break;
+            case PRIVATE_TABS:
+                mPanel = mPanelPrivate;
+                break;
+            case REMOTE_TABS:
+                mPanel = mPanelRemote;
+                break;
+
+            default:
+                throw new IllegalArgumentException("Unknown panel type " + panelToShow);
         }
-
         mPanel.show();
 
         if (mCurrentPanel == Panel.REMOTE_TABS) {
             if (mFooter != null)
                 mFooter.setVisibility(View.GONE);
 
             mAddTab.setVisibility(View.INVISIBLE);
         } else {
--- a/mobile/android/base/tabspanel/TabsTray.java
+++ b/mobile/android/base/tabspanel/TabsTray.java
@@ -34,32 +34,31 @@ import android.view.ViewGroup;
 import android.widget.BaseAdapter;
 import android.widget.Button;
 import android.widget.ImageButton;
 import android.widget.ImageView;
 import android.widget.TextView;
 
 class TabsTray extends TwoWayView
     implements TabsPanel.PanelView {
-    private static final String LOGTAG = "GeckoTabsTray";
+    private static final String LOGTAG = "Gecko" + TabsTray.class.getSimpleName();
 
     private Context mContext;
     private TabsPanel mTabsPanel;
 
     private TabsAdapter mTabsAdapter;
 
     private List<View> mPendingClosedTabs;
     private int mCloseAnimationCount;
 
     private TabSwipeGestureListener mSwipeListener;
 
     // Time to animate non-flinged tabs of screen, in milliseconds
     private static final int ANIMATION_DURATION = 250;
 
-    private static final String ABOUT_HOME = "about:home";
     private int mOriginalSize = 0;
 
     public TabsTray(Context context, AttributeSet attrs) {
         super(context, attrs);
         mContext = context;
 
         mCloseAnimationCount = 0;
         mPendingClosedTabs = new ArrayList<View>();
@@ -425,27 +424,25 @@ class TabsTray extends TwoWayView
 
         private int mMaxFlingVelocity;
         private VelocityTracker mVelocityTracker;
 
         private int mListWidth = 1;
         private int mListHeight = 1;
 
         private View mSwipeView;
-        private int mSwipeViewPosition;
         private Runnable mPendingCheckForTap;
 
         private float mSwipeStartX;
         private float mSwipeStartY;
         private boolean mSwiping;
         private boolean mEnabled;
 
         public TabSwipeGestureListener() {
             mSwipeView = null;
-            mSwipeViewPosition = TwoWayView.INVALID_POSITION;
             mSwiping = false;
             mEnabled = true;
 
             ViewConfiguration vc = ViewConfiguration.get(TabsTray.this.getContext());
             mSwipeThreshold = vc.getScaledTouchSlop();
             mMinFlingVelocity = (int) (getContext().getResources().getDisplayMetrics().density * MIN_VELOCITY);
             mMaxFlingVelocity = vc.getScaledMaximumFlingVelocity();
         }
@@ -487,17 +484,16 @@ class TabsTray extends TwoWayView
                     final float y = e.getRawY();
 
                     // Find out which view is being touched
                     mSwipeView = findViewAt(x, y);
 
                     if (mSwipeView != null) {
                         mSwipeStartX = e.getRawX();
                         mSwipeStartY = e.getRawY();
-                        mSwipeViewPosition = TabsTray.this.getPositionForView(mSwipeView);
 
                         mVelocityTracker = VelocityTracker.obtain();
                         mVelocityTracker.addMovement(e);
                     }
 
                     view.onTouchEvent(e);
                     return true;
                 }
@@ -508,16 +504,19 @@ class TabsTray extends TwoWayView
 
                     cancelCheckForTap();
                     mSwipeView.setPressed(false);
 
                     if (!mSwiping) {
                         TabRow tab = (TabRow) mSwipeView.getTag();
                         Tabs.getInstance().selectTab(tab.id);
                         autoHidePanel();
+
+                        mVelocityTracker.recycle();
+                        mVelocityTracker = null;
                         break;
                     }
 
                     mVelocityTracker.addMovement(e);
                     mVelocityTracker.computeCurrentVelocity(1000, mMaxFlingVelocity);
 
                     float velocityX = Math.abs(mVelocityTracker.getXVelocity());
                     float velocityY = Math.abs(mVelocityTracker.getYVelocity());
@@ -554,19 +553,19 @@ class TabsTray extends TwoWayView
                         dismissTranslation = (dismissDirection ? mListHeight : -mListHeight);
                      }
 
                     if (dismiss)
                         animateClose(mSwipeView, dismissTranslation);
                     else
                         animateCancel(mSwipeView);
 
+                    mVelocityTracker.recycle();
                     mVelocityTracker = null;
                     mSwipeView = null;
-                    mSwipeViewPosition = TwoWayView.INVALID_POSITION;
 
                     mSwipeStartX = 0;
                     mSwipeStartY = 0;
                     mSwiping = false;
 
                     break;
                 }
 
@@ -599,16 +598,17 @@ class TabsTray extends TwoWayView
                         tab.close.setVisibility(View.INVISIBLE);
 
                         // Stops listview from highlighting the touched item
                         // in the list when swiping.
                         MotionEvent cancelEvent = MotionEvent.obtain(e);
                         cancelEvent.setAction(MotionEvent.ACTION_CANCEL |
                                 (e.getActionIndex() << MotionEvent.ACTION_POINTER_INDEX_SHIFT));
                         TabsTray.this.onTouchEvent(cancelEvent);
+                        cancelEvent.recycle();
                     }
 
                     if (mSwiping) {
                         if (isVertical)
                             ViewHelper.setTranslationX(mSwipeView, delta);
                         else
                             ViewHelper.setTranslationY(mSwipeView, delta);
 
--- a/mobile/android/base/widget/TwoWayView.java
+++ b/mobile/android/base/widget/TwoWayView.java
@@ -1324,17 +1324,17 @@ public class TwoWayView extends AdapterV
 
             final float diff = pos - mLastTouchPos + mTouchRemainderPos;
             final int delta = (int) diff;
             mTouchRemainderPos = diff - delta;
 
             if (maybeStartScrolling(delta)) {
                 return true;
             }
-            
+
             break;
         }
 
         case MotionEvent.ACTION_CANCEL:
         case MotionEvent.ACTION_UP:
             mActivePointerId = INVALID_POINTER;
             mTouchMode = TOUCH_MODE_REST;
             recycleVelocityTracker();
@@ -1443,16 +1443,18 @@ public class TwoWayView extends AdapterV
                 maybeScroll(delta);
                 break;
             }
 
             break;
         }
 
         case MotionEvent.ACTION_CANCEL:
+            // If this MotionEvent has been created by us, be sure not to store
+            // pointers to it outside of this method call because we recycle it.
             cancelCheckForTap();
             mTouchMode = TOUCH_MODE_REST;
             reportScrollStateChange(OnScrollListener.SCROLL_STATE_IDLE);
 
             setPressed(false);
             View motionView = this.getChildAt(mMotionPosition - mFirstPosition);
             if (motionView != null) {
                 motionView.setPressed(false);
@@ -2379,17 +2381,17 @@ public class TwoWayView extends AdapterV
                 // Item already has enough of it visible, changing selection is good enough
                 return 0;
             }
 
             int amountToScroll = (goalStart - viewToMakeVisibleStart);
 
             if (mFirstPosition == 0) {
                 final View firstChild = getChildAt(0);
-                final int firstChildStart = (mIsVertical ? firstChild.getTop() : firstChild.getLeft()); 
+                final int firstChildStart = (mIsVertical ? firstChild.getTop() : firstChild.getLeft());
 
                 // First is first in list -> make sure we don't scroll past it
                 final int max = start - firstChildStart;
                 amountToScroll = Math.min(amountToScroll,  max);
             }
 
             return Math.min(amountToScroll, getMaxScrollAmount());
         }