Bug 902038: MultiTypeCusorAdapter that wraps a Cursor and its associated meta data. [r=lucasr]
authorSriram Ramasubramanian <sriram@mozilla.com>
Wed, 07 Aug 2013 08:44:55 -0700
changeset 143506 5ac4c656ec55e074cd16081ec071d8f3749165de
parent 143505 9040f8043f4640f4943a0f392a89a8603e604a4f
child 143507 04d3d68298d01ed8213df96150a281179737cef1
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)
reviewerslucasr
bugs902038
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 902038: MultiTypeCusorAdapter that wraps a Cursor and its associated meta data. [r=lucasr]
mobile/android/base/Makefile.in
mobile/android/base/home/MultiTypeCursorAdapter.java
--- a/mobile/android/base/Makefile.in
+++ b/mobile/android/base/Makefile.in
@@ -227,16 +227,17 @@ FENNEC_JAVA_FILES = \
   home/HomeListView.java \
   home/HomePager.java \
   home/HomePagerTabStrip.java \
   home/FadedTextView.java \
   home/FaviconsLoader.java \
   home/LastTabsPage.java \
   home/MostRecentPage.java \
   home/MostVisitedPage.java \
+  home/MultiTypeCursorAdapter.java \
   home/PinBookmarkDialog.java \
   home/ReadingListPage.java \
   home/SearchEngine.java \
   home/SearchEngineRow.java \
   home/SearchLoader.java \
   home/SimpleCursorLoader.java \
   home/SuggestClient.java \
   home/TopBookmarkItemView.java \
new file mode 100644
--- /dev/null
+++ b/mobile/android/base/home/MultiTypeCursorAdapter.java
@@ -0,0 +1,95 @@
+/* -*- 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 android.content.Context;
+import android.database.Cursor;
+import android.support.v4.widget.CursorAdapter;
+import android.view.LayoutInflater;
+import android.view.View;
+import android.view.ViewGroup;
+
+/**
+ * MultiTypeCursorAdapter wraps a cursor and any meta data associated with it.
+ * A set of view types (corresponding to the cursor and its meta data)
+ * are mapped to a set of layouts.
+ */
+abstract class MultiTypeCursorAdapter extends CursorAdapter {
+    private final int[] mViewTypes;
+    private final int[] mLayouts;
+
+    // Bind the view for the given position.
+    abstract public void bindView(View view, Context context, int position);
+
+    public MultiTypeCursorAdapter(Context context, Cursor cursor, int[] viewTypes, int[] layouts) {
+        super(context, cursor);
+
+        if (viewTypes.length != layouts.length) {
+            throw new IllegalStateException("The view types and the layouts should be of same size");
+        }
+
+        mViewTypes = viewTypes;
+        mLayouts = layouts;
+    }
+
+    @Override
+    public final int getViewTypeCount() {
+        return mViewTypes.length;
+    }
+
+    /**
+     * @return Cursor for the given position.
+     */
+    public final Cursor getCursor(int position) {
+        final Cursor cursor = getCursor();
+        if (cursor == null || !cursor.moveToPosition(position)) {
+            throw new IllegalStateException("Couldn't move cursor to position " + position);
+        }
+
+        return cursor;
+    }
+
+    @Override
+    public final View getView(int position, View convertView, ViewGroup parent) {
+        final Context context = parent.getContext();
+        if (convertView == null) {
+            convertView = newView(context, position, parent);
+        }
+
+        bindView(convertView, context, position);
+        return convertView;
+    }
+
+    @Override
+    public final void bindView(View view, Context context, Cursor cursor) {
+        // Do nothing.
+    }
+
+    @Override
+    public final View newView(Context context, Cursor cursor, ViewGroup parent) {
+        return null;
+    }
+
+    /**
+     * Inflate a new view from a set of view types and layouts based on the position.
+     *
+     * @param context Context for inflating the view.
+     * @param position Position of the view.
+     * @param parent Parent view group that will hold this view.
+     */
+    public View newView(Context context, int position, ViewGroup parent) {
+        final int type = getItemViewType(position);
+        final int count = mViewTypes.length;
+
+        for (int i = 0; i < count; i++) {
+            if (mViewTypes[i] == type) {
+                return LayoutInflater.from(context).inflate(mLayouts[i], parent, false);
+            }
+        }
+
+        return null;
+    }
+}