Adam Cohen | 50f3d1b | 2012-12-11 18:36:07 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package android.widget; |
| 18 | |
| 19 | import android.content.Context; |
| 20 | import android.view.View; |
| 21 | import android.view.ViewGroup; |
| 22 | |
| 23 | import java.util.ArrayList; |
Adam Cohen | 50f3d1b | 2012-12-11 18:36:07 -0800 | [diff] [blame] | 24 | |
| 25 | /** |
| 26 | * @hide |
| 27 | */ |
| 28 | public class RemoteViewsListAdapter extends BaseAdapter { |
| 29 | |
| 30 | private Context mContext; |
| 31 | private ArrayList<RemoteViews> mRemoteViewsList; |
| 32 | private ArrayList<Integer> mViewTypes = new ArrayList<Integer>(); |
Adam Cohen | b00d9f0 | 2013-01-10 14:12:52 -0800 | [diff] [blame] | 33 | private int mViewTypeCount; |
Adam Cohen | 50f3d1b | 2012-12-11 18:36:07 -0800 | [diff] [blame] | 34 | |
Adam Cohen | b00d9f0 | 2013-01-10 14:12:52 -0800 | [diff] [blame] | 35 | public RemoteViewsListAdapter(Context context, ArrayList<RemoteViews> remoteViews, |
| 36 | int viewTypeCount) { |
Adam Cohen | 50f3d1b | 2012-12-11 18:36:07 -0800 | [diff] [blame] | 37 | mContext = context; |
| 38 | mRemoteViewsList = remoteViews; |
Adam Cohen | b00d9f0 | 2013-01-10 14:12:52 -0800 | [diff] [blame] | 39 | mViewTypeCount = viewTypeCount; |
Adam Cohen | 50f3d1b | 2012-12-11 18:36:07 -0800 | [diff] [blame] | 40 | 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 Cohen | b00d9f0 | 2013-01-10 14:12:52 -0800 | [diff] [blame] | 58 | |
| 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 Cohen | 50f3d1b | 2012-12-11 18:36:07 -0800 | [diff] [blame] | 63 | } |
| 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 Cohen | b00d9f0 | 2013-01-10 14:12:52 -0800 | [diff] [blame] | 88 | rv.setIsWidgetCollectionChild(true); |
Adam Cohen | 50f3d1b | 2012-12-11 18:36:07 -0800 | [diff] [blame] | 89 | 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 Cohen | b00d9f0 | 2013-01-10 14:12:52 -0800 | [diff] [blame] | 114 | return mViewTypeCount; |
Adam Cohen | 50f3d1b | 2012-12-11 18:36:07 -0800 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | @Override |
| 118 | public boolean hasStableIds() { |
| 119 | return false; |
| 120 | } |
| 121 | } |