[MDM] Rework Third Party Cookie Restriction for new UI

Previous implementation of the Third Party Cookie (TPC)
Restriction was done before SWE had a UI element to
control this feature. The implementation included a
separate override for the MDM setting since it was
not clear how the TPC preference could/would be changed/set.

Note that the original implementation was functional after
the addition of the UI element because of the 'override'
design, but of course now that there is a UI element, we
need to manage it as well. Since we are now managing
the UI element, it makes sense to align the implementation
with other similar MDM implementations (e.g. Do-Not-Track)
which means we no longer need the 'override' design.

This patch removes the 'override' code, and re-implements
TPC Restrictions in the same manner as other UI bound
features.

Change-Id: I5898409d3f0aa4d5b254d00032feda1d6080bfd1
diff --git a/res/xml/privacy_and_security_preferences.xml b/res/xml/privacy_and_security_preferences.xml
index d5b2f9a..37eeaf8 100644
--- a/res/xml/privacy_and_security_preferences.xml
+++ b/res/xml/privacy_and_security_preferences.xml
@@ -56,7 +56,7 @@
             android:summaryOff="@string/pref_security_not_allowed"
             android:title="@string/pref_security_save_form_data" />
 
-        <SwitchPreference
+        <com.android.browser.mdm.MdmCheckBoxPreference
             android:layout="@layout/swe_preference"
             android:defaultValue="true"
             android:key="accept_third_cookies"
diff --git a/src/com/android/browser/mdm/ThirdPartyCookiesRestriction.java b/src/com/android/browser/mdm/ThirdPartyCookiesRestriction.java
index 86e3103..43bf6bd 100644
--- a/src/com/android/browser/mdm/ThirdPartyCookiesRestriction.java
+++ b/src/com/android/browser/mdm/ThirdPartyCookiesRestriction.java
@@ -31,6 +31,7 @@
 package com.android.browser.mdm;
 
 import android.os.Bundle;
+import android.preference.Preference;
 import android.util.Log;
 
 import org.codeaurora.swe.MdmManager;
@@ -40,10 +41,11 @@
     private final static String TAG = "TPC_Restriction";
 
     public static final String TPC_ENABLED = "ThirdPartyCookiesRestrictionEnabled"; // boolean
-    public static final String TPC_VALUE   = "ThirdPartyCookiesValue";   // boolean
+    public static final String TPC_ALLOWED = "AllowThirdPartyCookies";   // boolean
 
     private static ThirdPartyCookiesRestriction sInstance;
-    private boolean mTpcValue;
+    private boolean mAllowTpc;
+    private MdmCheckBoxPreference mPref = null;
 
     private ThirdPartyCookiesRestriction() {
         super(TAG);
@@ -58,15 +60,41 @@
         return sInstance;
     }
 
+    public void registerPreference (Preference pref) {
+        mPref = (MdmCheckBoxPreference) pref;
+        updatePref();
+    }
+
+    private void updatePref() {
+        if (null != mPref) {
+            if (isEnabled()) {
+                mPref.setChecked(getValue());
+                mPref.disablePref();
+            }
+            else {
+                mPref.enablePref();
+            }
+            mPref.setMdmRestrictionState(isEnabled());
+        }
+    }
+
     @Override
     public void enforce(Bundle restrictions) {
         enable(restrictions.getBoolean(TPC_ENABLED,false));
-        mTpcValue = restrictions.getBoolean(TPC_VALUE, false);
-        Log.i(TAG, "Enforcing. enabled[" + isEnabled() + "]. val[" + mTpcValue + "]");
-        MdmManager.updateMdmThirdPartyCookies(isEnabled(), mTpcValue);
+        mAllowTpc = restrictions.getBoolean(TPC_ALLOWED, true);
+        Log.d(TAG, "Enforcing. enabled[" + isEnabled() + "]. tpc allowed[" + mAllowTpc + "]");
+
+        // Real time update of the Preference if it is registered
+        updatePref();
+
+        if (isEnabled()) {
+            // Logic in native is "should we block?", so we need to
+            // reverse the logic here.
+            MdmManager.updateMdmThirdPartyCookies(!mAllowTpc);
+        }
     }
 
     public boolean getValue() {
-        return mTpcValue;
+        return mAllowTpc;
     }
 }
diff --git a/src/com/android/browser/mdm/tests/ThirdPartyCookiesRestrictionsTest.java b/src/com/android/browser/mdm/tests/ThirdPartyCookiesRestrictionsTest.java
index 230eab8..a77e94a 100644
--- a/src/com/android/browser/mdm/tests/ThirdPartyCookiesRestrictionsTest.java
+++ b/src/com/android/browser/mdm/tests/ThirdPartyCookiesRestrictionsTest.java
@@ -66,7 +66,7 @@
 
         clearTPCRestrictions();
         assertFalse(mTBCRestriction.isEnabled());
-        assertFalse(mTBCRestriction.getValue());
+        assertTrue(mTBCRestriction.getValue()); // default is 'allowed'
 
         setTPCRestrictions(false, true);
         assertFalse(mTBCRestriction.isEnabled());
@@ -88,7 +88,7 @@
      *
      * @param enable boolean. Set the state of the restriction.
      *
-     * @param value boolean. Set the state of Do Not Track if enabled is set to true.
+     * @param value boolean. Set the state of TPC. true == allowed. If enabled is set to true.
      *              we still bundle it, but it should be ignored by the handler.
      */
     private void setTPCRestrictions(boolean clear, boolean enable, boolean value) {
@@ -97,7 +97,7 @@
 
         if(!clear) {
             restrictions.putBoolean(ThirdPartyCookiesRestriction.TPC_ENABLED,enable);
-            restrictions.putBoolean(ThirdPartyCookiesRestriction.TPC_VALUE, value);
+            restrictions.putBoolean(ThirdPartyCookiesRestriction.TPC_ALLOWED, value);
         }
 
         // Deliver restriction on UI thread
diff --git a/src/com/android/browser/preferences/PrivacySecurityPreferencesFragment.java b/src/com/android/browser/preferences/PrivacySecurityPreferencesFragment.java
index 7e02de0..b021916 100644
--- a/src/com/android/browser/preferences/PrivacySecurityPreferencesFragment.java
+++ b/src/com/android/browser/preferences/PrivacySecurityPreferencesFragment.java
@@ -19,6 +19,7 @@
 import com.android.browser.PreferenceKeys;
 import com.android.browser.R;
 import com.android.browser.mdm.DoNotTrackRestriction;
+import com.android.browser.mdm.ThirdPartyCookiesRestriction;
 
 import android.app.ActionBar;
 import android.app.Activity;
@@ -76,6 +77,8 @@
         // Register Preference objects with their MDM restriction handlers
         DoNotTrackRestriction.getInstance().
                 registerPreference(findPreference(PreferenceKeys.PREF_DO_NOT_TRACK));
+        ThirdPartyCookiesRestriction.getInstance().
+                registerPreference(findPreference("accept_third_cookies"));
     }
 
     @Override
@@ -84,6 +87,7 @@
 
         // Un-register Preference objects from their MDM restriction handlers
         DoNotTrackRestriction.getInstance().registerPreference(null);
+        ThirdPartyCookiesRestriction.getInstance().registerPreference(null);
     }
 
     @Override