1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mobile/android/base/home/PanelViewAdapter.java Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,113 @@ 1.4 +/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*- 1.5 + * This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +package org.mozilla.gecko.home; 1.10 + 1.11 +import org.mozilla.gecko.home.HomeConfig.ItemType; 1.12 +import org.mozilla.gecko.home.HomeConfig.ViewConfig; 1.13 +import org.mozilla.gecko.home.PanelLayout.FilterManager; 1.14 + 1.15 +import org.mozilla.gecko.R; 1.16 + 1.17 +import android.content.Context; 1.18 +import android.database.Cursor; 1.19 +import android.support.v4.widget.CursorAdapter; 1.20 +import android.view.View; 1.21 +import android.view.ViewGroup; 1.22 + 1.23 +class PanelViewAdapter extends CursorAdapter { 1.24 + private static final int VIEW_TYPE_ITEM = 0; 1.25 + private static final int VIEW_TYPE_BACK = 1; 1.26 + 1.27 + private final ViewConfig viewConfig; 1.28 + private FilterManager filterManager; 1.29 + private final Context context; 1.30 + 1.31 + public PanelViewAdapter(Context context, ViewConfig viewConfig) { 1.32 + super(context, null, 0); 1.33 + this.context = context; 1.34 + this.viewConfig = viewConfig; 1.35 + } 1.36 + 1.37 + public void setFilterManager(FilterManager manager) { 1.38 + this.filterManager = manager; 1.39 + } 1.40 + 1.41 + @Override 1.42 + public final int getViewTypeCount() { 1.43 + return 2; 1.44 + } 1.45 + 1.46 + @Override 1.47 + public int getCount() { 1.48 + return super.getCount() + (isShowingBack() ? 1 : 0); 1.49 + } 1.50 + 1.51 + @Override 1.52 + public int getItemViewType(int position) { 1.53 + if (isShowingBack() && position == 0) { 1.54 + return VIEW_TYPE_BACK; 1.55 + } else { 1.56 + return VIEW_TYPE_ITEM; 1.57 + } 1.58 + } 1.59 + 1.60 + @Override 1.61 + public final View getView(int position, View convertView, ViewGroup parent) { 1.62 + if (convertView == null) { 1.63 + convertView = newView(parent.getContext(), position, parent); 1.64 + } 1.65 + 1.66 + bindView(convertView, position); 1.67 + return convertView; 1.68 + } 1.69 + 1.70 + private View newView(Context context, int position, ViewGroup parent) { 1.71 + if (getItemViewType(position) == VIEW_TYPE_BACK) { 1.72 + return new PanelBackItemView(context, viewConfig.getBackImageUrl()); 1.73 + } else { 1.74 + return PanelItemView.create(context, viewConfig.getItemType()); 1.75 + } 1.76 + } 1.77 + 1.78 + private void bindView(View view, int position) { 1.79 + if (isShowingBack()) { 1.80 + if (position == 0) { 1.81 + final PanelBackItemView item = (PanelBackItemView) view; 1.82 + item.updateFromFilter(filterManager.getPreviousFilter()); 1.83 + return; 1.84 + } 1.85 + 1.86 + position--; 1.87 + } 1.88 + 1.89 + final Cursor cursor = getCursor(position); 1.90 + final PanelItemView item = (PanelItemView) view; 1.91 + item.updateFromCursor(cursor); 1.92 + } 1.93 + 1.94 + private boolean isShowingBack() { 1.95 + return (filterManager != null ? filterManager.canGoBack() : false); 1.96 + } 1.97 + 1.98 + private final Cursor getCursor(int position) { 1.99 + final Cursor cursor = getCursor(); 1.100 + if (cursor == null || !cursor.moveToPosition(position)) { 1.101 + throw new IllegalStateException("Couldn't move cursor to position " + position); 1.102 + } 1.103 + 1.104 + return cursor; 1.105 + } 1.106 + 1.107 + @Override 1.108 + public final void bindView(View view, Context context, Cursor cursor) { 1.109 + // Do nothing. 1.110 + } 1.111 + 1.112 + @Override 1.113 + public final View newView(Context context, Cursor cursor, ViewGroup parent) { 1.114 + return null; 1.115 + } 1.116 +}