blob: bd2916674aa3600cfdefc1abe6f84bd169091a64 [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;
37import android.webkit.WebIconDatabase.IconListener;
Patrick Scott3918d442009-08-04 13:22:29 -040038import android.webkit.WebView;
The Android Open Source Project0c908882009-03-03 19:32:16 -080039import android.widget.BaseAdapter;
Leon Scroggins892df312009-07-14 14:48:02 -040040import android.widget.ImageView;
41import android.widget.TextView;
The Android Open Source Project0c908882009-03-03 19:32:16 -080042
43import java.io.ByteArrayOutputStream;
44
45class BrowserBookmarksAdapter extends BaseAdapter {
46
The Android Open Source Project0c908882009-03-03 19:32:16 -080047 private String mCurrentPage;
Leon Scroggins89c6d362009-07-15 16:54:37 -040048 private String mCurrentTitle;
Ben Murdochdcc2b6f2009-09-21 14:29:20 +010049 private Bitmap mCurrentThumbnail;
The Android Open Source Project0c908882009-03-03 19:32:16 -080050 private Cursor mCursor;
51 private int mCount;
The Android Open Source Project0c908882009-03-03 19:32:16 -080052 private BrowserBookmarksPage mBookmarksPage;
53 private ContentResolver mContentResolver;
The Android Open Source Project0c908882009-03-03 19:32:16 -080054 private boolean mDataValid;
Ben Murdoch328ea872009-09-16 13:33:29 +010055 private BookmarkViewMode mViewMode;
Leon Scrogginsa5d669e2009-08-05 14:07:58 -040056 private boolean mMostVisited;
57 private boolean mNeedsOffset;
58 private int mExtraOffset;
The Android Open Source Project0c908882009-03-03 19:32:16 -080059
60 // Implementation of WebIconDatabase.IconListener
61 private class IconReceiver implements IconListener {
62 public void onReceivedIcon(String url, Bitmap icon) {
Patrick Scott3918d442009-08-04 13:22:29 -040063 updateBookmarkFavicon(mContentResolver, null, url, icon);
The Android Open Source Project0c908882009-03-03 19:32:16 -080064 }
65 }
66
67 // Instance of IconReceiver
68 private final IconReceiver mIconReceiver = new IconReceiver();
69
70 /**
71 * Create a new BrowserBookmarksAdapter.
The Android Open Source Project0c908882009-03-03 19:32:16 -080072 * @param b BrowserBookmarksPage that instantiated this.
73 * Necessary so it will adjust its focus
74 * appropriately after a search.
75 */
76 public BrowserBookmarksAdapter(BrowserBookmarksPage b, String curPage,
Ben Murdochdcc2b6f2009-09-21 14:29:20 +010077 String curTitle, Bitmap curThumbnail, boolean createShortcut,
78 boolean mostVisited) {
Leon Scrogginsa5d669e2009-08-05 14:07:58 -040079 mNeedsOffset = !(createShortcut || mostVisited);
80 mMostVisited = mostVisited;
81 mExtraOffset = mNeedsOffset ? 1 : 0;
The Android Open Source Project0c908882009-03-03 19:32:16 -080082 mBookmarksPage = b;
Leon Scroggins89c6d362009-07-15 16:54:37 -040083 mCurrentPage = b.getResources().getString(R.string.current_page)
84 + curPage;
85 mCurrentTitle = curTitle;
Ben Murdochdcc2b6f2009-09-21 14:29:20 +010086 mCurrentThumbnail = curThumbnail;
The Android Open Source Project0c908882009-03-03 19:32:16 -080087 mContentResolver = b.getContentResolver();
Ben Murdoch328ea872009-09-16 13:33:29 +010088 mViewMode = BookmarkViewMode.LIST;
Leon Scroggins892df312009-07-14 14:48:02 -040089
Leon Scrogginsa5d669e2009-08-05 14:07:58 -040090 String whereClause;
The Android Open Source Project0c908882009-03-03 19:32:16 -080091 // FIXME: Should have a default sort order that the user selects.
Leon Scroggins892df312009-07-14 14:48:02 -040092 String orderBy = Browser.BookmarkColumns.VISITS + " DESC";
Leon Scrogginsa5d669e2009-08-05 14:07:58 -040093 if (mostVisited) {
94 whereClause = Browser.BookmarkColumns.VISITS + " != 0";
95 } else {
96 whereClause = Browser.BookmarkColumns.BOOKMARK + " != 0";
97 }
Leon Scroggins892df312009-07-14 14:48:02 -040098 mCursor = b.managedQuery(Browser.BOOKMARKS_URI,
99 Browser.HISTORY_PROJECTION, whereClause, null, orderBy);
100 mCursor.registerContentObserver(new ChangeObserver());
101 mCursor.registerDataSetObserver(new MyDataSetObserver());
102
103 mDataValid = true;
104 notifyDataSetChanged();
105
106 mCount = mCursor.getCount() + mExtraOffset;
107
The Android Open Source Project0c908882009-03-03 19:32:16 -0800108 // FIXME: This requires another query of the database after the
Leon Scroggins89c6d362009-07-15 16:54:37 -0400109 // managedQuery. Can we optimize this?
The Android Open Source Project0c908882009-03-03 19:32:16 -0800110 Browser.requestAllIcons(mContentResolver,
111 Browser.BookmarkColumns.FAVICON + " is NULL AND " +
112 Browser.BookmarkColumns.BOOKMARK + " == 1", mIconReceiver);
113 }
114
115 /**
116 * Return a hashmap with one row's Title, Url, and favicon.
117 * @param position Position in the list.
118 * @return Bundle Stores title, url of row position, favicon, and id
119 * for the url. Return a blank map if position is out of
120 * range.
121 */
122 public Bundle getRow(int position) {
123 Bundle map = new Bundle();
124 if (position < mExtraOffset || position >= mCount) {
125 return map;
126 }
127 mCursor.moveToPosition(position- mExtraOffset);
128 String url = mCursor.getString(Browser.HISTORY_PROJECTION_URL_INDEX);
129 map.putString(Browser.BookmarkColumns.TITLE,
130 mCursor.getString(Browser.HISTORY_PROJECTION_TITLE_INDEX));
131 map.putString(Browser.BookmarkColumns.URL, url);
132 byte[] data = mCursor.getBlob(Browser.HISTORY_PROJECTION_FAVICON_INDEX);
133 if (data != null) {
134 map.putParcelable(Browser.BookmarkColumns.FAVICON,
135 BitmapFactory.decodeByteArray(data, 0, data.length));
136 }
137 map.putInt("id", mCursor.getInt(Browser.HISTORY_PROJECTION_ID_INDEX));
138 return map;
139 }
140
141 /**
142 * Update a row in the database with new information.
143 * Requeries the database if the information has changed.
144 * @param map Bundle storing id, title and url of new information
145 */
146 public void updateRow(Bundle map) {
147
148 // Find the record
149 int id = map.getInt("id");
150 int position = -1;
151 for (mCursor.moveToFirst(); !mCursor.isAfterLast(); mCursor.moveToNext()) {
152 if (mCursor.getInt(Browser.HISTORY_PROJECTION_ID_INDEX) == id) {
153 position = mCursor.getPosition();
154 break;
155 }
156 }
157 if (position < 0) {
158 return;
159 }
160
161 mCursor.moveToPosition(position);
162 ContentValues values = new ContentValues();
163 String title = map.getString(Browser.BookmarkColumns.TITLE);
164 if (!title.equals(mCursor
165 .getString(Browser.HISTORY_PROJECTION_TITLE_INDEX))) {
166 values.put(Browser.BookmarkColumns.TITLE, title);
167 }
168 String url = map.getString(Browser.BookmarkColumns.URL);
169 if (!url.equals(mCursor.
170 getString(Browser.HISTORY_PROJECTION_URL_INDEX))) {
171 values.put(Browser.BookmarkColumns.URL, url);
172 }
173 if (values.size() > 0
174 && mContentResolver.update(Browser.BOOKMARKS_URI, values,
175 "_id = " + id, null) != -1) {
176 refreshList();
177 }
178 }
179
180 /**
181 * Delete a row from the database. Requeries the database.
182 * Does nothing if the provided position is out of range.
183 * @param position Position in the list.
184 */
185 public void deleteRow(int position) {
186 if (position < mExtraOffset || position >= getCount()) {
187 return;
188 }
189 mCursor.moveToPosition(position- mExtraOffset);
190 String url = mCursor.getString(Browser.HISTORY_PROJECTION_URL_INDEX);
Leon Scrogginse372c022009-06-12 17:07:29 -0400191 Bookmarks.removeFromBookmarks(null, mContentResolver, url);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800192 refreshList();
193 }
194
195 /**
196 * Delete all bookmarks from the db. Requeries the database.
197 * All bookmarks with become visited URLs or if never visited
198 * are removed
199 */
200 public void deleteAllRows() {
201 StringBuilder deleteIds = null;
202 StringBuilder convertIds = null;
203
204 for (mCursor.moveToFirst(); !mCursor.isAfterLast(); mCursor.moveToNext()) {
205 String url = mCursor.getString(Browser.HISTORY_PROJECTION_URL_INDEX);
206 WebIconDatabase.getInstance().releaseIconForPageUrl(url);
207 int id = mCursor.getInt(Browser.HISTORY_PROJECTION_ID_INDEX);
208 int numVisits = mCursor.getInt(Browser.HISTORY_PROJECTION_VISITS_INDEX);
209 if (0 == numVisits) {
210 if (deleteIds == null) {
211 deleteIds = new StringBuilder();
212 deleteIds.append("( ");
213 } else {
214 deleteIds.append(" OR ( ");
215 }
216 deleteIds.append(BookmarkColumns._ID);
217 deleteIds.append(" = ");
218 deleteIds.append(id);
219 deleteIds.append(" )");
220 } else {
221 // It is no longer a bookmark, but it is still a visited site.
222 if (convertIds == null) {
223 convertIds = new StringBuilder();
224 convertIds.append("( ");
225 } else {
226 convertIds.append(" OR ( ");
227 }
228 convertIds.append(BookmarkColumns._ID);
229 convertIds.append(" = ");
230 convertIds.append(id);
231 convertIds.append(" )");
232 }
233 }
234
235 if (deleteIds != null) {
236 mContentResolver.delete(Browser.BOOKMARKS_URI, deleteIds.toString(),
237 null);
238 }
239 if (convertIds != null) {
240 ContentValues values = new ContentValues();
241 values.put(Browser.BookmarkColumns.BOOKMARK, 0);
242 mContentResolver.update(Browser.BOOKMARKS_URI, values,
243 convertIds.toString(), null);
244 }
245 refreshList();
246 }
247
248 /**
249 * Refresh list to recognize a change in the database.
250 */
251 public void refreshList() {
Leon Scroggins892df312009-07-14 14:48:02 -0400252 mCursor.requery();
253 mCount = mCursor.getCount() + mExtraOffset;
254 notifyDataSetChanged();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800255 }
256
257 /**
Patrick Scott3918d442009-08-04 13:22:29 -0400258 * Update the bookmark's favicon. This is a convenience method for updating
259 * a bookmark favicon for the originalUrl and url of the passed in WebView.
The Android Open Source Project0c908882009-03-03 19:32:16 -0800260 * @param cr The ContentResolver to use.
Patrick Scott3918d442009-08-04 13:22:29 -0400261 * @param WebView The WebView containing the url to update.
The Android Open Source Project0c908882009-03-03 19:32:16 -0800262 * @param favicon The favicon bitmap to write to the db.
263 */
264 /* package */ static void updateBookmarkFavicon(ContentResolver cr,
Patrick Scott3918d442009-08-04 13:22:29 -0400265 WebView view, Bitmap favicon) {
266 if (view != null) {
267 updateBookmarkFavicon(cr, view.getOriginalUrl(), view.getUrl(),
268 favicon);
269 }
270 }
271
272 private static void updateBookmarkFavicon(ContentResolver cr,
273 String originalUrl, String url, Bitmap favicon) {
Leon Scrogginsa5d669e2009-08-05 14:07:58 -0400274 final Cursor c = queryBookmarksForUrl(cr, originalUrl, url, true);
Patrick Scott3918d442009-08-04 13:22:29 -0400275 if (c == null) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800276 return;
277 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800278 boolean succeed = c.moveToFirst();
279 ContentValues values = null;
280 while (succeed) {
281 if (values == null) {
282 final ByteArrayOutputStream os = new ByteArrayOutputStream();
283 favicon.compress(Bitmap.CompressFormat.PNG, 100, os);
284 values = new ContentValues();
285 values.put(Browser.BookmarkColumns.FAVICON, os.toByteArray());
286 }
287 cr.update(ContentUris.withAppendedId(Browser.BOOKMARKS_URI, c
288 .getInt(0)), values, null, null);
289 succeed = c.moveToNext();
290 }
291 c.close();
292 }
293
Patrick Scott3918d442009-08-04 13:22:29 -0400294 /* package */ static Cursor queryBookmarksForUrl(ContentResolver cr,
Leon Scrogginsa5d669e2009-08-05 14:07:58 -0400295 String originalUrl, String url, boolean onlyBookmarks) {
Patrick Scott3918d442009-08-04 13:22:29 -0400296 if (cr == null || url == null) {
297 return null;
298 }
299
300 // If originalUrl is null, just set it to url.
301 if (originalUrl == null) {
302 originalUrl = url;
303 }
304
305 // Look for both the original url and the actual url. This takes in to
306 // account redirects.
307 String originalUrlNoQuery = removeQuery(originalUrl);
308 String urlNoQuery = removeQuery(url);
309 originalUrl = originalUrlNoQuery + '?';
310 url = urlNoQuery + '?';
311
312 // Use NoQuery to search for the base url (i.e. if the url is
313 // http://www.yahoo.com/?rs=1, search for http://www.yahoo.com)
314 // Use url to match the base url with other queries (i.e. if the url is
315 // http://www.google.com/m, search for
316 // http://www.google.com/m?some_query)
317 final String[] selArgs = new String[] {
318 originalUrlNoQuery, urlNoQuery, originalUrl, url };
Leon Scrogginsa5d669e2009-08-05 14:07:58 -0400319 String where = BookmarkColumns.URL + " == ? OR "
Patrick Scott3918d442009-08-04 13:22:29 -0400320 + BookmarkColumns.URL + " == ? OR "
321 + BookmarkColumns.URL + " GLOB ? || '*' OR "
Leon Scrogginsa5d669e2009-08-05 14:07:58 -0400322 + BookmarkColumns.URL + " GLOB ? || '*'";
323 if (onlyBookmarks) {
324 where = "(" + where + ") AND " + BookmarkColumns.BOOKMARK + " == 1";
325 }
Patrick Scott3918d442009-08-04 13:22:29 -0400326 final String[] projection =
327 new String[] { Browser.BookmarkColumns._ID };
328 return cr.query(Browser.BOOKMARKS_URI, projection, where, selArgs,
329 null);
330 }
331
332 // Strip the query from the given url.
333 private static String removeQuery(String url) {
334 if (url == null) {
335 return null;
336 }
337 int query = url.indexOf('?');
338 String noQuery = url;
339 if (query != -1) {
340 noQuery = url.substring(0, query);
341 }
342 return noQuery;
343 }
344
The Android Open Source Project0c908882009-03-03 19:32:16 -0800345 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -0800346 * How many items should be displayed in the list.
347 * @return Count of items.
348 */
349 public int getCount() {
350 if (mDataValid) {
351 return mCount;
352 } else {
353 return 0;
354 }
355 }
356
357 public boolean areAllItemsEnabled() {
358 return true;
359 }
360
361 public boolean isEnabled(int position) {
362 return true;
363 }
364
365 /**
366 * Get the data associated with the specified position in the list.
367 * @param position Index of the item whose data we want.
368 * @return The data at the specified position.
369 */
370 public Object getItem(int position) {
371 return null;
372 }
373
374 /**
375 * Get the row id associated with the specified position in the list.
376 * @param position Index of the item whose row id we want.
377 * @return The id of the item at the specified position.
378 */
379 public long getItemId(int position) {
380 return position;
381 }
382
Ben Murdoch328ea872009-09-16 13:33:29 +0100383 /* package */ void switchViewMode(BookmarkViewMode viewMode) {
384 mViewMode = viewMode;
Leon Scroggins892df312009-07-14 14:48:02 -0400385 }
386
387 /* package */ void populateBookmarkItem(BookmarkItem b, int position) {
388 mCursor.moveToPosition(position - mExtraOffset);
Patrick Scott8f0076b2009-09-17 13:51:30 -0400389 String url = mCursor.getString(Browser.HISTORY_PROJECTION_URL_INDEX);
390 b.setUrl(url);
Leon Scroggins892df312009-07-14 14:48:02 -0400391 b.setName(mCursor.getString(Browser.HISTORY_PROJECTION_TITLE_INDEX));
392 byte[] data = mCursor.getBlob(Browser.HISTORY_PROJECTION_FAVICON_INDEX);
Patrick Scott8f0076b2009-09-17 13:51:30 -0400393 Bitmap bitmap = null;
394 if (data == null) {
395 bitmap = CombinedBookmarkHistoryActivity.getIconListenerSet()
396 .getFavicon(url);
397 } else {
398 bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
399 }
Leon Scroggins892df312009-07-14 14:48:02 -0400400 b.setFavicon(bitmap);
401 }
402
The Android Open Source Project0c908882009-03-03 19:32:16 -0800403 /**
404 * Get a View that displays the data at the specified position
405 * in the list.
406 * @param position Index of the item whose view we want.
407 * @return A View corresponding to the data at the specified position.
408 */
409 public View getView(int position, View convertView, ViewGroup parent) {
410 if (!mDataValid) {
411 throw new IllegalStateException(
412 "this should only be called when the cursor is valid");
413 }
414 if (position < 0 || position > mCount) {
415 throw new AssertionError(
416 "BrowserBookmarksAdapter tried to get a view out of range");
417 }
Ben Murdoch328ea872009-09-16 13:33:29 +0100418 if (mViewMode == BookmarkViewMode.GRID) {
Leon Scroggins892df312009-07-14 14:48:02 -0400419 if (convertView == null || convertView instanceof AddNewBookmark
420 || convertView instanceof BookmarkItem) {
421 LayoutInflater factory = LayoutInflater.from(mBookmarksPage);
422 convertView
423 = factory.inflate(R.layout.bookmark_thumbnail, null);
424 }
Leon Scroggins89c6d362009-07-15 16:54:37 -0400425 View holder = convertView.findViewById(R.id.holder);
Leon Scroggins892df312009-07-14 14:48:02 -0400426 ImageView thumb = (ImageView) convertView.findViewById(R.id.thumb);
Leon Scroggins892df312009-07-14 14:48:02 -0400427 TextView tv = (TextView) convertView.findViewById(R.id.label);
428
Leon Scrogginsa5d669e2009-08-05 14:07:58 -0400429 if (0 == position && mNeedsOffset) {
Leon Scroggins892df312009-07-14 14:48:02 -0400430 // This is to create a bookmark for the current page.
Leon Scroggins89c6d362009-07-15 16:54:37 -0400431 holder.setVisibility(View.VISIBLE);
Leon Scroggins89c6d362009-07-15 16:54:37 -0400432 tv.setText(mCurrentTitle);
Ben Murdochdcc2b6f2009-09-21 14:29:20 +0100433
434 if (mCurrentThumbnail != null) {
435 thumb.setImageBitmap(mCurrentThumbnail);
436 } else {
437 thumb.setImageResource(
438 R.drawable.ic_launcher_shortcut_browser_bookmark);
439 }
Leon Scroggins892df312009-07-14 14:48:02 -0400440 return convertView;
441 }
Leon Scroggins89c6d362009-07-15 16:54:37 -0400442 holder.setVisibility(View.GONE);
Leon Scroggins892df312009-07-14 14:48:02 -0400443 mCursor.moveToPosition(position - mExtraOffset);
444 tv.setText(mCursor.getString(
445 Browser.HISTORY_PROJECTION_TITLE_INDEX));
446 byte[] data = mCursor.getBlob(
447 Browser.HISTORY_PROJECTION_THUMBNAIL_INDEX);
448 if (data == null) {
Leon Scrogginsb40bf272009-09-14 18:43:27 -0400449 thumb.setImageResource(R.drawable.ic_launcher_shortcut_browser_bookmark);
Leon Scroggins892df312009-07-14 14:48:02 -0400450 } else {
451 thumb.setImageBitmap(
452 BitmapFactory.decodeByteArray(data, 0, data.length));
453 }
Leon Scroggins89c6d362009-07-15 16:54:37 -0400454
Leon Scroggins892df312009-07-14 14:48:02 -0400455 return convertView;
456
457 }
Leon Scrogginsa5d669e2009-08-05 14:07:58 -0400458 if (position == 0 && mNeedsOffset) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800459 AddNewBookmark b;
460 if (convertView instanceof AddNewBookmark) {
461 b = (AddNewBookmark) convertView;
462 } else {
463 b = new AddNewBookmark(mBookmarksPage);
464 }
465 b.setUrl(mCurrentPage);
466 return b;
467 }
Leon Scrogginsa5d669e2009-08-05 14:07:58 -0400468 if (mMostVisited) {
469 if (convertView == null || !(convertView instanceof HistoryItem)) {
470 convertView = new HistoryItem(mBookmarksPage);
471 }
472 } else {
473 if (convertView == null || !(convertView instanceof BookmarkItem)) {
474 convertView = new BookmarkItem(mBookmarksPage);
475 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800476 }
Leon Scrogginsa5d669e2009-08-05 14:07:58 -0400477 bind((BookmarkItem) convertView, position);
478 if (mMostVisited) {
479 ((HistoryItem) convertView).setIsBookmark(
480 getIsBookmark(position));
481 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800482 return convertView;
483 }
484
485 /**
486 * Return the title for this item in the list.
487 */
488 public String getTitle(int position) {
489 return getString(Browser.HISTORY_PROJECTION_TITLE_INDEX, position);
490 }
491
492 /**
493 * Return the Url for this item in the list.
494 */
495 public String getUrl(int position) {
496 return getString(Browser.HISTORY_PROJECTION_URL_INDEX, position);
497 }
498
499 /**
Patrick Scotte09761e2009-03-24 20:43:37 -0700500 * Return the favicon for this item in the list.
501 */
502 public Bitmap getFavicon(int position) {
Patrick Scott3918d442009-08-04 13:22:29 -0400503 return getBitmap(Browser.HISTORY_PROJECTION_FAVICON_INDEX, position);
504 }
505
506 public Bitmap getTouchIcon(int position) {
507 return getBitmap(Browser.HISTORY_PROJECTION_TOUCH_ICON_INDEX, position);
508 }
509
510 private Bitmap getBitmap(int cursorIndex, int position) {
Patrick Scotte09761e2009-03-24 20:43:37 -0700511 if (position < mExtraOffset || position > mCount) {
512 return null;
513 }
514 mCursor.moveToPosition(position - mExtraOffset);
Patrick Scott3918d442009-08-04 13:22:29 -0400515 byte[] data = mCursor.getBlob(cursorIndex);
Patrick Scotte09761e2009-03-24 20:43:37 -0700516 if (data == null) {
517 return null;
518 }
519 return BitmapFactory.decodeByteArray(data, 0, data.length);
520 }
521
522 /**
Leon Scrogginsa5d669e2009-08-05 14:07:58 -0400523 * Return whether or not this item represents a bookmarked site.
524 */
525 public boolean getIsBookmark(int position) {
526 if (position < mExtraOffset || position > mCount) {
527 return false;
528 }
529 mCursor.moveToPosition(position - mExtraOffset);
530 return (1 == mCursor.getInt(Browser.HISTORY_PROJECTION_BOOKMARK_INDEX));
531 }
532
533 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -0800534 * Private helper function to return the title or url.
535 */
536 private String getString(int cursorIndex, int position) {
537 if (position < mExtraOffset || position > mCount) {
538 return "";
539 }
540 mCursor.moveToPosition(position- mExtraOffset);
541 return mCursor.getString(cursorIndex);
542 }
543
544 private void bind(BookmarkItem b, int position) {
545 mCursor.moveToPosition(position- mExtraOffset);
546
547 String title = mCursor.getString(Browser.HISTORY_PROJECTION_TITLE_INDEX);
548 if (title.length() > BrowserSettings.MAX_TEXTVIEW_LEN) {
549 title = title.substring(0, BrowserSettings.MAX_TEXTVIEW_LEN);
550 }
551 b.setName(title);
552 String url = mCursor.getString(Browser.HISTORY_PROJECTION_URL_INDEX);
553 if (url.length() > BrowserSettings.MAX_TEXTVIEW_LEN) {
554 url = url.substring(0, BrowserSettings.MAX_TEXTVIEW_LEN);
555 }
556 b.setUrl(url);
557 byte[] data = mCursor.getBlob(Browser.HISTORY_PROJECTION_FAVICON_INDEX);
558 if (data != null) {
559 b.setFavicon(BitmapFactory.decodeByteArray(data, 0, data.length));
560 } else {
Patrick Scott8f0076b2009-09-17 13:51:30 -0400561 b.setFavicon(CombinedBookmarkHistoryActivity.getIconListenerSet()
562 .getFavicon(url));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800563 }
564 }
565
566 private class ChangeObserver extends ContentObserver {
567 public ChangeObserver() {
568 super(new Handler());
569 }
570
571 @Override
572 public boolean deliverSelfNotifications() {
573 return true;
574 }
575
576 @Override
577 public void onChange(boolean selfChange) {
578 refreshList();
579 }
580 }
581
582 private class MyDataSetObserver extends DataSetObserver {
583 @Override
584 public void onChanged() {
585 mDataValid = true;
586 notifyDataSetChanged();
587 }
588
589 @Override
590 public void onInvalidated() {
591 mDataValid = false;
592 notifyDataSetInvalidated();
593 }
594 }
595}