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