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; 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>(); 33 private int mViewTypeCount; 34 private RemoteViews.ColorResources mColorResources; 35 RemoteViewsListAdapter(Context context, ArrayList<RemoteViews> remoteViews, int viewTypeCount, RemoteViews.ColorResources colorResources)36 public RemoteViewsListAdapter(Context context, ArrayList<RemoteViews> remoteViews, 37 int viewTypeCount, RemoteViews.ColorResources colorResources) { 38 mContext = context; 39 mRemoteViewsList = remoteViews; 40 mViewTypeCount = viewTypeCount; 41 mColorResources = colorResources; 42 init(); 43 } 44 setViewsList(ArrayList<RemoteViews> remoteViews)45 public void setViewsList(ArrayList<RemoteViews> remoteViews) { 46 mRemoteViewsList = remoteViews; 47 init(); 48 notifyDataSetChanged(); 49 } 50 init()51 private void init() { 52 if (mRemoteViewsList == null) return; 53 54 mViewTypes.clear(); 55 for (RemoteViews rv: mRemoteViewsList) { 56 if (!mViewTypes.contains(rv.getLayoutId())) { 57 mViewTypes.add(rv.getLayoutId()); 58 } 59 } 60 61 if (mViewTypes.size() > mViewTypeCount || mViewTypeCount < 1) { 62 throw new RuntimeException("Invalid view type count -- view type count must be >= 1" + 63 "and must be as large as the total number of distinct view types"); 64 } 65 } 66 67 @Override getCount()68 public int getCount() { 69 if (mRemoteViewsList != null) { 70 return mRemoteViewsList.size(); 71 } else { 72 return 0; 73 } 74 } 75 76 @Override getItem(int position)77 public Object getItem(int position) { 78 return null; 79 } 80 81 @Override getItemId(int position)82 public long getItemId(int position) { 83 return position; 84 } 85 86 @Override getView(int position, View convertView, ViewGroup parent)87 public View getView(int position, View convertView, ViewGroup parent) { 88 if (position < getCount()) { 89 RemoteViews rv = mRemoteViewsList.get(position); 90 rv.addFlags(RemoteViews.FLAG_WIDGET_IS_COLLECTION_CHILD); 91 View v; 92 if (convertView != null && rv != null && 93 convertView.getId() == rv.getLayoutId()) { 94 v = convertView; 95 rv.reapply(mContext, v, null /* handler */, null /* size */, mColorResources); 96 } else { 97 v = rv.apply(mContext, parent, null /* handler */, null /* size */, 98 mColorResources); 99 } 100 return v; 101 } else { 102 return null; 103 } 104 } 105 106 @Override getItemViewType(int position)107 public int getItemViewType(int position) { 108 if (position < getCount()) { 109 int layoutId = mRemoteViewsList.get(position).getLayoutId(); 110 return mViewTypes.indexOf(layoutId); 111 } else { 112 return 0; 113 } 114 } 115 getViewTypeCount()116 public int getViewTypeCount() { 117 return mViewTypeCount; 118 } 119 120 @Override hasStableIds()121 public boolean hasStableIds() { 122 return false; 123 } 124 } 125