Merge "AutoFill Profile editor UI initial checkin"
diff --git a/res/layout/autofill_settings_fragment.xml b/res/layout/autofill_settings_fragment.xml
new file mode 100644
index 0000000..35dfbe6
--- /dev/null
+++ b/res/layout/autofill_settings_fragment.xml
@@ -0,0 +1,71 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2010 The Android Open Source Project
+
+     Licensed under the Apache License, Version 2.0 (the "License");
+     you may not use this file except in compliance with the License.
+     You may obtain a copy of the License at
+
+          http://www.apache.org/licenses/LICENSE-2.0
+
+     Unless required by applicable law or agreed to in writing, software
+     distributed under the License is distributed on an "AS IS" BASIS,
+     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+     See the License for the specific language governing permissions and
+     limitations under the License.
+-->
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    android:layout_width="match_parent"
+    android:layout_height="match_parent"
+    android:orientation="vertical">
+
+<TextView
+    android:layout_width="wrap_content"
+    android:layout_height="wrap_content"
+    android:textAppearance="?android:attr/textAppearanceLarge"
+    android:text="@string/pref_autofill_profile_editor" />
+
+<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    android:layout_width="match_parent"
+    android:layout_height="wrap_content"
+    android:stretchColumns="1"
+    android:paddingLeft="100dip"
+    android:paddingRight="100dip" >
+
+    <TableRow>
+        <TextView
+            android:layout_width="wrap_content"
+            android:layout_height="wrap_content"
+            android:gravity="center_vertical"
+            android:padding="10dip"
+            android:textAppearance="?android:attr/textAppearanceMedium"
+            android:text="@string/autofill_profile_editor_name" />
+        <EditText android:id="@+id/autofill_profile_editor_name_edit"
+            android:layout_width="match_parent"
+            android:layout_height="wrap_content"
+            android:textAppearance="?android:attr/textAppearanceMedium"
+            android:singleLine="true" />
+    </TableRow>
+    <TableRow>
+        <TextView
+            android:layout_width="wrap_content"
+            android:layout_height="wrap_content"
+            android:gravity="center_vertical"
+            android:padding="10dip"
+            android:textAppearance="?android:attr/textAppearanceMedium"
+            android:text="@string/autofill_profile_editor_email_address" />
+        <EditText android:id="@+id/autofill_profile_editor_email_address_edit"
+            android:layout_width="match_parent"
+            android:layout_height="wrap_content"
+            android:textAppearance="?android:attr/textAppearanceMedium"
+            android:singleLine="true" />
+    </TableRow>
+</TableLayout>
+
+<Button
+    android:id="@+id/autofill_profile_editor_save_button"
+    android:layout_width="wrap_content"
+    android:layout_height="wrap_content"
+    android:text="@string/autofill_profile_editor_save_profile"
+    android:textAppearance="?android:attr/textAppearanceMedium" />
+
+</LinearLayout>
diff --git a/res/values/strings.xml b/res/values/strings.xml
index 54e8f63..29d8703 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -341,6 +341,21 @@
     <!-- Dialog title used when asking the user which Google account they want to use to sync data between Android Browser and Google Chrome [CHAR-LIMIT=20] -->
     <string name="pref_personal_account_dialog_title">Select Google account to share with</string>
 
+    <!-- Checkbox setting for enabling/disabling the form AutoFill feature [CHAR-LIMIT=32] -->
+    <string name="pref_autofill_enabled">Form AutoFill</string>
+    <!-- Settings summary for the form AutoFill feature. [CHAR-LIMIT=none] -->
+    <string name="pref_autofill_enabled_summary">Fill out web forms in a single click</string>
+    <!-- Label for option that when clicked opens the AutoFill settings screen. Also used as the title of that AutoFill Settings screen. [CHAR-LIMIT=32] -->
+    <string name="pref_autofill_profile_editor">AutoFill Settings</string>
+    <!-- Summary for the AutoFill Settings preference [CHAR-LIMIT=none] -->
+    <string name="pref_autofill_profile_editor_summary">Set up &amp; manage data for AutoFilled forms</string>
+    <!-- String for the user's full name in the AutoFill profile editor. [CHAR-LIMIT=32] -->
+    <string name="autofill_profile_editor_name">Full Name:</string>
+    <!-- String for the user's e-mail address in the AutoFill profile editor. [CHAR-LIMIT=32] -->
+    <string name="autofill_profile_editor_email_address">e-mail Address:</string>
+    <!-- Button text to save the AutoFill profile [CHAR-LIMIT=20] -->
+    <string name="autofill_profile_editor_save_profile">Save profile</string>
+
     <!-- Settings screen, section title -->
     <string name="pref_privacy_title">Privacy settings</string>
     <!-- Settings label -->
@@ -393,10 +408,6 @@
     <string name="pref_security_save_form_data">Remember form data</string>
     <!-- Settings summary -->
     <string name="pref_security_save_form_data_summary">Remember data I type in forms for later use</string>
-    <!-- Settings label for enabling/disabling the form AutoFill feature -->
-    <string name="pref_autoFill_enabled">AutoFill forms</string>
-    <!-- Settings summary for the form AutoFill feature. -->
-    <string name="pref_autoFill_enabled_summary">Automatically fill out forms where possible</string>
     <!-- Settings label -->
     <string name="pref_security_show_security_warning">Show security warnings</string>
     <!-- Settings summmary -->
diff --git a/res/xml/browser_preferences.xml b/res/xml/browser_preferences.xml
index cc9b18a..cb4e0b5 100644
--- a/res/xml/browser_preferences.xml
+++ b/res/xml/browser_preferences.xml
@@ -140,10 +140,10 @@
                 android:summary="@string/pref_security_save_form_data_summary" />
 
         <CheckBoxPreference
-                android:key="autoFill_enabled"
+                android:key="autofill_enabled"
                 android:defaultValue="false"
-                android:title="@string/pref_autoFill_enabled"
-                android:summary="@string/pref_autoFill_enabled_summary" />
+                android:title="@string/pref_autofill_enabled"
+                android:summary="@string/pref_autofill_enabled_summary" />
 
         <com.android.browser.BrowserYesNoPreference
                 android:key="privacy_clear_form_data"
diff --git a/res/xml/personal_preferences.xml b/res/xml/personal_preferences.xml
index 14b057c..939d14c 100644
--- a/res/xml/personal_preferences.xml
+++ b/res/xml/personal_preferences.xml
@@ -21,4 +21,15 @@
         android:summary="@string/pref_personal_sync_with_chrome_summary"
     />
 
+    <CheckBoxPreference android:key="autofill_enabled"
+        android:title="@string/pref_autofill_enabled"
+        android:summary="@string/pref_autofill_enabled_summary"
+    />
+
+    <PreferenceScreen
+        android:fragment="com.android.browser.AutoFillSettingsFragment"
+        android:key="autofill_profile"
+        android:title="@string/pref_autofill_profile_editor"
+        android:summary="@string/pref_autofill_profile_editor_summary" />
+
 </PreferenceScreen>
diff --git a/res/xml/privacy_preferences.xml b/res/xml/privacy_preferences.xml
index f8a8d91..a6ea16b 100644
--- a/res/xml/privacy_preferences.xml
+++ b/res/xml/privacy_preferences.xml
@@ -21,16 +21,16 @@
             android:key="privacy_clear_cache"
             android:title="@string/pref_privacy_clear_cache"
             android:summary="@string/pref_privacy_clear_cache_summary"
-            android:dialogMessage="@string/pref_privacy_clear_cache_dlg" 
-            android:dialogTitle="@string/clear" 
+            android:dialogMessage="@string/pref_privacy_clear_cache_dlg"
+            android:dialogTitle="@string/clear"
             android:dialogIcon="@android:drawable/ic_dialog_alert" />
-            
+
     <com.android.browser.BrowserYesNoPreference
             android:key="privacy_clear_history"
             android:title="@string/pref_privacy_clear_history"
             android:summary="@string/pref_privacy_clear_history_summary"
-            android:dialogMessage="@string/pref_privacy_clear_history_dlg" 
-            android:dialogTitle="@string/clear" 
+            android:dialogMessage="@string/pref_privacy_clear_history_dlg"
+            android:dialogTitle="@string/clear"
             android:dialogIcon="@android:drawable/ic_dialog_alert"/>
 
     <CheckBoxPreference
@@ -38,13 +38,13 @@
             android:defaultValue="true"
             android:title="@string/pref_security_accept_cookies"
             android:summary="@string/pref_security_accept_cookies_summary" />
-            
+
     <com.android.browser.BrowserYesNoPreference
             android:key="privacy_clear_cookies"
             android:title="@string/pref_privacy_clear_cookies"
             android:summary="@string/pref_privacy_clear_cookies_summary"
-            android:dialogMessage="@string/pref_privacy_clear_cookies_dlg" 
-            android:dialogTitle="@string/clear" 
+            android:dialogMessage="@string/pref_privacy_clear_cookies_dlg"
+            android:dialogTitle="@string/clear"
             android:dialogIcon="@android:drawable/ic_dialog_alert"/>
 
     <CheckBoxPreference
@@ -53,18 +53,12 @@
             android:title="@string/pref_security_save_form_data"
             android:summary="@string/pref_security_save_form_data_summary" />
 
-    <CheckBoxPreference
-            android:key="autoFill_enabled"
-            android:defaultValue="false"
-            android:title="@string/pref_autoFill_enabled"
-            android:summary="@string/pref_autoFill_enabled_summary" />
-
     <com.android.browser.BrowserYesNoPreference
             android:key="privacy_clear_form_data"
             android:title="@string/pref_privacy_clear_form_data"
             android:summary="@string/pref_privacy_clear_form_data_summary"
-            android:dialogMessage="@string/pref_privacy_clear_form_data_dlg" 
-            android:dialogTitle="@string/clear" 
+            android:dialogMessage="@string/pref_privacy_clear_form_data_dlg"
+            android:dialogTitle="@string/clear"
             android:dialogIcon="@android:drawable/ic_dialog_alert"/>
 
     <CheckBoxPreference
diff --git a/src/com/android/browser/AutoFillProfileDatabase.java b/src/com/android/browser/AutoFillProfileDatabase.java
new file mode 100644
index 0000000..e3d6ea2
--- /dev/null
+++ b/src/com/android/browser/AutoFillProfileDatabase.java
@@ -0,0 +1,101 @@
+/*
+ * Copyright (C) 2010 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.browser;
+
+import android.content.Context;
+import android.database.Cursor;
+import android.database.sqlite.SQLiteDatabase;
+import android.database.sqlite.SQLiteOpenHelper;
+import android.provider.BaseColumns;
+import android.util.Log;
+
+public class AutoFillProfileDatabase {
+
+    static final String LOGTAG = "AutoFillProfileDatabase";
+
+    static final String DATABASE_NAME = "autofill.db";
+    static final int DATABASE_VERSION = 1;
+    static final String PROFILES_TABLE_NAME = "profiles";
+    private AutoFillProfileDatabaseHelper mOpenHelper;
+    private static AutoFillProfileDatabase sInstance;
+
+    public static final class Profiles implements BaseColumns {
+        private Profiles() { }
+
+        static final String FULL_NAME = "fullname";
+        static final String EMAIL_ADDRESS = "email";
+    }
+
+    private static class AutoFillProfileDatabaseHelper extends SQLiteOpenHelper {
+        AutoFillProfileDatabaseHelper(Context context) {
+             super(context, DATABASE_NAME, null, DATABASE_VERSION);
+        }
+
+        @Override
+        public void onCreate(SQLiteDatabase db) {
+            db.execSQL("CREATE TABLE " + PROFILES_TABLE_NAME + " ("
+                    + Profiles._ID + " INTEGER PRIMARY KEY,"
+                    + Profiles.FULL_NAME + " TEXT,"
+                    + Profiles.EMAIL_ADDRESS + " TEXT"
+                    + " );");
+        }
+
+        @Override
+        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
+            Log.w(LOGTAG, "Upgrading database from version " + oldVersion + " to "
+                    + newVersion + ", which will destroy all old data");
+            db.execSQL("DROP TABLE IF EXISTS " + PROFILES_TABLE_NAME);
+            onCreate(db);
+        }
+    }
+
+    private AutoFillProfileDatabase(Context context) {
+        mOpenHelper = new AutoFillProfileDatabaseHelper(context);
+    }
+
+    public static AutoFillProfileDatabase getInstance(Context context) {
+        if (sInstance == null) {
+            sInstance = new AutoFillProfileDatabase(context);
+        }
+        return sInstance;
+    }
+
+    private SQLiteDatabase getDatabase(boolean writable) {
+        return writable ? mOpenHelper.getWritableDatabase() : mOpenHelper.getReadableDatabase();
+    }
+
+    public void addOrUpdateProfile(final int id, final String fullName, final String email) {
+        final String SQL = "INSERT OR REPLACE INTO " + PROFILES_TABLE_NAME + " ("
+                + Profiles._ID + ","
+                + Profiles.FULL_NAME + ","
+                + Profiles.EMAIL_ADDRESS
+                + ") VALUES (?,?,?);";
+        final Object[] PARAMS = {id, fullName, email};
+        getDatabase(true).execSQL(SQL, PARAMS);
+    }
+
+    public Cursor getProfile(int id) {
+        final String[] COLS = {Profiles.FULL_NAME, Profiles.EMAIL_ADDRESS };
+        final String[] SEL_ARGS = { Integer.toString(id) };
+        return getDatabase(false).query(PROFILES_TABLE_NAME, COLS, Profiles._ID + "=?", SEL_ARGS,
+                null, null, null, "1");
+    }
+
+    public void close() {
+        mOpenHelper.close();
+    }
+}
diff --git a/src/com/android/browser/AutoFillSettingsFragment.java b/src/com/android/browser/AutoFillSettingsFragment.java
new file mode 100644
index 0000000..b650ede
--- /dev/null
+++ b/src/com/android/browser/AutoFillSettingsFragment.java
@@ -0,0 +1,142 @@
+/*
+ * Copyright (C) 2010 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.browser;
+
+import android.app.Fragment;
+import android.database.Cursor;
+import android.database.sqlite.SQLiteDatabase;
+import android.os.AsyncTask;
+import android.os.Bundle;
+import android.util.Log;
+import android.view.View;
+import android.view.ViewGroup;
+import android.view.View.OnClickListener;
+import android.view.LayoutInflater;
+import android.widget.Button;
+import android.widget.EditText;
+import android.widget.Toast;
+
+public class AutoFillSettingsFragment extends Fragment {
+
+    private static final String LOGTAG = "AutoFillSettingsFragment";
+
+    // TODO: This will become dynamic once we support more than one profile.
+    private int mProfileId = 1;
+
+    public AutoFillSettingsFragment() {
+
+    }
+
+    @Override
+    public void onCreate(Bundle savedState) {
+        super.onCreate(savedState);
+    }
+
+    @Override
+    public View onCreateView(LayoutInflater inflater, ViewGroup container,
+                Bundle savedInstanceState) {
+        View v = inflater.inflate(R.layout.autofill_settings_fragment, container, false);
+
+        Button saveButton = (Button)v.findViewById(R.id.autofill_profile_editor_save_button);
+        saveButton.setOnClickListener(new OnClickListener() {
+            public void onClick(View button) {
+                View v = getView();
+                EditText fullName = (EditText)v.findViewById(
+                        R.id.autofill_profile_editor_name_edit);
+                EditText email = (EditText)v.findViewById(
+                        R.id.autofill_profile_editor_email_address_edit);
+                new SaveProfileToDbTask().execute(fullName.getText().toString(),
+                        email.getText().toString());
+            }
+        });
+
+        // Load the profile and populate the text views in the background
+        new LoadProfileFromDbTask().execute(mProfileId);
+
+        return v;
+    }
+
+    @Override
+    public void onPause() {
+        AutoFillProfileDatabase db =
+                AutoFillProfileDatabase.getInstance(getActivity());
+        db.close();
+        super.onPause();
+    }
+
+    private class SaveProfileToDbTask extends AsyncTask<String, Void, Void> {
+        protected Void doInBackground(String... values) {
+            AutoFillProfileDatabase db =
+                    AutoFillProfileDatabase.getInstance(getActivity());
+            db.addOrUpdateProfile(mProfileId, values[0], values[1]);
+            return null;
+        }
+
+        protected void onPostExecute(Void result) {
+            Toast.makeText(getActivity(), "Saved profile", Toast.LENGTH_SHORT).show();
+        }
+    }
+
+    private static class LoadedProfileData {
+        private String mFullName;
+        private String mEmailAddress;
+
+        public LoadedProfileData(String fullName, String emailAddress) {
+            mFullName = fullName;
+            mEmailAddress = emailAddress;
+        }
+
+        public String getFullName() { return mFullName; }
+        public String getEmailAddress() { return mEmailAddress; }
+    }
+
+    private class LoadProfileFromDbTask extends AsyncTask<Integer, Void, LoadedProfileData> {
+        protected LoadedProfileData doInBackground(Integer... id) {
+            AutoFillProfileDatabase db = AutoFillProfileDatabase.getInstance(getActivity());
+            Cursor c = db.getProfile(id[0]);
+            c.moveToFirst();
+
+            LoadedProfileData profileData = null;
+
+            if (c.getCount() > 0) {
+                String fullName = c.getString(c.getColumnIndex(
+                        AutoFillProfileDatabase.Profiles.FULL_NAME));
+                String email = c.getString(c.getColumnIndex(
+                        AutoFillProfileDatabase.Profiles.EMAIL_ADDRESS));
+                profileData =  new LoadedProfileData(fullName, email);
+            }
+           c.close();
+           return profileData;
+        }
+
+        protected void onPostExecute(LoadedProfileData data) {
+            if (data == null) {
+                return;
+            }
+
+            View v = getView();
+            if (v != null) {
+                EditText fullName = (EditText)v.findViewById(
+                        R.id.autofill_profile_editor_name_edit);
+                EditText email = (EditText)v.findViewById(
+                        R.id.autofill_profile_editor_email_address_edit);
+                fullName.setText(data.getFullName());
+                email.setText(data.getEmailAddress());
+            }
+        }
+    }
+}
diff --git a/src/com/android/browser/BrowserSettings.java b/src/com/android/browser/BrowserSettings.java
index dfec48f..4c798a0 100644
--- a/src/com/android/browser/BrowserSettings.java
+++ b/src/com/android/browser/BrowserSettings.java
@@ -147,6 +147,8 @@
             "default_text_encoding";
     public final static String PREF_CLEAR_GEOLOCATION_ACCESS =
             "privacy_clear_geolocation_access";
+    public final static String PREF_AUTOFILL_ENABLED = "autofill_enabled";
+    public final static String PREF_AUTOFILL_PROFILE = "autofill_profile";
 
     private static final String DESKTOP_USERAGENT = "Mozilla/5.0 (Macintosh; " +
             "U; Intel Mac OS X 10_6_3; en-us) AppleWebKit/533.16 (KHTML, " +
@@ -340,7 +342,7 @@
                 rememberPasswords);
         saveFormData = p.getBoolean("save_formdata",
                 saveFormData);
-        autoFillEnabled = p.getBoolean("autoFill_enabled", autoFillEnabled);
+        autoFillEnabled = p.getBoolean("autofill_enabled", autoFillEnabled);
         boolean accept_cookies = p.getBoolean("accept_cookies",
                 CookieManager.getInstance().acceptCookie());
         CookieManager.getInstance().setAcceptCookie(accept_cookies);
diff --git a/src/com/android/browser/preferences/PersonalPreferencesFragment.java b/src/com/android/browser/preferences/PersonalPreferencesFragment.java
index 12751c5..f8a7563 100644
--- a/src/com/android/browser/preferences/PersonalPreferencesFragment.java
+++ b/src/com/android/browser/preferences/PersonalPreferencesFragment.java
@@ -17,6 +17,7 @@
 package com.android.browser.preferences;
 
 import com.android.browser.BrowserBookmarksPage;
+import com.android.browser.BrowserSettings;
 import com.android.browser.R;
 
 import android.accounts.Account;
@@ -39,6 +40,7 @@
 import android.preference.Preference.OnPreferenceClickListener;
 import android.preference.PreferenceFragment;
 import android.preference.PreferenceManager;
+import android.preference.PreferenceScreen;
 import android.provider.BrowserContract;
 import android.provider.BrowserContract.Bookmarks;
 import android.provider.BrowserContract.ChromeSyncColumns;
@@ -101,6 +103,10 @@
             }
             mChromeSync.setOnPreferenceClickListener(this);
         }
+
+        PreferenceScreen autoFillSettings =
+                (PreferenceScreen)findPreference(BrowserSettings.PREF_AUTOFILL_PROFILE);
+        autoFillSettings.setDependency(BrowserSettings.PREF_AUTOFILL_ENABLED);
     }
 
     @Override