Bug 898197: Refactor history page to make the adapter a static class. [r=margaret]
authorSriram Ramasubramanian <sriram@mozilla.com>
Thu, 25 Jul 2013 22:42:17 -0700
changeset 143495 4c3421eb1d4358150b151a9f3b74bddf1d8a0e7d
parent 143494 d51f4bd67608654c1a271065773736600d3a0409
child 143496 2c24acf4816b28a62c3871555c6e3c3e33bdb736
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)
reviewersmargaret
bugs898197
milestone25.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 898197: Refactor history page to make the adapter a static class. [r=margaret]
mobile/android/base/home/MostRecentPage.java
--- a/mobile/android/base/home/MostRecentPage.java
+++ b/mobile/android/base/home/MostRecentPage.java
@@ -9,17 +9,16 @@ import org.mozilla.gecko.R;
 import org.mozilla.gecko.db.BrowserDB;
 import org.mozilla.gecko.db.BrowserDB.URLColumns;
 import org.mozilla.gecko.home.HomePager.OnUrlOpenListener;
 import org.mozilla.gecko.home.TwoLinePageRow;
 
 import android.app.Activity;
 import android.content.ContentResolver;
 import android.content.Context;
-import android.content.res.Resources;
 import android.database.Cursor;
 import android.os.Bundle;
 import android.support.v4.app.LoaderManager;
 import android.support.v4.content.Loader;
 import android.support.v4.widget.SimpleCursorAdapter;
 import android.util.SparseArray;
 import android.view.View;
 import android.view.ViewGroup;
@@ -35,43 +34,25 @@ import java.util.Date;
  */
 public class MostRecentPage extends HomeFragment {
     // Logging tag name
     private static final String LOGTAG = "GeckoMostRecentPage";
 
     // Cursor loader ID for history query
     private static final int LOADER_ID_HISTORY = 0;
 
-    // For the time sections in history
-    private static final long MS_PER_DAY = 86400000;
-    private static final long MS_PER_WEEK = MS_PER_DAY * 7;
-
-    // The time ranges for each section
-    private static enum MostRecentSection {
-        TODAY,
-        YESTERDAY,
-        WEEK,
-        OLDER
-    };
-
-    // Maps headers in the list with their respective sections
-    private SparseArray<MostRecentSection> mMostRecentSections;
-
     // Adapter for the list of search results
     private MostRecentAdapter mAdapter;
 
     // The view shown by the fragment.
     private ListView mList;
 
     // Callbacks used for the search and favicon cursor loaders
     private CursorLoaderCallbacks mCursorLoaderCallbacks;
 
-    // Inflater used by the adapter
-    private LayoutInflater mInflater;
-
     // On URL open listener
     private OnUrlOpenListener mUrlOpenListener;
 
     public static MostRecentPage newInstance() {
         return new MostRecentPage();
     }
 
     public MostRecentPage() {
@@ -83,45 +64,39 @@ public class MostRecentPage extends Home
         super.onAttach(activity);
 
         try {
             mUrlOpenListener = (OnUrlOpenListener) activity;
         } catch (ClassCastException e) {
             throw new ClassCastException(activity.toString()
                     + " must implement HomePager.OnUrlOpenListener");
         }
-
-        mInflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
     }
 
     @Override
     public void onDetach() {
         super.onDetach();
-
-        mMostRecentSections = null;
-        mInflater = null;
         mUrlOpenListener = null;
     }
 
     @Override
     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
         return inflater.inflate(R.layout.home_most_recent_page, container, false);
     }
 
     @Override
     public void onViewCreated(View view, Bundle savedInstanceState) {
         final TextView title = (TextView) view.findViewById(R.id.title);
         title.setText(R.string.home_most_recent_title);
 
         mList = (ListView) view.findViewById(R.id.list);
-
         mList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
             @Override
             public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
-                position -= getMostRecentSectionsCountBefore(position);
+                position -= mAdapter.getMostRecentSectionsCountBefore(position);
 
                 final Cursor c = mAdapter.getCursor();
                 if (c == null || !c.moveToPosition(position)) {
                     return;
                 }
 
                 final String url = c.getString(c.getColumnIndexOrThrow(URLColumns.URL));
                 mUrlOpenListener.onUrlOpen(url);
@@ -138,141 +113,74 @@ public class MostRecentPage extends Home
     }
 
     @Override
     public void onActivityCreated(Bundle savedInstanceState) {
         super.onActivityCreated(savedInstanceState);
 
         final Activity activity = getActivity();
 
-        // Initialize map of history sections
-        mMostRecentSections = new SparseArray<MostRecentSection>();
-
         // Intialize adapter
         mAdapter = new MostRecentAdapter(activity);
         mList.setAdapter(mAdapter);
 
         // Create callbacks before the initial loader is started
         mCursorLoaderCallbacks = new CursorLoaderCallbacks(activity, getLoaderManager());
         loadIfVisible();
     }
 
     @Override
     protected void load() {
         getLoaderManager().initLoader(LOADER_ID_HISTORY, null, mCursorLoaderCallbacks);
     }
 
-    private String getMostRecentSectionTitle(MostRecentSection section) {
-        final Resources resources = getActivity().getResources();
-
-        switch (section) {
-        case TODAY:
-            return resources.getString(R.string.history_today_section);
-        case YESTERDAY:
-            return resources.getString(R.string.history_yesterday_section);
-        case WEEK:
-            return resources.getString(R.string.history_week_section);
-        case OLDER:
-            return resources.getString(R.string.history_older_section);
-        }
-
-        throw new IllegalStateException("Unrecognized history section");
-    }
-
-    private int getMostRecentSectionsCountBefore(int position) {
-        // Account for the number headers before the given position
-        int sectionsBefore = 0;
-
-        final int historySectionsCount = mMostRecentSections.size();
-        for (int i = 0; i < historySectionsCount; i++) {
-            final int sectionPosition = mMostRecentSections.keyAt(i);
-            if (sectionPosition > position) {
-                break;
-            }
-
-            sectionsBefore++;
-        }
-
-        return sectionsBefore;
-    }
-
-    private MostRecentSection getMostRecentSectionForTime(long from, long time) {
-        long delta = from - time;
-
-        if (delta < 0) {
-            return MostRecentSection.TODAY;
-        }
-
-        if (delta < MS_PER_DAY) {
-            return MostRecentSection.YESTERDAY;
-        }
-
-        if (delta < MS_PER_WEEK) {
-            return MostRecentSection.WEEK;
-        }
-
-        return MostRecentSection.OLDER;
-    }
-
-    private void loadMostRecentSections(Cursor c) {
-        if (c == null || !c.moveToFirst()) {
-            return;
-        }
-
-        // Clear any history sections that may have been loaded before.
-        mMostRecentSections.clear();
-
-        final Date now = new Date();
-        now.setHours(0);
-        now.setMinutes(0);
-        now.setSeconds(0);
-
-        final long today = now.getTime();
-        MostRecentSection section = null;
-
-        do {
-            final int position = c.getPosition();
-            final long time = c.getLong(c.getColumnIndexOrThrow(URLColumns.DATE_LAST_VISITED));
-            final MostRecentSection itemSection = getMostRecentSectionForTime(today, time);
-
-            if (section != itemSection) {
-                section = itemSection;
-                mMostRecentSections.append(position + mMostRecentSections.size(), section);
-            }
-
-            // Reached the last section, no need to continue
-            if (section == MostRecentSection.OLDER) {
-                break;
-            }
-        } while (c.moveToNext());
-    }
-
     private static class MostRecentCursorLoader extends SimpleCursorLoader {
         // Max number of history results
         private static final int HISTORY_LIMIT = 100;
 
         public MostRecentCursorLoader(Context context) {
             super(context);
         }
 
         @Override
         public Cursor loadCursor() {
             final ContentResolver cr = getContext().getContentResolver();
             return BrowserDB.getRecentHistory(cr, HISTORY_LIMIT);
         }
     }
 
-    private class MostRecentAdapter extends SimpleCursorAdapter {
+    private static class MostRecentAdapter extends SimpleCursorAdapter {
         private static final int ROW_HEADER = 0;
         private static final int ROW_STANDARD = 1;
 
         private static final int ROW_TYPE_COUNT = 2;
 
+        // For the time sections in history
+        private static final long MS_PER_DAY = 86400000;
+        private static final long MS_PER_WEEK = MS_PER_DAY * 7;
+
+        // The time ranges for each section
+        private static enum MostRecentSection {
+            TODAY,
+            YESTERDAY,
+            WEEK,
+            OLDER
+        };
+
+        private final Context mContext;
+
+        // Maps headers in the list with their respective sections
+        private final SparseArray<MostRecentSection> mMostRecentSections;
+
         public MostRecentAdapter(Context context) {
             super(context, -1, null, new String[] {}, new int[] {});
+            mContext = context;
+
+            // Initialize map of history sections
+            mMostRecentSections = new SparseArray<MostRecentSection>();
         }
 
         @Override
         public Object getItem(int position) {
             final int type = getItemViewType(position);
 
             // Header items are not in the cursor
             if (type == ROW_HEADER) {
@@ -304,35 +212,42 @@ public class MostRecentPage extends Home
 
         @Override
         public int getCount() {
             // Add the history section headers to the number of reported results.
             return super.getCount() + mMostRecentSections.size();
         }
 
         @Override
+        public Cursor swapCursor(Cursor cursor) {
+            Cursor oldCursor = super.swapCursor(cursor);
+            loadMostRecentSections(cursor);
+            return oldCursor;
+        }
+
+        @Override
         public View getView(int position, View convertView, ViewGroup parent) {
             final int type = getItemViewType(position);
 
             if (type == ROW_HEADER) {
                 final TextView row;
                 if (convertView == null) {
-                    row = (TextView) mInflater.inflate(R.layout.home_header_row, mList, false);
+                    row = (TextView) LayoutInflater.from(mContext).inflate(R.layout.home_header_row, parent, false);
                 } else {
                     row = (TextView) convertView;
                 }
 
                 final MostRecentSection section = mMostRecentSections.get(position);
                 row.setText(getMostRecentSectionTitle(section));
 
                 return row;
             } else {
                 final TwoLinePageRow row;
                 if (convertView == null) {
-                    row = (TwoLinePageRow) mInflater.inflate(R.layout.home_item_row, mList, false);
+                    row = (TwoLinePageRow) LayoutInflater.from(mContext).inflate(R.layout.home_item_row, parent, false);
                 } else {
                     row = (TwoLinePageRow) convertView;
                 }
 
                 // Account for the search engines
                 position -= getMostRecentSectionsCountBefore(position);
 
                 final Cursor c = getCursor();
@@ -340,16 +255,99 @@ public class MostRecentPage extends Home
                     throw new IllegalStateException("Couldn't move cursor to position " + position);
                 }
 
                 row.updateFromCursor(c);
 
                 return row;
             }
         }
+
+        private String getMostRecentSectionTitle(MostRecentSection section) {
+            switch (section) {
+            case TODAY:
+                return mContext.getString(R.string.history_today_section);
+            case YESTERDAY:
+                return mContext.getString(R.string.history_yesterday_section);
+            case WEEK:
+                return mContext.getString(R.string.history_week_section);
+            case OLDER:
+                return mContext.getString(R.string.history_older_section);
+            }
+
+            throw new IllegalStateException("Unrecognized history section");
+        }
+
+        private int getMostRecentSectionsCountBefore(int position) {
+            // Account for the number headers before the given position
+            int sectionsBefore = 0;
+
+            final int historySectionsCount = mMostRecentSections.size();
+            for (int i = 0; i < historySectionsCount; i++) {
+                final int sectionPosition = mMostRecentSections.keyAt(i);
+                if (sectionPosition > position) {
+                    break;
+                }
+
+                sectionsBefore++;
+            }
+
+            return sectionsBefore;
+        }
+
+        private static MostRecentSection getMostRecentSectionForTime(long from, long time) {
+            long delta = from - time;
+
+            if (delta < 0) {
+                return MostRecentSection.TODAY;
+            }
+
+            if (delta < MS_PER_DAY) {
+                return MostRecentSection.YESTERDAY;
+            }
+
+            if (delta < MS_PER_WEEK) {
+                return MostRecentSection.WEEK;
+            }
+
+            return MostRecentSection.OLDER;
+        }
+
+        private void loadMostRecentSections(Cursor c) {
+            if (c == null || !c.moveToFirst()) {
+                return;
+            }
+
+            // Clear any history sections that may have been loaded before.
+            mMostRecentSections.clear();
+
+            final Date now = new Date();
+            now.setHours(0);
+            now.setMinutes(0);
+            now.setSeconds(0);
+
+            final long today = now.getTime();
+            MostRecentSection section = null;
+
+            do {
+                final int position = c.getPosition();
+                final long time = c.getLong(c.getColumnIndexOrThrow(URLColumns.DATE_LAST_VISITED));
+                final MostRecentSection itemSection = MostRecentAdapter.getMostRecentSectionForTime(today, time);
+
+                if (section != itemSection) {
+                    section = itemSection;
+                    mMostRecentSections.append(position + mMostRecentSections.size(), section);
+                }
+
+                // Reached the last section, no need to continue
+                if (section == MostRecentSection.OLDER) {
+                    break;
+                }
+            } while (c.moveToNext());
+        }
     }
 
     private class CursorLoaderCallbacks extends HomeCursorLoaderCallbacks {
         public CursorLoaderCallbacks(Context context, LoaderManager loaderManager) {
             super(context, loaderManager);
         }
 
         @Override
@@ -359,17 +357,16 @@ public class MostRecentPage extends Home
             } else {
                 return super.onCreateLoader(id, args);
             }
         }
 
         @Override
         public void onLoadFinished(Loader<Cursor> loader, Cursor c) {
             if (loader.getId() == LOADER_ID_HISTORY) {
-                loadMostRecentSections(c);
                 mAdapter.swapCursor(c);
                 loadFavicons(c);
             } else {
                 super.onLoadFinished(loader, c);
             }
         }
 
         @Override