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.view.LayoutInflater;
21 import android.view.View;
22 import android.view.ViewGroup;
23 import android.widget.BaseAdapter;
24 import android.widget.CheckedTextView;
25 
26 import com.android.settings.R;
27 
28 import java.util.List;
29 
30 /** This adapter is for supported links dialog. */
31 public class SupportedLinksAdapter extends BaseAdapter {
32     private final Context mContext;
33     private final List<SupportedLinkWrapper> mWrapperList;
34 
SupportedLinksAdapter(Context context, List<SupportedLinkWrapper> list)35     public SupportedLinksAdapter(Context context, List<SupportedLinkWrapper> list) {
36         mContext = context;
37         mWrapperList = list;
38     }
39 
40     @Override
getCount()41     public int getCount() {
42         return mWrapperList.size();
43     }
44 
45     @Override
getItem(int position)46     public Object getItem(int position) {
47         if (position < mWrapperList.size()) {
48             return mWrapperList.get(position);
49         }
50         return null;
51     }
52 
53     @Override
getItemId(int position)54     public long getItemId(int position) {
55         return position;
56     }
57 
58     @Override
getView(int position, View convertView, ViewGroup parent)59     public View getView(int position, View convertView, ViewGroup parent) {
60         if (convertView == null) {
61             convertView = LayoutInflater.from(mContext).inflate(
62                     R.layout.supported_links_dialog_item, /* root= */ null);
63         }
64         final CheckedTextView textView = convertView.findViewById(android.R.id.text1);
65         textView.setText(mWrapperList.get(position).getDisplayTitle(mContext));
66         textView.setEnabled(mWrapperList.get(position).isEnabled());
67         textView.setChecked(mWrapperList.get(position).isChecked());
68         textView.setOnClickListener(l -> {
69             textView.toggle();
70             mWrapperList.get(position).setChecked(textView.isChecked());
71         });
72         return convertView;
73     }
74 }
75