diff options
| author | 2021-01-20 05:51:45 +0000 | |
|---|---|---|
| committer | 2021-01-20 05:51:45 +0000 | |
| commit | 148195ee608aabefc1a0c49e1ac38a935afcc61d (patch) | |
| tree | 03d503b7866baa121be781a1976c92453b8df59e | |
| parent | 9e4440d78505663847da31f6c0ead9881125d817 (diff) | |
| parent | 76229342e55f41f2a2a283de3fe0af4812ecad76 (diff) | |
Merge "Add SettingsSpinnerPreference"
6 files changed, 283 insertions, 2 deletions
diff --git a/packages/SettingsLib/SettingsSpinner/Android.bp b/packages/SettingsLib/SettingsSpinner/Android.bp index f18917cd2ebb..ca23616ca588 100644 --- a/packages/SettingsLib/SettingsSpinner/Android.bp +++ b/packages/SettingsLib/SettingsSpinner/Android.bp @@ -4,6 +4,10 @@ android_library { srcs: ["src/**/*.java"], resource_dirs: ["res"], + static_libs: [ + "androidx.preference_preference", + ], + sdk_version: "system_current", min_sdk_version: "21", } diff --git a/packages/SettingsLib/SettingsSpinner/res/layout/settings_spinner_preference.xml b/packages/SettingsLib/SettingsSpinner/res/layout/settings_spinner_preference.xml new file mode 100644 index 000000000000..7d5b6db6f6d6 --- /dev/null +++ b/packages/SettingsLib/SettingsSpinner/res/layout/settings_spinner_preference.xml @@ -0,0 +1,31 @@ +<?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. +--> + +<RelativeLayout + xmlns:android="http://schemas.android.com/apk/res/android" + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:minHeight="?android:attr/listPreferredItemHeight" + android:layout_marginStart="16dp" + android:layout_marginEnd="16dp"> + + <com.android.settingslib.widget.settingsspinner.SettingsSpinner + android:id="@+id/spinner" + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:layout_centerVertical="true"/> +</RelativeLayout> + diff --git a/packages/SettingsLib/SettingsSpinner/src/com/android/settingslib/widget/SettingsSpinnerPreference.java b/packages/SettingsLib/SettingsSpinner/src/com/android/settingslib/widget/SettingsSpinnerPreference.java new file mode 100644 index 000000000000..154a0f44788d --- /dev/null +++ b/packages/SettingsLib/SettingsSpinner/src/com/android/settingslib/widget/SettingsSpinnerPreference.java @@ -0,0 +1,128 @@ +/* + * 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 android.content.Context; +import android.util.AttributeSet; +import android.view.View; +import android.widget.AdapterView; + +import androidx.preference.Preference; +import androidx.preference.PreferenceViewHolder; + +import com.android.settingslib.widget.settingsspinner.SettingsSpinner; +import com.android.settingslib.widget.settingsspinner.SettingsSpinnerAdapter; + +/** + * This preference uses SettingsSpinner & SettingsSpinnerAdapter which provide default layouts for + * both view and drop down view of the Spinner. + */ +public class SettingsSpinnerPreference extends Preference { + + private SettingsSpinnerAdapter mAdapter; + private AdapterView.OnItemSelectedListener mListener; + private int mPosition; //Default 0 for internal shard storage. + + /** + * Perform inflation from XML and apply a class-specific base style. + * + * @param context The {@link Context} this is associated with, through which it can + * access the current theme, resources, {@link SharedPreferences}, etc. + * @param attrs The attributes of the XML tag that is inflating the preference + * @param defStyle An attribute in the current theme that contains a reference to a style + * resource that supplies default values for the view. Can be 0 to not + * look for defaults. + */ + public SettingsSpinnerPreference(Context context, AttributeSet attrs, int defStyle) { + super(context, attrs, defStyle); + setLayoutResource(R.layout.settings_spinner_preference); + } + + /** + * Perform inflation from XML and apply a class-specific base style. + * + * @param context The {@link Context} this is associated with, through which it can + * access the current theme, resources, {@link SharedPreferences}, etc. + * @param attrs The attributes of the XML tag that is inflating the preference + */ + public SettingsSpinnerPreference(Context context, AttributeSet attrs) { + super(context, attrs); + setLayoutResource(R.layout.settings_spinner_preference); + } + + /** + * Constructor to create a preference. + * + * @param context The Context this is associated with. + */ + public SettingsSpinnerPreference(Context context) { + this(context, null); + } + + /** Sets adapter of the spinner. */ + public <T extends SettingsSpinnerAdapter> void setAdapter(T adapter) { + mAdapter = adapter; + notifyChanged(); + } + + /** Sets item selection listener of the spinner. */ + public void setOnItemSelectedListener(AdapterView.OnItemSelectedListener listener) { + mListener = listener; + } + + /** Gets selected item of the spinner. */ + public Object getSelectedItem() { + return mAdapter == null ? null : mAdapter.getItem(mPosition); + } + + /** Gets selection position of the spinner */ + public void setSelection(int position) { + if (mPosition == position) { + return; + } + mPosition = position; + notifyChanged(); + } + + @Override + public void onBindViewHolder(PreferenceViewHolder holder) { + super.onBindViewHolder(holder); + final SettingsSpinner spinner = (SettingsSpinner) holder.findViewById(R.id.spinner); + spinner.setAdapter(mAdapter); + spinner.setSelection(mPosition); + spinner.setOnItemSelectedListener(mOnSelectedListener); + } + + private final AdapterView.OnItemSelectedListener mOnSelectedListener = + new AdapterView.OnItemSelectedListener() { + @Override + public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { + if (mPosition == position) return; + mPosition = position; + if (mListener != null) { + mListener.onItemSelected(parent, view, position, id); + } + } + + @Override + public void onNothingSelected(AdapterView<?> parent) { + if (mListener != null) { + mListener.onNothingSelected(parent); + } + } + }; +} diff --git a/packages/SettingsLib/SettingsSpinner/src/com/android/settingslib/widget/settingsspinner/SettingsSpinnerAdapter.java b/packages/SettingsLib/SettingsSpinner/src/com/android/settingslib/widget/settingsspinner/SettingsSpinnerAdapter.java index f9aedd9c07a4..a8ca0d8664f3 100644 --- a/packages/SettingsLib/SettingsSpinner/src/com/android/settingslib/widget/settingsspinner/SettingsSpinnerAdapter.java +++ b/packages/SettingsLib/SettingsSpinner/src/com/android/settingslib/widget/settingsspinner/SettingsSpinnerAdapter.java @@ -17,6 +17,9 @@ package com.android.settingslib.widget.settingsspinner; import android.content.Context; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; import android.widget.ArrayAdapter; import com.android.settingslib.widget.R; @@ -26,6 +29,11 @@ import com.android.settingslib.widget.R; */ public class SettingsSpinnerAdapter<T> extends ArrayAdapter<T> { + private static final int DEFAULT_RESOURCE = R.layout.settings_spinner_view; + private static final int DFAULT_DROPDOWN_RESOURCE = + android.R.layout.simple_spinner_dropdown_item; + private final LayoutInflater mDefaultInflater; + /** * Constructs a new SettingsSpinnerAdapter with the given context. * And it customizes title bar with a settings style. @@ -34,7 +42,24 @@ public class SettingsSpinnerAdapter<T> extends ArrayAdapter<T> { * access the current theme, resources, etc. */ public SettingsSpinnerAdapter(Context context) { - super(context, R.layout.settings_spinner_view); - setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); + super(context, DEFAULT_RESOURCE); + + setDropDownViewResource(DFAULT_DROPDOWN_RESOURCE); + mDefaultInflater = LayoutInflater.from(context); + } + + /** + * In overridded {@link #getView(int, View, ViewGroup)}, use this method to get default view. + */ + public View getDefaultView(int position, View convertView, ViewGroup parent) { + return mDefaultInflater.inflate(DEFAULT_RESOURCE, parent, false /* attachToRoot */); + } + + /** + * In overridded {@link #getDropDownView(int, View, ViewGroup)}, use this method to get default + * drop down view. + */ + public View getDefaultDropDownView(int position, View convertView, ViewGroup parent) { + return mDefaultInflater.inflate(DFAULT_DROPDOWN_RESOURCE, parent, false /* attachToRoot */); } } diff --git a/packages/SettingsLib/tests/integ/Android.bp b/packages/SettingsLib/tests/integ/Android.bp index f6f9dba784a0..92e32d96cdf3 100644 --- a/packages/SettingsLib/tests/integ/Android.bp +++ b/packages/SettingsLib/tests/integ/Android.bp @@ -38,6 +38,7 @@ android_test { "androidx.test.espresso.core", "mockito-target-minus-junit4", "truth-prebuilt", + "SettingsLibSettingsSpinner", "SettingsLibUsageProgressBarPreference", ], diff --git a/packages/SettingsLib/tests/integ/src/com/android/settingslib/widget/SettingsSpinnerPreferenceTest.java b/packages/SettingsLib/tests/integ/src/com/android/settingslib/widget/SettingsSpinnerPreferenceTest.java new file mode 100644 index 000000000000..53a382a9ebf6 --- /dev/null +++ b/packages/SettingsLib/tests/integ/src/com/android/settingslib/widget/SettingsSpinnerPreferenceTest.java @@ -0,0 +1,92 @@ +/* + * 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.LayoutInflater; +import android.view.View; +import android.widget.LinearLayout; + +import androidx.preference.PreferenceViewHolder; +import androidx.test.core.app.ApplicationProvider; +import androidx.test.runner.AndroidJUnit4; + +import com.android.settingslib.widget.settingsspinner.SettingsSpinner; +import com.android.settingslib.widget.settingsspinner.SettingsSpinnerAdapter; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; + +import java.util.ArrayList; +import java.util.List; + +@RunWith(AndroidJUnit4.class) +public class SettingsSpinnerPreferenceTest { + + private Context mContext; + private PreferenceViewHolder mViewHolder; + private SettingsSpinner mSpinner; + private SettingsSpinnerPreference mSpinnerPreference; + + @Before + public void setUp() { + mContext = ApplicationProvider.getApplicationContext(); + mSpinnerPreference = new SettingsSpinnerPreference(mContext); + final LayoutInflater inflater = LayoutInflater.from(mContext); + final View rootView = inflater.inflate(mSpinnerPreference.getLayoutResource(), + new LinearLayout(mContext), false /* attachToRoot */); + mViewHolder = PreferenceViewHolder.createInstanceForTests(rootView); + mSpinner = (SettingsSpinner) mViewHolder.findViewById(R.id.spinner); + } + + @Test + public void onBindViewHolder_noSetSelection_getDefaultItem() { + final List<CharSequence> list = new ArrayList<>(); + list.add("TEST1"); + list.add("TEST2"); + list.add("TEST3"); + final SettingsSpinnerAdapter adapter = new SettingsSpinnerAdapter(mContext); + adapter.addAll(list); + mSpinnerPreference.setAdapter(adapter); + + mSpinnerPreference.onBindViewHolder(mViewHolder); + + assertThat(adapter).isEqualTo(mSpinner.getAdapter()); + assertThat(mSpinnerPreference.getSelectedItem()) + .isEqualTo(mSpinner.getAdapter().getItem(0)); + } + + @Test + public void onBindViewHolder_setSelection_getSelectedItem() { + final List<CharSequence> list = new ArrayList<>(); + list.add("TEST1"); + list.add("TEST2"); + list.add("TEST3"); + final SettingsSpinnerAdapter adapter = new SettingsSpinnerAdapter(mContext); + adapter.addAll(list); + mSpinnerPreference.setAdapter(adapter); + mSpinnerPreference.setSelection(1); + + mSpinnerPreference.onBindViewHolder(mViewHolder); + + assertThat(mSpinnerPreference.getSelectedItem()) + .isEqualTo(mSpinner.getAdapter().getItem(1)); + } +} |