• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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.settings.accessibility;
18 
19 import android.content.Context;
20 import android.text.TextUtils;
21 import android.view.View;
22 import android.view.ViewGroup;
23 import android.widget.ArrayAdapter;
24 import android.widget.ImageView;
25 import android.widget.TextView;
26 
27 import androidx.annotation.DrawableRes;
28 import androidx.annotation.NonNull;
29 import androidx.annotation.Nullable;
30 
31 import com.android.settings.R;
32 
33 import java.util.List;
34 
35 /**
36  * An {@link ArrayAdapter} to fill the information of {@link ItemInfo} in the item view. The item
37  * view must have textview to set the title.
38  *
39  * @param <T> the type of elements in the array, inherited from {@link ItemInfo}.
40  */
41 public class ItemInfoArrayAdapter<T extends ItemInfoArrayAdapter.ItemInfo> extends ArrayAdapter<T> {
42 
ItemInfoArrayAdapter(@onNull Context context, @NonNull List<T> items)43     public ItemInfoArrayAdapter(@NonNull Context context, @NonNull List<T> items) {
44         super(context, R.layout.dialog_single_radio_choice_list_item, R.id.title, items);
45     }
46 
47     @NonNull
48     @Override
getView(int position, @Nullable View convertView, @NonNull ViewGroup parent)49     public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
50         final View root = super.getView(position, convertView, parent);
51 
52         final ItemInfo item = getItem(position);
53         final TextView title = root.findViewById(R.id.title);
54         title.setText(item.mTitle);
55         final TextView summary = root.findViewById(R.id.summary);
56         if (!TextUtils.isEmpty(item.mSummary)) {
57             summary.setVisibility(View.VISIBLE);
58             summary.setText(item.mSummary);
59         } else {
60             summary.setVisibility(View.GONE);
61         }
62         final ImageView image = root.findViewById(R.id.image);
63         if (item.mDrawableId == null) {
64             image.setVisibility(View.GONE);
65         } else {
66             image.setVisibility(View.VISIBLE);
67             image.setImageResource(item.mDrawableId);
68             if (getContext().getResources().getConfiguration().getLayoutDirection()
69                     == View.LAYOUT_DIRECTION_LTR) {
70                 image.setScaleType(ImageView.ScaleType.FIT_START);
71             } else {
72                 image.setScaleType(ImageView.ScaleType.FIT_END);
73             }
74         }
75         return root;
76     }
77 
78     /**
79      * Presents a data structure shown in the item view.
80      */
81     public static class ItemInfo {
82         @NonNull
83         public final CharSequence mTitle;
84         @Nullable
85         public final CharSequence mSummary;
86         @Nullable
87         @DrawableRes
88         public final Integer mDrawableId;
89 
ItemInfo(@onNull CharSequence title, @Nullable CharSequence summary, @Nullable @DrawableRes Integer drawableId)90         public ItemInfo(@NonNull CharSequence title, @Nullable CharSequence summary,
91                 @Nullable @DrawableRes Integer drawableId) {
92             mTitle = title;
93             mSummary = summary;
94             mDrawableId = drawableId;
95         }
96     }
97 }
98