PageRenderTime 42ms CodeModel.GetById 1ms app.highlight 34ms RepoModel.GetById 2ms app.codeStats 0ms

/WebVox/src/com/marvin/webvox/Bookmarks.java

http://eyes-free.googlecode.com/
Java | 219 lines | 141 code | 10 blank | 68 comment | 25 complexity | 1f91aed97fa41dafa0728264899368e0 MD5 | raw file
  1/*
  2 * Copyright (C) 2009 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.marvin.webvox;
 18
 19import com.marvin.webvox.R;
 20
 21import android.content.ContentResolver;
 22import android.content.ContentUris;
 23import android.content.ContentValues;
 24import android.content.Context;
 25import android.database.Cursor;
 26import android.graphics.Bitmap;
 27import android.net.Uri;
 28import android.provider.Browser;
 29import android.util.Log;
 30import android.webkit.WebIconDatabase;
 31import android.widget.Toast;
 32
 33import java.io.ByteArrayOutputStream;
 34import java.util.Date;
 35
 36/**
 37 *  This class is purely to have a common place for adding/deleting bookmarks.
 38 */
 39/* package */ class Bookmarks {
 40    private static final String     WHERE_CLAUSE
 41            = "url = ? OR url = ? OR url = ? OR url = ?";
 42    private static final String     WHERE_CLAUSE_SECURE = "url = ? OR url = ?";
 43
 44    private static String[]         SELECTION_ARGS;
 45
 46    /**
 47     *  Add a bookmark to the database.
 48     *  @param context Context of the calling Activity.  This is used to make
 49     *          Toast confirming that the bookmark has been added.  If the
 50     *          caller provides null, the Toast will not be shown.
 51     *  @param cr The ContentResolver being used to add the bookmark to the db.
 52     *  @param url URL of the website to be bookmarked.
 53     *  @param name Provided name for the bookmark.
 54     *  @param thumbnail A thumbnail for the bookmark.
 55     *  @param retainIcon Whether to retain the page's icon in the icon database.
 56     *          This will usually be <code>true</code> except when bookmarks are
 57     *          added by a settings restore agent.
 58     */
 59    /* package */ static void addBookmark(Context context,
 60            ContentResolver cr, String url, String name,
 61            Bitmap thumbnail, boolean retainIcon) {
 62        // Want to append to the beginning of the list
 63        long creationTime = new Date().getTime();
 64        // First we check to see if the user has already visited this
 65        // site.  They may have bookmarked it in a different way from
 66        // how it's stored in the database, so allow different combos
 67        // to map to the same url.
 68        boolean secure = false;
 69        String compareString = url;
 70        if (compareString.startsWith("http://")) {
 71            compareString = compareString.substring(7);
 72        } else if (compareString.startsWith("https://")) {
 73            compareString = compareString.substring(8);
 74            secure = true;
 75        }
 76        if (compareString.startsWith("www.")) {
 77            compareString = compareString.substring(4);
 78        }
 79        if (secure) {
 80            SELECTION_ARGS = new String[2];
 81            SELECTION_ARGS[0] = "https://" + compareString;
 82            SELECTION_ARGS[1] = "https://www." + compareString;
 83        } else {
 84            SELECTION_ARGS = new String[4];
 85            SELECTION_ARGS[0] = compareString;
 86            SELECTION_ARGS[1] = "www." + compareString;
 87            SELECTION_ARGS[2] = "http://" + compareString;
 88            SELECTION_ARGS[3] = "http://" + SELECTION_ARGS[1];
 89        }
 90        Cursor cursor = cr.query(Browser.BOOKMARKS_URI,
 91                Browser.HISTORY_PROJECTION,
 92                secure ? WHERE_CLAUSE_SECURE : WHERE_CLAUSE,
 93                SELECTION_ARGS,
 94                null);
 95        ContentValues map = new ContentValues();
 96        if (cursor.moveToFirst() && cursor.getInt(
 97                Browser.HISTORY_PROJECTION_BOOKMARK_INDEX) == 0) {
 98            // This means we have been to this site but not bookmarked
 99            // it, so convert the history item to a bookmark
100            map.put(Browser.BookmarkColumns.CREATED, creationTime);
101            map.put(Browser.BookmarkColumns.TITLE, name);
102            map.put(Browser.BookmarkColumns.BOOKMARK, 1);            
103//            map.put(Browser.BookmarkColumns.THUMBNAIL, bitmapToBytes(thumbnail));
104            cr.update(Browser.BOOKMARKS_URI, map,
105                    "_id = " + cursor.getInt(0), null);
106        } else {
107            int count = cursor.getCount();
108            boolean matchedTitle = false;
109            for (int i = 0; i < count; i++) {
110                // One or more bookmarks already exist for this site.
111                // Check the names of each
112                cursor.moveToPosition(i);
113                if (cursor.getString(Browser.HISTORY_PROJECTION_TITLE_INDEX)
114                        .equals(name)) {
115                    // The old bookmark has the same name.
116                    // Update its creation time.
117                    map.put(Browser.BookmarkColumns.CREATED,
118                            creationTime);
119                    cr.update(Browser.BOOKMARKS_URI, map,
120                            "_id = " + cursor.getInt(0), null);
121                    matchedTitle = true;
122                    break;
123                }
124            }
125            if (!matchedTitle) {
126                // Adding a bookmark for a site the user has visited,
127                // or a new bookmark (with a different name) for a site
128                // the user has visited
129                map.put(Browser.BookmarkColumns.TITLE, name);
130                map.put(Browser.BookmarkColumns.URL, url);
131                map.put(Browser.BookmarkColumns.CREATED, creationTime);
132                map.put(Browser.BookmarkColumns.BOOKMARK, 1);
133                map.put(Browser.BookmarkColumns.DATE, 0);
134//                map.put(Browser.BookmarkColumns.THUMBNAIL, bitmapToBytes(thumbnail));
135                int visits = 0;
136                if (count > 0) {
137                    // The user has already bookmarked, and possibly
138                    // visited this site.  However, they are creating
139                    // a new bookmark with the same url but a different
140                    // name.  The new bookmark should have the same
141                    // number of visits as the already created bookmark.
142                    visits = cursor.getInt(
143                            Browser.HISTORY_PROJECTION_VISITS_INDEX);
144                }
145                // Bookmark starts with 3 extra visits so that it will
146                // bubble up in the most visited and goto search box
147                map.put(Browser.BookmarkColumns.VISITS, visits + 3);
148                cr.insert(Browser.BOOKMARKS_URI, map);
149            }
150        }
151        if (retainIcon) {
152            WebIconDatabase.getInstance().retainIconForPageUrl(url);
153        }
154        cursor.deactivate();
155        if (context != null) {
156            Toast.makeText(context, R.string.added_to_bookmarks,
157                    Toast.LENGTH_LONG).show();
158        }
159    }
160
161    /**
162     *  Remove a bookmark from the database.  If the url is a visited site, it
163     *  will remain in the database, but only as a history item, and not as a
164     *  bookmarked site.
165     *  @param context Context of the calling Activity.  This is used to make
166     *          Toast confirming that the bookmark has been removed.  If the
167     *          caller provides null, the Toast will not be shown.
168     *  @param cr The ContentResolver being used to remove the bookmark.
169     *  @param url URL of the website to be removed.
170     */
171    /* package */ static void removeFromBookmarks(Context context,
172            ContentResolver cr, String url, String title) {
173        Cursor cursor = cr.query(
174                Browser.BOOKMARKS_URI,
175                Browser.HISTORY_PROJECTION,
176                "url = ? AND title = ?",
177                new String[] { url, title },
178                null);
179        boolean first = cursor.moveToFirst();
180        // Should be in the database no matter what
181        if (!first) {
182            throw new AssertionError("URL is not in the database! " + url + " " + title);
183        }
184        // Remove from bookmarks
185        WebIconDatabase.getInstance().releaseIconForPageUrl(url);
186        Uri uri = ContentUris.withAppendedId(Browser.BOOKMARKS_URI,
187                cursor.getInt(Browser.HISTORY_PROJECTION_ID_INDEX));
188        int numVisits = cursor.getInt(
189                Browser.HISTORY_PROJECTION_VISITS_INDEX);
190        if (0 == numVisits) {
191            cr.delete(uri, null, null);
192        } else {
193            // It is no longer a bookmark, but it is still a visited
194            // site.
195            ContentValues values = new ContentValues();
196            values.put(Browser.BookmarkColumns.BOOKMARK, 0);
197            try {
198                cr.update(uri, values, null, null);
199            } catch (IllegalStateException e) {
200                Log.e("removeFromBookmarks", "no database!");
201            }
202        }
203        if (context != null) {
204            Toast.makeText(context, R.string.removed_from_bookmarks,
205                    Toast.LENGTH_LONG).show();
206        }
207        cursor.deactivate();
208    }
209
210    private static byte[] bitmapToBytes(Bitmap bm) {
211        if (bm == null) {
212            return null;
213        }
214
215        final ByteArrayOutputStream os = new ByteArrayOutputStream();
216        bm.compress(Bitmap.CompressFormat.PNG, 100, os);
217        return os.toByteArray();
218    }
219}