From 3755040b84970071e07ccfa1e90f76c1bc3b119c Mon Sep 17 00:00:00 2001 From: Henrik Baard Date: Wed, 21 Apr 2010 12:36:33 +0200 Subject: Fixed FindBugs warnings in BrowserBackupAgent.java This makes sure that streams are closed in corner cases. Change-Id: I185c0dbddc09338de3a63eab22c57afb8d1a9c1a --- src/com/android/browser/BrowserBackupAgent.java | 57 +++++++++++++++++-------- 1 file changed, 40 insertions(+), 17 deletions(-) diff --git a/src/com/android/browser/BrowserBackupAgent.java b/src/com/android/browser/BrowserBackupAgent.java index 387555d..84672b1 100644 --- a/src/com/android/browser/BrowserBackupAgent.java +++ b/src/com/android/browser/BrowserBackupAgent.java @@ -84,6 +84,10 @@ public class BrowserBackupAgent extends BackupAgent { savedVersion = in.readInt(); } catch (EOFException e) { // It means we had no previous state; that's fine + } finally { + if (in != null) { + in.close(); + } } // Build a flattened representation of the bookmarks table @@ -174,6 +178,10 @@ public class BrowserBackupAgent extends BackupAgent { } catch (IOException ioe) { Log.w(TAG, "Bad backup data; not restoring"); crc = -1; + } finally { + if (in != null) { + in.close(); + } } } @@ -187,7 +195,7 @@ public class BrowserBackupAgent extends BackupAgent { } } - class Bookmark { + static class Bookmark { public String url; public int visits; public long date; @@ -258,13 +266,18 @@ public class BrowserBackupAgent extends BackupAgent { data.writeEntityHeader(key, toCopy); FileInputStream in = new FileInputStream(file); - int nRead; - while (toCopy > 0) { - nRead = in.read(buf, 0, CHUNK); - data.writeEntityData(buf, nRead); - toCopy -= nRead; + try { + int nRead; + while (toCopy > 0) { + nRead = in.read(buf, 0, CHUNK); + data.writeEntityData(buf, nRead); + toCopy -= nRead; + } + } finally { + if (in != null) { + in.close(); + } } - in.close(); } // Read the given file from backup to a file, calculating a CRC32 along the way @@ -275,14 +288,18 @@ public class BrowserBackupAgent extends BackupAgent { CRC32 crc = new CRC32(); FileOutputStream out = new FileOutputStream(file); - while (toRead > 0) { - int numRead = data.readEntityData(buf, 0, CHUNK); - crc.update(buf, 0, numRead); - out.write(buf, 0, numRead); - toRead -= numRead; + try { + while (toRead > 0) { + int numRead = data.readEntityData(buf, 0, CHUNK); + crc.update(buf, 0, numRead); + out.write(buf, 0, numRead); + toRead -= numRead; + } + } finally { + if (out != null) { + out.close(); + } } - - out.close(); return crc.getValue(); } @@ -291,8 +308,14 @@ public class BrowserBackupAgent extends BackupAgent { throws IOException { DataOutputStream out = new DataOutputStream( new FileOutputStream(stateFile.getFileDescriptor())); - out.writeLong(fileSize); - out.writeLong(crc); - out.writeInt(BACKUP_AGENT_VERSION); + try { + out.writeLong(fileSize); + out.writeLong(crc); + out.writeInt(BACKUP_AGENT_VERSION); + } finally { + if (out != null) { + out.close(); + } + } } } -- cgit v1.1 From 671935fd8a96043bb4bbfa1fe69e50da0c212d5c Mon Sep 17 00:00:00 2001 From: Henrik Baard Date: Tue, 27 Apr 2010 08:20:57 +0200 Subject: Fixed FindBugs warnings in ErrorConsoleView.java Save some memory by marking ErrorConsoleMessageList static. Change-Id: I6034eb295fde9639073f2c15c872743afd5aefe1 --- src/com/android/browser/ErrorConsoleView.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/com/android/browser/ErrorConsoleView.java b/src/com/android/browser/ErrorConsoleView.java index 56f663b..9f1873c 100644 --- a/src/com/android/browser/ErrorConsoleView.java +++ b/src/com/android/browser/ErrorConsoleView.java @@ -228,7 +228,7 @@ import java.util.Vector; * This class is an adapter for ErrorConsoleListView that contains the error console * message data. */ - private class ErrorConsoleMessageList extends android.widget.BaseAdapter + private static class ErrorConsoleMessageList extends android.widget.BaseAdapter implements android.widget.ListAdapter { private Vector mMessages; -- cgit v1.1 From 98f42de9a48ee87c007a2d233b6d790779b494b5 Mon Sep 17 00:00:00 2001 From: Henrik Baard Date: Tue, 27 Apr 2010 08:26:06 +0200 Subject: Optimized Maps usage by adding explicit types and improved performance Optimized the use of Maps for improved runtime performance, the site object can be accessed directly. Change-Id: Icfcd020541e806427c4ac7ba0817d30b2c7eb9a5 --- .../android/browser/WebsiteSettingsActivity.java | 89 +++++++++++----------- 1 file changed, 45 insertions(+), 44 deletions(-) diff --git a/src/com/android/browser/WebsiteSettingsActivity.java b/src/com/android/browser/WebsiteSettingsActivity.java index 047867a..a5270a4 100644 --- a/src/com/android/browser/WebsiteSettingsActivity.java +++ b/src/com/android/browser/WebsiteSettingsActivity.java @@ -62,7 +62,7 @@ public class WebsiteSettingsActivity extends ListActivity { private static String sMBStored = null; private SiteAdapter mAdapter = null; - class Site { + static class Site { private String mOrigin; private String mTitle; private Bitmap mIcon; @@ -186,7 +186,7 @@ public class WebsiteSettingsActivity extends ListActivity { * Adds the specified feature to the site corresponding to supplied * origin in the map. Creates the site if it does not already exist. */ - private void addFeatureToSite(Map sites, String origin, int feature) { + private void addFeatureToSite(Map sites, String origin, int feature) { Site site = null; if (sites.containsKey(origin)) { site = (Site) sites.get(origin); @@ -209,7 +209,7 @@ public class WebsiteSettingsActivity extends ListActivity { WebStorage.getInstance().getOrigins(new ValueCallback() { public void onReceiveValue(Map origins) { - Map sites = new HashMap(); + Map sites = new HashMap(); if (origins != null) { Iterator iter = origins.keySet().iterator(); while (iter.hasNext()) { @@ -221,7 +221,7 @@ public class WebsiteSettingsActivity extends ListActivity { }); } - public void askForGeolocation(final Map sites) { + public void askForGeolocation(final Map sites) { GeolocationPermissions.getInstance().getOrigins(new ValueCallback >() { public void onReceiveValue(Set origins) { if (origins != null) { @@ -236,19 +236,19 @@ public class WebsiteSettingsActivity extends ListActivity { }); } - public void populateIcons(Map sites) { + 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 keys = sites.keySet(); - Iterator originIter = keys.iterator(); + HashMap> hosts = new HashMap>(); + Set> elements = sites.entrySet(); + Iterator> originIter = elements.iterator(); while (originIter.hasNext()) { - String origin = originIter.next(); - Site site = (Site) sites.get(origin); - String host = Uri.parse(origin).getHost(); - Set hostSites = null; + 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); + hostSites = (Set)hosts.get(host); } else { hostSites = new HashSet(); hosts.put(host, hostSites); @@ -262,46 +262,47 @@ public class WebsiteSettingsActivity extends ListActivity { new String[] { Browser.BookmarkColumns.URL, Browser.BookmarkColumns.TITLE, Browser.BookmarkColumns.FAVICON }, "bookmark = 1", null, null); - if ((c != null) && c.moveToFirst()) { - int urlIndex = c.getColumnIndex(Browser.BookmarkColumns.URL); - int titleIndex = c.getColumnIndex(Browser.BookmarkColumns.TITLE); - int faviconIndex = c.getColumnIndex(Browser.BookmarkColumns.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(); - site.setTitle(title); - if (bmp != null) { - site.setIcon(bmp); + if (c != null) { + if(c.moveToFirst()) { + int urlIndex = c.getColumnIndex(Browser.BookmarkColumns.URL); + int titleIndex = c.getColumnIndex(Browser.BookmarkColumns.TITLE); + int faviconIndex = c.getColumnIndex(Browser.BookmarkColumns.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(); + site.setTitle(title); + if (bmp != null) { + site.setIcon(bmp); + } } } - } - } while (c.moveToNext()); + } while (c.moveToNext()); + } + c.close(); } - - c.close(); } - public void populateOrigins(Map sites) { + public void populateOrigins(Map sites) { clear(); // We can now simply populate our array with Site instances - Set keys = sites.keySet(); - Iterator originIter = keys.iterator(); - while (originIter.hasNext()) { - String origin = originIter.next(); - Site site = (Site) sites.get(origin); + Set> elements = sites.entrySet(); + Iterator> entryIterator = elements.iterator(); + while (entryIterator.hasNext()) { + Map.Entry entry = entryIterator.next(); + Site site = entry.getValue(); add(site); } -- cgit v1.1