FileDocCategorySizeDatePackage
BrowserProvider.javaAPI DocAndroid 1.5 API26851Wed May 06 22:42:42 BST 2009com.android.browser

BrowserProvider

public class BrowserProvider extends android.content.ContentProvider

Fields Summary
private android.database.sqlite.SQLiteOpenHelper
mOpenHelper
private static final String
sDatabaseName
private static final String
TAG
private static final String
ORDER_BY
private static final String
PICASA_URL
private static final String[]
TABLE_NAMES
private static final String[]
SUGGEST_PROJECTION
private static final String
SUGGEST_SELECTION
private String[]
SUGGEST_ARGS
private static final int
SUGGEST_COLUMN_INTENT_ACTION_ID
private static final int
SUGGEST_COLUMN_INTENT_DATA_ID
private static final int
SUGGEST_COLUMN_TEXT_1_ID
private static final int
SUGGEST_COLUMN_TEXT_2_ID
private static final int
SUGGEST_COLUMN_ICON_1_ID
private static final int
SUGGEST_COLUMN_ICON_2_ID
private static final int
SUGGEST_COLUMN_QUERY_ID
private static final String[]
COLUMNS
private static final int
MAX_SUGGESTION_SHORT_ENTRIES
private static final int
MAX_SUGGESTION_LONG_ENTRIES
private static final int
URI_MATCH_BOOKMARKS
private static final int
URI_MATCH_SEARCHES
private static final int
URI_MATCH_BOOKMARKS_ID
private static final int
URI_MATCH_SEARCHES_ID
private static final int
URI_MATCH_SUGGEST
private static final android.content.UriMatcher
URI_MATCHER
private static final int
DATABASE_VERSION
Constructors Summary
public BrowserProvider()


      
    
Methods Summary
public intdelete(android.net.Uri url, java.lang.String where, java.lang.String[] whereArgs)

        SQLiteDatabase db = mOpenHelper.getWritableDatabase();

        int match = URI_MATCHER.match(url);
        if (match == -1 || match == URI_MATCH_SUGGEST) {
            throw new IllegalArgumentException("Unknown URL");
        }

        if (match == URI_MATCH_BOOKMARKS_ID || match == URI_MATCH_SEARCHES_ID) {
            StringBuilder sb = new StringBuilder();
            if (where != null && where.length() > 0) {
                sb.append("( ");
                sb.append(where);
                sb.append(" ) AND ");
            }
            sb.append("_id = ");
            sb.append(url.getPathSegments().get(1));
            where = sb.toString();
        }

        int count = db.delete(TABLE_NAMES[match % 10], where, whereArgs);
        getContext().getContentResolver().notifyChange(url, null);
        return count;
    
private voidfixPicasaBookmark()

        SQLiteDatabase db = mOpenHelper.getWritableDatabase();
        Cursor cursor = db.rawQuery("SELECT _id FROM bookmarks WHERE " +
                "bookmark = 1 AND url = ?", new String[] { PICASA_URL });
        try {
            if (!cursor.moveToFirst()) {
                // set "created" so that it will be on the top of the list
                db.execSQL("INSERT INTO bookmarks (title, url, visits, " +
                        "date, created, bookmark)" + " VALUES('" +
                        getContext().getString(R.string.picasa) + "', '"
                        + PICASA_URL + "', 0, 0, " + new Date().getTime()
                        + ", 1);");
            }
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
    
public java.lang.StringgetType(android.net.Uri url)

        int match = URI_MATCHER.match(url);
        switch (match) {
            case URI_MATCH_BOOKMARKS:
                return "vnd.android.cursor.dir/bookmark";

            case URI_MATCH_BOOKMARKS_ID:
                return "vnd.android.cursor.item/bookmark";

            case URI_MATCH_SEARCHES:
                return "vnd.android.cursor.dir/searches";

            case URI_MATCH_SEARCHES_ID:
                return "vnd.android.cursor.item/searches";

            case URI_MATCH_SUGGEST:
                return SearchManager.SUGGEST_MIME_TYPE;

            default:
                throw new IllegalArgumentException("Unknown URL");
        }
    
public android.net.Uriinsert(android.net.Uri url, android.content.ContentValues initialValues)

        SQLiteDatabase db = mOpenHelper.getWritableDatabase();

        int match = URI_MATCHER.match(url);
        Uri uri = null;
        switch (match) {
            case URI_MATCH_BOOKMARKS: {
                // Insert into the bookmarks table
                long rowID = db.insert(TABLE_NAMES[URI_MATCH_BOOKMARKS], "url",
                        initialValues);
                if (rowID > 0) {
                    uri = ContentUris.withAppendedId(Browser.BOOKMARKS_URI,
                            rowID);
                }
                break;
            }

            case URI_MATCH_SEARCHES: {
                // Insert into the searches table
                long rowID = db.insert(TABLE_NAMES[URI_MATCH_SEARCHES], "url",
                        initialValues);
                if (rowID > 0) {
                    uri = ContentUris.withAppendedId(Browser.SEARCHES_URI,
                            rowID);
                }
                break;
            }

            default:
                throw new IllegalArgumentException("Unknown URL");
        }

        if (uri == null) {
            throw new IllegalArgumentException("Unknown URL");
        }
        getContext().getContentResolver().notifyChange(uri, null);
        return uri;
    
public booleanonCreate()

        final Context context = getContext();
        mOpenHelper = new DatabaseHelper(context);
        // we added "picasa web album" into default bookmarks for version 19. 
        // To avoid erasing the bookmark table, we added it explicitly for
        // version 18 and 19 as in the other cases, we will erase the table.
        if (DATABASE_VERSION == 18 || DATABASE_VERSION == 19) {
            SharedPreferences p = PreferenceManager
                    .getDefaultSharedPreferences(context);
            boolean fix = p.getBoolean("fix_picasa", true);
            if (fix) {
                fixPicasaBookmark();
                Editor ed = p.edit();
                ed.putBoolean("fix_picasa", false);
                ed.commit();
            }
        }
        return true;
    
public android.database.Cursorquery(android.net.Uri url, java.lang.String[] projectionIn, java.lang.String selection, java.lang.String[] selectionArgs, java.lang.String sortOrder)

        SQLiteDatabase db = mOpenHelper.getReadableDatabase();

        int match = URI_MATCHER.match(url);
        if (match == -1) {
            throw new IllegalArgumentException("Unknown URL");
        }

        if (match == URI_MATCH_SUGGEST) {
            String suggestSelection;
            String [] myArgs;
            if (selectionArgs[0] == null || selectionArgs[0].equals("")) {
                suggestSelection = null;
                myArgs = null;
            } else {
                String like = selectionArgs[0] + "%";
                if (selectionArgs[0].startsWith("http")) {
                    myArgs = new String[1];
                    myArgs[0] = like;
                    suggestSelection = selection;
                } else {
                    SUGGEST_ARGS[0] = "http://" + like;
                    SUGGEST_ARGS[1] = "http://www." + like;
                    SUGGEST_ARGS[2] = "https://" + like;
                    SUGGEST_ARGS[3] = "https://www." + like;
                    myArgs = SUGGEST_ARGS;
                    suggestSelection = SUGGEST_SELECTION;
                }
            }

            Cursor c = db.query(TABLE_NAMES[URI_MATCH_BOOKMARKS],
                    SUGGEST_PROJECTION, suggestSelection, myArgs, null, null,
                    ORDER_BY,
                    (new Integer(MAX_SUGGESTION_LONG_ENTRIES)).toString());

            if (Regex.WEB_URL_PATTERN.matcher(selectionArgs[0]).matches()) {
                return new MySuggestionCursor(c, null, "");
            } else {
                // get Google suggest if there is still space in the list
                if (myArgs != null && myArgs.length > 1
                        && c.getCount() < (MAX_SUGGESTION_SHORT_ENTRIES - 1)) {
                    ISearchManager sm = ISearchManager.Stub
                            .asInterface(ServiceManager
                                    .getService(Context.SEARCH_SERVICE));
                    SearchableInfo si = null;
                    try {
                        // use the global search to get Google suggest provider
                        si = sm.getSearchableInfo(new ComponentName(
                                getContext(), "com.android.browser"), true);

                        // similar to the getSuggestions() in SearchDialog.java
                        StringBuilder uriStr = new StringBuilder("content://");
                        uriStr.append(si.getSuggestAuthority());
                        // if content path provided, insert it now
                        final String contentPath = si.getSuggestPath();
                        if (contentPath != null) {
                            uriStr.append('/");
                            uriStr.append(contentPath);
                        }
                        // append standard suggestion query path 
                        uriStr.append('/" + SearchManager.SUGGEST_URI_PATH_QUERY);
                        // inject query, either as selection args or inline
                        String[] selArgs = null;
                        if (si.getSuggestSelection() != null) {
                            selArgs = new String[] {selectionArgs[0]};
                        } else {
                            uriStr.append('/");
                            uriStr.append(Uri.encode(selectionArgs[0]));
                        }

                        // finally, make the query
                        Cursor sc = getContext().getContentResolver().query(
                                Uri.parse(uriStr.toString()), null,
                                si.getSuggestSelection(), selArgs, null);

                        return new MySuggestionCursor(c, sc, selectionArgs[0]);
                    } catch (RemoteException e) {
                    }
                }
                return new MySuggestionCursor(c, null, selectionArgs[0]);
            }
        }

        String[] projection = null;
        if (projectionIn != null && projectionIn.length > 0) {
            projection = new String[projectionIn.length + 1];
            System.arraycopy(projectionIn, 0, projection, 0, projectionIn.length);
            projection[projectionIn.length] = "_id AS _id";
        }

        StringBuilder whereClause = new StringBuilder(256);
        if (match == URI_MATCH_BOOKMARKS_ID || match == URI_MATCH_SEARCHES_ID) {
            whereClause.append("(_id = ").append(url.getPathSegments().get(1))
                    .append(")");
        }

        // Tack on the user's selection, if present
        if (selection != null && selection.length() > 0) {
            if (whereClause.length() > 0) {
                whereClause.append(" AND ");
            }

            whereClause.append('(");
            whereClause.append(selection);
            whereClause.append(')");
        }
        Cursor c = db.query(TABLE_NAMES[match % 10], projection,
                whereClause.toString(), selectionArgs, null, null, sortOrder,
                null);
        c.setNotificationUri(getContext().getContentResolver(), url);
        return c;
    
private static java.lang.CharSequencereplaceSystemPropertyInString(android.content.Context context, java.lang.CharSequence srcString)

        StringBuffer sb = new StringBuffer();
        int lastCharLoc = 0;
        
        final String client_id = Partner.getString(context.getContentResolver(), Partner.CLIENT_ID);

        for (int i = 0; i < srcString.length(); ++i) {
            char c = srcString.charAt(i);
            if (c == '{") {
                sb.append(srcString.subSequence(lastCharLoc, i));
                lastCharLoc = i;
          inner:
                for (int j = i; j < srcString.length(); ++j) {
                    char k = srcString.charAt(j);
                    if (k == '}") {
                        String propertyKeyValue = srcString.subSequence(i + 1, j).toString();
                        if (propertyKeyValue.equals("CLIENT_ID")) {
                            sb.append(client_id);
                        } else {
                            sb.append("unknown");
                        }
                        lastCharLoc = j + 1;
                        i = j;
                        break inner;
                    }
                }
            }
        }
        if (srcString.length() - lastCharLoc > 0) {
            // Put on the tail, if there is one
            sb.append(srcString.subSequence(lastCharLoc, srcString.length()));
        }
        return sb;
    
public intupdate(android.net.Uri url, android.content.ContentValues values, java.lang.String where, java.lang.String[] whereArgs)

        SQLiteDatabase db = mOpenHelper.getWritableDatabase();

        int match = URI_MATCHER.match(url);
        if (match == -1 || match == URI_MATCH_SUGGEST) {
            throw new IllegalArgumentException("Unknown URL");
        }

        if (match == URI_MATCH_BOOKMARKS_ID || match == URI_MATCH_SEARCHES_ID) {
            StringBuilder sb = new StringBuilder();
            if (where != null && where.length() > 0) {
                sb.append("( ");
                sb.append(where);
                sb.append(" ) AND ");
            }
            sb.append("_id = ");
            sb.append(url.getPathSegments().get(1));
            where = sb.toString();
        }

        int ret = db.update(TABLE_NAMES[match % 10], values, where, whereArgs);
        getContext().getContentResolver().notifyChange(url, null);
        return ret;