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