Add SettingsSpinnerPreference

This preference uses SettingsSpinner & SettingsSpinnerAdapter
which provide default layouts for both view and drop down view
of the Spinner.

Bug: 177617478
Test: atest SettingsSpinnerPreferenceTest
Change-Id: I9ceb2db6d82692ee21b6d96af7b42c49400d5071
This commit is contained in:
Arc Wang
2021-01-13 16:39:01 +08:00
parent b57d8d8a26
commit 76229342e5
6 changed files with 283 additions and 2 deletions

View File

@@ -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",
}

View File

@@ -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>

View File

@@ -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);
}
}
};
}

View File

@@ -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 */);
}
}

View File

@@ -38,6 +38,7 @@ android_test {
"androidx.test.espresso.core",
"mockito-target-minus-junit4",
"truth-prebuilt",
"SettingsLibSettingsSpinner",
"SettingsLibUsageProgressBarPreference",
],

View File

@@ -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));
}
}