• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 package com.google.android.setupdesign;
18 
19 import android.content.Context;
20 import android.os.Bundle;
21 import androidx.recyclerview.widget.RecyclerView;
22 import android.util.AttributeSet;
23 import android.view.LayoutInflater;
24 import android.view.View;
25 import android.view.ViewGroup;
26 import com.google.android.setupcompat.util.ForceTwoPaneHelper;
27 import com.google.android.setupdesign.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 {@code
32  * app:preferenceTheme}.
33  *
34  * <p>Example:
35  *
36  * <pre>{@code
37  * &lt;style android:name="MyActivityTheme">
38  *     &lt;item android:name="preferenceTheme">@style/MyPreferenceTheme&lt;/item>
39  * &lt;/style>
40  *
41  * &lt;style android:name="MyPreferenceTheme">
42  *     &lt;item android:name="preferenceFragmentStyle">@style/MyPreferenceFragmentStyle&lt;/item>
43  * &lt;/style>
44  *
45  * &lt;style android:name="MyPreferenceFragmentStyle">
46  *     &lt;item android:name="android:layout">@layout/my_preference_layout&lt;/item>
47  * &lt;/style>
48  * }</pre>
49  *
50  * where {@code my_preference_layout} is a layout that contains {@link
51  * com.google.android.setupdesign.GlifPreferenceLayout}.
52  *
53  * <p>Example:
54  *
55  * <pre>{@code
56  * &lt;com.google.android.setupdesign.GlifPreferenceLayout
57  *     xmlns:android="http://schemas.android.com/apk/res/android"
58  *     android:id="@id/list_container"
59  *     android:layout_width="match_parent"
60  *     android:layout_height="match_parent" />
61  * }</pre>
62  *
63  * <p>Fragments using this layout <em>must</em> delegate {@code onCreateRecyclerView} to the
64  * implementation in this class: {@link #onCreateRecyclerView(android.view.LayoutInflater,
65  * android.view.ViewGroup, android.os.Bundle)}
66  */
67 public class GlifPreferenceLayout extends GlifRecyclerLayout {
68 
GlifPreferenceLayout(Context context)69   public GlifPreferenceLayout(Context context) {
70     super(context);
71   }
72 
GlifPreferenceLayout(Context context, int template, int containerId)73   public GlifPreferenceLayout(Context context, int template, int containerId) {
74     super(context, template, containerId);
75   }
76 
GlifPreferenceLayout(Context context, AttributeSet attrs)77   public GlifPreferenceLayout(Context context, AttributeSet attrs) {
78     super(context, attrs);
79   }
80 
GlifPreferenceLayout(Context context, AttributeSet attrs, int defStyleAttr)81   public GlifPreferenceLayout(Context context, AttributeSet attrs, int defStyleAttr) {
82     super(context, attrs, defStyleAttr);
83   }
84 
85   @Override
findContainer(int containerId)86   protected ViewGroup findContainer(int containerId) {
87     if (containerId == 0) {
88       containerId = R.id.sud_layout_content;
89     }
90     return super.findContainer(containerId);
91   }
92 
93   /** This method must be called in {@code PreferenceFragment#onCreateRecyclerView}. */
onCreateRecyclerView( LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState)94   public RecyclerView onCreateRecyclerView(
95       LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
96     return recyclerMixin.getRecyclerView();
97   }
98 
99   @Override
onInflateTemplate(LayoutInflater inflater, int template)100   protected View onInflateTemplate(LayoutInflater inflater, int template) {
101     if (template == 0) {
102       template = R.layout.sud_glif_preference_template;
103       // if the activity is embedded should apply an embedded layout.
104       if (isEmbeddedActivityOnePaneEnabled(getContext())) {
105         template = R.layout.sud_glif_preference_embedded_template;
106       } else if (ForceTwoPaneHelper.isForceTwoPaneEnable(getContext())) {
107         template = ForceTwoPaneHelper.getForceTwoPaneStyleLayout(getContext(), template);
108       }
109     }
110     return super.onInflateTemplate(inflater, template);
111   }
112 
113   @Override
onTemplateInflated()114   protected void onTemplateInflated() {
115     // Inflate the recycler view here, so attributes on the decoration views can be applied
116     // immediately.
117     final LayoutInflater inflater = LayoutInflater.from(getContext());
118     int recyclerViewLayoutId = R.layout.sud_glif_preference_recycler_view;
119     if (ForceTwoPaneHelper.isForceTwoPaneEnable(getContext())) {
120       recyclerViewLayoutId =
121           ForceTwoPaneHelper.getForceTwoPaneStyleLayout(getContext(), recyclerViewLayoutId);
122     }
123     RecyclerView recyclerView = (RecyclerView) inflater.inflate(recyclerViewLayoutId, this, false);
124     recyclerMixin = new RecyclerMixin(this, recyclerView);
125   }
126 }
127