• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 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.android.settings.widget;
18 
19 import android.view.View;
20 
21 import androidx.preference.Preference;
22 import androidx.preference.PreferenceViewHolder;
23 
24 import com.android.settings.R;
25 import com.android.settings.flags.Flags;
26 import com.android.settingslib.widget.SettingsThemeHelper;
27 
28 /** Helper for homepage preference to manage layout. */
29 public class HomepagePreferenceLayoutHelper {
30 
31     private View mIcon;
32     private View mText;
33     private boolean mIconVisible = true;
34     private int mIconPaddingStart = -1;
35     private int mTextPaddingStart = -1;
36 
37     /** The interface for managing preference layouts on homepage */
38     public interface HomepagePreferenceLayout {
39         /** Returns a {@link HomepagePreferenceLayoutHelper}  */
getHelper()40         HomepagePreferenceLayoutHelper getHelper();
41     }
42 
HomepagePreferenceLayoutHelper(Preference preference)43     public HomepagePreferenceLayoutHelper(Preference preference) {
44         preference.setLayoutResource(
45                 Flags.homepageRevamp()
46                         ? SettingsThemeHelper.isExpressiveTheme(preference.getContext())
47                                 ? R.layout.homepage_preference_expressive
48                                 : R.layout.homepage_preference_v2
49                         : R.layout.homepage_preference);
50     }
51 
52     /** Sets whether the icon should be visible */
setIconVisible(boolean visible)53     public void setIconVisible(boolean visible) {
54         mIconVisible = visible;
55         if (mIcon != null) {
56             mIcon.setVisibility(visible ? View.VISIBLE : View.GONE);
57         }
58     }
59 
60     /** Sets the icon padding start */
setIconPaddingStart(int paddingStart)61     public void setIconPaddingStart(int paddingStart) {
62         mIconPaddingStart = paddingStart;
63         if (mIcon != null && paddingStart >= 0) {
64             mIcon.setPaddingRelative(paddingStart, mIcon.getPaddingTop(), mIcon.getPaddingEnd(),
65                     mIcon.getPaddingBottom());
66         }
67     }
68 
69     /** Sets the text padding start */
setTextPaddingStart(int paddingStart)70     public void setTextPaddingStart(int paddingStart) {
71         mTextPaddingStart = paddingStart;
72         if (mText != null && paddingStart >= 0) {
73             mText.setPaddingRelative(paddingStart, mText.getPaddingTop(), mText.getPaddingEnd(),
74                     mText.getPaddingBottom());
75         }
76     }
77 
onBindViewHolder(PreferenceViewHolder holder)78     void onBindViewHolder(PreferenceViewHolder holder) {
79         mIcon = holder.findViewById(R.id.icon_frame);
80         mText = holder.findViewById(R.id.text_frame);
81         setIconVisible(mIconVisible);
82         setIconPaddingStart(mIconPaddingStart);
83         setTextPaddingStart(mTextPaddingStart);
84     }
85 }
86