Home | History | Annotate | Download | only in items
      1 /*
      2  * Copyright (C) 2015 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.setupwizardlib.items;
     18 
     19 import android.util.SparseIntArray;
     20 import android.view.LayoutInflater;
     21 import android.view.View;
     22 import android.view.ViewGroup;
     23 import android.widget.BaseAdapter;
     24 
     25 /**
     26  * An adapter typically used with ListView to display an
     27  * {@link com.android.setupwizardlib.items.ItemHierarchy}. The item hierarchy used to create this
     28  * adapter can be inflated by {@link ItemInflater} from XML.
     29  */
     30 public class ItemAdapter extends BaseAdapter implements ItemHierarchy.Observer {
     31 
     32     private final ItemHierarchy mItemHierarchy;
     33     private ViewTypes mViewTypes = new ViewTypes();
     34 
     35     public ItemAdapter(ItemHierarchy hierarchy) {
     36         mItemHierarchy = hierarchy;
     37         mItemHierarchy.registerObserver(this);
     38         refreshViewTypes();
     39     }
     40 
     41     @Override
     42     public int getCount() {
     43         return mItemHierarchy.getCount();
     44     }
     45 
     46     @Override
     47     public IItem getItem(int position) {
     48         return mItemHierarchy.getItemAt(position);
     49     }
     50 
     51     @Override
     52     public long getItemId(int position) {
     53         return position;
     54     }
     55 
     56     @Override
     57     public int getItemViewType(int position) {
     58         IItem item = getItem(position);
     59         int layoutRes = item.getLayoutResource();
     60         return mViewTypes.get(layoutRes);
     61     }
     62 
     63     @Override
     64     public int getViewTypeCount() {
     65         return mViewTypes.size();
     66     }
     67 
     68     private void refreshViewTypes() {
     69         for (int i = 0; i < getCount(); i++) {
     70             IItem item = getItem(i);
     71             mViewTypes.add(item.getLayoutResource());
     72         }
     73     }
     74 
     75     @Override
     76     public View getView(int position, View convertView, ViewGroup parent) {
     77         IItem item = getItem(position);
     78         if (convertView == null) {
     79             LayoutInflater inflater = LayoutInflater.from(parent.getContext());
     80             convertView = inflater.inflate(item.getLayoutResource(), parent, false);
     81         }
     82         item.onBindView(convertView);
     83         return convertView;
     84     }
     85 
     86     @Override
     87     public void onChanged(ItemHierarchy hierarchy) {
     88         refreshViewTypes();
     89         notifyDataSetChanged();
     90     }
     91 
     92     @Override
     93     public void onItemRangeChanged(ItemHierarchy itemHierarchy, int positionStart, int itemCount) {
     94         onChanged(itemHierarchy);
     95     }
     96 
     97     @Override
     98     public void onItemRangeInserted(ItemHierarchy itemHierarchy, int positionStart, int itemCount) {
     99         onChanged(itemHierarchy);
    100     }
    101 
    102     @Override
    103     public void onItemRangeMoved(ItemHierarchy itemHierarchy, int fromPosition, int toPosition,
    104             int itemCount) {
    105         onChanged(itemHierarchy);
    106     }
    107 
    108     @Override
    109     public void onItemRangeRemoved(ItemHierarchy itemHierarchy, int positionStart, int itemCount) {
    110         onChanged(itemHierarchy);
    111     }
    112 
    113     @Override
    114     public boolean isEnabled(int position) {
    115         return getItem(position).isEnabled();
    116     }
    117 
    118     public ItemHierarchy findItemById(int id) {
    119         return mItemHierarchy.findItemById(id);
    120     }
    121 
    122     public ItemHierarchy getRootItemHierarchy() {
    123         return mItemHierarchy;
    124     }
    125 
    126     /**
    127      * A helper class to pack a sparse set of integers (e.g. resource IDs) to a contiguous list of
    128      * integers (e.g. adapter positions), providing mapping to retrieve the original ID from a given
    129      * position. This is used to pack the view types of the adapter into contiguous integers from
    130      * a given layout resource.
    131      */
    132     private static class ViewTypes {
    133         private SparseIntArray mPositionMap = new SparseIntArray();
    134         private int nextPosition = 0;
    135 
    136         public int add(int id) {
    137             if (mPositionMap.indexOfKey(id) < 0) {
    138                 mPositionMap.put(id, nextPosition);
    139                 nextPosition++;
    140             }
    141             return mPositionMap.get(id);
    142         }
    143 
    144         public int size() {
    145             return mPositionMap.size();
    146         }
    147 
    148         public int get(int id) {
    149             return mPositionMap.get(id);
    150         }
    151     }
    152 }
    153