summaryrefslogtreecommitdiff
path: root/library/main/src/com/android/setupwizardlib/items/ItemAdapter.java
blob: 851736c846c2fcd5d7108108bb5b6e40164282f3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.setupwizardlib.items;

import android.util.SparseIntArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

/**
 * An adapter typically used with ListView to display an {@link
 * com.android.setupwizardlib.items.ItemHierarchy}. The item hierarchy used to create this adapter
 * can be inflated by {@link ItemInflater} from XML.
 */
public class ItemAdapter extends BaseAdapter implements ItemHierarchy.Observer {

  private final ItemHierarchy itemHierarchy;
  private final ViewTypes viewTypes = new ViewTypes();

  public ItemAdapter(ItemHierarchy hierarchy) {
    itemHierarchy = hierarchy;
    itemHierarchy.registerObserver(this);
    refreshViewTypes();
  }

  @Override
  public int getCount() {
    return itemHierarchy.getCount();
  }

  @Override
  public IItem getItem(int position) {
    return itemHierarchy.getItemAt(position);
  }

  @Override
  public long getItemId(int position) {
    return position;
  }

  @Override
  public int getItemViewType(int position) {
    IItem item = getItem(position);
    int layoutRes = item.getLayoutResource();
    return viewTypes.get(layoutRes);
  }

  @Override
  public int getViewTypeCount() {
    return viewTypes.size();
  }

  private void refreshViewTypes() {
    for (int i = 0; i < getCount(); i++) {
      IItem item = getItem(i);
      viewTypes.add(item.getLayoutResource());
    }
  }

  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    IItem item = getItem(position);
    if (convertView == null) {
      LayoutInflater inflater = LayoutInflater.from(parent.getContext());
      convertView = inflater.inflate(item.getLayoutResource(), parent, false);
    }
    item.onBindView(convertView);
    return convertView;
  }

  @Override
  public void onChanged(ItemHierarchy hierarchy) {
    refreshViewTypes();
    notifyDataSetChanged();
  }

  @Override
  public void onItemRangeChanged(ItemHierarchy itemHierarchy, int positionStart, int itemCount) {
    onChanged(itemHierarchy);
  }

  @Override
  public void onItemRangeInserted(ItemHierarchy itemHierarchy, int positionStart, int itemCount) {
    onChanged(itemHierarchy);
  }

  @Override
  public void onItemRangeMoved(
      ItemHierarchy itemHierarchy, int fromPosition, int toPosition, int itemCount) {
    onChanged(itemHierarchy);
  }

  @Override
  public void onItemRangeRemoved(ItemHierarchy itemHierarchy, int positionStart, int itemCount) {
    onChanged(itemHierarchy);
  }

  @Override
  public boolean isEnabled(int position) {
    return getItem(position).isEnabled();
  }

  public ItemHierarchy findItemById(int id) {
    return itemHierarchy.findItemById(id);
  }

  public ItemHierarchy getRootItemHierarchy() {
    return itemHierarchy;
  }

  /**
   * A helper class to pack a sparse set of integers (e.g. resource IDs) to a contiguous list of
   * integers (e.g. adapter positions), providing mapping to retrieve the original ID from a given
   * position. This is used to pack the view types of the adapter into contiguous integers from a
   * given layout resource.
   */
  private static class ViewTypes {
    private final SparseIntArray positionMap = new SparseIntArray();
    private int nextPosition = 0;

    public int add(int id) {
      if (positionMap.indexOfKey(id) < 0) {
        positionMap.put(id, nextPosition);
        nextPosition++;
      }
      return positionMap.get(id);
    }

    public int size() {
      return positionMap.size();
    }

    public int get(int id) {
      return positionMap.get(id);
    }
  }
}