From 6dc0e30b564787b91dd7c0145dad3af94edcce44 Mon Sep 17 00:00:00 2001 From: jasonwshsu Date: Sun, 28 Jan 2024 01:00:12 +0800 Subject: [PATCH] Provides BackgroundPreference that can set background statically or dynamically Bug: 320709814 Test: atest BackgroundPreferenceTest Change-Id: I73e62733e6afd032ce980a4d6db0f4ca4573e770 --- res/layout/preference_background.xml | 95 +++++++++++++++++++ res/values/attrs.xml | 5 + .../accessibility/BackgroundPreference.java | 93 ++++++++++++++++++ .../BackgroundPreferenceTest.java | 72 ++++++++++++++ 4 files changed, 265 insertions(+) create mode 100644 res/layout/preference_background.xml create mode 100644 src/com/android/settings/accessibility/BackgroundPreference.java create mode 100644 tests/robotests/src/com/android/settings/accessibility/BackgroundPreferenceTest.java diff --git a/res/layout/preference_background.xml b/res/layout/preference_background.xml new file mode 100644 index 00000000000..129076aac1b --- /dev/null +++ b/res/layout/preference_background.xml @@ -0,0 +1,95 @@ + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/res/values/attrs.xml b/res/values/attrs.xml index 40a7c581159..200253acce6 100644 --- a/res/values/attrs.xml +++ b/res/values/attrs.xml @@ -198,4 +198,9 @@ + + + + + diff --git a/src/com/android/settings/accessibility/BackgroundPreference.java b/src/com/android/settings/accessibility/BackgroundPreference.java new file mode 100644 index 00000000000..ea56ac561c8 --- /dev/null +++ b/src/com/android/settings/accessibility/BackgroundPreference.java @@ -0,0 +1,93 @@ +/* + * 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. + */ + +package com.android.settings.accessibility; + +import android.content.Context; +import android.content.res.TypedArray; +import android.graphics.drawable.Drawable; +import android.util.AttributeSet; +import android.widget.LinearLayout; + +import androidx.annotation.DrawableRes; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.core.content.res.TypedArrayUtils; +import androidx.preference.Preference; +import androidx.preference.PreferenceViewHolder; + +import com.android.settings.R; + +/** + * A preference with custom background. + */ +public class BackgroundPreference extends Preference { + + private int mBackgroundId; + + public BackgroundPreference(@NonNull Context context, + @Nullable AttributeSet attrs, int defStyleAttr, + int defStyleRes) { + super(context, attrs, defStyleAttr, defStyleRes); + setLayoutResource(R.layout.preference_background); + setIconSpaceReserved(false); + + final TypedArray styledAttrs = context.obtainStyledAttributes(attrs, + R.styleable.BackgroundPreference); + mBackgroundId = styledAttrs.getResourceId(R.styleable.BackgroundPreference_background, 0); + styledAttrs.recycle(); + } + + public BackgroundPreference(@NonNull Context context, @Nullable AttributeSet attrs, + int defStyleAttr) { + this(context, attrs, defStyleAttr, 0); + } + + public BackgroundPreference(@NonNull Context context, @Nullable AttributeSet attrs) { + this(context, attrs, TypedArrayUtils.getAttr(context, + androidx.preference.R.attr.preferenceStyle, + com.android.internal.R.attr.preferenceStyle), 0); + } + + public BackgroundPreference(@NonNull Context context) { + this(context, null, TypedArrayUtils.getAttr(context, + androidx.preference.R.attr.preferenceStyle, + com.android.internal.R.attr.preferenceStyle), 0); + } + + @Override + public void onBindViewHolder(@NonNull PreferenceViewHolder holder) { + super.onBindViewHolder(holder); + + final LinearLayout layout = (LinearLayout) holder.findViewById(R.id.background); + if (mBackgroundId != 0) { + final Drawable backgroundDrawable = getContext().getDrawable(mBackgroundId); + layout.setBackground(backgroundDrawable); + } + } + + /** + * Sets the background to a given resource. The resource should refer to a Drawable object. + * + * @param resId The identifier of the resource. + */ + public void setBackground(@DrawableRes int resId) { + if (mBackgroundId != resId) { + mBackgroundId = resId; + notifyChanged(); + } + } +} diff --git a/tests/robotests/src/com/android/settings/accessibility/BackgroundPreferenceTest.java b/tests/robotests/src/com/android/settings/accessibility/BackgroundPreferenceTest.java new file mode 100644 index 00000000000..007d6645106 --- /dev/null +++ b/tests/robotests/src/com/android/settings/accessibility/BackgroundPreferenceTest.java @@ -0,0 +1,72 @@ +/* + * 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. + */ + +package com.android.settings.accessibility; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.verify; + +import android.content.Context; +import android.view.View; +import android.widget.LinearLayout; + +import androidx.preference.PreferenceViewHolder; +import androidx.test.core.app.ApplicationProvider; + +import com.android.settings.R; + +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Spy; +import org.mockito.junit.MockitoJUnit; +import org.mockito.junit.MockitoRule; +import org.robolectric.RobolectricTestRunner; + +/** Tests for {@link BackgroundPreference} */ +@RunWith(RobolectricTestRunner.class) +public class BackgroundPreferenceTest { + + @Rule + public final MockitoRule mMockitoRule = MockitoJUnit.rule(); + + private final Context mContext = ApplicationProvider.getApplicationContext(); + + private View mRootView = new View(mContext); + @Spy + private PreferenceViewHolder mViewHolder = PreferenceViewHolder.createInstanceForTests( + mRootView); + @Spy + private LinearLayout mLinearLayout = new LinearLayout(mContext); + private BackgroundPreference mPreference; + + @Before + public void setUp() { + mPreference = new BackgroundPreference(mContext); + } + + @Test + public void setBackground_success() { + doReturn(mLinearLayout).when(mViewHolder).findViewById(R.id.background); + + mPreference.setBackground(android.R.drawable.screen_background_dark); + mPreference.onBindViewHolder(mViewHolder); + + verify(mLinearLayout).setBackground(any()); + } +}