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