blob: e490458b38a8dec98eaebf188de809733aebec41 [file] [log] [blame]
Adam Cohen50f3d1b2012-12-11 18:36:07 -08001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.widget;
18
19import android.content.Context;
20import android.view.View;
21import android.view.ViewGroup;
22
23import java.util.ArrayList;
Adam Cohen50f3d1b2012-12-11 18:36:07 -080024
25/**
26 * @hide
27 */
28public class RemoteViewsListAdapter extends BaseAdapter {
29
30 private Context mContext;
31 private ArrayList<RemoteViews> mRemoteViewsList;
32 private ArrayList<Integer> mViewTypes = new ArrayList<Integer>();
Adam Cohenb00d9f02013-01-10 14:12:52 -080033 private int mViewTypeCount;
Adam Cohen50f3d1b2012-12-11 18:36:07 -080034
Adam Cohenb00d9f02013-01-10 14:12:52 -080035 public RemoteViewsListAdapter(Context context, ArrayList<RemoteViews> remoteViews,
36 int viewTypeCount) {
Adam Cohen50f3d1b2012-12-11 18:36:07 -080037 mContext = context;
38 mRemoteViewsList = remoteViews;
Adam Cohenb00d9f02013-01-10 14:12:52 -080039 mViewTypeCount = viewTypeCount;
Adam Cohen50f3d1b2012-12-11 18:36:07 -080040 init();
41 }
42
43 public void setViewsList(ArrayList<RemoteViews> remoteViews) {
44 mRemoteViewsList = remoteViews;
45 init();
46 notifyDataSetChanged();
47 }
48
49 private void init() {
50 if (mRemoteViewsList == null) return;
51
52 mViewTypes.clear();
53 for (RemoteViews rv: mRemoteViewsList) {
54 if (!mViewTypes.contains(rv.getLayoutId())) {
55 mViewTypes.add(rv.getLayoutId());
56 }
57 }
Adam Cohenb00d9f02013-01-10 14:12:52 -080058
59 if (mViewTypes.size() > mViewTypeCount || mViewTypeCount < 1) {
60 throw new RuntimeException("Invalid view type count -- view type count must be >= 1" +
61 "and must be as large as the total number of distinct view types");
62 }
Adam Cohen50f3d1b2012-12-11 18:36:07 -080063 }
64
65 @Override
66 public int getCount() {
67 if (mRemoteViewsList != null) {
68 return mRemoteViewsList.size();
69 } else {
70 return 0;
71 }
72 }
73
74 @Override
75 public Object getItem(int position) {
76 return null;
77 }
78
79 @Override
80 public long getItemId(int position) {
81 return position;
82 }
83
84 @Override
85 public View getView(int position, View convertView, ViewGroup parent) {
86 if (position < getCount()) {
87 RemoteViews rv = mRemoteViewsList.get(position);
Adam Cohenb00d9f02013-01-10 14:12:52 -080088 rv.setIsWidgetCollectionChild(true);
Adam Cohen50f3d1b2012-12-11 18:36:07 -080089 View v;
90 if (convertView != null && rv != null &&
91 convertView.getId() == rv.getLayoutId()) {
92 v = convertView;
93 rv.reapply(mContext, v);
94 } else {
95 v = rv.apply(mContext, parent);
96 }
97 return v;
98 } else {
99 return null;
100 }
101 }
102
103 @Override
104 public int getItemViewType(int position) {
105 if (position < getCount()) {
106 int layoutId = mRemoteViewsList.get(position).getLayoutId();
107 return mViewTypes.indexOf(layoutId);
108 } else {
109 return 0;
110 }
111 }
112
113 public int getViewTypeCount() {
Adam Cohenb00d9f02013-01-10 14:12:52 -0800114 return mViewTypeCount;
Adam Cohen50f3d1b2012-12-11 18:36:07 -0800115 }
116
117 @Override
118 public boolean hasStableIds() {
119 return false;
120 }
121}