FileDocCategorySizeDatePackage
BrowserBookmarksAdapter.javaAPI DocAndroid 1.5 API20379Wed May 06 22:42:42 BST 2009com.android.browser

BrowserBookmarksAdapter

public class BrowserBookmarksAdapter extends android.widget.BaseAdapter

Fields Summary
private final String
LOGTAG
private String
mCurrentPage
private android.database.Cursor
mCursor
private int
mCount
private String
mLastWhereClause
private String[]
mLastSelectionArgs
private String
mLastOrderBy
private BrowserBookmarksPage
mBookmarksPage
private android.content.ContentResolver
mContentResolver
private ChangeObserver
mChangeObserver
private android.database.DataSetObserver
mDataSetObserver
private boolean
mDataValid
private boolean
mCreateShortcut
private int
mExtraOffset
private final IconReceiver
mIconReceiver
Constructors Summary
public BrowserBookmarksAdapter(BrowserBookmarksPage b, String curPage)
Create a new BrowserBookmarksAdapter.

param
b BrowserBookmarksPage that instantiated this. Necessary so it will adjust its focus appropriately after a search.


                                                                           
         
        this(b, curPage, false);
    
public BrowserBookmarksAdapter(BrowserBookmarksPage b, String curPage, boolean createShortcut)
Create a new BrowserBookmarksAdapter.

param
b BrowserBookmarksPage that instantiated this. Necessary so it will adjust its focus appropriately after a search.

        mDataValid = false;
        mCreateShortcut = createShortcut;
        mExtraOffset = createShortcut ? 0 : 1;
        mBookmarksPage = b;
        mCurrentPage = b.getResources().getString(R.string.current_page) + 
                curPage;
        mContentResolver = b.getContentResolver();
        mLastOrderBy = Browser.BookmarkColumns.CREATED + " DESC";
        mChangeObserver = new ChangeObserver();
        mDataSetObserver = new MyDataSetObserver();
        // FIXME: Should have a default sort order that the user selects.
        search(null);
        // FIXME: This requires another query of the database after the
        // initial search(null). Can we optimize this?
        Browser.requestAllIcons(mContentResolver,
                Browser.BookmarkColumns.FAVICON + " is NULL AND " +
                Browser.BookmarkColumns.BOOKMARK + " == 1", mIconReceiver);
    
Methods Summary
public booleanareAllItemsEnabled()

        return true;
    
private voidbind(BookmarkItem b, int position)

        mCursor.moveToPosition(position- mExtraOffset);

        String title = mCursor.getString(Browser.HISTORY_PROJECTION_TITLE_INDEX);
        if (title.length() > BrowserSettings.MAX_TEXTVIEW_LEN) {
            title = title.substring(0, BrowserSettings.MAX_TEXTVIEW_LEN);
        }
        b.setName(title);
        String url = mCursor.getString(Browser.HISTORY_PROJECTION_URL_INDEX);
        if (url.length() > BrowserSettings.MAX_TEXTVIEW_LEN) {
            url = url.substring(0, BrowserSettings.MAX_TEXTVIEW_LEN);
        }
        b.setUrl(url);
        byte[] data = mCursor.getBlob(Browser.HISTORY_PROJECTION_FAVICON_INDEX);
        if (data != null) {
            b.setFavicon(BitmapFactory.decodeByteArray(data, 0, data.length));
        } else {
            b.setFavicon(null);
        }
    
public voiddeleteAllRows()
Delete all bookmarks from the db. Requeries the database. All bookmarks with become visited URLs or if never visited are removed

        StringBuilder deleteIds = null;
        StringBuilder convertIds = null;
        
        for (mCursor.moveToFirst(); !mCursor.isAfterLast(); mCursor.moveToNext()) {
            String url = mCursor.getString(Browser.HISTORY_PROJECTION_URL_INDEX);
            WebIconDatabase.getInstance().releaseIconForPageUrl(url);
            int id = mCursor.getInt(Browser.HISTORY_PROJECTION_ID_INDEX);
            int numVisits = mCursor.getInt(Browser.HISTORY_PROJECTION_VISITS_INDEX);
            if (0 == numVisits) {
                if (deleteIds == null) {
                    deleteIds = new StringBuilder();
                    deleteIds.append("( ");
                } else {
                    deleteIds.append(" OR ( ");
                }
                deleteIds.append(BookmarkColumns._ID);
                deleteIds.append(" = ");
                deleteIds.append(id);
                deleteIds.append(" )");
            } else {
                // It is no longer a bookmark, but it is still a visited site.
                if (convertIds == null) {
                    convertIds = new StringBuilder();
                    convertIds.append("( ");
                } else {
                    convertIds.append(" OR ( ");
                }
                convertIds.append(BookmarkColumns._ID);
                convertIds.append(" = ");
                convertIds.append(id);
                convertIds.append(" )");
            }
        }
        
        if (deleteIds != null) {
            mContentResolver.delete(Browser.BOOKMARKS_URI, deleteIds.toString(), 
                null);
        }
        if (convertIds != null) {
            ContentValues values = new ContentValues();
            values.put(Browser.BookmarkColumns.BOOKMARK, 0);
            mContentResolver.update(Browser.BOOKMARKS_URI, values, 
                    convertIds.toString(), null);
        }
        refreshList();
    
public voiddeleteRow(int position)
Delete a row from the database. Requeries the database. Does nothing if the provided position is out of range.

param
position Position in the list.

        if (position < mExtraOffset || position >= getCount()) {
            return;
        }
        mCursor.moveToPosition(position- mExtraOffset);
        String url = mCursor.getString(Browser.HISTORY_PROJECTION_URL_INDEX);
        WebIconDatabase.getInstance().releaseIconForPageUrl(url);
        Uri uri = ContentUris.withAppendedId(Browser.BOOKMARKS_URI, mCursor
                .getInt(Browser.HISTORY_PROJECTION_ID_INDEX));
        int numVisits = mCursor.getInt(Browser.HISTORY_PROJECTION_VISITS_INDEX);
        if (0 == numVisits) {
            mContentResolver.delete(uri, null, null);
        } else {
            // It is no longer a bookmark, but it is still a visited site.
            ContentValues values = new ContentValues();
            values.put(Browser.BookmarkColumns.BOOKMARK, 0);
            mContentResolver.update(uri, values, null, null);
        }
        refreshList();
    
public intgetCount()
How many items should be displayed in the list.

return
Count of items.

        if (mDataValid) {
            return mCount;
        } else {
            return 0;
        }
    
public android.graphics.BitmapgetFavicon(int position)
Return the favicon for this item in the list.

        if (position < mExtraOffset || position > mCount) {
            return null;
        }
        mCursor.moveToPosition(position - mExtraOffset);
        byte[] data = mCursor.getBlob(Browser.HISTORY_PROJECTION_FAVICON_INDEX);
        if (data == null) {
            return null;
        }
        return BitmapFactory.decodeByteArray(data, 0, data.length);
    
public java.lang.ObjectgetItem(int position)
Get the data associated with the specified position in the list.

param
position Index of the item whose data we want.
return
The data at the specified position.

        return null;
    
public longgetItemId(int position)
Get the row id associated with the specified position in the list.

param
position Index of the item whose row id we want.
return
The id of the item at the specified position.

        return position;
    
public android.os.BundlegetRow(int position)
Return a hashmap with one row's Title, Url, and favicon.

param
position Position in the list.
return
Bundle Stores title, url of row position, favicon, and id for the url. Return a blank map if position is out of range.

        Bundle map = new Bundle();
        if (position < mExtraOffset || position >= mCount) {
            return map;
        }
        mCursor.moveToPosition(position- mExtraOffset);
        String url = mCursor.getString(Browser.HISTORY_PROJECTION_URL_INDEX);
        map.putString(Browser.BookmarkColumns.TITLE, 
                mCursor.getString(Browser.HISTORY_PROJECTION_TITLE_INDEX));
        map.putString(Browser.BookmarkColumns.URL, url);
        byte[] data = mCursor.getBlob(Browser.HISTORY_PROJECTION_FAVICON_INDEX);
        if (data != null) {
            map.putParcelable(Browser.BookmarkColumns.FAVICON,
                    BitmapFactory.decodeByteArray(data, 0, data.length));
        }
        map.putInt("id", mCursor.getInt(Browser.HISTORY_PROJECTION_ID_INDEX));
        return map;
    
private java.lang.StringgetString(int cursorIndex, int position)
Private helper function to return the title or url.

        if (position < mExtraOffset || position > mCount) {
            return "";
        }
        mCursor.moveToPosition(position- mExtraOffset);
        return mCursor.getString(cursorIndex);
    
public java.lang.StringgetTitle(int position)
Return the title for this item in the list.

        return getString(Browser.HISTORY_PROJECTION_TITLE_INDEX, position);
    
public java.lang.StringgetUrl(int position)
Return the Url for this item in the list.

        return getString(Browser.HISTORY_PROJECTION_URL_INDEX, position);
    
public android.view.ViewgetView(int position, android.view.View convertView, android.view.ViewGroup parent)
Get a View that displays the data at the specified position in the list.

param
position Index of the item whose view we want.
return
A View corresponding to the data at the specified position.

        if (!mDataValid) {
            throw new IllegalStateException(
                    "this should only be called when the cursor is valid");
        }
        if (position < 0 || position > mCount) {
            throw new AssertionError(
                    "BrowserBookmarksAdapter tried to get a view out of range");
        }
        if (position == 0 && !mCreateShortcut) {
            AddNewBookmark b;
            if (convertView instanceof AddNewBookmark) {
                b = (AddNewBookmark) convertView;
            } else {
                b = new AddNewBookmark(mBookmarksPage);
            }
            b.setUrl(mCurrentPage);
            return b;
        }
        if (convertView == null || convertView instanceof AddNewBookmark) {
            convertView = new BookmarkItem(mBookmarksPage);
        }
        bind((BookmarkItem)convertView, position);
        return convertView;
    
public booleanisEnabled(int position)

        return true;
    
public voidrefreshList()
Refresh list to recognize a change in the database.

        // FIXME: consider using requery().
        // Need to do more work to get it to function though.
        searchInternal(mLastWhereClause, mLastSelectionArgs, mLastOrderBy);
    
public voidsearch(java.lang.String like)
Search the database for bookmarks that match the input string.

param
like String to use to search the database. Strings with spaces are treated as having multiple search terms using the OR operator. Search both the title and url.

        String whereClause = Browser.BookmarkColumns.BOOKMARK + " == 1";
        String[] selectionArgs = null;
        if (like != null) {
            String[] likes = like.split(" ");
            int count = 0;
            boolean firstTerm = true;
            StringBuilder andClause = new StringBuilder(256);
            for (int j = 0; j < likes.length; j++) {
                if (likes[j].length() > 0) {
                    if (firstTerm) {
                        firstTerm = false;
                    } else {
                        andClause.append(" OR ");
                    }
                    andClause.append(Browser.BookmarkColumns.TITLE
                            + " LIKE ? OR " + Browser.BookmarkColumns.URL
                            + " LIKE ? ");
                    count += 2;
                }
            }
            if (count > 0) {
                selectionArgs = new String[count];
                count = 0;
                for (int j = 0; j < likes.length; j++) {
                    if (likes[j].length() > 0) {
                        like = "%" + likes[j] + "%";
                        selectionArgs[count++] = like;
                        selectionArgs[count++] = like;
                    }
                }
                whereClause += " AND (" + andClause + ")";
            }
        }
        searchInternal(whereClause, selectionArgs, mLastOrderBy);
    
private voidsearchInternal(java.lang.String whereClause, java.lang.String[] selectionArgs, java.lang.String orderBy)
Internal function used in search, sort, and refreshList.

        if (mCursor != null) {
            mCursor.unregisterContentObserver(mChangeObserver);
            mCursor.unregisterDataSetObserver(mDataSetObserver);
            mBookmarksPage.stopManagingCursor(mCursor);
            mCursor.deactivate();
        }

        mLastWhereClause = whereClause;
        mLastSelectionArgs = selectionArgs;
        mLastOrderBy = orderBy;
        mCursor = mContentResolver.query(
            Browser.BOOKMARKS_URI,
            Browser.HISTORY_PROJECTION,
            whereClause,
            selectionArgs, 
            orderBy);
        mCursor.registerContentObserver(mChangeObserver);
        mCursor.registerDataSetObserver(mDataSetObserver);
        mBookmarksPage.startManagingCursor(mCursor);

        mDataValid = true;
        notifyDataSetChanged();

        mCount = mCursor.getCount() + mExtraOffset;
    
public voidsortAlphabetical()
This sorts alphabetically, with non-capitalized titles before capitalized.

        searchInternal(mLastWhereClause, mLastSelectionArgs,
                Browser.BookmarkColumns.TITLE + " COLLATE UNICODE ASC");
    
static voidupdateBookmarkFavicon(android.content.ContentResolver cr, java.lang.String url, android.graphics.Bitmap favicon)
Update the bookmark's favicon.

param
cr The ContentResolver to use.
param
url The url of the bookmark to update.
param
favicon The favicon bitmap to write to the db.

        if (url == null || favicon == null) {
            return;
        }
        // Strip the query.
        int query = url.indexOf('?");
        String noQuery = url;
        if (query != -1) {
            noQuery = url.substring(0, query);
        }
        url = noQuery + '?";
        // Use noQuery to search for the base url (i.e. if the url is
        // http://www.yahoo.com/?rs=1, search for http://www.yahoo.com)
        // Use url to match the base url with other queries (i.e. if the url is
        // http://www.google.com/m, search for
        // http://www.google.com/m?some_query)
        final String[] selArgs = new String[] { noQuery, url };
        final String where = "(" + Browser.BookmarkColumns.URL + " == ? OR "
                + Browser.BookmarkColumns.URL + " GLOB ? || '*') AND "
                + Browser.BookmarkColumns.BOOKMARK + " == 1";
        final String[] projection = new String[] { Browser.BookmarkColumns._ID };
        final Cursor c = cr.query(Browser.BOOKMARKS_URI, projection, where,
                selArgs, null);
        boolean succeed = c.moveToFirst();
        ContentValues values = null;
        while (succeed) {
            if (values == null) {
                final ByteArrayOutputStream os = new ByteArrayOutputStream();
                favicon.compress(Bitmap.CompressFormat.PNG, 100, os);
                values = new ContentValues();
                values.put(Browser.BookmarkColumns.FAVICON, os.toByteArray());
            }
            cr.update(ContentUris.withAppendedId(Browser.BOOKMARKS_URI, c
                    .getInt(0)), values, null, null);
            succeed = c.moveToNext();
        }
        c.close();
    
public voidupdateRow(android.os.Bundle map)
Update a row in the database with new information. Requeries the database if the information has changed.

param
map Bundle storing id, title and url of new information


        // Find the record
        int id = map.getInt("id");
        int position = -1;
        for (mCursor.moveToFirst(); !mCursor.isAfterLast(); mCursor.moveToNext()) {
            if (mCursor.getInt(Browser.HISTORY_PROJECTION_ID_INDEX) == id) {
                position = mCursor.getPosition();
                break;
            }
        }
        if (position < 0) {
            return;
        }

        mCursor.moveToPosition(position);
        ContentValues values = new ContentValues();
        String title = map.getString(Browser.BookmarkColumns.TITLE);
        if (!title.equals(mCursor
                .getString(Browser.HISTORY_PROJECTION_TITLE_INDEX))) {
            values.put(Browser.BookmarkColumns.TITLE, title);
        }
        String url = map.getString(Browser.BookmarkColumns.URL);
        if (!url.equals(mCursor.
                getString(Browser.HISTORY_PROJECTION_URL_INDEX))) {
            values.put(Browser.BookmarkColumns.URL, url);
        }
        if (values.size() > 0
                && mContentResolver.update(Browser.BOOKMARKS_URI, values,
                        "_id = " + id, null) != -1) {
            refreshList();
        }