Tidy up the website settings activity. This is mostly a cosmetic change. The only functional changes are to:
1) Not dismiss the site specific settings screen when a feature is removed if that site still has more features in use, and
2) Not use the title of the URL extracted from the bookmarks database to identify the site if that bookmark is not the root
   of the origin. For example, if the user bookmarked www.google.com/foo, we do not use the title of that bookmark to
   identify www.google.com in the WebsiteSettingsActivity. If they had www.google.com bookmarked, then we would use that
   title in the WebsiteSettingsActivity.

Bug: b/2252579

Change-Id: Ic4187987ae7ce40ba3678e00d0b9b848c122f380
diff --git a/src/com/android/browser/WebsiteSettingsActivity.java b/src/com/android/browser/WebsiteSettingsActivity.java
index 7166b16..a04c46f 100644
--- a/src/com/android/browser/WebsiteSettingsActivity.java
+++ b/src/com/android/browser/WebsiteSettingsActivity.java
@@ -88,6 +88,10 @@
             mFeatures |= (1 << feature);
         }
 
+        public void removeFeature(int feature) {
+            mFeatures &= ~(1 << feature);
+        }
+
         public boolean hasFeature(int feature) {
             return (mFeatures & (1 << feature)) != 0;
         }
@@ -168,7 +172,7 @@
             mResource = rsc;
             mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
             mDefaultIcon = BitmapFactory.decodeResource(getResources(),
-                    R.drawable.ic_launcher_shortcut_browser_bookmark);
+                    R.drawable.app_web_browser_sm);
             mUsageEmptyIcon = BitmapFactory.decodeResource(getResources(),
                     R.drawable.ic_list_data_off);
             mUsageLowIcon = BitmapFactory.decodeResource(getResources(),
@@ -280,7 +284,16 @@
                         Iterator<Site> sitesIter = matchingSites.iterator();
                         while (sitesIter.hasNext()) {
                             Site site = sitesIter.next();
-                            site.setTitle(title);
+                            // 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);
+                            }
                             if (bmp != null) {
                                 site.setIcon(bmp);
                             }
@@ -323,7 +336,7 @@
             // We display the size in MB, to 1dp, rounding up to the next 0.1MB.
             // bytes should always be greater than zero.
             if (bytes <= 0) {
-                Log.e(LOGTAG, "sizeValueToString called with non-positive value");
+                Log.e(LOGTAG, "sizeValueToString called with non-positive value: " + bytes);
                 return "0";
             }
             float megabytes = (float) bytes / (1024.0F * 1024.0F);
@@ -354,8 +367,6 @@
          */
         public void setIconForUsage(ImageView usageIcon, long usageInBytes) {
             float usageInMegabytes = (float) usageInBytes / (1024.0F * 1024.0F);
-            usageIcon.setVisibility(View.VISIBLE);
-
             // We set the correct icon:
             // 0 < empty < 0.1MB
             // 0.1MB < low < 5MB
@@ -373,9 +384,10 @@
             View view;
             final TextView title;
             final TextView subtitle;
-            ImageView icon;
+            final ImageView icon;
             final ImageView usageIcon;
             final ImageView locationIcon;
+            final ImageView featureIcon;
 
             if (convertView == null) {
                 view = mInflater.inflate(mResource, parent, false);
@@ -386,6 +398,7 @@
             title = (TextView) view.findViewById(R.id.title);
             subtitle = (TextView) view.findViewById(R.id.subtitle);
             icon = (ImageView) view.findViewById(R.id.icon);
+            featureIcon = (ImageView) view.findViewById(R.id.feature_icon);
             usageIcon = (ImageView) view.findViewById(R.id.usage_icon);
             locationIcon = (ImageView) view.findViewById(R.id.location_icon);
             usageIcon.setVisibility(View.GONE);
@@ -396,10 +409,22 @@
 
                 Site site = getItem(position);
                 title.setText(site.getPrettyTitle());
-                subtitle.setText(site.getPrettyOrigin());
+                String subtitleText = site.getPrettyOrigin();
+                if (subtitleText != null) {
+                    title.setMaxLines(1);
+                    title.setSingleLine(true);
+                    subtitle.setVisibility(View.VISIBLE);
+                    subtitle.setText(subtitleText);
+                } else {
+                    subtitle.setVisibility(View.GONE);
+                    title.setMaxLines(2);
+                    title.setSingleLine(false);
+                }
+
                 icon.setVisibility(View.VISIBLE);
                 usageIcon.setVisibility(View.INVISIBLE);
                 locationIcon.setVisibility(View.INVISIBLE);
+                featureIcon.setVisibility(View.GONE);
                 Bitmap bmp = site.getIcon();
                 if (bmp == null) {
                     bmp = mDefaultIcon;
@@ -415,6 +440,7 @@
                         public void onReceiveValue(Long value) {
                             if (value != null) {
                                 setIconForUsage(usageIcon, value.longValue());
+                                usageIcon.setVisibility(View.VISIBLE);
                             }
                         }
                     });
@@ -435,8 +461,11 @@
                     });
                 }
             } else {
-                setTitle(mCurrentSite.getPrettyTitle());
                 icon.setVisibility(View.GONE);
+                locationIcon.setVisibility(View.GONE);
+                usageIcon.setVisibility(View.GONE);
+                featureIcon.setVisibility(View.VISIBLE);
+                setTitle(mCurrentSite.getPrettyTitle());
                 String origin = mCurrentSite.getOrigin();
                 switch (mCurrentSite.getFeatureByIndex(position)) {
                     case Site.FEATURE_WEB_STORAGE:
@@ -446,6 +475,8 @@
                                     String usage = sizeValueToString(value.longValue()) + " " + sMBStored;
                                     title.setText(R.string.webstorage_clear_data_title);
                                     subtitle.setText(usage);
+                                    subtitle.setVisibility(View.VISIBLE);
+                                    setIconForUsage(featureIcon, value.longValue());
                                 }
                             }
                         });
@@ -457,9 +488,12 @@
                                 if (allowed != null) {
                                     if (allowed.booleanValue()) {
                                         subtitle.setText(R.string.geolocation_settings_page_summary_allowed);
+                                        featureIcon.setImageBitmap(mLocationAllowedIcon);
                                     } else {
                                         subtitle.setText(R.string.geolocation_settings_page_summary_not_allowed);
+                                        featureIcon.setImageBitmap(mLocationDisallowedIcon);
                                     }
+                                    subtitle.setVisibility(View.VISIBLE);
                                 }
                             }
                         });
@@ -484,8 +518,14 @@
                                                new AlertDialog.OnClickListener() {
                                 public void onClick(DialogInterface dlg, int which) {
                                     WebStorage.getInstance().deleteOrigin(mCurrentSite.getOrigin());
-                                    mCurrentSite = null;
+                                    // If this site has no more features, then go back to the
+                                    // origins list.
+                                    mCurrentSite.removeFeature(Site.FEATURE_WEB_STORAGE);
+                                    if (mCurrentSite.getFeatureCount() == 0) {
+                                        mCurrentSite = null;
+                                    }
                                     askForOrigins();
+                                    notifyDataSetChanged();
                                 }})
                             .setNegativeButton(R.string.webstorage_clear_data_dialog_cancel_button, null)
                             .setIcon(android.R.drawable.ic_dialog_alert)
@@ -499,8 +539,12 @@
                                                new AlertDialog.OnClickListener() {
                                 public void onClick(DialogInterface dlg, int which) {
                                     GeolocationPermissions.getInstance().clear(mCurrentSite.getOrigin());
-                                    mCurrentSite = null;
+                                    mCurrentSite.removeFeature(Site.FEATURE_GEOLOCATION);
+                                    if (mCurrentSite.getFeatureCount() == 0) {
+                                        mCurrentSite = null;
+                                    }
                                     askForOrigins();
+                                    notifyDataSetChanged();
                                 }})
                             .setNegativeButton(R.string.geolocation_settings_page_dialog_cancel_button, null)
                             .setIcon(android.R.drawable.ic_dialog_alert)