michael@0: /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*- michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: package org.mozilla.gecko.home; michael@0: michael@0: import org.mozilla.gecko.home.HomeConfig.ItemType; michael@0: import org.mozilla.gecko.home.HomeConfig.ViewConfig; michael@0: import org.mozilla.gecko.home.PanelLayout.FilterManager; michael@0: michael@0: import org.mozilla.gecko.R; michael@0: michael@0: import android.content.Context; michael@0: import android.database.Cursor; michael@0: import android.support.v4.widget.CursorAdapter; michael@0: import android.view.View; michael@0: import android.view.ViewGroup; michael@0: michael@0: class PanelViewAdapter extends CursorAdapter { michael@0: private static final int VIEW_TYPE_ITEM = 0; michael@0: private static final int VIEW_TYPE_BACK = 1; michael@0: michael@0: private final ViewConfig viewConfig; michael@0: private FilterManager filterManager; michael@0: private final Context context; michael@0: michael@0: public PanelViewAdapter(Context context, ViewConfig viewConfig) { michael@0: super(context, null, 0); michael@0: this.context = context; michael@0: this.viewConfig = viewConfig; michael@0: } michael@0: michael@0: public void setFilterManager(FilterManager manager) { michael@0: this.filterManager = manager; michael@0: } michael@0: michael@0: @Override michael@0: public final int getViewTypeCount() { michael@0: return 2; michael@0: } michael@0: michael@0: @Override michael@0: public int getCount() { michael@0: return super.getCount() + (isShowingBack() ? 1 : 0); michael@0: } michael@0: michael@0: @Override michael@0: public int getItemViewType(int position) { michael@0: if (isShowingBack() && position == 0) { michael@0: return VIEW_TYPE_BACK; michael@0: } else { michael@0: return VIEW_TYPE_ITEM; michael@0: } michael@0: } michael@0: michael@0: @Override michael@0: public final View getView(int position, View convertView, ViewGroup parent) { michael@0: if (convertView == null) { michael@0: convertView = newView(parent.getContext(), position, parent); michael@0: } michael@0: michael@0: bindView(convertView, position); michael@0: return convertView; michael@0: } michael@0: michael@0: private View newView(Context context, int position, ViewGroup parent) { michael@0: if (getItemViewType(position) == VIEW_TYPE_BACK) { michael@0: return new PanelBackItemView(context, viewConfig.getBackImageUrl()); michael@0: } else { michael@0: return PanelItemView.create(context, viewConfig.getItemType()); michael@0: } michael@0: } michael@0: michael@0: private void bindView(View view, int position) { michael@0: if (isShowingBack()) { michael@0: if (position == 0) { michael@0: final PanelBackItemView item = (PanelBackItemView) view; michael@0: item.updateFromFilter(filterManager.getPreviousFilter()); michael@0: return; michael@0: } michael@0: michael@0: position--; michael@0: } michael@0: michael@0: final Cursor cursor = getCursor(position); michael@0: final PanelItemView item = (PanelItemView) view; michael@0: item.updateFromCursor(cursor); michael@0: } michael@0: michael@0: private boolean isShowingBack() { michael@0: return (filterManager != null ? filterManager.canGoBack() : false); michael@0: } michael@0: michael@0: private final Cursor getCursor(int position) { michael@0: final Cursor cursor = getCursor(); michael@0: if (cursor == null || !cursor.moveToPosition(position)) { michael@0: throw new IllegalStateException("Couldn't move cursor to position " + position); michael@0: } michael@0: michael@0: return cursor; michael@0: } michael@0: michael@0: @Override michael@0: public final void bindView(View view, Context context, Cursor cursor) { michael@0: // Do nothing. michael@0: } michael@0: michael@0: @Override michael@0: public final View newView(Context context, Cursor cursor, ViewGroup parent) { michael@0: return null; michael@0: } michael@0: }