Bug 843234 - Move default favicon creation to separate thread. r=margaret

This commit is contained in:
Wes Johnston 2013-02-22 16:05:35 -08:00
Родитель 7c98e44ec1
Коммит 3693c62237
1 изменённых файлов: 41 добавлений и 24 удалений

Просмотреть файл

@ -1005,7 +1005,7 @@ public class BrowserProvider extends ContentProvider {
} }
// Returns the number of bookmarks inserted in the db // Returns the number of bookmarks inserted in the db
private int createDistributionBookmarks(SQLiteDatabase db) { private int createDistributionBookmarks(final SQLiteDatabase db) {
JSONArray bookmarks = Distribution.getBookmarks(mContext); JSONArray bookmarks = Distribution.getBookmarks(mContext);
if (bookmarks == null) { if (bookmarks == null) {
return 0; return 0;
@ -1015,20 +1015,32 @@ public class BrowserProvider extends ContentProvider {
int pos = 0; int pos = 0;
for (int i = 0; i < bookmarks.length(); i++) { for (int i = 0; i < bookmarks.length(); i++) {
try { try {
JSONObject bookmark = bookmarks.getJSONObject(i); final JSONObject bookmark = bookmarks.getJSONObject(i);
String title = getLocalizedProperty(bookmark, "title", locale); String title = getLocalizedProperty(bookmark, "title", locale);
String url = getLocalizedProperty(bookmark, "url", locale); final String url = getLocalizedProperty(bookmark, "url", locale);
createBookmark(db, title, url, pos);
pos++;
// Look for an optional icon data URI // return early if there is no icon for this bookmark
Bitmap icon = null; if (!bookmark.has("icon")) {
if (bookmark.has("icon")) { continue;
String iconData = bookmark.getString("icon");
icon = BitmapUtils.getBitmapFromDataURI(iconData);
} }
createBookmark(db, title, url, pos, icon); // create icons in a separate thread to avoid blocking about:home on startup
pos++; GeckoBackgroundThread.getHandler().post(new Runnable() {
public void run() {
try {
String iconData = bookmark.getString("icon");
Bitmap icon = BitmapUtils.getBitmapFromDataURI(iconData);
if (icon != null) {
createFavicon(db, url, icon);
}
} catch (JSONException e) {
Log.e(LOGTAG, "Error creating distribution bookmark icon", e);
}
}
});
} catch (JSONException e) { } catch (JSONException e) {
Log.e(LOGTAG, "Error creating distribution bookmark", e); Log.e(LOGTAG, "Error creating distribution bookmark", e);
} }
@ -1037,13 +1049,13 @@ public class BrowserProvider extends ContentProvider {
} }
// Inserts default bookmarks, starting at a specified position // Inserts default bookmarks, starting at a specified position
private void createDefaultBookmarks(SQLiteDatabase db, int pos) { private void createDefaultBookmarks(final SQLiteDatabase db, int pos) {
Class<?> stringsClass = R.string.class; Class<?> stringsClass = R.string.class;
Field[] fields = stringsClass.getFields(); Field[] fields = stringsClass.getFields();
Pattern p = Pattern.compile("^bookmarkdefaults_title_"); Pattern p = Pattern.compile("^bookmarkdefaults_title_");
for (int i = 0; i < fields.length; i++) { for (int i = 0; i < fields.length; i++) {
String name = fields[i].getName(); final String name = fields[i].getName();
Matcher m = p.matcher(name); Matcher m = p.matcher(name);
if (!m.find()) { if (!m.find()) {
continue; continue;
@ -1054,13 +1066,21 @@ public class BrowserProvider extends ContentProvider {
Field urlField = stringsClass.getField(name.replace("_title_", "_url_")); Field urlField = stringsClass.getField(name.replace("_title_", "_url_"));
int urlId = urlField.getInt(null); int urlId = urlField.getInt(null);
String url = mContext.getString(urlId); final String url = mContext.getString(urlId);
createBookmark(db, title, url, pos);
Bitmap icon = getDefaultFaviconFromPath(name); // create icons in a separate thread to avoid blocking about:home on startup
if (icon == null) { GeckoBackgroundThread.getHandler().post(new Runnable() {
icon = getDefaultFaviconFromDrawable(name); public void run() {
} Bitmap icon = getDefaultFaviconFromPath(name);
createBookmark(db, title, url, pos, icon); if (icon == null) {
icon = getDefaultFaviconFromDrawable(name);
}
if (icon != null) {
createFavicon(db, url, icon);
}
}
});
pos++; pos++;
} catch (java.lang.IllegalAccessException ex) { } catch (java.lang.IllegalAccessException ex) {
Log.e(LOGTAG, "Can't create bookmark " + name, ex); Log.e(LOGTAG, "Can't create bookmark " + name, ex);
@ -1070,7 +1090,7 @@ public class BrowserProvider extends ContentProvider {
} }
} }
private void createBookmark(SQLiteDatabase db, String title, String url, int pos, Bitmap icon) { private void createBookmark(SQLiteDatabase db, String title, String url, int pos) {
ContentValues bookmarkValues = new ContentValues(); ContentValues bookmarkValues = new ContentValues();
bookmarkValues.put(Bookmarks.PARENT, guidToID(db, Bookmarks.MOBILE_FOLDER_GUID)); bookmarkValues.put(Bookmarks.PARENT, guidToID(db, Bookmarks.MOBILE_FOLDER_GUID));
@ -1083,12 +1103,9 @@ public class BrowserProvider extends ContentProvider {
bookmarkValues.put(Bookmarks.GUID, Utils.generateGuid()); bookmarkValues.put(Bookmarks.GUID, Utils.generateGuid());
bookmarkValues.put(Bookmarks.POSITION, pos); bookmarkValues.put(Bookmarks.POSITION, pos);
db.insertOrThrow(TABLE_BOOKMARKS, Bookmarks.TITLE, bookmarkValues); db.insertOrThrow(TABLE_BOOKMARKS, Bookmarks.TITLE, bookmarkValues);
}
// Return early if there's no icon to set private void createFavicon(SQLiteDatabase db, String url, Bitmap icon) {
if (icon == null) {
return;
}
ByteArrayOutputStream stream = new ByteArrayOutputStream(); ByteArrayOutputStream stream = new ByteArrayOutputStream();
icon.compress(Bitmap.CompressFormat.PNG, 100, stream); icon.compress(Bitmap.CompressFormat.PNG, 100, stream);