1b01f3ef075d501d1f61e6f61794a5cadd3ff2026Maurice Lam/*
2b01f3ef075d501d1f61e6f61794a5cadd3ff2026Maurice Lam * Copyright (C) 2015 The Android Open Source Project
3b01f3ef075d501d1f61e6f61794a5cadd3ff2026Maurice Lam *
4b01f3ef075d501d1f61e6f61794a5cadd3ff2026Maurice Lam * Licensed under the Apache License, Version 2.0 (the "License");
5b01f3ef075d501d1f61e6f61794a5cadd3ff2026Maurice Lam * you may not use this file except in compliance with the License.
6b01f3ef075d501d1f61e6f61794a5cadd3ff2026Maurice Lam * You may obtain a copy of the License at
7b01f3ef075d501d1f61e6f61794a5cadd3ff2026Maurice Lam *
8b01f3ef075d501d1f61e6f61794a5cadd3ff2026Maurice Lam *      http://www.apache.org/licenses/LICENSE-2.0
9b01f3ef075d501d1f61e6f61794a5cadd3ff2026Maurice Lam *
10b01f3ef075d501d1f61e6f61794a5cadd3ff2026Maurice Lam * Unless required by applicable law or agreed to in writing, software
11b01f3ef075d501d1f61e6f61794a5cadd3ff2026Maurice Lam * distributed under the License is distributed on an "AS IS" BASIS,
12b01f3ef075d501d1f61e6f61794a5cadd3ff2026Maurice Lam * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13b01f3ef075d501d1f61e6f61794a5cadd3ff2026Maurice Lam * See the License for the specific language governing permissions and
14b01f3ef075d501d1f61e6f61794a5cadd3ff2026Maurice Lam * limitations under the License.
15b01f3ef075d501d1f61e6f61794a5cadd3ff2026Maurice Lam */
16b01f3ef075d501d1f61e6f61794a5cadd3ff2026Maurice Lam
17b01f3ef075d501d1f61e6f61794a5cadd3ff2026Maurice Lampackage com.android.setupwizardlib;
18b01f3ef075d501d1f61e6f61794a5cadd3ff2026Maurice Lam
19b01f3ef075d501d1f61e6f61794a5cadd3ff2026Maurice Lamimport android.annotation.TargetApi;
20b01f3ef075d501d1f61e6f61794a5cadd3ff2026Maurice Lamimport android.content.Context;
21180360409c9e4e9163c670ff48663244b4057eafMaurice Lamimport android.graphics.drawable.Drawable;
22b01f3ef075d501d1f61e6f61794a5cadd3ff2026Maurice Lamimport android.os.Build.VERSION_CODES;
23b01f3ef075d501d1f61e6f61794a5cadd3ff2026Maurice Lamimport android.support.v7.widget.RecyclerView;
2403b52ec3584d8f5fd25d73bd933226a150b85642Maurice Lamimport android.support.v7.widget.RecyclerView.Adapter;
259ac3d54040b4dae6addc224fd8a69bf62ee84a83Maurice Lamimport android.support.v7.widget.RecyclerView.ViewHolder;
26b01f3ef075d501d1f61e6f61794a5cadd3ff2026Maurice Lamimport android.util.AttributeSet;
27b01f3ef075d501d1f61e6f61794a5cadd3ff2026Maurice Lamimport android.view.LayoutInflater;
28b01f3ef075d501d1f61e6f61794a5cadd3ff2026Maurice Lamimport android.view.View;
29b01f3ef075d501d1f61e6f61794a5cadd3ff2026Maurice Lamimport android.view.ViewGroup;
30b01f3ef075d501d1f61e6f61794a5cadd3ff2026Maurice Lam
3103b52ec3584d8f5fd25d73bd933226a150b85642Maurice Lamimport com.android.setupwizardlib.template.RecyclerMixin;
320ceb8d53e39ebb5bc103863787afb39ec5c41ad8Maurice Lamimport com.android.setupwizardlib.template.RecyclerViewScrollHandlingDelegate;
330ceb8d53e39ebb5bc103863787afb39ec5c41ad8Maurice Lamimport com.android.setupwizardlib.template.RequireScrollMixin;
34b01f3ef075d501d1f61e6f61794a5cadd3ff2026Maurice Lam
35b01f3ef075d501d1f61e6f61794a5cadd3ff2026Maurice Lam/**
36b01f3ef075d501d1f61e6f61794a5cadd3ff2026Maurice Lam * A GLIF themed layout with a RecyclerView. {@code android:entries} can also be used to specify an
37b01f3ef075d501d1f61e6f61794a5cadd3ff2026Maurice Lam * {@link com.android.setupwizardlib.items.ItemHierarchy} to be used with this layout in XML.
38b01f3ef075d501d1f61e6f61794a5cadd3ff2026Maurice Lam */
39b01f3ef075d501d1f61e6f61794a5cadd3ff2026Maurice Lampublic class GlifRecyclerLayout extends GlifLayout {
40b01f3ef075d501d1f61e6f61794a5cadd3ff2026Maurice Lam
4103b52ec3584d8f5fd25d73bd933226a150b85642Maurice Lam    protected RecyclerMixin mRecyclerMixin;
42b01f3ef075d501d1f61e6f61794a5cadd3ff2026Maurice Lam
43b01f3ef075d501d1f61e6f61794a5cadd3ff2026Maurice Lam    public GlifRecyclerLayout(Context context) {
44b01f3ef075d501d1f61e6f61794a5cadd3ff2026Maurice Lam        this(context, 0, 0);
45b01f3ef075d501d1f61e6f61794a5cadd3ff2026Maurice Lam    }
46b01f3ef075d501d1f61e6f61794a5cadd3ff2026Maurice Lam
47b01f3ef075d501d1f61e6f61794a5cadd3ff2026Maurice Lam    public GlifRecyclerLayout(Context context, int template) {
48b01f3ef075d501d1f61e6f61794a5cadd3ff2026Maurice Lam        this(context, template, 0);
49b01f3ef075d501d1f61e6f61794a5cadd3ff2026Maurice Lam    }
50b01f3ef075d501d1f61e6f61794a5cadd3ff2026Maurice Lam
51b01f3ef075d501d1f61e6f61794a5cadd3ff2026Maurice Lam    public GlifRecyclerLayout(Context context, int template, int containerId) {
52b01f3ef075d501d1f61e6f61794a5cadd3ff2026Maurice Lam        super(context, template, containerId);
53b01f3ef075d501d1f61e6f61794a5cadd3ff2026Maurice Lam        init(context, null, 0);
54b01f3ef075d501d1f61e6f61794a5cadd3ff2026Maurice Lam    }
55b01f3ef075d501d1f61e6f61794a5cadd3ff2026Maurice Lam
56b01f3ef075d501d1f61e6f61794a5cadd3ff2026Maurice Lam    public GlifRecyclerLayout(Context context, AttributeSet attrs) {
57b01f3ef075d501d1f61e6f61794a5cadd3ff2026Maurice Lam        super(context, attrs);
58b01f3ef075d501d1f61e6f61794a5cadd3ff2026Maurice Lam        init(context, attrs, 0);
59b01f3ef075d501d1f61e6f61794a5cadd3ff2026Maurice Lam    }
60b01f3ef075d501d1f61e6f61794a5cadd3ff2026Maurice Lam
61b01f3ef075d501d1f61e6f61794a5cadd3ff2026Maurice Lam    @TargetApi(VERSION_CODES.HONEYCOMB)
62b01f3ef075d501d1f61e6f61794a5cadd3ff2026Maurice Lam    public GlifRecyclerLayout(Context context, AttributeSet attrs, int defStyleAttr) {
63b01f3ef075d501d1f61e6f61794a5cadd3ff2026Maurice Lam        super(context, attrs, defStyleAttr);
64b01f3ef075d501d1f61e6f61794a5cadd3ff2026Maurice Lam        init(context, attrs, defStyleAttr);
65b01f3ef075d501d1f61e6f61794a5cadd3ff2026Maurice Lam    }
66b01f3ef075d501d1f61e6f61794a5cadd3ff2026Maurice Lam
67b01f3ef075d501d1f61e6f61794a5cadd3ff2026Maurice Lam    private void init(Context context, AttributeSet attrs, int defStyleAttr) {
6803b52ec3584d8f5fd25d73bd933226a150b85642Maurice Lam        mRecyclerMixin.parseAttributes(attrs, defStyleAttr);
6903b52ec3584d8f5fd25d73bd933226a150b85642Maurice Lam        registerMixin(RecyclerMixin.class, mRecyclerMixin);
700ceb8d53e39ebb5bc103863787afb39ec5c41ad8Maurice Lam
710ceb8d53e39ebb5bc103863787afb39ec5c41ad8Maurice Lam        final RequireScrollMixin requireScrollMixin = getMixin(RequireScrollMixin.class);
720ceb8d53e39ebb5bc103863787afb39ec5c41ad8Maurice Lam        requireScrollMixin.setScrollHandlingDelegate(
730ceb8d53e39ebb5bc103863787afb39ec5c41ad8Maurice Lam                new RecyclerViewScrollHandlingDelegate(requireScrollMixin, getRecyclerView()));
74b01f3ef075d501d1f61e6f61794a5cadd3ff2026Maurice Lam    }
75b01f3ef075d501d1f61e6f61794a5cadd3ff2026Maurice Lam
76b01f3ef075d501d1f61e6f61794a5cadd3ff2026Maurice Lam    @Override
77180360409c9e4e9163c670ff48663244b4057eafMaurice Lam    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
78180360409c9e4e9163c670ff48663244b4057eafMaurice Lam        super.onLayout(changed, left, top, right, bottom);
7903b52ec3584d8f5fd25d73bd933226a150b85642Maurice Lam        mRecyclerMixin.onLayout();
80180360409c9e4e9163c670ff48663244b4057eafMaurice Lam    }
81180360409c9e4e9163c670ff48663244b4057eafMaurice Lam
82180360409c9e4e9163c670ff48663244b4057eafMaurice Lam    @Override
83b01f3ef075d501d1f61e6f61794a5cadd3ff2026Maurice Lam    protected View onInflateTemplate(LayoutInflater inflater, int template) {
84b01f3ef075d501d1f61e6f61794a5cadd3ff2026Maurice Lam        if (template == 0) {
85b01f3ef075d501d1f61e6f61794a5cadd3ff2026Maurice Lam            template = R.layout.suw_glif_recycler_template;
86b01f3ef075d501d1f61e6f61794a5cadd3ff2026Maurice Lam        }
87b01f3ef075d501d1f61e6f61794a5cadd3ff2026Maurice Lam        return super.onInflateTemplate(inflater, template);
88b01f3ef075d501d1f61e6f61794a5cadd3ff2026Maurice Lam    }
89b01f3ef075d501d1f61e6f61794a5cadd3ff2026Maurice Lam
90b01f3ef075d501d1f61e6f61794a5cadd3ff2026Maurice Lam    @Override
9103b52ec3584d8f5fd25d73bd933226a150b85642Maurice Lam    protected void onTemplateInflated() {
9203b52ec3584d8f5fd25d73bd933226a150b85642Maurice Lam        final View recyclerView = findViewById(R.id.suw_recycler_view);
9303b52ec3584d8f5fd25d73bd933226a150b85642Maurice Lam        if (recyclerView instanceof RecyclerView) {
9403b52ec3584d8f5fd25d73bd933226a150b85642Maurice Lam            mRecyclerMixin = new RecyclerMixin(this, (RecyclerView) recyclerView);
9503b52ec3584d8f5fd25d73bd933226a150b85642Maurice Lam        } else {
9603b52ec3584d8f5fd25d73bd933226a150b85642Maurice Lam            throw new IllegalStateException(
9703b52ec3584d8f5fd25d73bd933226a150b85642Maurice Lam                    "GlifRecyclerLayout should use a template with recycler view");
98b01f3ef075d501d1f61e6f61794a5cadd3ff2026Maurice Lam        }
99b01f3ef075d501d1f61e6f61794a5cadd3ff2026Maurice Lam    }
100b01f3ef075d501d1f61e6f61794a5cadd3ff2026Maurice Lam
101b01f3ef075d501d1f61e6f61794a5cadd3ff2026Maurice Lam    @Override
10203b52ec3584d8f5fd25d73bd933226a150b85642Maurice Lam    protected ViewGroup findContainer(int containerId) {
10303b52ec3584d8f5fd25d73bd933226a150b85642Maurice Lam        if (containerId == 0) {
10403b52ec3584d8f5fd25d73bd933226a150b85642Maurice Lam            containerId = R.id.suw_recycler_view;
105b01f3ef075d501d1f61e6f61794a5cadd3ff2026Maurice Lam        }
10603b52ec3584d8f5fd25d73bd933226a150b85642Maurice Lam        return super.findContainer(containerId);
107b01f3ef075d501d1f61e6f61794a5cadd3ff2026Maurice Lam    }
108b01f3ef075d501d1f61e6f61794a5cadd3ff2026Maurice Lam
109b01f3ef075d501d1f61e6f61794a5cadd3ff2026Maurice Lam    @Override
1107514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam    public View findManagedViewById(int id) {
11103b52ec3584d8f5fd25d73bd933226a150b85642Maurice Lam        final View header = mRecyclerMixin.getHeader();
11203b52ec3584d8f5fd25d73bd933226a150b85642Maurice Lam        if (header != null) {
11303b52ec3584d8f5fd25d73bd933226a150b85642Maurice Lam            final View view = header.findViewById(id);
11484979a6b2874d09762404c3c8a902f4aad016d01Maurice Lam            if (view != null) {
11584979a6b2874d09762404c3c8a902f4aad016d01Maurice Lam                return view;
11684979a6b2874d09762404c3c8a902f4aad016d01Maurice Lam            }
11784979a6b2874d09762404c3c8a902f4aad016d01Maurice Lam        }
11884979a6b2874d09762404c3c8a902f4aad016d01Maurice Lam        return super.findViewById(id);
119b01f3ef075d501d1f61e6f61794a5cadd3ff2026Maurice Lam    }
120b01f3ef075d501d1f61e6f61794a5cadd3ff2026Maurice Lam
12103b52ec3584d8f5fd25d73bd933226a150b85642Maurice Lam    /**
12203b52ec3584d8f5fd25d73bd933226a150b85642Maurice Lam     * @see RecyclerMixin#setDividerItemDecoration(DividerItemDecoration)
12303b52ec3584d8f5fd25d73bd933226a150b85642Maurice Lam     */
124325b78cfbd0d7366771ee81838bc444c69e39963Maurice Lam    public void setDividerItemDecoration(DividerItemDecoration decoration) {
12503b52ec3584d8f5fd25d73bd933226a150b85642Maurice Lam        mRecyclerMixin.setDividerItemDecoration(decoration);
126325b78cfbd0d7366771ee81838bc444c69e39963Maurice Lam    }
127325b78cfbd0d7366771ee81838bc444c69e39963Maurice Lam
12803b52ec3584d8f5fd25d73bd933226a150b85642Maurice Lam    /**
12903b52ec3584d8f5fd25d73bd933226a150b85642Maurice Lam     * @see RecyclerMixin#getRecyclerView()
13003b52ec3584d8f5fd25d73bd933226a150b85642Maurice Lam     */
131b01f3ef075d501d1f61e6f61794a5cadd3ff2026Maurice Lam    public RecyclerView getRecyclerView() {
13203b52ec3584d8f5fd25d73bd933226a150b85642Maurice Lam        return mRecyclerMixin.getRecyclerView();
133b01f3ef075d501d1f61e6f61794a5cadd3ff2026Maurice Lam    }
134b01f3ef075d501d1f61e6f61794a5cadd3ff2026Maurice Lam
13503b52ec3584d8f5fd25d73bd933226a150b85642Maurice Lam    /**
13603b52ec3584d8f5fd25d73bd933226a150b85642Maurice Lam     * @see RecyclerMixin#setAdapter(Adapter)
13703b52ec3584d8f5fd25d73bd933226a150b85642Maurice Lam     */
1389ac3d54040b4dae6addc224fd8a69bf62ee84a83Maurice Lam    public void setAdapter(Adapter<? extends ViewHolder> adapter) {
13903b52ec3584d8f5fd25d73bd933226a150b85642Maurice Lam        mRecyclerMixin.setAdapter(adapter);
140b01f3ef075d501d1f61e6f61794a5cadd3ff2026Maurice Lam    }
141b01f3ef075d501d1f61e6f61794a5cadd3ff2026Maurice Lam
14203b52ec3584d8f5fd25d73bd933226a150b85642Maurice Lam    /**
14303b52ec3584d8f5fd25d73bd933226a150b85642Maurice Lam     * @see RecyclerMixin#getAdapter()
14403b52ec3584d8f5fd25d73bd933226a150b85642Maurice Lam     */
1459ac3d54040b4dae6addc224fd8a69bf62ee84a83Maurice Lam    public Adapter<? extends ViewHolder> getAdapter() {
14603b52ec3584d8f5fd25d73bd933226a150b85642Maurice Lam        return mRecyclerMixin.getAdapter();
147b01f3ef075d501d1f61e6f61794a5cadd3ff2026Maurice Lam    }
148180360409c9e4e9163c670ff48663244b4057eafMaurice Lam
149180360409c9e4e9163c670ff48663244b4057eafMaurice Lam    /**
15003b52ec3584d8f5fd25d73bd933226a150b85642Maurice Lam     * @see RecyclerMixin#setDividerInset(int)
151180360409c9e4e9163c670ff48663244b4057eafMaurice Lam     */
152180360409c9e4e9163c670ff48663244b4057eafMaurice Lam    public void setDividerInset(int inset) {
15303b52ec3584d8f5fd25d73bd933226a150b85642Maurice Lam        mRecyclerMixin.setDividerInset(inset);
154180360409c9e4e9163c670ff48663244b4057eafMaurice Lam    }
155180360409c9e4e9163c670ff48663244b4057eafMaurice Lam
15603b52ec3584d8f5fd25d73bd933226a150b85642Maurice Lam    /**
15703b52ec3584d8f5fd25d73bd933226a150b85642Maurice Lam     * @see RecyclerMixin#getDividerInset()
15803b52ec3584d8f5fd25d73bd933226a150b85642Maurice Lam     */
159180360409c9e4e9163c670ff48663244b4057eafMaurice Lam    public int getDividerInset() {
16003b52ec3584d8f5fd25d73bd933226a150b85642Maurice Lam        return mRecyclerMixin.getDividerInset();
161180360409c9e4e9163c670ff48663244b4057eafMaurice Lam    }
162180360409c9e4e9163c670ff48663244b4057eafMaurice Lam
16303b52ec3584d8f5fd25d73bd933226a150b85642Maurice Lam    /**
16403b52ec3584d8f5fd25d73bd933226a150b85642Maurice Lam     * @see RecyclerMixin#getDivider()
16503b52ec3584d8f5fd25d73bd933226a150b85642Maurice Lam     */
166180360409c9e4e9163c670ff48663244b4057eafMaurice Lam    public Drawable getDivider() {
16703b52ec3584d8f5fd25d73bd933226a150b85642Maurice Lam        return mRecyclerMixin.getDivider();
168180360409c9e4e9163c670ff48663244b4057eafMaurice Lam    }
169b01f3ef075d501d1f61e6f61794a5cadd3ff2026Maurice Lam}
170