blob: 4442c7f6c200cd05ad2ec655ef499bfcb5c30b59 [file] [log] [blame]
The Android Open Source Project0c908882009-03-03 19:32:16 -08001/*
2 * Copyright (C) 2006 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 com.android.browser;
18
19import android.content.ContentResolver;
20import android.content.ContentUris;
21import android.content.ContentValues;
22import android.database.ContentObserver;
23import android.database.Cursor;
24import android.database.DataSetObserver;
25import android.graphics.Bitmap;
26import android.graphics.BitmapFactory;
27import android.net.Uri;
28import android.os.Bundle;
29import android.os.Handler;
30import android.provider.Browser;
31import android.provider.Browser.BookmarkColumns;
32import android.view.KeyEvent;
Leon Scroggins892df312009-07-14 14:48:02 -040033import android.view.LayoutInflater;
The Android Open Source Project0c908882009-03-03 19:32:16 -080034import android.view.View;
35import android.view.ViewGroup;
36import android.webkit.WebIconDatabase;
Patrick Scott3918d442009-08-04 13:22:29 -040037import android.webkit.WebView;
The Android Open Source Project0c908882009-03-03 19:32:16 -080038import android.widget.BaseAdapter;
Leon Scroggins892df312009-07-14 14:48:02 -040039import android.widget.ImageView;
40import android.widget.TextView;
The Android Open Source Project0c908882009-03-03 19:32:16 -080041
42import java.io.ByteArrayOutputStream;
43
44class BrowserBookmarksAdapter extends BaseAdapter {
45
The Android Open Source Project0c908882009-03-03 19:32:16 -080046 private String mCurrentPage;
Leon Scroggins89c6d362009-07-15 16:54:37 -040047 private String mCurrentTitle;
Ben Murdochdcc2b6f2009-09-21 14:29:20 +010048 private Bitmap mCurrentThumbnail;
The Android Open Source Project0c908882009-03-03 19:32:16 -080049 private Cursor mCursor;
50 private int mCount;
The Android Open Source Project0c908882009-03-03 19:32:16 -080051 private BrowserBookmarksPage mBookmarksPage;
52 private ContentResolver mContentResolver;
The Android Open Source Project0c908882009-03-03 19:32:16 -080053 private boolean mDataValid;
Ben Murdoch328ea872009-09-16 13:33:29 +010054 private BookmarkViewMode mViewMode;
Leon Scrogginsa5d669e2009-08-05 14:07:58 -040055 private boolean mMostVisited;
56 private boolean mNeedsOffset;
57 private int mExtraOffset;
The Android Open Source Project0c908882009-03-03 19:32:16 -080058
The Android Open Source Project0c908882009-03-03 19:32:16 -080059 /**
60 * Create a new BrowserBookmarksAdapter.
The Android Open Source Project0c908882009-03-03 19:32:16 -080061 * @param b BrowserBookmarksPage that instantiated this.
62 * Necessary so it will adjust its focus
63 * appropriately after a search.
64 */
65 public BrowserBookmarksAdapter(BrowserBookmarksPage b, String curPage,
Ben Murdochdcc2b6f2009-09-21 14:29:20 +010066 String curTitle, Bitmap curThumbnail, boolean createShortcut,
67 boolean mostVisited) {
Leon Scrogginsa5d669e2009-08-05 14:07:58 -040068 mNeedsOffset = !(createShortcut || mostVisited);
69 mMostVisited = mostVisited;
70 mExtraOffset = mNeedsOffset ? 1 : 0;
The Android Open Source Project0c908882009-03-03 19:32:16 -080071 mBookmarksPage = b;
Leon Scroggins89c6d362009-07-15 16:54:37 -040072 mCurrentPage = b.getResources().getString(R.string.current_page)
73 + curPage;
74 mCurrentTitle = curTitle;
Ben Murdochdcc2b6f2009-09-21 14:29:20 +010075 mCurrentThumbnail = curThumbnail;
The Android Open Source Project0c908882009-03-03 19:32:16 -080076 mContentResolver = b.getContentResolver();
Ben Murdoch328ea872009-09-16 13:33:29 +010077 mViewMode = BookmarkViewMode.LIST;
Leon Scroggins892df312009-07-14 14:48:02 -040078
Leon Scrogginsa5d669e2009-08-05 14:07:58 -040079 String whereClause;
The Android Open Source Project0c908882009-03-03 19:32:16 -080080 // FIXME: Should have a default sort order that the user selects.
Leon Scroggins892df312009-07-14 14:48:02 -040081 String orderBy = Browser.BookmarkColumns.VISITS + " DESC";
Leon Scrogginsa5d669e2009-08-05 14:07:58 -040082 if (mostVisited) {
83 whereClause = Browser.BookmarkColumns.VISITS + " != 0";
84 } else {
Patrick Scottc1cf63a2010-03-09 16:02:08 -050085 whereClause = Browser.BookmarkColumns.BOOKMARK + " = 1";
Leon Scrogginsa5d669e2009-08-05 14:07:58 -040086 }
Leon Scroggins892df312009-07-14 14:48:02 -040087 mCursor = b.managedQuery(Browser.BOOKMARKS_URI,
88 Browser.HISTORY_PROJECTION, whereClause, null, orderBy);
89 mCursor.registerContentObserver(new ChangeObserver());
90 mCursor.registerDataSetObserver(new MyDataSetObserver());
91
92 mDataValid = true;
93 notifyDataSetChanged();
94
95 mCount = mCursor.getCount() + mExtraOffset;
The Android Open Source Project0c908882009-03-03 19:32:16 -080096 }
97
98 /**
99 * Return a hashmap with one row's Title, Url, and favicon.
100 * @param position Position in the list.
101 * @return Bundle Stores title, url of row position, favicon, and id
102 * for the url. Return a blank map if position is out of
103 * range.
104 */
105 public Bundle getRow(int position) {
106 Bundle map = new Bundle();
107 if (position < mExtraOffset || position >= mCount) {
108 return map;
109 }
110 mCursor.moveToPosition(position- mExtraOffset);
111 String url = mCursor.getString(Browser.HISTORY_PROJECTION_URL_INDEX);
112 map.putString(Browser.BookmarkColumns.TITLE,
113 mCursor.getString(Browser.HISTORY_PROJECTION_TITLE_INDEX));
114 map.putString(Browser.BookmarkColumns.URL, url);
115 byte[] data = mCursor.getBlob(Browser.HISTORY_PROJECTION_FAVICON_INDEX);
116 if (data != null) {
117 map.putParcelable(Browser.BookmarkColumns.FAVICON,
118 BitmapFactory.decodeByteArray(data, 0, data.length));
119 }
120 map.putInt("id", mCursor.getInt(Browser.HISTORY_PROJECTION_ID_INDEX));
121 return map;
122 }
123
124 /**
125 * Update a row in the database with new information.
126 * Requeries the database if the information has changed.
127 * @param map Bundle storing id, title and url of new information
128 */
129 public void updateRow(Bundle map) {
130
131 // Find the record
132 int id = map.getInt("id");
133 int position = -1;
134 for (mCursor.moveToFirst(); !mCursor.isAfterLast(); mCursor.moveToNext()) {
135 if (mCursor.getInt(Browser.HISTORY_PROJECTION_ID_INDEX) == id) {
136 position = mCursor.getPosition();
137 break;
138 }
139 }
140 if (position < 0) {
141 return;
142 }
143
144 mCursor.moveToPosition(position);
145 ContentValues values = new ContentValues();
146 String title = map.getString(Browser.BookmarkColumns.TITLE);
147 if (!title.equals(mCursor
148 .getString(Browser.HISTORY_PROJECTION_TITLE_INDEX))) {
149 values.put(Browser.BookmarkColumns.TITLE, title);
150 }
151 String url = map.getString(Browser.BookmarkColumns.URL);
152 if (!url.equals(mCursor.
153 getString(Browser.HISTORY_PROJECTION_URL_INDEX))) {
154 values.put(Browser.BookmarkColumns.URL, url);
155 }
Ben Murdoch1794fe22009-09-29 18:14:30 +0100156
157 if (map.getBoolean("invalidateThumbnail") == true) {
158 values.put(Browser.BookmarkColumns.THUMBNAIL, new byte[0]);
159 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800160 if (values.size() > 0
161 && mContentResolver.update(Browser.BOOKMARKS_URI, values,
162 "_id = " + id, null) != -1) {
163 refreshList();
164 }
165 }
166
167 /**
168 * Delete a row from the database. Requeries the database.
169 * Does nothing if the provided position is out of range.
170 * @param position Position in the list.
171 */
172 public void deleteRow(int position) {
173 if (position < mExtraOffset || position >= getCount()) {
174 return;
175 }
176 mCursor.moveToPosition(position- mExtraOffset);
177 String url = mCursor.getString(Browser.HISTORY_PROJECTION_URL_INDEX);
Andrei Popescuc9526192009-09-23 15:52:16 +0100178 String title = mCursor.getString(Browser.HISTORY_PROJECTION_TITLE_INDEX);
179 Bookmarks.removeFromBookmarks(null, mContentResolver, url, title);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800180 refreshList();
181 }
182
183 /**
184 * Delete all bookmarks from the db. Requeries the database.
185 * All bookmarks with become visited URLs or if never visited
186 * are removed
187 */
188 public void deleteAllRows() {
189 StringBuilder deleteIds = null;
190 StringBuilder convertIds = null;
191
192 for (mCursor.moveToFirst(); !mCursor.isAfterLast(); mCursor.moveToNext()) {
193 String url = mCursor.getString(Browser.HISTORY_PROJECTION_URL_INDEX);
194 WebIconDatabase.getInstance().releaseIconForPageUrl(url);
195 int id = mCursor.getInt(Browser.HISTORY_PROJECTION_ID_INDEX);
196 int numVisits = mCursor.getInt(Browser.HISTORY_PROJECTION_VISITS_INDEX);
197 if (0 == numVisits) {
198 if (deleteIds == null) {
199 deleteIds = new StringBuilder();
200 deleteIds.append("( ");
201 } else {
202 deleteIds.append(" OR ( ");
203 }
204 deleteIds.append(BookmarkColumns._ID);
205 deleteIds.append(" = ");
206 deleteIds.append(id);
207 deleteIds.append(" )");
208 } else {
209 // It is no longer a bookmark, but it is still a visited site.
210 if (convertIds == null) {
211 convertIds = new StringBuilder();
212 convertIds.append("( ");
213 } else {
214 convertIds.append(" OR ( ");
215 }
216 convertIds.append(BookmarkColumns._ID);
217 convertIds.append(" = ");
218 convertIds.append(id);
219 convertIds.append(" )");
220 }
221 }
222
223 if (deleteIds != null) {
224 mContentResolver.delete(Browser.BOOKMARKS_URI, deleteIds.toString(),
225 null);
226 }
227 if (convertIds != null) {
228 ContentValues values = new ContentValues();
229 values.put(Browser.BookmarkColumns.BOOKMARK, 0);
230 mContentResolver.update(Browser.BOOKMARKS_URI, values,
231 convertIds.toString(), null);
232 }
233 refreshList();
234 }
235
236 /**
237 * Refresh list to recognize a change in the database.
238 */
239 public void refreshList() {
Leon Scroggins892df312009-07-14 14:48:02 -0400240 mCursor.requery();
241 mCount = mCursor.getCount() + mExtraOffset;
242 notifyDataSetChanged();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800243 }
244
245 /**
Patrick Scott3918d442009-08-04 13:22:29 -0400246 * Update the bookmark's favicon. This is a convenience method for updating
247 * a bookmark favicon for the originalUrl and url of the passed in WebView.
The Android Open Source Project0c908882009-03-03 19:32:16 -0800248 * @param cr The ContentResolver to use.
Patrick Scott15525d42009-09-21 13:39:37 -0400249 * @param originalUrl The original url before any redirects.
250 * @param url The current url.
The Android Open Source Project0c908882009-03-03 19:32:16 -0800251 * @param favicon The favicon bitmap to write to the db.
252 */
253 /* package */ static void updateBookmarkFavicon(ContentResolver cr,
Patrick Scott3918d442009-08-04 13:22:29 -0400254 String originalUrl, String url, Bitmap favicon) {
Leon Scrogginsa5d669e2009-08-05 14:07:58 -0400255 final Cursor c = queryBookmarksForUrl(cr, originalUrl, url, true);
Patrick Scott3918d442009-08-04 13:22:29 -0400256 if (c == null) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800257 return;
258 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800259 boolean succeed = c.moveToFirst();
260 ContentValues values = null;
261 while (succeed) {
262 if (values == null) {
263 final ByteArrayOutputStream os = new ByteArrayOutputStream();
264 favicon.compress(Bitmap.CompressFormat.PNG, 100, os);
265 values = new ContentValues();
266 values.put(Browser.BookmarkColumns.FAVICON, os.toByteArray());
267 }
268 cr.update(ContentUris.withAppendedId(Browser.BOOKMARKS_URI, c
269 .getInt(0)), values, null, null);
270 succeed = c.moveToNext();
271 }
272 c.close();
273 }
274
Patrick Scott3918d442009-08-04 13:22:29 -0400275 /* package */ static Cursor queryBookmarksForUrl(ContentResolver cr,
Leon Scrogginsa5d669e2009-08-05 14:07:58 -0400276 String originalUrl, String url, boolean onlyBookmarks) {
Patrick Scott3918d442009-08-04 13:22:29 -0400277 if (cr == null || url == null) {
278 return null;
279 }
280
281 // If originalUrl is null, just set it to url.
282 if (originalUrl == null) {
283 originalUrl = url;
284 }
285
286 // Look for both the original url and the actual url. This takes in to
287 // account redirects.
288 String originalUrlNoQuery = removeQuery(originalUrl);
289 String urlNoQuery = removeQuery(url);
290 originalUrl = originalUrlNoQuery + '?';
291 url = urlNoQuery + '?';
292
293 // Use NoQuery to search for the base url (i.e. if the url is
294 // http://www.yahoo.com/?rs=1, search for http://www.yahoo.com)
295 // Use url to match the base url with other queries (i.e. if the url is
296 // http://www.google.com/m, search for
297 // http://www.google.com/m?some_query)
298 final String[] selArgs = new String[] {
299 originalUrlNoQuery, urlNoQuery, originalUrl, url };
Leon Scrogginsa5d669e2009-08-05 14:07:58 -0400300 String where = BookmarkColumns.URL + " == ? OR "
Patrick Scott3918d442009-08-04 13:22:29 -0400301 + BookmarkColumns.URL + " == ? OR "
Patrick Scott193def92010-01-07 15:01:09 -0500302 + BookmarkColumns.URL + " LIKE ? || '%' OR "
303 + BookmarkColumns.URL + " LIKE ? || '%'";
Leon Scrogginsa5d669e2009-08-05 14:07:58 -0400304 if (onlyBookmarks) {
305 where = "(" + where + ") AND " + BookmarkColumns.BOOKMARK + " == 1";
306 }
Patrick Scott3918d442009-08-04 13:22:29 -0400307 final String[] projection =
308 new String[] { Browser.BookmarkColumns._ID };
309 return cr.query(Browser.BOOKMARKS_URI, projection, where, selArgs,
310 null);
311 }
312
313 // Strip the query from the given url.
314 private static String removeQuery(String url) {
315 if (url == null) {
316 return null;
317 }
318 int query = url.indexOf('?');
319 String noQuery = url;
320 if (query != -1) {
321 noQuery = url.substring(0, query);
322 }
323 return noQuery;
324 }
325
The Android Open Source Project0c908882009-03-03 19:32:16 -0800326 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -0800327 * How many items should be displayed in the list.
328 * @return Count of items.
329 */
330 public int getCount() {
331 if (mDataValid) {
332 return mCount;
333 } else {
334 return 0;
335 }
336 }
337
338 public boolean areAllItemsEnabled() {
339 return true;
340 }
341
342 public boolean isEnabled(int position) {
343 return true;
344 }
345
346 /**
347 * Get the data associated with the specified position in the list.
348 * @param position Index of the item whose data we want.
349 * @return The data at the specified position.
350 */
351 public Object getItem(int position) {
352 return null;
353 }
354
355 /**
356 * Get the row id associated with the specified position in the list.
357 * @param position Index of the item whose row id we want.
358 * @return The id of the item at the specified position.
359 */
360 public long getItemId(int position) {
361 return position;
362 }
363
Ben Murdoch328ea872009-09-16 13:33:29 +0100364 /* package */ void switchViewMode(BookmarkViewMode viewMode) {
365 mViewMode = viewMode;
Leon Scroggins892df312009-07-14 14:48:02 -0400366 }
367
368 /* package */ void populateBookmarkItem(BookmarkItem b, int position) {
369 mCursor.moveToPosition(position - mExtraOffset);
Patrick Scott8f0076b2009-09-17 13:51:30 -0400370 String url = mCursor.getString(Browser.HISTORY_PROJECTION_URL_INDEX);
371 b.setUrl(url);
Leon Scroggins892df312009-07-14 14:48:02 -0400372 b.setName(mCursor.getString(Browser.HISTORY_PROJECTION_TITLE_INDEX));
373 byte[] data = mCursor.getBlob(Browser.HISTORY_PROJECTION_FAVICON_INDEX);
Patrick Scott8f0076b2009-09-17 13:51:30 -0400374 Bitmap bitmap = null;
375 if (data == null) {
376 bitmap = CombinedBookmarkHistoryActivity.getIconListenerSet()
377 .getFavicon(url);
378 } else {
379 bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
380 }
Leon Scroggins892df312009-07-14 14:48:02 -0400381 b.setFavicon(bitmap);
382 }
383
The Android Open Source Project0c908882009-03-03 19:32:16 -0800384 /**
385 * Get a View that displays the data at the specified position
386 * in the list.
387 * @param position Index of the item whose view we want.
388 * @return A View corresponding to the data at the specified position.
389 */
390 public View getView(int position, View convertView, ViewGroup parent) {
391 if (!mDataValid) {
392 throw new IllegalStateException(
393 "this should only be called when the cursor is valid");
394 }
395 if (position < 0 || position > mCount) {
396 throw new AssertionError(
397 "BrowserBookmarksAdapter tried to get a view out of range");
398 }
Ben Murdoch328ea872009-09-16 13:33:29 +0100399 if (mViewMode == BookmarkViewMode.GRID) {
Leon Scroggins892df312009-07-14 14:48:02 -0400400 if (convertView == null || convertView instanceof AddNewBookmark
401 || convertView instanceof BookmarkItem) {
402 LayoutInflater factory = LayoutInflater.from(mBookmarksPage);
403 convertView
404 = factory.inflate(R.layout.bookmark_thumbnail, null);
405 }
Leon Scroggins89c6d362009-07-15 16:54:37 -0400406 View holder = convertView.findViewById(R.id.holder);
Leon Scroggins892df312009-07-14 14:48:02 -0400407 ImageView thumb = (ImageView) convertView.findViewById(R.id.thumb);
Leon Scroggins892df312009-07-14 14:48:02 -0400408 TextView tv = (TextView) convertView.findViewById(R.id.label);
409
Leon Scrogginsa5d669e2009-08-05 14:07:58 -0400410 if (0 == position && mNeedsOffset) {
Leon Scroggins892df312009-07-14 14:48:02 -0400411 // This is to create a bookmark for the current page.
Leon Scroggins89c6d362009-07-15 16:54:37 -0400412 holder.setVisibility(View.VISIBLE);
Leon Scroggins89c6d362009-07-15 16:54:37 -0400413 tv.setText(mCurrentTitle);
Ben Murdochdcc2b6f2009-09-21 14:29:20 +0100414
415 if (mCurrentThumbnail != null) {
416 thumb.setImageBitmap(mCurrentThumbnail);
417 } else {
418 thumb.setImageResource(
Leon Scrogginsf8551612009-09-24 16:06:02 -0400419 R.drawable.browser_thumbnail);
Ben Murdochdcc2b6f2009-09-21 14:29:20 +0100420 }
Leon Scroggins892df312009-07-14 14:48:02 -0400421 return convertView;
422 }
Leon Scroggins89c6d362009-07-15 16:54:37 -0400423 holder.setVisibility(View.GONE);
Leon Scroggins892df312009-07-14 14:48:02 -0400424 mCursor.moveToPosition(position - mExtraOffset);
425 tv.setText(mCursor.getString(
426 Browser.HISTORY_PROJECTION_TITLE_INDEX));
Leon Scroggins96afcb12009-12-10 12:35:56 -0500427 Bitmap thumbnail = getScreenshot(position);
Ben Murdochaac7aa62009-09-17 16:57:40 +0100428 if (thumbnail == null) {
Leon Scrogginsf8551612009-09-24 16:06:02 -0400429 thumb.setImageResource(R.drawable.browser_thumbnail);
Leon Scroggins892df312009-07-14 14:48:02 -0400430 } else {
Ben Murdochaac7aa62009-09-17 16:57:40 +0100431 thumb.setImageBitmap(thumbnail);
Leon Scroggins892df312009-07-14 14:48:02 -0400432 }
Leon Scroggins89c6d362009-07-15 16:54:37 -0400433
Leon Scroggins892df312009-07-14 14:48:02 -0400434 return convertView;
435
436 }
Leon Scrogginsa5d669e2009-08-05 14:07:58 -0400437 if (position == 0 && mNeedsOffset) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800438 AddNewBookmark b;
439 if (convertView instanceof AddNewBookmark) {
440 b = (AddNewBookmark) convertView;
441 } else {
442 b = new AddNewBookmark(mBookmarksPage);
443 }
444 b.setUrl(mCurrentPage);
445 return b;
446 }
Leon Scrogginsa5d669e2009-08-05 14:07:58 -0400447 if (mMostVisited) {
448 if (convertView == null || !(convertView instanceof HistoryItem)) {
449 convertView = new HistoryItem(mBookmarksPage);
450 }
451 } else {
452 if (convertView == null || !(convertView instanceof BookmarkItem)) {
453 convertView = new BookmarkItem(mBookmarksPage);
454 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800455 }
Leon Scrogginsa5d669e2009-08-05 14:07:58 -0400456 bind((BookmarkItem) convertView, position);
457 if (mMostVisited) {
458 ((HistoryItem) convertView).setIsBookmark(
459 getIsBookmark(position));
460 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800461 return convertView;
462 }
463
464 /**
465 * Return the title for this item in the list.
466 */
467 public String getTitle(int position) {
468 return getString(Browser.HISTORY_PROJECTION_TITLE_INDEX, position);
469 }
470
471 /**
472 * Return the Url for this item in the list.
473 */
474 public String getUrl(int position) {
475 return getString(Browser.HISTORY_PROJECTION_URL_INDEX, position);
476 }
477
478 /**
Leon Scroggins96afcb12009-12-10 12:35:56 -0500479 * Return the screenshot for this item in the list.
480 */
481 public Bitmap getScreenshot(int position) {
482 return getBitmap(Browser.HISTORY_PROJECTION_THUMBNAIL_INDEX, position);
483 }
484
485 /**
Patrick Scotte09761e2009-03-24 20:43:37 -0700486 * Return the favicon for this item in the list.
487 */
488 public Bitmap getFavicon(int position) {
Patrick Scott3918d442009-08-04 13:22:29 -0400489 return getBitmap(Browser.HISTORY_PROJECTION_FAVICON_INDEX, position);
490 }
491
492 public Bitmap getTouchIcon(int position) {
493 return getBitmap(Browser.HISTORY_PROJECTION_TOUCH_ICON_INDEX, position);
494 }
495
496 private Bitmap getBitmap(int cursorIndex, int position) {
Patrick Scotte09761e2009-03-24 20:43:37 -0700497 if (position < mExtraOffset || position > mCount) {
498 return null;
499 }
500 mCursor.moveToPosition(position - mExtraOffset);
Patrick Scott3918d442009-08-04 13:22:29 -0400501 byte[] data = mCursor.getBlob(cursorIndex);
Patrick Scotte09761e2009-03-24 20:43:37 -0700502 if (data == null) {
503 return null;
504 }
505 return BitmapFactory.decodeByteArray(data, 0, data.length);
506 }
507
508 /**
Leon Scrogginsa5d669e2009-08-05 14:07:58 -0400509 * Return whether or not this item represents a bookmarked site.
510 */
511 public boolean getIsBookmark(int position) {
512 if (position < mExtraOffset || position > mCount) {
513 return false;
514 }
515 mCursor.moveToPosition(position - mExtraOffset);
516 return (1 == mCursor.getInt(Browser.HISTORY_PROJECTION_BOOKMARK_INDEX));
517 }
518
519 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -0800520 * Private helper function to return the title or url.
521 */
522 private String getString(int cursorIndex, int position) {
523 if (position < mExtraOffset || position > mCount) {
524 return "";
525 }
526 mCursor.moveToPosition(position- mExtraOffset);
527 return mCursor.getString(cursorIndex);
528 }
529
530 private void bind(BookmarkItem b, int position) {
531 mCursor.moveToPosition(position- mExtraOffset);
532
Ben Murdoch9907efc2009-10-28 13:22:46 +0000533 b.setName(mCursor.getString(Browser.HISTORY_PROJECTION_TITLE_INDEX));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800534 String url = mCursor.getString(Browser.HISTORY_PROJECTION_URL_INDEX);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800535 b.setUrl(url);
536 byte[] data = mCursor.getBlob(Browser.HISTORY_PROJECTION_FAVICON_INDEX);
537 if (data != null) {
538 b.setFavicon(BitmapFactory.decodeByteArray(data, 0, data.length));
539 } else {
Patrick Scott8f0076b2009-09-17 13:51:30 -0400540 b.setFavicon(CombinedBookmarkHistoryActivity.getIconListenerSet()
541 .getFavicon(url));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800542 }
543 }
544
545 private class ChangeObserver extends ContentObserver {
546 public ChangeObserver() {
547 super(new Handler());
548 }
549
550 @Override
551 public boolean deliverSelfNotifications() {
552 return true;
553 }
554
555 @Override
556 public void onChange(boolean selfChange) {
557 refreshList();
558 }
559 }
560
561 private class MyDataSetObserver extends DataSetObserver {
562 @Override
563 public void onChanged() {
564 mDataValid = true;
565 notifyDataSetChanged();
566 }
567
568 @Override
569 public void onInvalidated() {
570 mDataValid = false;
571 notifyDataSetInvalidated();
572 }
573 }
574}