auto import from //branches/cupcake/...@131421
diff --git a/src/com/android/browser/GearsSettingsDialog.java b/src/com/android/browser/GearsSettingsDialog.java
index 56a1d8d..5ea2342 100644
--- a/src/com/android/browser/GearsSettingsDialog.java
+++ b/src/com/android/browser/GearsSettingsDialog.java
@@ -17,14 +17,21 @@
 package com.android.browser;
 
 import android.app.Activity;
+import android.app.AlertDialog;
+import android.app.Dialog;
 import android.content.Context;
+import android.content.DialogInterface;
 import android.os.Handler;
 import android.util.Log;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
 import android.widget.ArrayAdapter;
+import android.widget.AdapterView;
+import android.widget.AdapterView.OnItemClickListener;
+import android.widget.BaseAdapter;
 import android.widget.Button;
+import android.widget.CheckBox;
 import android.widget.CompoundButton;
 import android.widget.ImageView;
 import android.widget.ListAdapter;
@@ -33,6 +40,7 @@
 import android.widget.TextView;
 
 import com.android.browser.GearsPermissions.OriginPermissions;
+import com.android.browser.GearsPermissions.Permission;
 import com.android.browser.GearsPermissions.PermissionsChangesListener;
 import com.android.browser.GearsPermissions.PermissionType;
 
@@ -55,6 +63,7 @@
   private Vector<OriginPermissions> mCurrentPermissions = null;
 
   private Vector<PermissionType> mPermissions;
+  private static final int CONFIRMATION_REMOVE_DIALOG = 1;
 
   // We declare the permissions globally to simplify the code
   private final PermissionType LOCAL_STORAGE =
@@ -64,23 +73,23 @@
 
   private boolean mChanges = false;
 
+  SettingsAdapter mListAdapter;
 
   public GearsSettingsDialog(Activity activity,
                              Handler handler,
                              String arguments) {
     super (activity, handler, arguments);
+    activity.setContentView(R.layout.gears_settings);
   }
 
   public void setup() {
     // First let's add the permissions' resources
-    LOCAL_STORAGE.setResources(R.id.local_storage_choice,
-       R.id.local_storage_allowed,
-       R.id.local_storage_denied);
-
-    LOCATION_DATA.setResources(R.id.location_data_choice,
-       R.id.location_data_allowed,
-       R.id.location_data_denied);
-
+    LOCAL_STORAGE.setResources(R.string.settings_storage_title,
+                               R.string.settings_storage_subtitle_on,
+                               R.string.settings_storage_subtitle_off);
+    LOCATION_DATA.setResources(R.string.settings_location_title,
+                               R.string.settings_location_subtitle_on,
+                               R.string.settings_location_subtitle_off);
     // add the permissions to the list of permissions.
     mPermissions = new Vector<PermissionType>();
     mPermissions.add(LOCAL_STORAGE);
@@ -88,25 +97,7 @@
     OriginPermissions.setListener(this);
 
 
-    inflate(R.layout.gears_dialog_settings, R.id.panel_content);
     setupDialog();
-    setupButtons(0,
-                 R.string.settings_button_allow,
-                 R.string.settings_button_deny);
-
-    // by default disable the allow button (it will get enabled if
-    // something is changed by the user)
-    View buttonView = findViewById(R.id.button_allow);
-    if (buttonView != null) {
-      Button button = (Button) buttonView;
-      button.setEnabled(false);
-    }
-
-    View gearsVersionView = findViewById(R.id.gears_version);
-    if (gearsVersionView != null) {
-      TextView gearsVersion = (TextView) gearsVersionView;
-      gearsVersion.setText(mGearsVersion);
-    }
 
     // We manage the permissions using three vectors, mSitesPermissions,
     // mOriginalPermissions and mCurrentPermissions.
@@ -165,32 +156,23 @@
     View listView = findViewById(R.id.sites_list);
     if (listView != null) {
       ListView list = (ListView) listView;
-      list.setAdapter(new SettingsAdapter(mActivity, mSitesPermissions));
+      mListAdapter = new SettingsAdapter(mActivity, mSitesPermissions);
+      list.setAdapter(mListAdapter);
       list.setScrollBarStyle(android.view.View.SCROLLBARS_OUTSIDE_INSET);
+      list.setOnItemClickListener(mListAdapter);
     }
     if (mDebug) {
       printPermissions();
     }
   }
 
+  private void setMainTitle() {
+    String windowTitle = mActivity.getString(R.string.pref_extras_gears_settings);
+    mActivity.setTitle(windowTitle);
+  }
+
   public void setupDialog() {
-    View dialogTitleView = findViewById(R.id.dialog_title);
-    if (dialogTitleView != null) {
-      TextView dialogTitle = (TextView) dialogTitleView;
-      dialogTitle.setText(R.string.settings_title);
-      dialogTitle.setVisibility(View.VISIBLE);
-    }
-    View dialogSubtitleView = findViewById(R.id.dialog_subtitle);
-    if (dialogSubtitleView != null) {
-      TextView dialogSubtitle = (TextView) dialogSubtitleView;
-      dialogSubtitle.setText(R.string.settings_message);
-      dialogSubtitle.setVisibility(View.VISIBLE);
-    }
-    View iconView = findViewById(R.id.icon);
-    if (iconView != null) {
-      ImageView icon = (ImageView) iconView;
-      icon.setImageResource(R.drawable.gears_icon_32x32);
-    }
+    setMainTitle();
   }
 
   /**
@@ -198,164 +180,95 @@
    */
   public boolean setPermission(PermissionType type, int perm) {
     if (mChanges == false) {
-      signalChanges();
+      mChanges = true;
     }
     return mChanges;
   }
 
-  /**
-   * Controller class for binding the model (OriginPermissions) with
-   * the UI.
-   */
-  class PermissionController {
-    final static int ALLOWED_BUTTON = 1;
-    final static int DENIED_BUTTON = 2;
-    private int mButtonType;
-    private PermissionType mPermissionType;
-    private OriginPermissions mPermissions;
-
-    PermissionController(PermissionType permissionType, int buttonType,
-        OriginPermissions permissions) {
-      mPermissionType = permissionType;
-      mButtonType = buttonType;
-      mPermissions = permissions;
-    }
-
-    public boolean isChecked() {
-      boolean checked = false;
-
-      switch (mButtonType) {
-        case ALLOWED_BUTTON:
-          if (mPermissions.getPermission(mPermissionType) ==
-              PermissionType.PERMISSION_ALLOWED) {
-            checked = true;
-          } break;
-        case DENIED_BUTTON:
-          if (mPermissions.getPermission(mPermissionType) ==
-              PermissionType.PERMISSION_DENIED) {
-            checked = true;
-          }
-      }
-      return checked;
-    }
-
-    public String print() {
-        return printType() + " for " + mPermissions.getOrigin();
-    }
-
-    private String printType() {
-      switch (mButtonType) {
-        case ALLOWED_BUTTON:
-          return "ALLOWED_BUTTON";
-        case DENIED_BUTTON:
-          return "DENIED_BUTTON";
-      }
-      return "UNKNOWN BUTTON";
-    }
-
-    public void changed(boolean isChecked) {
-      if (isChecked == isChecked()) {
-        return; // already set
-      }
-
-      switch (mButtonType) {
-        case ALLOWED_BUTTON:
-          mPermissions.setPermission(mPermissionType,
-              PermissionType.PERMISSION_ALLOWED);
-          break;
-        case DENIED_BUTTON:
-          mPermissions.setPermission(mPermissionType,
-              PermissionType.PERMISSION_DENIED);
-          break;
-      }
-    }
+  public boolean handleBackButton() {
+    return mListAdapter.backButtonPressed();
   }
 
-
+  /**
+   * We use this to create a confirmation dialog when the user
+   * clicks on "remove this site from gears"
+   */
+  public Dialog onCreateDialog(int id) {
+    return new AlertDialog.Builder(mActivity)
+        .setTitle(R.string.settings_confirmation_remove_title)
+        .setMessage(R.string.settings_confirmation_remove)
+        .setPositiveButton(android.R.string.ok,
+                           new AlertDialog.OnClickListener() {
+          public void onClick(DialogInterface dlg, int which) {
+            mListAdapter.removeCurrentSite();
+          }
+        })
+        .setNegativeButton(android.R.string.cancel, null)
+        .setIcon(android.R.drawable.ic_dialog_alert)
+        .create();
+  }
 
   /**
    * Adapter class for the list view in the settings dialog
    *
-   * Every row in the settings dialog display the permissions
-   * for a given origin. For every type of permission
-   * (location, local data...) there is two radio buttons to
-   * authorize or deny the permission.
-   * A remove button is also present to let the user remove
-   * all the authorization of an origin in one step.
+   * We first display a list of all the origins (sites), or
+   * a message saying that no permission is set if the list is empty.
+   * When the user click on one of the origin, we then display
+   * the list of the permissions existing for that origin.
+   * Each permission can be either allowed or denied by clicking
+   * on the checkbox.
+   * The last row is a special case, allowing to remove the entire origin.
    */
-  class SettingsAdapter extends ArrayAdapter {
+  class SettingsAdapter extends BaseAdapter
+      implements AdapterView.OnItemClickListener {
     private Activity mContext;
     private List mItems;
+    private OriginPermissions mCurrentSite;
+    private Vector mCurrentPermissions;
+    private int MAX_ROW_HEIGHT = 64;
 
     SettingsAdapter(Activity context, List items) {
-      super(context, R.layout.gears_dialog_settings_row, items);
       mContext = context;
       mItems = items;
+      mCurrentSite = null;
     }
 
-    /*
-     * setup the necessary listeners for the radiobuttons
-     * When the buttons are clicked the permissions change.
-     */
-    private void createAndSetButtonListener(View buttonView,
-        OriginPermissions perms, PermissionType permissionType,
-        int buttonType) {
-      if (buttonView == null) {
-        return;
-      }
-      RadioButton button = (RadioButton) buttonView;
-
-      button.setOnCheckedChangeListener(null);
-      PermissionController p = new PermissionController(permissionType,
-          buttonType, perms);
-      button.setTag(p);
-
-      CompoundButton.OnCheckedChangeListener listener =
-          new CompoundButton.OnCheckedChangeListener() {
-        public void onCheckedChanged(CompoundButton buttonView,
-            boolean isChecked) {
-          PermissionController perm = (PermissionController)buttonView.getTag();
-          perm.changed(isChecked);
+    public int getCount() {
+      if (mCurrentSite == null) {
+        int size = mItems.size();
+        if (size == 0) {
+          return 1;
+        } else {
+          return size;
         }
-      };
-
-      button.setOnCheckedChangeListener(listener);
-
-      if (p.isChecked() != button.isChecked()) {
-        button.setChecked(p.isChecked());
       }
+      return mCurrentPermissions.size() + 1;
     }
 
-    /*
-     * setup the remove button for an origin: each row has a global
-     * remove button in addition to the radio buttons controlling the
-     * permissions.
-     */
-    private void setRemoveButton(Button button, OriginPermissions perms) {
-      Button.OnClickListener listener = new Button.OnClickListener() {
-        public void onClick(View buttonView) {
-          if (mChanges == false) {
-            signalChanges();
-          }
-          OriginPermissions perm = (OriginPermissions) buttonView.getTag();
-          perm.setPermission(LOCAL_STORAGE, PermissionType.PERMISSION_NOT_SET);
-          perm.setPermission(LOCATION_DATA, PermissionType.PERMISSION_NOT_SET);
-          mSitesPermissions.remove(perm);
+    public long getItemId(int position) {
+      return position;
+    }
 
-          View view = findViewById(R.id.sites_list);
-          if (view != null) {
-            ListView listView = (ListView) view;
-            ListAdapter listAdapter = listView.getAdapter();
-            if (listAdapter != null) {
-              SettingsAdapter settingsAdapter = (SettingsAdapter) listAdapter;
-              settingsAdapter.notifyDataSetChanged();
-            }
-          }
+    private String shortName(String url) {
+        // We remove the http and https prefix
+        if (url.startsWith("http://")) {
+          return url.substring(7);
         }
-      };
-      button.setTag(perms);
-      button.setOnClickListener(listener);
-      displayAsLink(button);
+        if (url.startsWith("https://")) {
+          return url.substring(8);
+        }
+        return url;
+    }
+
+    public Object getItem(int position) {
+      if (mCurrentSite == null) {
+        if (mItems.size() == 0) {
+          return null;
+        } else {
+          return mItems.get(position);
+        }
+      }
+      return mCurrentPermissions.get(position);
     }
 
     public View getView(int position, View convertView, ViewGroup parent) {
@@ -363,46 +276,117 @@
       if (row == null) { // no cached view, we create one
         LayoutInflater inflater = (LayoutInflater) getSystemService(
             Context.LAYOUT_INFLATER_SERVICE);
-        row = inflater.inflate(R.layout.gears_dialog_settings_row, null);
+        row = inflater.inflate(R.layout.gears_settings_row, null);
       }
+      row.setMinimumHeight(MAX_ROW_HEIGHT);
 
-      OriginPermissions perms = (OriginPermissions) mItems.get(position);
+      if (mCurrentSite == null) {
+        if (mItems.size() == 0) {
+          hideView(row, R.id.title);
+          hideView(row, R.id.subtitle);
+          hideView(row, R.id.checkbox);
+          hideView(row, R.id.icon);
+          setText(row, R.id.info, R.string.settings_empty);
+        } else {
+          hideView(row, R.id.subtitle);
+          hideView(row, R.id.info);
+          hideView(row, R.id.checkbox);
+          OriginPermissions perms = (OriginPermissions) mItems.get(position);
+          setText(row, R.id.title, shortName(perms.getOrigin()));
+          showView(row, R.id.icon);
+        }
+      } else {
+        if (position == getCount() - 1) {
+          // last position: "remove this site from gears"
+          hideView(row, R.id.subtitle);
+          hideView(row, R.id.info);
+          hideView(row, R.id.checkbox);
+          hideView(row, R.id.icon);
+          setText(row, R.id.title, R.string.settings_remove_site);
+        } else {
+          hideView(row, R.id.info);
+          hideView(row, R.id.icon);
+          showView(row, R.id.checkbox);
 
-      View nameView = row.findViewById(R.id.origin_name);
-      if (nameView != null) {
-        TextView originName = (TextView) nameView;
-        originName.setText(perms.getOrigin());
-      }
+          PermissionType type =
+              (PermissionType) mCurrentPermissions.get(position);
+          setText(row, R.id.title, type.getTitleRsc());
 
-      View removeButtonView = row.findViewById(R.id.origin_remove);
-      if (removeButtonView != null) {
-        Button removeButton = (Button) removeButtonView;
-        setRemoveButton(removeButton, perms);
-      }
-
-      for (int i = 0; i < mPermissions.size(); i++) {
-        PermissionType type = mPermissions.get(i);
-        int rowRsc = type.getRowRsc();
-        int allowedButtonRsc = type.getAllowedButtonRsc();
-        int deniedButtonRsc = type.getDeniedButtonRsc();
-
-        View rowView = row.findViewById(rowRsc);
-        if (rowView != null) {
-          int perm = perms.getPermission(type);
-          if (perm != PermissionType.PERMISSION_NOT_SET) {
-            createAndSetButtonListener(row.findViewById(allowedButtonRsc),
-                perms, type, PermissionController.ALLOWED_BUTTON);
-            createAndSetButtonListener(row.findViewById(deniedButtonRsc),
-                perms, type, PermissionController.DENIED_BUTTON);
-            rowView.setVisibility(View.VISIBLE);
-          } else {
-            rowView.setVisibility(View.GONE);
+          View checkboxView = row.findViewById(R.id.checkbox);
+          if (checkboxView != null) {
+            CheckBox checkbox = (CheckBox) checkboxView;
+            int perm = mCurrentSite.getPermission(type);
+            if (perm == PermissionType.PERMISSION_DENIED) {
+              setText(row, R.id.subtitle, type.getSubtitleOffRsc());
+              checkbox.setChecked(false);
+            } else {
+              setText(row, R.id.subtitle, type.getSubtitleOnRsc());
+              checkbox.setChecked(true);
+            }
           }
         }
       }
-
       return row;
     }
+
+    public void removeCurrentSite() {
+      mCurrentSite.setPermission(LOCAL_STORAGE,
+                                 PermissionType.PERMISSION_NOT_SET);
+      mCurrentSite.setPermission(LOCATION_DATA,
+                                 PermissionType.PERMISSION_NOT_SET);
+      mSitesPermissions.remove(mCurrentSite);
+      mCurrentSite = null;
+      setMainTitle();
+      notifyDataSetChanged();
+    }
+
+    public void onItemClick(AdapterView<?> parent,
+                            View view,
+                            int position,
+                            long id) {
+      if (mItems.size() == 0) {
+        return;
+      }
+      if (mCurrentSite == null) {
+         mCurrentSite = (OriginPermissions) mItems.get(position);
+         mCurrentPermissions = new Vector();
+         for (int i = 0; i < mPermissions.size(); i++) {
+           PermissionType type = mPermissions.get(i);
+           int perm = mCurrentSite.getPermission(type);
+           if (perm != PermissionType.PERMISSION_NOT_SET) {
+             mCurrentPermissions.add(type);
+           }
+         }
+         mContext.setTitle(shortName(mCurrentSite.getOrigin()));
+      } else {
+        if (position == getCount() - 1) { // last item (remove site)
+          // Ask the user to confirm
+          // If yes, removeCurrentSite() will be called via the dialog callback.
+          mActivity.showDialog(CONFIRMATION_REMOVE_DIALOG);
+        } else {
+          PermissionType type =
+              (PermissionType) mCurrentPermissions.get(position);
+          if (mCurrentSite.getPermission(type) ==
+              PermissionType.PERMISSION_ALLOWED) {
+            mCurrentSite.setPermission(type, PermissionType.PERMISSION_DENIED);
+          } else {
+            mCurrentSite.setPermission(type, PermissionType.PERMISSION_ALLOWED);
+          }
+        }
+      }
+      notifyDataSetChanged();
+    }
+
+    public boolean backButtonPressed() {
+      if (mCurrentSite != null) { // we intercept the back button
+        mCurrentSite = null;
+        setMainTitle();
+        notifyDataSetChanged();
+        return true;
+      }
+      return false;
+    }
+
   }
 
   /**
@@ -423,21 +407,6 @@
   }
 
   /**
-   * Utility method used by the settings dialog, signaling
-   * the user the settings have been modified.
-   * We reflect this by enabling the Allow button (disabled
-   * by default).
-   */
-  public void signalChanges() {
-    View view = findViewById(R.id.button_allow);
-    if (view != null) {
-      Button button = (Button) view;
-      button.setEnabled(true);
-    }
-    mChanges = true;
-  }
-
-  /**
    * Computes the difference between the original permissions and the
    * current ones. Returns a json-formatted string.
    * It is used by the Settings dialog.
@@ -479,18 +448,7 @@
   }
 
   public String closeDialog(int closingType) {
-    String ret = null;
-    switch (closingType) {
-      case ALWAYS_DENY:
-        ret = "{\"allow\": false }";
-        break;
-      case ALLOW:
-        ret = computeDiff(true);
-        break;
-      case DENY:
-        ret = computeDiff(false);
-        break;
-    }
+    String ret = computeDiff(mChanges);
 
     if (mDebug) {
       printPermissions();