Remove PreferenceSwitchWidget

PreferenceSwitchWidget was more code for us to support, and at the same time,
created inconsistencies. Any switches that weren't PreferenceSwitchWidgets
looked broken on polestar customizations. Even within chassis, switches
in a PreferenceFragment looked different than switches outside.

An updated polestar customization will accompany this commit.

Fixes: 143242926
Test: Manually
Change-Id: I6a1e9bb450148742ba75437a7db0373cacd048b8
diff --git a/car-ui-lib/res/drawable/car_ui_preference_switch_text_track.xml b/car-ui-lib/res/drawable/car_ui_preference_switch_text_track.xml
deleted file mode 100644
index 802bd5d..0000000
--- a/car-ui-lib/res/drawable/car_ui_preference_switch_text_track.xml
+++ /dev/null
@@ -1,56 +0,0 @@
-<?xml version="1.0" encoding="utf-8"?>
-<!--
-  ~ Copyright 2019 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.
-  -->
-<layer-list
-    xmlns:android="http://schemas.android.com/apk/res/android"
-    xmlns:app="http://schemas.android.com/apk/res-auto">
-    <item>
-        <!-- Shape used to set the size of the overlay drawable -->
-        <shape
-            android:shape="rectangle"
-            android:tint="@android:color/transparent">
-            <size
-                android:width="@dimen/car_ui_preference_switch_width"
-                android:height="@dimen/car_ui_preference_switch_height"/>
-        </shape>
-    </item>
-    <item
-        android:gravity="center"
-        android:top="0dp"
-        android:bottom="0dp"
-        android:right="0dp"
-        android:left="@dimen/car_ui_preference_switch_width_half">
-        <drawable
-            class="com.android.car.ui.drawable.CarUiTextDrawable"
-            app:renderedText="@string/car_ui_preference_switch_on"
-            app:textColor="@color/car_ui_preference_switch_track_text_color"
-            app:textSize="@dimen/car_ui_preference_switch_text_size">
-        </drawable>
-    </item>
-    <item
-        android:gravity="center"
-        android:top="0dp"
-        android:bottom="0dp"
-        android:left="0dp"
-        android:right="@dimen/car_ui_preference_switch_width_half">
-        <drawable
-            class="com.android.car.ui.drawable.CarUiTextDrawable"
-            app:renderedText="@string/car_ui_preference_switch_off"
-            app:textColor="@color/car_ui_preference_switch_track_text_color"
-            app:textSize="@dimen/car_ui_preference_switch_text_size">
-        </drawable>
-    </item>
-</layer-list>
diff --git a/car-ui-lib/res/layout/car_ui_preference_widget_switch.xml b/car-ui-lib/res/layout/car_ui_preference_widget_switch.xml
index b85340c..ae0a3ce 100644
--- a/car-ui-lib/res/layout/car_ui_preference_widget_switch.xml
+++ b/car-ui-lib/res/layout/car_ui_preference_widget_switch.xml
@@ -15,7 +15,7 @@
     limitations under the License.
 -->
 
-<com.android.car.ui.preference.PreferenceSwitchWidget
+<Switch
     xmlns:android="http://schemas.android.com/apk/res/android"
     android:id="@android:id/switch_widget"
     android:layout_width="wrap_content"
diff --git a/car-ui-lib/res/values/attrs.xml b/car-ui-lib/res/values/attrs.xml
index 5778cda..9e88d47 100644
--- a/car-ui-lib/res/values/attrs.xml
+++ b/car-ui-lib/res/values/attrs.xml
@@ -110,17 +110,5 @@
     <!-- Theme attribute to specify a default style for all CarUiRecyclerViews -->
     <attr name="carUiRecyclerViewStyle" format="reference" />
 
-    <!-- TODO: Replace this with the Android default text styleable -->
-    <declare-styleable name="CarUiTextDrawable">
-        <!-- Text to be rendered by the drawable -->
-        <attr name="renderedText" format="string" />
-        <!-- Font size -->
-        <attr name="textSize" format="dimension" />
-        <!-- Text color -->
-        <attr name="textColor" format="color" />
-        <!-- Type face to use -->
-        <attr name="typeface" format="string" />
-    </declare-styleable>
-
     <attr name="state_ux_restricted" format="boolean" />
 </resources>
diff --git a/car-ui-lib/res/values/bools.xml b/car-ui-lib/res/values/bools.xml
index cb7d18d..cb4b9ce 100644
--- a/car-ui-lib/res/values/bools.xml
+++ b/car-ui-lib/res/values/bools.xml
@@ -39,10 +39,6 @@
 
     <!-- Preferences -->
 
-    <!-- Whether toggle transition should be animated or not -->
-    <bool name="car_ui_preference_switch_toggle_show_animation">true</bool>
-    <!-- Whether a text track should be used instead of the standard device default track -->
-    <bool name="car_ui_preference_switch_toggle_use_text_track">false</bool>
     <!-- Whether list, edit, dropdown and intent preferences should show a chevron or not -->
     <bool name="car_ui_preference_show_chevron">false</bool>
     <!-- whether list preference should be shown in full screen or as a dialog -->
diff --git a/car-ui-lib/src/com/android/car/ui/drawable/CarUiTextDrawable.java b/car-ui-lib/src/com/android/car/ui/drawable/CarUiTextDrawable.java
deleted file mode 100644
index 167dfd3..0000000
--- a/car-ui-lib/src/com/android/car/ui/drawable/CarUiTextDrawable.java
+++ /dev/null
@@ -1,223 +0,0 @@
-/*
- * Copyright (C) 2019 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.
- */
-package com.android.car.ui.drawable;
-
-import android.content.res.Resources;
-import android.content.res.TypedArray;
-import android.graphics.Canvas;
-import android.graphics.Color;
-import android.graphics.ColorFilter;
-import android.graphics.Paint;
-import android.graphics.PixelFormat;
-import android.graphics.Rect;
-import android.graphics.Typeface;
-import android.graphics.drawable.Drawable;
-import android.text.TextPaint;
-import android.util.AttributeSet;
-
-import androidx.annotation.NonNull;
-import androidx.annotation.Nullable;
-
-import com.android.car.ui.R;
-
-import org.xmlpull.v1.XmlPullParser;
-import org.xmlpull.v1.XmlPullParserException;
-
-import java.io.IOException;
-
-/**
- * A drawable to renders a given text. It can be used as part of a compound
- * drawable as follows:
- *
- * <pre>
- * {@code
- * <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
- *     ...
- *     <item
- *         <drawable
- *             class="com.android.car.ui.drawable.CarUiTextDrawable"
- *             app:text="Some text"
- *             app:textSize="30sp">
- *         </drawable>
- *     </item>
- *     ...
- * </layer-list>
- * }
- * </pre>
- *
- * <b>Important: This class is referenced by reflection from overlays. DO NOT
- * RENAME, REMOVE OR MOVE TO ANOTHER PACKAGE, otherwise the overlays would
- * cause the target application to crash.</b>
- */
-public class CarUiTextDrawable extends Drawable {
-    private final TextPaint mPaint = new TextPaint();
-    private final Rect mTextBounds = new Rect(); // Minimum bounds of the text only.
-
-    // Attributes initialized during inflation
-    @Nullable
-    private String mText;
-    private Typeface mTypeface = Typeface.DEFAULT;
-    private float mTextSize = 10;
-    private int mTextColor = Color.WHITE;
-
-    /** Text sample used to measure font height */
-    private static final String TEXT_HEIGHT_SAMPLE = "Ag";
-
-    /** Constructor available to include this drawable by code */
-    public CarUiTextDrawable(@Nullable String text,
-                             Typeface typeface,
-                             float textSize,
-                             int textColor) {
-        mText = text;
-        mTypeface = typeface;
-        mTextSize = textSize;
-        mTextColor = textColor;
-        refreshTextBoundsAndInvalidate();
-    }
-
-    /** Constructor invoked by XML inflator */
-    public CarUiTextDrawable() {
-        // To be used during inflation.
-    }
-
-    /** Updates the text rendered by this drawable */
-    public void setText(@Nullable String text) {
-        this.mText = text;
-        refreshTextBoundsAndInvalidate();
-    }
-
-    @Override
-    public int getIntrinsicHeight() {
-        return mTextBounds.height();
-    }
-
-    @Override
-    public int getIntrinsicWidth() {
-        return mTextBounds.width();
-    }
-
-    @Override
-    public void draw(Canvas canvas) {
-        Rect bounds = getBounds();
-        if (bounds.isEmpty() || mPaint.getAlpha() == 0) {
-            return;
-        }
-
-        // Draw text.
-        if (mText != null) {
-            canvas.drawText(mText,
-                    bounds.centerX(),
-                    bounds.centerY() + (mTextBounds.height() / 2.0f),
-                    mPaint);
-        }
-    }
-
-    @Override
-    public void setAlpha(int alpha) {
-        final int old = mPaint.getAlpha();
-        if (alpha != old) {
-            mPaint.setAlpha(alpha);
-            invalidateSelf();
-        }
-    }
-
-    @Override
-    public void setColorFilter(ColorFilter cf) {
-        mPaint.setColorFilter(cf);
-        invalidateSelf();
-    }
-
-    @Override
-    public int getOpacity() {
-        return mPaint.bgColor != 0 ? PixelFormat.OPAQUE : PixelFormat.TRANSPARENT;
-    }
-
-    /**
-     * Call this to re-measure the text when properties that may affect the
-     * textBounds are changed.
-     */
-    private void refreshTextBoundsAndInvalidate() {
-        mPaint.setTypeface(mTypeface);
-        mPaint.setTextSize(mTextSize);
-        mPaint.setColor(mTextColor);
-        mPaint.setTextAlign(Paint.Align.CENTER);
-        mPaint.setAntiAlias(true);
-        mPaint.setDither(true);
-        calculateTextBounds(mPaint, mText, mTextBounds);
-        invalidateSelf();
-    }
-
-    /**
-     * Calculate the bounds of the given text.
-     *
-     * The textBounds are different from the drawable bounds: the textBounds is
-     * is strictly a subset, and measures the minimum bounding box necessary to
-     * draw the text. The left and right of the textBounds are used to calculate
-     * the intrinsic width and intrinsic height.
-     *
-     * Text height is calculated based on a fixed sample text (to avoid height
-     * changing every time a different text is rendered)
-     */
-    private static void calculateTextBounds(TextPaint paint, @Nullable String text,
-                                            Rect outBounds) {
-        outBounds.setEmpty();
-        paint.getTextBounds(TEXT_HEIGHT_SAMPLE, 0, TEXT_HEIGHT_SAMPLE.length(), outBounds);
-
-        if (text != null) {
-            // Save the top and bottom bounds of the sample text.
-            int top = outBounds.top;
-            int bottom = outBounds.bottom;
-
-            // Get the actual text bounds.
-            paint.getTextBounds(text, 0, text.length(), outBounds);
-
-            // Replace top and bottom with sample text bounds. We only care about left
-            // and right.
-            outBounds.top = top;
-            outBounds.bottom = bottom;
-        } else {
-            outBounds.left = 0;
-            outBounds.right = 0;
-        }
-    }
-
-    @NonNull
-    private static TypedArray themedObtainAttributes(@NonNull Resources res,
-                                              @Nullable Resources.Theme theme,
-                                              @NonNull AttributeSet set,
-                                              @NonNull int[] attrs) {
-        if (theme == null) {
-            return res.obtainAttributes(set, attrs);
-        }
-        return theme.obtainStyledAttributes(set, attrs, 0, 0);
-    }
-
-    @Override
-    public void inflate(@NonNull Resources r, @NonNull XmlPullParser parser,
-                        @NonNull AttributeSet attrs, @Nullable Resources.Theme theme)
-            throws XmlPullParserException, IOException {
-        final TypedArray a = themedObtainAttributes(r, theme, attrs, R.styleable.CarUiTextDrawable);
-        mText = a.getString(R.styleable.CarUiTextDrawable_renderedText);
-        mTypeface = Typeface.create(a.getString(R.styleable.CarUiTextDrawable_typeface),
-                Typeface.NORMAL);
-        mTextSize = a.getDimension(R.styleable.CarUiTextDrawable_textSize, 10);
-        mTextColor = a.getColor(R.styleable.CarUiTextDrawable_textColor,
-                r.getColor(R.color.car_ui_text_color_primary, theme));
-        a.recycle();
-        refreshTextBoundsAndInvalidate();
-        super.inflate(r, parser, attrs, theme);
-    }
-}
diff --git a/car-ui-lib/src/com/android/car/ui/preference/PreferenceSwitchWidget.java b/car-ui-lib/src/com/android/car/ui/preference/PreferenceSwitchWidget.java
deleted file mode 100644
index bd67a00..0000000
--- a/car-ui-lib/src/com/android/car/ui/preference/PreferenceSwitchWidget.java
+++ /dev/null
@@ -1,72 +0,0 @@
-/*
- * Copyright 2019 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.
- */
-
-package com.android.car.ui.preference;
-
-import android.content.Context;
-import android.util.AttributeSet;
-import android.widget.Switch;
-
-import com.android.car.ui.R;
-
-/**
- * Switch preference widget. This widget is exactly similar to switch widget just that it calls
- * {@link Switch#jumpDrawablesToCurrentState} on each click.
- */
-public class PreferenceSwitchWidget extends Switch {
-
-    private boolean mEnableAnimation = true;
-
-    public PreferenceSwitchWidget(Context context) {
-        super(context);
-        init(context);
-    }
-
-    public PreferenceSwitchWidget(Context context, AttributeSet attrs) {
-        super(context, attrs);
-        init(context);
-    }
-
-    public PreferenceSwitchWidget(Context context, AttributeSet attrs, int defStyleAttr) {
-        super(context, attrs, defStyleAttr);
-        init(context);
-    }
-
-    public PreferenceSwitchWidget(Context context, AttributeSet attrs, int defStyleAttr,
-            int defStyleRes) {
-        super(context, attrs, defStyleAttr, defStyleRes);
-        init(context);
-    }
-
-    private void init(Context context) {
-        mEnableAnimation = context.getResources().getBoolean(
-                R.bool.car_ui_preference_switch_toggle_show_animation);
-        boolean textBackground = context.getResources().getBoolean(
-                R.bool.car_ui_preference_switch_toggle_use_text_track);
-        if (textBackground) {
-            setTrackDrawable(context.getDrawable(R.drawable.car_ui_preference_switch_text_track));
-        }
-    }
-
-    @Override
-    public void setChecked(boolean checked) {
-        super.setChecked(checked);
-
-        if (!mEnableAnimation) {
-            jumpDrawablesToCurrentState();
-        }
-    }
-}
diff --git a/car-ui-lib/src/com/android/car/ui/uxr/DrawableStateSwitch.java b/car-ui-lib/src/com/android/car/ui/uxr/DrawableStateSwitch.java
index 8064dbf..bfa018c 100644
--- a/car-ui-lib/src/com/android/car/ui/uxr/DrawableStateSwitch.java
+++ b/car-ui-lib/src/com/android/car/ui/uxr/DrawableStateSwitch.java
@@ -21,13 +21,11 @@
 
 import androidx.annotation.Nullable;
 
-import com.android.car.ui.preference.PreferenceSwitchWidget;
-
 /**
  * A {@link Switch} that implements {@link DrawableStateView}, for allowing additional states
  * such as ux restriction.
  */
-public class DrawableStateSwitch extends PreferenceSwitchWidget implements DrawableStateView {
+public class DrawableStateSwitch extends Switch implements DrawableStateView {
     private int[] mState;
 
     public DrawableStateSwitch(Context context) {
diff --git a/car-ui-lib/tests/apitest/current.xml b/car-ui-lib/tests/apitest/current.xml
index 6d809ca..1dab46a 100644
--- a/car-ui-lib/tests/apitest/current.xml
+++ b/car-ui-lib/tests/apitest/current.xml
@@ -7,8 +7,6 @@
   <public type="bool" name="car_ui_list_item_single_line_title"/>
   <public type="bool" name="car_ui_preference_list_show_full_screen"/>
   <public type="bool" name="car_ui_preference_show_chevron"/>
-  <public type="bool" name="car_ui_preference_switch_toggle_show_animation"/>
-  <public type="bool" name="car_ui_preference_switch_toggle_use_text_track"/>
   <public type="bool" name="car_ui_scrollbar_above_recycler_view"/>
   <public type="bool" name="car_ui_scrollbar_enable"/>
   <public type="bool" name="car_ui_toolbar_logo_fills_nav_icon_space"/>
@@ -148,7 +146,6 @@
   <public type="drawable" name="car_ui_icon_settings"/>
   <public type="drawable" name="car_ui_list_item_divider"/>
   <public type="drawable" name="car_ui_preference_icon_chevron"/>
-  <public type="drawable" name="car_ui_preference_switch_text_track"/>
   <public type="drawable" name="car_ui_recyclerview_button_ripple_background"/>
   <public type="drawable" name="car_ui_recyclerview_divider"/>
   <public type="drawable" name="car_ui_recyclerview_ic_down"/>
diff --git a/car-ui-lib/tests/paintbooth/res/layout/widgets_activity.xml b/car-ui-lib/tests/paintbooth/res/layout/widgets_activity.xml
index 633dc66..b282921 100644
--- a/car-ui-lib/tests/paintbooth/res/layout/widgets_activity.xml
+++ b/car-ui-lib/tests/paintbooth/res/layout/widgets_activity.xml
@@ -33,7 +33,7 @@
         android:layout_height="wrap_content"
         android:text="@string/widget_checkbox_text"/>
 
-    <com.android.car.ui.preference.PreferenceSwitchWidget
+    <Switch
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="@string/widget_switch_text"/>