mobile/android/base/home/PanelViewAdapter.java

changeset 0
6474c204b198
equal deleted inserted replaced
-1:000000000000 0:ec1e319f4ee5
1 /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
2 * This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5
6 package org.mozilla.gecko.home;
7
8 import org.mozilla.gecko.home.HomeConfig.ItemType;
9 import org.mozilla.gecko.home.HomeConfig.ViewConfig;
10 import org.mozilla.gecko.home.PanelLayout.FilterManager;
11
12 import org.mozilla.gecko.R;
13
14 import android.content.Context;
15 import android.database.Cursor;
16 import android.support.v4.widget.CursorAdapter;
17 import android.view.View;
18 import android.view.ViewGroup;
19
20 class PanelViewAdapter extends CursorAdapter {
21 private static final int VIEW_TYPE_ITEM = 0;
22 private static final int VIEW_TYPE_BACK = 1;
23
24 private final ViewConfig viewConfig;
25 private FilterManager filterManager;
26 private final Context context;
27
28 public PanelViewAdapter(Context context, ViewConfig viewConfig) {
29 super(context, null, 0);
30 this.context = context;
31 this.viewConfig = viewConfig;
32 }
33
34 public void setFilterManager(FilterManager manager) {
35 this.filterManager = manager;
36 }
37
38 @Override
39 public final int getViewTypeCount() {
40 return 2;
41 }
42
43 @Override
44 public int getCount() {
45 return super.getCount() + (isShowingBack() ? 1 : 0);
46 }
47
48 @Override
49 public int getItemViewType(int position) {
50 if (isShowingBack() && position == 0) {
51 return VIEW_TYPE_BACK;
52 } else {
53 return VIEW_TYPE_ITEM;
54 }
55 }
56
57 @Override
58 public final View getView(int position, View convertView, ViewGroup parent) {
59 if (convertView == null) {
60 convertView = newView(parent.getContext(), position, parent);
61 }
62
63 bindView(convertView, position);
64 return convertView;
65 }
66
67 private View newView(Context context, int position, ViewGroup parent) {
68 if (getItemViewType(position) == VIEW_TYPE_BACK) {
69 return new PanelBackItemView(context, viewConfig.getBackImageUrl());
70 } else {
71 return PanelItemView.create(context, viewConfig.getItemType());
72 }
73 }
74
75 private void bindView(View view, int position) {
76 if (isShowingBack()) {
77 if (position == 0) {
78 final PanelBackItemView item = (PanelBackItemView) view;
79 item.updateFromFilter(filterManager.getPreviousFilter());
80 return;
81 }
82
83 position--;
84 }
85
86 final Cursor cursor = getCursor(position);
87 final PanelItemView item = (PanelItemView) view;
88 item.updateFromCursor(cursor);
89 }
90
91 private boolean isShowingBack() {
92 return (filterManager != null ? filterManager.canGoBack() : false);
93 }
94
95 private final Cursor getCursor(int position) {
96 final Cursor cursor = getCursor();
97 if (cursor == null || !cursor.moveToPosition(position)) {
98 throw new IllegalStateException("Couldn't move cursor to position " + position);
99 }
100
101 return cursor;
102 }
103
104 @Override
105 public final void bindView(View view, Context context, Cursor cursor) {
106 // Do nothing.
107 }
108
109 @Override
110 public final View newView(Context context, Cursor cursor, ViewGroup parent) {
111 return null;
112 }
113 }

mercurial