1 /*
2  * Copyright (C) 2022 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.settingslib.widget;
18 
19 import android.content.Context;
20 import android.os.Build;
21 import android.view.LayoutInflater;
22 import android.view.View;
23 import android.view.ViewGroup;
24 import android.widget.ArrayAdapter;
25 
26 /**
27  * An ArrayAdapter which was used by Spinner with settings style.
28  * @param <T> the data type to be loaded.
29  */
30 public class SettingsSpinnerAdapter<T> extends ArrayAdapter<T> {
31 
32     private static final int DEFAULT_RESOURCE = R.layout.settings_spinner_view;
33     private static final int DFAULT_DROPDOWN_RESOURCE = R.layout.settings_spinner_dropdown_view;
34     private final LayoutInflater mDefaultInflater;
35 
36     /**
37      * Constructs a new SettingsSpinnerAdapter with the given context.
38      * And it customizes title bar with a settings style.
39      *
40      * @param context The Context the view is running in, through which it can
41      *                access the current theme, resources, etc.
42      */
SettingsSpinnerAdapter(Context context)43     public SettingsSpinnerAdapter(Context context) {
44         super(context, getDefaultResource());
45 
46         setDropDownViewResource(getDropdownResource());
47         mDefaultInflater = LayoutInflater.from(context);
48     }
49 
50     /**
51      * In overridded {@link #getView(int, View, ViewGroup)}, use this method to get default view.
52      */
getDefaultView(int position, View convertView, ViewGroup parent)53     public View getDefaultView(int position, View convertView, ViewGroup parent) {
54         return mDefaultInflater.inflate(getDefaultResource(), parent, false /* attachToRoot */);
55     }
56 
57     /**
58      * In overridded {@link #getDropDownView(int, View, ViewGroup)}, use this method to get default
59      * drop down view.
60      */
getDefaultDropDownView(int position, View convertView, ViewGroup parent)61     public View getDefaultDropDownView(int position, View convertView, ViewGroup parent) {
62         return mDefaultInflater.inflate(getDropdownResource(), parent, false /* attachToRoot */);
63     }
64 
getDefaultResource()65     private static int getDefaultResource() {
66         return (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU)
67                 ? DEFAULT_RESOURCE : android.R.layout.simple_spinner_dropdown_item;
68     }
getDropdownResource()69     private static int getDropdownResource() {
70         return (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU)
71                 ? DFAULT_DROPDOWN_RESOURCE : android.R.layout.simple_spinner_dropdown_item;
72     }
73 }
74