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
diff --git a/packages/SettingsLib/SettingsSpinner/Android.bp b/packages/SettingsLib/SettingsSpinner/Android.bp
index f18917c..ca23616 100644
--- a/packages/SettingsLib/SettingsSpinner/Android.bp
+++ b/packages/SettingsLib/SettingsSpinner/Android.bp
@@ -4,6 +4,10 @@
     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 0000000..7d5b6db
--- /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 0000000..154a0f4
--- /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 f9aedd9..a8ca0d8 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 @@
  */
 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 @@
      *                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 */);
     }
 }