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.support.v7.widget.LinearLayoutManager;
26import android.support.v7.widget.RecyclerView;
27import android.util.AttributeSet;
28import android.view.LayoutInflater;
29import android.view.View;
30import android.view.ViewGroup;
31
32import com.android.setupwizardlib.items.ItemGroup;
33import com.android.setupwizardlib.items.ItemInflater;
34import com.android.setupwizardlib.items.RecyclerItemAdapter;
35import com.android.setupwizardlib.util.DrawableLayoutDirectionHelper;
36import com.android.setupwizardlib.view.HeaderRecyclerView;
37
38/**
39 * A GLIF themed layout with a RecyclerView. {@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 GlifRecyclerLayout extends GlifLayout {
43
44    private RecyclerView mRecyclerView;
45    private View mHeader;
46
47    private DividerItemDecoration mDividerDecoration;
48    private Drawable mDefaultDivider;
49    private Drawable mDivider;
50    private int mDividerInset;
51
52    public GlifRecyclerLayout(Context context) {
53        this(context, 0, 0);
54    }
55
56    public GlifRecyclerLayout(Context context, int template) {
57        this(context, template, 0);
58    }
59
60    public GlifRecyclerLayout(Context context, int template, int containerId) {
61        super(context, template, containerId);
62        init(context, null, 0);
63    }
64
65    public GlifRecyclerLayout(Context context, AttributeSet attrs) {
66        super(context, attrs);
67        init(context, attrs, 0);
68    }
69
70    @TargetApi(VERSION_CODES.HONEYCOMB)
71    public GlifRecyclerLayout(Context context, AttributeSet attrs, int defStyleAttr) {
72        super(context, attrs, defStyleAttr);
73        init(context, attrs, defStyleAttr);
74    }
75
76    private void init(Context context, AttributeSet attrs, int defStyleAttr) {
77        final TypedArray a = context.obtainStyledAttributes(attrs,
78                R.styleable.SuwGlifRecyclerLayout, defStyleAttr, 0);
79        final int xml = a.getResourceId(R.styleable.SuwGlifRecyclerLayout_android_entries, 0);
80        if (xml != 0) {
81            final ItemGroup inflated = (ItemGroup) new ItemInflater(context).inflate(xml);
82            RecyclerItemAdapter adapter = new RecyclerItemAdapter(inflated);
83            adapter.setHasStableIds(a.getBoolean(
84                    R.styleable.SuwGlifRecyclerLayout_suwHasStableIds, false));
85            setAdapter(adapter);
86        }
87        int dividerInset =
88                a.getDimensionPixelSize(R.styleable.SuwGlifRecyclerLayout_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_recycler_template;
110        }
111        return super.onInflateTemplate(inflater, template);
112    }
113
114    @Override
115    protected ViewGroup findContainer(int containerId) {
116        if (containerId == 0) {
117            containerId = R.id.suw_recycler_view;
118        }
119        return super.findContainer(containerId);
120    }
121
122    @Override
123    protected void onTemplateInflated() {
124        initRecyclerView((RecyclerView) findViewById(R.id.suw_recycler_view));
125    }
126
127    protected void initRecyclerView(RecyclerView recyclerView) {
128        mRecyclerView = recyclerView;
129        mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
130        if (mRecyclerView instanceof HeaderRecyclerView) {
131            mHeader = ((HeaderRecyclerView) mRecyclerView).getHeader();
132        }
133        mDividerDecoration = DividerItemDecoration.getDefault(getContext());
134        mRecyclerView.addItemDecoration(mDividerDecoration);
135    }
136
137    @Override
138    protected View findManagedViewById(int id) {
139        if (mHeader != null) {
140            final View view = mHeader.findViewById(id);
141            if (view != null) {
142                return view;
143            }
144        }
145        return super.findViewById(id);
146    }
147
148    public RecyclerView getRecyclerView() {
149        return mRecyclerView;
150    }
151
152    public void setAdapter(RecyclerView.Adapter adapter) {
153        getRecyclerView().setAdapter(adapter);
154    }
155
156    public RecyclerView.Adapter getAdapter() {
157        final RecyclerView.Adapter adapter = getRecyclerView().getAdapter();
158        if (adapter instanceof HeaderRecyclerView.HeaderAdapter) {
159            return ((HeaderRecyclerView.HeaderAdapter) adapter).getWrappedAdapter();
160        }
161        return adapter;
162    }
163
164    /**
165     * Sets the start inset of the divider. This will use the default divider drawable set in the
166     * theme and inset it {@code inset} pixels to the right (or left in RTL layouts).
167     *
168     * @param inset The number of pixels to inset on the "start" side of the list divider. Typically
169     *              this will be either {@code @dimen/suw_items_glif_icon_divider_inset} or
170     *              {@code @dimen/suw_items_glif_text_divider_inset}.
171     */
172    public void setDividerInset(int inset) {
173        mDividerInset = inset;
174        updateDivider();
175    }
176
177    public int getDividerInset() {
178        return mDividerInset;
179    }
180
181    private void updateDivider() {
182        boolean shouldUpdate = true;
183        if (Build.VERSION.SDK_INT >= VERSION_CODES.KITKAT) {
184            shouldUpdate = isLayoutDirectionResolved();
185        }
186        if (shouldUpdate) {
187            if (mDefaultDivider == null) {
188                mDefaultDivider = mDividerDecoration.getDivider();
189            }
190            mDivider = DrawableLayoutDirectionHelper.createRelativeInsetDrawable(mDefaultDivider,
191                    mDividerInset /* start */, 0 /* top */, 0 /* end */, 0 /* bottom */, this);
192            mDividerDecoration.setDivider(mDivider);
193        }
194    }
195
196    public Drawable getDivider() {
197        return mDivider;
198    }
199}
200