1 /* 2 * Copyright (C) 2021 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.settingslib.widget; 18 19 import android.content.Context; 20 import android.util.AttributeSet; 21 import android.view.View; 22 import android.widget.ImageView; 23 import android.widget.LinearLayout; 24 25 import androidx.annotation.IntDef; 26 import androidx.preference.Preference; 27 import androidx.preference.PreferenceViewHolder; 28 29 import java.lang.annotation.Retention; 30 import java.lang.annotation.RetentionPolicy; 31 32 /** 33 * The Base preference with two target areas divided by a vertical divider 34 */ 35 public class TwoTargetPreference extends Preference { 36 37 @IntDef({ICON_SIZE_DEFAULT, ICON_SIZE_MEDIUM, ICON_SIZE_SMALL}) 38 @Retention(RetentionPolicy.SOURCE) 39 public @interface IconSize { 40 } 41 42 public static final int ICON_SIZE_DEFAULT = 0; 43 public static final int ICON_SIZE_MEDIUM = 1; 44 public static final int ICON_SIZE_SMALL = 2; 45 46 @IconSize 47 private int mIconSize; 48 private int mSmallIconSize; 49 private int mMediumIconSize; 50 TwoTargetPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)51 public TwoTargetPreference(Context context, AttributeSet attrs, 52 int defStyleAttr, int defStyleRes) { 53 super(context, attrs, defStyleAttr, defStyleRes); 54 init(context); 55 } 56 TwoTargetPreference(Context context, AttributeSet attrs, int defStyleAttr)57 public TwoTargetPreference(Context context, AttributeSet attrs, int defStyleAttr) { 58 super(context, attrs, defStyleAttr); 59 init(context); 60 } 61 TwoTargetPreference(Context context, AttributeSet attrs)62 public TwoTargetPreference(Context context, AttributeSet attrs) { 63 super(context, attrs); 64 init(context); 65 } 66 TwoTargetPreference(Context context)67 public TwoTargetPreference(Context context) { 68 super(context); 69 init(context); 70 } 71 init(Context context)72 private void init(Context context) { 73 setLayoutResource(R.layout.preference_two_target); 74 mSmallIconSize = context.getResources().getDimensionPixelSize( 75 R.dimen.two_target_pref_small_icon_size); 76 mMediumIconSize = context.getResources().getDimensionPixelSize( 77 R.dimen.two_target_pref_medium_icon_size); 78 final int secondTargetResId = getSecondTargetResId(); 79 if (secondTargetResId != 0) { 80 setWidgetLayoutResource(secondTargetResId); 81 } 82 } 83 setIconSize(@conSize int iconSize)84 public void setIconSize(@IconSize int iconSize) { 85 mIconSize = iconSize; 86 } 87 88 @Override onBindViewHolder(PreferenceViewHolder holder)89 public void onBindViewHolder(PreferenceViewHolder holder) { 90 super.onBindViewHolder(holder); 91 final ImageView icon = holder.itemView.findViewById(android.R.id.icon); 92 switch (mIconSize) { 93 case ICON_SIZE_SMALL: 94 icon.setLayoutParams(new LinearLayout.LayoutParams(mSmallIconSize, mSmallIconSize)); 95 break; 96 case ICON_SIZE_MEDIUM: 97 icon.setLayoutParams( 98 new LinearLayout.LayoutParams(mMediumIconSize, mMediumIconSize)); 99 break; 100 } 101 final View divider = holder.findViewById(R.id.two_target_divider); 102 final View widgetFrame = holder.findViewById(android.R.id.widget_frame); 103 final boolean shouldHideSecondTarget = shouldHideSecondTarget(); 104 if (divider != null) { 105 divider.setVisibility(shouldHideSecondTarget ? View.GONE : View.VISIBLE); 106 } 107 if (widgetFrame != null) { 108 widgetFrame.setVisibility(shouldHideSecondTarget ? View.GONE : View.VISIBLE); 109 } 110 } 111 shouldHideSecondTarget()112 protected boolean shouldHideSecondTarget() { 113 return getSecondTargetResId() == 0; 114 } 115 getSecondTargetResId()116 protected int getSecondTargetResId() { 117 return 0; 118 } 119 } 120