Bug 871651 - Only load pages after editing mode animation ends (r=sriram)
authorLucas Rocha <lucasr@mozilla.com>
Fri, 16 Aug 2013 23:07:35 +0100
changeset 143551 8e581c890c8800a1fe532f33d706832750e78989
parent 143550 84e56b699e9b1a9595c363d8c9bae75567888054
child 143552 7a8a1649d30adb2fd91f464ac0a6d602a8e37d35
push id25130
push userlrocha@mozilla.com
push dateWed, 21 Aug 2013 09:41:27 +0000
treeherdermozilla-central@b2486721572e [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssriram
bugs871651
milestone26.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 871651 - Only load pages after editing mode animation ends (r=sriram)
mobile/android/base/home/HistoryPage.java
mobile/android/base/home/HomeFragment.java
mobile/android/base/home/HomePager.java
--- a/mobile/android/base/home/HistoryPage.java
+++ b/mobile/android/base/home/HistoryPage.java
@@ -85,16 +85,20 @@ public class HistoryPage extends HomeFra
             getFragmentManager().beginTransaction()
                                 .detach(this)
                                 .attach(this)
                                 .commitAllowingStateLoss();
         }
     }
 
     private void showSubPage(Fragment subPage) {
+        final Bundle args = new Bundle();
+        args.putBoolean(HomePager.CAN_LOAD_ARG, getCanLoadHint());
+        subPage.setArguments(args);
+
         getChildFragmentManager().beginTransaction()
                 .addToBackStack(null).replace(R.id.visited_page_container, subPage)
                 .commitAllowingStateLoss();
     }
 
     private void showMostVisitedPage() {
         final MostVisitedPage mostVisitedPage = MostVisitedPage.newInstance();
         showSubPage(mostVisitedPage);
--- a/mobile/android/base/home/HomeFragment.java
+++ b/mobile/android/base/home/HomeFragment.java
@@ -17,16 +17,17 @@ import org.mozilla.gecko.gfx.BitmapUtils
 import org.mozilla.gecko.home.HomeListView.HomeContextMenuInfo;
 import org.mozilla.gecko.util.ThreadUtils;
 import org.mozilla.gecko.util.UiAsyncTask;
 
 import android.content.ContentResolver;
 import android.content.Context;
 import android.content.Intent;
 import android.graphics.Bitmap;
+import android.os.Bundle;
 import android.support.v4.app.Fragment;
 import android.text.TextUtils;
 import android.util.Log;
 import android.view.ContextMenu;
 import android.view.ContextMenu.ContextMenuInfo;
 import android.view.MenuInflater;
 import android.view.MenuItem;
 import android.view.View;
@@ -41,16 +42,38 @@ abstract class HomeFragment extends Frag
     private static final String LOGTAG="GeckoHomeFragment";
 
     // Share MIME type.
     private static final String SHARE_MIME_TYPE = "text/plain";
 
     // URL to Title replacement regex.
     private static final String REGEX_URL_TO_TITLE = "^([a-z]+://)?(www\\.)?";
 
+    // Whether the fragment can load its content or not
+    // This is used to defer data loading until the editing
+    // mode animation ends.
+    private boolean mCanLoadHint;
+
+    // Whether the fragment has loaded its content
+    private boolean mIsLoaded;
+
+    @Override
+    public void onCreate(Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+
+        final Bundle args = getArguments();
+        if (args != null) {
+            mCanLoadHint = args.getBoolean(HomePager.CAN_LOAD_ARG, false);
+        } else {
+            mCanLoadHint = false;
+        }
+
+        mIsLoaded = false;
+    }
+
     @Override
     public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfo) {
         if (menuInfo == null || !(menuInfo instanceof HomeContextMenuInfo)) {
             return;
         }
 
         HomeContextMenuInfo info = (HomeContextMenuInfo) menuInfo;
 
@@ -167,24 +190,40 @@ abstract class HomeFragment extends Frag
     }
 
     @Override
     public void setUserVisibleHint (boolean isVisibleToUser) {
         super.setUserVisibleHint(isVisibleToUser);
         loadIfVisible();
     }
 
+    void setCanLoadHint(boolean canLoadHint) {
+        if (mCanLoadHint == canLoadHint) {
+            return;
+        }
+
+        mCanLoadHint = canLoadHint;
+        loadIfVisible();
+    }
+
+    boolean getCanLoadHint() {
+        return mCanLoadHint;
+    }
+
     protected abstract void load();
 
     protected void loadIfVisible() {
-        if (!isVisible() || !getUserVisibleHint()) {
+        if (!mCanLoadHint || !isVisible() || !getUserVisibleHint()) {
             return;
         }
 
-        load();
+        if (!mIsLoaded) {
+            load();
+            mIsLoaded = true;
+        }
     }
 
     private static class RemoveBookmarkTask extends UiAsyncTask<Void, Void, Void> {
         private final Context mContext;
         private final int mId;
         private final String mUrl;
         private final boolean mInReadingList;
 
--- a/mobile/android/base/home/HomePager.java
+++ b/mobile/android/base/home/HomePager.java
@@ -66,16 +66,18 @@ public class HomePager extends ViewPager
     interface Decor {
         public void onAddPagerView(String title);
         public void removeAllPagerViews();
         public void onPageSelected(int position);
         public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels);
         public void setOnTitleClickListener(OnTitleClickListener onTitleClickListener);
     }
 
+    static final String CAN_LOAD_ARG = "canLoad";
+
     public HomePager(Context context) {
         this(context, null);
     }
 
     public HomePager(Context context, AttributeSet attrs) {
         super(context, attrs);
         mContext = context;
 
@@ -110,38 +112,47 @@ public class HomePager extends ViewPager
 
     /**
      * Loads and initializes the pager.
      *
      * @param fm FragmentManager for the adapter
      */
     public void show(FragmentManager fm, Page page, PropertyAnimator animator) {
         mLoaded = true;
-        TabsAdapter adapter = new TabsAdapter(fm);
+        final TabsAdapter adapter = new TabsAdapter(fm);
+
+        // Only animate on post-HC devices, when a non-null animator is given
+        final boolean shouldAnimate = (animator != null && Build.VERSION.SDK_INT >= 11);
 
         // Add the pages to the adapter in order.
-        adapter.addTab(Page.HISTORY, HistoryPage.class, null, getContext().getString(R.string.home_history_title));
-        adapter.addTab(Page.BOOKMARKS, BookmarksPage.class, null, getContext().getString(R.string.bookmarks_title));
-        adapter.addTab(Page.READING_LIST, ReadingListPage.class, null, getContext().getString(R.string.reading_list));
+        adapter.addTab(Page.HISTORY, HistoryPage.class, new Bundle(),
+                getContext().getString(R.string.home_history_title));
+        adapter.addTab(Page.BOOKMARKS, BookmarksPage.class, new Bundle(),
+                getContext().getString(R.string.bookmarks_title));
+        adapter.addTab(Page.READING_LIST, ReadingListPage.class, new Bundle(),
+                getContext().getString(R.string.reading_list));
+
+        adapter.setCanLoadHint(!shouldAnimate);
 
         setAdapter(adapter);
 
         setCurrentItem(adapter.getItemPosition(page), false);
         setVisibility(VISIBLE);
 
-        if (animator != null && Build.VERSION.SDK_INT >= 11) {
+        if (shouldAnimate) {
             animator.addPropertyAnimationListener(new PropertyAnimator.PropertyAnimationListener() {
                 @Override
                 public void onPropertyAnimationStart() {
                     setLayerType(View.LAYER_TYPE_HARDWARE, null);
                 }
 
                 @Override
                 public void onPropertyAnimationEnd() {
                     setLayerType(View.LAYER_TYPE_NONE, null);
+                    adapter.setCanLoadHint(true);
                 }
             });
 
             ViewHelper.setAlpha(this, 0.0f);
 
             animator.attach(this,
                             PropertyAnimator.Property.ALPHA,
                             1.0f);
@@ -249,16 +260,29 @@ public class HomePager extends ViewPager
         }
 
         @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) {
             // XXX: Drop the soft keyboard by stealing focus. Note that the HomePager (via XML
             // attr) is focusable after its descendants allowing requestFocus to succeed and drop
             // the soft keyboard even if there are no other focusable views on the screen (e.g.