Offer the user the option to configure an AutoFill profile.

When the user selects a form field that is part of a form that can
be autofilled, inform the embedder that this is the case to give
them the opportunity to configure a profile if they the user has
not done so already. A Message is also provided
to the embedder that when sent will trigger the form to be filled
with the profile that the embedder set up.

Change-Id: Ica995e5b1ed92a3ec3e356401b261740d9f90e57
diff --git a/core/java/android/webkit/WebChromeClient.java b/core/java/android/webkit/WebChromeClient.java
index b2ba7e23..755366c 100644
--- a/core/java/android/webkit/WebChromeClient.java
+++ b/core/java/android/webkit/WebChromeClient.java
@@ -330,4 +330,14 @@
      */
     public void setInstallableWebApp() { }
 
+    /**
+     * Tell the client that the page being viewed has an autofillable
+     * form and the user would like to set a profile up.
+     * @param msg A Message to send once the user has successfully
+     *      set up a profile and to inform the WebTextView it should
+     *      now autofill using that new profile.
+     * @hide
+     */
+    public void setupAutoFill(Message msg) { }
+
 }
diff --git a/core/java/android/webkit/WebSettings.java b/core/java/android/webkit/WebSettings.java
index f4caa74..98e6ab8 100644
--- a/core/java/android/webkit/WebSettings.java
+++ b/core/java/android/webkit/WebSettings.java
@@ -1626,6 +1626,13 @@
         }
     }
 
+    /**
+     * @hide
+     */
+    public synchronized AutoFillProfile getAutoFillProfile() {
+        return mAutoFillProfile;
+    }
+
     int getDoubleTapToastCount() {
         return mDoubleTapToastCount;
     }
diff --git a/core/java/android/webkit/WebTextView.java b/core/java/android/webkit/WebTextView.java
index fafb6be..e1a5c2d 100644
--- a/core/java/android/webkit/WebTextView.java
+++ b/core/java/android/webkit/WebTextView.java
@@ -26,6 +26,8 @@
 import android.graphics.PixelFormat;
 import android.graphics.Rect;
 import android.graphics.drawable.Drawable;
+import android.os.Handler;
+import android.os.Message;
 import android.text.BoringLayout.Metrics;
 import android.text.DynamicLayout;
 import android.text.Editable;
@@ -129,6 +131,7 @@
 
     private boolean mAutoFillable; // Is this textview part of an autofillable form?
     private int mQueryId;
+    private boolean mAutoFillProfileIsSet;
 
     // Types used with setType.  Keep in sync with CachedInput.h
     private static final int NORMAL_TEXT_FIELD = 0;
@@ -140,6 +143,9 @@
     private static final int TELEPHONE = 6;
     private static final int URL = 7;
 
+    private static final int AUTOFILL_FORM = 100;
+    private Handler mHandler;
+
     /**
      * Create a new WebTextView.
      * @param   context The Context for this WebTextView.
@@ -163,6 +169,18 @@
         setTextColor(DebugFlags.DRAW_WEBTEXTVIEW ? Color.RED : Color.BLACK);
         // This helps to align the text better with the text in the web page.
         setIncludeFontPadding(false);
+
+        mHandler = new Handler() {
+            @Override
+            public void handleMessage(Message msg) {
+                switch (msg.what) {
+                case AUTOFILL_FORM:
+                    mWebView.autoFillForm(mQueryId);
+                    break;
+                }
+            }
+        };
+
     }
 
     public void setAutoFillable(int queryId) {
@@ -801,8 +819,17 @@
                     if (id == 0 && position == 0) {
                         // Blank out the text box while we wait for WebCore to fill the form.
                         replaceText("");
-                        // Call a webview method to tell WebCore to autofill the form.
-                        mWebView.autoFillForm(mQueryId);
+                        WebSettings settings = mWebView.getSettings();
+                        if (mAutoFillProfileIsSet) {
+                            // Call a webview method to tell WebCore to autofill the form.
+                            mWebView.autoFillForm(mQueryId);
+                        } else {
+                            // There is no autofill profile setup yet and the user has
+                            // elected to try and set one up. Call through to the
+                            // embedder to action that.
+                            mWebView.getWebChromeClient().setupAutoFill(
+                                    mHandler.obtainMessage(AUTOFILL_FORM));
+                        }
                     }
                 }
             });
@@ -1124,4 +1151,8 @@
     /* package */ void updateCachedTextfield() {
         mWebView.updateCachedTextfield(getText().toString());
     }
+
+    /* package */ void setAutoFillProfileIsSet(boolean autoFillProfileIsSet) {
+        mAutoFillProfileIsSet = autoFillProfileIsSet;
+    }
 }
diff --git a/core/java/android/webkit/WebView.java b/core/java/android/webkit/WebView.java
index 602975f..376b1d0 100644
--- a/core/java/android/webkit/WebView.java
+++ b/core/java/android/webkit/WebView.java
@@ -4051,10 +4051,20 @@
                 // Note that code inside the adapter click handler in WebTextView depends
                 // on the AutoFill item being at the top of the drop down list. If you change
                 // the order, make sure to do it there too!
-                pastEntries.add(getResources().getText(
-                        com.android.internal.R.string.autofill_this_form).toString() +
-                        " " +
-                        mAutoFillData.getPreviewString());
+                WebSettings settings = getSettings();
+                if (settings != null && settings.getAutoFillProfile() != null) {
+                    pastEntries.add(getResources().getText(
+                            com.android.internal.R.string.autofill_this_form).toString() +
+                            " " +
+                            mAutoFillData.getPreviewString());
+                    mWebTextView.setAutoFillProfileIsSet(true);
+                } else {
+                    // There is no autofill profile set up yet, so add an option that
+                    // will invite the user to set their profile up.
+                    pastEntries.add(getResources().getText(
+                            com.android.internal.R.string.setup_autofill).toString());
+                    mWebTextView.setAutoFillProfileIsSet(false);
+                }
             }
 
             pastEntries.addAll(mDatabase.getFormData(mUrl, mName));
diff --git a/core/res/res/values/strings.xml b/core/res/res/values/strings.xml
index c8a5de8..0c3361d 100755
--- a/core/res/res/values/strings.xml
+++ b/core/res/res/values/strings.xml
@@ -1824,8 +1824,10 @@
     <!-- Toast for double-tap -->
     <string name="double_tap_toast">Tip: double-tap to zoom in and out.</string>
 
-    <!-- Text to show in the auto complete drop down list on a text view when the browser can auto fill the entire form -->
+    <!-- Text to show in the auto complete drop down list on a text view when the WebView can auto fill the entire form, and the user has configured an AutoFill profile [CHAR-LIMIT=8] -->
     <string name="autofill_this_form">AutoFill</string>
+    <!-- Text to show in the auto complete drop down list on a text view when the WebView can auto fill the entire form but the user has not configured an AutoFill profile [CHAR-LIMIT=16] -->
+    <string name="setup_autofill">Setup AutoFill</string>
 
     <!-- String used to separate FirstName and LastName when writing out a local name
          e.g. John<separator>Smith [CHAR-LIMIT=NONE]-->