Backout 839f36123bce (bug 704490) for Try failure

This commit is contained in:
Lucas Rocha 2011-12-02 18:38:46 +00:00
parent 446c9f4861
commit ac952b7dce
10 changed files with 201 additions and 496 deletions

View File

@ -63,9 +63,6 @@ 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);
@ -82,7 +79,7 @@ public class AboutHomeContent extends LinearLayout {
}
private static final String LOGTAG = "GeckoAboutHome";
private static final String TITLE_KEY = "title";
private static final int NUMBER_OF_TOP_SITES = 9;
private static final String kAbouthomeWhereClause = Browser.BookmarkColumns.BOOKMARK + " = 1";
private static final int kTileWidth = 122;
private Cursor mCursor;
@ -130,8 +127,8 @@ public class AboutHomeContent extends LinearLayout {
GeckoAppShell.getHandler().post(new Runnable() {
public void run() {
ContentResolver resolver = GeckoApp.mAppContext.getContentResolver();
mCursor = BrowserDB.filter(resolver, "", NUMBER_OF_TOP_SITES);
mCursor = activity.managedQuery(Browser.BOOKMARKS_URI,
null, kAbouthomeWhereClause, null, null);
activity.startManagingCursor(mCursor);
onActivityContentChanged(activity);
@ -140,10 +137,10 @@ public class AboutHomeContent extends LinearLayout {
public void run() {
final SimpleCursorAdapter gridAdapter =
new SimpleCursorAdapter(activity, R.layout.abouthome_grid_box, mCursor,
new String[] { URLColumns.TITLE,
URLColumns.FAVICON,
URLColumns.URL,
URLColumns.THUMBNAIL },
new String[] {Browser.BookmarkColumns.TITLE,
Browser.BookmarkColumns.FAVICON,
Browser.BookmarkColumns.URL,
"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());
@ -214,7 +211,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(URLColumns.URL));
String spec = mCursor.getString(mCursor.getColumnIndex(Browser.BookmarkColumns.URL));
Log.i(LOGTAG, "clicked: " + spec);
if (mUriLoadCallback != null)
mUriLoadCallback.callback(spec);
@ -242,7 +239,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(URLColumns.URL);
int urlIndex = cursor.getColumnIndexOrThrow(Browser.BookmarkColumns.URL);
title = cursor.getString(urlIndex);
}
@ -268,22 +265,23 @@ class AwesomeCursorViewBinder implements SimpleCursorAdapter.ViewBinder {
@Override
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
int faviconIndex = cursor.getColumnIndexOrThrow(URLColumns.FAVICON);
int faviconIndex = cursor.getColumnIndexOrThrow(Browser.BookmarkColumns.FAVICON);
if (columnIndex == faviconIndex) {
return updateImage(view, cursor, faviconIndex);
}
int titleIndex = cursor.getColumnIndexOrThrow(URLColumns.TITLE);
int titleIndex = cursor.getColumnIndexOrThrow(Browser.BookmarkColumns.TITLE);
if (columnIndex == titleIndex) {
return updateTitle(view, cursor, titleIndex);
}
int urlIndex = cursor.getColumnIndexOrThrow(URLColumns.URL);
int urlIndex = cursor.getColumnIndexOrThrow(Browser.BookmarkColumns.URL);
if (columnIndex == urlIndex) {
return updateUrl(view, cursor, urlIndex);
}
int thumbIndex = cursor.getColumnIndexOrThrow(URLColumns.THUMBNAIL);
int thumbIndex = cursor.getColumnIndexOrThrow("thumbnail");
if (columnIndex == thumbIndex) {
return updateImage(view, cursor, thumbIndex);
}

View File

@ -60,6 +60,7 @@ public class AwesomeBar extends Activity {
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 enum Type { ADD, EDIT };

View File

@ -72,9 +72,6 @@ import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import org.mozilla.gecko.db.BrowserDB;
import org.mozilla.gecko.db.BrowserDB.URLColumns;
public class AwesomeBarTabs extends TabHost {
private static final String LOGTAG = "GeckoAwesomeBarTabs";
@ -121,7 +118,7 @@ public class AwesomeBarTabs extends TabHost {
Map<String,Object> historyItem =
(Map<String,Object>) mHistoryAdapter.getChild(groupPosition, childPosition);
byte[] b = (byte[]) historyItem.get(URLColumns.FAVICON);
byte[] b = (byte[]) historyItem.get(Browser.BookmarkColumns.FAVICON);
ImageView favicon = (ImageView) childView.findViewById(R.id.favicon);
if (b == null) {
@ -156,7 +153,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(URLColumns.URL);
int urlIndex = cursor.getColumnIndexOrThrow(Browser.BookmarkColumns.URL);
title = cursor.getString(urlIndex);
}
@ -165,12 +162,12 @@ public class AwesomeBarTabs extends TabHost {
}
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
int faviconIndex = cursor.getColumnIndexOrThrow(URLColumns.FAVICON);
int faviconIndex = cursor.getColumnIndexOrThrow(Browser.BookmarkColumns.FAVICON);
if (columnIndex == faviconIndex) {
return updateFavicon(view, cursor, faviconIndex);
}
int titleIndex = cursor.getColumnIndexOrThrow(URLColumns.TITLE);
int titleIndex = cursor.getColumnIndexOrThrow(Browser.BookmarkColumns.TITLE);
if (columnIndex == titleIndex) {
return updateTitle(view, cursor, titleIndex);
}
@ -183,7 +180,16 @@ public class AwesomeBarTabs extends TabHost {
private class BookmarksQueryTask extends AsyncTask<Void, Void, Cursor> {
protected Cursor doInBackground(Void... arg0) {
ContentResolver resolver = mContext.getContentResolver();
return BrowserDB.getAllBookmarks(resolver);
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);
}
protected void onPostExecute(Cursor cursor) {
@ -192,9 +198,9 @@ public class AwesomeBarTabs extends TabHost {
mContext,
R.layout.awesomebar_row,
cursor,
new String[] { URLColumns.TITLE,
URLColumns.URL,
URLColumns.FAVICON },
new String[] { AwesomeBar.TITLE_KEY,
AwesomeBar.URL_KEY,
Browser.BookmarkColumns.FAVICON },
new int[] { R.id.title, R.id.url, R.id.favicon }
);
@ -219,7 +225,15 @@ public class AwesomeBarTabs extends TabHost {
protected Pair<List,List> doInBackground(Void... arg0) {
Pair<List,List> result = null;
ContentResolver resolver = mContext.getContentResolver();
Cursor cursor = BrowserDB.getRecentHistory(resolver, MAX_RESULTS);
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);
Date now = new Date();
now.setHours(0);
@ -244,7 +258,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(URLColumns.DATE_LAST_VISITED));
long time = cursor.getLong(cursor.getColumnIndexOrThrow(Browser.BookmarkColumns.DATE));
HistorySection itemSection = getSectionForTime(time, today);
if (groups == null)
@ -286,20 +300,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(URLColumns.URL));
String title = cursor.getString(cursor.getColumnIndexOrThrow(URLColumns.TITLE));
byte[] favicon = cursor.getBlob(cursor.getColumnIndexOrThrow(URLColumns.FAVICON));
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));
// 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(URLColumns.URL, url);
historyItem.put(URLColumns.TITLE, title);
historyItem.put(AwesomeBar.URL_KEY, url);
historyItem.put(AwesomeBar.TITLE_KEY, title);
if (favicon != null)
historyItem.put(URLColumns.FAVICON, favicon);
historyItem.put(Browser.BookmarkColumns.FAVICON, favicon);
return historyItem;
}
@ -307,7 +321,7 @@ public class AwesomeBarTabs extends TabHost {
public Map<String,?> createGroupItem(HistorySection section) {
Map<String,String> groupItem = new HashMap<String,String>();
groupItem.put(URLColumns.TITLE, getSectionName(section));
groupItem.put(AwesomeBar.TITLE_KEY, getSectionName(section));
return groupItem;
}
@ -361,11 +375,11 @@ public class AwesomeBarTabs extends TabHost {
mContext,
result.first,
R.layout.awesomebar_header_row,
new String[] { URLColumns.TITLE },
new String[] { AwesomeBar.TITLE_KEY },
new int[] { R.id.title },
result.second,
R.layout.awesomebar_row,
new String[] { URLColumns.TITLE, URLColumns.URL },
new String[] { AwesomeBar.TITLE_KEY, AwesomeBar.URL_KEY },
new int[] { R.id.title, R.id.url }
);
@ -486,9 +500,9 @@ public class AwesomeBarTabs extends TabHost {
mContext,
R.layout.awesomebar_row,
null,
new String[] { URLColumns.TITLE,
URLColumns.URL,
URLColumns.FAVICON },
new String[] { AwesomeBar.TITLE_KEY,
AwesomeBar.URL_KEY,
Browser.BookmarkColumns.FAVICON },
new int[] { R.id.title, R.id.url, R.id.favicon }
);
@ -497,7 +511,19 @@ public class AwesomeBarTabs extends TabHost {
mAllPagesAdapter.setFilterQueryProvider(new FilterQueryProvider() {
public Cursor runQuery(CharSequence constraint) {
ContentResolver resolver = mContext.getContentResolver();
return BrowserDB.filter(resolver, constraint, MAX_RESULTS);
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);
}
});
@ -553,7 +579,7 @@ public class AwesomeBarTabs extends TabHost {
Map<String,Object> historyItem =
(Map<String,Object>) mHistoryAdapter.getChild(groupPosition, childPosition);
String url = (String) historyItem.get(URLColumns.URL);
String url = (String) historyItem.get(AwesomeBar.URL_KEY);
if (mUrlOpenListener != null)
mUrlOpenListener.onUrlOpen(this, url);
@ -561,7 +587,7 @@ public class AwesomeBarTabs extends TabHost {
private void handleItemClick(ListView list, int position) {
Cursor cursor = (Cursor) list.getItemAtPosition(position);
String url = cursor.getString(cursor.getColumnIndexOrThrow(URLColumns.URL));
String url = cursor.getString(cursor.getColumnIndexOrThrow(AwesomeBar.URL_KEY));
if (mUrlOpenListener != null)
mUrlOpenListener.onUrlOpen(this, url);

View File

@ -37,10 +37,9 @@
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;
@ -65,7 +64,7 @@ class ConfirmPreference extends DialogPreference {
if ("clear_history".equalsIgnoreCase(mAction)) {
GeckoAppShell.getHandler().post(new Runnable(){
public void run() {
BrowserDB.clearHistory(mContext.getContentResolver());
Browser.clearHistory(mContext.getContentResolver());
}
});
} else if ("clear_private_data".equalsIgnoreCase(mAction)) {

View File

@ -44,14 +44,19 @@ 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.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
@ -60,8 +65,6 @@ 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";
@ -232,19 +235,51 @@ public class Favicons {
Log.d(LOGTAG, "Loading favicon from DB for URL = " + mPageUrl);
ContentResolver resolver = mContext.getContentResolver();
BitmapDrawable favicon = BrowserDB.getFaviconForUrl(resolver, mPageUrl);
if (favicon != null)
Cursor c = resolver.query(Browser.BOOKMARKS_URI,
new String[] { Browser.BookmarkColumns.FAVICON },
Browser.BookmarkColumns.URL + " = ?",
new String[] { mPageUrl },
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);
Log.d(LOGTAG, "Loaded favicon from DB successfully for URL = " + mPageUrl);
return favicon;
return new BitmapDrawable(bitmap);
}
// Runs in background thread
private void saveFaviconToDb(BitmapDrawable favicon) {
Log.d(LOGTAG, "Saving favicon on browser database for URL = " + mPageUrl);
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();
BrowserDB.updateFaviconForUrl(resolver, mPageUrl, favicon);
Log.d(LOGTAG, "Saving favicon on browser database for URL = " + mPageUrl);
resolver.update(Browser.BOOKMARKS_URI,
values,
Browser.BookmarkColumns.URL + " = ?",
new String[] { mPageUrl });
Log.d(LOGTAG, "Saving favicon URL for URL = " + mPageUrl);
mDbHelper.setFaviconUrlForPageUrl(mPageUrl, mFaviconUrl);

View File

@ -43,13 +43,12 @@ import java.util.Queue;
import java.util.Set;
import java.lang.ref.SoftReference;
import android.content.ContentResolver;
import android.content.ContentValues;
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";
@ -81,7 +80,12 @@ class GlobalHistory {
// the cache was wiped away, repopulate it
Log.w(LOGTAG, "Rebuilding visited link set...");
visitedSet = new HashSet<String>();
Cursor c = BrowserDB.getAllVisitedHistory(GeckoApp.mAppContext.getContentResolver());
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);
if (c.moveToFirst()) {
do {
visitedSet.add(c.getString(0));
@ -108,7 +112,7 @@ class GlobalHistory {
}
public void add(String uri) {
BrowserDB.updateVisitedHistory(GeckoApp.mAppContext.getContentResolver(), uri);
Browser.updateVisitedHistory(GeckoApp.mAppContext.getContentResolver(), uri, true);
Set<String> visitedSet = mVisitedCache.get();
if (visitedSet != null) {
visitedSet.add(uri);
@ -117,8 +121,13 @@ class GlobalHistory {
}
public void update(String uri, String title) {
ContentResolver resolver = GeckoApp.mAppContext.getContentResolver();
BrowserDB.updateHistoryTitle(resolver, uri, 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 });
}
public void checkUriVisited(final String uri) {

View File

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

View File

@ -38,9 +38,12 @@
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;
@ -53,8 +56,6 @@ 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";
@ -142,7 +143,7 @@ public class Tab {
}
Bitmap bitmap = Bitmap.createBitmap(b, 0, 0, sMinDim, sMinDim);
mThumbnail = new BitmapDrawable(bitmap);
saveThumbnailToDB((BitmapDrawable) mThumbnail);
saveThumbnailToDB(bitmap);
}
});
}
@ -350,7 +351,17 @@ public class Tab {
@Override
protected Boolean doInBackground(Void... unused) {
ContentResolver resolver = Tabs.getInstance().getContentResolver();
return BrowserDB.isBookmark(resolver, getURL());
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;
}
@Override
@ -363,7 +374,31 @@ public class Tab {
@Override
protected Void doInBackground(Void... unused) {
ContentResolver resolver = Tabs.getInstance().getContentResolver();
BrowserDB.addBookmark(resolver, getTitle(), getURL());
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();
return null;
}
@ -373,10 +408,33 @@ public class Tab {
}
}
private void saveThumbnailToDB(BitmapDrawable thumbnail) {
private void saveThumbnailToDB(Bitmap bitmap) {
try {
ContentResolver resolver = Tabs.getInstance().getContentResolver();
BrowserDB.updateThumbnailForUrl(resolver, getURL(), thumbnail);
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();
} catch (Exception e) {
// ignore
}
@ -386,7 +444,12 @@ public class Tab {
@Override
protected Void doInBackground(Void... unused) {
ContentResolver resolver = Tabs.getInstance().getContentResolver();
BrowserDB.removeBookmark(resolver, getURL());
ContentValues values = new ContentValues();
values.put(Browser.BookmarkColumns.BOOKMARK, "0");
resolver.update(Browser.BOOKMARKS_URI,
values,
Browser.BookmarkColumns.URL + " = ?",
new String[] { getURL() });
return null;
}

View File

@ -1,282 +0,0 @@
/* -*- 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 LOGTAG = "GeckoAndroidBrowserDB";
public Cursor filter(ContentResolver cr, CharSequence constraint, int limit) {
Cursor c = cr.query(Browser.BOOKMARKS_URI,
new String[] { BookmarkColumns.URL, BookmarkColumns.TITLE, BookmarkColumns.FAVICON, "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[] { 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[] { 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("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 = "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

@ -1,142 +0,0 @@
/* -*- 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 {
private static final String LOGTAG = "GeckoBrowserDB";
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);
}
}