Bug 942231 - Factor out HomePager's adapter into a separate file (r=margaret)
☠☠ backed out by 744c6ffb42e3 ☠ ☠
authorLucas Rocha <lucasr@mozilla.com>
Thu, 12 Dec 2013 10:55:35 +0000
changeset 159970 c4e1151b6a7ee890cc316c0dc0ec06b51486356c
parent 159969 f1769583f43b4264d297d3c9ae1cc902c643b7ac
child 159971 f279d53c2d4d9674f736901203ecf8a42c2763d2
push id3931
push userlrocha@mozilla.com
push dateThu, 12 Dec 2013 10:55:57 +0000
treeherderfx-team@64376a7a08df [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmargaret
bugs942231
milestone29.0a1
Bug 942231 - Factor out HomePager's adapter into a separate file (r=margaret)
mobile/android/base/home/HomeAdapter.java
mobile/android/base/home/HomePager.java
mobile/android/base/moz.build
new file mode 100644
--- /dev/null
+++ b/mobile/android/base/home/HomeAdapter.java
@@ -0,0 +1,139 @@
+/* -*- 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;
+
+import org.mozilla.gecko.home.HomePager;
+import org.mozilla.gecko.home.HomePager.Page;
+
+import android.content.Context;
+import android.os.Bundle;
+import android.support.v4.app.Fragment;
+import android.support.v4.app.FragmentManager;
+import android.support.v4.app.FragmentStatePagerAdapter;
+import android.view.ViewGroup;
+
+import java.util.ArrayList;
+import java.util.EnumMap;
+
+class HomeAdapter extends FragmentStatePagerAdapter {
+
+    private final Context mContext;
+    private final ArrayList<TabInfo> mTabs;
+    private final EnumMap<Page, Fragment> mPages;
+
+    private OnAddTabListener mAddTabListener;
+
+    interface OnAddTabListener {
+        public void onAddTab(String title);
+    }
+
+    final class TabInfo {
+        private final Page page;
+        private final Class<?> clss;
+        private final Bundle args;
+        private final String title;
+
+        TabInfo(Page page, Class<?> clss, Bundle args, String title) {
+            this.page = page;
+            this.clss = clss;
+            this.args = args;
+            this.title = title;
+        }
+    }
+
+    public HomeAdapter(Context context, FragmentManager fm) {
+        super(fm);
+
+        mContext = context;
+
+        mTabs = new ArrayList<TabInfo>();
+        mPages = new EnumMap<Page, Fragment>(Page.class);
+    }
+
+    @Override
+    public int getCount() {
+        return mTabs.size();
+    }
+
+    @Override
+    public Fragment getItem(int position) {
+        TabInfo info = mTabs.get(position);
+        return Fragment.instantiate(mContext, info.clss.getName(), info.args);
+    }
+
+    @Override
+    public CharSequence getPageTitle(int position) {
+        TabInfo info = mTabs.get(position);
+        return info.title.toUpperCase();
+    }
+
+    @Override
+    public Object instantiateItem(ViewGroup container, int position) {
+        Fragment fragment = (Fragment) super.instantiateItem(container, position);
+        mPages.put(mTabs.get(position).page, fragment);
+
+        return fragment;
+    }
+
+    @Override
+    public void destroyItem(ViewGroup container, int position, Object object) {
+        super.destroyItem(container, position, object);
+        mPages.remove(mTabs.get(position).page);
+    }
+
+    public void setOnAddTabListener(OnAddTabListener listener) {
+        mAddTabListener = listener;
+    }
+
+    public void addTab(Page page, Class<?> clss, Bundle args, String title) {
+        addTab(-1, page, clss, args, title);
+    }
+
+    public void addTab(int index, Page page, Class<?> clss, Bundle args, String title) {
+        TabInfo info = new TabInfo(page, clss, args, title);
+
+        if (index >= 0) {
+            mTabs.add(index, info);
+        } else {
+            mTabs.add(info);
+        }
+
+        notifyDataSetChanged();
+
+        if (mAddTabListener != null) {
+            mAddTabListener.onAddTab(title);
+        }
+    }
+
+    public int getItemPosition(Page page) {
+        for (int i = 0; i < mTabs.size(); i++) {
+            TabInfo info = mTabs.get(i);
+            if (info.page == page) {
+                return i;
+            }
+        }
+
+        return -1;
+    }
+
+    public Page getPageAtPosition(int position) {
+        TabInfo info = mTabs.get(position);
+        return info.page;
+    }
+
+    public void setCanLoadHint(boolean canLoadHint) {
+        // Update fragment arguments for future instances
+        for (TabInfo info : mTabs) {
+            info.args.putBoolean(HomePager.CAN_LOAD_ARG, canLoadHint);
+        }
+
+        // Enable/disable loading on all existing pages
+        for (Fragment page : mPages.values()) {
+            final HomeFragment homePage = (HomeFragment) page;
+            homePage.setCanLoadHint(canLoadHint);
+        }
+    }
+}
--- a/mobile/android/base/home/HomePager.java
+++ b/mobile/android/base/home/HomePager.java
@@ -3,42 +3,42 @@
  * 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;
 
 import org.mozilla.gecko.R;
 import org.mozilla.gecko.animation.PropertyAnimator;
 import org.mozilla.gecko.animation.ViewHelper;
+import org.mozilla.gecko.home.HomeAdapter.OnAddTabListener;
 import org.mozilla.gecko.mozglue.RobocopTarget;
 import org.mozilla.gecko.util.HardwareUtils;
 
 import android.content.Context;
 import android.os.Build;
 import android.os.Bundle;
 import android.support.v4.app.Fragment;
 import android.support.v4.app.FragmentManager;
-import android.support.v4.app.FragmentStatePagerAdapter;
-import android.support.v4.view.PagerAdapter;
 import android.support.v4.view.ViewPager;
 import android.view.ViewGroup.LayoutParams;
 import android.util.AttributeSet;
 import android.view.MotionEvent;
 import android.view.ViewGroup;
 import android.view.View;
 
-import java.util.ArrayList;
-import java.util.EnumMap;
 import java.util.EnumSet;
 
 public class HomePager extends ViewPager {
+
     private final Context mContext;
     private volatile boolean mLoaded;
     private Decor mDecor;
 
+    private final OnAddTabListener mAddTabListener;
+
     // List of pages in order.
     @RobocopTarget
     public enum Page {
         HISTORY,
         TOP_SITES,
         BOOKMARKS,
         READING_LIST
     }
@@ -48,18 +48,16 @@ public class HomePager extends ViewPager
     static final String LIST_TAG_HISTORY = "history";
     static final String LIST_TAG_BOOKMARKS = "bookmarks";
     static final String LIST_TAG_READING_LIST = "reading_list";
     static final String LIST_TAG_TOP_SITES = "top_sites";
     static final String LIST_TAG_MOST_RECENT = "most_recent";
     static final String LIST_TAG_LAST_TABS = "last_tabs";
     static final String LIST_TAG_BROWSER_SEARCH = "browser_search";
 
-    private EnumMap<Page, Fragment> mPages = new EnumMap<Page, Fragment>(Page.class);
-
     public interface OnUrlOpenListener {
         public enum Flags {
             ALLOW_SWITCH_TO_TAB
         }
 
         public void onUrlOpen(String url, EnumSet<Flags> flags);
     }
 
@@ -87,16 +85,25 @@ public class HomePager extends ViewPager
     public HomePager(Context context) {
         this(context, null);
     }
 
     public HomePager(Context context, AttributeSet attrs) {
         super(context, attrs);
         mContext = context;
 
+        mAddTabListener = new OnAddTabListener() {
+            @Override
+            public void onAddTab(String title) {
+                if (mDecor != null) {
+                    mDecor.onAddPagerView(title);
+                }
+            }
+        };
+
         // This is to keep all 4 pages in memory after they are
         // selected in the pager.
         setOffscreenPageLimit(3);
 
         //  We can call HomePager.requestFocus to steal focus from the URL bar and drop the soft
         //  keyboard. However, if there are no focusable views (e.g. an empty reading list), the
         //  URL bar will be refocused. Therefore, we make the HomePager container focusable to
         //  ensure there is always a focusable view. This would ordinarily be done via an XML
@@ -104,16 +111,24 @@ public class HomePager extends ViewPager
         setFocusableInTouchMode(true);
     }
 
     @Override
     public void addView(View child, int index, ViewGroup.LayoutParams params) {
         if (child instanceof Decor) {
             ((ViewPager.LayoutParams) params).isDecor = true;
             mDecor = (Decor) child;
+
+            mDecor.setOnTitleClickListener(new OnTitleClickListener() {
+                @Override
+                public void onTitleClicked(int index) {
+                    setCurrentItem(index, true);
+                }
+            });
+
             setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                 @Override
                 public void onPageSelected(int position) {
                     mDecor.onPageSelected(position);
                 }
 
                 @Override
                 public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
@@ -124,28 +139,36 @@ public class HomePager extends ViewPager
                 public void onPageScrollStateChanged(int state) { }
             });
         }
 
         super.addView(child, index, params);
     }
 
     public void redisplay(FragmentManager fm) {
-        final TabsAdapter adapter = (TabsAdapter) getAdapter();
-        show(fm, adapter.getCurrentPage(), null);
+        final HomeAdapter adapter = (HomeAdapter) getAdapter();
+        final Page currentPage = adapter.getPageAtPosition(getCurrentItem());
+
+        show(fm, currentPage, null);
     }
 
     /**
      * Loads and initializes the pager.
      *
      * @param fm FragmentManager for the adapter
      */
     public void show(FragmentManager fm, Page page, PropertyAnimator animator) {
         mLoaded = true;
-        final TabsAdapter adapter = new TabsAdapter(fm);
+
+        if (mDecor != null) {
+            mDecor.removeAllPagerViews();
+        }
+
+        final HomeAdapter adapter = new HomeAdapter(mContext, fm);
+        adapter.setOnAddTabListener(mAddTabListener);
 
         // Only animate on post-HC devices, when a non-null animator is given
         final boolean shouldAnimate = (animator != null && Build.VERSION.SDK_INT >= 11);
 
         adapter.addTab(Page.TOP_SITES, TopSitesPage.class, new Bundle(),
                 getContext().getString(R.string.home_top_sites_title));
         adapter.addTab(Page.BOOKMARKS, BookmarksPage.class, new Bundle(),
                 getContext().getString(R.string.bookmarks_title));
@@ -217,132 +240,16 @@ public class HomePager extends ViewPager
     public void setCurrentItem(int item, boolean smoothScroll) {
         super.setCurrentItem(item, smoothScroll);
 
         if (mDecor != null) {
             mDecor.onPageSelected(item);
         }
     }
 
-    class TabsAdapter extends FragmentStatePagerAdapter
-                      implements OnTitleClickListener {
-        private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>();
-
-        final class TabInfo {
-            private final Page page;
-            private final Class<?> clss;
-            private final Bundle args;
-            private final String title;
-
-            TabInfo(Page page, Class<?> clss, Bundle args, String title) {
-                this.page = page;
-                this.clss = clss;
-                this.args = args;
-                this.title = title;
-            }
-        }
-
-        public TabsAdapter(FragmentManager fm) {
-            super(fm);
-
-            if (mDecor != null) {
-                mDecor.removeAllPagerViews();
-                mDecor.setOnTitleClickListener(this);
-            }
-        }
-
-        public void addTab(Page page, Class<?> clss, Bundle args, String title) {
-            addTab(-1, page, clss, args, title);
-        }
-
-        public void addTab(int index, Page page, Class<?> clss, Bundle args, String title) {
-            TabInfo info = new TabInfo(page, clss, args, title);
-
-            if (index >= 0) {
-                mTabs.add(index, info);
-            } else {
-                mTabs.add(info);
-            }
-
-            notifyDataSetChanged();
-
-            if (mDecor != null) {
-                mDecor.onAddPagerView(title);
-            }
-        }
-
-        @Override
-        public void onTitleClicked(int index) {
-            setCurrentItem(index, true);
-        }
-
-        public int getItemPosition(Page page) {
-            for (int i = 0; i < mTabs.size(); i++) {
-                TabInfo info = mTabs.get(i);
-                if (info.page == page) {
-                    return i;
-                }
-            }
-
-            return -1;
-        }
-
-        public Page getCurrentPage() {
-            int currentItem = getCurrentItem();
-            TabInfo info = mTabs.get(currentItem);
-            return info.page;
-        }
-
-        @Override
-        public int getCount() {
-            return mTabs.size();
-        }
-
-        @Override
-        public Fragment getItem(int position) {
-            TabInfo info = mTabs.get(position);
-            return Fragment.instantiate(mContext, info.clss.getName(), info.args);
-        }
-
-        @Override
-        public CharSequence getPageTitle(int position) {
-            TabInfo info = mTabs.get(position);
-            return info.title.toUpperCase();
-        }
-
-        @Override
-        public Object instantiateItem(ViewGroup container, int position) {
-            Fragment fragment = (Fragment) super.instantiateItem(container, position);
-
-            mPages.put(mTabs.get(position).page, fragment);
-
-            return fragment;
-        }
-
-        @Override
-        public void destroyItem(ViewGroup container, int position, Object object) {
-            super.destroyItem(container, position, object);
-
-            mPages.remove(mTabs.get(position).page);
-        }
-
-        public void setCanLoadHint(boolean canLoadHint) {
-            // Update fragment arguments for future instances
-            for (TabInfo info : mTabs) {
-                info.args.putBoolean(CAN_LOAD_ARG, canLoadHint);
-            }
-
-            // Enable/disable loading on all existing pages
-            for (Fragment page : mPages.values()) {
-                final HomeFragment homePage = (HomeFragment) page;
-                homePage.setCanLoadHint(canLoadHint);
-            }
-        }
-    }
-
     @Override
     public boolean onInterceptTouchEvent(MotionEvent event) {
         if (event.getActionMasked() == MotionEvent.ACTION_DOWN) {
             // Drop the soft keyboard by stealing focus from the URL bar.
             requestFocus();
         }
 
         return super.onInterceptTouchEvent(event);
--- a/mobile/android/base/moz.build
+++ b/mobile/android/base/moz.build
@@ -203,16 +203,17 @@ gbjar.sources += [
     'health/BrowserHealthReporter.java',
     'home/BookmarkFolderView.java',
     'home/BookmarksListAdapter.java',
     'home/BookmarksListView.java',
     'home/BookmarksPage.java',
     'home/BrowserSearch.java',
     'home/FadedTextView.java',
     'home/HistoryPage.java',
+    'home/HomeAdapter.java',
     'home/HomeBanner.java',
     'home/HomeFragment.java',
     'home/HomeListView.java',
     'home/HomePager.java',
     'home/HomePagerTabStrip.java',
     'home/LastTabsPage.java',
     'home/MostRecentPage.java',
     'home/MultiTypeCursorAdapter.java',