From 99604ae5442c67c0894cd53c1e109237ae713ea0 Mon Sep 17 00:00:00 2001 From: Yuhang Gong Date: Wed, 10 Aug 2022 16:01:15 +0800 Subject: Add AppHeaderPreference Add a Preference widget for pages need to show big apps icon and name in the header of the page. BUG: 239779252 Test: add a simialr test like AppPreference Change-Id: I296b0ed7b9c7645f31a9080c29da490b843494b0 --- .../res/layout/app_header_preference.xml | 81 ++++++++++++ .../AppPreference/res/values/strings.xml | 22 ++++ .../settingslib/widget/AppHeaderPreference.java | 144 +++++++++++++++++++++ packages/SettingsLib/LayoutPreference/Android.bp | 1 + .../LayoutPreference/res/values/styles.xml | 14 -- .../SettingsTheme/res/values/styles.xml | 14 ++ .../widget/AppHeaderPreferenceTest.java | 86 ++++++++++++ 7 files changed, 348 insertions(+), 14 deletions(-) create mode 100644 packages/SettingsLib/AppPreference/res/layout/app_header_preference.xml create mode 100644 packages/SettingsLib/AppPreference/res/values/strings.xml create mode 100644 packages/SettingsLib/AppPreference/src/com/android/settingslib/widget/AppHeaderPreference.java create mode 100644 packages/SettingsLib/tests/robotests/src/com/android/settingslib/widget/AppHeaderPreferenceTest.java diff --git a/packages/SettingsLib/AppPreference/res/layout/app_header_preference.xml b/packages/SettingsLib/AppPreference/res/layout/app_header_preference.xml new file mode 100644 index 000000000000..a2389a9b83a2 --- /dev/null +++ b/packages/SettingsLib/AppPreference/res/layout/app_header_preference.xml @@ -0,0 +1,81 @@ + + + + + + + + + + + + + + + + + + + + diff --git a/packages/SettingsLib/AppPreference/res/values/strings.xml b/packages/SettingsLib/AppPreference/res/values/strings.xml new file mode 100644 index 000000000000..ca148c00bbb5 --- /dev/null +++ b/packages/SettingsLib/AppPreference/res/values/strings.xml @@ -0,0 +1,22 @@ + + + + + + + Instant app + diff --git a/packages/SettingsLib/AppPreference/src/com/android/settingslib/widget/AppHeaderPreference.java b/packages/SettingsLib/AppPreference/src/com/android/settingslib/widget/AppHeaderPreference.java new file mode 100644 index 000000000000..60d00da29755 --- /dev/null +++ b/packages/SettingsLib/AppPreference/src/com/android/settingslib/widget/AppHeaderPreference.java @@ -0,0 +1,144 @@ +/* + * 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. + */ + +package com.android.settingslib.widget; + +import android.content.Context; +import android.text.TextUtils; +import android.util.AttributeSet; +import android.view.View; +import android.widget.TextView; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.annotation.StringRes; +import androidx.preference.Preference; +import androidx.preference.PreferenceViewHolder; + +/** + * The Preference for the pages need to show big apps icon and name in the header of the page. + */ +public class AppHeaderPreference extends Preference { + + private boolean mIsInstantApp; + private CharSequence mSecondSummary; + + public AppHeaderPreference(Context context, AttributeSet attrs, int defStyleAttr, + int defStyleRes) { + super(context, attrs, defStyleAttr, defStyleRes); + init(); + } + + public AppHeaderPreference(Context context, AttributeSet attrs, int defStyleAttr) { + super(context, attrs, defStyleAttr); + init(); + } + + public AppHeaderPreference(Context context, AttributeSet attrs) { + super(context, attrs); + init(); + } + + public AppHeaderPreference(Context context) { + super(context); + init(); + } + + private void init() { + setLayoutResource(R.layout.app_header_preference); + setSelectable(false); + setIsInstantApp(false); + } + + /** + * Returns the installation type of this preference. + * + * @return whether the app is an instant app + * @see #setIsInstantApp(boolean) + */ + public boolean getIsInstantApp() { + return mIsInstantApp; + } + + /** + * Sets the installation type for this preference with a boolean. + * + * @param isInstantApp whether the app is an instant app + */ + public void setIsInstantApp(@NonNull boolean isInstantApp) { + if (mIsInstantApp != isInstantApp) { + mIsInstantApp = isInstantApp; + notifyChanged(); + } + } + + /** + * Returns the second summary of this preference. + * + * @return The second summary + * @see #setSecondSummary(CharSequence) + */ + @Nullable + public CharSequence getSecondSummary() { + return mSecondSummary; + } + + /** + * Sets the second summary for this preference with a CharSequence. + * + * @param secondSummary The second summary for the preference + */ + public void setSecondSummary(@Nullable CharSequence secondSummary) { + if (!TextUtils.equals(mSecondSummary, secondSummary)) { + mSecondSummary = secondSummary; + notifyChanged(); + } + } + + /** + * Sets the second summary for this preference with a resource ID. + * + * @param secondSummaryResId The second summary as a resource + * @see #setSecondSummary(CharSequence) + */ + public void setSecondSummary(@StringRes int secondSummaryResId) { + setSecondSummary(getContext().getString(secondSummaryResId)); + } + + @Override + public void onBindViewHolder(@NonNull PreferenceViewHolder holder) { + super.onBindViewHolder(holder); + + final TextView installTypeView = (TextView) holder.findViewById(R.id.install_type); + if (installTypeView != null) { + if (mIsInstantApp) { + installTypeView.setVisibility(View.VISIBLE); + } else { + installTypeView.setVisibility(View.GONE); + } + } + + final TextView secondSummaryView = (TextView) holder.findViewById(R.id.second_summary); + if (secondSummaryView != null) { + if (!TextUtils.isEmpty(mSecondSummary)) { + secondSummaryView.setText(mSecondSummary); + secondSummaryView.setVisibility(View.VISIBLE); + } else { + secondSummaryView.setVisibility(View.GONE); + } + } + } +} diff --git a/packages/SettingsLib/LayoutPreference/Android.bp b/packages/SettingsLib/LayoutPreference/Android.bp index aaffdc922875..c29e1f789fd3 100644 --- a/packages/SettingsLib/LayoutPreference/Android.bp +++ b/packages/SettingsLib/LayoutPreference/Android.bp @@ -15,6 +15,7 @@ android_library { static_libs: [ "androidx.preference_preference", + "SettingsLibSettingsTheme", ], sdk_version: "system_current", diff --git a/packages/SettingsLib/LayoutPreference/res/values/styles.xml b/packages/SettingsLib/LayoutPreference/res/values/styles.xml index 2bdd6fe92e1a..f958037cfca6 100644 --- a/packages/SettingsLib/LayoutPreference/res/values/styles.xml +++ b/packages/SettingsLib/LayoutPreference/res/values/styles.xml @@ -22,20 +22,6 @@ 16dp - - - - + + + + diff --git a/packages/SettingsLib/tests/robotests/src/com/android/settingslib/widget/AppHeaderPreferenceTest.java b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/widget/AppHeaderPreferenceTest.java new file mode 100644 index 000000000000..fd181ff07222 --- /dev/null +++ b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/widget/AppHeaderPreferenceTest.java @@ -0,0 +1,86 @@ +/* + * 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. + */ + +package com.android.settingslib.widget; + +import static com.google.common.truth.Truth.assertThat; + +import android.content.Context; +import android.view.View; +import android.widget.TextView; + +import androidx.preference.PreferenceViewHolder; + + + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.RobolectricTestRunner; +import org.robolectric.RuntimeEnvironment; + +@RunWith(RobolectricTestRunner.class) +public class AppHeaderPreferenceTest { + + private Context mContext; + private View mRootView; + private AppHeaderPreference mPreference; + private PreferenceViewHolder mHolder; + + @Before + public void setUp() { + mContext = RuntimeEnvironment.application; + mRootView = View.inflate(mContext, R.layout.app_header_preference, /* parent */ null); + mHolder = PreferenceViewHolder.createInstanceForTests(mRootView); + mPreference = new AppHeaderPreference(mContext); + } + + @Test + public void setNonSelectable_viewShouldNotBeSelectable() { + mPreference.onBindViewHolder(mHolder); + + assertThat(mHolder.itemView.isClickable()).isFalse(); + } + + @Test + public void defaultInstallType_viewShouldNotBeVisible() { + mPreference.onBindViewHolder(mHolder); + + assertThat(mRootView.findViewById(R.id.install_type).getVisibility()) + .isEqualTo(View.GONE); + } + + @Test + public void setIsInstantApp_shouldUpdateInstallType() { + + mPreference.onBindViewHolder(mHolder); + mPreference.setIsInstantApp(true); + + assertThat(((TextView) mRootView.findViewById(R.id.install_type)).getText().toString()) + .isEqualTo(mContext.getResources().getString(R.string.install_type_instant)); + } + + @Test + public void setSecondSummary_shouldUpdateSecondSummary() { + final String defaultTestText = "Test second summary"; + + mPreference.onBindViewHolder(mHolder); + mPreference.setSecondSummary(defaultTestText); + + assertThat(((TextView) mRootView.findViewById(R.id.second_summary)).getText().toString()) + .isEqualTo(defaultTestText); + } +} -- cgit v1.2.3-59-g8ed1b