• 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.applications.intentpicker;
18 
19 import android.content.Context;
20 import android.graphics.drawable.Drawable;
21 import android.util.Log;
22 import android.view.LayoutInflater;
23 import android.view.View;
24 import android.view.ViewGroup;
25 import android.widget.BaseAdapter;
26 import android.widget.CheckedTextView;
27 
28 import com.android.settings.R;
29 
30 import java.util.List;
31 
32 /** This adapter is for supported links dialog. */
33 public class SupportedLinksAdapter extends BaseAdapter {
34     private static final String TAG = "SupportedLinksAdapter";
35     private final Context mContext;
36     private final List<SupportedLinkWrapper> mWrapperList;
37 
SupportedLinksAdapter(Context context, List<SupportedLinkWrapper> list)38     public SupportedLinksAdapter(Context context, List<SupportedLinkWrapper> list) {
39         mContext = context;
40         mWrapperList = list;
41     }
42 
43     @Override
getCount()44     public int getCount() {
45         return mWrapperList.size();
46     }
47 
48     @Override
getItem(int position)49     public Object getItem(int position) {
50         if (position < mWrapperList.size()) {
51             return mWrapperList.get(position);
52         }
53         return null;
54     }
55 
56     @Override
getItemId(int position)57     public long getItemId(int position) {
58         return position;
59     }
60 
61     @Override
getView(int position, View convertView, ViewGroup parent)62     public View getView(int position, View convertView, ViewGroup parent) {
63         if (convertView == null) {
64             convertView = LayoutInflater.from(mContext).inflate(
65                     R.layout.supported_links_dialog_item, /* root= */ null);
66         }
67         final CheckedTextView textView = convertView.findViewById(android.R.id.text1);
68         Drawable[] drawables = textView.getCompoundDrawables();
69         if (mContext.getResources().getConfiguration().getLayoutDirection()
70                 == View.LAYOUT_DIRECTION_RTL && drawables[0] != null) {
71             Log.d(TAG, "getView: RTL direction.");
72             // Set a checkbox position. It is same as the android:drawableRight attribute.
73             textView.setCompoundDrawables(/* left= */ null, /* top= */ null, drawables[0],
74                 /* bottom= */ null);
75         }
76         textView.setText(mWrapperList.get(position).getDisplayTitle(mContext));
77         textView.setEnabled(mWrapperList.get(position).isEnabled());
78         textView.setChecked(mWrapperList.get(position).isChecked());
79         textView.setOnClickListener(l -> {
80             textView.toggle();
81             mWrapperList.get(position).setChecked(textView.isChecked());
82         });
83         return convertView;
84     }
85 }
86