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