From a7c9a5505a0a1de5da3041a704070c3de9c129d9 Mon Sep 17 00:00:00 2001 From: Ben Murdoch Date: Thu, 25 Nov 2010 15:42:19 +0000 Subject: Fix StrictMode violation in WebsiteSettingsActivity WebsiteSettings was querying the bookmarks database on the UI thread. This is a strict mode violation so move that database read into an AsyncTask. Change-Id: I4f27dc2ec3cb842fe09591a0ee406f8af61d3022 --- .../android/browser/WebsiteSettingsActivity.java | 138 +++++++++++++-------- 1 file changed, 83 insertions(+), 55 deletions(-) (limited to 'src/com/android/browser/WebsiteSettingsActivity.java') diff --git a/src/com/android/browser/WebsiteSettingsActivity.java b/src/com/android/browser/WebsiteSettingsActivity.java index bd57c91..95f8fb0 100644 --- a/src/com/android/browser/WebsiteSettingsActivity.java +++ b/src/com/android/browser/WebsiteSettingsActivity.java @@ -24,6 +24,7 @@ import android.database.Cursor; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.net.Uri; +import android.os.AsyncTask; import android.os.Bundle; import android.provider.BrowserContract.Bookmarks; import android.util.Log; @@ -242,67 +243,94 @@ public class WebsiteSettingsActivity extends ListActivity { public void populateIcons(Map sites) { // Create a map from host to origin. This is used to add metadata - // (title, icon) for this origin from the bookmarks DB. - HashMap> hosts = new HashMap>(); - Set> elements = sites.entrySet(); - Iterator> originIter = elements.iterator(); - while (originIter.hasNext()) { - Map.Entry entry = originIter.next(); - Site site = entry.getValue(); - String host = Uri.parse(entry.getKey()).getHost(); - Set hostSites = null; - if (hosts.containsKey(host)) { - hostSites = (Set)hosts.get(host); - } else { - hostSites = new HashSet(); - hosts.put(host, hostSites); - } - hostSites.add(site); + // (title, icon) for this origin from the bookmarks DB. We must do + // the DB access on a background thread. + new UpdateFromBookmarksDbTask(this.getContext(), sites).execute(); + } + + private class UpdateFromBookmarksDbTask extends AsyncTask { + + private Context mContext; + private boolean mDataSetChanged; + private Map mSites; + + public UpdateFromBookmarksDbTask(Context ctx, Map sites) { + mContext = ctx; + mSites = sites; } - // Check the bookmark DB. If we have data for a host used by any of - // our origins, use it to set their title and favicon - Cursor c = getContext().getContentResolver().query(Bookmarks.CONTENT_URI, - new String[] { Bookmarks.URL, Bookmarks.TITLE, Bookmarks.FAVICON }, - Bookmarks.IS_FOLDER + " == 0", null, null); - - if (c != null) { - if (c.moveToFirst()) { - int urlIndex = c.getColumnIndex(Bookmarks.URL); - int titleIndex = c.getColumnIndex(Bookmarks.TITLE); - int faviconIndex = c.getColumnIndex(Bookmarks.FAVICON); - do { - String url = c.getString(urlIndex); - String host = Uri.parse(url).getHost(); - if (hosts.containsKey(host)) { - String title = c.getString(titleIndex); - Bitmap bmp = null; - byte[] data = c.getBlob(faviconIndex); - if (data != null) { - bmp = BitmapFactory.decodeByteArray(data, 0, data.length); - } - Set matchingSites = (Set) hosts.get(host); - Iterator sitesIter = matchingSites.iterator(); - while (sitesIter.hasNext()) { - Site site = sitesIter.next(); - // We should only set the title if the bookmark is for the root - // (i.e. www.google.com), as website settings act on the origin - // as a whole rather than a single page under that origin. If the - // user has bookmarked a page under the root but *not* the root, - // then we risk displaying the title of that page which may or - // may not have any relevance to the origin. - if (url.equals(site.getOrigin()) || - (new String(site.getOrigin()+"/")).equals(url)) { - site.setTitle(title); + protected Void doInBackground(Void... unused) { + HashMap> hosts = new HashMap>(); + Set> elements = mSites.entrySet(); + Iterator> originIter = elements.iterator(); + while (originIter.hasNext()) { + Map.Entry entry = originIter.next(); + Site site = entry.getValue(); + String host = Uri.parse(entry.getKey()).getHost(); + Set hostSites = null; + if (hosts.containsKey(host)) { + hostSites = (Set)hosts.get(host); + } else { + hostSites = new HashSet(); + hosts.put(host, hostSites); + } + hostSites.add(site); + } + + // Check the bookmark DB. If we have data for a host used by any of + // our origins, use it to set their title and favicon + Cursor c = mContext.getContentResolver().query(Bookmarks.CONTENT_URI, + new String[] { Bookmarks.URL, Bookmarks.TITLE, Bookmarks.FAVICON }, + Bookmarks.IS_FOLDER + " == 0", null, null); + + if (c != null) { + if (c.moveToFirst()) { + int urlIndex = c.getColumnIndex(Bookmarks.URL); + int titleIndex = c.getColumnIndex(Bookmarks.TITLE); + int faviconIndex = c.getColumnIndex(Bookmarks.FAVICON); + do { + String url = c.getString(urlIndex); + String host = Uri.parse(url).getHost(); + if (hosts.containsKey(host)) { + String title = c.getString(titleIndex); + Bitmap bmp = null; + byte[] data = c.getBlob(faviconIndex); + if (data != null) { + bmp = BitmapFactory.decodeByteArray(data, 0, data.length); } - if (bmp != null) { - site.setIcon(bmp); + Set matchingSites = (Set) hosts.get(host); + Iterator sitesIter = matchingSites.iterator(); + while (sitesIter.hasNext()) { + Site site = sitesIter.next(); + // We should only set the title if the bookmark is for the root + // (i.e. www.google.com), as website settings act on the origin + // as a whole rather than a single page under that origin. If the + // user has bookmarked a page under the root but *not* the root, + // then we risk displaying the title of that page which may or + // may not have any relevance to the origin. + if (url.equals(site.getOrigin()) || + (new String(site.getOrigin()+"/")).equals(url)) { + mDataSetChanged = true; + site.setTitle(title); + } + + if (bmp != null) { + mDataSetChanged = true; + site.setIcon(bmp); + } } } - } - } while (c.moveToNext()); + } while (c.moveToNext()); + } + c.close(); + } + return null; + } + + protected void onPostExecute(Void unused) { + if (mDataSetChanged) { + notifyDataSetChanged(); } - c.close(); } } -- cgit v1.1