DO NOT MERGE Add additional unavailable toggle drawable state

Bug: 216643999
Test: manual
Change-Id: I4143f75ad69b9e6a462363633458d95fc1656cd0
diff --git a/car-qc-lib/res/drawable/qc_toggle_button_background.xml b/car-qc-lib/res/drawable/qc_toggle_button_background.xml
new file mode 100644
index 0000000..f42ebf8
--- /dev/null
+++ b/car-qc-lib/res/drawable/qc_toggle_button_background.xml
@@ -0,0 +1,34 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+  ~ Copyright (C) 2021 The Android Open Source Project
+  ~
+  ~ Licensed under the Apache License, Version 2.0 (the "License");
+  ~ you may not use this file except in compliance with the License.
+  ~ You may obtain a copy of the License at
+  ~
+  ~      http://www.apache.org/licenses/LICENSE-2.0
+  ~
+  ~ Unless required by applicable law or agreed to in writing, software
+  ~ distributed under the License is distributed on an "AS IS" BASIS,
+  ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  ~ See the License for the specific language governing permissions and
+  ~ limitations under the License.
+  -->
+<selector
+    xmlns:android="http://schemas.android.com/apk/res/android"
+    xmlns:app="http://schemas.android.com/apk/res-auto">
+    <item app:state_toggle_unavailable="true">
+        <shape android:shape="rectangle">
+            <solid android:color="@color/qc_toggle_unavailable_background_color" />
+            <stroke android:color="@color/qc_toggle_unavailable_color"
+                android:width="@dimen/qc_toggle_unavailable_outline_width" />
+            <corners android:radius="@dimen/qc_toggle_background_radius" />
+        </shape>
+    </item>
+    <item>
+        <shape android:shape="rectangle">
+            <solid android:color="@color/qc_toggle_background_color" />
+            <corners android:radius="@dimen/qc_toggle_background_radius" />
+        </shape>
+    </item>
+</selector>
\ No newline at end of file
diff --git a/car-qc-lib/res/values/attrs.xml b/car-qc-lib/res/values/attrs.xml
new file mode 100644
index 0000000..94613b9
--- /dev/null
+++ b/car-qc-lib/res/values/attrs.xml
@@ -0,0 +1,19 @@
+<!--
+  ~ Copyright (C) 2022 The Android Open Source Project
+  ~
+  ~ Licensed under the Apache License, Version 2.0 (the "License");
+  ~ you may not use this file except in compliance with the License.
+  ~ You may obtain a copy of the License at
+  ~
+  ~      http://www.apache.org/licenses/LICENSE-2.0
+  ~
+  ~ Unless required by applicable law or agreed to in writing, software
+  ~ distributed under the License is distributed on an "AS IS" BASIS,
+  ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  ~ See the License for the specific language governing permissions and
+  ~ limitations under the License.
+  -->
+
+<resources>
+    <attr name="state_toggle_unavailable"/>
+</resources>
\ No newline at end of file
diff --git a/car-qc-lib/src/com/android/car/qc/view/QCRowView.java b/car-qc-lib/src/com/android/car/qc/view/QCRowView.java
index a11643a..1e10e4b 100644
--- a/car-qc-lib/src/com/android/car/qc/view/QCRowView.java
+++ b/car-qc-lib/src/com/android/car/qc/view/QCRowView.java
@@ -337,7 +337,8 @@
             tmpToggleButton = actionView.requireViewById(R.id.qc_toggle_button);
         }
         DrawableStateToggleButton toggleButton = tmpToggleButton; // must be effectively final
-        CarUiUtils.makeAllViewsEnabled(toggleButton, action.isEnabled());
+        boolean shouldEnableView =
+                (action.isEnabled() || action.isClickableWhileDisabled()) && action.isAvailable();
         toggleButton.setText(null);
         toggleButton.setTextOn(null);
         toggleButton.setTextOff(null);
@@ -346,7 +347,8 @@
                 action.getIcon(), action.isAvailable());
         toggleButton.setButtonDrawable(icon);
         toggleButton.setChecked(action.isChecked());
-        toggleButton.setEnabled(action.isEnabled() && action.isAvailable());
+        toggleButton.setEnabled(shouldEnableView);
+        setToggleButtonDrawableState(toggleButton, action.isEnabled(), action.isAvailable());
         toggleButton.setOnTouchListener((v, event) -> {
             if (!action.isEnabled()) {
                 if (event.getActionMasked() == MotionEvent.ACTION_UP) {
@@ -364,6 +366,30 @@
                 });
     }
 
+    private void setToggleButtonDrawableState(DrawableStateToggleButton view,
+            boolean enabled, boolean available) {
+        int[] statesToAdd = null;
+        int[] statesToRemove = null;
+        if (enabled) {
+            if (!available) {
+                statesToAdd =
+                        new int[]{android.R.attr.state_enabled, R.attr.state_toggle_unavailable};
+            } else {
+                statesToAdd = new int[]{android.R.attr.state_enabled};
+                statesToRemove = new int[]{R.attr.state_toggle_unavailable};
+            }
+        } else {
+            if (available) {
+                statesToRemove =
+                        new int[]{android.R.attr.state_enabled, R.attr.state_toggle_unavailable};
+            } else {
+                statesToAdd = new int[]{R.attr.state_toggle_unavailable};
+                statesToRemove = new int[]{android.R.attr.state_enabled};
+            }
+        }
+        CarUiUtils.applyDrawableStatesToAllViews(view, statesToAdd, statesToRemove);
+    }
+
     @NonNull
     private View createActionView(@NonNull ViewGroup root, @Nullable View actionView,
             @LayoutRes int resId) {