TermsListAdapter.java revision 2060bd8c315260de75a8680c2de84554bbaf526b
1/*
2 * Copyright 2016, 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 */
16package com.android.managedprovisioning.preprovisioning.terms;
17
18import static com.android.internal.util.Preconditions.checkNotNull;
19
20import android.text.method.LinkMovementMethod;
21import android.view.LayoutInflater;
22import android.view.View;
23import android.view.ViewGroup;
24import android.view.accessibility.AccessibilityNodeInfo;
25import android.widget.BaseExpandableListAdapter;
26import android.widget.ImageView;
27import android.widget.TextView;
28
29import com.android.managedprovisioning.R;
30
31import java.util.List;
32
33/**
34 * Allows for displaying {@link TermsDocument} objects in an
35 * {@link android.widget.ExpandableListView}.
36 */
37class TermsListAdapter extends BaseExpandableListAdapter {
38    private final List<TermsDocument> mTermsDocuments;
39    private final LayoutInflater mInflater;
40    private final GroupExpandedInfo mGroupExpandedInfo;
41
42    /**
43     * Creates a new instance of the class.
44     */
45    TermsListAdapter(List<TermsDocument> termsDocuments, LayoutInflater layoutInflater,
46            GroupExpandedInfo groupExpandedInfo) {
47        mTermsDocuments = checkNotNull(termsDocuments);
48        mInflater = checkNotNull(layoutInflater);
49        mGroupExpandedInfo = checkNotNull(groupExpandedInfo);
50    }
51
52    @Override
53    public int getGroupCount() {
54        return mTermsDocuments.size();
55    }
56
57    @Override
58    public int getChildrenCount(int groupPosition) {
59        return 1; // one content piece per header
60    }
61
62    @Override
63    public TermsDocument getGroup(int groupPosition) {
64        return getDisclaimer(groupPosition);
65    }
66
67    @Override
68    public TermsDocument getChild(int groupPosition, int childPosition) {
69        return getDisclaimer(groupPosition);
70    }
71
72    @Override
73    public long getGroupId(int groupPosition) {
74        return groupPosition;
75    }
76
77    @Override
78    public long getChildId(int groupPosition, int childPosition) {
79        return childPosition;
80    }
81
82    @Override
83    public boolean hasStableIds() {
84        return true;
85    }
86
87    // TODO: encapsulate this logic - too much direct view manipulation
88    @Override
89    public View getGroupView(int groupPosition, boolean isExpanded, View convertView,
90            ViewGroup parent) {
91        String heading = getDisclaimer(groupPosition).getHeading();
92
93        View groupView = convertView != null ? convertView : mInflater.inflate(
94                R.layout.terms_disclaimer_header, parent, false);
95        groupView.setContentDescription(
96                parent.getResources().getString(R.string.section_heading, heading));
97        groupView.setAccessibilityDelegate(new View.AccessibilityDelegate() {
98            @Override public void onInitializeAccessibilityNodeInfo(View host,
99                    AccessibilityNodeInfo info) {
100                super.onInitializeAccessibilityNodeInfo(host, info);
101                info.addAction(new AccessibilityNodeInfo.AccessibilityAction(
102                        AccessibilityNodeInfo.AccessibilityAction.ACTION_CLICK.getId(),
103                        parent.getResources().getString(
104                                isExpanded ? R.string.collapse : R.string.expand)));
105            }
106        });
107
108        TextView textView = groupView.findViewById(R.id.header_text);
109        textView.setText(heading);
110
111        ImageView chevron = groupView.findViewById(R.id.chevron);
112        chevron.setRotation(isExpanded ? 90 : -90); // chevron down / up retrospectively
113        groupView.findViewById(R.id.divider).setVisibility(
114                shouldShowGroupDivider(groupPosition) ? View.VISIBLE : View.INVISIBLE);
115
116        return groupView;
117    }
118
119    /**
120     * Helps avoid a double thick divider line: one above header, one from the bottom of prev child
121     */
122    private boolean shouldShowGroupDivider(int groupPosition) {
123        return mGroupExpandedInfo.isGroupExpanded(groupPosition)
124                && (groupPosition == 0 || !mGroupExpandedInfo.isGroupExpanded(groupPosition - 1));
125    }
126
127    // TODO: encapsulate this logic - too much direct view manipulation
128    @Override
129    public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
130            View convertView, ViewGroup parent) {
131        View view = convertView != null ? convertView : mInflater.inflate(
132                R.layout.terms_disclaimer_content, parent, false);
133
134        TermsDocument disclaimer = getDisclaimer(groupPosition);
135        TextView textView = view.findViewById(R.id.disclaimer_content);
136        textView.setText(disclaimer.getContent());
137        textView.setContentDescription(
138                parent.getResources().getString(R.string.section_content, disclaimer.getHeading(),
139                        disclaimer.getContent()));
140        textView.setMovementMethod(LinkMovementMethod.getInstance()); // makes html links clickable
141
142        return view;
143    }
144
145    private TermsDocument getDisclaimer(int index) {
146        return mTermsDocuments.get(index);
147    }
148
149    @Override
150    public boolean isChildSelectable(int groupPosition, int childPosition) {
151        return false;
152    }
153
154    interface GroupExpandedInfo {
155        boolean isGroupExpanded(int groupPosition);
156    }
157}