811a5a4a |
1 | package org.openintents.filemanager;\r |
2 | \r |
3 | /* $Id: BulletedTextListAdapter.java 57 2007-11-21 18:31:52Z steven $ \r |
4 | * \r |
5 | * Copyright 2007 Steven Osborn \r |
6 | * \r |
7 | * Licensed under the Apache License, Version 2.0 (the "License"); \r |
8 | * you may not use this file except in compliance with the License. \r |
9 | * You may obtain a copy of the License at \r |
10 | * \r |
11 | * http://www.apache.org/licenses/LICENSE-2.0 \r |
12 | * \r |
13 | * Unless required by applicable law or agreed to in writing, software \r |
14 | * distributed under the License is distributed on an "AS IS" BASIS, \r |
15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. \r |
16 | * See the License for the specific language governing permissions and \r |
17 | * limitations under the License. \r |
18 | */ \r |
19 | \r |
20 | import java.io.File;\r |
21 | import java.util.ArrayList; \r |
22 | import java.util.List; \r |
23 | \r |
24 | import org.openintents.filemanager.util.FileUtils;\r |
25 | import org.openintents.filemanager.util.MimeTypes;\r |
26 | \r |
27 | import android.content.Context; \r |
28 | import android.graphics.Bitmap;\r |
29 | import android.graphics.BitmapFactory;\r |
30 | import android.graphics.drawable.Drawable;\r |
31 | import android.view.View; \r |
32 | import android.view.ViewGroup; \r |
33 | import android.widget.BaseAdapter; \r |
34 | import android.widget.Filter;\r |
35 | import android.widget.Filterable;\r |
36 | \r |
37 | /** @author Steven Osborn - http://steven.bitsetters.com */ \r |
38 | public class IconifiedTextListAdapter extends BaseAdapter implements Filterable { \r |
39 | \r |
40 | /** Remember our context so we can use it when constructing views. */ \r |
41 | private Context mContext; \r |
42 | \r |
43 | private static String lastFilter;\r |
44 | \r |
45 | class IconifiedFilter extends Filter {\r |
46 | @Override\r |
47 | protected FilterResults performFiltering(CharSequence arg0) {\r |
48 | \r |
49 | lastFilter = (arg0 != null) ? arg0.toString() : null;\r |
50 | \r |
51 | Filter.FilterResults results = new Filter.FilterResults();\r |
52 | \r |
53 | // No results yet?\r |
54 | if (mOriginalItems == null) {\r |
55 | results.count = 0;\r |
56 | results.values = null;\r |
57 | return results;\r |
58 | }\r |
59 | \r |
60 | int count = mOriginalItems.size();\r |
61 | \r |
62 | if (arg0 == null || arg0.length() == 0) {\r |
63 | results.count = count;\r |
64 | results.values = mOriginalItems;\r |
65 | return results;\r |
66 | }\r |
67 | \r |
68 | \r |
69 | List<IconifiedText> filteredItems = new ArrayList<IconifiedText>(count);\r |
70 | \r |
71 | int outCount = 0;\r |
72 | CharSequence lowerCs = arg0.toString().toLowerCase();\r |
73 | \r |
74 | \r |
75 | for (int x=0; x<count; x++) {\r |
76 | IconifiedText text = mOriginalItems.get(x);\r |
77 | \r |
78 | if (text.getText().toLowerCase().contains(lowerCs)) {\r |
79 | // This one matches.\r |
80 | filteredItems.add(text);\r |
81 | outCount++;\r |
82 | }\r |
83 | }\r |
84 | \r |
85 | results.count = outCount;\r |
86 | results.values = filteredItems;\r |
87 | return results;\r |
88 | }\r |
89 | \r |
90 | @Override\r |
91 | protected void publishResults(CharSequence arg0, FilterResults arg1) {\r |
92 | mItems = (List<IconifiedText>) arg1.values;\r |
93 | notifyDataSetChanged();\r |
94 | }\r |
95 | \r |
96 | List<IconifiedText> synchronousFilter(CharSequence filter) {\r |
97 | FilterResults results = performFiltering(filter);\r |
98 | return (List<IconifiedText>) (results.values);\r |
99 | }\r |
100 | }\r |
101 | \r |
102 | private IconifiedFilter mFilter = new IconifiedFilter();\r |
103 | \r |
104 | private List<IconifiedText> mItems = new ArrayList<IconifiedText>(); \r |
105 | private List<IconifiedText> mOriginalItems = new ArrayList<IconifiedText>();\r |
106 | \r |
107 | private Drawable mIconChecked;\r |
108 | private Drawable mIconUnchecked;\r |
109 | \r |
110 | public ThumbnailLoader mThumbnailLoader;\r |
111 | \r |
112 | private File parentFile;\r |
113 | \r |
114 | private MimeTypes mMimeTypes;\r |
115 | \r |
116 | private boolean scrolling = false;\r |
117 | \r |
118 | public IconifiedTextListAdapter(Context context) { \r |
119 | mContext = context; \r |
120 | \r |
121 | mThumbnailLoader = new ThumbnailLoader(context);\r |
122 | \r |
123 | // Cache the checked and unchecked icons so we're not decoding them everytime getView is called.\r |
124 | mIconChecked = context.getResources().getDrawable(R.drawable.ic_button_checked);\r |
125 | mIconUnchecked = context.getResources().getDrawable(R.drawable.ic_button_unchecked);\r |
126 | } \r |
127 | \r |
128 | public void addItem(IconifiedText it) { mItems.add(it); } \r |
129 | \r |
130 | public void setListItems(List<IconifiedText> lit, boolean filter, File parentFile, MimeTypes mimeTypes) {\r |
131 | mOriginalItems = lit;\r |
132 | this.parentFile = parentFile;\r |
133 | mMimeTypes = mimeTypes;\r |
134 | \r |
135 | if (filter) {\r |
136 | mItems = mFilter.synchronousFilter(lastFilter);\r |
137 | } else {\r |
138 | mItems = lit;\r |
139 | }\r |
140 | } \r |
141 | \r |
142 | /** @return The number of items in the */ \r |
143 | public int getCount() { return mItems.size(); } \r |
144 | \r |
145 | public Object getItem(int position) { return mItems.get(position); } \r |
146 | \r |
147 | public boolean areAllItemsSelectable() { return false; } \r |
148 | /*\r |
149 | public boolean isSelectable(int position) { \r |
150 | try{ \r |
151 | return mItems.get(position).isSelectable(); \r |
152 | }catch (IndexOutOfBoundsException aioobe){ \r |
153 | return super.isSelectable(position); \r |
154 | } \r |
155 | } \r |
156 | */\r |
157 | \r |
158 | /** Use the array index as a unique id. */ \r |
159 | public long getItemId(int position) { \r |
160 | return position; \r |
161 | } \r |
162 | \r |
163 | public ThumbnailLoader getThumbnailLoader(){\r |
164 | return mThumbnailLoader;\r |
165 | }\r |
166 | \r |
167 | public void toggleScrolling(boolean isScrolling){\r |
168 | scrolling = isScrolling;\r |
169 | }\r |
170 | \r |
171 | /** @param convertView The old view to overwrite, if one is passed \r |
172 | * @returns a IconifiedTextView that holds wraps around an IconifiedText */ \r |
173 | public View getView(int position, View convertView, ViewGroup parent) { \r |
174 | IconifiedText curItem = mItems.get(position);\r |
175 | IconifiedTextView btv; \r |
176 | if (convertView == null) { \r |
177 | btv = new IconifiedTextView(mContext, curItem); \r |
178 | } else { // Reuse/Overwrite the View passed \r |
179 | // We are assuming(!) that it is castable! \r |
180 | btv = (IconifiedTextView) convertView; \r |
181 | } \r |
182 | btv.setText(curItem.getText()); \r |
183 | btv.setInfo(curItem.getInfo()); \r |
184 | if(curItem.isCheckIconVisible()){\r |
185 | btv.setCheckVisible(true);\r |
186 | if(curItem.isSelected()){\r |
187 | btv.setCheckDrawable(mIconChecked);\r |
188 | } else {\r |
189 | btv.setCheckDrawable(mIconUnchecked);\r |
190 | }\r |
191 | } else {\r |
192 | btv.setCheckVisible(false);\r |
193 | }\r |
194 | \r |
195 | Object icon = curItem.getIconBitmap();\r |
196 | if(icon instanceof Bitmap){\r |
197 | btv.setIcon((Bitmap) icon);\r |
198 | } else {\r |
199 | btv.setIcon((Drawable) icon);\r |
200 | }\r |
201 | \r |
202 | if(!scrolling && FileUtils.getFile(parentFile, curItem.getText()).isFile() && !"video/mpeg".equals(mMimeTypes.getMimeType(curItem.getText()))){\r |
203 | if(mThumbnailLoader != null) {\r |
204 | mThumbnailLoader.loadImage(parentFile.getPath(), curItem, btv.getImageView());\r |
205 | }\r |
206 | }\r |
207 | \r |
208 | return btv; \r |
209 | }\r |
210 | \r |
211 | public Filter getFilter() {\r |
212 | return mFilter;\r |
213 | } \r |
214 | \r |
215 | public void cancelLoader(){\r |
216 | mThumbnailLoader.cancel();\r |
217 | }\r |
218 | } |