Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 148195ee authored by Arc Wang's avatar Arc Wang Committed by Android (Google) Code Review
Browse files

Merge "Add SettingsSpinnerPreference"

parents 9e4440d7 76229342
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
@@ -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",
}
+31 −0
Original line number Diff line number Diff line
<?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>
+128 −0
Original line number Diff line number Diff line
/*
 * 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);
            }
        }
    };
}
+27 −2
Original line number Diff line number Diff line
@@ -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 */);
    }
}
+1 −0
Original line number Diff line number Diff line
@@ -38,6 +38,7 @@ android_test {
        "androidx.test.espresso.core",
        "mockito-target-minus-junit4",
        "truth-prebuilt",
        "SettingsLibSettingsSpinner",
        "SettingsLibUsageProgressBarPreference",
    ],

Loading