Merge "Can disable checkbox without tint on text" into pi-dev
am: 48831c837f

Change-Id: I1a006094735f5d18e7fc8aeb843bac6736613af8
diff --git a/res/xml/zen_mode_block_settings.xml b/res/xml/zen_mode_block_settings.xml
index 63dbd47..cccc63c 100644
--- a/res/xml/zen_mode_block_settings.xml
+++ b/res/xml/zen_mode_block_settings.xml
@@ -24,15 +24,15 @@
        android:title="@string/zen_mode_block_effects_screen_off"
        android:key="zen_mode_block_screen_off">
 
-       <CheckBoxPreference
+       <com.android.settings.widget.DisabledCheckBoxPreference
            android:key="zen_effect_intent"
            android:title="@string/zen_mode_block_effect_intent" />
 
-       <CheckBoxPreference
+       <com.android.settings.widget.DisabledCheckBoxPreference
            android:key="zen_effect_light"
            android:title="@string/zen_mode_block_effect_light" />
 
-       <CheckBoxPreference
+       <com.android.settings.widget.DisabledCheckBoxPreference
            android:key="zen_effect_ambient"
            android:title="@string/zen_mode_block_effect_ambient" />
 
@@ -40,19 +40,19 @@
     <PreferenceCategory
         android:title="@string/zen_mode_block_effects_screen_on"
         android:key="zen_mode_block_screen_on">
-       <CheckBoxPreference
+       <com.android.settings.widget.DisabledCheckBoxPreference
            android:key="zen_effect_badge"
            android:title="@string/zen_mode_block_effect_badge" />
 
-        <CheckBoxPreference
+        <com.android.settings.widget.DisabledCheckBoxPreference
             android:key="zen_effect_status"
             android:title="@string/zen_mode_block_effect_status" />
 
-        <CheckBoxPreference
+        <com.android.settings.widget.DisabledCheckBoxPreference
             android:key="zen_effect_peek"
             android:title="@string/zen_mode_block_effect_peek" />
 
-       <CheckBoxPreference
+       <com.android.settings.widget.DisabledCheckBoxPreference
            android:key="zen_effect_list"
            android:title="@string/zen_mode_block_effect_list" />
    </PreferenceCategory>
diff --git a/res/xml/zen_mode_settings.xml b/res/xml/zen_mode_settings.xml
index 385e8ff..717b6c5 100644
--- a/res/xml/zen_mode_settings.xml
+++ b/res/xml/zen_mode_settings.xml
@@ -25,7 +25,8 @@
     <!-- sound vibration -->
     <com.android.settings.widget.DisabledCheckBoxPreference
         android:key="zen_effect_sound"
-        android:title="@string/zen_mode_block_effect_sound" />
+        android:title="@string/zen_mode_block_effect_sound"
+        android:enabled="false"/>
 
     <!-- What to block (effects) -->
     <Preference
diff --git a/src/com/android/settings/notification/ZenModeVisEffectPreferenceController.java b/src/com/android/settings/notification/ZenModeVisEffectPreferenceController.java
index 23723c5..67fd822 100644
--- a/src/com/android/settings/notification/ZenModeVisEffectPreferenceController.java
+++ b/src/com/android/settings/notification/ZenModeVisEffectPreferenceController.java
@@ -24,6 +24,7 @@
 
 import com.android.settings.core.PreferenceControllerMixin;
 import com.android.settingslib.core.lifecycle.Lifecycle;
+import com.android.settings.widget.DisabledCheckBoxPreference;
 
 public class ZenModeVisEffectPreferenceController
         extends AbstractZenModePreferenceController
@@ -78,9 +79,9 @@
         if (parentSuppressed) {
             ((CheckBoxPreference) preference).setChecked(parentSuppressed);
             onPreferenceChange(preference, parentSuppressed);
-            preference.setEnabled(false);
+            ((DisabledCheckBoxPreference) preference).enableCheckbox(false);
         } else {
-            preference.setEnabled(true);
+            ((DisabledCheckBoxPreference) preference).enableCheckbox(true);
             ((CheckBoxPreference) preference).setChecked(suppressed);
         }
     }
diff --git a/src/com/android/settings/widget/DisabledCheckBoxPreference.java b/src/com/android/settings/widget/DisabledCheckBoxPreference.java
index 482cff3..e441e21 100644
--- a/src/com/android/settings/widget/DisabledCheckBoxPreference.java
+++ b/src/com/android/settings/widget/DisabledCheckBoxPreference.java
@@ -17,46 +17,85 @@
 package com.android.settings.widget;
 
 import android.content.Context;
+import android.content.res.TypedArray;
+import android.support.v7.preference.CheckBoxPreference;
+import android.support.v7.preference.PreferenceViewHolder;
 import android.util.AttributeSet;
 import android.view.View;
 
-import android.support.v7.preference.CheckBoxPreference;
-import android.support.v7.preference.PreferenceViewHolder;
-
 /**
- * A CheckboxPreference with a disabled checkbox. Differs from CheckboxPreference.setDisabled()
- * in that the text is not dimmed.
+ * A CheckboxPreference that can disable its checkbox separate from its text.
+ * Differs from CheckboxPreference.setDisabled() in that the text is not dimmed.
  */
 public class DisabledCheckBoxPreference extends CheckBoxPreference {
+    private PreferenceViewHolder mViewHolder;
+    private View mCheckBox;
+    private boolean mEnabledCheckBox;
 
-    public DisabledCheckBoxPreference(Context context, AttributeSet attrs,
-            int defStyleAttr, int defStyleRes) {
+    public DisabledCheckBoxPreference(Context context, AttributeSet attrs, int defStyleAttr,
+            int defStyleRes) {
         super(context, attrs, defStyleAttr, defStyleRes);
+        setupDisabledCheckBoxPreference(context, attrs, defStyleAttr, defStyleRes);
     }
 
     public DisabledCheckBoxPreference(Context context, AttributeSet attrs, int defStyleAttr) {
         super(context, attrs, defStyleAttr);
+        setupDisabledCheckBoxPreference(context, attrs, defStyleAttr, 0);
     }
 
     public DisabledCheckBoxPreference(Context context, AttributeSet attrs) {
         super(context, attrs);
+        setupDisabledCheckBoxPreference(context, attrs, 0, 0);
     }
 
     public DisabledCheckBoxPreference(Context context) {
         super(context);
+        setupDisabledCheckBoxPreference(context, null, 0, 0);
+    }
+
+    private void setupDisabledCheckBoxPreference(Context context, AttributeSet attrs,
+            int defStyleAttr, int defStyleRes) {
+        final TypedArray a = context.obtainStyledAttributes(
+                attrs, com.android.internal.R.styleable.Preference, defStyleAttr, defStyleRes);
+        for (int i = a.getIndexCount() - 1; i >= 0; i--) {
+            int attr = a.getIndex(i);
+            switch (attr) {
+                case com.android.internal.R.styleable.Preference_enabled:
+                    mEnabledCheckBox = a.getBoolean(attr, true);
+                    break;
+            }
+        }
+        a.recycle();
+
+        // Always tell super class this preference is enabled.
+        // We manually enable/disable checkbox using enableCheckBox.
+        super.setEnabled(true);
+        enableCheckbox(mEnabledCheckBox);
+    }
+
+    public void enableCheckbox(boolean enabled) {
+        mEnabledCheckBox = enabled;
+        if (mViewHolder != null && mCheckBox != null) {
+            mCheckBox.setEnabled(mEnabledCheckBox);
+            mViewHolder.itemView.setEnabled(mEnabledCheckBox);
+        }
     }
 
     @Override
     public void onBindViewHolder(PreferenceViewHolder holder) {
         super.onBindViewHolder(holder);
+        mViewHolder = holder;
+        mCheckBox = holder.findViewById(android.R.id.checkbox);
 
-        View view = holder.findViewById(android.R.id.checkbox);
-        view.setEnabled(false);
-        holder.itemView.setEnabled(false);
+        enableCheckbox(mEnabledCheckBox);
     }
 
     @Override
     protected void performClick(View view) {
-        // Do nothing
+        // only perform clicks if the checkbox is enabled
+        if (mEnabledCheckBox) {
+            super.performClick(view);
+        }
     }
+
 }
diff --git a/tests/robotests/src/com/android/settings/notification/ZenModeVisEffectPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/notification/ZenModeVisEffectPreferenceControllerTest.java
index 6ca04ba..29ced0b 100644
--- a/tests/robotests/src/com/android/settings/notification/ZenModeVisEffectPreferenceControllerTest.java
+++ b/tests/robotests/src/com/android/settings/notification/ZenModeVisEffectPreferenceControllerTest.java
@@ -42,6 +42,7 @@
 import com.android.settings.testutils.FakeFeatureFactory;
 import com.android.settings.testutils.SettingsRobolectricTestRunner;
 import com.android.settingslib.core.lifecycle.Lifecycle;
+import com.android.settings.widget.DisabledCheckBoxPreference;
 
 import org.junit.Before;
 import org.junit.Test;
@@ -58,7 +59,7 @@
     @Mock
     private ZenModeBackend mBackend;
     @Mock
-    private CheckBoxPreference mockPref;
+    private DisabledCheckBoxPreference mockPref;
     private Context mContext;
     private FakeFeatureFactory mFeatureFactory;
     @Mock
@@ -114,7 +115,7 @@
         mController.updateState(mockPref);
 
         verify(mockPref).setChecked(false);
-        verify(mockPref).setEnabled(true);
+        verify(mockPref).enableCheckbox(true);
     }
 
     @Test
@@ -123,7 +124,7 @@
         mController.updateState(mockPref);
 
         verify(mockPref).setChecked(true);
-        verify(mockPref).setEnabled(true);
+        verify(mockPref).enableCheckbox(true);
     }
 
     @Test
@@ -138,7 +139,7 @@
         mController.updateState(mockPref);
 
         verify(mockPref).setChecked(true);
-        verify(mockPref).setEnabled(false);
+        verify(mockPref).enableCheckbox(false);
         verify(mBackend, times(1)).saveVisualEffectsPolicy(SUPPRESSED_EFFECT_PEEK, true);
     }
 
@@ -154,7 +155,7 @@
         mController.updateState(mockPref);
 
         verify(mockPref).setChecked(false);
-        verify(mockPref).setEnabled(true);
+        verify(mockPref).enableCheckbox(true);
         verify(mBackend, never()).saveVisualEffectsPolicy(SUPPRESSED_EFFECT_PEEK, true);
     }
 
diff --git a/tests/robotests/src/com/android/settings/widget/DisabledCheckBoxPreferenceTest.java b/tests/robotests/src/com/android/settings/widget/DisabledCheckBoxPreferenceTest.java
index 5ab7013..ff10833 100644
--- a/tests/robotests/src/com/android/settings/widget/DisabledCheckBoxPreferenceTest.java
+++ b/tests/robotests/src/com/android/settings/widget/DisabledCheckBoxPreferenceTest.java
@@ -21,6 +21,7 @@
 import static org.mockito.Matchers.any;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.times;
 import static org.mockito.Mockito.verify;
 
 import android.content.Context;
@@ -70,14 +71,28 @@
     }
 
     @Test
-    public void checkboxOnClick_doesNothing() {
+    public void checkboxOnClick_checkboxDisabled() {
         Preference.OnPreferenceClickListener onClick =
                 mock(Preference.OnPreferenceClickListener.class);
         mPref.setOnPreferenceClickListener(onClick);
         inflatePreference();
 
+        mPref.enableCheckbox(false);
         mPref.performClick(mRootView);
 
         verify(onClick, never()).onPreferenceClick(any());
     }
+
+    @Test
+    public void checkboxOnClick_checkboxEnabled() {
+        Preference.OnPreferenceClickListener onClick =
+                mock(Preference.OnPreferenceClickListener.class);
+        mPref.setOnPreferenceClickListener(onClick);
+        inflatePreference();
+
+        mPref.enableCheckbox(true);
+        mPref.performClick(mRootView);
+
+        verify(onClick, times(1)).onPreferenceClick(any());
+    }
 }