1 /*
2  * Copyright (C) 2015 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 package com.android.settings.applications;
17 
18 import android.content.Context;
19 import android.content.res.TypedArray;
20 import android.util.AttributeSet;
21 import android.view.ViewGroup.LayoutParams;
22 
23 import androidx.core.content.res.TypedArrayUtils;
24 import androidx.preference.Preference;
25 import androidx.preference.PreferenceViewHolder;
26 
27 import com.android.settings.R;
28 
29 /**
30  * A blank preference that has a specified height by android:layout_height.  It can be used
31  * to fine tune screens that combine custom layouts and standard preferences.
32  */
33 public class SpacePreference extends Preference {
34 
35     private int mHeight;
36 
SpacePreference(Context context, AttributeSet attrs)37     public SpacePreference(Context context, AttributeSet attrs) {
38         this(context, attrs, TypedArrayUtils.getAttr(context,
39                 androidx.preference.R.attr.preferenceStyle,
40                 android.R.attr.preferenceStyle));
41     }
42 
SpacePreference(Context context, AttributeSet attrs, int defStyleAttr)43     public SpacePreference(Context context, AttributeSet attrs, int defStyleAttr) {
44         this(context, attrs, defStyleAttr, 0);
45     }
46 
SpacePreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)47     public SpacePreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
48         super(context, attrs, defStyleAttr, defStyleRes);
49         setLayoutResource(R.layout.space_preference);
50 
51         final TypedArray a = context.obtainStyledAttributes(attrs,
52                 new int[] { com.android.internal.R.attr.layout_height }, defStyleAttr, defStyleRes);
53         mHeight = a.getDimensionPixelSize(0, 0);
54         a.recycle();
55     }
56 
setHeight(int height)57     public void setHeight(int height) {
58         mHeight = height;
59     }
60 
61     @Override
onBindViewHolder(PreferenceViewHolder view)62     public void onBindViewHolder(PreferenceViewHolder view) {
63         super.onBindViewHolder(view);
64 
65         LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, mHeight);
66         view.itemView.setLayoutParams(params);
67     }
68 
69 }
70