• 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         image.setImageResource(item.mDrawableId);
64         if (getContext().getResources().getConfiguration().getLayoutDirection()
65                 == View.LAYOUT_DIRECTION_LTR) {
66             image.setScaleType(ImageView.ScaleType.FIT_START);
67         } else {
68             image.setScaleType(ImageView.ScaleType.FIT_END);
69         }
70         return root;
71     }
72 
73     /**
74      * Presents a data structure shown in the item view.
75      */
76     public static class ItemInfo {
77         @NonNull
78         public final CharSequence mTitle;
79         @Nullable
80         public final CharSequence mSummary;
81         @DrawableRes
82         public final int mDrawableId;
83 
ItemInfo(@onNull CharSequence title, @Nullable CharSequence summary, @DrawableRes int drawableId)84         public ItemInfo(@NonNull CharSequence title, @Nullable CharSequence summary,
85                 @DrawableRes int drawableId) {
86             mTitle = title;
87             mSummary = summary;
88             mDrawableId = drawableId;
89         }
90     }
91 }
92