Bug 704490 - Abstract all bookmark/history access behing a common API (r=blassey)

This commit is contained in:
Lucas Rocha 2011-12-02 17:27:56 +00:00
parent f76f7d6da3
commit 8164a18e67
10 changed files with 503 additions and 201 deletions

View File

@ -63,6 +63,9 @@ import android.os.Handler;
import org.json.*;
import android.util.AttributeSet;
import org.mozilla.gecko.db.BrowserDB;
import org.mozilla.gecko.db.BrowserDB.URLColumns;
public class AboutHomeContent extends LinearLayout {
public interface UriLoadCallback {
public void callback(String uriSpec);
@ -83,7 +86,7 @@ public class AboutHomeContent extends LinearLayout {
private static final String LOGTAG = "GeckoAboutHome";
private static final String TITLE_KEY = "title";
private static final String kAbouthomeWhereClause = Browser.BookmarkColumns.BOOKMARK + " = 1";
private static final int NUMBER_OF_TOP_SITES = 3;
private static final int kTileWidth = 122;
private Cursor mCursor;
@ -126,8 +129,8 @@ public class AboutHomeContent extends LinearLayout {
void init(final Activity activity) {
GeckoAppShell.getHandler().post(new Runnable() {
public void run() {
mCursor = activity.managedQuery(Browser.BOOKMARKS_URI,
null, kAbouthomeWhereClause, null, null);
ContentResolver resolver = GeckoApp.mAppContext.getContentResolver();
mCursor = BrowserDB.filter(resolver, "", NUMBER_OF_TOP_SITES);
activity.startManagingCursor(mCursor);
onActivityContentChanged(activity);
@ -136,10 +139,10 @@ public class AboutHomeContent extends LinearLayout {
public void run() {
final SimpleCursorAdapter gridAdapter =
new SimpleCursorAdapter(activity, R.layout.abouthome_grid_box, mCursor,
new String[] {Browser.BookmarkColumns.TITLE,
Browser.BookmarkColumns.FAVICON,
Browser.BookmarkColumns.URL,
"thumbnail"},
new String[] { URLColumns.TITLE,
URLColumns.FAVICON,
URLColumns.URL,
URLColumns.THUMBNAIL },
new int[] {R.id.bookmark_title, R.id.bookmark_icon, R.id.bookmark_url, R.id.screenshot});
mGrid.setAdapter(gridAdapter);
gridAdapter.setViewBinder(new AwesomeCursorViewBinder());
@ -210,7 +213,7 @@ public class AboutHomeContent extends LinearLayout {
protected void onGridItemClick(GridView l, View v, int position, long id) {
mCursor.moveToPosition(position);
String spec = mCursor.getString(mCursor.getColumnIndex(Browser.BookmarkColumns.URL));
String spec = mCursor.getString(mCursor.getColumnIndex(URLColumns.URL));
Log.i(LOGTAG, "clicked: " + spec);
if (mUriLoadCallback != null)
mUriLoadCallback.callback(spec);
@ -238,7 +241,7 @@ class AwesomeCursorViewBinder implements SimpleCursorAdapter.ViewBinder {
// Use the URL instead of an empty title for consistency with the normal URL
// bar view - this is the equivalent of getDisplayTitle() in Tab.java
if (title == null || title.length() == 0) {
int urlIndex = cursor.getColumnIndexOrThrow(Browser.BookmarkColumns.URL);
int urlIndex = cursor.getColumnIndexOrThrow(URLColumns.URL);
title = cursor.getString(urlIndex);
}
@ -264,23 +267,22 @@ class AwesomeCursorViewBinder implements SimpleCursorAdapter.ViewBinder {
@Override
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
int faviconIndex = cursor.getColumnIndexOrThrow(Browser.BookmarkColumns.FAVICON);
int faviconIndex = cursor.getColumnIndexOrThrow(URLColumns.FAVICON);
if (columnIndex == faviconIndex) {
return updateImage(view, cursor, faviconIndex);
}
int titleIndex = cursor.getColumnIndexOrThrow(Browser.BookmarkColumns.TITLE);
int titleIndex = cursor.getColumnIndexOrThrow(URLColumns.TITLE);
if (columnIndex == titleIndex) {
return updateTitle(view, cursor, titleIndex);
}
int urlIndex = cursor.getColumnIndexOrThrow(Browser.BookmarkColumns.URL);
int urlIndex = cursor.getColumnIndexOrThrow(URLColumns.URL);
if (columnIndex == urlIndex) {
return updateUrl(view, cursor, urlIndex);
}
int thumbIndex = cursor.getColumnIndexOrThrow("thumbnail");
int thumbIndex = cursor.getColumnIndexOrThrow(URLColumns.THUMBNAIL);
if (columnIndex == thumbIndex) {
return updateImage(view, cursor, thumbIndex);
}

View File

@ -63,7 +63,6 @@ public class AwesomeBar extends Activity implements GeckoEventListener {
private static final String LOGTAG = "GeckoAwesomeBar";
static final String URL_KEY = "url";
static final String TITLE_KEY = "title";
static final String CURRENT_URL_KEY = "currenturl";
static final String TYPE_KEY = "type";
static final String SEARCH_KEY = "search";

View File

@ -79,6 +79,9 @@ import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.mozilla.gecko.db.BrowserDB;
import org.mozilla.gecko.db.BrowserDB.URLColumns;
public class AwesomeBarTabs extends TabHost {
private static final String LOGTAG = "GeckoAwesomeBarTabs";
@ -127,7 +130,7 @@ public class AwesomeBarTabs extends TabHost {
Map<String,Object> historyItem =
(Map<String,Object>) mHistoryAdapter.getChild(groupPosition, childPosition);
byte[] b = (byte[]) historyItem.get(Browser.BookmarkColumns.FAVICON);
byte[] b = (byte[]) historyItem.get(URLColumns.FAVICON);
ImageView favicon = (ImageView) childView.findViewById(R.id.favicon);
if (b == null) {
@ -162,7 +165,7 @@ public class AwesomeBarTabs extends TabHost {
// Use the URL instead of an empty title for consistency with the normal URL
// bar view - this is the equivalent of getDisplayTitle() in Tab.java
if (title == null || title.length() == 0) {
int urlIndex = cursor.getColumnIndexOrThrow(Browser.BookmarkColumns.URL);
int urlIndex = cursor.getColumnIndexOrThrow(URLColumns.URL);
title = cursor.getString(urlIndex);
}
@ -171,12 +174,12 @@ public class AwesomeBarTabs extends TabHost {
}
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
int faviconIndex = cursor.getColumnIndexOrThrow(Browser.BookmarkColumns.FAVICON);
int faviconIndex = cursor.getColumnIndexOrThrow(URLColumns.FAVICON);
if (columnIndex == faviconIndex) {
return updateFavicon(view, cursor, faviconIndex);
}
int titleIndex = cursor.getColumnIndexOrThrow(Browser.BookmarkColumns.TITLE);
int titleIndex = cursor.getColumnIndexOrThrow(URLColumns.TITLE);
if (columnIndex == titleIndex) {
return updateTitle(view, cursor, titleIndex);
}
@ -189,16 +192,7 @@ public class AwesomeBarTabs extends TabHost {
private class BookmarksQueryTask extends AsyncTask<Void, Void, Cursor> {
protected Cursor doInBackground(Void... arg0) {
ContentResolver resolver = mContext.getContentResolver();
return resolver.query(Browser.BOOKMARKS_URI,
null,
// Select all bookmarks with a non-empty URL. When the history
// is empty there appears to be a dummy entry in the database
// which has a title of "Bookmarks" and no URL; the length restriction
// is to avoid picking that up specifically.
Browser.BookmarkColumns.BOOKMARK + " = 1 AND LENGTH(" + Browser.BookmarkColumns.URL + ") > 0",
null,
Browser.BookmarkColumns.TITLE);
return BrowserDB.getAllBookmarks(resolver);
}
protected void onPostExecute(Cursor cursor) {
@ -207,9 +201,9 @@ public class AwesomeBarTabs extends TabHost {
mContext,
R.layout.awesomebar_row,
cursor,
new String[] { AwesomeBar.TITLE_KEY,
AwesomeBar.URL_KEY,
Browser.BookmarkColumns.FAVICON },
new String[] { URLColumns.TITLE,
URLColumns.URL,
URLColumns.FAVICON },
new int[] { R.id.title, R.id.url, R.id.favicon }
);
@ -234,15 +228,7 @@ public class AwesomeBarTabs extends TabHost {
protected Pair<List,List> doInBackground(Void... arg0) {
Pair<List,List> result = null;
ContentResolver resolver = mContext.getContentResolver();
Cursor cursor =
resolver.query(Browser.BOOKMARKS_URI,
null,
// Bookmarks that have not been visited have a date value
// of 0, so don't pick them up in the history view.
Browser.BookmarkColumns.DATE + " > 0",
null,
Browser.BookmarkColumns.DATE + " DESC LIMIT " + MAX_RESULTS);
Cursor cursor = BrowserDB.getRecentHistory(resolver, MAX_RESULTS);
Date now = new Date();
now.setHours(0);
@ -267,7 +253,7 @@ public class AwesomeBarTabs extends TabHost {
// Browser content provider don't support limitting the number
// of returned rows so we limit it here.
while (cursor.moveToNext()) {
long time = cursor.getLong(cursor.getColumnIndexOrThrow(Browser.BookmarkColumns.DATE));
long time = cursor.getLong(cursor.getColumnIndexOrThrow(URLColumns.DATE_LAST_VISITED));
HistorySection itemSection = getSectionForTime(time, today);
if (groups == null)
@ -309,20 +295,20 @@ public class AwesomeBarTabs extends TabHost {
public Map<String,?> createHistoryItem(Cursor cursor) {
Map<String,Object> historyItem = new HashMap<String,Object>();
String url = cursor.getString(cursor.getColumnIndexOrThrow(AwesomeBar.URL_KEY));
String title = cursor.getString(cursor.getColumnIndexOrThrow(AwesomeBar.TITLE_KEY));
byte[] favicon = cursor.getBlob(cursor.getColumnIndexOrThrow(Browser.BookmarkColumns.FAVICON));
String url = cursor.getString(cursor.getColumnIndexOrThrow(URLColumns.URL));
String title = cursor.getString(cursor.getColumnIndexOrThrow(URLColumns.TITLE));
byte[] favicon = cursor.getBlob(cursor.getColumnIndexOrThrow(URLColumns.FAVICON));
// Use the URL instead of an empty title for consistency with the normal URL
// bar view - this is the equivalent of getDisplayTitle() in Tab.java
if (title == null || title.length() == 0)
title = url;
historyItem.put(AwesomeBar.URL_KEY, url);
historyItem.put(AwesomeBar.TITLE_KEY, title);
historyItem.put(URLColumns.URL, url);
historyItem.put(URLColumns.TITLE, title);
if (favicon != null)
historyItem.put(Browser.BookmarkColumns.FAVICON, favicon);
historyItem.put(URLColumns.FAVICON, favicon);
return historyItem;
}
@ -330,7 +316,7 @@ public class AwesomeBarTabs extends TabHost {
public Map<String,?> createGroupItem(HistorySection section) {
Map<String,String> groupItem = new HashMap<String,String>();
groupItem.put(AwesomeBar.TITLE_KEY, getSectionName(section));
groupItem.put(URLColumns.TITLE, getSectionName(section));
return groupItem;
}
@ -384,11 +370,11 @@ public class AwesomeBarTabs extends TabHost {
mContext,
result.first,
R.layout.awesomebar_header_row,
new String[] { AwesomeBar.TITLE_KEY },
new String[] { URLColumns.TITLE },
new int[] { R.id.title },
result.second,
R.layout.awesomebar_row,
new String[] { AwesomeBar.TITLE_KEY, AwesomeBar.URL_KEY },
new String[] { URLColumns.TITLE, URLColumns.URL },
new int[] { R.id.title, R.id.url }
);
@ -611,9 +597,9 @@ public class AwesomeBarTabs extends TabHost {
mContext,
R.layout.awesomebar_row,
null,
new String[] { AwesomeBar.TITLE_KEY,
AwesomeBar.URL_KEY,
Browser.BookmarkColumns.FAVICON },
new String[] { URLColumns.TITLE,
URLColumns.URL,
URLColumns.FAVICON },
new int[] { R.id.title, R.id.url, R.id.favicon }
);
@ -622,19 +608,7 @@ public class AwesomeBarTabs extends TabHost {
mAllPagesCursorAdapter.setFilterQueryProvider(new FilterQueryProvider() {
public Cursor runQuery(CharSequence constraint) {
ContentResolver resolver = mContext.getContentResolver();
return resolver.query(Browser.BOOKMARKS_URI,
null,
// The length restriction on URL is for the same reason as in the general bookmark query
// (see comment earlier in this file).
"(" + Browser.BookmarkColumns.URL + " LIKE ? OR " + Browser.BookmarkColumns.TITLE + " LIKE ?)"
+ " AND LENGTH(" + Browser.BookmarkColumns.URL + ") > 0",
new String[] {"%" + constraint.toString() + "%", "%" + constraint.toString() + "%",},
// ORDER BY is number of visits times a multiplier from 1 - 120 of how recently the site
// was accessed with a site accessed today getting 120 and a site accessed 119 or more
// days ago getting 1
Browser.BookmarkColumns.VISITS + " * MAX(1, (" +
Browser.BookmarkColumns.DATE + " - " + new Date().getTime() + ") / 86400000 + 120) DESC LIMIT " + MAX_RESULTS);
return BrowserDB.filter(resolver, constraint, MAX_RESULTS);
}
});
@ -690,7 +664,7 @@ public class AwesomeBarTabs extends TabHost {
Map<String,Object> historyItem =
(Map<String,Object>) mHistoryAdapter.getChild(groupPosition, childPosition);
String url = (String) historyItem.get(AwesomeBar.URL_KEY);
String url = (String) historyItem.get(URLColumns.URL);
if (mUrlOpenListener != null)
mUrlOpenListener.onUrlOpen(url);
@ -703,7 +677,7 @@ public class AwesomeBarTabs extends TabHost {
// item will be a String containing the name of the search engine.
if (item instanceof Cursor) {
Cursor cursor = (Cursor) item;
String url = cursor.getString(cursor.getColumnIndexOrThrow(AwesomeBar.URL_KEY));
String url = cursor.getString(cursor.getColumnIndexOrThrow(URLColumns.URL));
if (mUrlOpenListener != null)
mUrlOpenListener.onUrlOpen(url);
} else {

View File

@ -37,9 +37,10 @@
package org.mozilla.gecko;
import org.mozilla.gecko.db.BrowserDB;
import android.content.Context;
import android.preference.DialogPreference;
import android.provider.Browser;
import android.util.AttributeSet;
import android.util.Log;
@ -64,7 +65,7 @@ class ConfirmPreference extends DialogPreference {
if ("clear_history".equalsIgnoreCase(mAction)) {
GeckoAppShell.getHandler().post(new Runnable(){
public void run() {
Browser.clearHistory(mContext.getContentResolver());
BrowserDB.clearHistory(mContext.getContentResolver());
}
});
} else if ("clear_private_data".equalsIgnoreCase(mAction)) {

View File

@ -44,19 +44,14 @@ import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteQueryBuilder;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.provider.Browser;
import android.util.Log;
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLConnection;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
@ -66,6 +61,8 @@ import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import org.mozilla.gecko.db.BrowserDB;
public class Favicons {
private static final String LOGTAG = "GeckoFavicons";
@ -236,51 +233,19 @@ public class Favicons {
Log.d(LOGTAG, "Loading favicon from DB for URL = " + mPageUrl);
ContentResolver resolver = mContext.getContentResolver();
BitmapDrawable favicon = BrowserDB.getFaviconForUrl(resolver, mPageUrl);
Cursor c = resolver.query(Browser.BOOKMARKS_URI,
new String[] { Browser.BookmarkColumns.FAVICON },
Browser.BookmarkColumns.URL + " = ?",
new String[] { mPageUrl },
null);
if (favicon != null)
Log.d(LOGTAG, "Loaded favicon from DB successfully for URL = " + mPageUrl);
if (!c.moveToFirst()) {
c.close();
return null;
}
int faviconIndex = c.getColumnIndexOrThrow(Browser.BookmarkColumns.FAVICON);
byte[] b = c.getBlob(faviconIndex);
c.close();
if (b == null)
return null;
Bitmap bitmap = BitmapFactory.decodeByteArray(b, 0, b.length);
Log.d(LOGTAG, "Loaded favicon from DB successfully for URL = " + mPageUrl);
return new BitmapDrawable(bitmap);
return favicon;
}
// Runs in background thread
private void saveFaviconToDb(BitmapDrawable favicon) {
Bitmap bitmap = favicon.getBitmap();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
ContentValues values = new ContentValues();
values.put(Browser.BookmarkColumns.FAVICON, stream.toByteArray());
values.put(Browser.BookmarkColumns.URL, mPageUrl);
ContentResolver resolver = mContext.getContentResolver();
Log.d(LOGTAG, "Saving favicon on browser database for URL = " + mPageUrl);
resolver.update(Browser.BOOKMARKS_URI,
values,
Browser.BookmarkColumns.URL + " = ?",
new String[] { mPageUrl });
ContentResolver resolver = mContext.getContentResolver();
BrowserDB.updateFaviconForUrl(resolver, mPageUrl, favicon);
Log.d(LOGTAG, "Saving favicon URL for URL = " + mPageUrl);
mDbHelper.setFaviconUrlForPageUrl(mPageUrl, mFaviconUrl);

View File

@ -43,12 +43,13 @@ import java.util.Queue;
import java.util.Set;
import java.lang.ref.SoftReference;
import android.content.ContentValues;
import android.content.ContentResolver;
import android.database.Cursor;
import android.os.Handler;
import android.provider.Browser;
import android.util.Log;
import org.mozilla.gecko.db.BrowserDB;
class GlobalHistory {
private static final String LOGTAG = "GeckoGlobalHistory";
@ -80,12 +81,7 @@ class GlobalHistory {
// the cache was wiped away, repopulate it
Log.w(LOGTAG, "Rebuilding visited link set...");
visitedSet = new HashSet<String>();
Cursor c = GeckoApp.mAppContext.getContentResolver().query(Browser.BOOKMARKS_URI,
new String[] { Browser.BookmarkColumns.URL },
Browser.BookmarkColumns.BOOKMARK + "=0 AND " +
Browser.BookmarkColumns.VISITS + ">0",
null,
null);
Cursor c = BrowserDB.getAllVisitedHistory(GeckoApp.mAppContext.getContentResolver());
if (c.moveToFirst()) {
do {
visitedSet.add(c.getString(0));
@ -112,7 +108,7 @@ class GlobalHistory {
}
public void add(String uri) {
Browser.updateVisitedHistory(GeckoApp.mAppContext.getContentResolver(), uri, true);
BrowserDB.updateVisitedHistory(GeckoApp.mAppContext.getContentResolver(), uri);
Set<String> visitedSet = mVisitedCache.get();
if (visitedSet != null) {
visitedSet.add(uri);
@ -121,13 +117,8 @@ class GlobalHistory {
}
public void update(String uri, String title) {
ContentValues values = new ContentValues();
values.put(Browser.BookmarkColumns.TITLE, title);
GeckoApp.mAppContext.getContentResolver().update(
Browser.BOOKMARKS_URI,
values,
Browser.BookmarkColumns.URL + " = ?",
new String[] { uri });
ContentResolver resolver = GeckoApp.mAppContext.getContentResolver();
BrowserDB.updateHistoryTitle(resolver, uri, title);
}
public void checkUriVisited(final String uri) {

View File

@ -54,6 +54,8 @@ JAVAFILES = \
AwesomeBarTabs.java \
BrowserToolbar.java \
ConfirmPreference.java \
db/AndroidBrowserDB.java \
db/BrowserDB.java \
DoorHanger.java \
DoorHangerPopup.java \
Favicons.java \

View File

@ -38,12 +38,9 @@
package org.mozilla.gecko;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.Bitmap;
import android.provider.Browser;
import android.util.DisplayMetrics;
import android.util.Log;
import android.graphics.Bitmap;
@ -56,6 +53,8 @@ import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.mozilla.gecko.db.BrowserDB;
public class Tab {
public static enum AgentMode { MOBILE, DESKTOP };
private static final String LOGTAG = "GeckoTab";
@ -144,7 +143,7 @@ public class Tab {
if (b != null) {
Bitmap bitmap = Bitmap.createBitmap(b, 0, 0, sMinDim, sMinDim);
mThumbnail = new BitmapDrawable(bitmap);
saveThumbnailToDB(bitmap);
saveThumbnailToDB((BitmapDrawable) mThumbnail);
} else {
mThumbnail = null;
}
@ -355,17 +354,7 @@ public class Tab {
@Override
protected Boolean doInBackground(Void... unused) {
ContentResolver resolver = Tabs.getInstance().getContentResolver();
Cursor cursor = resolver.query(Browser.BOOKMARKS_URI,
null,
Browser.BookmarkColumns.URL + " = ? and " + Browser.BookmarkColumns.BOOKMARK + " = ?",
new String[] { getURL(), "1" },
Browser.BookmarkColumns.URL);
int count = cursor.getCount();
cursor.close();
if (count == 1)
return true;
else
return false;
return BrowserDB.isBookmark(resolver, getURL());
}
@Override
@ -378,31 +367,7 @@ public class Tab {
@Override
protected Void doInBackground(Void... unused) {
ContentResolver resolver = Tabs.getInstance().getContentResolver();
Cursor cursor = resolver.query(Browser.BOOKMARKS_URI,
null,
Browser.BookmarkColumns.URL + " = ?",
new String[] { getURL() },
Browser.BookmarkColumns.URL);
ContentValues values = new ContentValues();
values.put(Browser.BookmarkColumns.BOOKMARK, "1");
values.put(Browser.BookmarkColumns.TITLE, getTitle());
if (cursor.getCount() == 1) {
//entry exists, update the bookmark flag
resolver.update(Browser.BOOKMARKS_URI,
values,
Browser.BookmarkColumns.URL + " = ?",
new String[] { getURL() });
} else {
//add a new entry
values.put(Browser.BookmarkColumns.URL, mUrl);
resolver.insert(Browser.BOOKMARKS_URI,
values);
}
cursor.close();
BrowserDB.addBookmark(resolver, getTitle(), getURL());
return null;
}
@ -412,33 +377,10 @@ public class Tab {
}
}
private void saveThumbnailToDB(Bitmap bitmap) {
private void saveThumbnailToDB(BitmapDrawable thumbnail) {
try {
ContentResolver resolver = Tabs.getInstance().getContentResolver();
Cursor cursor = resolver.query(Browser.BOOKMARKS_URI,
null,
Browser.BookmarkColumns.URL + " = ?",
new String[] { getURL() },
Browser.BookmarkColumns.URL);
ContentValues values = new ContentValues();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 0, bos);
values.put("thumbnail", bos.toByteArray());
if (cursor.getCount() == 1) {
//entry exists, update the bookmark flag
resolver.update(Browser.BOOKMARKS_URI,
values,
Browser.BookmarkColumns.URL + " = ?",
new String[] { getURL() });
} else {
//add a new entry
values.put(Browser.BookmarkColumns.URL, mUrl);
resolver.insert(Browser.BOOKMARKS_URI,
values);
}
cursor.close();
BrowserDB.updateThumbnailForUrl(resolver, getURL(), thumbnail);
} catch (Exception e) {
// ignore
}
@ -448,12 +390,7 @@ public class Tab {
@Override
protected Void doInBackground(Void... unused) {
ContentResolver resolver = Tabs.getInstance().getContentResolver();
ContentValues values = new ContentValues();
values.put(Browser.BookmarkColumns.BOOKMARK, "0");
resolver.update(Browser.BOOKMARKS_URI,
values,
Browser.BookmarkColumns.URL + " = ?",
new String[] { getURL() });
BrowserDB.removeBookmark(resolver, getURL());
return null;
}

View File

@ -0,0 +1,291 @@
/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is Mozilla Android code.
*
* The Initial Developer of the Original Code is Mozilla Foundation.
* Portions created by the Initial Developer are Copyright (C) 2011
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Lucas Rocha <lucasr@mozilla.com>
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
package org.mozilla.gecko.db;
import java.io.ByteArrayOutputStream;
import java.util.Date;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.CursorWrapper;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.provider.Browser;
import android.provider.Browser.BookmarkColumns;
public class AndroidBrowserDB implements BrowserDB.BrowserDBIface {
private static final String URL_COLUMN_ID = "_id";
private static final String URL_COLUMN_THUMBNAIL = "thumbnail";
public Cursor filter(ContentResolver cr, CharSequence constraint, int limit) {
Cursor c = cr.query(Browser.BOOKMARKS_URI,
new String[] { URL_COLUMN_ID,
BookmarkColumns.URL,
BookmarkColumns.TITLE,
BookmarkColumns.FAVICON,
URL_COLUMN_THUMBNAIL },
// The length restriction on URL is for the same reason as in the general bookmark query
// (see comment earlier in this file).
"(" + Browser.BookmarkColumns.URL + " LIKE ? OR " + Browser.BookmarkColumns.TITLE + " LIKE ?)"
+ " AND LENGTH(" + Browser.BookmarkColumns.URL + ") > 0",
new String[] {"%" + constraint.toString() + "%", "%" + constraint.toString() + "%",},
// ORDER BY is number of visits times a multiplier from 1 - 120 of how recently the site
// was accessed with a site accessed today getting 120 and a site accessed 119 or more
// days ago getting 1
Browser.BookmarkColumns.VISITS + " * MAX(1, (" +
Browser.BookmarkColumns.DATE + " - " + new Date().getTime() + ") / 86400000 + 120) DESC LIMIT " + limit);
return new AndroidDBCursor(c);
}
public void updateVisitedHistory(ContentResolver cr, String uri) {
Browser.updateVisitedHistory(cr, uri, true);
}
public void updateHistoryTitle(ContentResolver cr, String uri, String title) {
ContentValues values = new ContentValues();
values.put(Browser.BookmarkColumns.TITLE, title);
cr.update(Browser.BOOKMARKS_URI,
values,
Browser.BookmarkColumns.URL + " = ?",
new String[] { uri });
}
public Cursor getAllVisitedHistory(ContentResolver cr) {
Cursor c = cr.query(Browser.BOOKMARKS_URI,
new String[] { Browser.BookmarkColumns.URL },
Browser.BookmarkColumns.BOOKMARK + " = 0 AND " +
Browser.BookmarkColumns.VISITS + " > 0",
null,
null);
return new AndroidDBCursor(c);
}
public Cursor getRecentHistory(ContentResolver cr, int limit) {
Cursor c = cr.query(Browser.BOOKMARKS_URI,
new String[] { URL_COLUMN_ID,
BookmarkColumns.URL,
BookmarkColumns.TITLE,
BookmarkColumns.FAVICON,
BookmarkColumns.DATE },
// Bookmarks that have not been visited have a date value
// of 0, so don't pick them up in the history view.
Browser.BookmarkColumns.DATE + " > 0",
null,
Browser.BookmarkColumns.DATE + " DESC LIMIT " + limit);
return new AndroidDBCursor(c);
}
public void clearHistory(ContentResolver cr) {
Browser.clearHistory(cr);
}
public Cursor getAllBookmarks(ContentResolver cr) {
Cursor c = cr.query(Browser.BOOKMARKS_URI,
new String[] { URL_COLUMN_ID,
BookmarkColumns.URL,
BookmarkColumns.TITLE,
BookmarkColumns.FAVICON },
// Select all bookmarks with a non-empty URL. When the history
// is empty there appears to be a dummy entry in the database
// which has a title of "Bookmarks" and no URL; the length restriction
// is to avoid picking that up specifically.
Browser.BookmarkColumns.BOOKMARK + " = 1 AND LENGTH(" + Browser.BookmarkColumns.URL + ") > 0",
null,
Browser.BookmarkColumns.TITLE);
return new AndroidDBCursor(c);
}
public boolean isBookmark(ContentResolver cr, String uri) {
Cursor cursor = cr.query(Browser.BOOKMARKS_URI,
new String[] { BookmarkColumns.URL },
Browser.BookmarkColumns.URL + " = ? and " + Browser.BookmarkColumns.BOOKMARK + " = ?",
new String[] { uri, "1" },
Browser.BookmarkColumns.URL);
int count = cursor.getCount();
cursor.close();
return (count == 1);
}
public void addBookmark(ContentResolver cr, String title, String uri) {
Cursor cursor = cr.query(Browser.BOOKMARKS_URI,
new String[] { BookmarkColumns.URL },
Browser.BookmarkColumns.URL + " = ?",
new String[] { uri },
Browser.BookmarkColumns.URL);
ContentValues values = new ContentValues();
values.put(Browser.BookmarkColumns.BOOKMARK, "1");
values.put(Browser.BookmarkColumns.TITLE, title);
if (cursor.getCount() == 1) {
// entry exists, update the bookmark flag
cr.update(Browser.BOOKMARKS_URI,
values,
Browser.BookmarkColumns.URL + " = ?",
new String[] { uri });
} else {
// add a new entry
values.put(Browser.BookmarkColumns.URL, uri);
cr.insert(Browser.BOOKMARKS_URI, values);
}
cursor.close();
}
public void removeBookmark(ContentResolver cr, String uri) {
ContentValues values = new ContentValues();
values.put(Browser.BookmarkColumns.BOOKMARK, "0");
cr.update(Browser.BOOKMARKS_URI,
values,
Browser.BookmarkColumns.URL + " = ?",
new String[] { uri });
}
public BitmapDrawable getFaviconForUrl(ContentResolver cr, String uri) {
Cursor c = cr.query(Browser.BOOKMARKS_URI,
new String[] { Browser.BookmarkColumns.FAVICON },
Browser.BookmarkColumns.URL + " = ?",
new String[] { uri },
null);
if (!c.moveToFirst()) {
c.close();
return null;
}
int faviconIndex = c.getColumnIndexOrThrow(Browser.BookmarkColumns.FAVICON);
byte[] b = c.getBlob(faviconIndex);
c.close();
if (b == null)
return null;
Bitmap bitmap = BitmapFactory.decodeByteArray(b, 0, b.length);
return new BitmapDrawable(bitmap);
}
public void updateFaviconForUrl(ContentResolver cr, String uri,
BitmapDrawable favicon) {
Bitmap bitmap = favicon.getBitmap();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
ContentValues values = new ContentValues();
values.put(Browser.BookmarkColumns.FAVICON, stream.toByteArray());
values.put(Browser.BookmarkColumns.URL, uri);
cr.update(Browser.BOOKMARKS_URI,
values,
Browser.BookmarkColumns.URL + " = ?",
new String[] { uri });
}
public void updateThumbnailForUrl(ContentResolver cr, String uri,
BitmapDrawable thumbnail) {
Bitmap bitmap = thumbnail.getBitmap();
Cursor cursor = cr.query(Browser.BOOKMARKS_URI,
new String[] { BookmarkColumns.URL },
Browser.BookmarkColumns.URL + " = ?",
new String[] { uri },
Browser.BookmarkColumns.URL);
ContentValues values = new ContentValues();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 0, bos);
values.put(URL_COLUMN_THUMBNAIL, bos.toByteArray());
if (cursor.getCount() == 1) {
// entry exists, simply update image
cr.update(Browser.BOOKMARKS_URI,
values,
Browser.BookmarkColumns.URL + " = ?",
new String[] { uri });
} else {
// add a new entry
values.put(Browser.BookmarkColumns.URL, uri);
cr.insert(Browser.BOOKMARKS_URI, values);
}
cursor.close();
}
private static class AndroidDBCursor extends CursorWrapper {
public AndroidDBCursor(Cursor c) {
super(c);
}
private String translateColumnName(String columnName) {
if (columnName.equals(BrowserDB.URLColumns.URL)) {
columnName = Browser.BookmarkColumns.URL;
} else if (columnName.equals(BrowserDB.URLColumns.TITLE)) {
columnName = Browser.BookmarkColumns.TITLE;
} else if (columnName.equals(BrowserDB.URLColumns.FAVICON)) {
columnName = Browser.BookmarkColumns.FAVICON;
} else if (columnName.equals(BrowserDB.URLColumns.THUMBNAIL)) {
columnName = URL_COLUMN_THUMBNAIL;
} else if (columnName.equals(BrowserDB.URLColumns.DATE_LAST_VISITED)) {
columnName = Browser.BookmarkColumns.DATE;
}
return columnName;
}
@Override
public int getColumnIndex(String columnName) {
return super.getColumnIndex(translateColumnName(columnName));
}
@Override
public int getColumnIndexOrThrow(String columnName) {
return super.getColumnIndexOrThrow(translateColumnName(columnName));
}
}
}

View File

@ -0,0 +1,140 @@
/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is Mozilla Android code.
*
* The Initial Developer of the Original Code is Mozilla Foundation.
* Portions created by the Initial Developer are Copyright (C) 2011
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Lucas Rocha <lucasr@mozilla.com>
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
package org.mozilla.gecko.db;
import android.content.ContentResolver;
import android.database.Cursor;
import android.graphics.drawable.BitmapDrawable;
public class BrowserDB {
public static interface URLColumns {
public static String URL = "url";
public static String TITLE = "title";
public static String FAVICON = "favicon";
public static String THUMBNAIL = "thumbnail";
public static String DATE_LAST_VISITED = "date-last-visited";
}
private static BrowserDBIface sDb;
public interface BrowserDBIface {
public Cursor filter(ContentResolver cr, CharSequence constraint, int limit);
public void updateVisitedHistory(ContentResolver cr, String uri);
public void updateHistoryTitle(ContentResolver cr, String uri, String title);
public Cursor getAllVisitedHistory(ContentResolver cr);
public Cursor getRecentHistory(ContentResolver cr, int limit);
public void clearHistory(ContentResolver cr);
public Cursor getAllBookmarks(ContentResolver cr);
public boolean isBookmark(ContentResolver cr, String uri);
public void addBookmark(ContentResolver cr, String title, String uri);
public void removeBookmark(ContentResolver cr, String uri);
public BitmapDrawable getFaviconForUrl(ContentResolver cr, String uri);
public void updateFaviconForUrl(ContentResolver cr, String uri, BitmapDrawable favicon);
public void updateThumbnailForUrl(ContentResolver cr, String uri, BitmapDrawable thumbnail);
}
static {
// FIXME: Still need to figure out how to use local or android
// database here.
sDb = new AndroidBrowserDB();
}
public static Cursor filter(ContentResolver cr, CharSequence constraint, int limit) {
return sDb.filter(cr, constraint, limit);
}
public static void updateVisitedHistory(ContentResolver cr, String uri) {
sDb.updateVisitedHistory(cr, uri);
}
public static void updateHistoryTitle(ContentResolver cr, String uri, String title) {
sDb.updateHistoryTitle(cr, uri, title);
}
public static Cursor getAllVisitedHistory(ContentResolver cr) {
return sDb.getAllVisitedHistory(cr);
}
public static Cursor getRecentHistory(ContentResolver cr, int limit) {
return sDb.getRecentHistory(cr, limit);
}
public static void clearHistory(ContentResolver cr) {
sDb.clearHistory(cr);
}
public static Cursor getAllBookmarks(ContentResolver cr) {
return sDb.getAllBookmarks(cr);
}
public static boolean isBookmark(ContentResolver cr, String uri) {
return sDb.isBookmark(cr, uri);
}
public static void addBookmark(ContentResolver cr, String title, String uri) {
sDb.addBookmark(cr, title, uri);
}
public static void removeBookmark(ContentResolver cr, String uri) {
sDb.removeBookmark(cr, uri);
}
public static BitmapDrawable getFaviconForUrl(ContentResolver cr, String uri) {
return sDb.getFaviconForUrl(cr, uri);
}
public static void updateFaviconForUrl(ContentResolver cr, String uri, BitmapDrawable favicon) {
sDb.updateFaviconForUrl(cr, uri, favicon);
}
public static void updateThumbnailForUrl(ContentResolver cr, String uri, BitmapDrawable thumbnail) {
sDb.updateThumbnailForUrl(cr, uri, thumbnail);
}
}