GlifListLayout.java revision 180360409c9e4e9163c670ff48663244b4057eaf
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
17package com.android.setupwizardlib;
18
19import android.annotation.TargetApi;
20import android.content.Context;
21import android.content.res.TypedArray;
22import android.graphics.drawable.Drawable;
23import android.os.Build;
24import android.os.Build.VERSION_CODES;
25import android.util.AttributeSet;
26import android.view.LayoutInflater;
27import android.view.View;
28import android.view.ViewGroup;
29import android.widget.ListAdapter;
30import android.widget.ListView;
31
32import com.android.setupwizardlib.items.ItemAdapter;
33import com.android.setupwizardlib.items.ItemGroup;
34import com.android.setupwizardlib.items.ItemInflater;
35import com.android.setupwizardlib.util.DrawableLayoutDirectionHelper;
36
37/**
38 * A GLIF themed layout with a ListView. {@code android:entries} can also be used to specify an
39 * {@link com.android.setupwizardlib.items.ItemHierarchy} to be used with this layout in XML.
40 */
41public class GlifListLayout extends GlifLayout {
42
43    /* static section */
44
45    private static final String TAG = "GlifListLayout";
46
47    /* non-static section */
48
49    private ListView mListView;
50    private Drawable mDivider;
51    private Drawable mDefaultDivider;
52    private int mDividerInset;
53
54    public GlifListLayout(Context context) {
55        this(context, 0, 0);
56    }
57
58    public GlifListLayout(Context context, int template) {
59        this(context, template, 0);
60    }
61
62    public GlifListLayout(Context context, int template, int containerId) {
63        super(context, template, containerId);
64        init(context, null, 0);
65    }
66
67    public GlifListLayout(Context context, AttributeSet attrs) {
68        super(context, attrs);
69        init(context, attrs, 0);
70    }
71
72    @TargetApi(VERSION_CODES.HONEYCOMB)
73    public GlifListLayout(Context context, AttributeSet attrs, int defStyleAttr) {
74        super(context, attrs, defStyleAttr);
75        init(context, attrs, defStyleAttr);
76    }
77
78    private void init(Context context, AttributeSet attrs, int defStyleAttr) {
79        final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.SuwGlifListLayout,
80                defStyleAttr, 0);
81        final int xml = a.getResourceId(R.styleable.SuwGlifListLayout_android_entries, 0);
82        if (xml != 0) {
83            final ItemGroup inflated = (ItemGroup) new ItemInflater(context).inflate(xml);
84            setAdapter(new ItemAdapter(inflated));
85        }
86        int dividerInset =
87                a.getDimensionPixelSize(R.styleable.SuwGlifListLayout_suwDividerInset, 0);
88        if (dividerInset == 0) {
89            dividerInset = getResources()
90                    .getDimensionPixelSize(R.dimen.suw_items_icon_divider_inset);
91        }
92        setDividerInset(dividerInset);
93        a.recycle();
94    }
95
96    @Override
97    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
98        super.onLayout(changed, left, top, right, bottom);
99        if (mDivider == null) {
100            // Update divider in case layout direction has just been resolved
101            updateDivider();
102        }
103    }
104
105    @Override
106    protected View onInflateTemplate(LayoutInflater inflater, int template) {
107        if (template == 0) {
108            template = R.layout.suw_glif_list_template;
109        }
110        return super.onInflateTemplate(inflater, template);
111    }
112
113    @Override
114    protected ViewGroup findContainer(int containerId) {
115        if (containerId == 0) {
116            containerId = android.R.id.list;
117        }
118        return super.findContainer(containerId);
119    }
120
121    @Override
122    protected void onTemplateInflated() {
123        mListView = (ListView) findViewById(android.R.id.list);
124    }
125
126    public ListView getListView() {
127        return mListView;
128    }
129
130    public void setAdapter(ListAdapter adapter) {
131        getListView().setAdapter(adapter);
132    }
133
134    public ListAdapter getAdapter() {
135        return getListView().getAdapter();
136    }
137
138    /**
139     * Sets the start inset of the divider. This will use the default divider drawable set in the
140     * theme and inset it {@code inset} pixels to the right (or left in RTL layouts).
141     *
142     * @param inset The number of pixels to inset on the "start" side of the list divider. Typically
143     *              this will be either {@code @dimen/suw_items_icon_divider_inset} or
144     *              {@code @dimen/suw_items_text_divider_inset}.
145     */
146    public void setDividerInset(int inset) {
147        mDividerInset = inset;
148        updateDivider();
149    }
150
151    public int getDividerInset() {
152        return mDividerInset;
153    }
154
155    private void updateDivider() {
156        boolean shouldUpdate = true;
157        if (Build.VERSION.SDK_INT >= VERSION_CODES.KITKAT) {
158            shouldUpdate = isLayoutDirectionResolved();
159        }
160        if (shouldUpdate) {
161            final ListView listView = getListView();
162            if (mDefaultDivider == null) {
163                mDefaultDivider = listView.getDivider();
164            }
165            mDivider = DrawableLayoutDirectionHelper.createRelativeInsetDrawable(mDefaultDivider,
166                    mDividerInset /* start */, 0 /* top */, 0 /* end */, 0 /* bottom */, this);
167            listView.setDivider(mDivider);
168        }
169    }
170
171    public Drawable getDivider() {
172        return mDivider;
173    }
174}
175