1/*
2 * Copyright (C) 2016 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.os.Bundle;
21import android.support.v7.widget.RecyclerView;
22import android.util.AttributeSet;
23import android.view.LayoutInflater;
24import android.view.View;
25import android.view.ViewGroup;
26
27/**
28 * A layout to be used with {@code PreferenceFragment} in v14 support library. This can be specified
29 * as the {@code android:layout} in the {@code app:preferenceFragmentStyle} in
30 * {@code app:preferenceTheme}.
31 *
32 * <p />Example:
33 * <pre>{@code
34 * &lt;style android:name="MyActivityTheme">
35 *     &lt;item android:name="preferenceTheme">@style/MyPreferenceTheme&lt;/item>
36 * &lt;/style>
37 *
38 * &lt;style android:name="MyPreferenceTheme">
39 *     &lt;item android:name="preferenceFragmentStyle">@style/MyPreferenceFragmentStyle&lt;/item>
40 * &lt;/style>
41 *
42 * &lt;style android:name="MyPreferenceFragmentStyle">
43 *     &lt;item android:name="android:layout">@layout/my_preference_layout&lt;/item>
44 * &lt;/style>
45 * }</pre>
46 *
47 * where {@code my_preference_layout} is a layout that contains
48 * {@link com.android.setupwizardlib.SetupWizardPreferenceLayout}.
49 *
50 * <p />Example:
51 * <pre>{@code
52 * &lt;com.android.setupwizardlib.SetupWizardPreferenceLayout
53 *     xmlns:android="http://schemas.android.com/apk/res/android"
54 *     android:id="@id/list_container"
55 *     android:layout_width="match_parent"
56 *     android:layout_height="match_parent" />
57 * }</pre>
58 *
59 * <p />Fragments using this layout <em>must</em> delegate {@code onCreateRecyclerView} to the
60 * implementation in this class:
61 * {@link #onCreateRecyclerView(android.view.LayoutInflater, android.view.ViewGroup, android.os.Bundle)}
62 */
63public class SetupWizardPreferenceLayout extends SetupWizardRecyclerLayout {
64
65    private RecyclerView mRecyclerView;
66
67    public SetupWizardPreferenceLayout(Context context) {
68        super(context);
69    }
70
71    public SetupWizardPreferenceLayout(Context context, int template, int containerId) {
72        super(context, template, containerId);
73    }
74
75    public SetupWizardPreferenceLayout(Context context, AttributeSet attrs) {
76        super(context, attrs);
77    }
78
79    public SetupWizardPreferenceLayout(Context context, AttributeSet attrs, int defStyleAttr) {
80        super(context, attrs, defStyleAttr);
81    }
82
83    public RecyclerView getRecyclerView() {
84        return mRecyclerView;
85    }
86
87    @Override
88    protected ViewGroup findContainer(int containerId) {
89        if (containerId == 0) {
90            containerId = R.id.suw_layout_content;
91        }
92        return super.findContainer(containerId);
93    }
94
95    /**
96     * This method must be called in {@code PreferenceFragment#onCreateRecyclerView}.
97     */
98    public RecyclerView onCreateRecyclerView(LayoutInflater inflater, ViewGroup parent,
99            Bundle savedInstanceState) {
100        return mRecyclerView;
101    }
102
103    @Override
104    protected View onInflateTemplate(LayoutInflater inflater, int template) {
105        if (template == 0) {
106            template = R.layout.suw_preference_template;
107        }
108        return super.onInflateTemplate(inflater, template);
109    }
110
111    @Override
112    protected void onTemplateInflated() {
113        // Inflate the recycler view here, so attributes on the decoration views can be applied
114        // immediately.
115        final LayoutInflater inflater = LayoutInflater.from(getContext());
116        mRecyclerView = (RecyclerView) inflater.inflate(R.layout.suw_preference_recycler_view,
117                this, false);
118        initRecyclerView(mRecyclerView);
119    }
120}
121