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.GlifPreferenceLayout}.
51 *
52 * <p />Example:
53 * <pre>{@code
54 * &lt;com.android.setupwizardlib.GlifPreferenceLayout
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,
64 * android.os.Bundle)}
65 */
66public class GlifPreferenceLayout extends GlifRecyclerLayout {
67
68    public GlifPreferenceLayout(Context context) {
69        super(context);
70    }
71
72    public GlifPreferenceLayout(Context context, int template, int containerId) {
73        super(context, template, containerId);
74    }
75
76    public GlifPreferenceLayout(Context context, AttributeSet attrs) {
77        super(context, attrs);
78    }
79
80    public GlifPreferenceLayout(Context context, AttributeSet attrs, int defStyleAttr) {
81        super(context, attrs, defStyleAttr);
82    }
83
84    @Override
85    protected ViewGroup findContainer(int containerId) {
86        if (containerId == 0) {
87            containerId = R.id.suw_layout_content;
88        }
89        return super.findContainer(containerId);
90    }
91
92    /**
93     * This method must be called in {@code PreferenceFragment#onCreateRecyclerView}.
94     */
95    public RecyclerView onCreateRecyclerView(LayoutInflater inflater, ViewGroup parent,
96            Bundle savedInstanceState) {
97        return mRecyclerMixin.getRecyclerView();
98    }
99
100    @Override
101    protected View onInflateTemplate(LayoutInflater inflater, int template) {
102        if (template == 0) {
103            template = R.layout.suw_glif_preference_template;
104        }
105        return super.onInflateTemplate(inflater, template);
106    }
107
108    @Override
109    protected void onTemplateInflated() {
110        // Inflate the recycler view here, so attributes on the decoration views can be applied
111        // immediately.
112        final LayoutInflater inflater = LayoutInflater.from(getContext());
113        RecyclerView recyclerView = (RecyclerView) inflater.inflate(
114                R.layout.suw_glif_preference_recycler_view, this, false);
115        mRecyclerMixin = new RecyclerMixin(this, recyclerView);
116    }
117}
118