Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 1174f91f authored by Edgar Wang's avatar Edgar Wang Committed by Android (Google) Code Review
Browse files

Merge "[Expressive design] update MainSwitchPreference style" into main

parents a00b43bc ec72a55a
Loading
Loading
Loading
Loading
+26 −0
Original line number Diff line number Diff line
<?xml version="1.0" encoding="utf-8"?>
<!--
  Copyright (C) 2024 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.
  -->

<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="?android:attr/colorControlHighlight">
    <item>
        <shape android:shape="rectangle">
            <solid android:color="@color/settingslib_materialColorPrimaryContainer"/>
            <corners android:radius="@dimen/settingslib_expressive_radius_extralarge3"/>
        </shape>
    </item>
</ripple>
 No newline at end of file
+75 −0
Original line number Diff line number Diff line
<?xml version="1.0" encoding="utf-8"?>
<!--
  Copyright (C) 2024 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.
  -->

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:minHeight="?android:attr/listPreferredItemHeight"
    android:paddingStart="?android:attr/listPreferredItemPaddingStart"
    android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
    android:paddingVertical="@dimen/settingslib_expressive_space_small1"
    android:orientation="vertical">

    <LinearLayout
        android:id="@+id/frame"
        android:minHeight="@dimen/settingslib_expressive_space_large3"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:paddingStart="@dimen/settingslib_expressive_space_medium1"
        android:paddingEnd="@dimen/settingslib_expressive_space_small3"
        android:background="@drawable/settingslib_expressive_switch_bar_bg">

        <RelativeLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_marginEnd="@dimen/settingslib_expressive_space_extrasmall4"
            android:layout_marginVertical="@dimen/settingslib_expressive_space_small4"
            android:layout_weight="1">

            <TextView
                android:id="@+id/switch_text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="start"
                android:ellipsize="end"
                android:textAlignment="viewStart"
                android:textAppearance="?android:attr/textAppearanceListItem"
                android:textColor="@color/settingslib_main_switch_text_color" />

            <TextView
                android:id="@+id/switch_summary"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignLeft="@id/switch_text"
                android:layout_alignStart="@id/switch_text"
                android:layout_below="@id/switch_text"
                android:layout_gravity="start"
                android:textAlignment="viewStart"
                android:textAppearance="?android:attr/textAppearanceListItemSmall"
                android:textColor="@color/settingslib_main_switch_text_color"
                android:visibility="gone" />
        </RelativeLayout>

        <com.google.android.material.materialswitch.MaterialSwitch
            android:theme="@style/Theme.Material3.DynamicColors.DayNight"
            android:id="@android:id/switch_widget"
            style="@style/SettingslibMainSwitchStyle.Expressive" />
    </LinearLayout>

</LinearLayout>
 No newline at end of file
+35 −1
Original line number Diff line number Diff line
@@ -22,6 +22,7 @@ import android.os.Build;
import android.os.Build.VERSION_CODES;
import android.os.Parcel;
import android.os.Parcelable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
@@ -31,6 +32,7 @@ import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.ColorInt;
import androidx.annotation.RequiresApi;

import com.android.settingslib.widget.mainswitch.R;

@@ -52,6 +54,7 @@ public class MainSwitchBar extends LinearLayout implements OnCheckedChangeListen
    private int mBackgroundActivatedColor;

    protected TextView mTextView;
    protected TextView mSummaryView;
    protected CompoundButton mSwitch;
    private final View mFrameView;

@@ -71,7 +74,11 @@ public class MainSwitchBar extends LinearLayout implements OnCheckedChangeListen
            int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);

        LayoutInflater.from(context).inflate(R.layout.settingslib_main_switch_bar, this);
        boolean isExpressive = SettingsThemeHelper.isExpressiveTheme(context);
        int resId = isExpressive
                ? R.layout.settingslib_expressive_main_switch_bar
                : R.layout.settingslib_main_switch_bar;
        LayoutInflater.from(context).inflate(resId, this);

        if (Build.VERSION.SDK_INT < VERSION_CODES.S) {
            TypedArray a;
@@ -93,6 +100,9 @@ public class MainSwitchBar extends LinearLayout implements OnCheckedChangeListen

        mFrameView = findViewById(R.id.frame);
        mTextView = findViewById(R.id.switch_text);
        if (isExpressive) {
            mSummaryView = findViewById(R.id.switch_summary);
        }
        mSwitch = findViewById(android.R.id.switch_widget);
        addOnSwitchChangeListener((switchView, isChecked) -> setChecked(isChecked));

@@ -109,6 +119,12 @@ public class MainSwitchBar extends LinearLayout implements OnCheckedChangeListen
            final CharSequence title = a.getText(
                    androidx.preference.R.styleable.Preference_android_title);
            setTitle(title);
            //TODO(b/369470034): update to next version
            if (isExpressive && Build.VERSION.SDK_INT >= VERSION_CODES.VANILLA_ICE_CREAM) {
                CharSequence summary = a.getText(
                        androidx.preference.R.styleable.Preference_android_summary);
                setSummary(summary);
            }
            a.recycle();
        }

@@ -152,6 +168,18 @@ public class MainSwitchBar extends LinearLayout implements OnCheckedChangeListen
        }
    }

    /**
     * Set the summary text
     */
    @RequiresApi(VERSION_CODES.VANILLA_ICE_CREAM)
    //TODO(b/369470034): update to next version
    public void setSummary(CharSequence text) {
        if (mSummaryView != null) {
            mSummaryView.setText(text);
            mSummaryView.setVisibility(TextUtils.isEmpty(text) ? GONE : VISIBLE);
        }
    }

    /**
     * Set icon space reserved for title
     */
@@ -219,6 +247,12 @@ public class MainSwitchBar extends LinearLayout implements OnCheckedChangeListen
            mFrameView.setEnabled(enabled);
            mFrameView.setActivated(isChecked());
        }

        if (SettingsThemeHelper.isExpressiveTheme(getContext())) {
            if (mSummaryView != null) {
                mSummaryView.setEnabled(enabled);
            }
        }
    }

    private void propagateChecked(boolean isChecked) {
+14 −0
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@ package com.android.settingslib.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.os.Build;
import android.util.AttributeSet;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
@@ -89,6 +90,10 @@ public class MainSwitchPreference extends TwoStatePreference implements OnChecke
                    androidx.preference.R.styleable.Preference_android_title);
            setTitle(title);

            CharSequence summary = a.getText(
                    androidx.preference.R.styleable.Preference_android_summary);
            setSummary(summary);

            final boolean bIconSpaceReserved = a.getBoolean(
                    androidx.preference.R.styleable.Preference_android_iconSpaceReserved, true);
            setIconSpaceReserved(bIconSpaceReserved);
@@ -112,6 +117,15 @@ public class MainSwitchPreference extends TwoStatePreference implements OnChecke
        }
    }

    @Override
    public void setSummary(CharSequence summary) {
        super.setSummary(summary);
        if (mMainSwitchBar != null
                && Build.VERSION.SDK_INT >= Build.VERSION_CODES.VANILLA_ICE_CREAM) {
            mMainSwitchBar.setSummary(summary);
        }
    }

    @Override
    public void setIconSpaceReserved(boolean iconSpaceReserved) {
        super.setIconSpaceReserved(iconSpaceReserved);
+47 −0
Original line number Diff line number Diff line
<?xml version="1.0" encoding="utf-8"?>
<!--
  Copyright (C) 2024 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">
    <!-- State Disabled, Unchecked -->
    <item
        android:alpha="?android:attr/disabledAlpha"
        android:color="@color/settingslib_materialColorSurfaceContainerHighest"
        android:state_enabled="false" android:state_checked="false"/>
    <!-- State Disabled, Checked -->
    <item
        android:alpha="?android:attr/disabledAlpha"
        android:color="@color/settingslib_materialColorPrimary"
        android:state_enabled="false" android:state_checked="true"/>
    <!-- State Checked -->
    <item
        android:color="@color/settingslib_materialColorPrimary"
        android:state_checked="true"/>

    <!-- State Unchecked -->
    <item
        android:color="@color/settingslib_materialColorSurfaceContainerHighest"
        android:state_hovered="true" android:state_checked="false"/>
    <item
        android:color="@color/settingslib_materialColorSurfaceContainerHighest"
        android:state_focused="true" android:state_checked="false"/>
    <item
        android:color="@color/settingslib_materialColorSurfaceContainerHighest"
        android:state_pressed="true" android:state_checked="false"/>
    <item
        android:color="@color/settingslib_materialColorSurfaceContainerHighest"
        android:state_checked="false"/>
</selector>
 No newline at end of file
Loading