diff options
author | Ben Murdoch <benm@google.com> | 2010-11-25 15:42:19 +0000 |
---|---|---|
committer | Ben Murdoch <benm@google.com> | 2010-11-26 10:10:33 +0000 |
commit | a7c9a5505a0a1de5da3041a704070c3de9c129d9 (patch) | |
tree | 4650411013cd184aa26c93e99023c604dc22a609 /src/com/android/browser/WebsiteSettingsActivity.java | |
parent | b4b87c66e241a7bda0c6c571a82d461775032438 (diff) | |
download | packages_apps_Browser-a7c9a5505a0a1de5da3041a704070c3de9c129d9.zip packages_apps_Browser-a7c9a5505a0a1de5da3041a704070c3de9c129d9.tar.gz packages_apps_Browser-a7c9a5505a0a1de5da3041a704070c3de9c129d9.tar.bz2 |
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
Diffstat (limited to 'src/com/android/browser/WebsiteSettingsActivity.java')
-rw-r--r-- | src/com/android/browser/WebsiteSettingsActivity.java | 138 |
1 files changed, 83 insertions, 55 deletions
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<String, Site> sites) { // Create a map from host to origin. This is used to add metadata - // (title, icon) for this origin from the bookmarks DB. - HashMap<String, Set<Site>> hosts = new HashMap<String, Set<Site>>(); - Set<Map.Entry<String, Site>> elements = sites.entrySet(); - Iterator<Map.Entry<String, Site>> originIter = elements.iterator(); - while (originIter.hasNext()) { - Map.Entry<String, Site> entry = originIter.next(); - Site site = entry.getValue(); - String host = Uri.parse(entry.getKey()).getHost(); - Set<Site> hostSites = null; - if (hosts.containsKey(host)) { - hostSites = (Set<Site>)hosts.get(host); - } else { - hostSites = new HashSet<Site>(); - 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<Void, Void, Void> { + + private Context mContext; + private boolean mDataSetChanged; + private Map<String, Site> mSites; + + public UpdateFromBookmarksDbTask(Context ctx, Map<String, Site> 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<Site> 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<String, Set<Site>> hosts = new HashMap<String, Set<Site>>(); + Set<Map.Entry<String, Site>> elements = mSites.entrySet(); + Iterator<Map.Entry<String, Site>> originIter = elements.iterator(); + while (originIter.hasNext()) { + Map.Entry<String, Site> entry = originIter.next(); + Site site = entry.getValue(); + String host = Uri.parse(entry.getKey()).getHost(); + Set<Site> hostSites = null; + if (hosts.containsKey(host)) { + hostSites = (Set<Site>)hosts.get(host); + } else { + hostSites = new HashSet<Site>(); + 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<Site> 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(); } } |