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