[MDM] Added restrictionEnable to AutoFill restriction

THe Chromium policy document had a single controlling key for
Autofill, and I attempted to emulate it.  Unfortunately, a side-effect
was reported in the non-mdm browser.

The Fix was to go ahead and implement a second key (as we do for other
restrictions), restrictionEnable.  As a bonus, the enable logic for the actual
value is no longer reversed, therfore les confusing.

Change-Id: Ib074bf3efa79bded3fca246bf63824d640c26954
diff --git a/src/com/android/browser/mdm/AutoFillRestriction.java b/src/com/android/browser/mdm/AutoFillRestriction.java
index e0e516d..6fe9bbc 100644
--- a/src/com/android/browser/mdm/AutoFillRestriction.java
+++ b/src/com/android/browser/mdm/AutoFillRestriction.java
@@ -42,11 +42,14 @@
 
     private final static String TAG = "AutoFillRestriction";
 
-    public static final String AUTO_FILL_RESTRICTION = "AutoFillEnabled";
+    public static final String AUTO_FILL_RESTRICTION_ENABLED = "AutoFillRestrictionEnabled";
+    public static final String AUTO_FILL_ALLOWED = "AutoFillAllowed";
 
     private static AutoFillRestriction sInstance;
     private MdmCheckBoxPreference mPref = null;
 
+    private boolean m_bAfAllowed;
+
     private AutoFillRestriction() {
         super(TAG);
     }
@@ -73,36 +76,38 @@
     private void updatePref() {
         if (null != mPref) {
             if (isEnabled()) {
-                mPref.setChecked(false);
+                mPref.setChecked(getValue());
                 mPref.disablePref();
             }
             else {
-                mPref.setChecked(true);
                 mPref.enablePref();
             }
             mPref.setMdmRestrictionState(isEnabled());
         }
     }
 
-    /*
-     *   Note reversed logic:
-     *       [x] 'Restrict' true  = AutoFillEnabled : false   => disable Auto fill in swe
-     *       [ ] 'Restrict' false = AutoFillEnabled : true    => enable Auto fill in swe
-     */
     @Override
     public void enforce(Bundle restrictions) {
         SharedPreferences.Editor editor = BrowserSettings.getInstance().getPreferences().edit();
 
-        boolean bEnable = false;
-        if (restrictions.containsKey(AUTO_FILL_RESTRICTION)) {
-            bEnable = ! restrictions.getBoolean(AUTO_FILL_RESTRICTION);
+        boolean restrictionEnabled = restrictions.getBoolean(AUTO_FILL_RESTRICTION_ENABLED, false);
+        enable(restrictionEnabled);
+
+        if(isEnabled()) {
+            m_bAfAllowed = true;
+            if (restrictions.containsKey(AUTO_FILL_ALLOWED)) {
+                m_bAfAllowed = restrictions.getBoolean(AUTO_FILL_ALLOWED);
+            }
+
+            Log.i(TAG, "Enforce [" + m_bAfAllowed + "]");
+
+            editor.putBoolean(PREF_AUTOFILL_ENABLED, m_bAfAllowed);
+            editor.apply();
         }
-        Log.i(TAG, "Enforce [" + bEnable + "]");
-        enable(bEnable);
-
-        editor.putBoolean(PREF_AUTOFILL_ENABLED, !isEnabled());
-        editor.apply();
-
         updatePref();
     }
+
+    public boolean getValue() {
+        return m_bAfAllowed;
+    }
 }
diff --git a/src/com/android/browser/mdm/tests/AutoFillRestrictionsTest.java b/src/com/android/browser/mdm/tests/AutoFillRestrictionsTest.java
index 291f6fe..fc1f35a 100644
--- a/src/com/android/browser/mdm/tests/AutoFillRestrictionsTest.java
+++ b/src/com/android/browser/mdm/tests/AutoFillRestrictionsTest.java
@@ -37,7 +37,6 @@
 
 import com.android.browser.BrowserActivity;
 import com.android.browser.mdm.AutoFillRestriction;
-import com.android.browser.mdm.EditBookmarksRestriction;
 import com.android.browser.mdm.ManagedProfileManager;
 
 public class AutoFillRestrictionsTest extends ActivityInstrumentationTestCase2<BrowserActivity> {
@@ -65,27 +64,35 @@
         clearAutoFillRestrictions();
         assertFalse(autoFillRestriction.isEnabled());
 
-        setAutoFillRestrictions(true);
+        setAutoFillRestrictions(true, true);
         assertTrue(autoFillRestriction.isEnabled());
+        assertTrue(autoFillRestriction.getValue());
 
-        setAutoFillRestrictions(false);
+        setAutoFillRestrictions(true, false);
+        assertTrue(autoFillRestriction.isEnabled());
+        assertFalse(autoFillRestriction.getValue());
+
+        setAutoFillRestrictions(false, true);
+        assertFalse(autoFillRestriction.isEnabled());
+
+        setAutoFillRestrictions(false, false);
         assertFalse(autoFillRestriction.isEnabled());
     }
 
     /**
     * Activate EditBookmarks restriction
      * @param clear    if true, sends an empty bundle (which is interpreted as "allow editing"
-     * @param enabled  Required. true (disallow editing: restriction enforced)
-     *                        or false (allow editing: restriction lifted)
+     * @param restrictionEnabled Enables the restriction
+     * @param allowed  Required. true  : allow editing
+     *                        or false : disallow editing
     *
     */
-    private void setAutoFillRestrictions(boolean clear, boolean enabled) {
+    private void setAutoFillRestrictions(boolean clear, boolean restrictionEnabled, boolean allowed) {
         final Bundle restrictions = new Bundle();
 
         if (!clear) {
-            // note reversed logic. This is setting 'EditBookmarksEnabled'
-            //    if enabled is true, we want it set to false and vice cersa
-            restrictions.putBoolean(AutoFillRestriction.AUTO_FILL_RESTRICTION, ! enabled);
+            restrictions.putBoolean(AutoFillRestriction.AUTO_FILL_RESTRICTION_ENABLED, restrictionEnabled);
+            restrictions.putBoolean(AutoFillRestriction.AUTO_FILL_ALLOWED, allowed);
         }
 
         // Deliver restriction on UI thread
@@ -101,10 +108,10 @@
     }
 
     private void clearAutoFillRestrictions() {
-        setAutoFillRestrictions(true, false);
+        setAutoFillRestrictions(true, false, false);
     }
 
-    private void setAutoFillRestrictions(boolean enabled) {
-        setAutoFillRestrictions(false, enabled);
+    private void setAutoFillRestrictions(boolean enabled, boolean allowed) {
+        setAutoFillRestrictions(false, enabled, allowed);
     }
 }