Invite the user to set up AutoFill

If the user has not set up an AutoFill profile but has the
feature enabled and they start to fill out a form that we
have determined as "autofillable" then offer to take them
to the profile editor to set up their profile.

Change-Id: Ia44c7036ef616d4ea826e541471dd916262488f2
diff --git a/res/values/strings.xml b/res/values/strings.xml
index d724085..7aa9478 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -393,6 +393,12 @@
     <!-- Button text to delete all the AutoFill profile data [CHAR-LIMIT=20] -->
     <string name="autofill_profile_editor_delete_profile">Delete profile data</string>
 
+    <!-- Text on a dialog shown to the user when they are prompted to set up the autofill feature [CHAR-LIMIT=NONE] -->
+    <string name="autofill_setup_dialog_message">Browser can automatically complete web forms like this one. Would you like to set up your profile?</string>
+    <!-- Toast message displayed when the user decides to not set up autofill at this time. We want to remind them that they can configure
+         it through the Browser Settings menu. [CHAR-LIMIT=NONE] -->
+    <string name="autofill_setup_dialog_negative_toast">AutoFill can always be configured through Browser Settings -&gt; Personal Settings.</string>
+
     <!-- Settings screen, section title -->
     <string name="pref_privacy_title">Privacy settings</string>
     <!-- Settings label -->
diff --git a/src/com/android/browser/BrowserSettings.java b/src/com/android/browser/BrowserSettings.java
index 069f886..ab2ddb2 100644
--- a/src/com/android/browser/BrowserSettings.java
+++ b/src/com/android/browser/BrowserSettings.java
@@ -567,6 +567,13 @@
         ed.apply();
     }
 
+    /* package */ void disableAutoFill(Context ctx) {
+        autoFillEnabled = false;
+        Editor ed = PreferenceManager.getDefaultSharedPreferences(ctx).edit();
+        ed.putBoolean(PREF_AUTOFILL_ENABLED, false);
+        ed.apply();
+    }
+
     /**
      * Add a WebSettings object to the list of observers that will be updated
      * when update() is called.
diff --git a/src/com/android/browser/Controller.java b/src/com/android/browser/Controller.java
index e0b15b4..41e7356 100644
--- a/src/com/android/browser/Controller.java
+++ b/src/com/android/browser/Controller.java
@@ -47,6 +47,7 @@
 import android.os.Message;
 import android.os.PowerManager;
 import android.os.PowerManager.WakeLock;
+import android.preference.PreferenceActivity;
 import android.provider.Browser;
 import android.provider.BrowserContract;
 import android.provider.BrowserContract.History;
@@ -114,6 +115,8 @@
     // activity requestCode
     final static int PREFERENCES_PAGE = 3;
     final static int FILE_SELECTED = 4;
+    final static int AUTOFILL_SETUP = 5;
+
     private final static int WAKELOCK_TIMEOUT = 5 * 60 * 1000; // 5 minutes
 
     // As the ids are dynamically created, we can't guarantee that they will
@@ -143,6 +146,8 @@
     private PageDialogsHandler mPageDialogsHandler;
     private NetworkStateHandler mNetworkHandler;
 
+    private Message mAutoFillSetupMessage;
+
     private boolean mShouldShowErrorConsole;
 
     private SystemAllowGeolocationOrigins mSystemAllowGeolocationOrigins;
@@ -1050,6 +1055,15 @@
                 mUploadHandler.onResult(resultCode, intent);
                 mUploadHandler = null;
                 break;
+            case AUTOFILL_SETUP:
+                // Determine whether a profile was actually set up or not
+                // and if so, send the message back to the WebTextView to
+                // fill the form with the new profile.
+                if (getSettings().getAutoFillProfile() != null) {
+                    mAutoFillSetupMessage.sendToTarget();
+                    mAutoFillSetupMessage = null;
+                }
+                break;
             default:
                 break;
         }
@@ -2409,4 +2423,14 @@
         return mMenuIsDown;
     }
 
+    public void setupAutoFill(Message message) {
+        // Open the settings activity at the AutoFill profile fragment so that
+        // the user can create a new profile. When they return, we will dispatch
+        // the message so that we can autofill the form using their new profile.
+        Intent intent = new Intent(mActivity, BrowserPreferencesPage.class);
+        intent.putExtra(PreferenceActivity.EXTRA_SHOW_FRAGMENT,
+                AutoFillSettingsFragment.class.getName());
+        mAutoFillSetupMessage = message;
+        mActivity.startActivityForResult(intent, AUTOFILL_SETUP);
+    }
 }
diff --git a/src/com/android/browser/Tab.java b/src/com/android/browser/Tab.java
index b12b317..0f8ce6b 100644
--- a/src/com/android/browser/Tab.java
+++ b/src/com/android/browser/Tab.java
@@ -54,6 +54,7 @@
 import android.widget.FrameLayout;
 import android.widget.LinearLayout;
 import android.widget.TextView;
+import android.widget.Toast;
 
 import java.util.ArrayList;
 import java.util.HashMap;
@@ -1061,6 +1062,35 @@
         public void getVisitedHistory(final ValueCallback<String[]> callback) {
             mWebViewController.getVisitedHistory(callback);
         }
+
+        @Override
+        public void setupAutoFill(Message message) {
+            // Prompt the user to set up their profile.
+            final Message msg = message;
+            AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);
+            builder.setMessage(R.string.autofill_setup_dialog_message)
+                   .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
+                       @Override
+                       public void onClick(DialogInterface dialog, int id) {
+                           // Take user to the AutoFill profile editor. When they return,
+                           // we will send the message that we pass here which will trigger
+                           // the form to get filled out with their new profile.
+                           mWebViewController.setupAutoFill(msg);
+                       }
+                   })
+                   .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
+                       @Override
+                       public void onClick(DialogInterface dialog, int id) {
+                           // Disable autofill and show a toast with how to turn it on again.
+                           BrowserSettings s = BrowserSettings.getInstance();
+                           s.addObserver(mMainView.getSettings());
+                           s.disableAutoFill(mActivity);
+                           s.update();
+                           Toast.makeText(mActivity, R.string.autofill_setup_dialog_negative_toast,
+                                   Toast.LENGTH_LONG).show();
+                       }
+                   }).show();
+        }
     };
 
     // -------------------------------------------------------------------------
diff --git a/src/com/android/browser/WebViewController.java b/src/com/android/browser/WebViewController.java
index 8c99c58..eeeee18 100644
--- a/src/com/android/browser/WebViewController.java
+++ b/src/com/android/browser/WebViewController.java
@@ -22,6 +22,7 @@
 import android.graphics.Bitmap;
 import android.net.Uri;
 import android.net.http.SslError;
+import android.os.Message;
 import android.view.KeyEvent;
 import android.view.View;
 import android.webkit.HttpAuthHandler;
@@ -102,4 +103,6 @@
 
     void closeTab(Tab tab);
 
+    void setupAutoFill(Message message);
+
 }