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.content.Context;
20import android.content.res.TypedArray;
21import android.graphics.drawable.Drawable;
22import android.os.Build;
23import android.support.v7.widget.LinearLayoutManager;
24import android.support.v7.widget.RecyclerView;
25import android.util.AttributeSet;
26import android.util.Log;
27import android.view.LayoutInflater;
28import android.view.View;
29import android.view.ViewGroup;
30
31import com.android.setupwizardlib.items.ItemGroup;
32import com.android.setupwizardlib.items.ItemInflater;
33import com.android.setupwizardlib.items.RecyclerItemAdapter;
34import com.android.setupwizardlib.util.DrawableLayoutDirectionHelper;
35import com.android.setupwizardlib.util.RecyclerViewRequireScrollHelper;
36import com.android.setupwizardlib.view.HeaderRecyclerView;
37import com.android.setupwizardlib.view.NavigationBar;
38
39/**
40 * A setup wizard layout for use with {@link android.support.v7.widget.RecyclerView}.
41 * {@code android:entries} can also be used to specify an
42 * {@link com.android.setupwizardlib.items.ItemHierarchy} to be used with this layout in XML.
43 *
44 * @see SetupWizardItemsLayout
45 */
46public class SetupWizardRecyclerLayout extends SetupWizardLayout {
47
48    private static final String TAG = "RecyclerLayout";
49
50    private RecyclerView.Adapter mAdapter;
51    private RecyclerView mRecyclerView;
52    private View mHeader;
53
54    private DividerItemDecoration mDividerDecoration;
55    private Drawable mDefaultDivider;
56    private Drawable mDivider;
57    private int mDividerInset;
58
59    public SetupWizardRecyclerLayout(Context context) {
60        this(context, 0, 0);
61    }
62
63    public SetupWizardRecyclerLayout(Context context, int template, int containerId) {
64        super(context, template, containerId);
65        init(context, null, 0);
66    }
67
68    public SetupWizardRecyclerLayout(Context context, AttributeSet attrs) {
69        super(context, attrs);
70        init(context, attrs, 0);
71    }
72
73    public SetupWizardRecyclerLayout(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,
80                R.styleable.SuwSetupWizardRecyclerItemsLayout, defStyleAttr, 0);
81        final int xml = a.getResourceId(
82                R.styleable.SuwSetupWizardRecyclerItemsLayout_android_entries, 0);
83        if (xml != 0) {
84            final ItemGroup inflated = (ItemGroup) new ItemInflater(context).inflate(xml);
85            mAdapter = new RecyclerItemAdapter(inflated);
86            mAdapter.setHasStableIds(a.getBoolean(
87                    R.styleable.SuwSetupWizardRecyclerItemsLayout_suwHasStableIds, false));
88            setAdapter(mAdapter);
89        }
90        int dividerInset = a.getDimensionPixelSize(
91                R.styleable.SuwSetupWizardRecyclerItemsLayout_suwDividerInset, 0);
92        if (dividerInset == 0) {
93            dividerInset = getResources()
94                    .getDimensionPixelSize(R.dimen.suw_items_icon_divider_inset);
95        }
96        setDividerInset(dividerInset);
97        a.recycle();
98    }
99
100    @Override
101    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
102        super.onLayout(changed, left, top, right, bottom);
103        if (mDivider == null) {
104            // Update divider in case layout direction has just been resolved
105            updateDivider();
106        }
107    }
108
109    public RecyclerView.Adapter getAdapter() {
110        return mAdapter;
111    }
112
113    public void setAdapter(RecyclerView.Adapter adapter) {
114        mAdapter = adapter;
115        getRecyclerView().setAdapter(adapter);
116    }
117
118    public RecyclerView getRecyclerView() {
119        return mRecyclerView;
120    }
121
122    @Override
123    protected ViewGroup findContainer(int containerId) {
124        if (containerId == 0) {
125            containerId = R.id.suw_recycler_view;
126        }
127        return super.findContainer(containerId);
128    }
129
130    @Override
131    protected void onTemplateInflated() {
132        initRecyclerView((RecyclerView) findViewById(R.id.suw_recycler_view));
133    }
134
135    protected void initRecyclerView(RecyclerView recyclerView) {
136        mRecyclerView = recyclerView;
137        mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
138        if (mRecyclerView instanceof HeaderRecyclerView) {
139            mHeader = ((HeaderRecyclerView) mRecyclerView).getHeader();
140        }
141        mDividerDecoration = new DividerItemDecoration(getContext());
142        mRecyclerView.addItemDecoration(mDividerDecoration);
143    }
144
145    @Override
146    protected View onInflateTemplate(LayoutInflater inflater, int template) {
147        if (template == 0) {
148            template = R.layout.suw_recycler_template;
149        }
150        return super.onInflateTemplate(inflater, template);
151    }
152
153    @Override
154    protected View findManagedViewById(int id) {
155        if (mHeader != null) {
156            final View view = mHeader.findViewById(id);
157            if (view != null) {
158                return view;
159            }
160        }
161        return super.findViewById(id);
162    }
163
164    @Override
165    public void requireScrollToBottom() {
166        final NavigationBar navigationBar = getNavigationBar();
167        final RecyclerView recyclerView = getRecyclerView();
168        if (navigationBar != null && recyclerView != null) {
169            RecyclerViewRequireScrollHelper.requireScroll(navigationBar, recyclerView);
170        } else {
171            Log.e(TAG, "Both suw_layout_navigation_bar and suw_recycler_view must exist in"
172                    + " the template to require scrolling.");
173        }
174    }
175
176    /**
177     * Sets the start inset of the divider. This will use the default divider drawable set in the
178     * theme and inset it {@code inset} pixels to the right (or left in RTL layouts).
179     *
180     * @param inset The number of pixels to inset on the "start" side of the list divider. Typically
181     *              this will be either {@code @dimen/suw_items_icon_divider_inset} or
182     *              {@code @dimen/suw_items_text_divider_inset}.
183     */
184    public void setDividerInset(int inset) {
185        mDividerInset = inset;
186        updateDivider();
187    }
188
189    public int getDividerInset() {
190        return mDividerInset;
191    }
192
193    private void updateDivider() {
194        boolean shouldUpdate = true;
195        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
196            shouldUpdate = isLayoutDirectionResolved();
197        }
198        if (shouldUpdate) {
199            if (mDefaultDivider == null) {
200                mDefaultDivider = mDividerDecoration.getDivider();
201            }
202            mDivider = DrawableLayoutDirectionHelper.createRelativeInsetDrawable(mDefaultDivider,
203                    mDividerInset /* start */, 0 /* top */, 0 /* end */, 0 /* bottom */, this);
204            mDividerDecoration.setDivider(mDivider);
205        }
206    }
207
208    public Drawable getDivider() {
209        return mDivider;
210    }
211}
212